-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
135 lines (120 loc) · 3.66 KB
/
test.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
#include "back_tracer.h"
#include "common.h"
#include "cpu_sampler.h"
bool verbose = true;
bool samplingStarted = false;
pid_t mainPid = -1;
void TestProfilerConf() {
auto profilerConf = GetProfilerConf();
profilerConf->PrintProfilerConf();
}
void barFunc() {
auto backTracer = GetBackTracer();
backTracer->DoBackTrace(verbose);
}
void barFuncV2() {
auto backTracer = GetBackTracer();
backTracer->DoBackTrace(verbose);
}
void fooFunc() { barFunc(); }
void TestBackTracerOverheadS1() {
std::cout << "********** TestBackTracerOverheadS1 **********" << std::endl;
fooFunc();
}
void TestBackTracerRecursive(int depth) {
if (depth <= 0) {
auto backTracer = GetBackTracer();
backTracer->DoBackTrace(verbose);
} else {
TestBackTracerRecursive(depth - 1);
}
}
void TestBackTracerOverheadR1(int depth) {
std::cout << "********** TestBackTracerOverheadR1 **********" << std::endl;
auto timer = Timer::GetGlobalTimer("test_back_tracer_overhead");
timer->start();
TestBackTracerRecursive(depth);
timer->stop();
std::cout << "overhead of simple sample: " << timer->getAccumulatedTime()
<< std::endl;
timer->reset();
}
namespace TestA {
namespace TestA1 {
class A {
public:
void foo(int depth, int i1, int i2, int i3, float f1, float f2, float f3) {
if (depth <= 0) {
GetBackTracer()->DoBackTrace(verbose);
} else {
bar(depth - 1, i1 + 1, i2 + 1, i3 + 1, f1 + 1, f2 + 1, f3 + 1);
}
}
void bar(int depth, int i1, int i2, int i3, float f1, float f2, float f3) {
if (depth <= 0) {
GetBackTracer()->DoBackTrace(verbose);
} else {
foo(depth - 1, i1 + 1, i2 + 1, i3 + 1, f1 + 1, f2 + 1, f3 + 1);
}
}
};
} // namespace TestA1
} // namespace TestA
void TestBackTracerOverheadR2(int depth) {
std::cout << "********** TestBackTracerOverheadR2 **********" << std::endl;
auto timer = Timer::GetGlobalTimer("test_BT_overhead_complex");
timer->start();
auto a = new TestA::TestA1::A();
a->foo(depth, 1, 2, 3, 4.0, 5.0, 6.0);
timer->stop();
std::cout << "overhead of complex sample: " << timer->getAccumulatedTime()
<< std::endl;
}
void TestCppStackPointer() {
std::cout << "********** TestCppStackPointer **********" << std::endl;
barFunc();
barFuncV2();
barFunc();
}
void TestCPUCallStackSampler() {
if (mainPid < 0)
std::cerr << "main pid not initialized" << std::endl;
auto cpuSampler = GetOrCreateCPUCallStackSampler(mainPid);
cpuSampler->EnableSampling();
while (samplingStarted) {
CPUCallStackSampler::CallStack callStack;
int ret = cpuSampler->CollectData(GetProfilerConf()->cpuSamplingTimeout,
GetProfilerConf()->cpuSamplingMaxDepth,
callStack);
printf("ret=%d\n", ret);
if (ret == 0) {
printf("time=%lu\n", callStack.time);
printf("pid,tid=%d,%d\n", callStack.pid, callStack.tid);
printf("stack:\n");
for (int j = 0; j < callStack.depth; ++j) {
printf("[%d] %s:%lx\n", j, callStack.fnames[j].c_str(),
callStack.pcs[j]);
}
}
}
std::cout << "sampling stopped" << std::endl;
delete cpuSampler;
}
int main(int argc, char **argv) {
if (argc > 1)
verbose = std::atoi(argv[2]);
mainPid = gettid();
samplingStarted = true;
auto testCPUCallStackSamplerThreadHandle =
std::thread(TestCPUCallStackSampler);
TestProfilerConf();
TestBackTracerOverheadS1();
TestBackTracerOverheadR1(std::atoi(argv[1]));
TestBackTracerOverheadR2(std::atoi(argv[1]));
TestCppStackPointer();
samplingStarted = false;
if (testCPUCallStackSamplerThreadHandle.joinable()) {
testCPUCallStackSamplerThreadHandle.join();
}
return 0;
}