diff --git a/lib_fiber/c/src/common/gettimeofday.c b/lib_fiber/c/src/common/gettimeofday.c index decf40c5e..e1bdd49fe 100644 --- a/lib_fiber/c/src/common/gettimeofday.c +++ b/lib_fiber/c/src/common/gettimeofday.c @@ -143,6 +143,26 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) #elif defined(USE_FAST_TIME) +#include "fiber.h" + +typedef int (*gettimeofday_fn)(struct timeval *, struct timezone *); +static gettimeofday_fn __gettimeofday = NULL; + +static void hook_api(void) +{ + __gettimeofday = (gettimeofday_fn) dlsym(RTLD_NEXT, "gettimeofday"); + assert(__gettimeofday); +} + +static pthread_once_t __once_control = PTHREAD_ONCE_INIT; + +static void hook_init(void) +{ + if (pthread_once(&__once_control, hook_api) != 0) { + abort(); + } +} + static inline unsigned long long rte_rdtsc(void) { union { @@ -168,12 +188,13 @@ static void set_time_metric(void) unsigned long long now, startup, end; unsigned long long begin = rte_rdtsc(); -#define MSEC_ONE 1000 +#define MSEC_ONE 1000 +#define METRIC 50 - usleep(MSEC_ONE); + usleep(MSEC_ONE * METRIC); end = rte_rdtsc(); - __one_msec = end - begin; + __one_msec = (end - begin) / METRIC; __one_sec = __one_msec * 1000; startup = rte_rdtsc(); @@ -201,6 +222,13 @@ int acl_fiber_gettimeofday(struct timeval *tv, struct timezone *tz fiber_unused) int gettimeofday(struct timeval *tv, struct timezone *tz) { + if (!var_hook_sys_api) { + if (__gettimeofday == NULL) { + hook_init(); + } + return __gettimeofday(tv, tz); + } + return acl_fiber_gettimeofday(tv, tz); } diff --git a/lib_fiber/c/src/fiber.h b/lib_fiber/c/src/fiber.h index 9261c0f55..9d4ddc7ac 100644 --- a/lib_fiber/c/src/fiber.h +++ b/lib_fiber/c/src/fiber.h @@ -1,6 +1,7 @@ #ifndef FIBER_INCLUDE_H #define FIBER_INCLUDE_H +#include "common/ring.h" #include "event.h" #ifdef ACL_ARM_LINUX diff --git a/lib_fiber/c/src/hook/io.c b/lib_fiber/c/src/hook/io.c index 136651d2f..18b3284b3 100644 --- a/lib_fiber/c/src/hook/io.c +++ b/lib_fiber/c/src/hook/io.c @@ -222,13 +222,14 @@ static void hook_init(void) #ifdef SYS_UNIX unsigned int sleep(unsigned int seconds) { - if (!var_hook_sys_api) { + if (var_hook_sys_api) { #ifndef USE_SYSCALL if (__sys_sleep == NULL) { hook_init(); } #endif + printf("use system gettimeofday\r\n"); return __sys_sleep(seconds); } diff --git a/lib_fiber/changes.txt b/lib_fiber/changes.txt index 71c734e7e..b6bc28bbb 100644 --- a/lib_fiber/changes.txt +++ b/lib_fiber/changes.txt @@ -1,4 +1,7 @@ +110) 2019.8.6 +110.1) bugfix: gettimeofday 内部在进行时间校准时休眠 50 毫秒,之前是休眠 1 毫秒造成较大误差 + 109) 2019.7.31 109.1) performance: 因为 ring.h 中的方法被调用次数比较频繁,所以将其中代码实现 放在头文件中,成为内联函数或宏定义