/proc/self/cwd/source/server/admin/profiling_handler.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "source/server/admin/profiling_handler.h" |
2 | | |
3 | | #include "source/common/profiler/profiler.h" |
4 | | #include "source/server/admin/utils.h" |
5 | | |
6 | | namespace Envoy { |
7 | | namespace Server { |
8 | | |
9 | 5.34k | ProfilingHandler::ProfilingHandler(const std::string& profile_path) : profile_path_(profile_path) {} |
10 | | |
11 | | Http::Code ProfilingHandler::handlerCpuProfiler(Http::ResponseHeaderMap&, |
12 | | Buffer::Instance& response, |
13 | 0 | AdminStream& admin_stream) { |
14 | 0 | Http::Utility::QueryParams query_params = admin_stream.queryParams(); |
15 | 0 | if (query_params.size() != 1 || query_params.begin()->first != "enable" || |
16 | 0 | (query_params.begin()->second != "y" && query_params.begin()->second != "n")) { |
17 | 0 | response.add("?enable=<y|n>\n"); |
18 | 0 | return Http::Code::BadRequest; |
19 | 0 | } |
20 | | |
21 | 0 | bool enable = query_params.begin()->second == "y"; |
22 | 0 | if (enable && !Profiler::Cpu::profilerEnabled()) { |
23 | 0 | if (!Profiler::Cpu::startProfiler(profile_path_)) { |
24 | 0 | response.add("failure to start the profiler"); |
25 | 0 | return Http::Code::InternalServerError; |
26 | 0 | } |
27 | |
|
28 | 0 | } else if (!enable && Profiler::Cpu::profilerEnabled()) { |
29 | 0 | Profiler::Cpu::stopProfiler(); |
30 | 0 | } |
31 | | |
32 | 0 | response.add("OK\n"); |
33 | 0 | return Http::Code::OK; |
34 | 0 | } |
35 | | |
36 | | Http::Code ProfilingHandler::handlerHeapProfiler(Http::ResponseHeaderMap&, |
37 | | Buffer::Instance& response, |
38 | 0 | AdminStream& admin_stream) { |
39 | 0 | if (!Profiler::Heap::profilerEnabled()) { |
40 | 0 | response.add("The current build does not support heap profiler"); |
41 | 0 | return Http::Code::NotImplemented; |
42 | 0 | } |
43 | | |
44 | 0 | Http::Utility::QueryParams query_params = admin_stream.queryParams(); |
45 | 0 | if (query_params.size() != 1 || query_params.begin()->first != "enable" || |
46 | 0 | (query_params.begin()->second != "y" && query_params.begin()->second != "n")) { |
47 | 0 | response.add("?enable=<y|n>\n"); |
48 | 0 | return Http::Code::BadRequest; |
49 | 0 | } |
50 | | |
51 | 0 | Http::Code res = Http::Code::OK; |
52 | 0 | bool enable = query_params.begin()->second == "y"; |
53 | 0 | if (enable) { |
54 | 0 | if (Profiler::Heap::isProfilerStarted()) { |
55 | 0 | response.add("Fail to start heap profiler: already started"); |
56 | 0 | res = Http::Code::BadRequest; |
57 | 0 | } else if (!Profiler::Heap::startProfiler(profile_path_)) { |
58 | 0 | response.add("Fail to start the heap profiler"); |
59 | 0 | res = Http::Code::InternalServerError; |
60 | 0 | } else { |
61 | 0 | response.add("Starting heap profiler"); |
62 | 0 | res = Http::Code::OK; |
63 | 0 | } |
64 | 0 | } else { |
65 | | // !enable |
66 | 0 | if (!Profiler::Heap::isProfilerStarted()) { |
67 | 0 | response.add("Fail to stop heap profiler: not started"); |
68 | 0 | res = Http::Code::BadRequest; |
69 | 0 | } else { |
70 | 0 | Profiler::Heap::stopProfiler(); |
71 | 0 | response.add( |
72 | 0 | fmt::format("Heap profiler stopped and data written to {}. See " |
73 | 0 | "http://goog-perftools.sourceforge.net/doc/heap_profiler.html for details.", |
74 | 0 | profile_path_)); |
75 | 0 | res = Http::Code::OK; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | return res; |
79 | 0 | } |
80 | | |
81 | | Http::Code TcmallocProfilingHandler::handlerHeapDump(Http::ResponseHeaderMap&, |
82 | 0 | Buffer::Instance& response, AdminStream&) { |
83 | 0 | auto dump_result = Profiler::TcmallocProfiler::tcmallocHeapProfile(); |
84 | |
|
85 | 0 | if (dump_result.ok()) { |
86 | 0 | response.add(dump_result.value()); |
87 | 0 | return Http::Code::OK; |
88 | 0 | } |
89 | | |
90 | 0 | response.add(dump_result.status().message()); |
91 | 0 | return Http::Code::NotImplemented; |
92 | 0 | } |
93 | | |
94 | | } // namespace Server |
95 | | } // namespace Envoy |