zstd_trace.h 4.5 KB

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