LCOV - code coverage report
Current view: top level - source/common/memory - utils.cc (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 8 14 57.1 %
Date: 2024-01-05 06:35:25 Functions: 1 2 50.0 %

          Line data    Source code
       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           0 :   MallocExtension::instance()->ReleaseFreeMemory();
      27           0 : #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         222 : void Utils::tryShrinkHeap() {
      37         222 : #if defined(TCMALLOC) || defined(GPERFTOOLS_TCMALLOC)
      38         222 :   auto total_physical_bytes = Stats::totalPhysicalBytes();
      39         222 :   auto allocated_size_by_app = Stats::totalCurrentlyAllocated();
      40             : 
      41         222 :   if (total_physical_bytes >= allocated_size_by_app &&
      42         222 :       (total_physical_bytes - allocated_size_by_app) >= MAX_UNFREED_MEMORY_BYTE) {
      43           0 :     Utils::releaseFreeMemory();
      44           0 :   }
      45         222 : #endif
      46         222 : }
      47             : 
      48             : } // namespace Memory
      49             : } // namespace Envoy

Generated by: LCOV version 1.15