-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler.cpp
More file actions
285 lines (244 loc) · 7.49 KB
/
Copy pathprofiler.cpp
File metadata and controls
285 lines (244 loc) · 7.49 KB
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
#include <strings.h>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "profiler.h"
#include "tsc.h"
LuaProfiler LuaProfiler::_self;
////////////////////////////////////////////////////////////////////////////
struct LuaProfilerNode {
std::string key;
std::string func;
int call_count;
uint64_t full_elapse; // including calling stack
uint64_t inner_elapse;
explicit LuaProfilerNode(const std::string& key)
: key(key)
, call_count(0)
, full_elapse(0)
, inner_elapse(0) {}
void OnEnter() {
++ call_count;
}
void OnExit(uint64_t elapse, uint64_t children_elapse) {
full_elapse += elapse;
inner_elapse += (elapse - children_elapse);
}
};
////////////////////////////////////////////////////////////////////////////
struct LuaCallStack {
// frame data
struct Frame {
LuaProfilerNode* node;
bool tailcall;
uint64_t enter_stamp;
uint64_t exit_stamp;
uint64_t child_elapse;
Frame() : node(NULL)
, tailcall(false)
, enter_stamp(0)
, exit_stamp(0)
, child_elapse(0) {}
};
std::vector<Frame> frames;
void Reset() { frames.clear(); }
void Push(LuaProfilerNode* node, bool tailcall = false) {
Frame f;
f.node = node;
f.tailcall = tailcall;
rdtscll(f.enter_stamp);
f.node->OnEnter();
frames.push_back(f);
}
void Pop(LuaProfilerNode* node, bool pop_all = false) {
if (frames.empty() || frames.back().node != node) {
return;
}
// pop self
Frame& frame = frames.back();
rdtscll(frame.exit_stamp);
uint64_t elapse = frame.exit_stamp - frame.enter_stamp;
pop_all = (pop_all || frame.tailcall);
frame.node->OnExit(elapse, frame.child_elapse);
frames.pop_back();
// parent's child elapse
if (!frames.empty()) {
Frame& parent = frames.back();
parent.child_elapse += elapse;
}
// tailcall recursive pop
if (pop_all && !frames.empty()) {
return Pop(frames.back().node, true);
}
}
void Debug(const std::string& name, int event) {
printf("%s event=%d:\n", name.c_str(), event);
for (size_t i = 0; i < frames.size(); ++ i) {
#if defined(__x86_64) && !defined(__APPLE__)
printf("\t%s: enter[%lu] quit[%lu] full[%lu] child[%lu] inner[%lu]\n",
#else
printf("\t%s: enter[%llu] quit[%llu] full[%llu] child[%llu] inner[%llu]\n",
#endif
frames[i].node->key.c_str(), frames[i].enter_stamp, frames[i].exit_stamp,
frames[i].node->full_elapse, frames[i].child_elapse, frames[i].node->inner_elapse);
}
printf("\n");
}
};
////////////////////////////////////////////////////////////////////////////
LuaProfiler::LuaProfiler()
: _started(false)
, _origin_hook(NULL)
, _origin_mask(0)
, _current_is_lua(-1)
, _current_is_main(-1)
, _current_is_native(-1)
, _stack(new LuaCallStack)
{
}
LuaProfiler::~LuaProfiler()
{
CleanUp();
delete _stack;
_stack = NULL;
}
void LuaProfiler::Start(lua_State* L)
{
if (!IsStarted()) {
_origin_hook = lua_gethook(L);
_origin_mask = lua_gethookmask(L);
lua_sethook(L, hook, _origin_mask | LUA_MASKCALL | LUA_MASKRET | LUA_HOOKTAILCALL, lua_gethookcount(L));
_started = true;
}
}
void LuaProfiler::Stop(lua_State* L)
{
if (IsStarted()) {
lua_sethook(L, _origin_hook, _origin_mask, lua_gethookcount(L));
_origin_hook = NULL;
_origin_mask = 0;
_started = false;
}
}
void LuaProfiler::CleanUp()
{
for (std::map<std::string, LuaProfilerNode*>::iterator it = _nodes.begin();
it != _nodes.end(); ++ it) {
delete it->second;
it->second = NULL;
}
_nodes.clear();
_stack->Reset();
}
void LuaProfiler::set_current(lua_Debug* ar)
{
_current_is_lua = strcasecmp(ar->what, "lua");
_current_is_main = strcasecmp(ar->what, "main");
_current_is_native = strcasecmp(ar->what, "C");
}
std::string LuaProfiler::node_key_get(lua_Debug* ar) const
{
char key[1024];
key[0] = 0;
if (_current_is_lua == 0) {
snprintf(key, sizeof(key), "%s:%d", ar->source, ar->linedefined);
} else if (_current_is_main == 0) {
snprintf(key, sizeof(key), "main:%d", ar->linedefined);
} else if (_current_is_native == 0) {
snprintf(key, sizeof(key), "native:%s", ar->name ? ar->name : "null");
}
return std::string(key);
}
LuaProfilerNode* LuaProfiler::node_get(const std::string& key)
{
std::map<std::string, LuaProfilerNode*>::iterator it = _nodes.find(key);
return it == _nodes.end() ? NULL : it->second;
}
LuaProfilerNode* LuaProfiler::node_create(const std::string& key, lua_Debug* ar)
{
LuaProfilerNode* node = node_get(key);
if (node) {
fprintf(stderr, "node[%s] already existed!", key.c_str());
return node;
}
node = new LuaProfilerNode(key);
if (ar->name && ar->name[0]) {
node->func = std::string(ar->name);
} else if (ar->namewhat && ar->namewhat[0]) {
node->func = std::string(ar->namewhat);
} else {
node->func = key;
}
_nodes.insert(std::make_pair(node->key, node));
return node;
}
void LuaProfiler::hook(lua_State* L, lua_Debug* ar)
{
LuaProfiler* lp = LuaProfiler::Get();
// original hook first
if (lp->_origin_hook && (1 << ar->event) & lp->_origin_mask) {
lp->_origin_hook(L, ar);
}
// catch only call & return & tail-call
if (ar->event != LUA_HOOKCALL
&& ar->event != LUA_HOOKRET
&& ar->event != LUA_HOOKTAILCALL) {
return;
}
// get debug info
ar->name = NULL;
lua_getinfo(L, "Sln", ar);
// current info
lp->set_current(ar);
// node
std::string key = lp->node_key_get(ar);
LuaProfilerNode* node = lp->node_get(key);
if (!node) {
node = lp->node_create(key, ar);
}
assert(node);
// enter
if (ar->event == LUA_HOOKCALL) {
lp->_stack->Push(node);
}
// exit function
else if (ar->event == LUA_HOOKRET) {
lp->_stack->Pop(node);
}
// tail return
else if (ar->event == LUA_HOOKTAILCALL) {
lp->_stack->Push(node, true);
}
// lp->_stack->Debug(key, ar->event);
}
void LuaProfiler::Dump2File(const std::string& file) const
{
std::fstream fs;
fs.open(file, std::ios::out);
assert(fs.is_open());
fs << DumpString();
fs.close();
}
std::string LuaProfiler::DumpString() const
{
std::stringstream ss;
#define _align() std::setiosflags(std::ios::left) << std::setw(30)
ss << _align() << "[position]"
<< _align() << "[function]"
<< _align() << "[call times]"
<< _align() << "[average cost]"
<< _align() << "[average inner cost]" << std::endl;
std::map<std::string, LuaProfilerNode*>::const_iterator it;
for (it = _nodes.begin(); it != _nodes.end(); ++ it) {
LuaProfilerNode* node = it->second;
if (node->call_count > 0) {
ss << _align() << node->key
<< _align() << node->func
<< _align() << node->call_count
<< _align() << node->full_elapse / node->call_count
<< _align() << node->inner_elapse / node->call_count << std::endl;
}
}
ss << std::endl;
return ss.str();
}