From 4a79d2fce67d761e14ed28dd4e2edc725611b8ec Mon Sep 17 00:00:00 2001 From: Thirupathi S <108743108+Thirsrin@users.noreply.github.com> Date: Thu, 2 May 2024 19:20:30 +0530 Subject: [PATCH] [Linux]DGSW_2_3 fails CurrentHeapUsed is greater than CurrentHeapHighWatermark (#33252) * changes to store max heap size * Restyled by whitespace * Restyled by clang-format * typecast changes --------- Co-authored-by: Restyled.io --- .../Linux/DiagnosticDataProviderImpl.cpp | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.cpp b/src/platform/Linux/DiagnosticDataProviderImpl.cpp index 74f7003676c051..cc57da65e2d036 100644 --- a/src/platform/Linux/DiagnosticDataProviderImpl.cpp +++ b/src/platform/Linux/DiagnosticDataProviderImpl.cpp @@ -74,6 +74,9 @@ enum class WiFiStatsCountType kWiFiOverrunCount }; +// Static variable to store the maximum heap size +static size_t maxHeapHighWatermark = 0; + CHIP_ERROR GetEthernetStatsCount(EthernetStatsCountType type, uint64_t & count) { CHIP_ERROR err = CHIP_ERROR_READ_FAILED; @@ -244,6 +247,11 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeap // the current running program. currentHeapUsed = mallocInfo.uordblks; + // Update the maximum heap high watermark if the current heap usage exceeds it. + if (currentHeapUsed > maxHeapHighWatermark) + { + maxHeapHighWatermark = currentHeapUsed; + } return CHIP_NO_ERROR; #endif } @@ -260,9 +268,15 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu // has been used by the Node. // On Linux, since it uses virtual memory, whereby a page of memory could be copied to // the hard disk, called swap space, and free up that page of memory. So it is impossible - // to know accurately peak physical memory it use. We just return the current heap memory - // being used by the current running program. - currentHeapHighWatermark = mallocInfo.uordblks; + // to know accurately peak physical memory it use. + // Update the maximum heap high watermark if the current heap usage exceeds it. + if (mallocInfo.uordblks > static_cast(maxHeapHighWatermark)) + { + maxHeapHighWatermark = mallocInfo.uordblks; + } + + // Set the current heap high watermark. + currentHeapHighWatermark = maxHeapHighWatermark; return CHIP_NO_ERROR; #endif @@ -275,6 +289,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::ResetWatermarks() // On Linux, the write operation is non-op since we always rely on the mallinfo system // function to get the current heap memory. + struct mallinfo mallocInfo = mallinfo(); + maxHeapHighWatermark = mallocInfo.uordblks; return CHIP_NO_ERROR; }