Skip to content

Commit 48a69e5

Browse files
committed
Limit the extra options of jemalloc profiler
1 parent 7229c36 commit 48a69e5

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/brpc/details/jemalloc_profiler.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ static int JeProfileReset(size_t lg_sample) {
166166
return 0;
167167
}
168168

169+
// https://github.com/jemalloc/jemalloc/blob/5.3.0/bin/jeprof.in#L211-L222
170+
static std::unordered_set<std::string> g_extra_options_set = {
171+
"inuse_space", "inuse_objects", "alloc_space",
172+
"alloc_objects", "show_bytes", "drop_negative",
173+
"total_delay", "contentions", "mean_delay"
174+
};
175+
169176
void JeControlProfile(Controller* cntl) {
170177
const brpc::URI& uri = cntl->http_request().uri();
171178
// http:ip:port/pprof/heap?display=(text|svg|stats|flamegraph)&extra_options=(inuse_space|inuse_objects..)
@@ -228,13 +235,15 @@ void JeControlProfile(Controller* cntl) {
228235
}
229236
const std::string process_file(process_path, len);
230237

231-
std::string cmd_str = jeprof + " " + process_file + " " + prof_name;
238+
std::string cmd_str = butil::string_printf(
239+
"%s %s %s", jeprof.c_str(), process_file.c_str(), prof_name.c_str());
232240

233241
// https://github.com/jemalloc/jemalloc/blob/5.3.0/bin/jeprof.in#L211-L222
234242
// e.g: inuse_space, contentions
235243
const std::string* uri_extra_options = uri.GetQuery("extra_options");
236-
if (uri_extra_options != nullptr && !uri_extra_options->empty()) {
237-
cmd_str += " --" + *uri_extra_options + " ";
244+
if (uri_extra_options != nullptr && !uri_extra_options->empty() &&
245+
g_extra_options_set.find(*uri_extra_options) != g_extra_options_set.end()) {
246+
butil::string_appendf(&cmd_str, " --%s", uri_extra_options->c_str());
238247
}
239248

240249
bool display_img = false;
@@ -244,12 +253,14 @@ void JeControlProfile(Controller* cntl) {
244253
} else if (*uri_display == "flamegraph") {
245254
const char* flamegraph_tool = getenv("FLAMEGRAPH_PL_PATH");
246255
if (!flamegraph_tool) {
247-
LOG(WARNING) << " display: " << *uri_display << " invalid, env FLAMEGRAPH_PL_PATH invalid";
256+
LOG(WARNING) << " display: " << *uri_display
257+
<< " invalid, env FLAMEGRAPH_PL_PATH invalid";
248258
buf.append("\ndisplay:" + *uri_display + " invalid, env FLAMEGRAPH_PL_PATH invalid");
249259
return;
250260
}
251261
const int width_size = FLAGS_max_flame_graph_width > 0 ? FLAGS_max_flame_graph_width : 1200;
252-
cmd_str += " --collapsed | " + std::string(flamegraph_tool) + " --colors mem --width " + std::to_string(width_size);
262+
butil::string_appendf(&cmd_str, " --collapsed | %s --colors mem --width %d",
263+
flamegraph_tool, width_size);
253264
display_img = true;
254265
} else if (*uri_display == "text") {
255266
cmd_str += " --text ";

0 commit comments

Comments
 (0)