Skip to content

Commit 2bf6508

Browse files
committed
debug stat report boot time
1 parent b6f57ae commit 2bf6508

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lualib-src/trace_service.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ current_time(struct timespec *ti) {
2828

2929
void
3030
diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec) {
31-
if (sec == NULL) {
32-
current_time(ti);
33-
return;
34-
}
3531
struct timespec end;
3632
current_time(&end);
3733
int diffsec = end.tv_sec - ti->tv_sec;

lualib/skynet.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ function dbgcmd.STAT()
509509
local stat = {}
510510
query_state(stat, "count")
511511
query_state(stat, "time")
512+
stat.boottime = debug.getregistry().skynet_boottime
512513
skynet.ret(skynet.pack(stat))
513514
end
514515

service-src/service_lua.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,42 @@
1212
#include <stdlib.h>
1313
#include <stdio.h>
1414

15+
// time
16+
17+
#include <time.h>
18+
#define NANOSEC 1000000000
19+
20+
#if defined(__APPLE__)
21+
#include <mach/task.h>
22+
#include <mach/mach.h>
23+
#endif
24+
25+
static void
26+
current_time(struct timespec *ti) {
27+
#if !defined(__APPLE__)
28+
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ti);
29+
#else
30+
struct task_thread_times_info aTaskInfo;
31+
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
32+
assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount));
33+
ti->tv_sec = aTaskInfo.user_time.seconds;
34+
ti->tv_nsec = aTaskInfo.user_time.microseconds * 1000;
35+
#endif
36+
}
37+
38+
static double
39+
diff_time(struct timespec *ti) {
40+
struct timespec end;
41+
current_time(&end);
42+
int diffsec = end.tv_sec - ti->tv_sec;
43+
int diffnsec = end.tv_nsec - ti->tv_nsec;
44+
if (diffnsec < 0) {
45+
--diffsec;
46+
diffnsec += NANOSEC;
47+
}
48+
return (double)diffsec + (double)diffnsec / NANOSEC;
49+
}
50+
1551
static int
1652
_try_load(lua_State *L, const char * path, int pathlen, const char * name) {
1753
int namelen = strlen(name);
@@ -178,7 +214,13 @@ _launch(struct skynet_context * context, void *ud, int type, int session, uint32
178214
assert(type == 0 && session == 0);
179215
struct snlua *l = ud;
180216
skynet_callback(context, NULL, NULL);
181-
if (_init(l, context, msg)) {
217+
struct timespec ti;
218+
current_time(&ti);
219+
int err = _init(l, context, msg);
220+
double t = diff_time(&ti);
221+
lua_pushnumber(l->L, t);
222+
lua_setfield(l->L, LUA_REGISTRYINDEX, "skynet_boottime");
223+
if (err) {
182224
skynet_command(context, "EXIT", NULL);
183225
}
184226

0 commit comments

Comments
 (0)