CMS 3D CMS Logo

memory_usage.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <dlfcn.h>
3 
5 
6 // see <jemalloc/jemalloc.h>
7 extern "C" {
8 typedef int (*mallctl_t)(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
9 typedef int (*mallctlnametomib_t)(const char *name, size_t *mibp, size_t *miblenp);
10 typedef int (*mallctlbymib_t)(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
11 }
12 
13 namespace {
14  bool initialise();
15  bool initialise_peak();
16  const uint64_t *initialise_thread_allocated_p();
17  const uint64_t *initialise_thread_deallocated_p();
18 
19  const uint64_t zero = 0UL;
20  size_t mib_peak_read[3]; // management information base for "thread.peak.read"
21  size_t mib_peak_reset[3]; // management information base for "thread.peak.reset"
22  thread_local const uint64_t *thread_allocated_p = initialise_thread_allocated_p();
23  thread_local const uint64_t *thread_deallocated_p = initialise_thread_deallocated_p();
24 
25  mallctl_t mallctl = nullptr;
26  mallctlnametomib_t mallctlnametomib = nullptr;
27  mallctlbymib_t mallctlbymib = nullptr;
28  const bool have_jemalloc_and_stats = initialise();
29  const bool have_jemalloc_and_peak = have_jemalloc_and_stats and initialise_peak();
30 
31  bool initialise() {
32  // check if mallctl and friends are available, if we are using jemalloc
33  mallctl = (mallctl_t)::dlsym(RTLD_DEFAULT, "mallctl");
34  if (mallctl == nullptr)
35  return false;
36  mallctlnametomib = (mallctlnametomib_t)::dlsym(RTLD_DEFAULT, "mallctlnametomib");
37  if (mallctlnametomib == nullptr)
38  return false;
39  mallctlbymib = (mallctlbymib_t)::dlsym(RTLD_DEFAULT, "mallctlbymib");
40  if (mallctlbymib == nullptr)
41  return false;
42 
43  // check if the statistics are available, if --enable-stats was specified at build time
44  bool enable_stats = false;
45  size_t bool_s = sizeof(bool);
46  mallctl("config.stats", &enable_stats, &bool_s, nullptr, 0);
47  return enable_stats;
48  }
49 
50  bool initialise_peak() {
51  // check if thread.peak.read and thread.peak.reset are available
52  size_t miblen = 3;
53  if (mallctlnametomib("thread.peak.read", mib_peak_read, &miblen) != 0)
54  return false;
55  if (mallctlnametomib("thread.peak.reset", mib_peak_reset, &miblen) != 0)
56  return false;
57  return true;
58  }
59 
60  const uint64_t *initialise_thread_allocated_p() {
61  const uint64_t *stats = &zero;
62  size_t ptr_s = sizeof(uint64_t *);
63 
64  if (have_jemalloc_and_stats)
65  // get a pointer to the thread-specific allocation statistics
66  mallctl("thread.allocatedp", &stats, &ptr_s, nullptr, 0);
67 
68  return stats;
69  }
70 
71  const uint64_t *initialise_thread_deallocated_p() {
72  const uint64_t *stats = &zero;
73  size_t ptr_s = sizeof(uint64_t *);
74 
75  if (have_jemalloc_and_stats)
76  // get a pointer to the thread-specific allocation statistics
77  mallctl("thread.deallocatedp", &stats, &ptr_s, nullptr, 0);
78 
79  return stats;
80  }
81 
82 } // namespace
83 
84 bool memory_usage::is_available() { return have_jemalloc_and_stats; }
85 
86 uint64_t memory_usage::allocated() { return *thread_allocated_p; }
87 
88 uint64_t memory_usage::deallocated() { return *thread_deallocated_p; }
89 
91  uint64_t peak = 0;
92  size_t size = sizeof(uint64_t);
93  if (have_jemalloc_and_peak)
94  mallctlbymib(mib_peak_read, 3, &peak, &size, nullptr, 0);
95  return peak;
96 }
97 
99  if (have_jemalloc_and_peak)
100  mallctlbymib(mib_peak_reset, 3, nullptr, nullptr, nullptr, 0);
101 }
electrons_cff.bool
bool
Definition: electrons_cff.py:372
mallctlnametomib_t
int(* mallctlnametomib_t)(const char *name, size_t *mibp, size_t *miblenp)
Definition: memory_usage.cc:9
memory_usage.h
mallctl_t
int(* mallctl_t)(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
Definition: memory_usage.cc:8
memory_usage::peak
static uint64_t peak()
Definition: memory_usage.cc:90
mallctlbymib_t
int(* mallctlbymib_t)(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
Definition: memory_usage.cc:10
memory_usage::deallocated
static uint64_t deallocated()
Definition: memory_usage.cc:88
createfilelist.int
int
Definition: createfilelist.py:10
memory_usage::allocated
static uint64_t allocated()
Definition: memory_usage.cc:86
dqmMemoryStats.stats
stats
Definition: dqmMemoryStats.py:134
memory_usage::reset_peak
static void reset_peak()
Definition: memory_usage.cc:98
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
memory_usage::is_available
static bool is_available()
Definition: memory_usage.cc:84
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443