From 7f701ae7f21198fe670c773bf5803d02d309bfb1 Mon Sep 17 00:00:00 2001
From: Thirupathi S <Thirupathi.S@silabs.com>
Date: Wed, 1 May 2024 18:24:55 +0530
Subject: [PATCH 1/4] changes to store max heap size

---
 .../Linux/DiagnosticDataProviderImpl.cpp      | 20 ++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.cpp b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
index 74f7003676c051..fb0725fa59054f 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,10 @@ 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 +267,14 @@ 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 (static_cast<ssize_t>(mallocInfo.uordblks) > static_cast<ssize_t>(maxHeapHighWatermark)) {
+        maxHeapHighWatermark = mallocInfo.uordblks;
+    }
+
+    // Set the current heap high watermark.
+    currentHeapHighWatermark = maxHeapHighWatermark;
 
     return CHIP_NO_ERROR;
 #endif
@@ -275,6 +287,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;
 }

From aed4131f126e50ec2efabae7833df9b68d76aedd Mon Sep 17 00:00:00 2001
From: "Restyled.io" <commits@restyled.io>
Date: Wed, 1 May 2024 13:11:45 +0000
Subject: [PATCH 2/4] Restyled by whitespace

---
 src/platform/Linux/DiagnosticDataProviderImpl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.cpp b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
index fb0725fa59054f..ee16a26e080789 100644
--- a/src/platform/Linux/DiagnosticDataProviderImpl.cpp
+++ b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
@@ -267,7 +267,7 @@ 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. 
+    // to know accurately peak physical memory it use.
     // Update the maximum heap high watermark if the current heap usage exceeds it.
     if (static_cast<ssize_t>(mallocInfo.uordblks) > static_cast<ssize_t>(maxHeapHighWatermark)) {
         maxHeapHighWatermark = mallocInfo.uordblks;

From 80d9f6883b8b17e40535574da90dc4e4cef1ab53 Mon Sep 17 00:00:00 2001
From: "Restyled.io" <commits@restyled.io>
Date: Wed, 1 May 2024 13:11:47 +0000
Subject: [PATCH 3/4] Restyled by clang-format

---
 src/platform/Linux/DiagnosticDataProviderImpl.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.cpp b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
index ee16a26e080789..6718d28367e461 100644
--- a/src/platform/Linux/DiagnosticDataProviderImpl.cpp
+++ b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
@@ -248,7 +248,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeap
     currentHeapUsed = mallocInfo.uordblks;
 
     // Update the maximum heap high watermark if the current heap usage exceeds it.
-    if (currentHeapUsed > maxHeapHighWatermark) {
+    if (currentHeapUsed > maxHeapHighWatermark)
+    {
         maxHeapHighWatermark = currentHeapUsed;
     }
     return CHIP_NO_ERROR;
@@ -269,7 +270,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu
     // 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.
     // Update the maximum heap high watermark if the current heap usage exceeds it.
-    if (static_cast<ssize_t>(mallocInfo.uordblks) > static_cast<ssize_t>(maxHeapHighWatermark)) {
+    if (static_cast<ssize_t>(mallocInfo.uordblks) > static_cast<ssize_t>(maxHeapHighWatermark))
+    {
         maxHeapHighWatermark = mallocInfo.uordblks;
     }
 
@@ -288,7 +290,7 @@ 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;
+    maxHeapHighWatermark       = mallocInfo.uordblks;
 
     return CHIP_NO_ERROR;
 }

From 6931e9f1511c595c5dfe7448dba0ab6517609832 Mon Sep 17 00:00:00 2001
From: Thirupathi S <Thirupathi.S@silabs.com>
Date: Thu, 2 May 2024 11:45:04 +0530
Subject: [PATCH 4/4] typecast changes

---
 src/platform/Linux/DiagnosticDataProviderImpl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/platform/Linux/DiagnosticDataProviderImpl.cpp b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
index 6718d28367e461..cc57da65e2d036 100644
--- a/src/platform/Linux/DiagnosticDataProviderImpl.cpp
+++ b/src/platform/Linux/DiagnosticDataProviderImpl.cpp
@@ -270,7 +270,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu
     // 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.
     // Update the maximum heap high watermark if the current heap usage exceeds it.
-    if (static_cast<ssize_t>(mallocInfo.uordblks) > static_cast<ssize_t>(maxHeapHighWatermark))
+    if (mallocInfo.uordblks > static_cast<int>(maxHeapHighWatermark))
     {
         maxHeapHighWatermark = mallocInfo.uordblks;
     }