Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 9 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 10 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 11 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 12 | /*-************************************* |
| 13 | * Includes |
| 14 | ***************************************/ |
inikep | 13c8424 | 2016-05-05 13:58:56 +0200 | [diff] [blame] | 15 | #include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_getTotalFileSize */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 16 | #include <stdlib.h> /* malloc, free */ |
| 17 | #include <string.h> /* memset */ |
| 18 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
inikep | 3733797 | 2016-05-10 14:22:55 +0200 | [diff] [blame] | 19 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ |
Yann Collet | a3d03a3 | 2016-07-06 16:27:17 +0200 | [diff] [blame] | 20 | #include <errno.h> /* errno */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 21 | |
| 22 | #include "mem.h" /* read */ |
| 23 | #include "error_private.h" |
inikep | 23a0889 | 2016-04-22 12:43:18 +0200 | [diff] [blame] | 24 | #include "dibio.h" |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 25 | |
| 26 | |
| 27 | /*-************************************* |
| 28 | * Constants |
| 29 | ***************************************/ |
| 30 | #define KB *(1 <<10) |
| 31 | #define MB *(1 <<20) |
| 32 | #define GB *(1U<<30) |
| 33 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame^] | 34 | #define SAMPLESIZE_MAX (128 KB) |
| 35 | #define MEMMULT 11 /* rough estimation : memory cost to analyze 1 byte of sample */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 36 | static const size_t maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((size_t)(512 MB) << sizeof(size_t)); |
| 37 | |
| 38 | #define NOISELENGTH 32 |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /*-************************************* |
| 42 | * Console display |
| 43 | ***************************************/ |
| 44 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 45 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 46 | 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] | 47 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 48 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 49 | if ((DIB_clockSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 50 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 51 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 52 | static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10; |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 53 | static clock_t g_time = 0; |
| 54 | |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 55 | static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; } |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 56 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 57 | |
| 58 | /*-************************************* |
| 59 | * Exceptions |
| 60 | ***************************************/ |
| 61 | #ifndef DEBUG |
| 62 | # define DEBUG 0 |
| 63 | #endif |
| 64 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 65 | #define EXM_THROW(error, ...) \ |
| 66 | { \ |
| 67 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 68 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 69 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 70 | DISPLAYLEVEL(1, "\n"); \ |
| 71 | exit(error); \ |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* ******************************************************** |
| 76 | * Helper functions |
| 77 | **********************************************************/ |
| 78 | unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); } |
| 79 | |
| 80 | const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } |
| 81 | |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 82 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 83 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 84 | |
| 85 | /* ******************************************************** |
| 86 | * File related operations |
| 87 | **********************************************************/ |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 88 | /** DiB_loadFiles() : |
| 89 | * @return : nb of files effectively loaded into `buffer` */ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 90 | static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr, |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 91 | size_t* fileSizes, |
| 92 | const char** fileNamesTable, unsigned nbFiles) |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 93 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 94 | char* const buff = (char*)buffer; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 95 | size_t pos = 0; |
| 96 | unsigned n; |
| 97 | |
| 98 | for (n=0; n<nbFiles; n++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 99 | const char* const fileName = fileNamesTable[n]; |
| 100 | unsigned long long const fs64 = UTIL_getFileSize(fileName); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame^] | 101 | size_t const fileSize = (size_t) MIN(fs64, SAMPLESIZE_MAX); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 102 | if (fileSize > *bufferSizePtr-pos) break; |
| 103 | { FILE* const f = fopen(fileName, "rb"); |
| 104 | if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno)); |
| 105 | DISPLAYUPDATE(2, "Loading %s... \r", fileName); |
| 106 | { size_t const readSize = fread(buff+pos, 1, fileSize, f); |
| 107 | if (readSize != fileSize) EXM_THROW(11, "Pb reading %s", fileName); |
| 108 | pos += readSize; } |
| 109 | fileSizes[n] = fileSize; |
| 110 | fclose(f); |
| 111 | } } |
| 112 | *bufferSizePtr = pos; |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 113 | return n; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | /*-******************************************************** |
| 118 | * Dictionary training functions |
| 119 | **********************************************************/ |
| 120 | static size_t DiB_findMaxMem(unsigned long long requiredMem) |
| 121 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 122 | size_t const step = 8 MB; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 123 | void* testmem = NULL; |
| 124 | |
| 125 | requiredMem = (((requiredMem >> 23) + 1) << 23); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 126 | requiredMem += step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 127 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
| 128 | |
| 129 | while (!testmem) { |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 130 | testmem = malloc((size_t)requiredMem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 131 | requiredMem -= step; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | free(testmem); |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 135 | return (size_t)requiredMem; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | static void DiB_fillNoise(void* buffer, size_t length) |
| 140 | { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 141 | unsigned const prime1 = 2654435761U; |
| 142 | unsigned const prime2 = 2246822519U; |
| 143 | unsigned acc = prime1; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 144 | size_t p=0;; |
| 145 | |
| 146 | for (p=0; p<length; p++) { |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 147 | acc *= prime2; |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 148 | ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | static void DiB_saveDict(const char* dictFileName, |
| 154 | const void* buff, size_t buffSize) |
| 155 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 156 | FILE* const f = fopen(dictFileName, "wb"); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 157 | if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName); |
| 158 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 159 | { size_t const n = fwrite(buff, 1, buffSize, f); |
| 160 | if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 161 | |
Yann Collet | f6ca09b | 2016-05-09 04:44:45 +0200 | [diff] [blame] | 162 | { size_t const n = (size_t)fclose(f); |
| 163 | if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) } |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame^] | 167 | static int g_tooLargeSamples = 0; |
| 168 | static U64 DiB_getTotalCappedFileSize(const char** fileNamesTable, unsigned nbFiles) |
| 169 | { |
| 170 | U64 total = 0; |
| 171 | unsigned n; |
| 172 | for (n=0; n<nbFiles; n++) { |
| 173 | U64 const fileSize = UTIL_getFileSize(fileNamesTable[n]); |
| 174 | U64 const cappedFileSize = MIN(fileSize, SAMPLESIZE_MAX); |
| 175 | total += cappedFileSize; |
| 176 | g_tooLargeSamples |= (fileSize > 2*SAMPLESIZE_MAX); |
| 177 | } |
| 178 | return total; |
| 179 | } |
| 180 | |
| 181 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 182 | /*! ZDICT_trainFromBuffer_unsafe() : |
| 183 | Strictly Internal use only !! |
| 184 | Same as ZDICT_trainFromBuffer_advanced(), but does not control `samplesBuffer`. |
| 185 | `samplesBuffer` must be followed by noisy guard band to avoid out-of-buffer reads. |
| 186 | @return : size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 187 | or an error code. |
| 188 | */ |
| 189 | size_t ZDICT_trainFromBuffer_unsafe(void* dictBuffer, size_t dictBufferCapacity, |
| 190 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, |
| 191 | ZDICT_params_t parameters); |
| 192 | |
| 193 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 194 | int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize, |
| 195 | const char** fileNamesTable, unsigned nbFiles, |
| 196 | ZDICT_params_t params) |
| 197 | { |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 198 | void* const dictBuffer = malloc(maxDictSize); |
| 199 | size_t* const fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame^] | 200 | unsigned long long const totalSizeToLoad = DiB_getTotalCappedFileSize(fileNamesTable, nbFiles); |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 201 | size_t const maxMem = DiB_findMaxMem(totalSizeToLoad * MEMMULT) / MEMMULT; |
Yann Collet | d46ecb5 | 2016-12-17 16:28:12 +0100 | [diff] [blame] | 202 | size_t benchedSize = (size_t) MIN ((unsigned long long)maxMem, totalSizeToLoad); |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 203 | void* const srcBuffer = malloc(benchedSize+NOISELENGTH); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 204 | int result = 0; |
| 205 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 206 | /* Checks */ |
| 207 | if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */ |
Yann Collet | dd25a27 | 2016-07-27 12:35:29 +0200 | [diff] [blame] | 208 | g_displayLevel = params.notificationLevel; |
Yann Collet | 1496c3d | 2016-12-18 11:58:23 +0100 | [diff] [blame^] | 209 | if (g_tooLargeSamples) { |
| 210 | DISPLAYLEVEL(2, "! Warning : some samples are very large \n"); |
| 211 | DISPLAYLEVEL(2, "! Note that dictionary is only useful for small files or beginning of large files. \n"); |
| 212 | DISPLAYLEVEL(2, "! As a consequence, only the first %u bytes of each file are loaded \n", SAMPLESIZE_MAX); |
| 213 | } |
| 214 | if ((nbFiles < 5) || (totalSizeToLoad < 9 * (unsigned long long)maxDictSize)) { |
Yann Collet | 49d105c | 2016-08-18 15:02:11 +0200 | [diff] [blame] | 215 | DISPLAYLEVEL(2, "! Warning : nb of samples too low for proper processing ! \n"); |
| 216 | DISPLAYLEVEL(2, "! Please provide _one file per sample_. \n"); |
| 217 | DISPLAYLEVEL(2, "! Do not concatenate samples together into a single file, \n"); |
| 218 | DISPLAYLEVEL(2, "! as dictBuilder will be unable to find the beginning of each sample, \n"); |
| 219 | DISPLAYLEVEL(2, "! resulting in poor dictionary quality. \n"); |
Yann Collet | dd25a27 | 2016-07-27 12:35:29 +0200 | [diff] [blame] | 220 | } |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 221 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 222 | /* init */ |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 223 | if (benchedSize < totalSizeToLoad) |
| 224 | DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20)); |
| 225 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 226 | /* Load input buffer */ |
Yann Collet | bcb5f77 | 2016-07-06 15:41:03 +0200 | [diff] [blame] | 227 | nbFiles = DiB_loadFiles(srcBuffer, &benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 228 | DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */ |
| 229 | |
Yann Collet | 290aaa7 | 2016-05-30 21:18:52 +0200 | [diff] [blame] | 230 | { size_t const dictSize = ZDICT_trainFromBuffer_unsafe(dictBuffer, maxDictSize, |
| 231 | srcBuffer, fileSizes, nbFiles, |
| 232 | params); |
| 233 | if (ZDICT_isError(dictSize)) { |
| 234 | DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */ |
| 235 | result = 1; |
| 236 | goto _cleanup; |
| 237 | } |
| 238 | /* save dict */ |
| 239 | DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName); |
| 240 | DiB_saveDict(dictFileName, dictBuffer, dictSize); |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Yann Collet | 71eafdd | 2016-02-12 02:31:57 +0100 | [diff] [blame] | 243 | /* clean up */ |
| 244 | _cleanup: |
| 245 | free(srcBuffer); |
| 246 | free(dictBuffer); |
| 247 | free(fileSizes); |
| 248 | return result; |
| 249 | } |