diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index b63fc13c1028d4..6ced08cff90f32 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -27,12 +27,12 @@ #include +#include #include #include #include - -#include +#include namespace chip { namespace DeviceLayer { @@ -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 diff --git a/src/platform/Zephyr/PlatformManagerImpl.h b/src/platform/Zephyr/PlatformManagerImpl.h index 37160c89d6c49c..e7999e579455ed 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.h +++ b/src/platform/Zephyr/PlatformManagerImpl.h @@ -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.