xref: /freebsd-src/sys/contrib/zstd/examples/streaming_memory_usage.c (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
12b9c00cbSConrad Meyer /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
32b9c00cbSConrad Meyer  * All rights reserved.
42b9c00cbSConrad Meyer  *
52b9c00cbSConrad Meyer  * This source code is licensed under both the BSD-style license (found in the
62b9c00cbSConrad Meyer  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
72b9c00cbSConrad Meyer  * in the COPYING file in the root directory of this source tree).
82b9c00cbSConrad Meyer  * You may select, at your option, one of the above-listed licenses.
92b9c00cbSConrad Meyer  */
102b9c00cbSConrad Meyer 
112b9c00cbSConrad Meyer 
122b9c00cbSConrad Meyer /*===   Tuning parameter   ===*/
132b9c00cbSConrad Meyer #ifndef MAX_TESTED_LEVEL
142b9c00cbSConrad Meyer #define MAX_TESTED_LEVEL 12
152b9c00cbSConrad Meyer #endif
162b9c00cbSConrad Meyer 
172b9c00cbSConrad Meyer 
182b9c00cbSConrad Meyer /*===   Dependencies   ===*/
192b9c00cbSConrad Meyer #include <stdio.h>     // printf
202b9c00cbSConrad Meyer #define ZSTD_STATIC_LINKING_ONLY
212b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
222b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
232b9c00cbSConrad Meyer 
242b9c00cbSConrad Meyer 
252b9c00cbSConrad Meyer /*===   functions   ===*/
262b9c00cbSConrad Meyer 
272b9c00cbSConrad Meyer /*! readU32FromChar() :
282b9c00cbSConrad Meyer     @return : unsigned integer value read from input in `char` format
292b9c00cbSConrad Meyer     allows and interprets K, KB, KiB, M, MB and MiB suffix.
302b9c00cbSConrad Meyer     Will also modify `*stringPtr`, advancing it to position where it stopped reading.
312b9c00cbSConrad Meyer     Note : function result can overflow if digit string > MAX_UINT */
readU32FromChar(const char ** stringPtr)322b9c00cbSConrad Meyer static unsigned readU32FromChar(const char** stringPtr)
332b9c00cbSConrad Meyer {
342b9c00cbSConrad Meyer     unsigned result = 0;
352b9c00cbSConrad Meyer     while ((**stringPtr >='0') && (**stringPtr <='9'))
362b9c00cbSConrad Meyer         result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
372b9c00cbSConrad Meyer     if ((**stringPtr=='K') || (**stringPtr=='M')) {
382b9c00cbSConrad Meyer         result <<= 10;
392b9c00cbSConrad Meyer         if (**stringPtr=='M') result <<= 10;
402b9c00cbSConrad Meyer         (*stringPtr)++ ;
412b9c00cbSConrad Meyer         if (**stringPtr=='i') (*stringPtr)++;
422b9c00cbSConrad Meyer         if (**stringPtr=='B') (*stringPtr)++;
432b9c00cbSConrad Meyer     }
442b9c00cbSConrad Meyer     return result;
452b9c00cbSConrad Meyer }
462b9c00cbSConrad Meyer 
472b9c00cbSConrad Meyer 
main(int argc,char const * argv[])482b9c00cbSConrad Meyer int main(int argc, char const *argv[]) {
492b9c00cbSConrad Meyer 
502b9c00cbSConrad Meyer     printf("\n Zstandard (v%s) memory usage for streaming : \n\n", ZSTD_versionString());
512b9c00cbSConrad Meyer 
522b9c00cbSConrad Meyer     unsigned wLog = 0;
532b9c00cbSConrad Meyer     if (argc > 1) {
542b9c00cbSConrad Meyer         const char* valStr = argv[1];
552b9c00cbSConrad Meyer         wLog = readU32FromChar(&valStr);
562b9c00cbSConrad Meyer     }
572b9c00cbSConrad Meyer 
582b9c00cbSConrad Meyer     int compressionLevel;
592b9c00cbSConrad Meyer     for (compressionLevel = 1; compressionLevel <= MAX_TESTED_LEVEL; compressionLevel++) {
602b9c00cbSConrad Meyer #define INPUT_SIZE 5
612b9c00cbSConrad Meyer #define COMPRESSED_SIZE 128
622b9c00cbSConrad Meyer         char const dataToCompress[INPUT_SIZE] = "abcde";
632b9c00cbSConrad Meyer         char compressedData[COMPRESSED_SIZE];
642b9c00cbSConrad Meyer         char decompressedData[INPUT_SIZE];
652b9c00cbSConrad Meyer         /* the ZSTD_CCtx_params structure is a way to save parameters and use
662b9c00cbSConrad Meyer          * them across multiple contexts. We use them here so we can call the
672b9c00cbSConrad Meyer          * function ZSTD_estimateCStreamSize_usingCCtxParams().
682b9c00cbSConrad Meyer          */
692b9c00cbSConrad Meyer         ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams();
702b9c00cbSConrad Meyer         CHECK(cctxParams != NULL, "ZSTD_createCCtxParams() failed!");
712b9c00cbSConrad Meyer 
722b9c00cbSConrad Meyer         /* Set the compression level. */
732b9c00cbSConrad Meyer         CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_compressionLevel, compressionLevel) );
742b9c00cbSConrad Meyer         /* Set the window log.
752b9c00cbSConrad Meyer          * The value 0 means use the default window log, which is equivalent to
762b9c00cbSConrad Meyer          * not setting it.
772b9c00cbSConrad Meyer          */
782b9c00cbSConrad Meyer         CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, wLog) );
792b9c00cbSConrad Meyer 
802b9c00cbSConrad Meyer         /* Force the compressor to allocate the maximum memory size for a given
812b9c00cbSConrad Meyer          * level by not providing the pledged source size, or calling
822b9c00cbSConrad Meyer          * ZSTD_compressStream2() with ZSTD_e_end.
832b9c00cbSConrad Meyer          */
842b9c00cbSConrad Meyer         ZSTD_CCtx* const cctx = ZSTD_createCCtx();
852b9c00cbSConrad Meyer         CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
862b9c00cbSConrad Meyer         CHECK_ZSTD( ZSTD_CCtx_setParametersUsingCCtxParams(cctx, cctxParams) );
872b9c00cbSConrad Meyer         size_t compressedSize;
882b9c00cbSConrad Meyer         {
892b9c00cbSConrad Meyer             ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 };
902b9c00cbSConrad Meyer             ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 };
912b9c00cbSConrad Meyer             CHECK_ZSTD( ZSTD_compressStream(cctx, &outBuff, &inBuff) );
922b9c00cbSConrad Meyer             size_t const remaining = ZSTD_endStream(cctx, &outBuff);
932b9c00cbSConrad Meyer             CHECK_ZSTD(remaining);
942b9c00cbSConrad Meyer             CHECK(remaining == 0, "Frame not flushed!");
952b9c00cbSConrad Meyer             compressedSize = outBuff.pos;
962b9c00cbSConrad Meyer         }
972b9c00cbSConrad Meyer 
982b9c00cbSConrad Meyer         ZSTD_DCtx* const dctx = ZSTD_createDCtx();
992b9c00cbSConrad Meyer         CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
1002b9c00cbSConrad Meyer         /* Set the maximum allowed window log.
1012b9c00cbSConrad Meyer          * The value 0 means use the default window log, which is equivalent to
1022b9c00cbSConrad Meyer          * not setting it.
1032b9c00cbSConrad Meyer          */
1042b9c00cbSConrad Meyer         CHECK_ZSTD( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, wLog) );
1052b9c00cbSConrad Meyer         /* forces decompressor to use maximum memory size, since the
1062b9c00cbSConrad Meyer          * decompressed size is not stored in the frame header.
1072b9c00cbSConrad Meyer          */
1082b9c00cbSConrad Meyer         {   ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 };
1092b9c00cbSConrad Meyer             ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 };
1102b9c00cbSConrad Meyer             size_t const remaining = ZSTD_decompressStream(dctx, &outBuff, &inBuff);
1112b9c00cbSConrad Meyer             CHECK_ZSTD(remaining);
1122b9c00cbSConrad Meyer             CHECK(remaining == 0, "Frame not complete!");
1132b9c00cbSConrad Meyer             CHECK(outBuff.pos == sizeof(dataToCompress), "Bad decompression!");
1142b9c00cbSConrad Meyer         }
1152b9c00cbSConrad Meyer 
1162b9c00cbSConrad Meyer         size_t const cstreamSize = ZSTD_sizeof_CStream(cctx);
1172b9c00cbSConrad Meyer         size_t const cstreamEstimatedSize = ZSTD_estimateCStreamSize_usingCCtxParams(cctxParams);
1182b9c00cbSConrad Meyer         size_t const dstreamSize = ZSTD_sizeof_DStream(dctx);
1192b9c00cbSConrad Meyer         size_t const dstreamEstimatedSize = ZSTD_estimateDStreamSize_fromFrame(compressedData, compressedSize);
1202b9c00cbSConrad Meyer 
1212b9c00cbSConrad Meyer         CHECK(cstreamSize <= cstreamEstimatedSize, "Compression mem (%u) > estimated (%u)",
1222b9c00cbSConrad Meyer                 (unsigned)cstreamSize, (unsigned)cstreamEstimatedSize);
1232b9c00cbSConrad Meyer         CHECK(dstreamSize <= dstreamEstimatedSize, "Decompression mem (%u) > estimated (%u)",
1242b9c00cbSConrad Meyer                 (unsigned)dstreamSize, (unsigned)dstreamEstimatedSize);
1252b9c00cbSConrad Meyer 
1262b9c00cbSConrad Meyer         printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB (estimated : %5u KB)\n",
1272b9c00cbSConrad Meyer                 compressionLevel,
1282b9c00cbSConrad Meyer                 (unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10),
1292b9c00cbSConrad Meyer                 (unsigned)(dstreamSize>>10), (unsigned)(dstreamEstimatedSize>>10));
1302b9c00cbSConrad Meyer 
1312b9c00cbSConrad Meyer         ZSTD_freeDCtx(dctx);
1322b9c00cbSConrad Meyer         ZSTD_freeCCtx(cctx);
1332b9c00cbSConrad Meyer         ZSTD_freeCCtxParams(cctxParams);
1342b9c00cbSConrad Meyer         if (wLog) break;  /* single test */
1352b9c00cbSConrad Meyer     }
1362b9c00cbSConrad Meyer     return 0;
1372b9c00cbSConrad Meyer }
138