be/src/runtime/memory/heap_profiler.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "runtime/memory/heap_profiler.h" |
19 | | |
20 | | #ifdef USE_JEMALLOC |
21 | | #include "jemalloc/jemalloc.h" |
22 | | #endif |
23 | | #include "agent/utils.h" |
24 | | #include "common/config.h" |
25 | | #include "io/fs/local_file_system.h" |
26 | | |
27 | | namespace doris { |
28 | | #include "common/compile_check_begin.h" |
29 | | |
30 | 0 | void HeapProfiler::set_prof_active(bool prof) { |
31 | | #ifdef USE_JEMALLOC |
32 | | std::lock_guard guard(_mutex); |
33 | | try { |
34 | | int err = jemallctl("prof.active", nullptr, nullptr, &prof, 1); |
35 | | err |= jemallctl("prof.thread_active_init", nullptr, nullptr, &prof, 1); |
36 | | if (err) { |
37 | | LOG(WARNING) << "jemalloc heap profiling start failed, " << err; |
38 | | } else { |
39 | | LOG(INFO) << "jemalloc heap profiling started"; |
40 | | } |
41 | | } catch (...) { |
42 | | LOG(WARNING) << "jemalloc heap profiling start failed"; |
43 | | } |
44 | | #endif |
45 | 0 | } |
46 | | |
47 | 0 | bool HeapProfiler::get_prof_dump(const std::string& profile_file_name) { |
48 | | #ifdef USE_JEMALLOC |
49 | | std::lock_guard guard(_mutex); |
50 | | const char* file_name_ptr = profile_file_name.c_str(); |
51 | | try { |
52 | | int err = jemallctl("prof.dump", nullptr, nullptr, &file_name_ptr, sizeof(const char*)); |
53 | | if (err) { |
54 | | LOG(WARNING) << "dump heap profile failed, " << err; |
55 | | return false; |
56 | | } else { |
57 | | LOG(INFO) << "dump heap profile to " << profile_file_name; |
58 | | return true; |
59 | | } |
60 | | } catch (...) { |
61 | | LOG(WARNING) << "dump heap profile failed"; |
62 | | return false; |
63 | | } |
64 | | #else |
65 | 0 | return false; |
66 | 0 | #endif |
67 | 0 | } |
68 | | |
69 | 0 | static std::string jeprof_profile_to_dot(const std::string& profile_file_name) { |
70 | 0 | AgentUtils util; |
71 | 0 | const static std::string jeprof_path = fmt::format("{}/bin/jeprof", std::getenv("DORIS_HOME")); |
72 | 0 | const static std::string binary_path = |
73 | 0 | fmt::format("{}/lib/doris_be", std::getenv("DORIS_HOME")); |
74 | | // https://doris.apache.org/community/developer-guide/debug-tool/#3-jeprof-parses-heap-profile |
75 | 0 | std::string jeprof_cmd = |
76 | 0 | fmt::format("{} --dot {} {}", jeprof_path, binary_path, profile_file_name); |
77 | 0 | std::string msg; |
78 | 0 | bool rc = util.exec_cmd(jeprof_cmd, &msg); |
79 | 0 | if (!rc) { |
80 | 0 | LOG(WARNING) << "jeprof profile to dot failed: " << msg; |
81 | 0 | } |
82 | 0 | return msg; |
83 | 0 | } |
84 | | |
85 | 0 | void HeapProfiler::heap_profiler_start() { |
86 | 0 | set_prof_active(true); |
87 | 0 | } |
88 | | |
89 | 0 | void HeapProfiler::heap_profiler_stop() { |
90 | 0 | set_prof_active(false); |
91 | 0 | } |
92 | | |
93 | 0 | bool HeapProfiler::check_active_heap_profiler() { |
94 | | #ifdef USE_JEMALLOC |
95 | | size_t value = 0; |
96 | | size_t sz = sizeof(value); |
97 | | // A Bug in Jemalloc 5.3, `jemallctl("prof", &value, &sz, nullptr, 0)` always returns `0`, |
98 | | // the real conf `prof` value cannot be checked. |
99 | | jemallctl("prof.active", &value, &sz, nullptr, 0); |
100 | | return value; |
101 | | #else |
102 | 0 | return false; |
103 | 0 | #endif |
104 | 0 | } |
105 | | |
106 | 0 | bool HeapProfiler::check_enable_heap_profiler() { |
107 | | #ifdef USE_JEMALLOC |
108 | | bool prof = false; |
109 | | size_t sz = sizeof(prof); |
110 | | if (jemallctl("opt.prof", &prof, &sz, nullptr, 0) != 0) { |
111 | | LOG(WARNING) << "Failed to get option: opt.prof"; |
112 | | } |
113 | | return prof; |
114 | | #else |
115 | 0 | return false; |
116 | 0 | #endif |
117 | 0 | } |
118 | | |
119 | 0 | std::string HeapProfiler::dump_heap_profile() { |
120 | 0 | if (!config::jeprofile_dir.empty()) { |
121 | 0 | auto st = io::global_local_filesystem()->create_directory(config::jeprofile_dir); |
122 | 0 | if (!st.ok()) { |
123 | 0 | LOG(WARNING) << "create jeprofile dir failed."; |
124 | 0 | return ""; |
125 | 0 | } |
126 | 0 | } |
127 | 0 | std::string profile_file_name = |
128 | 0 | fmt::format("{}/jeheap_dump.{}.{}.{}.heap", config::jeprofile_dir, std::time(nullptr), |
129 | 0 | getpid(), rand()); |
130 | 0 | if (get_prof_dump(profile_file_name)) { |
131 | 0 | return profile_file_name; |
132 | 0 | } else { |
133 | 0 | return ""; |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | 0 | std::string HeapProfiler::dump_heap_profile_to_dot() { |
138 | 0 | std::string profile_file_name = dump_heap_profile(); |
139 | 0 | if (!profile_file_name.empty()) { |
140 | 0 | return jeprof_profile_to_dot(profile_file_name); |
141 | 0 | } else { |
142 | 0 | return ""; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | 0 | bool HeapProfiler::heap_profiler_reset(size_t lg_sample) { |
147 | | #ifdef USE_JEMALLOC |
148 | | int ret = jemallctl("prof.reset", nullptr, nullptr, &lg_sample, sizeof(lg_sample)); |
149 | | if (ret != 0) { |
150 | | LOG(WARNING) << "jemallctl failed to set prof.reset:" << lg_sample |
151 | | << ".Check whether JEMALLOC_CONF is configured with prof:true."; |
152 | | return false; |
153 | | } |
154 | | #endif |
155 | 0 | return true; |
156 | 0 | } |
157 | | |
158 | | #include "common/compile_check_end.h" |
159 | | } // namespace doris |