Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/memory/utils.cc
Line
Count
Source (jump to first uncovered line)
1
#include "source/common/memory/utils.h"
2
3
#include "source/common/common/assert.h"
4
#include "source/common/memory/stats.h"
5
6
#if defined(TCMALLOC)
7
#include "tcmalloc/malloc_extension.h"
8
#elif defined(GPERFTOOLS_TCMALLOC)
9
#include "gperftools/malloc_extension.h"
10
#endif
11
12
namespace Envoy {
13
namespace Memory {
14
15
namespace {
16
#if defined(TCMALLOC) || defined(GPERFTOOLS_TCMALLOC)
17
// TODO(zyfjeff): Make max unfreed memory byte configurable
18
constexpr uint64_t MAX_UNFREED_MEMORY_BYTE = 100 * 1024 * 1024;
19
#endif
20
} // namespace
21
22
0
void Utils::releaseFreeMemory() {
23
#if defined(TCMALLOC)
24
  tcmalloc::MallocExtension::ReleaseMemoryToSystem(MAX_UNFREED_MEMORY_BYTE);
25
#elif defined(GPERFTOOLS_TCMALLOC)
26
  MallocExtension::instance()->ReleaseFreeMemory();
27
#endif
28
0
}
29
30
/*
31
  The purpose of this function is to release the cache introduced by tcmalloc,
32
  mainly in xDS config updates, admin handler, and so on. all work on the main thread,
33
  so the overall impact on performance is small.
34
  Ref: https://github.com/envoyproxy/envoy/pull/9471#discussion_r363825985
35
*/
36
172
void Utils::tryShrinkHeap() {
37
#if defined(TCMALLOC) || defined(GPERFTOOLS_TCMALLOC)
38
  auto total_physical_bytes = Stats::totalPhysicalBytes();
39
  auto allocated_size_by_app = Stats::totalCurrentlyAllocated();
40
41
  if (total_physical_bytes >= allocated_size_by_app &&
42
      (total_physical_bytes - allocated_size_by_app) >= MAX_UNFREED_MEMORY_BYTE) {
43
    Utils::releaseFreeMemory();
44
  }
45
#endif
46
172
}
47
48
} // namespace Memory
49
} // namespace Envoy