compression.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "rrdpush.h"
  2. #ifdef ENABLE_RRDPUSH_COMPRESSION
  3. #include "lz4.h"
  4. #define STREAM_COMPRESSION_MSG "STREAM_COMPRESSION"
  5. /*
  6. * Reset compressor state for a new stream
  7. */
  8. void rrdpush_compressor_reset(struct compressor_state *state) {
  9. if(!state->initialized) {
  10. state->initialized = true;
  11. state->stream.lz4_stream = LZ4_createStream();
  12. state->stream.input_ring_buffer_size = LZ4_DECODER_RING_BUFFER_SIZE(COMPRESSION_MAX_MSG_SIZE * 2);
  13. state->stream.input_ring_buffer = callocz(1, state->stream.input_ring_buffer_size);
  14. state->compression_result_buffer_size = 0;
  15. }
  16. LZ4_resetStream_fast(state->stream.lz4_stream);
  17. state->stream.input_ring_buffer_pos = 0;
  18. }
  19. /*
  20. * Destroy compressor state and all related data
  21. */
  22. void rrdpush_compressor_destroy(struct compressor_state *state) {
  23. if (state->stream.lz4_stream) {
  24. LZ4_freeStream(state->stream.lz4_stream);
  25. state->stream.lz4_stream = NULL;
  26. }
  27. freez(state->stream.input_ring_buffer);
  28. state->stream.input_ring_buffer = NULL;
  29. freez(state->compression_result_buffer);
  30. state->compression_result_buffer = NULL;
  31. state->initialized = false;
  32. }
  33. /*
  34. * Compress the given block of data
  35. * Compressed data will remain in the internal buffer until the next invocation
  36. * Return the size of compressed data block as result and the pointer to internal buffer using the last argument
  37. * or 0 in case of error
  38. */
  39. size_t rrdpush_compress(struct compressor_state *state, const char *data, size_t size, char **out) {
  40. if(unlikely(!state || !size || !out))
  41. return 0;
  42. if(unlikely(size > COMPRESSION_MAX_MSG_SIZE)) {
  43. netdata_log_error("RRDPUSH COMPRESS: Compression Failed - Message size %lu above compression buffer limit: %d",
  44. (long unsigned int)size, COMPRESSION_MAX_MSG_SIZE);
  45. return 0;
  46. }
  47. size_t max_dst_size = LZ4_COMPRESSBOUND(size);
  48. size_t data_size = max_dst_size + RRDPUSH_COMPRESSION_SIGNATURE_SIZE;
  49. if (!state->compression_result_buffer) {
  50. state->compression_result_buffer = mallocz(data_size);
  51. state->compression_result_buffer_size = data_size;
  52. }
  53. else if(unlikely(state->compression_result_buffer_size < data_size)) {
  54. state->compression_result_buffer = reallocz(state->compression_result_buffer, data_size);
  55. state->compression_result_buffer_size = data_size;
  56. }
  57. // the ring buffer always has space for LZ4_MAX_MSG_SIZE
  58. memcpy(state->stream.input_ring_buffer + state->stream.input_ring_buffer_pos, data, size);
  59. // this call needs the last 64K of our previous data
  60. // they are available in the ring buffer
  61. long int compressed_data_size = LZ4_compress_fast_continue(
  62. state->stream.lz4_stream,
  63. state->stream.input_ring_buffer + state->stream.input_ring_buffer_pos,
  64. state->compression_result_buffer + RRDPUSH_COMPRESSION_SIGNATURE_SIZE,
  65. (int)size,
  66. (int)max_dst_size,
  67. 1);
  68. if (compressed_data_size < 0) {
  69. netdata_log_error("Data compression error: %ld", compressed_data_size);
  70. return 0;
  71. }
  72. // update the next writing position of the ring buffer
  73. state->stream.input_ring_buffer_pos += size;
  74. if(unlikely(state->stream.input_ring_buffer_pos >= state->stream.input_ring_buffer_size - COMPRESSION_MAX_MSG_SIZE))
  75. state->stream.input_ring_buffer_pos = 0;
  76. // update the signature header
  77. uint32_t len = ((compressed_data_size & 0x7f) | 0x80 | (((compressed_data_size & (0x7f << 7)) << 1) | 0x8000)) << 8;
  78. *(uint32_t *)state->compression_result_buffer = len | RRDPUSH_COMPRESSION_SIGNATURE;
  79. *out = state->compression_result_buffer;
  80. netdata_log_debug(D_STREAM, "%s: Compressed data header: %ld", STREAM_COMPRESSION_MSG, compressed_data_size);
  81. return compressed_data_size + RRDPUSH_COMPRESSION_SIGNATURE_SIZE;
  82. }
  83. /*
  84. * Decompress the compressed data in the internal buffer
  85. * Return the size of uncompressed data or 0 for error
  86. */
  87. size_t rrdpush_decompress(struct decompressor_state *state, const char *compressed_data, size_t compressed_size) {
  88. if (unlikely(!state || !compressed_data || !compressed_size))
  89. return 0;
  90. if(unlikely(state->stream.read_at != state->stream.write_at))
  91. fatal("RRDPUSH_DECOMPRESS: asked to decompress new data, while there are unread data in the decompression buffer!");
  92. if (unlikely(state->stream.write_at >= state->stream.size / 2)) {
  93. state->stream.write_at = 0;
  94. state->stream.read_at = 0;
  95. }
  96. long int decompressed_size = LZ4_decompress_safe_continue(
  97. state->stream.lz4_stream
  98. , compressed_data
  99. , state->stream.buffer + state->stream.write_at
  100. , (int)compressed_size
  101. , (int)(state->stream.size - state->stream.write_at)
  102. );
  103. if (unlikely(decompressed_size < 0)) {
  104. netdata_log_error("RRDPUSH DECOMPRESS: decompressor returned negative decompressed bytes: %ld", decompressed_size);
  105. return 0;
  106. }
  107. if(unlikely(decompressed_size + state->stream.write_at > state->stream.size))
  108. fatal("RRDPUSH DECOMPRESS: decompressor overflown the stream_buffer. size: %zu, pos: %zu, added: %ld, "
  109. "exceeding the buffer by %zu"
  110. , state->stream.size
  111. , state->stream.write_at
  112. , decompressed_size
  113. , (size_t)(state->stream.write_at + decompressed_size - state->stream.size)
  114. );
  115. state->stream.write_at += decompressed_size;
  116. // statistics
  117. state->total_compressed += compressed_size + RRDPUSH_COMPRESSION_SIGNATURE_SIZE;
  118. state->total_uncompressed += decompressed_size;
  119. state->packet_count++;
  120. return decompressed_size;
  121. }
  122. void rrdpush_decompressor_reset(struct decompressor_state *state) {
  123. if(!state->initialized) {
  124. state->initialized = true;
  125. state->stream.lz4_stream = LZ4_createStreamDecode();
  126. state->stream.size = LZ4_decoderRingBufferSize(COMPRESSION_MAX_MSG_SIZE) * 2;
  127. state->stream.buffer = mallocz(state->stream.size);
  128. }
  129. LZ4_setStreamDecode(state->stream.lz4_stream, NULL, 0);
  130. state->signature_size = RRDPUSH_COMPRESSION_SIGNATURE_SIZE;
  131. state->stream.write_at = 0;
  132. state->stream.read_at = 0;
  133. }
  134. void rrdpush_decompressor_destroy(struct decompressor_state *state) {
  135. if(unlikely(!state->initialized))
  136. return;
  137. if (state->stream.lz4_stream) {
  138. LZ4_freeStreamDecode(state->stream.lz4_stream);
  139. state->stream.lz4_stream = NULL;
  140. }
  141. freez(state->stream.buffer);
  142. state->stream.buffer = NULL;
  143. state->initialized = false;
  144. }
  145. #endif