Skip to content

Commit

Permalink
feat: update the way to get heap profile (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
hycdong authored Apr 28, 2020
1 parent fea6146 commit e2d33eb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/dist/http/pprof_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

#include "pprof_http_service.h"

#include <dsn/dist/fmt_logging.h>
#include <dsn/utility/string_conv.h>
#include <dsn/utility/defer.h>
#include <dsn/utility/timer.h>
#include <dsn/utility/string_splitter.h>
#include <gperftools/heap-profiler.h>
#include <gperftools/malloc_extension.h>
#include <gperftools/profiler.h>

Expand Down Expand Up @@ -319,26 +321,39 @@ void pprof_http_service::symbol_handler(const http_request &req, http_response &
// //
// == ip:port/pprof/heap == //
// //
void pprof_http_service::heap_handler(const http_request &req, http_response &resp)
{
bool in_pprof = false;
if (!_in_pprof_action.compare_exchange_strong(in_pprof, true)) {
dwarn_f("node is already exectuting pprof action, please wait and retry");
resp.status_code = http_status_code::internal_server_error;
return;
}

static constexpr const char *TCMALLOC_SAMPLE_PARAMETER = "TCMALLOC_SAMPLE_PARAMETER";

static bool is_heap_profile_enabled() { return ::getenv(TCMALLOC_SAMPLE_PARAMETER) != nullptr; }
const std::string SECOND = "seconds";
const uint32_t kDefaultSecond = 10;

static bool get_heap_profile(std::string &result)
{
if (!is_heap_profile_enabled()) {
result = "no TCMALLOC_SAMPLE_PARAMETER in env";
return false;
// get seconds from query params, default value is `kDefaultSecond`
uint32_t seconds = kDefaultSecond;
const auto iter = req.query_args.find(SECOND);
if (iter != req.query_args.end()) {
const auto seconds_str = iter->second;
dsn::internal::buf2unsigned(seconds_str, seconds);
}
MallocExtension::instance()->GetHeapSample(&result);
return true;
}

void pprof_http_service::heap_handler(const http_request &req, http_response &resp)
{
std::stringstream profile_name_prefix;
profile_name_prefix << "heap_profile." << getpid() << "." << dsn_now_ns();

HeapProfilerStart(profile_name_prefix.str().c_str());
sleep(seconds);
const char *profile = GetHeapProfile();
HeapProfilerStop();

resp.status_code = http_status_code::ok;
resp.body = profile;
delete profile;

get_heap_profile(resp.body);
_in_pprof_action.store(false);
}

// //
Expand Down Expand Up @@ -405,9 +420,18 @@ void pprof_http_service::cmdline_handler(const http_request &req, http_response

void pprof_http_service::growth_handler(const http_request &req, http_response &resp)
{
bool in_pprof = false;
if (!_in_pprof_action.compare_exchange_strong(in_pprof, true)) {
dwarn_f("node is already exectuting pprof action, please wait and retry");
resp.status_code = http_status_code::internal_server_error;
return;
}

MallocExtension *malloc_ext = MallocExtension::instance();
ddebug("received requests for growth profile");
malloc_ext->GetHeapGrowthStacks(&resp.body);

_in_pprof_action.store(false);
}

// //
Expand Down Expand Up @@ -439,6 +463,13 @@ static bool get_cpu_profile(std::string &result, useconds_t seconds)

void pprof_http_service::profile_handler(const http_request &req, http_response &resp)
{
bool in_pprof = false;
if (!_in_pprof_action.compare_exchange_strong(in_pprof, true)) {
dwarn_f("node is already exectuting pprof action, please wait and retry");
resp.status_code = http_status_code::internal_server_error;
return;
}

useconds_t seconds = 60000000;

const char *req_url = req.full_url.to_string().data();
Expand All @@ -461,6 +492,8 @@ void pprof_http_service::profile_handler(const http_request &req, http_response
resp.status_code = http_status_code::ok;

get_cpu_profile(resp.body, seconds);

_in_pprof_action.store(false);
}

} // namespace dsn
Expand Down
3 changes: 3 additions & 0 deletions src/dist/http/pprof_http_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class pprof_http_service : public http_service
void growth_handler(const http_request &req, http_response &resp);

void profile_handler(const http_request &req, http_response &resp);

private:
std::atomic_bool _in_pprof_action{false};
};

} // namespace dsn
Expand Down

0 comments on commit e2d33eb

Please sign in to comment.