streaming_memory_usage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) Yann Collet, 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. /*=== Tuning parameter ===*/
  11. #ifndef MAX_TESTED_LEVEL
  12. #define MAX_TESTED_LEVEL 12
  13. #endif
  14. /*=== Dependencies ===*/
  15. #include <stdio.h> // printf
  16. #define ZSTD_STATIC_LINKING_ONLY
  17. #include <zstd.h> // presumes zstd library is installed
  18. #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
  19. /*=== functions ===*/
  20. /*! readU32FromChar() :
  21. @return : unsigned integer value read from input in `char` format
  22. allows and interprets K, KB, KiB, M, MB and MiB suffix.
  23. Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  24. Note : function result can overflow if digit string > MAX_UINT */
  25. static unsigned readU32FromChar(const char** stringPtr)
  26. {
  27. unsigned result = 0;
  28. while ((**stringPtr >='0') && (**stringPtr <='9'))
  29. result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
  30. if ((**stringPtr=='K') || (**stringPtr=='M')) {
  31. result <<= 10;
  32. if (**stringPtr=='M') result <<= 10;
  33. (*stringPtr)++ ;
  34. if (**stringPtr=='i') (*stringPtr)++;
  35. if (**stringPtr=='B') (*stringPtr)++;
  36. }
  37. return result;
  38. }
  39. int main(int argc, char const *argv[]) {
  40. printf("\n Zstandard (v%s) memory usage for streaming : \n\n", ZSTD_versionString());
  41. unsigned wLog = 0;
  42. if (argc > 1) {
  43. const char* valStr = argv[1];
  44. wLog = readU32FromChar(&valStr);
  45. }
  46. int compressionLevel;
  47. for (compressionLevel = 1; compressionLevel <= MAX_TESTED_LEVEL; compressionLevel++) {
  48. #define INPUT_SIZE 5
  49. #define COMPRESSED_SIZE 128
  50. char const dataToCompress[INPUT_SIZE] = "abcde";
  51. char compressedData[COMPRESSED_SIZE];
  52. char decompressedData[INPUT_SIZE];
  53. /* the ZSTD_CCtx_params structure is a way to save parameters and use
  54. * them across multiple contexts. We use them here so we can call the
  55. * function ZSTD_estimateCStreamSize_usingCCtxParams().
  56. */
  57. ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams();
  58. CHECK(cctxParams != NULL, "ZSTD_createCCtxParams() failed!");
  59. /* Set the compression level. */
  60. CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_compressionLevel, compressionLevel) );
  61. /* Set the window log.
  62. * The value 0 means use the default window log, which is equivalent to
  63. * not setting it.
  64. */
  65. CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, wLog) );
  66. /* Force the compressor to allocate the maximum memory size for a given
  67. * level by not providing the pledged source size, or calling
  68. * ZSTD_compressStream2() with ZSTD_e_end.
  69. */
  70. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  71. CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
  72. CHECK_ZSTD( ZSTD_CCtx_setParametersUsingCCtxParams(cctx, cctxParams) );
  73. size_t compressedSize;
  74. {
  75. ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 };
  76. ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 };
  77. CHECK_ZSTD( ZSTD_compressStream(cctx, &outBuff, &inBuff) );
  78. size_t const remaining = ZSTD_endStream(cctx, &outBuff);
  79. CHECK_ZSTD(remaining);
  80. CHECK(remaining == 0, "Frame not flushed!");
  81. compressedSize = outBuff.pos;
  82. }
  83. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  84. CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
  85. /* Set the maximum allowed window log.
  86. * The value 0 means use the default window log, which is equivalent to
  87. * not setting it.
  88. */
  89. CHECK_ZSTD( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, wLog) );
  90. /* forces decompressor to use maximum memory size, since the
  91. * decompressed size is not stored in the frame header.
  92. */
  93. { ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 };
  94. ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 };
  95. size_t const remaining = ZSTD_decompressStream(dctx, &outBuff, &inBuff);
  96. CHECK_ZSTD(remaining);
  97. CHECK(remaining == 0, "Frame not complete!");
  98. CHECK(outBuff.pos == sizeof(dataToCompress), "Bad decompression!");
  99. }
  100. size_t const cstreamSize = ZSTD_sizeof_CStream(cctx);
  101. size_t const cstreamEstimatedSize = ZSTD_estimateCStreamSize_usingCCtxParams(cctxParams);
  102. size_t const dstreamSize = ZSTD_sizeof_DStream(dctx);
  103. size_t const dstreamEstimatedSize = ZSTD_estimateDStreamSize_fromFrame(compressedData, compressedSize);
  104. CHECK(cstreamSize <= cstreamEstimatedSize, "Compression mem (%u) > estimated (%u)",
  105. (unsigned)cstreamSize, (unsigned)cstreamEstimatedSize);
  106. CHECK(dstreamSize <= dstreamEstimatedSize, "Decompression mem (%u) > estimated (%u)",
  107. (unsigned)dstreamSize, (unsigned)dstreamEstimatedSize);
  108. printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB (estimated : %5u KB)\n",
  109. compressionLevel,
  110. (unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10),
  111. (unsigned)(dstreamSize>>10), (unsigned)(dstreamEstimatedSize>>10));
  112. ZSTD_freeDCtx(dctx);
  113. ZSTD_freeCCtx(cctx);
  114. ZSTD_freeCCtxParams(cctxParams);
  115. if (wLog) break; /* single test */
  116. }
  117. return 0;
  118. }