Skip to content

Commit

Permalink
Squashed 'deps/ccommon/' changes from f5efe29..1c8df42 (#228)
Browse files Browse the repository at this point in the history
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: 1c8df42
  • Loading branch information
Yao Yue authored Jul 11, 2019
1 parent 4e4fa90 commit 35b5218
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 23 deletions.
30 changes: 23 additions & 7 deletions deps/ccommon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 "
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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})
Expand Down
18 changes: 18 additions & 0 deletions deps/ccommon/include/cc_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ extern "C" {
* _INSERT_TAIL - - + + +
* _REMOVE_HEAD + - + - -
* _REMOVE + + + + +
* _REINIT - - - + -
*
*/

Expand Down Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions deps/ccommon/include/cc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
23 changes: 22 additions & 1 deletion deps/ccommon/include/time/cc_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,6 +66,7 @@ struct duration {
};
#elif defined OS_LINUX
struct duration {
enum duration_type type;
bool started;
bool stopped;
struct timespec start;
Expand Down Expand Up @@ -86,19 +95,31 @@ 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);
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.
Expand Down
3 changes: 1 addition & 2 deletions deps/ccommon/src/cc_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include <string.h>
#include <sys/mman.h>

/* 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 <malloc/malloc.h>
#define malloc_usable_size malloc_size
Expand Down
6 changes: 6 additions & 0 deletions deps/ccommon/src/time/cc_timer_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
42 changes: 30 additions & 12 deletions deps/ccommon/src/time/cc_timer_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -87,16 +105,17 @@ 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
duration_stop(struct duration *d)
{
ASSERT(d != NULL);

_gettime(&d->stop);
_gettime(&d->stop, d->type);
d->stopped = true;
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion deps/ccommon/test-coverage.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 35b5218

Please sign in to comment.