Skip to content

Commit de796eb

Browse files
[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 <commits@restyled.io>
1 parent f6b3594 commit de796eb

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/platform/Linux/DiagnosticDataProviderImpl.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ enum class WiFiStatsCountType
7474
kWiFiOverrunCount
7575
};
7676

77+
// Static variable to store the maximum heap size
78+
static size_t maxHeapHighWatermark = 0;
79+
7780
CHIP_ERROR GetEthernetStatsCount(EthernetStatsCountType type, uint64_t & count)
7881
{
7982
CHIP_ERROR err = CHIP_ERROR_READ_FAILED;
@@ -244,6 +247,11 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeap
244247
// the current running program.
245248
currentHeapUsed = mallocInfo.uordblks;
246249

250+
// Update the maximum heap high watermark if the current heap usage exceeds it.
251+
if (currentHeapUsed > maxHeapHighWatermark)
252+
{
253+
maxHeapHighWatermark = currentHeapUsed;
254+
}
247255
return CHIP_NO_ERROR;
248256
#endif
249257
}
@@ -260,9 +268,15 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu
260268
// has been used by the Node.
261269
// On Linux, since it uses virtual memory, whereby a page of memory could be copied to
262270
// the hard disk, called swap space, and free up that page of memory. So it is impossible
263-
// to know accurately peak physical memory it use. We just return the current heap memory
264-
// being used by the current running program.
265-
currentHeapHighWatermark = mallocInfo.uordblks;
271+
// to know accurately peak physical memory it use.
272+
// Update the maximum heap high watermark if the current heap usage exceeds it.
273+
if (mallocInfo.uordblks > static_cast<int>(maxHeapHighWatermark))
274+
{
275+
maxHeapHighWatermark = mallocInfo.uordblks;
276+
}
277+
278+
// Set the current heap high watermark.
279+
currentHeapHighWatermark = maxHeapHighWatermark;
266280

267281
return CHIP_NO_ERROR;
268282
#endif
@@ -275,6 +289,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::ResetWatermarks()
275289

276290
// On Linux, the write operation is non-op since we always rely on the mallinfo system
277291
// function to get the current heap memory.
292+
struct mallinfo mallocInfo = mallinfo();
293+
maxHeapHighWatermark = mallocInfo.uordblks;
278294

279295
return CHIP_NO_ERROR;
280296
}

0 commit comments

Comments
 (0)