Skip to content

Commit

Permalink
Make the hdr histogram log optional if allowed and zlib not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeb01 committed Oct 1, 2020
1 parent b9c1b3e commit 16a9c93
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 26 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(HDR_SOVERSION_REVISION 0)

set(HDR_VERSION ${HDR_SOVERSION_CURRENT}.${HDR_SOVERSION_AGE}.${HDR_SOVERSION_REVISION})
set(HDR_SOVERSION ${HDR_SOVERSION_CURRENT})
option(HDR_LOG_REQUIRED "HDR Logging component required" ON)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
Expand All @@ -41,6 +42,14 @@ if(UNIX)
-D_GNU_SOURCE)
endif()

find_package(ZLIB)
find_package(Threads REQUIRED)

set(HDR_LOG_ENABLED ${ZLIB_FOUND})
if (${HDR_LOG_REQUIRED} AND NOT HDR_LOG_ENABLED)
message(SEND_ERROR "HDR_LOG_REQUIRED=ON and unable to find zlib library")
endif()

add_subdirectory(src)

option(HDR_HISTOGRAM_BUILD_PROGRAMS "Build tests and examples" ON)
Expand Down
44 changes: 23 additions & 21 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
add_executable(hdr_decoder
hdr_decoder.c)
target_link_libraries(hdr_decoder
PRIVATE
$<$<BOOL:${WIN32}>:hdr_histogram_static>
$<$<NOT:$<BOOL:${WIN32}>>:hdr_histogram>)
install(
TARGETS hdr_decoder
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${CMAKE_INSTALL_BINDIR})

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(Threads)

add_executable(hiccup
hiccup.c)
target_link_libraries(hiccup
if (HDR_LOG_ENABLED)
add_executable(hdr_decoder
hdr_decoder.c)
target_link_libraries(hdr_decoder
PRIVATE
hdr_histogram
Threads::Threads)
$<$<BOOL:${WIN32}>:hdr_histogram_static>
$<$<NOT:$<BOOL:${WIN32}>>:hdr_histogram>)
install(
TARGETS hiccup
TARGETS hdr_decoder
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(Threads)

add_executable(hiccup
hiccup.c)
target_link_libraries(hiccup
PRIVATE
hdr_histogram
Threads::Threads)
install(
TARGETS hiccup
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()
16 changes: 12 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
find_package(ZLIB REQUIRED)
find_package(Threads REQUIRED)

include(CheckLibraryExists)
check_library_exists(m ceil "" HAVE_LIBM)
check_library_exists(rt clock_gettime "" HAVE_LIBRT)

if (HDR_LOG_ENABLED)
set(HDR_LOG_IMPLEMENTATION hdr_histogram_log.c)
set(HDR_ZLIB ZLIB::ZLIB)
else()
set(HDR_LOG_IMPLEMENTATION hdr_histogram_log_no_op.c)
set(HDR_ZLIB "")
endif()

set(HDR_HISTOGRAM_SOURCES
hdr_encoding.c
hdr_histogram.c
hdr_histogram_log.c
${HDR_LOG_IMPLEMENTATION}
hdr_interval_recorder.c
hdr_thread.c
hdr_time.c
Expand All @@ -35,7 +41,9 @@ function(hdr_histogram_add_library NAME LIBRARY_TYPE DO_INSTALL)
${HDR_HISTOGRAM_PUBLIC_HEADERS})
target_link_libraries(${NAME}
PRIVATE
ZLIB::ZLIB
# ZLIB::ZLIB
${HDR_ZLIB}
# $<$<BOOL:${ZLIB_FOUND}>ZLIB::ZLIB>
Threads::Threads
$<$<BOOL:${HAVE_LIBM}>:m>
$<$<BOOL:${HAVE_LIBRT}>:rt>
Expand Down
171 changes: 171 additions & 0 deletions src/hdr_histogram_log_no_op.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* hdr_histogram_log.c
* Written by Michael Barker and released to the public domain,
* as explained at http://creativecommons.org/publicdomain/zero/1.0/
*/

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "hdr_histogram.h"
#include "hdr_histogram_log.h"
#include "hdr_tests.h"

#define UNUSED(x) (void)(x)

const char* hdr_strerror(int errnum)
{
switch (errnum)
{
case HDR_COMPRESSION_COOKIE_MISMATCH:
return "Compression cookie mismatch";
case HDR_ENCODING_COOKIE_MISMATCH:
return "Encoding cookie mismatch";
case HDR_DEFLATE_INIT_FAIL:
return "Deflate initialisation failed";
case HDR_DEFLATE_FAIL:
return "Deflate failed";
case HDR_INFLATE_INIT_FAIL:
return "Inflate initialisation failed";
case HDR_INFLATE_FAIL:
return "Inflate failed";
case HDR_LOG_INVALID_VERSION:
return "Log - invalid version in log header";
case HDR_TRAILING_ZEROS_INVALID:
return "Invalid number of trailing zeros";
case HDR_VALUE_TRUNCATED:
return "Truncated value found when decoding";
case HDR_ENCODED_INPUT_TOO_LONG:
return "The encoded input exceeds the size of the histogram";
default:
return strerror(errnum);
}
}

int hdr_encode_compressed(
struct hdr_histogram* h,
uint8_t** compressed_histogram,
size_t* compressed_len)
{
UNUSED(h);
UNUSED(compressed_histogram);
UNUSED(compressed_len);

return -1;
}

int hdr_decode_compressed(
uint8_t* buffer, size_t length, struct hdr_histogram** histogram)
{
UNUSED(buffer);
UNUSED(length);
UNUSED(histogram);

return -1;
}

int hdr_log_writer_init(struct hdr_log_writer* writer)
{
UNUSED(writer);

return -1;
}

int hdr_log_write_header(
struct hdr_log_writer* writer, FILE* file,
const char* user_prefix, hdr_timespec* timestamp)
{
UNUSED(writer);
UNUSED(file);
UNUSED(user_prefix);
UNUSED(timestamp);

return -1;
}

int hdr_log_write(
struct hdr_log_writer* writer,
FILE* file,
const hdr_timespec* start_timestamp,
const hdr_timespec* end_timestamp,
struct hdr_histogram* histogram)
{
UNUSED(writer);
UNUSED(file);
UNUSED(start_timestamp);
UNUSED(end_timestamp);
UNUSED(histogram);

return -1;
}

int hdr_log_write_entry(
struct hdr_log_writer* writer,
FILE* file,
struct hdr_log_entry* entry,
struct hdr_histogram* histogram)
{
UNUSED(writer);
UNUSED(file);
UNUSED(entry);
UNUSED(histogram);

return -1;
}

int hdr_log_reader_init(struct hdr_log_reader* reader)
{
UNUSED(reader);

return -1;
}

int hdr_log_read_header(struct hdr_log_reader* reader, FILE* file)
{
UNUSED(reader);
UNUSED(file);

return -1;
}

int hdr_log_read(
struct hdr_log_reader* reader, FILE* file, struct hdr_histogram** histogram,
hdr_timespec* timestamp, hdr_timespec* interval)
{
UNUSED(reader);
UNUSED(file);
UNUSED(histogram);
UNUSED(timestamp);
UNUSED(interval);

return -1;
}

int hdr_log_read_entry(
struct hdr_log_reader* reader, FILE* file, struct hdr_log_entry *entry, struct hdr_histogram** histogram)
{
UNUSED(reader);
UNUSED(file);
UNUSED(entry);
UNUSED(histogram);

return -1;
}

int hdr_log_encode(struct hdr_histogram* histogram, char** encoded_histogram)
{
UNUSED(histogram);
UNUSED(encoded_histogram);

return -1;
}

int hdr_log_decode(struct hdr_histogram** histogram, char* base64_histogram, size_t base64_len)
{
UNUSED(histogram);
UNUSED(base64_histogram);
UNUSED(base64_len);

return -1;
}
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ endfunction()

hdr_histogram_add_test(hdr_histogram_test)
hdr_histogram_add_test(hdr_histogram_atomic_test)
hdr_histogram_add_test(hdr_histogram_log_test)
if (HDR_LOG_ENABLED)
hdr_histogram_add_test(hdr_histogram_log_test)
endif()
hdr_histogram_add_test(hdr_atomic_test)
if(UNIX)
hdr_histogram_add_test(hdr_histogram_atomic_concurrency_test)
Expand Down

0 comments on commit 16a9c93

Please sign in to comment.