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