blob: a793669cb8fb6eef6475c7cd8eb50b0c5bfa2af0 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
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 Collet71eafdd2016-02-12 02:31:57 +01009
Yann Collet71eafdd2016-02-12 02:31:57 +010010
Yann Collet71eafdd2016-02-12 02:31:57 +010011
Yann Collet71eafdd2016-02-12 02:31:57 +010012/*-*************************************
13* Includes
14***************************************/
inikep13c84242016-05-05 13:58:56 +020015#include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_getTotalFileSize */
Yann Collet71eafdd2016-02-12 02:31:57 +010016#include <stdlib.h> /* malloc, free */
17#include <string.h> /* memset */
18#include <stdio.h> /* fprintf, fopen, ftello64 */
inikep37337972016-05-10 14:22:55 +020019#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
Yann Colleta3d03a32016-07-06 16:27:17 +020020#include <errno.h> /* errno */
Yann Collet71eafdd2016-02-12 02:31:57 +010021
22#include "mem.h" /* read */
23#include "error_private.h"
inikep23a08892016-04-22 12:43:18 +020024#include "dibio.h"
Yann Collet71eafdd2016-02-12 02:31:57 +010025
26
27/*-*************************************
28* Constants
29***************************************/
30#define KB *(1 <<10)
31#define MB *(1 <<20)
32#define GB *(1U<<30)
33
Yann Collet71eafdd2016-02-12 02:31:57 +010034#define MEMMULT 11
35static 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 Collet71eafdd2016-02-12 02:31:57 +010038
39
40/*-*************************************
41* Console display
42***************************************/
43#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
44#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
45static unsigned g_displayLevel = 0; /* 0 : no display; 1: errors; 2: default; 4: full information */
Yann Collet71eafdd2016-02-12 02:31:57 +010046
Yann Colletf6ca09b2016-05-09 04:44:45 +020047#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletbcb5f772016-07-06 15:41:03 +020048 if ((DIB_clockSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
Yann Colletf6ca09b2016-05-09 04:44:45 +020049 { g_time = clock(); DISPLAY(__VA_ARGS__); \
50 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletbcb5f772016-07-06 15:41:03 +020051static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10;
Yann Colletf6ca09b2016-05-09 04:44:45 +020052static clock_t g_time = 0;
53
Yann Colletbcb5f772016-07-06 15:41:03 +020054static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; }
Yann Colletf6ca09b2016-05-09 04:44:45 +020055
Yann Collet71eafdd2016-02-12 02:31:57 +010056
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**********************************************************/
77unsigned DiB_isError(size_t errorCode) { return ERR_isError(errorCode); }
78
79const char* DiB_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
80
Yann Colletbcb5f772016-07-06 15:41:03 +020081#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
82
Yann Collet71eafdd2016-02-12 02:31:57 +010083
84/* ********************************************************
85* File related operations
86**********************************************************/
Yann Collet290aaa72016-05-30 21:18:52 +020087/** DiB_loadFiles() :
88* @return : nb of files effectively loaded into `buffer` */
Yann Colletbcb5f772016-07-06 15:41:03 +020089static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
Yann Collet290aaa72016-05-30 21:18:52 +020090 size_t* fileSizes,
91 const char** fileNamesTable, unsigned nbFiles)
Yann Collet71eafdd2016-02-12 02:31:57 +010092{
Yann Collet290aaa72016-05-30 21:18:52 +020093 char* const buff = (char*)buffer;
Yann Collet71eafdd2016-02-12 02:31:57 +010094 size_t pos = 0;
95 unsigned n;
96
97 for (n=0; n<nbFiles; n++) {
Yann Colletbcb5f772016-07-06 15:41:03 +020098 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 Collet290aaa72016-05-30 21:18:52 +0200112 return n;
Yann Collet71eafdd2016-02-12 02:31:57 +0100113}
114
115
116/*-********************************************************
117* Dictionary training functions
118**********************************************************/
119static size_t DiB_findMaxMem(unsigned long long requiredMem)
120{
Yann Collet290aaa72016-05-30 21:18:52 +0200121 size_t const step = 8 MB;
Yann Collet71eafdd2016-02-12 02:31:57 +0100122 void* testmem = NULL;
123
124 requiredMem = (((requiredMem >> 23) + 1) << 23);
Yann Colletbcb5f772016-07-06 15:41:03 +0200125 requiredMem += step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100126 if (requiredMem > maxMemory) requiredMem = maxMemory;
127
128 while (!testmem) {
Yann Collet71eafdd2016-02-12 02:31:57 +0100129 testmem = malloc((size_t)requiredMem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200130 requiredMem -= step;
Yann Collet71eafdd2016-02-12 02:31:57 +0100131 }
132
133 free(testmem);
Yann Colletbcb5f772016-07-06 15:41:03 +0200134 return (size_t)requiredMem;
Yann Collet71eafdd2016-02-12 02:31:57 +0100135}
136
137
138static void DiB_fillNoise(void* buffer, size_t length)
139{
Yann Colletbcb5f772016-07-06 15:41:03 +0200140 unsigned const prime1 = 2654435761U;
141 unsigned const prime2 = 2246822519U;
142 unsigned acc = prime1;
Yann Collet71eafdd2016-02-12 02:31:57 +0100143 size_t p=0;;
144
145 for (p=0; p<length; p++) {
Yann Colletbcb5f772016-07-06 15:41:03 +0200146 acc *= prime2;
Yann Collet71eafdd2016-02-12 02:31:57 +0100147 ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
148 }
149}
150
151
152static void DiB_saveDict(const char* dictFileName,
153 const void* buff, size_t buffSize)
154{
Yann Collet290aaa72016-05-30 21:18:52 +0200155 FILE* const f = fopen(dictFileName, "wb");
Yann Collet71eafdd2016-02-12 02:31:57 +0100156 if (f==NULL) EXM_THROW(3, "cannot open %s ", dictFileName);
157
Yann Colletf6ca09b2016-05-09 04:44:45 +0200158 { size_t const n = fwrite(buff, 1, buffSize, f);
159 if (n!=buffSize) EXM_THROW(4, "%s : write error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100160
Yann Colletf6ca09b2016-05-09 04:44:45 +0200161 { size_t const n = (size_t)fclose(f);
162 if (n!=0) EXM_THROW(5, "%s : flush error", dictFileName) }
Yann Collet71eafdd2016-02-12 02:31:57 +0100163}
164
165
Yann Collet6f3acba2016-02-12 20:19:48 +0100166/*! 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*/
173size_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 Collet71eafdd2016-02-12 02:31:57 +0100178int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
179 const char** fileNamesTable, unsigned nbFiles,
180 ZDICT_params_t params)
181{
Yann Collet290aaa72016-05-30 21:18:52 +0200182 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 Colletbcb5f772016-07-06 15:41:03 +0200186 size_t benchedSize = MIN (maxMem, (size_t)totalSizeToLoad);
Yann Collet290aaa72016-05-30 21:18:52 +0200187 void* const srcBuffer = malloc(benchedSize+NOISELENGTH);
Yann Collet71eafdd2016-02-12 02:31:57 +0100188 int result = 0;
189
Yann Collet290aaa72016-05-30 21:18:52 +0200190 /* Checks */
191 if ((!fileSizes) || (!srcBuffer) || (!dictBuffer)) EXM_THROW(12, "not enough memory for DiB_trainFiles"); /* should not happen */
Yann Colletdd25a272016-07-27 12:35:29 +0200192 g_displayLevel = params.notificationLevel;
193 if (nbFiles < 5) {
Yann Collet49d105c2016-08-18 15:02:11 +0200194 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 Colletdd25a272016-07-27 12:35:29 +0200199 }
Yann Collet290aaa72016-05-30 21:18:52 +0200200
Yann Collet71eafdd2016-02-12 02:31:57 +0100201 /* init */
Yann Collet71eafdd2016-02-12 02:31:57 +0100202 if (benchedSize < totalSizeToLoad)
203 DISPLAYLEVEL(1, "Not enough memory; training on %u MB only...\n", (unsigned)(benchedSize >> 20));
204
Yann Collet71eafdd2016-02-12 02:31:57 +0100205 /* Load input buffer */
Yann Colletbcb5f772016-07-06 15:41:03 +0200206 nbFiles = DiB_loadFiles(srcBuffer, &benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Collet71eafdd2016-02-12 02:31:57 +0100207 DiB_fillNoise((char*)srcBuffer + benchedSize, NOISELENGTH); /* guard band, for end of buffer condition */
208
Yann Collet290aaa72016-05-30 21:18:52 +0200209 { 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 Collet71eafdd2016-02-12 02:31:57 +0100220 }
221
Yann Collet71eafdd2016-02-12 02:31:57 +0100222 /* clean up */
223_cleanup:
224 free(srcBuffer);
225 free(dictBuffer);
226 free(fileSizes);
227 return result;
228}