blob: 73a63bf1d2a42035f9dbdbd0753fc77cf720be94 [file] [log] [blame]
Yann Collet71eafdd2016-02-12 02:31:57 +01001/*
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 Collet71eafdd2016-02-12 02:31:57 +010025/*-*************************************
26* Includes
27***************************************/
inikep13c84242016-05-05 13:58:56 +020028#include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_getTotalFileSize */
Yann Collet71eafdd2016-02-12 02:31:57 +010029#include <stdlib.h> /* malloc, free */
30#include <string.h> /* memset */
31#include <stdio.h> /* fprintf, fopen, ftello64 */
inikep37337972016-05-10 14:22:55 +020032#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
Yann Collet71eafdd2016-02-12 02:31:57 +010033
34#include "mem.h" /* read */
35#include "error_private.h"
inikep23a08892016-04-22 12:43:18 +020036#include "dibio.h"
Yann Collet71eafdd2016-02-12 02:31:57 +010037
38
39/*-*************************************
40* Constants
41***************************************/
42#define KB *(1 <<10)
43#define MB *(1 <<20)
44#define GB *(1U<<30)
45
Yann Collet71eafdd2016-02-12 02:31:57 +010046#define MEMMULT 11
47static 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 Collet71eafdd2016-02-12 02:31:57 +010050
51
52/*-*************************************
53* Console display
54***************************************/
55#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
56#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
57static unsigned g_displayLevel = 0; /* 0 : no display; 1: errors; 2: default; 4: full information */
Yann Collet71eafdd2016-02-12 02:31:57 +010058
Yann Colletf6ca09b2016-05-09 04:44:45 +020059#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletbcb5f772016-07-06 15:41:03 +020060 if ((DIB_clockSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
Yann Colletf6ca09b2016-05-09 04:44:45 +020061 { g_time = clock(); DISPLAY(__VA_ARGS__); \
62 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletbcb5f772016-07-06 15:41:03 +020063static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10;
Yann Colletf6ca09b2016-05-09 04:44:45 +020064static clock_t g_time = 0;
65
Yann Colletbcb5f772016-07-06 15:41:03 +020066static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; }
Yann Colletf6ca09b2016-05-09 04:44:45 +020067
Yann Collet71eafdd2016-02-12 02:31:57 +010068
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**********************************************************/
89unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); }
90
91const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
92
Yann Colletbcb5f772016-07-06 15:41:03 +020093#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
94
Yann Collet71eafdd2016-02-12 02:31:57 +010095
96/* ********************************************************
97* File related operations
98**********************************************************/
Yann Collet290aaa72016-05-30 21:18:52 +020099/** DiB_loadFiles() :
100* @return : nb of files effectively loaded into `buffer` */
Yann Colletbcb5f772016-07-06 15:41:03 +0200101static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
Yann Collet290aaa72016-05-30 21:18:52 +0200102 size_t* fileSizes,
103 const char** fileNamesTable, unsigned nbFiles)
Yann Collet71eafdd2016-02-12 02:31:57 +0100104{
Yann Collet290aaa72016-05-30 21:18:52 +0200105 char* const buff = (char*)buffer;
Yann Collet71eafdd2016-02-12 02:31:57 +0100106 size_t pos = 0;
107 unsigned n;
108
109 for (n=0; n<nbFiles; n++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200110 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 Collet290aaa72016-05-30 21:18:52 +0200124 return n;
Yann Collet71eafdd2016-02-12 02:31:57 +0100125}
126
127
128/*-********************************************************
129* Dictionary training functions
130**********************************************************/
131static size_t DiB_findMaxMem(unsigned long long requiredMem)
132{
Yann Collet290aaa72016-05-30 21:18:52 +0200133 size_t const step = 8 MB;
Yann Collet71eafdd2016-02-12 02:31:57 +0100134 void* testmem = NULL;
135
136 requiredMem = (((requiredMem >> 23) + 1) << 23);
Yann Colletbcb5f772016-07-06 15:41:03 +0200137 requiredMem += step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100138 if (requiredMem > maxMemory) requiredMem = maxMemory;
139
140 while (!testmem) {
Yann Collet71eafdd2016-02-12 02:31:57 +0100141 testmem = malloc((size_t)requiredMem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200142 requiredMem -= step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100143 }
144
145 free(testmem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200146 return (size_t)requiredMem;
Yann Collet71eafdd2016-02-12 02:31:57 +0100147}
148
149
150static void DiB_fillNoise(void* buffer, size_t length)
151{
Yann Colletbcb5f772016-07-06 15:41:03 +0200152 unsigned const prime1 = 2654435761U;
153 unsigned const prime2 = 2246822519U;
154 unsigned acc = prime1;
Yann Collet71eafdd2016-02-12 02:31:57 +0100155 size_t p=0;;
156
157 for (p=0; p<length; p++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200158 acc *= prime2;
Yann Collet71eafdd2016-02-12 02:31:57 +0100159 ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
160 }
161}
162
163
164static void DiB_saveDict(const char* dictFileName,
165 const void* buff, size_t buffSize)
166{
Yann Collet290aaa72016-05-30 21:18:52 +0200167 FILE* const f = fopen(dictFileName, "wb");
Yann Collet71eafdd2016-02-12 02:31:57 +0100168 if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
169
Yann Colletf6ca09b2016-05-09 04:44:45 +0200170 { size_t const n = fwrite(buff, 1, buffSize, f);
171 if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100172
Yann Colletf6ca09b2016-05-09 04:44:45 +0200173 { size_t const n = (size_t)fclose(f);
174 if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100175}
176
177
Yann Collet6f3acba2016-02-12 20:19:48 +0100178/*! 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*/
185size_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 Collet71eafdd2016-02-12 02:31:57 +0100190int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
191 const char** fileNamesTable, unsigned nbFiles,
192 ZDICT_params_t params)
193{
Yann Collet290aaa72016-05-30 21:18:52 +0200194 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 Colletbcb5f772016-07-06 15:41:03 +0200198 size_t benchedSize = MIN (maxMem, (size_t)totalSizeToLoad);
Yann Collet290aaa72016-05-30 21:18:52 +0200199 void* const srcBuffer = malloc(benchedSize+NOISELENGTH);
Yann Collet71eafdd2016-02-12 02:31:57 +0100200 int result = 0;
201
Yann Collet290aaa72016-05-30 21:18:52 +0200202 /* Checks */
203 if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
204
Yann Collet71eafdd2016-02-12 02:31:57 +0100205 /* init */
Yann Collet6f3acba2016-02-12 20:19:48 +0100206 g_displayLevel = params.notificationLevel;
Yann Collet71eafdd2016-02-12 02:31:57 +0100207 if (benchedSize < totalSizeToLoad)
208 DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20));
209
Yann Collet71eafdd2016-02-12 02:31:57 +0100210 /* Load input buffer */
Yann Colletbcb5f772016-07-06 15:41:03 +0200211 nbFiles = DiB_loadFiles(srcBuffer, &benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Collet71eafdd2016-02-12 02:31:57 +0100212 DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */
213
Yann Collet290aaa72016-05-30 21:18:52 +0200214 { 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 Collet71eafdd2016-02-12 02:31:57 +0100225 }
226
Yann Collet71eafdd2016-02-12 02:31:57 +0100227 /* clean up */
228_cleanup:
229 free(srcBuffer);
230 free(dictBuffer);
231 free(fileSizes);
232 return result;
233}