CMS 3D CMS Logo

jeprof.cc
Go to the documentation of this file.
3 #include <string>
4 #include <dlfcn.h>
5 #include <cstdio>
6 #include <cstring>
7 #include <mutex>
8 
9 extern "C" {
10 typedef int (*mallctl_t)(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
11 }
12 
13 namespace {
14  bool initialize_prof();
15  mallctl_t mallctl = nullptr;
16  const bool have_jemalloc_and_prof = initialize_prof();
17 
18  bool initialize_prof() {
19  // check if mallctl and friends are available, if we are using jemalloc
20  mallctl = (mallctl_t)::dlsym(RTLD_DEFAULT, "mallctl");
21  if (mallctl == nullptr)
22  return false;
23  // check if heap profiling available, if --enable-prof was specified at build time
24  bool enable_prof = false;
25  size_t bool_s = sizeof(bool);
26  mallctl("prof.active", &enable_prof, &bool_s, nullptr, 0);
27  return enable_prof;
28  }
29 } // namespace
30 
31 namespace cms::jeprof {
32  std::once_flag warning_flag;
33  void makeHeapDump(const char *fileName) {
34  std::once_flag warning_flag;
35  if (!have_jemalloc_and_prof) {
36  std::call_once(warning_flag,
37  []() {
38  edm::LogWarning("JeProfModule")
39  << "JeProfModule requested but application is not"
40  << " currently being profiled with jemalloc profiling enabled\n"
41  << "Enable jemalloc profiling by running\n"
42  << "MALLOC_CONF=prof_leak:true,lg_prof_sample:10,prof_final:true cmsRunJEProf config.py\n";
43  });
44  return;
45  }
46  mallctl("prof.dump", nullptr, nullptr, &fileName, sizeof(const char *));
47  }
48 } // namespace cms::jeprof
void makeHeapDump(const char *)
Definition: jeprof.cc:33
std::once_flag warning_flag
Definition: jeprof.cc:32
int(* mallctl_t)(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
Definition: jeprof.cc:10
Log< level::Warning, false > LogWarning