Skip to content

Commit

Permalink
[Linux]DGSW_2_3 fails CurrentHeapUsed is greater than CurrentHeapHigh…
Browse files Browse the repository at this point in the history
…Watermark (project-chip#33252)

* changes to store max heap size

* Restyled by whitespace

* Restyled by clang-format

* typecast changes

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
Thirsrin and restyled-commits committed Jul 31, 2024
1 parent b498d70 commit 4a79d2f
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/platform/Linux/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand All @@ -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<int>(maxHeapHighWatermark))
{
maxHeapHighWatermark = mallocInfo.uordblks;
}

// Set the current heap high watermark.
currentHeapHighWatermark = maxHeapHighWatermark;

return CHIP_NO_ERROR;
#endif
Expand All @@ -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;
}
Expand Down

0 comments on commit 4a79d2f

Please sign in to comment.