Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

feat(http): support CPU profiling using gperf #290

Merged
merged 36 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1f06eca
provide the settings of tcmalloc release rate
andylin-hao Jul 8, 2019
d42a3a5
add periodic memory release
andylin-hao Jul 9, 2019
a9f4442
add periodic memory release
andylin-hao Jul 9, 2019
99cd9f8
minor changes on periodic memory release
andylin-hao Jul 11, 2019
78f5d58
Merge branch 'master' into mem_release
Jul 11, 2019
e08df0a
clang formatting
andylin-hao Jul 12, 2019
e918422
Merge branch 'mem_release' of https://github.com/linlinhaohao888/rdsn…
andylin-hao Jul 12, 2019
f201e46
fix clang-format
andylin-hao Jul 15, 2019
296210a
fix clang-format
andylin-hao Jul 15, 2019
26f4d37
fix clang-format
andylin-hao Jul 15, 2019
63c7931
tcmalloc: add delay time configuration
andylin-hao Jul 15, 2019
f25e26b
fix clang-format
andylin-hao Jul 15, 2019
f699d4c
rdsn: fix MallocExtension does not exist when gperf is disable
andylin-hao Jul 15, 2019
628b7e7
rdsn: fix MallocExtension does not exist when gperf is disabled
andylin-hao Jul 15, 2019
d6c0f3a
fix clang-format
andylin-hao Jul 15, 2019
66580a4
rdsn: add configuration for memory release's related parameters
andylin-hao Jul 15, 2019
1b86ccf
shift the declaration of LPC_MEM_RELEASE
andylin-hao Jul 15, 2019
b07698b
change the memory release's interval time for testing
andylin-hao Jul 16, 2019
b3ef104
Merge branch 'master' into mem_release
qinzuoyan Jul 16, 2019
bb6801d
Merge branch 'master' into mem_release
Jul 22, 2019
6282fbc
fix file privileges
andylin-hao Jul 22, 2019
a372af2
Merge branch 'mem_release' of https://github.com/linlinhaohao888/rdsn…
andylin-hao Jul 22, 2019
c7c54f7
rdsn: change default value of memory release's interval
andylin-hao Jul 22, 2019
3f6bf31
Merge branch 'master' into mem_release
qinzuoyan Jul 22, 2019
4b5f430
rdsn: get logging rid of "\n"
andylin-hao Jul 22, 2019
341fa60
rdsn: remove delay time of memory release
andylin-hao Jul 22, 2019
3f11c0c
rdsn: remove cpu profiler test code
andylin-hao Jul 22, 2019
2ccea50
rdsn: add task cancelling
andylin-hao Jul 22, 2019
345935f
rdsn: remove delay time's declaration
andylin-hao Jul 22, 2019
842a7d0
rdsn: add macro for gperftools' include
andylin-hao Jul 22, 2019
c5fc5dd
fix clang format
andylin-hao Jul 23, 2019
f5e5d84
rdsn: add macro for gperftools' include
andylin-hao Jul 23, 2019
ab3ade5
Merge branch 'master' into mem_release
qinzuoyan Jul 23, 2019
e1671c4
rdsn: add cpu profiling
andylin-hao Jul 31, 2019
30c25c2
rdsn: add cpu profiling
andylin-hao Jul 31, 2019
a06c5dd
Merge branch 'master' into cpu_profiling
Oct 15, 2019
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
55 changes: 55 additions & 0 deletions src/dist/http/pprof_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <cstdlib>
#include <chrono>
#include <fstream>
#include <sstream>

#include "pprof_http_service.h"

Expand Down Expand Up @@ -408,6 +410,59 @@ void pprof_http_service::growth_handler(const http_request &req, http_response &
malloc_ext->GetHeapGrowthStacks(&resp.body);
}

// //
// == ip:port/pprof/profile == //
// //
static bool get_cpu_profile(std::string &result, useconds_t seconds)
{
const char *file_name = "cpu.prof";

ProfilerStart(file_name);
usleep(seconds);
ProfilerStop();

std::ifstream in(file_name);
if (!in.is_open()) {
result = "No profile file";
return false;
}
std::ostringstream content;
content << in.rdbuf();
result = content.str();
in.close();
if (remove(file_name) != 0) {
result = "Failed to remove temporary profile file";
return false;
}
return true;
}

void pprof_http_service::profile_handler(const http_request &req, http_response &resp)
{
useconds_t seconds = 60000000;

const char *req_url = req.full_url.to_string().data();
size_t len = req.full_url.length();
string_splitter url_sp(req_url, req_url + len, '?');
if (url_sp != NULL && ++url_sp != NULL) {
string_splitter param_sp(url_sp.field(), url_sp.field() + url_sp.length(), '&');
while (param_sp != NULL) {
string_splitter kv_sp(param_sp.field(), param_sp.field() + param_sp.length(), '=');
std::string key(kv_sp.field(), kv_sp.length());
if (kv_sp != NULL && key == "seconds" && ++kv_sp != NULL) {
char *end_ptr;
seconds = strtoul(kv_sp.field(), &end_ptr, 10) * 1000000;
break;
}
param_sp++;
}
}

resp.status_code = http_status_code::ok;

get_cpu_profile(resp.body, seconds);
}

} // namespace dsn

#endif // DSN_ENABLE_GPERF
9 changes: 9 additions & 0 deletions src/dist/http/pprof_http_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class pprof_http_service : public http_service
this,
std::placeholders::_1,
std::placeholders::_2));

// ip:port/pprof/profile
register_handler("profile",
std::bind(&pprof_http_service::profile_handler,
this,
std::placeholders::_1,
std::placeholders::_2));
}

std::string path() const override { return "pprof"; }
Expand All @@ -53,6 +60,8 @@ class pprof_http_service : public http_service
void cmdline_handler(const http_request &req, http_response &resp);

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

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

} // namespace dsn
Expand Down