From 2bf65084cd20fde41f9dc59df291fe28632c196e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Tue, 17 Dec 2013 11:24:30 +0800 Subject: [PATCH] debug stat report boot time --- lualib-src/trace_service.c | 4 ---- lualib/skynet.lua | 1 + service-src/service_lua.c | 44 +++++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lualib-src/trace_service.c b/lualib-src/trace_service.c index ebef3dcf..a7cbcf9b 100644 --- a/lualib-src/trace_service.c +++ b/lualib-src/trace_service.c @@ -28,10 +28,6 @@ current_time(struct timespec *ti) { 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; diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 2756fefd..4c42a9cb 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -509,6 +509,7 @@ function dbgcmd.STAT() local stat = {} query_state(stat, "count") query_state(stat, "time") + stat.boottime = debug.getregistry().skynet_boottime skynet.ret(skynet.pack(stat)) end diff --git a/service-src/service_lua.c b/service-src/service_lua.c index 5e8f01a3..144ca5f5 100644 --- a/service-src/service_lua.c +++ b/service-src/service_lua.c @@ -12,6 +12,42 @@ #include #include +// time + +#include +#define NANOSEC 1000000000 + +#if defined(__APPLE__) +#include +#include +#endif + +static void +current_time(struct timespec *ti) { +#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 +} + +static double +diff_time(struct timespec *ti) { + struct timespec end; + current_time(&end); + int diffsec = end.tv_sec - ti->tv_sec; + int diffnsec = end.tv_nsec - ti->tv_nsec; + if (diffnsec < 0) { + --diffsec; + diffnsec += NANOSEC; + } + return (double)diffsec + (double)diffnsec / NANOSEC; +} + static int _try_load(lua_State *L, const char * path, int pathlen, const char * name) { int namelen = strlen(name); @@ -178,7 +214,13 @@ _launch(struct skynet_context * context, void *ud, int type, int session, uint32 assert(type == 0 && session == 0); struct snlua *l = ud; skynet_callback(context, NULL, NULL); - if (_init(l, context, msg)) { + struct timespec ti; + current_time(&ti); + int err = _init(l, context, msg); + double t = diff_time(&ti); + lua_pushnumber(l->L, t); + lua_setfield(l->L, LUA_REGISTRYINDEX, "skynet_boottime"); + if (err) { skynet_command(context, "EXIT", NULL); }