Path: blob/main/sys/contrib/zstd/examples/streaming_compression_thread_pool.c
48249 views
/*1* Copyright (c) Martin Liska, SUSE, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/91011#include <stdio.h> // printf12#include <stdlib.h> // free13#include <string.h> // memset, strcat, strlen14#include <zstd.h> // presumes zstd library is installed15#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()16#include <pthread.h>1718typedef struct compress_args19{20const char *fname;21char *outName;22int cLevel;23#if defined(ZSTD_STATIC_LINKING_ONLY)24ZSTD_threadPool *pool;25#endif26} compress_args_t;2728static void *compressFile_orDie(void *data)29{30const int nbThreads = 16;3132compress_args_t *args = (compress_args_t *)data;33fprintf (stderr, "Starting compression of %s with level %d, using %d threads\n", args->fname, args->cLevel, nbThreads);34/* Open the input and output files. */35FILE* const fin = fopen_orDie(args->fname, "rb");36FILE* const fout = fopen_orDie(args->outName, "wb");37/* Create the input and output buffers.38* They may be any size, but we recommend using these functions to size them.39* Performance will only suffer significantly for very tiny buffers.40*/41size_t const buffInSize = ZSTD_CStreamInSize();42void* const buffIn = malloc_orDie(buffInSize);43size_t const buffOutSize = ZSTD_CStreamOutSize();44void* const buffOut = malloc_orDie(buffOutSize);4546/* Create the context. */47ZSTD_CCtx* const cctx = ZSTD_createCCtx();48CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");4950#if defined(ZSTD_STATIC_LINKING_ONLY)51size_t r = ZSTD_CCtx_refThreadPool(cctx, args->pool);52CHECK(r == 0, "ZSTD_CCtx_refThreadPool failed!");53#endif5455/* Set any parameters you want.56* Here we set the compression level, and enable the checksum.57*/58CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, args->cLevel) );59CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );60ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);6162/* This loop reads from the input file, compresses that entire chunk,63* and writes all output produced to the output file.64*/65size_t const toRead = buffInSize;66for (;;) {67size_t read = fread_orDie(buffIn, toRead, fin);68/* Select the flush mode.69* If the read may not be finished (read == toRead) we use70* ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.71* Zstd optimizes the case where the first flush mode is ZSTD_e_end,72* since it knows it is compressing the entire source in one pass.73*/74int const lastChunk = (read < toRead);75ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;76/* Set the input buffer to what we just read.77* We compress until the input buffer is empty, each time flushing the78* output.79*/80ZSTD_inBuffer input = { buffIn, read, 0 };81int finished;82do {83/* Compress into the output buffer and write all of the output to84* the file so we can reuse the buffer next iteration.85*/86ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };87size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);88CHECK_ZSTD(remaining);89fwrite_orDie(buffOut, output.pos, fout);90/* If we're on the last chunk we're finished when zstd returns 0,91* which means its consumed all the input AND finished the frame.92* Otherwise, we're finished when we've consumed all the input.93*/94finished = lastChunk ? (remaining == 0) : (input.pos == input.size);95} while (!finished);96CHECK(input.pos == input.size,97"Impossible: zstd only returns 0 when the input is completely consumed!");9899if (lastChunk) {100break;101}102}103104fprintf (stderr, "Finishing compression of %s\n", args->outName);105106ZSTD_freeCCtx(cctx);107fclose_orDie(fout);108fclose_orDie(fin);109free(buffIn);110free(buffOut);111free(args->outName);112113return NULL;114}115116117static char* createOutFilename_orDie(const char* filename)118{119size_t const inL = strlen(filename);120size_t const outL = inL + 5;121void* const outSpace = malloc_orDie(outL);122memset(outSpace, 0, outL);123strcat(outSpace, filename);124strcat(outSpace, ".zst");125return (char*)outSpace;126}127128int main(int argc, const char** argv)129{130const char* const exeName = argv[0];131132if (argc<=3) {133printf("wrong arguments\n");134printf("usage:\n");135printf("%s POOL_SIZE LEVEL FILES\n", exeName);136return 1;137}138139int pool_size = atoi (argv[1]);140CHECK(pool_size != 0, "can't parse POOL_SIZE!");141142int level = atoi (argv[2]);143CHECK(level != 0, "can't parse LEVEL!");144145argc -= 3;146argv += 3;147148#if defined(ZSTD_STATIC_LINKING_ONLY)149ZSTD_threadPool *pool = ZSTD_createThreadPool (pool_size);150CHECK(pool != NULL, "ZSTD_createThreadPool() failed!");151fprintf (stderr, "Using shared thread pool of size %d\n", pool_size);152#else153fprintf (stderr, "All threads use its own thread pool\n");154#endif155156pthread_t *threads = malloc_orDie(argc * sizeof(pthread_t));157compress_args_t *args = malloc_orDie(argc * sizeof(compress_args_t));158159for (unsigned i = 0; i < argc; i++)160{161args[i].fname = argv[i];162args[i].outName = createOutFilename_orDie(args[i].fname);163args[i].cLevel = level;164#if defined(ZSTD_STATIC_LINKING_ONLY)165args[i].pool = pool;166#endif167168pthread_create (&threads[i], NULL, compressFile_orDie, &args[i]);169}170171for (unsigned i = 0; i < argc; i++)172pthread_join (threads[i], NULL);173174#if defined(ZSTD_STATIC_LINKING_ONLY)175ZSTD_freeThreadPool (pool);176#endif177178return 0;179}180181182