Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | dibio - I/O API for dictionary builder |
| 3 | Copyright (C) Yann Collet 2016 |
| 4 | |
| 5 | GPL v2 License |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along |
| 18 | with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | You can contact the author at : |
| 22 | - zstd homepage : http://www.zstd.net/ |
| 23 | */ |
| 24 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 25 | /*-************************************* |
| 26 | * Includes |
| 27 | ***************************************/ |
inikep | 13c8424 | 2016-05-05 13:58:56 +0200 | [diff] [blame] | 28 | #include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_getTotalFileSize */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 29 | #include <stdlib.h> /* malloc, free */ |
| 30 | #include <string.h> /* memset */ |
| 31 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
inikep | 3733797 | 2016-05-10 14:22:55 +0200 | [diff] [blame] | 32 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 33 | |
| 34 | #include "mem.h" /* read */ |
| 35 | #include "error_private.h" |
inikep | 23a0889 | 2016-04-22 12:43:18 +0200 | [diff] [blame] | 36 | #include "dibio.h" |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 37 | |
| 38 | |
| 39 | /*-************************************* |
| 40 | * Constants |
| 41 | ***************************************/ |
| 42 | #define KB *(1 <<10) |
| 43 | #define MB *(1 <<20) |
| 44 | #define GB *(1U<<30) |
| 45 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 46 | #define MEMMULT 11 |
| 47 | static const size_t maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t)); |
| 48 | |
| 49 | #define NOISELENGTH 32 |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 50 | |
| 51 | |
| 52 | /*-************************************* |
| 53 | * Console display |
| 54 | ***************************************/ |
| 55 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 56 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 57 | static unsigned g_displayLevel = 0; /* 0 : no display; 1: errors; 2: default; 4: full information */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 58 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 59 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 60 | if ((DIB_clockSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 61 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 62 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 63 | static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10; |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 64 | static clock_t g_time = 0; |
| 65 | |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 66 | static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; } |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 67 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 68 | |
| 69 | /*-************************************* |
| 70 | * Exceptions |
| 71 | ***************************************/ |
| 72 | #ifndef DEBUG |
| 73 | # define DEBUG 0 |
| 74 | #endif |
| 75 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 76 | #define EXM_THROW(error, ...) \ |
| 77 | { \ |
| 78 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 79 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 80 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 81 | DISPLAYLEVEL(1, "\n"); \ |
| 82 | exit(error); \ |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* ******************************************************** |
| 87 | * Helper functions |
| 88 | **********************************************************/ |
| 89 | unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); } |
| 90 | |
| 91 | const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } |
| 92 | |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 93 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 94 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 95 | |
| 96 | /* ******************************************************** |
| 97 | * File related operations |
| 98 | **********************************************************/ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 99 | /** DiB_loadFiles() : |
| 100 | * @return : nb of files effectively loaded into `buffer` */ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 101 | static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr, |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 102 | size_t* fileSizes, |
| 103 | const char** fileNamesTable, unsigned nbFiles) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 104 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 105 | char* const buff = (char*)buffer; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 106 | size_t pos = 0; |
| 107 | unsigned n; |
| 108 | |
| 109 | for (n=0; n<nbFiles; n++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 110 | const char* const fileName = fileNamesTable[n]; |
| 111 | unsigned long long const fs64 = UTIL_getFileSize(fileName); |
| 112 | size_t const fileSize = (size_t) MIN(fs64, 128 KB); |
| 113 | if (fileSize > *bufferSizePtr-pos) break; |
| 114 | { FILE* const f = fopen(fileName, "rb"); |
| 115 | if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno)); |
| 116 | DISPLAYUPDATE(2, "Loading %s... \r", fileName); |
| 117 | { size_t const readSize = fread(buff+pos, 1, fileSize, f); |
| 118 | if (readSize != fileSize) EXM_THROW(11, "Pb reading %s", fileName); |
| 119 | pos += readSize; } |
| 120 | fileSizes[n] = fileSize; |
| 121 | fclose(f); |
| 122 | } } |
| 123 | *bufferSizePtr = pos; |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 124 | return n; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | /*-******************************************************** |
| 129 | * Dictionary training functions |
| 130 | **********************************************************/ |
| 131 | static size_t DiB_findMaxMem(unsigned long long requiredMem) |
| 132 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 133 | size_t const step = 8 MB; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 134 | void* testmem = NULL; |
| 135 | |
| 136 | requiredMem = (((requiredMem >> 23) + 1) << 23); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 137 | requiredMem += step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 138 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
| 139 | |
| 140 | while (!testmem) { |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 141 | testmem = malloc((size_t)requiredMem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 142 | requiredMem -= step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | free(testmem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 146 | return (size_t)requiredMem; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | static void DiB_fillNoise(void* buffer, size_t length) |
| 151 | { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 152 | unsigned const prime1 = 2654435761U; |
| 153 | unsigned const prime2 = 2246822519U; |
| 154 | unsigned acc = prime1; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 155 | size_t p=0;; |
| 156 | |
| 157 | for (p=0; p<length; p++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 158 | acc *= prime2; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 159 | ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static void DiB_saveDict(const char* dictFileName, |
| 165 | const void* buff, size_t buffSize) |
| 166 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 167 | FILE* const f = fopen(dictFileName, "wb"); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 168 | if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName); |
| 169 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 170 | { size_t const n = fwrite(buff, 1, buffSize, f); |
| 171 | if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 172 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 173 | { size_t const n = (size_t)fclose(f); |
| 174 | if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 178 | /*! ZDICT_trainFromBuffer_unsafe() : |
| 179 | Strictly Internal use only !! |
| 180 | Same as ZDICT_trainFromBuffer_advanced(), but does not control `samplesBuffer`. |
| 181 | `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads. |
| 182 | @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 183 | or an error code. |
| 184 | */ |
| 185 | size_t ZDICT_trainFromBuffer_unsafe(void* dictBuffer, size_t dictBufferCapacity, |
| 186 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, |
| 187 | ZDICT_params_t parameters); |
| 188 | |
| 189 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 190 | int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize, |
| 191 | const char** fileNamesTable, unsigned nbFiles, |
| 192 | ZDICT_params_t params) |
| 193 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 194 | void* const dictBuffer = malloc(maxDictSize); |
| 195 | size_t* const fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
| 196 | unsigned long long const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); |
| 197 | size_t const maxMem = DiB_findMaxMem(totalSizeToLoad * MEMMULT) / MEMMULT; |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 198 | size_t benchedSize = MIN (maxMem, (size_t)totalSizeToLoad); |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 199 | void* const srcBuffer = malloc(benchedSize+NOISELENGTH); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 200 | int result = 0; |
| 201 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 202 | /* Checks */ |
| 203 | if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */ |
| 204 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 205 | /* init */ |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 206 | g_displayLevel = params.notificationLevel; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 207 | if (benchedSize < totalSizeToLoad) |
| 208 | DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20)); |
| 209 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 210 | /* Load input buffer */ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 211 | nbFiles = DiB_loadFiles(srcBuffer, &benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 212 | DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */ |
| 213 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 214 | { size_t const dictSize = ZDICT_trainFromBuffer_unsafe(dictBuffer, maxDictSize, |
| 215 | srcBuffer, fileSizes, nbFiles, |
| 216 | params); |
| 217 | if (ZDICT_isError(dictSize)) { |
| 218 | DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */ |
| 219 | result = 1; |
| 220 | goto _cleanup; |
| 221 | } |
| 222 | /* save dict */ |
| 223 | DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName); |
| 224 | DiB_saveDict(dictFileName, dictBuffer, dictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 227 | /* clean up */ |
| 228 | _cleanup: |
| 229 | free(srcBuffer); |
| 230 | free(dictBuffer); |
| 231 | free(fileSizes); |
| 232 | return result; |
| 233 | } |