From ed09f28481d056fe3ec9bd44046a6a4ae4d9ca37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Wed, 18 Sep 2013 16:14:50 +0800 Subject: [PATCH] function:current time --- lualib-src/lua-skynet.c | 10 +--------- lualib-src/trace_service.c | 41 ++++++++++++++++---------------------- lualib-src/trace_service.h | 1 + 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 1424c92d..8e1631ab 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -32,15 +32,7 @@ struct stat { static void _stat_begin(struct stat *S, struct timespec *ti) { S->count++; -#if !defined(__APPLE__) - clock_gettime(CLOCK_THREAD_CPUTIME_ID, ti); -#else - struct task_thread_times_info aTaskInfo; - mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; - assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)); - ti->tv_sec = aTaskInfo.user_time.seconds; - ti->tv_nsec = aTaskInfo.user_time.microseconds * 1000; -#endif + current_time(ti); } inline static void diff --git a/lualib-src/trace_service.c b/lualib-src/trace_service.c index 23deb40f..ebef3dcf 100644 --- a/lualib-src/trace_service.c +++ b/lualib-src/trace_service.c @@ -13,18 +13,27 @@ #include #endif -void -diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec) { - struct timespec end; +void +current_time(struct timespec *ti) { #if !defined(__APPLE__) - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end); + clock_gettime(CLOCK_THREAD_CPUTIME_ID, ti); #else struct task_thread_times_info aTaskInfo; mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)); - end.tv_sec = aTaskInfo.user_time.seconds; - end.tv_nsec = aTaskInfo.user_time.microseconds * 1000; + ti->tv_sec = aTaskInfo.user_time.seconds; + ti->tv_nsec = aTaskInfo.user_time.microseconds * 1000; #endif +} + +void +diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec) { + if (sec == NULL) { + current_time(ti); + return; + } + struct timespec end; + current_time(&end); int diffsec = end.tv_sec - ti->tv_sec; assert(diffsec>=0); int diffnsec = end.tv_nsec - ti->tv_nsec; @@ -91,15 +100,7 @@ trace_new(struct trace_pool *p) { t->next = NULL; t->ti_sec = 0; t->ti_nsec = 0; -#if !defined(__APPLE__) - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t->ti); -#else - struct task_thread_times_info aTaskInfo; - mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; - assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)); - t->ti.tv_sec = aTaskInfo.user_time.seconds; - t->ti.tv_nsec = aTaskInfo.user_time.microseconds * 1000; -#endif + current_time(&t->ti); return t; } @@ -153,15 +154,7 @@ trace_switch(struct trace_pool *p, int session) { if (t->next) { t->next->prev = prev; } -#if !defined(__APPLE__) - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t->ti); -#else - struct task_thread_times_info aTaskInfo; - mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; - assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)); - t->ti.tv_sec = aTaskInfo.user_time.seconds; - t->ti.tv_nsec = aTaskInfo.user_time.microseconds * 1000; -#endif + current_time(&t->ti); return; } prev = t; diff --git a/lualib-src/trace_service.h b/lualib-src/trace_service.h index 61fbc79a..961dc94d 100644 --- a/lualib-src/trace_service.h +++ b/lualib-src/trace_service.h @@ -6,6 +6,7 @@ #define NANOSEC 1000000000 +void current_time(struct timespec *ti); void diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec); struct trace_pool;