]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/examples/streaming_memory_usage.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / zstd / examples / streaming_memory_usage.c
1 /*
2 * Copyright (c) 2017-present, 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"
22
23
24 /*=== functions ===*/
25
26 /*! readU32FromChar() :
27 @return : unsigned integer value read from input in `char` format
28 allows and interprets K, KB, KiB, M, MB and MiB suffix.
29 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
30 Note : function result can overflow if digit string > MAX_UINT */
31 static unsigned readU32FromChar(const char** stringPtr)
32 {
33 unsigned result = 0;
34 while ((**stringPtr >='0') && (**stringPtr <='9'))
35 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
36 if ((**stringPtr=='K') || (**stringPtr=='M')) {
37 result <<= 10;
38 if (**stringPtr=='M') result <<= 10;
39 (*stringPtr)++ ;
40 if (**stringPtr=='i') (*stringPtr)++;
41 if (**stringPtr=='B') (*stringPtr)++;
42 }
43 return result;
44 }
45
46
47 int main(int argc, char const *argv[]) {
48
49 printf("\n Zstandard (v%u) memory usage for streaming contexts : \n\n", ZSTD_versionNumber());
50
51 unsigned wLog = 0;
52 if (argc > 1) {
53 const char* valStr = argv[1];
54 wLog = readU32FromChar(&valStr);
55 }
56
57 int compressionLevel;
58 for (compressionLevel = 1; compressionLevel <= MAX_TESTED_LEVEL; compressionLevel++) {
59 #define INPUT_SIZE 5
60 #define COMPRESSED_SIZE 128
61 char const dataToCompress[INPUT_SIZE] = "abcde";
62 char compressedData[COMPRESSED_SIZE];
63 char decompressedData[INPUT_SIZE];
64 ZSTD_CStream* const cstream = ZSTD_createCStream();
65 if (cstream==NULL) {
66 printf("Level %i : ZSTD_CStream Memory allocation failure \n", compressionLevel);
67 return 1;
68 }
69
70 /* forces compressor to use maximum memory size for given compression level,
71 * by not providing any information on input size */
72 ZSTD_parameters params = ZSTD_getParams(compressionLevel, 0, 0);
73 if (wLog) { /* special mode : specific wLog */
74 printf("Using custom compression parameter : level 1 + wLog=%u \n", wLog);
75 params = ZSTD_getParams(1, 1 << wLog, 0);
76 size_t const error = ZSTD_initCStream_advanced(cstream, NULL, 0, params, 0);
77 if (ZSTD_isError(error)) {
78 printf("ZSTD_initCStream_advanced error : %s \n", ZSTD_getErrorName(error));
79 return 1;
80 }
81 } else {
82 size_t const error = ZSTD_initCStream(cstream, compressionLevel);
83 if (ZSTD_isError(error)) {
84 printf("ZSTD_initCStream error : %s \n", ZSTD_getErrorName(error));
85 return 1;
86 }
87 }
88
89 size_t compressedSize;
90 { ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 };
91 ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 };
92 size_t const cError = ZSTD_compressStream(cstream, &outBuff, &inBuff);
93 if (ZSTD_isError(cError)) {
94 printf("ZSTD_compressStream error : %s \n", ZSTD_getErrorName(cError));
95 return 1;
96 }
97 size_t const fError = ZSTD_endStream(cstream, &outBuff);
98 if (ZSTD_isError(fError)) {
99 printf("ZSTD_endStream error : %s \n", ZSTD_getErrorName(fError));
100 return 1;
101 }
102 compressedSize = outBuff.pos;
103 }
104
105 ZSTD_DStream* dstream = ZSTD_createDStream();
106 if (dstream==NULL) {
107 printf("Level %i : ZSTD_DStream Memory allocation failure \n", compressionLevel);
108 return 1;
109 }
110 { size_t const error = ZSTD_initDStream(dstream);
111 if (ZSTD_isError(error)) {
112 printf("ZSTD_initDStream error : %s \n", ZSTD_getErrorName(error));
113 return 1;
114 }
115 }
116 /* forces decompressor to use maximum memory size, as decompressed size is not known */
117 { ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 };
118 ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 };
119 size_t const dResult = ZSTD_decompressStream(dstream, &outBuff, &inBuff);
120 if (ZSTD_isError(dResult)) {
121 printf("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(dResult));
122 return 1;
123 }
124 if (dResult != 0) {
125 printf("ZSTD_decompressStream error : unfinished decompression \n");
126 return 1;
127 }
128 if (outBuff.pos != sizeof(dataToCompress)) {
129 printf("ZSTD_decompressStream error : incorrect decompression \n");
130 return 1;
131 }
132 }
133
134 size_t const cstreamSize = ZSTD_sizeof_CStream(cstream);
135 size_t const cstreamEstimatedSize = wLog ?
136 ZSTD_estimateCStreamSize_usingCParams(params.cParams) :
137 ZSTD_estimateCStreamSize(compressionLevel);
138 size_t const dstreamSize = ZSTD_sizeof_DStream(dstream);
139
140 printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB \n",
141 compressionLevel,
142 (unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10), (unsigned)(dstreamSize>>10));
143
144 ZSTD_freeDStream(dstream);
145 ZSTD_freeCStream(cstream);
146 if (wLog) break; /* single test */
147 }
148 return 0;
149 }