Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/brpc/details/jemalloc_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ static int JeProfileReset(size_t lg_sample) {
return 0;
}

// https://github.com/jemalloc/jemalloc/blob/5.3.0/bin/jeprof.in#L211-L222
static const std::unordered_set<const char*> g_extra_options_set = {
"inuse_space", "inuse_objects", "alloc_space",
"alloc_objects", "show_bytes", "drop_negative",
"total_delay", "contentions", "mean_delay"
};

std::string GlobalExtraOptionsString() {
std::string result;
result.reserve(64);
for (const auto& option : g_extra_options_set) {
result.append(option);
result.append(" ");
}
return result;
}

void JeControlProfile(Controller* cntl) {
const brpc::URI& uri = cntl->http_request().uri();
// http:ip:port/pprof/heap?display=(text|svg|stats|flamegraph)&extra_options=(inuse_space|inuse_objects..)
Expand Down Expand Up @@ -228,13 +245,20 @@ void JeControlProfile(Controller* cntl) {
}
const std::string process_file(process_path, len);

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

// https://github.com/jemalloc/jemalloc/blob/5.3.0/bin/jeprof.in#L211-L222
// e.g: inuse_space, contentions
const std::string* uri_extra_options = uri.GetQuery("extra_options");
if (uri_extra_options != nullptr && !uri_extra_options->empty()) {
cmd_str += " --" + *uri_extra_options + " ";
if (g_extra_options_set.count(uri_extra_options->c_str()) > 0) {
butil::string_appendf(&cmd_str, " --%s", uri_extra_options->c_str());
} else {
static std::string g_options_str = GlobalExtraOptionsString();
LOG(WARNING) << "Unsupported jemalloc options=" << *uri_extra_options
<< ", only support [" << g_options_str << "]";
}
}

bool display_img = false;
Expand All @@ -244,12 +268,14 @@ void JeControlProfile(Controller* cntl) {
} else if (*uri_display == "flamegraph") {
const char* flamegraph_tool = getenv("FLAMEGRAPH_PL_PATH");
if (!flamegraph_tool) {
LOG(WARNING) << " display: " << *uri_display << " invalid, env FLAMEGRAPH_PL_PATH invalid";
LOG(WARNING) << " display: " << *uri_display
<< " invalid, env FLAMEGRAPH_PL_PATH invalid";
buf.append("\ndisplay:" + *uri_display + " invalid, env FLAMEGRAPH_PL_PATH invalid");
return;
}
const int width_size = FLAGS_max_flame_graph_width > 0 ? FLAGS_max_flame_graph_width : 1200;
cmd_str += " --collapsed | " + std::string(flamegraph_tool) + " --colors mem --width " + std::to_string(width_size);
butil::string_appendf(&cmd_str, " --collapsed | %s --colors mem --width %d",
flamegraph_tool, width_size);
display_img = true;
} else if (*uri_display == "text") {
cmd_str += " --text ";
Expand Down
Loading