diff --git a/README.md b/README.md index 6941c2d..2499a0d 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,9 @@ log_level log_stats_samples - Values: 0-INT_MAX. Default 1000 -- Append statistics to log every N samples (this option specifies N). +- This option applies only if the shim is built with ENABLE_STATISTICS=ON. +- Append statistics to log every N samples (this option specifies N). A sample is one deflate() or inflate() call. +- If set to 0, statistics are not appended to the log. log_file - Values: path. Default: /tmp/zlib-accel.log diff --git a/config/default_config b/config/default_config index c735aa2..353e3de 100644 --- a/config/default_config +++ b/config/default_config @@ -13,4 +13,5 @@ qat_compression_allow_chunking = 0 ignore_zlib_dictionary = 0 map_shards = 64 log_level = 1 +log_stats_samples = 1000 log_file = /tmp/zlib-accel.log diff --git a/iaa.cpp b/iaa.cpp index 2bbcd27..92e5c09 100644 --- a/iaa.cpp +++ b/iaa.cpp @@ -6,6 +6,7 @@ #include "iaa.h" #include +#include #include #include #include @@ -90,8 +91,8 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output, uint32_t output_shift = 0; if (gzip_ext) { - job->next_out_ptr += GZIP_EXT_XHDR_SIZE; if (job->available_out >= GZIP_EXT_XHDR_SIZE) { + job->next_out_ptr += GZIP_EXT_XHDR_SIZE; job->available_out -= GZIP_EXT_XHDR_SIZE; } else { return 1; @@ -158,10 +159,11 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output, output[pos++] = 8; // LEN output[pos++] = 0; - *(uint32_t*)(output + pos) = *input_length; + std::memcpy(output + pos, input_length, sizeof(uint32_t)); pos += 4; - *(uint32_t*)(output + pos) = + uint32_t tmp_val = *output_length - header_length - GetTrailerLength(format); + std::memcpy(output + pos, &tmp_val, sizeof(uint32_t)); pos += 4; *output_length += GZIP_EXT_XHDR_SIZE; diff --git a/logging.h b/logging.h index 6a151ff..9d86266 100644 --- a/logging.h +++ b/logging.h @@ -47,11 +47,14 @@ inline std::ostream& GetLogStream() { return std::cout; } +// Serializes writes to the stream returned by GetLogStream(). Shared by Log() +// and LogStats(), which may write to the same stream from different threads. +inline std::mutex log_mutex; + #endif // DEBUG_LOG || ENABLE_STATISTICS #ifdef DEBUG_LOG -static std::mutex log_mutex; template inline void Log(LogLevel level, Args&&... args) { std::lock_guard lock(log_mutex); @@ -92,6 +95,7 @@ inline void Log(LogLevel level, Args&&... args) { template inline void LogStats(Args&&... args) { + std::lock_guard lock(log_mutex); std::ostream& stream = GetLogStream(); stream << "Stats:\n"; (..., (stream << args)); diff --git a/statistics.cpp b/statistics.cpp index 9dabeb6..633208b 100644 --- a/statistics.cpp +++ b/statistics.cpp @@ -25,6 +25,9 @@ const std::array stat_names{ thread_local std::array stats{}; void PrintStats() { + if (configs[LOG_STATS_SAMPLES] == 0) { + return; + } auto total_operations = stats[static_cast(Statistic::DEFLATE_COUNT)] + stats[static_cast(Statistic::INFLATE_COUNT)]; if (total_operations % configs[LOG_STATS_SAMPLES] != 0) { diff --git a/utils.cpp b/utils.cpp index 82ff618..969e12d 100644 --- a/utils.cpp +++ b/utils.cpp @@ -4,6 +4,7 @@ #include "utils.h" #include +#include CompressedFormat GetCompressedFormat(int window_bits) { if (window_bits >= -15 && window_bits <= -8) { @@ -86,7 +87,7 @@ bool DetectGzipExt(uint8_t* data, uint32_t len, uint32_t* src_size, } // Extract sizes from extended header - *src_size = *(reinterpret_cast(data + 16)); - *dest_size = *(reinterpret_cast(data + 20)); + std::memcpy(src_size, data + 16, sizeof(uint32_t)); + std::memcpy(dest_size, data + 20, sizeof(uint32_t)); return true; }