-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathstats.cc
More file actions
311 lines (284 loc) · 10.6 KB
/
stats.cc
File metadata and controls
311 lines (284 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "source/common/memory/stats.h"
#include <atomic>
#include <cstdint>
#include "source/common/common/assert.h"
#include "source/common/common/logger.h"
#if defined(TCMALLOC)
#include "tcmalloc/malloc_extension.h"
#elif defined(GPERFTOOLS_TCMALLOC)
#include "gperftools/malloc_extension.h"
#elif defined(JEMALLOC)
#include <jemalloc/jemalloc.h>
#endif
namespace Envoy {
namespace Memory {
namespace {
std::atomic<uint64_t> max_unfreed_memory_bytes{DEFAULT_MAX_UNFREED_MEMORY_BYTES};
} // namespace
uint64_t maxUnfreedMemoryBytes() {
return max_unfreed_memory_bytes.load(std::memory_order_relaxed);
}
void setMaxUnfreedMemoryBytes(uint64_t value) {
max_unfreed_memory_bytes.store(value, std::memory_order_relaxed);
}
#if defined(JEMALLOC)
namespace {
// Refresh jemalloc's epoch so that subsequently-read stats reflect current state.
void refreshJemallocEpoch() {
uint64_t epoch = 1;
size_t sz = sizeof(epoch);
mallctl("epoch", &epoch, &sz, &epoch, sz);
}
} // namespace
#endif
uint64_t Stats::totalCurrentlyAllocated() {
#if defined(TCMALLOC)
return tcmalloc::MallocExtension::GetNumericProperty("generic.current_allocated_bytes")
.value_or(0);
#elif defined(GPERFTOOLS_TCMALLOC)
size_t value = 0;
MallocExtension::instance()->GetNumericProperty("generic.current_allocated_bytes", &value);
return value;
#elif defined(JEMALLOC)
refreshJemallocEpoch();
size_t allocated = 0;
size_t sz = sizeof(allocated);
mallctl("stats.allocated", &allocated, &sz, nullptr, 0);
return allocated;
#else
return 0;
#endif
}
uint64_t Stats::totalCurrentlyReserved() {
#if defined(TCMALLOC)
// In Google's tcmalloc the semantics of generic.heap_size has
// changed: it doesn't include unmapped bytes.
return tcmalloc::MallocExtension::GetNumericProperty("generic.heap_size").value_or(0) +
tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.pageheap_unmapped_bytes")
.value_or(0);
#elif defined(GPERFTOOLS_TCMALLOC)
size_t value = 0;
MallocExtension::instance()->GetNumericProperty("generic.heap_size", &value);
return value;
#elif defined(JEMALLOC)
refreshJemallocEpoch();
size_t mapped = 0;
size_t sz = sizeof(mapped);
mallctl("stats.mapped", &mapped, &sz, nullptr, 0);
return mapped;
#else
return 0;
#endif
}
uint64_t Stats::totalThreadCacheBytes() {
#if defined(TCMALLOC)
return tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.current_total_thread_cache_bytes")
.value_or(0);
#elif defined(GPERFTOOLS_TCMALLOC)
size_t value = 0;
MallocExtension::instance()->GetNumericProperty("tcmalloc.current_total_thread_cache_bytes",
&value);
return value;
#elif defined(JEMALLOC)
// jemalloc uses per-arena caches rather than per-thread caches; no direct equivalent.
return 0;
#else
return 0;
#endif
}
uint64_t Stats::totalPageHeapFree() {
#if defined(TCMALLOC)
return tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.pageheap_free_bytes").value_or(0);
#elif defined(GPERFTOOLS_TCMALLOC)
size_t value = 0;
MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_free_bytes", &value);
return value;
#elif defined(JEMALLOC)
refreshJemallocEpoch();
size_t active = 0, allocated = 0;
size_t sz = sizeof(size_t);
mallctl("stats.active", &active, &sz, nullptr, 0);
mallctl("stats.allocated", &allocated, &sz, nullptr, 0);
return active > allocated ? active - allocated : 0;
#else
return 0;
#endif
}
uint64_t Stats::totalPageHeapUnmapped() {
#if defined(TCMALLOC)
return tcmalloc::MallocExtension::GetNumericProperty("tcmalloc.pageheap_unmapped_bytes")
.value_or(0);
#elif defined(GPERFTOOLS_TCMALLOC)
size_t value = 0;
MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_unmapped_bytes", &value);
return value;
#elif defined(JEMALLOC)
refreshJemallocEpoch();
size_t retained = 0;
size_t sz = sizeof(retained);
mallctl("stats.retained", &retained, &sz, nullptr, 0);
return retained;
#else
return 0;
#endif
}
uint64_t Stats::totalPhysicalBytes() {
#if defined(TCMALLOC)
return tcmalloc::MallocExtension::GetProperties()["generic.physical_memory_used"].value;
#elif defined(GPERFTOOLS_TCMALLOC)
size_t value = 0;
MallocExtension::instance()->GetNumericProperty("generic.total_physical_bytes", &value);
return value;
#elif defined(JEMALLOC)
refreshJemallocEpoch();
size_t resident = 0;
size_t sz = sizeof(resident);
mallctl("stats.resident", &resident, &sz, nullptr, 0);
return resident;
#else
return 0;
#endif
}
void Stats::dumpStatsToLog() {
#if defined(TCMALLOC)
ENVOY_LOG_MISC(debug, "TCMalloc stats:\n{}", tcmalloc::MallocExtension::GetStats());
#elif defined(GPERFTOOLS_TCMALLOC)
constexpr int buffer_size = 100000;
auto buffer = std::make_unique<char[]>(buffer_size);
MallocExtension::instance()->GetStats(buffer.get(), buffer_size);
ENVOY_LOG_MISC(debug, "TCMalloc stats:\n{}", buffer.get());
#elif defined(JEMALLOC)
std::string output;
malloc_stats_print(
[](void* opaque, const char* msg) { reinterpret_cast<std::string*>(opaque)->append(msg); },
&output, nullptr);
ENVOY_LOG_MISC(debug, "jemalloc stats:\n{}", output);
#else
return;
#endif
}
absl::optional<std::string> Stats::dumpStats() {
#if defined(TCMALLOC)
return tcmalloc::MallocExtension::GetStats();
#elif defined(GPERFTOOLS_TCMALLOC)
constexpr int buffer_size = 100000;
std::string buffer(buffer_size, '\0');
MallocExtension::instance()->GetStats(buffer.data(), buffer_size);
buffer.resize(strlen(buffer.c_str()));
return buffer;
#elif defined(JEMALLOC)
std::string output;
malloc_stats_print(
[](void* opaque, const char* msg) { reinterpret_cast<std::string*>(opaque)->append(msg); },
&output, nullptr);
return output;
#else
return absl::nullopt;
#endif
}
namespace {
/**
* Computes the background release rate in bytes per second from the configured bytes_to_release
* and memory_release_interval.
*/
size_t computeBackgroundReleaseRate(uint64_t bytes_to_release,
std::chrono::milliseconds memory_release_interval_msec) {
if (bytes_to_release == 0 || memory_release_interval_msec.count() == 0) {
return 0;
}
return static_cast<size_t>(bytes_to_release * 1000 / memory_release_interval_msec.count());
}
} // namespace
AllocatorManager::AllocatorManager(
Api::Api& api, const envoy::config::bootstrap::v3::MemoryAllocatorManager& config)
: bytes_to_release_(config.bytes_to_release()),
memory_release_interval_msec_(std::chrono::milliseconds(
PROTOBUF_GET_MS_OR_DEFAULT(config, memory_release_interval, 1000))),
background_release_rate_bytes_per_second_(
computeBackgroundReleaseRate(bytes_to_release_, memory_release_interval_msec_)),
api_(api) {
configureTcmallocOptions(config);
configureBackgroundMemoryRelease();
};
AllocatorManager::~AllocatorManager() {
#if defined(TCMALLOC)
if (tcmalloc_thread_) {
// Signal the ProcessBackgroundActions loop to exit and wait for the thread to finish.
tcmalloc::MallocExtension::SetBackgroundProcessActionsEnabled(false);
tcmalloc_thread_->join();
tcmalloc_thread_.reset();
// Reset the release rate and re-enable background actions so that a subsequent
// AllocatorManager instance can start fresh.
tcmalloc::MallocExtension::SetBackgroundReleaseRate(
tcmalloc::MallocExtension::BytesPerSecond{0});
tcmalloc::MallocExtension::SetBackgroundProcessActionsEnabled(true);
}
#endif
}
void AllocatorManager::configureTcmallocOptions(
const envoy::config::bootstrap::v3::MemoryAllocatorManager& config) {
if (config.max_unfreed_memory_bytes() > 0) {
setMaxUnfreedMemoryBytes(config.max_unfreed_memory_bytes());
ENVOY_LOG_MISC(info, "Set max unfreed memory threshold to {} bytes.",
config.max_unfreed_memory_bytes());
}
#if defined(TCMALLOC)
if (config.has_soft_memory_limit_bytes()) {
tcmalloc::MallocExtension::SetMemoryLimit(config.soft_memory_limit_bytes().value(),
tcmalloc::MallocExtension::LimitKind::kSoft);
ENVOY_LOG_MISC(info, "Set tcmalloc soft memory limit to {} bytes.",
config.soft_memory_limit_bytes().value());
}
if (config.has_max_per_cpu_cache_size_bytes()) {
tcmalloc::MallocExtension::SetMaxPerCpuCacheSize(config.max_per_cpu_cache_size_bytes().value());
ENVOY_LOG_MISC(info, "Set tcmalloc max per-CPU cache size to {} bytes.",
config.max_per_cpu_cache_size_bytes().value());
}
#else
if (config.has_soft_memory_limit_bytes()) {
ENVOY_LOG_MISC(warn, "Soft memory limit is only supported with Google's tcmalloc, ignoring.");
}
if (config.has_max_per_cpu_cache_size_bytes()) {
ENVOY_LOG_MISC(warn,
"Max per-CPU cache size is only supported with Google's tcmalloc, ignoring.");
}
#endif
}
/**
* Configures tcmalloc to use its native ProcessBackgroundActions for background memory
* maintenance. This enables comprehensive memory management including per-CPU cache reclamation,
* cache shuffling, size class resizing, transfer cache plundering, and memory release at the
* configured rate. If `bytes_to_release_` is `0`, no background processing will be started.
*/
void AllocatorManager::configureBackgroundMemoryRelease() {
#if defined(GPERFTOOLS_TCMALLOC)
if (bytes_to_release_ > 0) {
ENVOY_LOG_MISC(error,
"Memory releasing is not supported for gperf tcmalloc, no memory releasing "
"will be configured.");
}
#elif defined(TCMALLOC)
ENVOY_BUG(!tcmalloc_thread_, "Invalid state, tcmalloc has already been initialised.");
if (bytes_to_release_ > 0) {
if (!tcmalloc::MallocExtension::NeedsProcessBackgroundActions()) {
ENVOY_LOG_MISC(warn, "This platform does not support tcmalloc background actions.");
return;
}
tcmalloc::MallocExtension::SetBackgroundReleaseRate(
tcmalloc::MallocExtension::BytesPerSecond{background_release_rate_bytes_per_second_});
tcmalloc_thread_ = api_.threadFactory().createThread(
[]() -> void {
ENVOY_LOG_MISC(debug, "Started {}.", TCMALLOC_ROUTINE_THREAD_ID);
// ProcessBackgroundActions runs an infinite loop that handles all tcmalloc background
// maintenance including cache reclamation and memory release. It returns only when
// SetBackgroundProcessActionsEnabled(false) is called.
tcmalloc::MallocExtension::ProcessBackgroundActions();
},
Thread::Options{std::string(TCMALLOC_ROUTINE_THREAD_ID)});
ENVOY_LOG_MISC(info, "Configured tcmalloc with background release rate: {} bytes per second.",
background_release_rate_bytes_per_second_);
}
#endif
}
} // namespace Memory
} // namespace Envoy