|
12 | 12 | #include <stdlib.h> |
13 | 13 | #include <stdio.h> |
14 | 14 |
|
| 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 | + |
15 | 51 | static int |
16 | 52 | _try_load(lua_State *L, const char * path, int pathlen, const char * name) { |
17 | 53 | int namelen = strlen(name); |
@@ -178,7 +214,13 @@ _launch(struct skynet_context * context, void *ud, int type, int session, uint32 |
178 | 214 | assert(type == 0 && session == 0); |
179 | 215 | struct snlua *l = ud; |
180 | 216 | 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) { |
182 | 224 | skynet_command(context, "EXIT", NULL); |
183 | 225 | } |
184 | 226 |
|
|
0 commit comments