1
#pragma once
2

            
3
#include <cstdint>
4

            
5
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
6

            
7
#include "source/common/common/thread.h"
8
#include "source/common/protobuf/utility.h"
9

            
10
namespace Envoy {
11
namespace Memory {
12

            
13
constexpr absl::string_view TCMALLOC_ROUTINE_THREAD_ID = "TcmallocProcessBackgroundActions";
14
constexpr uint64_t DEFAULT_MAX_UNFREED_MEMORY_BYTES = 100 * 1024 * 1024;
15

            
16
/**
17
 * Accessors for the configurable max unfreed memory threshold. This value controls when
18
 * tryShrinkHeap releases memory back to the OS. Defaults to 100 MB.
19
 */
20
uint64_t maxUnfreedMemoryBytes();
21
void setMaxUnfreedMemoryBytes(uint64_t value);
22

            
23
/**
24
 * Runtime stats for process memory usage.
25
 */
26
class Stats {
27
public:
28
  /**
29
   * @return uint64_t the total memory currently allocated.
30
   */
31
  static uint64_t totalCurrentlyAllocated();
32

            
33
  /**
34
   * @return uint64_t the total memory reserved for the process by the heap but not necessarily
35
   *                  allocated.
36
   */
37
  static uint64_t totalCurrentlyReserved();
38

            
39
  /**
40
   * @return uint64_t the amount of memory used by the TCMalloc thread caches (for small objects).
41
   */
42
  static uint64_t totalThreadCacheBytes();
43

            
44
  /**
45
   * @return uint64_t the number of bytes in free, unmapped pages in the page heap. These bytes
46
   *                  always count towards virtual memory usage, and depending on the OS, typically
47
   *                  do not count towards physical memory usage.
48
   */
49
  static uint64_t totalPageHeapUnmapped();
50

            
51
  /**
52
   * @return uint64_t the number of bytes in free, mapped pages in the page heap. These bytes always
53
   *                  count towards virtual memory usage, and unless the underlying memory is
54
   *                  swapped out by the OS, they also count towards physical memory usage.
55
   */
56
  static uint64_t totalPageHeapFree();
57

            
58
  /**
59
   * @return uint64_t estimate of total bytes of the physical memory usage by the allocator
60
   */
61
  static uint64_t totalPhysicalBytes();
62

            
63
  /**
64
   * Log detailed stats about current memory allocation. Intended for debugging purposes.
65
   */
66
  static void dumpStatsToLog();
67

            
68
  /**
69
   * Get detailed stats about current memory allocation. Returns nullopt if not supported.
70
   */
71
  static absl::optional<std::string> dumpStats();
72
};
73

            
74
/**
75
 * Manages tcmalloc background memory release using the native ProcessBackgroundActions API.
76
 * When configured with a non-zero release rate, a dedicated thread is started that runs
77
 * tcmalloc's ProcessBackgroundActions, which handles per-CPU cache reclamation, cache shuffling,
78
 * size class resizing, transfer cache plundering, and memory release to the OS at the configured
79
 * rate. Also supports configuring a soft memory limit, per-CPU cache size, and the threshold
80
 * for tryShrinkHeap.
81
 */
82
class AllocatorManager {
83
public:
84
  AllocatorManager(Api::Api& api,
85
                   const envoy::config::bootstrap::v3::MemoryAllocatorManager& config);
86

            
87
  ~AllocatorManager();
88

            
89
private:
90
  const uint64_t bytes_to_release_;
91
  const std::chrono::milliseconds memory_release_interval_msec_;
92
  const size_t background_release_rate_bytes_per_second_;
93
  Api::Api& api_;
94
  Thread::ThreadPtr tcmalloc_thread_;
95
  void configureBackgroundMemoryRelease();
96
  void configureTcmallocOptions(const envoy::config::bootstrap::v3::MemoryAllocatorManager& config);
97
  // Used for testing.
98
  friend class AllocatorManagerPeer;
99
};
100

            
101
} // namespace Memory
102
} // namespace Envoy