forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-profile.c
187 lines (150 loc) · 3.89 KB
/
lua-profile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <lua.h>
#include <lauxlib.h>
#include <time.h>
#if defined(__APPLE__)
#include <mach/task.h>
#include <mach/mach.h>
#endif
#define NANOSEC 1000000000
#define MICROSEC 1000000
static double
get_time() {
#if !defined(__APPLE__)
struct timespec ti;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ti);
int sec = ti.tv_sec & 0xffff;
int nsec = ti.tv_nsec;
return (double)sec + (double)nsec / NANOSEC;
#else
struct task_thread_times_info aTaskInfo;
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
if (KERN_SUCCESS != task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount)) {
return 0;
}
int sec = aTaskInfo.user_time.seconds & 0xffff;
int msec = aTaskInfo.user_time.microseconds;
return (double)sec + (double)msec / MICROSEC;
#endif
}
static inline double
diff_time(double start) {
double now = get_time();
if (now < start) {
return now + 0x10000 - start;
} else {
return now - start;
}
}
static int
lstart(lua_State *L) {
lua_pushthread(L);
lua_rawget(L, lua_upvalueindex(2));
if (!lua_isnil(L, -1)) {
return luaL_error(L, "Thread %p start profile more than once", lua_topointer(L, 1));
}
lua_pushthread(L);
lua_pushnumber(L, 0);
lua_rawset(L, lua_upvalueindex(2));
lua_pushthread(L);
lua_pushnumber(L, get_time());
lua_rawset(L, lua_upvalueindex(1));
return 0;
}
static int
lstop(lua_State *L) {
lua_pushthread(L);
lua_rawget(L, lua_upvalueindex(1));
luaL_checktype(L, -1, LUA_TNUMBER);
double ti = diff_time(lua_tonumber(L, -1));
lua_pushthread(L);
lua_rawget(L, lua_upvalueindex(2));
double total_time = lua_tonumber(L, -1);
lua_pushthread(L);
lua_pushnil(L);
lua_rawset(L, lua_upvalueindex(1));
lua_pushthread(L);
lua_pushnil(L);
lua_rawset(L, lua_upvalueindex(2));
lua_pushnumber(L, ti + total_time);
return 1;
}
static int
lresume(lua_State *L) {
lua_pushvalue(L,1);
lua_rawget(L, lua_upvalueindex(2));
if (lua_isnil(L, -1)) { // check total time
lua_pop(L,1);
} else {
lua_pop(L,1);
lua_pushvalue(L,1);
double ti = get_time();
lua_pushnumber(L, ti);
lua_rawset(L, lua_upvalueindex(1)); // set start time
}
lua_CFunction co_resume = lua_tocfunction(L, lua_upvalueindex(3));
return co_resume(L);
}
static int
lyield(lua_State *L) {
lua_pushthread(L);
lua_rawget(L, lua_upvalueindex(2)); // check total time
if (lua_isnil(L, -1)) {
lua_pop(L,1);
} else {
double ti = lua_tonumber(L, -1);
lua_pop(L,1);
lua_pushthread(L);
lua_rawget(L, lua_upvalueindex(1));
double starttime = lua_tonumber(L, -1);
lua_pop(L,1);
ti += diff_time(starttime);
lua_pushthread(L);
lua_pushnumber(L, ti);
lua_rawset(L, lua_upvalueindex(2));
}
lua_CFunction co_yield = lua_tocfunction(L, lua_upvalueindex(3));
return co_yield(L);
}
int
luaopen_profile(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "start", lstart },
{ "stop", lstop },
{ "resume", lresume },
{ "yield", lyield },
{ NULL, NULL },
};
luaL_newlibtable(L,l);
lua_newtable(L); // table thread->start time
lua_newtable(L); // table thread->total time
lua_newtable(L); // weak table
lua_pushliteral(L, "kv");
lua_setfield(L, -2, "__mode");
lua_pushvalue(L, -1);
lua_setmetatable(L, -3);
lua_setmetatable(L, -3);
lua_pushnil(L);
luaL_setfuncs(L,l,3);
int libtable = lua_gettop(L);
lua_getglobal(L, "coroutine");
lua_getfield(L, -1, "resume");
lua_CFunction co_resume = lua_tocfunction(L, -1);
if (co_resume == NULL)
return luaL_error(L, "Can't get coroutine.resume");
lua_pop(L,1);
lua_getfield(L, libtable, "resume");
lua_pushcfunction(L, co_resume);
lua_setupvalue(L, -2, 3);
lua_pop(L,1);
lua_getfield(L, -1, "yield");
lua_CFunction co_yield = lua_tocfunction(L, -1);
if (co_yield == NULL)
return luaL_error(L, "Can't get coroutine.yield");
lua_pop(L,1);
lua_getfield(L, libtable, "yield");
lua_pushcfunction(L, co_yield);
lua_setupvalue(L, -2, 3);
lua_settop(L, libtable);
return 1;
}