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