Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/common/memory/stats.cc
Line
Count
Source (jump to first uncovered line)
1
#include "source/common/memory/stats.h"
2
3
#include <cstdint>
4
5
#include "source/common/common/logger.h"
6
7
#if defined(TCMALLOC)
8
9
#include "tcmalloc/malloc_extension.h"
10
11
namespace Envoy {
12
namespace Memory {
13
14
uint64_t Stats::totalCurrentlyAllocated() {
15
  return tcmalloc::MallocExtension::GetNumericProperty("generic.current_allocated_bytes")
16
      .value_or(0);
17
}
18
19
uint64_t Stats::totalCurrentlyReserved() {
20
  // In Google's tcmalloc the semantics of generic.heap_size has
21
  // changed: it doesn't include unmapped bytes.
22
  return tcmalloc::MallocExtension::GetNumericProperty("generic.heap_size").value_or(0) +
23
         tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.pageheap_unmapped_bytes")
24
             .value_or(0);
25
}
26
27
uint64_t Stats::totalThreadCacheBytes() {
28
  return tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.current_total_thread_cache_bytes")
29
      .value_or(0);
30
}
31
32
uint64_t Stats::totalPageHeapFree() {
33
  return tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.pageheap_free_bytes").value_or(0);
34
}
35
36
uint64_t Stats::totalPageHeapUnmapped() {
37
  return tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.pageheap_unmapped_bytes")
38
      .value_or(0);
39
}
40
41
uint64_t Stats::totalPhysicalBytes() {
42
  return tcmalloc::MallocExtension::GetProperties()["generic.physical_memory_used"].value;
43
}
44
45
void Stats::dumpStatsToLog() {
46
  ENVOY_LOG_MISC(debug, "TCMalloc stats:\n{}", tcmalloc::MallocExtension::GetStats());
47
}
48
49
} // namespace Memory
50
} // namespace Envoy
51
52
#elif defined(GPERFTOOLS_TCMALLOC)
53
54
#include "gperftools/malloc_extension.h"
55
56
namespace Envoy {
57
namespace Memory {
58
59
uint64_t Stats::totalCurrentlyAllocated() {
60
  size_t value = 0;
61
  MallocExtension::instance()->GetNumericProperty("generic.current_allocated_bytes", &value);
62
  return value;
63
}
64
65
uint64_t Stats::totalCurrentlyReserved() {
66
  size_t value = 0;
67
  MallocExtension::instance()->GetNumericProperty("generic.heap_size", &value);
68
  return value;
69
}
70
71
uint64_t Stats::totalThreadCacheBytes() {
72
  size_t value = 0;
73
  MallocExtension::instance()->GetNumericProperty("tcmalloc.current_total_thread_cache_bytes",
74
                                                  &value);
75
  return value;
76
}
77
78
uint64_t Stats::totalPageHeapFree() {
79
  size_t value = 0;
80
  MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_free_bytes", &value);
81
  return value;
82
}
83
84
uint64_t Stats::totalPageHeapUnmapped() {
85
  size_t value = 0;
86
  MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_unmapped_bytes", &value);
87
  return value;
88
}
89
90
uint64_t Stats::totalPhysicalBytes() {
91
  size_t value = 0;
92
  MallocExtension::instance()->GetNumericProperty("generic.total_physical_bytes", &value);
93
  return value;
94
}
95
96
void Stats::dumpStatsToLog() {
97
  constexpr int buffer_size = 100000;
98
  auto buffer = std::make_unique<char[]>(buffer_size);
99
  MallocExtension::instance()->GetStats(buffer.get(), buffer_size);
100
  ENVOY_LOG_MISC(debug, "TCMalloc stats:\n{}", buffer.get());
101
}
102
103
} // namespace Memory
104
} // namespace Envoy
105
106
#else
107
108
namespace Envoy {
109
namespace Memory {
110
111
6.43k
uint64_t Stats::totalCurrentlyAllocated() { return 0; }
112
0
uint64_t Stats::totalThreadCacheBytes() { return 0; }
113
6.43k
uint64_t Stats::totalCurrentlyReserved() { return 0; }
114
0
uint64_t Stats::totalPageHeapUnmapped() { return 0; }
115
0
uint64_t Stats::totalPageHeapFree() { return 0; }
116
6.43k
uint64_t Stats::totalPhysicalBytes() { return 0; }
117
0
void Stats::dumpStatsToLog() {}
118
119
} // namespace Memory
120
} // namespace Envoy
121
122
#endif // #if defined(TCMALLOC)