Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Diagnostics] RAM usage diagnostic sources #1411

Merged
merged 2 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hal/inc/core_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ typedef struct runtime_info_t {
uint16_t flags; /* reserved, set to 0. */
uint32_t freeheap; /* Amount of guaranteed heap memory available. */
uint32_t system_version;
uint32_t total_init_heap;
uint32_t total_heap;
uint32_t max_used_heap; // The "highwater mark" for allocated space—that is, the maximum amount of space that was ever allocated.
uint32_t user_static_ram;
} runtime_info_t;

uint32_t HAL_Core_Runtime_Info(runtime_info_t* info, void* reserved);
Expand Down
17 changes: 17 additions & 0 deletions hal/src/stm32f2xx/core_hal_stm32f2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
#include "wlan_hal.h"
#endif

extern char link_heap_location, link_heap_location_end;

#define STOP_MODE_EXIT_CONDITION_PIN 0x01
#define STOP_MODE_EXIT_CONDITION_RTC 0x02

Expand Down Expand Up @@ -1239,6 +1241,21 @@ uint32_t HAL_Core_Runtime_Info(runtime_info_t* info, void* reserved)
struct mallinfo heapinfo = mallinfo();
// fordblks The total number of bytes in free blocks.
info->freeheap = heapinfo.fordblks;
if (offsetof(runtime_info_t, total_init_heap) + sizeof(info->total_init_heap) <= info->size) {
info->total_init_heap = (uint32_t)(&link_heap_location_end - &link_heap_location);
}

if (offsetof(runtime_info_t, total_heap) + sizeof(info->total_heap) <= info->size) {
info->total_heap = heapinfo.arena;
}

if (offsetof(runtime_info_t, max_used_heap) + sizeof(info->max_used_heap) <= info->size) {
info->max_used_heap = heapinfo.usmblks;
}

if (offsetof(runtime_info_t, user_static_ram) + sizeof(info->user_static_ram) <= info->size) {
info->user_static_ram = info->total_init_heap - info->total_heap;
}

return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions services/inc/diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ constexpr const char* DIAG_NAME_CLOUD_DISCONNECTS = "cloud:disconn";
constexpr const char* DIAG_NAME_CLOUD_REPEATED_MESSAGES = "coap:resend";
constexpr const char* DIAG_NAME_CLOUD_UNACKNOWLEDGED_MESSAGES = "coap:unack";
constexpr const char* DIAG_NAME_CLOUD_RATE_LIMITED_EVENTS = "pub:limit";
constexpr const char* DIAG_NAME_SYSTEM_TOTAL_RAM = "sys:tram";
constexpr const char* DIAG_NAME_SYSTEM_USED_RAM = "sys:uram";

#endif

Expand Down Expand Up @@ -72,6 +74,8 @@ typedef enum diag_id {
DIAG_ID_CLOUD_REPEATED_MESSAGES = 21, // coap:resend
DIAG_ID_CLOUD_UNACKNOWLEDGED_MESSAGES = 22, // coap:unack
DIAG_ID_CLOUD_RATE_LIMITED_EVENTS = 20, // pub:throttle
DIAG_ID_SYSTEM_TOTAL_RAM = 25, // sys:tram
DIAG_ID_SYSTEM_USED_RAM = 26, // sys:uram
DIAG_ID_USER = 32768 // Base value for application-specific source IDs
} diag_id;

Expand Down
34 changes: 34 additions & 0 deletions system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,46 @@ class UptimeDiagnosticData: public AbstractIntegerDiagnosticData {
}
};

class RunTimeInfoDiagnosticData: public AbstractIntegerDiagnosticData {
public:
typedef IntType(*func_t)(const runtime_info_t&);
RunTimeInfoDiagnosticData(uint16_t id, const char* name, func_t f) :
AbstractIntegerDiagnosticData(id, name),
f_(f) {
}

virtual int get(IntType& val) override {
runtime_info_t info = {0};
info.size = sizeof(info);
if (HAL_Core_Runtime_Info(&info, nullptr) == 0) {
val = f_(info);
return SYSTEM_ERROR_NONE;
}
return SYSTEM_ERROR_UNKNOWN;
}

private:
func_t f_;
};

// Certain HAL events can be generated before app_setup_and_loop() is called. Using constructor of a
// global variable allows to register a handler for HAL events early
HALEventHandler g_halEventHandler;

UptimeDiagnosticData g_uptimeDiagData;

RunTimeInfoDiagnosticData g_totalRamDiagData(DIAG_ID_SYSTEM_TOTAL_RAM, DIAG_NAME_SYSTEM_TOTAL_RAM,
[](const runtime_info_t& info) -> RunTimeInfoDiagnosticData::IntType {
return info.total_init_heap;
}
);

RunTimeInfoDiagnosticData g_usedRamDiagData(DIAG_ID_SYSTEM_USED_RAM, DIAG_NAME_SYSTEM_USED_RAM,
[](const runtime_info_t& info) -> RunTimeInfoDiagnosticData::IntType {
return (info.total_init_heap - info.freeheap);
}
);

} // namespace

/*******************************************************************************
Expand Down