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