Skip to content

Commit

Permalink
debug stat report boot time
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Dec 17, 2013
1 parent b6f57ae commit 2bf6508
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
4 changes: 0 additions & 4 deletions lualib-src/trace_service.c
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lualib/skynet.lua
Expand Up @@ -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

Expand Down
44 changes: 43 additions & 1 deletion service-src/service_lua.c
Expand Up @@ -12,6 +12,42 @@
#include <stdlib.h>
#include <stdio.h>

// time

#include <time.h>
#define NANOSEC 1000000000

#if defined(__APPLE__)
#include <mach/task.h>
#include <mach/mach.h>
#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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 2bf6508

Please sign in to comment.