Path: blob/main/sys/contrib/zstd/programs/zstdcli_trace.c
48254 views
/*1* Copyright (c) Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#include "zstdcli_trace.h"1112#include <stdio.h>13#include <stdlib.h>1415#include "timefn.h"16#include "util.h"1718#define ZSTD_STATIC_LINKING_ONLY19#include "../lib/zstd.h"20/* We depend on the trace header to avoid duplicating the ZSTD_trace struct.21* But, we check the version so it is compatible with dynamic linking.22*/23#include "../lib/common/zstd_trace.h"24/* We only use macros from threading.h so it is compatible with dynamic linking */25#include "../lib/common/threading.h"2627#if ZSTD_TRACE2829static FILE* g_traceFile = NULL;30static int g_mutexInit = 0;31static ZSTD_pthread_mutex_t g_mutex;32static UTIL_time_t g_enableTime = UTIL_TIME_INITIALIZER;3334void TRACE_enable(char const* filename)35{36int const writeHeader = !UTIL_isRegularFile(filename);37if (g_traceFile)38fclose(g_traceFile);39g_traceFile = fopen(filename, "a");40if (g_traceFile && writeHeader) {41/* Fields:42* algorithm43* version44* method45* streaming46* level47* workers48* dictionary size49* uncompressed size50* compressed size51* duration nanos52* compression ratio53* speed MB/s54*/55fprintf(g_traceFile, "Algorithm, Version, Method, Mode, Level, Workers, Dictionary Size, Uncompressed Size, Compressed Size, Duration Nanos, Compression Ratio, Speed MB/s\n");56}57g_enableTime = UTIL_getTime();58if (!g_mutexInit) {59if (!ZSTD_pthread_mutex_init(&g_mutex, NULL)) {60g_mutexInit = 1;61} else {62TRACE_finish();63}64}65}6667void TRACE_finish(void)68{69if (g_traceFile) {70fclose(g_traceFile);71}72g_traceFile = NULL;73if (g_mutexInit) {74ZSTD_pthread_mutex_destroy(&g_mutex);75g_mutexInit = 0;76}77}7879static void TRACE_log(char const* method, PTime duration, ZSTD_Trace const* trace)80{81int level = 0;82int workers = 0;83double const ratio = (double)trace->uncompressedSize / (double)trace->compressedSize;84double const speed = ((double)trace->uncompressedSize * 1000) / (double)duration;85if (trace->params) {86ZSTD_CCtxParams_getParameter(trace->params, ZSTD_c_compressionLevel, &level);87ZSTD_CCtxParams_getParameter(trace->params, ZSTD_c_nbWorkers, &workers);88}89assert(g_traceFile != NULL);9091ZSTD_pthread_mutex_lock(&g_mutex);92/* Fields:93* algorithm94* version95* method96* streaming97* level98* workers99* dictionary size100* uncompressed size101* compressed size102* duration nanos103* compression ratio104* speed MB/s105*/106fprintf(g_traceFile,107"zstd, %u, %s, %s, %d, %d, %llu, %llu, %llu, %llu, %.2f, %.2f\n",108trace->version,109method,110trace->streaming ? "streaming" : "single-pass",111level,112workers,113(unsigned long long)trace->dictionarySize,114(unsigned long long)trace->uncompressedSize,115(unsigned long long)trace->compressedSize,116(unsigned long long)duration,117ratio,118speed);119ZSTD_pthread_mutex_unlock(&g_mutex);120}121122/**123* These symbols override the weak symbols provided by the library.124*/125126ZSTD_TraceCtx ZSTD_trace_compress_begin(ZSTD_CCtx const* cctx)127{128(void)cctx;129if (g_traceFile == NULL)130return 0;131return (ZSTD_TraceCtx)UTIL_clockSpanNano(g_enableTime);132}133134void ZSTD_trace_compress_end(ZSTD_TraceCtx ctx, ZSTD_Trace const* trace)135{136PTime const beginNanos = (PTime)ctx;137PTime const endNanos = UTIL_clockSpanNano(g_enableTime);138PTime const durationNanos = endNanos > beginNanos ? endNanos - beginNanos : 0;139assert(g_traceFile != NULL);140assert(trace->version == ZSTD_VERSION_NUMBER); /* CLI version must match. */141TRACE_log("compress", durationNanos, trace);142}143144ZSTD_TraceCtx ZSTD_trace_decompress_begin(ZSTD_DCtx const* dctx)145{146(void)dctx;147if (g_traceFile == NULL)148return 0;149return (ZSTD_TraceCtx)UTIL_clockSpanNano(g_enableTime);150}151152void ZSTD_trace_decompress_end(ZSTD_TraceCtx ctx, ZSTD_Trace const* trace)153{154PTime const beginNanos = (PTime)ctx;155PTime const endNanos = UTIL_clockSpanNano(g_enableTime);156PTime const durationNanos = endNanos > beginNanos ? endNanos - beginNanos : 0;157assert(g_traceFile != NULL);158assert(trace->version == ZSTD_VERSION_NUMBER); /* CLI version must match. */159TRACE_log("decompress", durationNanos, trace);160}161162#else /* ZSTD_TRACE */163164void TRACE_enable(char const* filename)165{166(void)filename;167}168169void TRACE_finish(void) {}170171#endif /* ZSTD_TRACE */172173174