Skip to content

Commit

Permalink
[Feature] support heap profiling at runtime (#30686)
Browse files Browse the repository at this point in the history
Signed-off-by: silverbullet233 <3675229+silverbullet233@users.noreply.github.com>
  • Loading branch information
silverbullet233 authored Oct 11, 2023
1 parent 2344370 commit 7c75f38
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
24 changes: 22 additions & 2 deletions be/src/http/action/pprof_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ static const int kPprofDefaultSampleSecs = 30;
// Protect, only one thread can work
static std::mutex kPprofActionMutex;

int set_jemalloc_profiling(bool enable) {
int ret = je_mallctl("prof.active", nullptr, nullptr, &enable, 1);
ret |= je_mallctl("prof.thread_active_init", nullptr, nullptr, &enable, 1);
return ret;
}

void HeapAction::handle(HttpRequest* req) {
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER)
(void)kPprofDefaultSampleSecs; // Avoid unused variable warning.
Expand All @@ -67,12 +73,25 @@ void HeapAction::handle(HttpRequest* req) {

HttpChannel::send_reply(req, str);
#else
(void)kPprofDefaultSampleSecs; // Avoid unused variable warning.
int seconds = kPprofDefaultSampleSecs;
const std::string& seconds_str = req->param(SECOND_KEY);
if (!seconds_str.empty()) {
if (int value = std::atoi(seconds_str.c_str()); value > 0) {
seconds = value;
}
}

std::lock_guard<std::mutex> lock(kPprofActionMutex);
std::string str;
std::stringstream tmp_prof_file_name;
tmp_prof_file_name << config::pprof_profile_dir << "/heap_profile." << getpid() << "." << rand();
if (set_jemalloc_profiling(true) != 0) {
std::string error_msg = "enable jemalloc profiling error.";
HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg);
return;
}
LOG(INFO) << "enable jemalloc profiling";
sleep(seconds);

// NOTE: Use fname to make the content which fname_cstr references to is still valid
// when je_mallctl is executing
Expand All @@ -84,6 +103,8 @@ void HeapAction::handle(HttpRequest* req) {
} else {
str = "dump jemalloc prof file failed";
}
(void)set_jemalloc_profiling(false);
LOG(INFO) << "disable jemalloc profiling";
HttpChannel::send_reply(req, str);
#endif
}
Expand Down Expand Up @@ -192,5 +213,4 @@ void SymbolAction::handle(HttpRequest* req) {
HttpChannel::send_reply(req, result);
}
}

} // namespace starrocks
1 change: 0 additions & 1 deletion be/src/http/action/pprof_actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,4 @@ class SymbolAction : public HttpHandler {
private:
BfdParser* _parser;
};

} // namespace starrocks
2 changes: 1 addition & 1 deletion bin/start_backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fi

# Set JEMALLOC_CONF environment variable if not already set
if [[ -z "$JEMALLOC_CONF" ]]; then
export JEMALLOC_CONF="percpu_arena:percpu,oversize_threshold:0,muzzy_decay_ms:5000,dirty_decay_ms:5000,metadata_thp:auto,background_thread:true"
export JEMALLOC_CONF="percpu_arena:percpu,oversize_threshold:0,muzzy_decay_ms:5000,dirty_decay_ms:5000,metadata_thp:auto,background_thread:true,prof:true,prof_active:false"
else
echo "JEMALLOC_CONF from conf is '$JEMALLOC_CONF'"
fi
Expand Down

0 comments on commit 7c75f38

Please sign in to comment.