1*22ce4affSfengbojiang /*
2*22ce4affSfengbojiang  * Copyright (c) 2020, Martin Liska, SUSE, Facebook, Inc.
3*22ce4affSfengbojiang  * All rights reserved.
4*22ce4affSfengbojiang  *
5*22ce4affSfengbojiang  * This source code is licensed under both the BSD-style license (found in the
6*22ce4affSfengbojiang  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*22ce4affSfengbojiang  * in the COPYING file in the root directory of this source tree).
8*22ce4affSfengbojiang  * You may select, at your option, one of the above-listed licenses.
9*22ce4affSfengbojiang  */
10*22ce4affSfengbojiang 
11*22ce4affSfengbojiang 
12*22ce4affSfengbojiang #include <stdio.h>     // printf
13*22ce4affSfengbojiang #include <stdlib.h>    // free
14*22ce4affSfengbojiang #include <string.h>    // memset, strcat, strlen
15*22ce4affSfengbojiang #include <zstd.h>      // presumes zstd library is installed
16*22ce4affSfengbojiang #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
17*22ce4affSfengbojiang #include <pthread.h>
18*22ce4affSfengbojiang 
19*22ce4affSfengbojiang typedef struct compress_args
20*22ce4affSfengbojiang {
21*22ce4affSfengbojiang   const char *fname;
22*22ce4affSfengbojiang   char *outName;
23*22ce4affSfengbojiang   int cLevel;
24*22ce4affSfengbojiang #if defined(ZSTD_STATIC_LINKING_ONLY)
25*22ce4affSfengbojiang   ZSTD_threadPool *pool;
26*22ce4affSfengbojiang #endif
27*22ce4affSfengbojiang } compress_args_t;
28*22ce4affSfengbojiang 
compressFile_orDie(void * data)29*22ce4affSfengbojiang static void *compressFile_orDie(void *data)
30*22ce4affSfengbojiang {
31*22ce4affSfengbojiang     compress_args_t *args = (compress_args_t *)data;
32*22ce4affSfengbojiang     fprintf (stderr, "Starting compression of %s with level %d\n", args->fname, args->cLevel);
33*22ce4affSfengbojiang     /* Open the input and output files. */
34*22ce4affSfengbojiang     FILE* const fin  = fopen_orDie(args->fname, "rb");
35*22ce4affSfengbojiang     FILE* const fout = fopen_orDie(args->outName, "wb");
36*22ce4affSfengbojiang     /* Create the input and output buffers.
37*22ce4affSfengbojiang      * They may be any size, but we recommend using these functions to size them.
38*22ce4affSfengbojiang      * Performance will only suffer significantly for very tiny buffers.
39*22ce4affSfengbojiang      */
40*22ce4affSfengbojiang     size_t const buffInSize = ZSTD_CStreamInSize();
41*22ce4affSfengbojiang     void*  const buffIn  = malloc_orDie(buffInSize);
42*22ce4affSfengbojiang     size_t const buffOutSize = ZSTD_CStreamOutSize();
43*22ce4affSfengbojiang     void*  const buffOut = malloc_orDie(buffOutSize);
44*22ce4affSfengbojiang 
45*22ce4affSfengbojiang     /* Create the context. */
46*22ce4affSfengbojiang     ZSTD_CCtx* const cctx = ZSTD_createCCtx();
47*22ce4affSfengbojiang     CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
48*22ce4affSfengbojiang 
49*22ce4affSfengbojiang #if defined(ZSTD_STATIC_LINKING_ONLY)
50*22ce4affSfengbojiang     size_t r = ZSTD_CCtx_refThreadPool(cctx, args->pool);
51*22ce4affSfengbojiang     CHECK(r == 0, "ZSTD_CCtx_refThreadPool failed!");
52*22ce4affSfengbojiang #endif
53*22ce4affSfengbojiang 
54*22ce4affSfengbojiang     /* Set any parameters you want.
55*22ce4affSfengbojiang      * Here we set the compression level, and enable the checksum.
56*22ce4affSfengbojiang      */
57*22ce4affSfengbojiang     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, args->cLevel) );
58*22ce4affSfengbojiang     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
59*22ce4affSfengbojiang     ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 16);
60*22ce4affSfengbojiang 
61*22ce4affSfengbojiang     /* This loop read from the input file, compresses that entire chunk,
62*22ce4affSfengbojiang      * and writes all output produced to the output file.
63*22ce4affSfengbojiang      */
64*22ce4affSfengbojiang     size_t const toRead = buffInSize;
65*22ce4affSfengbojiang     for (;;) {
66*22ce4affSfengbojiang         size_t read = fread_orDie(buffIn, toRead, fin);
67*22ce4affSfengbojiang         /* Select the flush mode.
68*22ce4affSfengbojiang          * If the read may not be finished (read == toRead) we use
69*22ce4affSfengbojiang          * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
70*22ce4affSfengbojiang          * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
71*22ce4affSfengbojiang          * since it knows it is compressing the entire source in one pass.
72*22ce4affSfengbojiang          */
73*22ce4affSfengbojiang         int const lastChunk = (read < toRead);
74*22ce4affSfengbojiang         ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
75*22ce4affSfengbojiang         /* Set the input buffer to what we just read.
76*22ce4affSfengbojiang          * We compress until the input buffer is empty, each time flushing the
77*22ce4affSfengbojiang          * output.
78*22ce4affSfengbojiang          */
79*22ce4affSfengbojiang         ZSTD_inBuffer input = { buffIn, read, 0 };
80*22ce4affSfengbojiang         int finished;
81*22ce4affSfengbojiang         do {
82*22ce4affSfengbojiang             /* Compress into the output buffer and write all of the output to
83*22ce4affSfengbojiang              * the file so we can reuse the buffer next iteration.
84*22ce4affSfengbojiang              */
85*22ce4affSfengbojiang             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
86*22ce4affSfengbojiang             size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
87*22ce4affSfengbojiang             CHECK_ZSTD(remaining);
88*22ce4affSfengbojiang             fwrite_orDie(buffOut, output.pos, fout);
89*22ce4affSfengbojiang             /* If we're on the last chunk we're finished when zstd returns 0,
90*22ce4affSfengbojiang              * which means its consumed all the input AND finished the frame.
91*22ce4affSfengbojiang              * Otherwise, we're finished when we've consumed all the input.
92*22ce4affSfengbojiang              */
93*22ce4affSfengbojiang             finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
94*22ce4affSfengbojiang         } while (!finished);
95*22ce4affSfengbojiang         CHECK(input.pos == input.size,
96*22ce4affSfengbojiang               "Impossible: zstd only returns 0 when the input is completely consumed!");
97*22ce4affSfengbojiang 
98*22ce4affSfengbojiang         if (lastChunk) {
99*22ce4affSfengbojiang             break;
100*22ce4affSfengbojiang         }
101*22ce4affSfengbojiang     }
102*22ce4affSfengbojiang 
103*22ce4affSfengbojiang     fprintf (stderr, "Finishing compression of %s\n", args->outName);
104*22ce4affSfengbojiang 
105*22ce4affSfengbojiang     ZSTD_freeCCtx(cctx);
106*22ce4affSfengbojiang     fclose_orDie(fout);
107*22ce4affSfengbojiang     fclose_orDie(fin);
108*22ce4affSfengbojiang     free(buffIn);
109*22ce4affSfengbojiang     free(buffOut);
110*22ce4affSfengbojiang     free(args->outName);
111*22ce4affSfengbojiang 
112*22ce4affSfengbojiang     return NULL;
113*22ce4affSfengbojiang }
114*22ce4affSfengbojiang 
115*22ce4affSfengbojiang 
createOutFilename_orDie(const char * filename)116*22ce4affSfengbojiang static char* createOutFilename_orDie(const char* filename)
117*22ce4affSfengbojiang {
118*22ce4affSfengbojiang     size_t const inL = strlen(filename);
119*22ce4affSfengbojiang     size_t const outL = inL + 5;
120*22ce4affSfengbojiang     void* const outSpace = malloc_orDie(outL);
121*22ce4affSfengbojiang     memset(outSpace, 0, outL);
122*22ce4affSfengbojiang     strcat(outSpace, filename);
123*22ce4affSfengbojiang     strcat(outSpace, ".zst");
124*22ce4affSfengbojiang     return (char*)outSpace;
125*22ce4affSfengbojiang }
126*22ce4affSfengbojiang 
main(int argc,const char ** argv)127*22ce4affSfengbojiang int main(int argc, const char** argv)
128*22ce4affSfengbojiang {
129*22ce4affSfengbojiang     const char* const exeName = argv[0];
130*22ce4affSfengbojiang 
131*22ce4affSfengbojiang     if (argc<=3) {
132*22ce4affSfengbojiang         printf("wrong arguments\n");
133*22ce4affSfengbojiang         printf("usage:\n");
134*22ce4affSfengbojiang         printf("%s POOL_SIZE LEVEL FILES\n", exeName);
135*22ce4affSfengbojiang         return 1;
136*22ce4affSfengbojiang     }
137*22ce4affSfengbojiang 
138*22ce4affSfengbojiang     int pool_size = atoi (argv[1]);
139*22ce4affSfengbojiang     CHECK(pool_size != 0, "can't parse POOL_SIZE!");
140*22ce4affSfengbojiang 
141*22ce4affSfengbojiang     int level = atoi (argv[2]);
142*22ce4affSfengbojiang     CHECK(level != 0, "can't parse LEVEL!");
143*22ce4affSfengbojiang 
144*22ce4affSfengbojiang     argc -= 3;
145*22ce4affSfengbojiang     argv += 3;
146*22ce4affSfengbojiang 
147*22ce4affSfengbojiang #if defined(ZSTD_STATIC_LINKING_ONLY)
148*22ce4affSfengbojiang     ZSTD_threadPool *pool = ZSTD_createThreadPool (pool_size);
149*22ce4affSfengbojiang     CHECK(pool != NULL, "ZSTD_createThreadPool() failed!");
150*22ce4affSfengbojiang     fprintf (stderr, "Using shared thread pool of size %d\n", pool_size);
151*22ce4affSfengbojiang #else
152*22ce4affSfengbojiang     fprintf (stderr, "All threads use its own thread pool\n");
153*22ce4affSfengbojiang #endif
154*22ce4affSfengbojiang 
155*22ce4affSfengbojiang     pthread_t *threads = malloc_orDie(argc * sizeof(pthread_t));
156*22ce4affSfengbojiang     compress_args_t *args = malloc_orDie(argc * sizeof(compress_args_t));
157*22ce4affSfengbojiang 
158*22ce4affSfengbojiang     for (unsigned i = 0; i < argc; i++)
159*22ce4affSfengbojiang     {
160*22ce4affSfengbojiang       args[i].fname = argv[i];
161*22ce4affSfengbojiang       args[i].outName = createOutFilename_orDie(args[i].fname);
162*22ce4affSfengbojiang       args[i].cLevel = level;
163*22ce4affSfengbojiang #if defined(ZSTD_STATIC_LINKING_ONLY)
164*22ce4affSfengbojiang       args[i].pool = pool;
165*22ce4affSfengbojiang #endif
166*22ce4affSfengbojiang 
167*22ce4affSfengbojiang       pthread_create (&threads[i], NULL, compressFile_orDie, &args[i]);
168*22ce4affSfengbojiang     }
169*22ce4affSfengbojiang 
170*22ce4affSfengbojiang     for (unsigned i = 0; i < argc; i++)
171*22ce4affSfengbojiang       pthread_join (threads[i], NULL);
172*22ce4affSfengbojiang 
173*22ce4affSfengbojiang #if defined(ZSTD_STATIC_LINKING_ONLY)
174*22ce4affSfengbojiang     ZSTD_freeThreadPool (pool);
175*22ce4affSfengbojiang #endif
176*22ce4affSfengbojiang 
177*22ce4affSfengbojiang     return 0;
178*22ce4affSfengbojiang }
179