Skip to content

Commit

Permalink
[Zephyr] Expose heap usage statistics (#10058)
Browse files Browse the repository at this point in the history
Implement methods used by Software Diagnostics Cluster
for Zephyr-based platforms.
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Oct 4, 2021
1 parent 25f2483 commit 479efc6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/platform/Zephyr/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <lib/support/logging/CHIPLogging.h>
#include <platform/PlatformManager.h>
#include <platform/internal/GenericPlatformManagerImpl_Zephyr.cpp>

#include <drivers/entropy.h>

#include <lib/support/logging/CHIPLogging.h>
#include <malloc.h>

namespace chip {
namespace DeviceLayer {
Expand Down Expand Up @@ -87,5 +87,41 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
return err;
}

CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapFree(uint64_t & currentHeapFree)
{
#ifdef CONFIG_NEWLIB_LIBC
// This will return the amount of memory which has been allocated from the system, but is not
// used right now. Ideally, this value should be increased by the amount of memory which can
// be allocated from the system, but Zephyr does not expose that number.
currentHeapFree = mallinfo().fordblks;
return CHIP_NO_ERROR;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}

CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapUsed(uint64_t & currentHeapUsed)
{
#ifdef CONFIG_NEWLIB_LIBC
currentHeapUsed = mallinfo().uordblks;
return CHIP_NO_ERROR;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}

CHIP_ERROR PlatformManagerImpl::_GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark)
{
#ifdef CONFIG_NEWLIB_LIBC
// ARM newlib does not provide a way to obtain the peak heap usage, so for now just return
// the amount of memory allocated from the system which should be an upper bound of the peak
// usage provided that the heap is not very fragmented.
currentHeapHighWatermark = mallinfo().arena;
return CHIP_NO_ERROR;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}

} // namespace DeviceLayer
} // namespace chip
3 changes: 3 additions & 0 deletions src/platform/Zephyr/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
// ===== Methods that implement the PlatformManager abstract interface.

CHIP_ERROR _InitChipStack(void);
CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree);
CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed);
CHIP_ERROR _GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark);

// ===== Members for internal use by the following friends.

Expand Down

0 comments on commit 479efc6

Please sign in to comment.