From 35b521830cf355e9253093fc3d959e0fa1f7d08c Mon Sep 17 00:00:00 2001 From: Yao Yue Date: Thu, 11 Jul 2019 10:07:24 -0700 Subject: [PATCH] Squashed 'deps/ccommon/' changes from f5efe29..1c8df42 (#228) 1c8df42 Add basic support of build type (#199) 7107988 Fix now_ns() (#198) da240e5 cc: extend cc_util module (#196) 4846b15 Fix TAILQ_REINIT (#195) 4f5dbb0 Update Cmake version to 2.8 (#197) 2e6f78a cc_mm use OS_DARWIN macro to detect OS (#194) 57acaf6 cc: extend queue module (#193) a64ada2 cc: extend duration module (#192) b117632 reverting CMake file changes (#191) dea5bee backport changes made to ccommon in pelikan (#190) a4c0334 add linebreak to stats_log() (#188) 05eb03e fix inconsistent naming and bump version (#187) 4acc53a Stats to file (#186) 2168fec minimize osx build config (#185) 42b24de Simplify rust options, specify fewer output targets (#183) c9fa905 update CMakeRust used to latest version, tweaks to make build work (#184) 2ef0163 Reorder dependency includes in cmake, don't parallel build (#182) a6a54d9 remove endian-specific logic from str*cmp (#177) 4c0668b epoll_create* ignores size hint in newer kernels, switch to new API (#179) c9c5ee5 improve cc_bstring string literal and cstring names (#176) 0184d73 Add unit tests for buffer, fix buf/dbuf bugs and refactor (#174) d7dab43 create a .cargo/config so intellij uses the same target dir as cmake (#173) e710712 use accept4 for tcp_accept when available (#171) 21ba10e Remove cargo lock for shared lib, closes #169 (#172) 24660f1 update style guide (#170) 17baf1e Per thread logging (#168) git-subtree-dir: deps/ccommon git-subtree-split: 1c8df4200ca245fc87b07a235b490e91e88f7b48 --- deps/ccommon/CMakeLists.txt | 30 +++++++++++++----- deps/ccommon/include/cc_queue.h | 18 +++++++++++ deps/ccommon/include/cc_util.h | 3 ++ deps/ccommon/include/time/cc_timer.h | 23 +++++++++++++- deps/ccommon/src/cc_mm.c | 3 +- deps/ccommon/src/time/cc_timer_darwin.c | 6 ++++ deps/ccommon/src/time/cc_timer_linux.c | 42 ++++++++++++++++++------- deps/ccommon/test-coverage.sh | 2 +- 8 files changed, 104 insertions(+), 23 deletions(-) diff --git a/deps/ccommon/CMakeLists.txt b/deps/ccommon/CMakeLists.txt index f6ddf7597..e81bac89c 100644 --- a/deps/ccommon/CMakeLists.txt +++ b/deps/ccommon/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8) project(ccommon C) # Uncomment the following to output dependency graph debugging messages @@ -115,12 +115,23 @@ configure_file( # set compiler flags # string concat is easier in 3.0, but older versions don't have the concat subcommand # so we are using list as input until we move to new version -# TODO add build types add_definitions(-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64) +# Set a default build type (Release) if none was specified + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + +if(CMAKE_BUILD_TYPE MATCHES Debug) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") +else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2") +endif() + set(CMAKE_MACOSX_RPATH 1) set(CFLAGS_LIST "-std=c11 " - "-ggdb3 -O2 " + "-ggdb3 " "-Wall " "-Wmissing-prototypes -Wmissing-declarations -Wredundant-decls " "-Wunused-function -Wunused-value -Wunused-variable " @@ -133,7 +144,10 @@ if(CMAKE_COMPILER_IS_GNUCC) endif() if (COVERAGE) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -W -fprofile-arcs -ftest-coverage") + if(NOT ${CMAKE_BUILD_TYPE} MATCHES Debug) + message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" ) + endif() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") endif(COVERAGE) # test dependencies @@ -165,10 +179,10 @@ include_directories( add_subdirectory(src) -if(CHECK_FOUND) - include_directories(${include_directories} ${CHECK_INCLUDES}) +if(CHECK_WORKING) + include_directories(${include_directories} "${CHECK_INCLUDES}") add_subdirectory(test) -endif(CHECK_FOUND) +endif(CHECK_WORKING) if(HAVE_RUST) @@ -183,6 +197,8 @@ endif() # print a summary # ################### +message(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE}) + message(STATUS "PLATFORM: " ${OS_PLATFORM}) message(STATUS "CPPFLAGS: " ${CMAKE_CPP_FLAGS}) diff --git a/deps/ccommon/include/cc_queue.h b/deps/ccommon/include/cc_queue.h index c8f8a5ce2..06bf9c31a 100644 --- a/deps/ccommon/include/cc_queue.h +++ b/deps/ccommon/include/cc_queue.h @@ -127,6 +127,7 @@ extern "C" { * _INSERT_TAIL - - + + + * _REMOVE_HEAD + - + - - * _REMOVE + + + + + + * _REINIT - - - + - * */ @@ -691,6 +692,23 @@ struct { \ (head2)->tqh_last = &(head2)->tqh_first; \ } while (0) +#define TAILQ_REINIT(head, var, field, offset) do { \ + TAILQ_FIRST((head)) = var; \ + TAILQ_FOREACH(var, head, field) { \ + if ((TAILQ_NEXT((var), field)) != NULL) { \ + TAILQ_NEXT((var), field) = \ + (void *)((char *)(TAILQ_NEXT((var), field)) + (offset));\ + } \ + if ((var) == TAILQ_FIRST(head)) { \ + (var)->field.tqe_prev = &TAILQ_FIRST(head); \ + } else { \ + (var)->field.tqe_prev = \ + (void *)((char *)((var)->field.tqe_prev) + (offset)); \ + } \ + (head)->tqh_last = &TAILQ_NEXT((var), field); \ + } \ +} while (0) + /* * Circular queue declarations. */ diff --git a/deps/ccommon/include/cc_util.h b/deps/ccommon/include/cc_util.h index d9c13955b..f8a770059 100644 --- a/deps/ccommon/include/cc_util.h +++ b/deps/ccommon/include/cc_util.h @@ -86,6 +86,9 @@ extern "C" { #define cc_strlen(_s) \ strlen((char *)(_s)) +#define cc_strnlen(_s, _n) \ + strnlen((char *)(_s), (size_t)(_n)) + #define cc_strcmp(_s1, _s2) \ strcmp((char *)(_s1), (char *)(_s2)) diff --git a/deps/ccommon/include/time/cc_timer.h b/deps/ccommon/include/time/cc_timer.h index 6a8c382ee..86b871a7a 100644 --- a/deps/ccommon/include/time/cc_timer.h +++ b/deps/ccommon/include/time/cc_timer.h @@ -49,6 +49,14 @@ struct duration; /* data structure to measure duration */ * relationship between this unit and nanosecond can be obtained via another * syscall */ + +enum duration_type { + DURATION_PRECISE, /* default */ + DURATION_FAST, + + MAX_DURATION_TYPE +}; + #ifdef OS_DARWIN struct duration { bool started; @@ -58,6 +66,7 @@ struct duration { }; #elif defined OS_LINUX struct duration { + enum duration_type type; bool started; bool stopped; struct timespec start; @@ -86,12 +95,12 @@ struct timeout { bool is_intvl; }; - /* update duration */ void duration_reset(struct duration *d); /* get a reading of duration and copy it without stopping the original timer */ void duration_snapshot(struct duration *s, const struct duration *d); void duration_start(struct duration *d); +void duration_start_type(struct duration *d, enum duration_type type); void duration_stop(struct duration *d); /* read duration */ double duration_ns(struct duration *d); @@ -99,6 +108,18 @@ double duration_us(struct duration *d); double duration_ms(struct duration *d); double duration_sec(struct duration *d); +static inline int +duration_compare(const void *lhs, const void *rhs) +{ + double lns = duration_ns((struct duration *)lhs); + double rns = duration_ns((struct duration *)rhs); + if (lns < rns) + return -1; + if (lns > rns) + return 1; + + return 0; +} /* * Not all possible granularity can be meaningfully used for sleep or event. diff --git a/deps/ccommon/src/cc_mm.c b/deps/ccommon/src/cc_mm.c index ef4ed349a..8fa28f980 100644 --- a/deps/ccommon/src/cc_mm.c +++ b/deps/ccommon/src/cc_mm.c @@ -24,8 +24,7 @@ #include #include -/* TODO(yao): detect OS in one place and use one variable everywhere */ -#if defined(__APPLE__) && defined(__MACH__) +#ifdef OS_DARWIN # define MAP_ANONYMOUS MAP_ANON #include #define malloc_usable_size malloc_size diff --git a/deps/ccommon/src/time/cc_timer_darwin.c b/deps/ccommon/src/time/cc_timer_darwin.c index 3127643be..a6c4110b1 100644 --- a/deps/ccommon/src/time/cc_timer_darwin.c +++ b/deps/ccommon/src/time/cc_timer_darwin.c @@ -72,6 +72,12 @@ duration_start(struct duration *d) d->start = mach_absolute_time(); } +void +duration_start_type(struct duration *d, enum duration_type type) +{ + duration_start(d); +} + void duration_stop(struct duration *d) { diff --git a/deps/ccommon/src/time/cc_timer_linux.c b/deps/ccommon/src/time/cc_timer_linux.c index 778fedf98..a3f8ba820 100644 --- a/deps/ccommon/src/time/cc_timer_linux.c +++ b/deps/ccommon/src/time/cc_timer_linux.c @@ -25,24 +25,35 @@ * CLOCK_REALTIME can be reset when clock has drifted, so it may not always be * monotonic, and should be avoided if possible. */ +static const clockid_t cid[MAX_DURATION_TYPE] = { + [DURATION_PRECISE] = #if defined(CLOCK_MONOTONIC_RAW) -static const clockid_t cid = CLOCK_MONOTONIC_RAW; + CLOCK_MONOTONIC_RAW #elif defined(CLOCK_MONOTONIC) -static const clockid_t cid = CLOCK_MONOTONIC; + CLOCK_MONOTONIC #elif defined(CLOCK_REALTIME) -static const clockid_t cid = CLOCK_REALTIME; + CLOCK_REALTIME #else -static const clockid_t cid = (clockid_t)-1; + (clockid_t)-1 #endif +, +#if defined(CLOCK_MONOTONIC) + CLOCK_MONOTONIC +#elif defined(CLOCK_REALTIME) + CLOCK_REALTIME +#else + (clockid_t)-1 +#endif +}; static inline void -_gettime(struct timespec *ts) +_gettime(struct timespec *ts, enum duration_type type) { int ret; - ASSERT(cid != (clockid_t)-1); + ASSERT(cid[type] != (clockid_t)-1); - ret = clock_gettime(cid, ts); + ret = clock_gettime(cid[type], ts); if (ret == -1) { /* * Note(yao): for the purpose of this module, it doesn't make much sense @@ -73,10 +84,17 @@ duration_reset(struct duration *d) void duration_start(struct duration *d) +{ + duration_start_type(d, DURATION_PRECISE); +} + +void +duration_start_type(struct duration *d, enum duration_type type) { ASSERT(d != NULL); - _gettime(&d->start); + _gettime(&d->start, type); + d->type = type; d->started = true; } @@ -87,8 +105,9 @@ duration_snapshot(struct duration *s, const struct duration *d) s->started = true; s->start = d->start; + s->type = d->type; s->stopped = true; - _gettime(&s->stop); + _gettime(&s->stop, s->type); } void @@ -96,7 +115,7 @@ duration_stop(struct duration *d) { ASSERT(d != NULL); - _gettime(&d->stop); + _gettime(&d->stop, d->type); d->stopped = true; } @@ -153,9 +172,8 @@ _now_ns(void) { struct timespec now; - _gettime(&now); + _gettime(&now, DURATION_PRECISE); return now.tv_sec * NSEC_PER_SEC + now.tv_nsec; - } void diff --git a/deps/ccommon/test-coverage.sh b/deps/ccommon/test-coverage.sh index f6d923818..59c7f0fa5 100755 --- a/deps/ccommon/test-coverage.sh +++ b/deps/ccommon/test-coverage.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -rm -rf lcov _build _bin && mkdir _build && pushd _build && cmake -DCOVERAGE=on .. && make -j && make test && popd +rm -rf lcov _build _bin && mkdir _build && pushd _build && cmake -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=on .. && make -j && make test && popd mkdir lcov lcov --directory . --capture --output-file lcov/app.info genhtml lcov/app.info -o lcov/html