blob: a61ea9cc6a4c1f61827fc11f3fcd84120eb94380 [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 Colleta3d03a32016-07-06 16:27:17 +020033#include <errno.h> /* errno */
Yann Collet71eafdd2016-02-12 02:31:57 +010034
35#include "mem.h" /* read */
36#include "error_private.h"
inikep23a08892016-04-22 12:43:18 +020037#include "dibio.h"
Yann Collet71eafdd2016-02-12 02:31:57 +010038
39
40/*-*************************************
41* Constants
42***************************************/
43#define KB *(1 <<10)
44#define MB *(1 <<20)
45#define GB *(1U<<30)
46
Yann Collet71eafdd2016-02-12 02:31:57 +010047#define MEMMULT 11
48static 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 Collet71eafdd2016-02-12 02:31:57 +010051
52
53/*-*************************************
54* Console display
55***************************************/
56#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
57#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
58static unsigned g_displayLevel = 0; /* 0 : no display; 1: errors; 2: default; 4: full information */
Yann Collet71eafdd2016-02-12 02:31:57 +010059
Yann Colletf6ca09b2016-05-09 04:44:45 +020060#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletbcb5f772016-07-06 15:41:03 +020061 if ((DIB_clockSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
Yann Colletf6ca09b2016-05-09 04:44:45 +020062 { g_time = clock(); DISPLAY(__VA_ARGS__); \
63 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletbcb5f772016-07-06 15:41:03 +020064static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10;
Yann Colletf6ca09b2016-05-09 04:44:45 +020065static clock_t g_time = 0;
66
Yann Colletbcb5f772016-07-06 15:41:03 +020067static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; }
Yann Colletf6ca09b2016-05-09 04:44:45 +020068
Yann Collet71eafdd2016-02-12 02:31:57 +010069
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**********************************************************/
90unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); }
91
92const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
93
Yann Colletbcb5f772016-07-06 15:41:03 +020094#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
95
Yann Collet71eafdd2016-02-12 02:31:57 +010096
97/* ********************************************************
98* File related operations
99**********************************************************/
Yann Collet290aaa72016-05-30 21:18:52 +0200100/** DiB_loadFiles() :
101* @return : nb of files effectively loaded into `buffer` */
Yann Colletbcb5f772016-07-06 15:41:03 +0200102static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
Yann Collet290aaa72016-05-30 21:18:52 +0200103 size_t* fileSizes,
104 const char** fileNamesTable, unsigned nbFiles)
Yann Collet71eafdd2016-02-12 02:31:57 +0100105{
Yann Collet290aaa72016-05-30 21:18:52 +0200106 char* const buff = (char*)buffer;
Yann Collet71eafdd2016-02-12 02:31:57 +0100107 size_t pos = 0;
108 unsigned n;
109
110 for (n=0; n<nbFiles; n++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200111 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 Collet290aaa72016-05-30 21:18:52 +0200125 return n;
Yann Collet71eafdd2016-02-12 02:31:57 +0100126}
127
128
129/*-********************************************************
130* Dictionary training functions
131**********************************************************/
132static size_t DiB_findMaxMem(unsigned long long requiredMem)
133{
Yann Collet290aaa72016-05-30 21:18:52 +0200134 size_t const step = 8 MB;
Yann Collet71eafdd2016-02-12 02:31:57 +0100135 void* testmem = NULL;
136
137 requiredMem = (((requiredMem >> 23) + 1) << 23);
Yann Colletbcb5f772016-07-06 15:41:03 +0200138 requiredMem += step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100139 if (requiredMem > maxMemory) requiredMem = maxMemory;
140
141 while (!testmem) {
Yann Collet71eafdd2016-02-12 02:31:57 +0100142 testmem = malloc((size_t)requiredMem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200143 requiredMem -= step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100144 }
145
146 free(testmem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200147 return (size_t)requiredMem;
Yann Collet71eafdd2016-02-12 02:31:57 +0100148}
149
150
151static void DiB_fillNoise(void* buffer, size_t length)
152{
Yann Colletbcb5f772016-07-06 15:41:03 +0200153 unsigned const prime1 = 2654435761U;
154 unsigned const prime2 = 2246822519U;
155 unsigned acc = prime1;
Yann Collet71eafdd2016-02-12 02:31:57 +0100156 size_t p=0;;
157
158 for (p=0; p<length; p++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200159 acc *= prime2;
Yann Collet71eafdd2016-02-12 02:31:57 +0100160 ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
161 }
162}
163
164
165static void DiB_saveDict(const char* dictFileName,
166 const void* buff, size_t buffSize)
167{
Yann Collet290aaa72016-05-30 21:18:52 +0200168 FILE* const f = fopen(dictFileName, "wb");
Yann Collet71eafdd2016-02-12 02:31:57 +0100169 if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
170
Yann Colletf6ca09b2016-05-09 04:44:45 +0200171 { size_t const n = fwrite(buff, 1, buffSize, f);
172 if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100173
Yann Colletf6ca09b2016-05-09 04:44:45 +0200174 { size_t const n = (size_t)fclose(f);
175 if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100176}
177
178
Yann Collet6f3acba2016-02-12 20:19:48 +0100179/*! 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*/
186size_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 Collet71eafdd2016-02-12 02:31:57 +0100191int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
192 const char** fileNamesTable, unsigned nbFiles,
193 ZDICT_params_t params)
194{
Yann Collet290aaa72016-05-30 21:18:52 +0200195 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 Colletbcb5f772016-07-06 15:41:03 +0200199 size_t benchedSize = MIN (maxMem, (size_t)totalSizeToLoad);
Yann Collet290aaa72016-05-30 21:18:52 +0200200 void* const srcBuffer = malloc(benchedSize+NOISELENGTH);
Yann Collet71eafdd2016-02-12 02:31:57 +0100201 int result = 0;
202
Yann Collet290aaa72016-05-30 21:18:52 +0200203 /* Checks */
204 if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
205
Yann Collet71eafdd2016-02-12 02:31:57 +0100206 /* init */
Yann Collet6f3acba2016-02-12 20:19:48 +0100207 g_displayLevel = params.notificationLevel;
Yann Collet71eafdd2016-02-12 02:31:57 +0100208 if (benchedSize < totalSizeToLoad)
209 DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20));
210
Yann Collet71eafdd2016-02-12 02:31:57 +0100211 /* Load input buffer */
Yann Colletbcb5f772016-07-06 15:41:03 +0200212 nbFiles = DiB_loadFiles(srcBuffer, &benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Collet71eafdd2016-02-12 02:31:57 +0100213 DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */
214
Yann Collet290aaa72016-05-30 21:18:52 +0200215 { size_t const dictSize = ZDICT_trainFromBuffer_unsafe(dictBuffer, maxDictSize,
216 srcBuffer, fileSizes, nbFiles,
217 params);
218 if (ZDICT_isError(dictSize)) {
219 DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
220 result = 1;
221 goto _cleanup;
222 }
223 /* save dict */
224 DISPLAYLEVEL(2, "Save dictionary of size %u into file %s \n", (U32)dictSize, dictFileName);
225 DiB_saveDict(dictFileName, dictBuffer, dictSize);
Yann Collet71eafdd2016-02-12 02:31:57 +0100226 }
227
Yann Collet71eafdd2016-02-12 02:31:57 +0100228 /* clean up */
229_cleanup:
230 free(srcBuffer);
231 free(dictBuffer);
232 free(fileSizes);
233 return result;
234}