Line data Source code
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 427 : uint64_t Stats::totalCurrentlyAllocated() { 60 427 : size_t value = 0; 61 427 : MallocExtension::instance()->GetNumericProperty("generic.current_allocated_bytes", &value); 62 427 : return value; 63 427 : } 64 : 65 205 : uint64_t Stats::totalCurrentlyReserved() { 66 205 : size_t value = 0; 67 205 : MallocExtension::instance()->GetNumericProperty("generic.heap_size", &value); 68 205 : return value; 69 205 : } 70 : 71 0 : uint64_t Stats::totalThreadCacheBytes() { 72 0 : size_t value = 0; 73 0 : MallocExtension::instance()->GetNumericProperty("tcmalloc.current_total_thread_cache_bytes", 74 0 : &value); 75 0 : return value; 76 0 : } 77 : 78 0 : uint64_t Stats::totalPageHeapFree() { 79 0 : size_t value = 0; 80 0 : MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_free_bytes", &value); 81 0 : return value; 82 0 : } 83 : 84 0 : uint64_t Stats::totalPageHeapUnmapped() { 85 0 : size_t value = 0; 86 0 : MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_unmapped_bytes", &value); 87 0 : return value; 88 0 : } 89 : 90 427 : uint64_t Stats::totalPhysicalBytes() { 91 427 : size_t value = 0; 92 427 : MallocExtension::instance()->GetNumericProperty("generic.total_physical_bytes", &value); 93 427 : return value; 94 427 : } 95 : 96 0 : void Stats::dumpStatsToLog() { 97 0 : constexpr int buffer_size = 100000; 98 0 : auto buffer = std::make_unique<char[]>(buffer_size); 99 0 : MallocExtension::instance()->GetStats(buffer.get(), buffer_size); 100 0 : ENVOY_LOG_MISC(debug, "TCMalloc stats:\n{}", buffer.get()); 101 0 : } 102 : 103 : } // namespace Memory 104 : } // namespace Envoy 105 : 106 : #else 107 : 108 : namespace Envoy { 109 : namespace Memory { 110 : 111 : uint64_t Stats::totalCurrentlyAllocated() { return 0; } 112 : uint64_t Stats::totalThreadCacheBytes() { return 0; } 113 : uint64_t Stats::totalCurrentlyReserved() { return 0; } 114 : uint64_t Stats::totalPageHeapUnmapped() { return 0; } 115 : uint64_t Stats::totalPageHeapFree() { return 0; } 116 : uint64_t Stats::totalPhysicalBytes() { return 0; } 117 : void Stats::dumpStatsToLog() {} 118 : 119 : } // namespace Memory 120 : } // namespace Envoy 121 : 122 : #endif // #if defined(TCMALLOC)