-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_profiler.cpp
executable file
·342 lines (281 loc) · 9.36 KB
/
lua_profiler.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
** LuaProfiler
** Copyright Kepler Project 2005-2007 (http://www.keplerproject.org/luaprofiler)
** $Id: lua50_profiler.c,v 1.16 2008-05-20 21:16:36 mascarenhas Exp $
*/
/*****************************************************************************
lua50_profiler.c:
Lua version dependent profiler interface
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua_profiler.h"
#include "clocks.h"
#include "core_profiler.h"
#include "function_meter.h"
#include "lua.h"
#include "lauxlib.h"
/* Indices for the main profiler stack and for the original exit function */
static int exit_id;
static int profstate_id;
static int profresult_id;
/* Forward declaration */
//static float calcCallTime(lua_State *L);
static int profiler_stop(lua_State *L);
static int profiler_clear(lua_State *L);
//static int stack_level = 0;
/* called by Lua (via the callhook mechanism) */
static void callhook(lua_State *L, lua_Debug *ar) {
//if (ar->event == LUA_HOOKCALL){
// stack_level++;
//}
//else if (ar->event == LUA_HOOKRET){
// stack_level--;
//}
//else{
// printf("except event = %d******************************************", stack_level);
//}
//printf("call hook: event = %d; stack_level = %d\n", ar->event, stack_level);
int currentline;
lua_Debug previous_ar;
lprofP_STATE* S;
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
S = (lprofP_STATE*)lua_touserdata(L, -1);
if (lua_getstack(L, 1, &previous_ar) == 0) {
currentline = -1;
}
else {
lua_getinfo(L, "l", &previous_ar);
currentline = previous_ar.currentline;
}
lua_getinfo(L, "nS", ar);
if (!ar->event) {
/* entering a function */
lprofP_callhookIN(S, (char *)ar->name,
(char *)ar->source, ar->linedefined,
currentline);
lua_pop(L, 1);
}
else { /* ar->event == "return" */
lprofP_STATE* R;
lua_pushlightuserdata(L, &profresult_id);
lua_gettable(L, LUA_REGISTRYINDEX);
R = (lprofP_STATE*)lua_touserdata(L, -1);
lprofP_callhookOUT(S, R);
lua_pop(L, 2);
}
//printf("%d\n", lua_gettop(L));
}
/* Lua function to exit politely the profiler */
/* redefines the lua exit() function to not break the log file integrity */
/* The log file is assumed to be valid if the last entry has a stack level */
/* of 1 (meaning that the function 'main' has been exited) */
static void exit_profiler(lua_State *L) {
lprofP_STATE* S;
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
S = (lprofP_STATE*)lua_touserdata(L, -1);
lprofP_STATE* R;
lua_pushlightuserdata(L, &profresult_id);
lua_gettable(L, LUA_REGISTRYINDEX);
R = (lprofP_STATE*)lua_touserdata(L, -1);
/* leave all functions under execution */
while (lprofP_callhookOUT(S, R));
/* call the original Lua 'exit' function */
lua_pushlightuserdata(L, &exit_id);
lua_gettable(L, LUA_REGISTRYINDEX);
lua_call(L, 0, 0);
}
/* Our new coroutine.create function */
/* Creates a new profile state for the coroutine */
#if 0
static int coroutine_create(lua_State *L) {
lprofP_STATE* S;
lua_State *NL = lua_newthread(L);
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
"Lua function expected");
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
/* Inits profiler and sets profiler hook for this coroutine */
S = lprofM_init();
lua_pushlightuserdata(L, NL);
lua_pushlightuserdata(L, S);
lua_settable(L, LUA_REGISTRYINDEX);
lua_sethook(NL, (lua_Hook)callhook, LUA_MASKCALL | LUA_MASKRET, 0);
return 1;
}
#endif
static int profiler_pause(lua_State *L) {
lprofP_STATE* S;
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
S = (lprofP_STATE*)lua_touserdata(L, -1);
lprofM_pause_function(S);
return 0;
}
static int profiler_resume(lua_State *L) {
lprofP_STATE* S;
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
S = (lprofP_STATE*)lua_touserdata(L, -1);
lprofM_resume_function(S);
return 0;
}
static int profiler_init(lua_State *L) {
lprofP_STATE* S;
lprofP_STATE* R;
const char* outfile;
//float function_call_time;
//stack_level = 0;
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) {
profiler_stop(L);
}
lua_pop(L, 1);
// 清理调用信息栈
lua_pushlightuserdata(L, &profresult_id);
lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)){
profiler_clear(L);
}
lua_pop(L, 1);
//function_call_time = calcCallTime(L);
outfile = NULL;
if (lua_gettop(L) >= 1)
outfile = luaL_checkstring(L, 1);
// 初始化调用信息栈
R = lprofM_init();
if (!R){
return luaL_error(L, "LuaProfiler error: can't init profiler result stack!");
}
lua_pushlightuserdata(L, &profresult_id);
lua_pushlightuserdata(L, R);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
/* init with default file name and printing a header line */
//if (!(S=lprofP_init_core_profiler(outfile, 1, function_call_time))) {
// return luaL_error(L,"LuaProfiler error: output file could not be opened!");
//}
if (!(S = lprofP_init_core_profiler(outfile, 1, 0))) {
return luaL_error(L, "LuaProfiler error: output file could not be opened!");
}
lua_sethook(L, (lua_Hook)callhook, LUA_MASKCALL | LUA_MASKRET, 0);
lua_pushlightuserdata(L, &profstate_id);
lua_pushlightuserdata(L, S);
lua_settable(L, LUA_REGISTRYINDEX);
/* use our own exit function instead */
lua_getglobal(L, "os");
lua_pushlightuserdata(L, &exit_id);
lua_pushstring(L, "exit");
lua_gettable(L, -3);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushstring(L, "exit");
lua_pushcfunction(L, (lua_CFunction)exit_profiler);
lua_settable(L, -3);
#if 0
/* use our own coroutine.create function instead */
lua_getglobal(L, "coroutine");
lua_pushstring(L, "create");
lua_pushcfunction(L, (lua_CFunction)coroutine_create);
lua_settable(L, -3);
#endif
/* the following statement is to simulate how the execution stack is */
/* supposed to be by the time the profiler is activated when loaded */
/* as a library. */
//lprofP_callhookIN(S, "profiler_init", "(C)", -1, -1);
lua_pushboolean(L, 1);
return 1;
}
static int profiler_clear(lua_State *L){
lprofP_STATE* S;
lua_sethook(L, (lua_Hook)callhook, 0, 0);
lua_pushlightuserdata(L, &profresult_id);
lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) {
S = (lprofP_STATE*)lua_touserdata(L, -1);
while (lprofP_output(S));
lua_pushlightuserdata(L, &profresult_id);
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushboolean(L, 1);
}
else { lua_pushboolean(L, 0); }
return 1;
}
static int profiler_stop(lua_State *L) {
lua_sethook(L, (lua_Hook)callhook, 0, 0);
lprofP_STATE* R;
lua_pushlightuserdata(L, &profresult_id);
lua_gettable(L, LUA_REGISTRYINDEX);
lprofP_STATE* S;
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) {
S = (lprofP_STATE*)lua_touserdata(L, -1);
R = (lprofP_STATE*)lua_touserdata(L, -2);
/* leave all functions under execution */
while (lprofP_callhookOUT(S, R));
while (lprofP_output(R));
lprofP_close_core_profiler(S);
lua_pushlightuserdata(L, &profstate_id);
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, &profresult_id);
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushboolean(L, 1);
}
else { lua_pushboolean(L, 0); }
return 1;
}
///* calculates the approximate time Lua takes to call a function */
//static float calcCallTime(lua_State *L) {
// clock_t timer;
// char lua_code[] = " \
// function lprofT_mesure_function() \
// local i \
// \
// local t = function() \
// end \
// \
// i = 1 \
// while (i < 100000) do \
// t() \
// i = i + 1 \
// end \
// end \
// \
// lprofT_mesure_function() \
// lprofT_mesure_function = nil \
// ";
//
// lprofC_start_timer(&timer);
// luaL_dostring(L, lua_code);
// return lprofC_get_seconds(timer) / (float) 100000;
//}
static const luaL_reg prof_funcs[] = {
{ "pause", profiler_pause },
{ "resume", profiler_resume },
{ "start", profiler_init },
{ "stop", profiler_stop },
{ NULL, NULL }
};
int luaopen_profiler(lua_State *L) {
luaL_openlib(L, "lua_profiler", prof_funcs, 0);
lua_pushliteral(L, "_COPYRIGHT");
lua_pushliteral(L, "Copyright (C) 2003-2007 Kepler Project");
lua_settable(L, -3);
lua_pushliteral(L, "_DESCRIPTION");
lua_pushliteral(L, "LuaProfiler is a time profiler designed to help finding bottlenecks in your Lua program.");
lua_settable(L, -3);
lua_pushliteral(L, "_NAME");
lua_pushliteral(L, "LuaProfiler");
lua_settable(L, -3);
lua_pushliteral(L, "_VERSION");
lua_pushliteral(L, "2.0.1");
lua_settable(L, -3);
return 1;
}