-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
pprof_actions.cpp
331 lines (280 loc) · 11.5 KB
/
pprof_actions.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "http/action/pprof_actions.h"
#include "http/http_handler_with_auth.h"
#if !defined(__SANITIZE_ADDRESS__) && !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && \
!defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC)
#include <gperftools/heap-profiler.h> // IWYU pragma: keep
#include <gperftools/malloc_extension.h> // IWYU pragma: keep
#endif
#if !defined(__SANITIZE_ADDRESS__) && !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && \
!defined(THREAD_SANITIZER)
#include <gperftools/profiler.h> // IWYU pragma: keep
#endif
#include <stdio.h>
#include <fstream>
#include <memory>
#include <mutex>
#include <string>
#include "common/config.h"
#include "common/object_pool.h"
#include "http/ev_http_server.h"
#include "http/http_channel.h"
#include "http/http_handler.h"
#include "http/http_method.h"
#include "http/http_request.h"
#include "io/fs/local_file_system.h"
#include "runtime/exec_env.h"
#include "util/bfd_parser.h"
#include "util/pprof_utils.h" // IWYU pragma: keep
namespace doris {
// pprof default sample time in seconds.
[[maybe_unused]] static const std::string SECOND_KEY = "seconds";
static const int kPprofDefaultSampleSecs = 30;
// Protect, only one thread can work
static std::mutex kPprofActionMutex;
class HeapAction : public HttpHandlerWithAuth {
public:
HeapAction(ExecEnv* exec_env) : HttpHandlerWithAuth(exec_env) {}
virtual ~HeapAction() {}
virtual void handle(HttpRequest* req) override;
};
void HeapAction::handle(HttpRequest* req) {
std::lock_guard<std::mutex> lock(kPprofActionMutex);
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER) || \
defined(USE_JEMALLOC)
(void)kPprofDefaultSampleSecs; // Avoid unused variable warning.
std::string str = "Heap profiling is not available with address sanitizer or jemalloc builds.";
HttpChannel::send_reply(req, str);
#else
int seconds = kPprofDefaultSampleSecs;
const std::string& seconds_str = req->param(SECOND_KEY);
if (!seconds_str.empty()) {
seconds = std::atoi(seconds_str.c_str());
}
std::stringstream tmp_prof_file_name;
// Build a temporary file name that is hopefully unique.
tmp_prof_file_name << config::pprof_profile_dir << "/heap_profile." << getpid() << "."
<< rand();
HeapProfilerStart(tmp_prof_file_name.str().c_str());
// Sleep to allow for some samples to be collected.
sleep(seconds);
const char* profile = GetHeapProfile();
HeapProfilerStop();
std::string str = profile;
delete profile;
const std::string& readable_str = req->param("readable");
if (!readable_str.empty()) {
std::stringstream readable_res;
Status st = PprofUtils::get_readable_profile(str, false, &readable_res);
if (!st.ok()) {
HttpChannel::send_reply(req, st.to_string());
} else {
HttpChannel::send_reply(req, readable_res.str());
}
} else {
HttpChannel::send_reply(req, str);
}
#endif
}
class GrowthAction : public HttpHandlerWithAuth {
public:
GrowthAction(ExecEnv* exec_env) : HttpHandlerWithAuth(exec_env) {}
virtual ~GrowthAction() {}
virtual void handle(HttpRequest* req) override;
};
void GrowthAction::handle(HttpRequest* req) {
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER) || \
defined(USE_JEMALLOC)
std::string str =
"Growth profiling is not available with address sanitizer or jemalloc builds.";
HttpChannel::send_reply(req, str);
#else
std::lock_guard<std::mutex> lock(kPprofActionMutex);
std::string heap_growth_stack;
MallocExtension::instance()->GetHeapGrowthStacks(&heap_growth_stack);
HttpChannel::send_reply(req, heap_growth_stack);
#endif
}
class ProfileAction : public HttpHandlerWithAuth {
public:
ProfileAction(ExecEnv* exec_env) : HttpHandlerWithAuth(exec_env) {}
virtual ~ProfileAction() {}
virtual void handle(HttpRequest* req) override;
};
void ProfileAction::handle(HttpRequest* req) {
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER)
std::string str = "CPU profiling is not available with address sanitizer or jemalloc builds.";
HttpChannel::send_reply(req, str);
#else
std::lock_guard<std::mutex> lock(kPprofActionMutex);
int seconds = kPprofDefaultSampleSecs;
const std::string& seconds_str = req->param(SECOND_KEY);
if (!seconds_str.empty()) {
seconds = std::atoi(seconds_str.c_str());
}
const std::string& type_str = req->param("type");
if (type_str != "flamegraph") {
// use pprof the sample the CPU
std::ostringstream tmp_prof_file_name;
tmp_prof_file_name << config::pprof_profile_dir << "/doris_profile." << getpid() << "."
<< rand();
ProfilerStart(tmp_prof_file_name.str().c_str());
sleep(seconds);
ProfilerStop();
if (type_str != "text") {
// return raw content via http response directly
std::ifstream prof_file(tmp_prof_file_name.str().c_str(), std::ios::in);
std::stringstream ss;
if (!prof_file.is_open()) {
ss << "Unable to open cpu profile: " << tmp_prof_file_name.str();
std::string str = ss.str();
HttpChannel::send_reply(req, str);
return;
}
ss << prof_file.rdbuf();
prof_file.close();
std::string str = ss.str();
HttpChannel::send_reply(req, str);
return;
}
// text type. we will return readable content via http response
std::stringstream readable_res;
Status st = PprofUtils::get_readable_profile(tmp_prof_file_name.str(), true, &readable_res);
if (!st.ok()) {
HttpChannel::send_reply(req, st.to_string());
} else {
HttpChannel::send_reply(req, readable_res.str());
}
} else {
// generate flamegraph
std::string svg_file_content;
std::string flamegraph_install_dir =
std::string(std::getenv("DORIS_HOME")) + "/tools/FlameGraph/";
Status st = PprofUtils::generate_flamegraph(seconds, flamegraph_install_dir, false,
&svg_file_content);
if (!st.ok()) {
HttpChannel::send_reply(req, st.to_string());
} else {
HttpChannel::send_reply(req, svg_file_content);
}
}
#endif
}
class PmuProfileAction : public HttpHandlerWithAuth {
public:
PmuProfileAction(ExecEnv* exec_env) : HttpHandlerWithAuth(exec_env) {}
virtual ~PmuProfileAction() {}
virtual void handle(HttpRequest* req) override {}
};
class ContentionAction : public HttpHandlerWithAuth {
public:
ContentionAction(ExecEnv* exec_env) : HttpHandlerWithAuth(exec_env) {}
virtual ~ContentionAction() {}
virtual void handle(HttpRequest* req) override {}
};
class CmdlineAction : public HttpHandlerWithAuth {
public:
CmdlineAction(ExecEnv* exec_env) : HttpHandlerWithAuth(exec_env) {}
virtual ~CmdlineAction() {}
virtual void handle(HttpRequest* req) override;
};
void CmdlineAction::handle(HttpRequest* req) {
FILE* fp = fopen("/proc/self/cmdline", "r");
if (fp == nullptr) {
std::string str = "Unable to open file: /proc/self/cmdline";
HttpChannel::send_reply(req, str);
return;
}
std::string str;
char buf[1024];
if (fscanf(fp, "%1023s ", buf) == 1) {
str = buf;
} else {
str = "Unable to read file: /proc/self/cmdline";
}
fclose(fp);
HttpChannel::send_reply(req, str);
}
class SymbolAction : public HttpHandlerWithAuth {
public:
SymbolAction(BfdParser* parser, ExecEnv* exec_env)
: HttpHandlerWithAuth(exec_env), _parser(parser) {}
virtual ~SymbolAction() {}
virtual void handle(HttpRequest* req) override;
private:
BfdParser* _parser;
};
void SymbolAction::handle(HttpRequest* req) {
// TODO: Implement symbol resolution. Without this, the binary needs to be passed
// to pprof to resolve all symbols.
if (req->method() == HttpMethod::GET) {
std::stringstream ss;
ss << "num_symbols: " << _parser->num_symbols();
std::string str = ss.str();
HttpChannel::send_reply(req, str);
return;
} else if (req->method() == HttpMethod::HEAD) {
HttpChannel::send_reply(req);
return;
} else if (req->method() == HttpMethod::POST) {
std::string request = req->get_request_body();
// parse address
std::string result;
const char* ptr = request.c_str();
const char* end = request.c_str() + request.size();
while (ptr < end && *ptr != '\0') {
std::string file_name;
std::string func_name;
unsigned int lineno = 0;
const char* old_ptr = ptr;
if (!_parser->decode_address(ptr, &ptr, &file_name, &func_name, &lineno)) {
result.append(old_ptr, ptr - old_ptr);
result.push_back('\t');
result.append(func_name);
result.push_back('\n');
}
if (ptr < end && *ptr == '+') {
ptr++;
}
}
HttpChannel::send_reply(req, result);
}
}
Status PprofActions::setup(ExecEnv* exec_env, EvHttpServer* http_server, ObjectPool& pool) {
if (!config::pprof_profile_dir.empty()) {
RETURN_IF_ERROR(io::global_local_filesystem()->create_directory(config::pprof_profile_dir));
}
http_server->register_handler(HttpMethod::GET, "/pprof/heap",
pool.add(new HeapAction(exec_env)));
http_server->register_handler(HttpMethod::GET, "/pprof/growth",
pool.add(new GrowthAction(exec_env)));
http_server->register_handler(HttpMethod::GET, "/pprof/profile",
pool.add(new ProfileAction(exec_env)));
http_server->register_handler(HttpMethod::GET, "/pprof/pmuprofile",
pool.add(new PmuProfileAction(exec_env)));
http_server->register_handler(HttpMethod::GET, "/pprof/contention",
pool.add(new ContentionAction(exec_env)));
http_server->register_handler(HttpMethod::GET, "/pprof/cmdline",
pool.add(new CmdlineAction(exec_env)));
auto action = pool.add(new SymbolAction(exec_env->bfd_parser(), exec_env));
http_server->register_handler(HttpMethod::GET, "/pprof/symbol", action);
http_server->register_handler(HttpMethod::HEAD, "/pprof/symbol", action);
http_server->register_handler(HttpMethod::POST, "/pprof/symbol", action);
return Status::OK();
}
} // namespace doris