streaming_compression_thread_pool.c 6.0 KB

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