-
Notifications
You must be signed in to change notification settings - Fork 0
/
back_tracer.cpp
223 lines (192 loc) · 6.46 KB
/
back_tracer.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
#include "back_tracer.h"
void getRSP(uint64_t *rsp) {
__asm__ __volatile__("mov %%rsp, %0" : "=m"(*rsp)::"memory");
}
CallStackStatus BackTracer::GenerateCallStack(std::stack<UNWValue> &q,
bool verbose /*=false*/) {
std::queue<UNWValue> pyFrameQueue;
CallStackStatus status;
if (GetProfilerConf()->doPyUnwinding)
pyBackTrace(pyFrameQueue);
if (pyFrameQueue.size())
status = CALL_STACK_HAS_PY;
else
status = CALL_STACK_NOT_HAS_PY;
unw_cursor_t cursor;
unw_context_t context;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
while (unw_step(&cursor) > 0) {
unw_word_t offset, pc;
char fname[FUNC_NAME_LENGTH];
char *outer_name;
unw_get_reg(&cursor, UNW_REG_IP, &pc);
unw_get_proc_name(&cursor, fname, sizeof(fname), &offset);
int status = 99;
if ((outer_name = abi::__cxa_demangle(fname, nullptr, nullptr, &status)) ==
0) {
outer_name = fname;
}
// skip cupti-related stack frames
if (HasExcludePatterns(outer_name))
continue;
if (GetProfilerConf()->doPyUnwinding &&
std::string(outer_name).find("_PyEval_EvalFrameDefault") !=
std::string::npos) {
UNWValue value = pyFrameQueue.front();
value.pc = pc + value.offset; // use native pc plus offset as PyFrame pc
q.push(value);
pyFrameQueue.pop();
} else {
UNWValue value(pc, offset, std::string(outer_name));
q.push(value);
}
if (verbose) {
PrintUNWValue(q.top());
}
}
return status;
}
void BackTracer::DoBackTrace(bool verbose) {
#if DEBUG
Timer *timer = Timer::GetGlobalTimer("back_tracer");
timer->start();
#endif
auto CPUCCTMap = GetCPUCCTMap();
pthread_t tid = pthread_self();
if (CPUCCTMap->find(tid) == CPUCCTMap->end()) {
DEBUG_LOG("new CCT, tid=%d\n", gettid());
CPUCCT *newCCT = new CPUCCT();
CPUCCTNode *vRootNode = new CPUCCTNode();
CPUCCTNodeIdMutex.lock();
vRootNode->id = CPUCCTNodeId;
++CPUCCTNodeId;
CPUCCTNodeIdMutex.unlock();
vRootNode->funcName = "thread:" + std::to_string(gettid()) +
"::id:" + std::to_string(vRootNode->id);
vRootNode->pc = 0;
vRootNode->offset = 0;
vRootNode->nodeType = CCTNODE_TYPE_CXX;
newCCT->setRootNode(vRootNode);
CPUCCTMap->insert({tid, newCCT});
}
CPUCCT *cpuCCT = CPUCCTMap->at(tid);
// If GetProfilerConf()->fakeBT is true, do not perform cpu
// call stack unwinding.
if (GetProfilerConf()->fakeBT) {
activeCPUPCIDMutex.lock();
if (verbose) {
DEBUG_LOG("active PC changed to %lu:%p\n", cpuCCT->root->id,
(void *)(cpuCCT->root->pc));
}
activeCPUPCID = cpuCCT->root->id;
activeCPUPCIDMutex.unlock();
return;
}
// Optimization of cpu call stack unwinding: check the rsp register first.
uint64_t rsp;
getRSP(&rsp);
if (verbose)
DEBUG_LOG("rsp=%p\n", (void *)rsp);
if (GetProfilerConf()->checkRSP &&
esp2pcIdMap.find(rsp) != esp2pcIdMap.end()) {
uint64_t pcId = esp2pcIdMap[rsp];
activeCPUPCIDMutex.lock();
activeCPUPCID = pcId;
activeCPUPCIDMutex.unlock();
DEBUG_LOG("already unwound, active pc id changed to %lu\n", pcId);
return;
}
// nodes to be inserted to the cpu calling context tree
std::stack<UNWValue> toInsertUNW;
std::stack<UNWValue> toInsertUNWMain;
auto status = GenerateCallStack(toInsertUNW, verbose);
// if the backend is Pytorch, and current thread has not PyFrame
// go to the main thread for PyFrame
if (GetProfilerConf()->doPyUnwinding && status == CALL_STACK_NOT_HAS_PY) {
DEBUG_LOG("this thread has not PyFrame, going to the main thread\n");
handlingRemoteUnwinding = true;
pthread_kill(GetProfilerConf()->mainThreadTid, SIGUSR1);
while (handlingRemoteUnwinding) {
}
toInsertUNWMain = g_callStack;
// TODO: clear the stack or not ?
while (!g_callStack.empty())
g_callStack.pop();
}
CPUCCTNode *parentNode = cpuCCT->root;
while (!toInsertUNW.empty()) {
UNWValue value;
TOP2(toInsertUNWMain, toInsertUNW, value);
CPUCCTNode *childNode = parentNode->getChildbyPC(value.pc);
// if finding a CXX node with _PyEval_EvalFrameDefault (C2P node)
// replace it with PY node
if (childNode) {
if (childNode->nodeType == CCTNODE_TYPE_C2P) {
if (value.nodeType == CCTNODE_TYPE_PY) {
childNode->nodeType = CCTNODE_TYPE_PY;
childNode->funcName = value.funcName;
DEBUG_LOG("py node renamed in unwinding: %s\n",
value.funcName.c_str());
} else {
DEBUG_LOG("wrong cct node type matching: %d/%d\n",
childNode->nodeType, value.nodeType);
}
}
parentNode = childNode;
POP2(toInsertUNWMain, toInsertUNW);
} else {
break;
}
}
// the call path has been searched before
if (toInsertUNW.empty()) {
activeCPUPCIDMutex.lock();
activeCPUPCID = parentNode->id;
if (verbose)
DEBUG_LOG("old pc, active pc changed to %lu:%p\n", parentNode->id,
(void *)(parentNode->pc));
activeCPUPCIDMutex.unlock();
}
// the call path has unsearched prefix
while (!toInsertUNW.empty()) {
UNWValue value;
TOP2(toInsertUNWMain, toInsertUNW, value);
CPUCCTNode *newNode = new CPUCCTNode(value.nodeType);
newNode->pc = value.pc;
newNode->offset = value.offset;
CPUCCTNodeIdMutex.lock();
newNode->id = CPUCCTNodeId;
++CPUCCTNodeId;
CPUCCTNodeIdMutex.unlock();
if (value.nodeType == CCTNODE_TYPE_CXX) {
newNode->funcName =
value.funcName; // + "_" + std::to_string(newNode->id);
} else {
newNode->funcName = value.fileName + "::" + value.funcName + "_" +
std::to_string(value.offset) + "_";
// + std::to_string(newNode->id);
}
// leaf node
if (toInsertUNW.size() == 1) {
activeCPUPCIDMutex.lock();
if (verbose)
DEBUG_LOG("active pc changed to %lu:%p\n", newNode->id,
(void *)(newNode->pc));
activeCPUPCID = newNode->id;
esp2pcIdMap[rsp] = newNode->id;
activeCPUPCIDMutex.unlock();
}
cpuCCT->insertNode(parentNode, newNode);
parentNode = newNode;
POP2(toInsertUNWMain, toInsertUNW);
}
#if DEBUG
timer->stop();
#endif
}
BackTracer *BackTracer::GetBackTracerSingleton() {
static auto singleton = new BackTracer();
return singleton;
}
BackTracer *GetBackTracer() { return BackTracer::GetBackTracerSingleton(); }