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