xref: /netbsd-src/external/bsd/zstd/dist/lib/common/zstd_trace.h (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos 
11*3117ece4Schristos #ifndef ZSTD_TRACE_H
12*3117ece4Schristos #define ZSTD_TRACE_H
13*3117ece4Schristos 
14*3117ece4Schristos #if defined (__cplusplus)
15*3117ece4Schristos extern "C" {
16*3117ece4Schristos #endif
17*3117ece4Schristos 
18*3117ece4Schristos #include <stddef.h>
19*3117ece4Schristos 
20*3117ece4Schristos /* weak symbol support
21*3117ece4Schristos  * For now, enable conservatively:
22*3117ece4Schristos  * - Only GNUC
23*3117ece4Schristos  * - Only ELF
24*3117ece4Schristos  * - Only x86-64, i386 and aarch64
25*3117ece4Schristos  * Also, explicitly disable on platforms known not to work so they aren't
26*3117ece4Schristos  * forgotten in the future.
27*3117ece4Schristos  */
28*3117ece4Schristos #if !defined(ZSTD_HAVE_WEAK_SYMBOLS) && \
29*3117ece4Schristos     defined(__GNUC__) && defined(__ELF__) && \
30*3117ece4Schristos     (defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) || defined(__aarch64__)) && \
31*3117ece4Schristos     !defined(__APPLE__) && !defined(_WIN32) && !defined(__MINGW32__) && \
32*3117ece4Schristos     !defined(__CYGWIN__) && !defined(_AIX)
33*3117ece4Schristos #  define ZSTD_HAVE_WEAK_SYMBOLS 1
34*3117ece4Schristos #else
35*3117ece4Schristos #  define ZSTD_HAVE_WEAK_SYMBOLS 0
36*3117ece4Schristos #endif
37*3117ece4Schristos #if ZSTD_HAVE_WEAK_SYMBOLS
38*3117ece4Schristos #  define ZSTD_WEAK_ATTR __attribute__((__weak__))
39*3117ece4Schristos #else
40*3117ece4Schristos #  define ZSTD_WEAK_ATTR
41*3117ece4Schristos #endif
42*3117ece4Schristos 
43*3117ece4Schristos /* Only enable tracing when weak symbols are available. */
44*3117ece4Schristos #ifndef ZSTD_TRACE
45*3117ece4Schristos #  define ZSTD_TRACE ZSTD_HAVE_WEAK_SYMBOLS
46*3117ece4Schristos #endif
47*3117ece4Schristos 
48*3117ece4Schristos #if ZSTD_TRACE
49*3117ece4Schristos 
50*3117ece4Schristos struct ZSTD_CCtx_s;
51*3117ece4Schristos struct ZSTD_DCtx_s;
52*3117ece4Schristos struct ZSTD_CCtx_params_s;
53*3117ece4Schristos 
54*3117ece4Schristos typedef struct {
55*3117ece4Schristos     /**
56*3117ece4Schristos      * ZSTD_VERSION_NUMBER
57*3117ece4Schristos      *
58*3117ece4Schristos      * This is guaranteed to be the first member of ZSTD_trace.
59*3117ece4Schristos      * Otherwise, this struct is not stable between versions. If
60*3117ece4Schristos      * the version number does not match your expectation, you
61*3117ece4Schristos      * should not interpret the rest of the struct.
62*3117ece4Schristos      */
63*3117ece4Schristos     unsigned version;
64*3117ece4Schristos     /**
65*3117ece4Schristos      * Non-zero if streaming (de)compression is used.
66*3117ece4Schristos      */
67*3117ece4Schristos     unsigned streaming;
68*3117ece4Schristos     /**
69*3117ece4Schristos      * The dictionary ID.
70*3117ece4Schristos      */
71*3117ece4Schristos     unsigned dictionaryID;
72*3117ece4Schristos     /**
73*3117ece4Schristos      * Is the dictionary cold?
74*3117ece4Schristos      * Only set on decompression.
75*3117ece4Schristos      */
76*3117ece4Schristos     unsigned dictionaryIsCold;
77*3117ece4Schristos     /**
78*3117ece4Schristos      * The dictionary size or zero if no dictionary.
79*3117ece4Schristos      */
80*3117ece4Schristos     size_t dictionarySize;
81*3117ece4Schristos     /**
82*3117ece4Schristos      * The uncompressed size of the data.
83*3117ece4Schristos      */
84*3117ece4Schristos     size_t uncompressedSize;
85*3117ece4Schristos     /**
86*3117ece4Schristos      * The compressed size of the data.
87*3117ece4Schristos      */
88*3117ece4Schristos     size_t compressedSize;
89*3117ece4Schristos     /**
90*3117ece4Schristos      * The fully resolved CCtx parameters (NULL on decompression).
91*3117ece4Schristos      */
92*3117ece4Schristos     struct ZSTD_CCtx_params_s const* params;
93*3117ece4Schristos     /**
94*3117ece4Schristos      * The ZSTD_CCtx pointer (NULL on decompression).
95*3117ece4Schristos      */
96*3117ece4Schristos     struct ZSTD_CCtx_s const* cctx;
97*3117ece4Schristos     /**
98*3117ece4Schristos      * The ZSTD_DCtx pointer (NULL on compression).
99*3117ece4Schristos      */
100*3117ece4Schristos     struct ZSTD_DCtx_s const* dctx;
101*3117ece4Schristos } ZSTD_Trace;
102*3117ece4Schristos 
103*3117ece4Schristos /**
104*3117ece4Schristos  * A tracing context. It must be 0 when tracing is disabled.
105*3117ece4Schristos  * Otherwise, any non-zero value returned by a tracing begin()
106*3117ece4Schristos  * function is presented to any subsequent calls to end().
107*3117ece4Schristos  *
108*3117ece4Schristos  * Any non-zero value is treated as tracing is enabled and not
109*3117ece4Schristos  * interpreted by the library.
110*3117ece4Schristos  *
111*3117ece4Schristos  * Two possible uses are:
112*3117ece4Schristos  * * A timestamp for when the begin() function was called.
113*3117ece4Schristos  * * A unique key identifying the (de)compression, like the
114*3117ece4Schristos  *   address of the [dc]ctx pointer if you need to track
115*3117ece4Schristos  *   more information than just a timestamp.
116*3117ece4Schristos  */
117*3117ece4Schristos typedef unsigned long long ZSTD_TraceCtx;
118*3117ece4Schristos 
119*3117ece4Schristos /**
120*3117ece4Schristos  * Trace the beginning of a compression call.
121*3117ece4Schristos  * @param cctx The dctx pointer for the compression.
122*3117ece4Schristos  *             It can be used as a key to map begin() to end().
123*3117ece4Schristos  * @returns Non-zero if tracing is enabled. The return value is
124*3117ece4Schristos  *          passed to ZSTD_trace_compress_end().
125*3117ece4Schristos  */
126*3117ece4Schristos ZSTD_WEAK_ATTR ZSTD_TraceCtx ZSTD_trace_compress_begin(
127*3117ece4Schristos     struct ZSTD_CCtx_s const* cctx);
128*3117ece4Schristos 
129*3117ece4Schristos /**
130*3117ece4Schristos  * Trace the end of a compression call.
131*3117ece4Schristos  * @param ctx The return value of ZSTD_trace_compress_begin().
132*3117ece4Schristos  * @param trace The zstd tracing info.
133*3117ece4Schristos  */
134*3117ece4Schristos ZSTD_WEAK_ATTR void ZSTD_trace_compress_end(
135*3117ece4Schristos     ZSTD_TraceCtx ctx,
136*3117ece4Schristos     ZSTD_Trace const* trace);
137*3117ece4Schristos 
138*3117ece4Schristos /**
139*3117ece4Schristos  * Trace the beginning of a decompression call.
140*3117ece4Schristos  * @param dctx The dctx pointer for the decompression.
141*3117ece4Schristos  *             It can be used as a key to map begin() to end().
142*3117ece4Schristos  * @returns Non-zero if tracing is enabled. The return value is
143*3117ece4Schristos  *          passed to ZSTD_trace_compress_end().
144*3117ece4Schristos  */
145*3117ece4Schristos ZSTD_WEAK_ATTR ZSTD_TraceCtx ZSTD_trace_decompress_begin(
146*3117ece4Schristos     struct ZSTD_DCtx_s const* dctx);
147*3117ece4Schristos 
148*3117ece4Schristos /**
149*3117ece4Schristos  * Trace the end of a decompression call.
150*3117ece4Schristos  * @param ctx The return value of ZSTD_trace_decompress_begin().
151*3117ece4Schristos  * @param trace The zstd tracing info.
152*3117ece4Schristos  */
153*3117ece4Schristos ZSTD_WEAK_ATTR void ZSTD_trace_decompress_end(
154*3117ece4Schristos     ZSTD_TraceCtx ctx,
155*3117ece4Schristos     ZSTD_Trace const* trace);
156*3117ece4Schristos 
157*3117ece4Schristos #endif /* ZSTD_TRACE */
158*3117ece4Schristos 
159*3117ece4Schristos #if defined (__cplusplus)
160*3117ece4Schristos }
161*3117ece4Schristos #endif
162*3117ece4Schristos 
163*3117ece4Schristos #endif /* ZSTD_TRACE_H */
164