2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
11 #include "zstdcli_trace.h"
19 #define ZSTD_STATIC_LINKING_ONLY
20 #include "../lib/zstd.h"
21 /* We depend on the trace header to avoid duplicating the ZSTD_trace struct.
22 * But, we check the version so it is compatible with dynamic linking.
24 #include "../lib/common/zstd_trace.h"
25 /* We only use macros from threading.h so it is compatible with dynamic linking */
26 #include "../lib/common/threading.h"
30 static FILE* g_traceFile = NULL;
31 static int g_mutexInit = 0;
32 static ZSTD_pthread_mutex_t g_mutex;
33 static UTIL_time_t g_enableTime = UTIL_TIME_INITIALIZER;
35 void TRACE_enable(char const* filename)
37 int const writeHeader = !UTIL_isRegularFile(filename);
40 g_traceFile = fopen(filename, "a");
41 if (g_traceFile && writeHeader) {
56 fprintf(g_traceFile, "Algorithm, Version, Method, Mode, Level, Workers, Dictionary Size, Uncompressed Size, Compressed Size, Duration Nanos, Compression Ratio, Speed MB/s\n");
58 g_enableTime = UTIL_getTime();
60 if (!ZSTD_pthread_mutex_init(&g_mutex, NULL)) {
68 void TRACE_finish(void)
75 ZSTD_pthread_mutex_destroy(&g_mutex);
80 static void TRACE_log(char const* method, PTime duration, ZSTD_Trace const* trace)
84 double const ratio = (double)trace->uncompressedSize / (double)trace->compressedSize;
85 double const speed = ((double)trace->uncompressedSize * 1000) / (double)duration;
87 ZSTD_CCtxParams_getParameter(trace->params, ZSTD_c_compressionLevel, &level);
88 ZSTD_CCtxParams_getParameter(trace->params, ZSTD_c_nbWorkers, &workers);
90 assert(g_traceFile != NULL);
92 ZSTD_pthread_mutex_lock(&g_mutex);
108 "zstd, %u, %s, %s, %d, %d, %llu, %llu, %llu, %llu, %.2f, %.2f\n",
111 trace->streaming ? "streaming" : "single-pass",
114 (unsigned long long)trace->dictionarySize,
115 (unsigned long long)trace->uncompressedSize,
116 (unsigned long long)trace->compressedSize,
117 (unsigned long long)duration,
120 ZSTD_pthread_mutex_unlock(&g_mutex);
124 * These symbols override the weak symbols provided by the library.
127 ZSTD_TraceCtx ZSTD_trace_compress_begin(ZSTD_CCtx const* cctx)
130 if (g_traceFile == NULL)
132 return (ZSTD_TraceCtx)UTIL_clockSpanNano(g_enableTime);
135 void ZSTD_trace_compress_end(ZSTD_TraceCtx ctx, ZSTD_Trace const* trace)
137 PTime const beginNanos = (PTime)ctx;
138 PTime const endNanos = UTIL_clockSpanNano(g_enableTime);
139 PTime const durationNanos = endNanos > beginNanos ? endNanos - beginNanos : 0;
140 assert(g_traceFile != NULL);
141 assert(trace->version == ZSTD_VERSION_NUMBER); /* CLI version must match. */
142 TRACE_log("compress", durationNanos, trace);
145 ZSTD_TraceCtx ZSTD_trace_decompress_begin(ZSTD_DCtx const* dctx)
148 if (g_traceFile == NULL)
150 return (ZSTD_TraceCtx)UTIL_clockSpanNano(g_enableTime);
153 void ZSTD_trace_decompress_end(ZSTD_TraceCtx ctx, ZSTD_Trace const* trace)
155 PTime const beginNanos = (PTime)ctx;
156 PTime const endNanos = UTIL_clockSpanNano(g_enableTime);
157 PTime const durationNanos = endNanos > beginNanos ? endNanos - beginNanos : 0;
158 assert(g_traceFile != NULL);
159 assert(trace->version == ZSTD_VERSION_NUMBER); /* CLI version must match. */
160 TRACE_log("decompress", durationNanos, trace);
163 #else /* ZSTD_TRACE */
165 void TRACE_enable(char const* filename)
170 void TRACE_finish(void) {}
172 #endif /* ZSTD_TRACE */