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 | | |
29 | 0 | void HeapProfiler::set_prof_active(bool prof) { |
30 | | #ifdef USE_JEMALLOC |
31 | | std::lock_guard guard(_mutex); |
32 | | try { |
33 | | int err = jemallctl("prof.active", nullptr, nullptr, &prof, 1); |
34 | | err |= jemallctl("prof.thread_active_init", nullptr, nullptr, &prof, 1); |
35 | | if (err) { |
36 | | LOG(WARNING) << "jemalloc heap profiling start failed, " << err; |
37 | | } else { |
38 | | LOG(INFO) << "jemalloc heap profiling started"; |
39 | | } |
40 | | } catch (...) { |
41 | | LOG(WARNING) << "jemalloc heap profiling start failed"; |
42 | | } |
43 | | #endif |
44 | 0 | } |
45 | | |
46 | 0 | bool HeapProfiler::get_prof_dump(const std::string& profile_file_name) { |
47 | | #ifdef USE_JEMALLOC |
48 | | std::lock_guard guard(_mutex); |
49 | | const char* file_name_ptr = profile_file_name.c_str(); |
50 | | try { |
51 | | int err = jemallctl("prof.dump", nullptr, nullptr, &file_name_ptr, sizeof(const char*)); |
52 | | if (err) { |
53 | | LOG(WARNING) << "dump heap profile failed, " << err; |
54 | | return false; |
55 | | } else { |
56 | | LOG(INFO) << "dump heap profile to " << profile_file_name; |
57 | | return true; |
58 | | } |
59 | | } catch (...) { |
60 | | LOG(WARNING) << "dump heap profile failed"; |
61 | | return false; |
62 | | } |
63 | | #else |
64 | 0 | return false; |
65 | 0 | #endif |
66 | 0 | } |
67 | | |
68 | 0 | static std::string jeprof_profile_to_dot(const std::string& profile_file_name) { |
69 | 0 | AgentUtils util; |
70 | 0 | const static std::string jeprof_path = fmt::format("{}/bin/jeprof", std::getenv("DORIS_HOME")); |
71 | 0 | const static std::string binary_path = |
72 | 0 | fmt::format("{}/lib/doris_be", std::getenv("DORIS_HOME")); |
73 | | // https://doris.apache.org/community/developer-guide/debug-tool/#3-jeprof-parses-heap-profile |
74 | 0 | std::string jeprof_cmd = |
75 | 0 | fmt::format("{} --dot {} {}", jeprof_path, binary_path, profile_file_name); |
76 | 0 | std::string msg; |
77 | 0 | bool rc = util.exec_cmd(jeprof_cmd, &msg); |
78 | 0 | if (!rc) { |
79 | 0 | LOG(WARNING) << "jeprof profile to dot failed: " << msg; |
80 | 0 | } |
81 | 0 | return msg; |
82 | 0 | } |
83 | | |
84 | 0 | void HeapProfiler::heap_profiler_start() { |
85 | 0 | set_prof_active(true); |
86 | 0 | } |
87 | | |
88 | 0 | void HeapProfiler::heap_profiler_stop() { |
89 | 0 | set_prof_active(false); |
90 | 0 | } |
91 | | |
92 | 0 | bool HeapProfiler::check_active_heap_profiler() { |
93 | | #ifdef USE_JEMALLOC |
94 | | size_t value = 0; |
95 | | size_t sz = sizeof(value); |
96 | | // A Bug in Jemalloc 5.3, `jemallctl("prof", &value, &sz, nullptr, 0)` always returns `0`, |
97 | | // the real conf `prof` value cannot be checked. |
98 | | jemallctl("prof.active", &value, &sz, nullptr, 0); |
99 | | return value; |
100 | | #else |
101 | 0 | return false; |
102 | 0 | #endif |
103 | 0 | } |
104 | | |
105 | 0 | bool HeapProfiler::check_enable_heap_profiler() { |
106 | | #ifdef USE_JEMALLOC |
107 | | bool prof = false; |
108 | | size_t sz = sizeof(prof); |
109 | | if (jemallctl("opt.prof", &prof, &sz, nullptr, 0) != 0) { |
110 | | LOG(WARNING) << "Failed to get option: opt.prof"; |
111 | | } |
112 | | return prof; |
113 | | #else |
114 | 0 | return false; |
115 | 0 | #endif |
116 | 0 | } |
117 | | |
118 | 0 | std::string HeapProfiler::dump_heap_profile() { |
119 | 0 | if (!config::jeprofile_dir.empty()) { |
120 | 0 | auto st = io::global_local_filesystem()->create_directory(config::jeprofile_dir); |
121 | 0 | if (!st.ok()) { |
122 | 0 | LOG(WARNING) << "create jeprofile dir failed."; |
123 | 0 | return ""; |
124 | 0 | } |
125 | 0 | } |
126 | 0 | std::string profile_file_name = |
127 | 0 | fmt::format("{}/jeheap_dump.{}.{}.{}.heap", config::jeprofile_dir, std::time(nullptr), |
128 | 0 | getpid(), rand()); |
129 | 0 | if (get_prof_dump(profile_file_name)) { |
130 | 0 | return profile_file_name; |
131 | 0 | } else { |
132 | 0 | return ""; |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | 0 | std::string HeapProfiler::dump_heap_profile_to_dot() { |
137 | 0 | std::string profile_file_name = dump_heap_profile(); |
138 | 0 | if (!profile_file_name.empty()) { |
139 | 0 | return jeprof_profile_to_dot(profile_file_name); |
140 | 0 | } else { |
141 | 0 | return ""; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 0 | bool HeapProfiler::heap_profiler_reset(size_t lg_sample) { |
146 | | #ifdef USE_JEMALLOC |
147 | | int ret = jemallctl("prof.reset", nullptr, nullptr, &lg_sample, sizeof(lg_sample)); |
148 | | if (ret != 0) { |
149 | | LOG(WARNING) << "jemallctl failed to set prof.reset:" << lg_sample |
150 | | << ".Check whether JEMALLOC_CONF is configured with prof:true."; |
151 | | return false; |
152 | | } |
153 | | #endif |
154 | 0 | return true; |
155 | 0 | } |
156 | | |
157 | | } // namespace doris |