mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-06-28 19:30:35 +08:00
load_mon: get mem_usage by reading /proc/meminfo on Linux
This commit is contained in:
@@ -86,6 +86,17 @@ void LoadMon::start()
|
||||
|
||||
void LoadMon::Run()
|
||||
{
|
||||
#if defined (__PX4_LINUX)
|
||||
|
||||
if (_proc_fd == nullptr) { // init fd
|
||||
_proc_fd = fopen("/proc/meminfo", "r");
|
||||
|
||||
if (_proc_fd == nullptr) {
|
||||
PX4_ERR("Failed to open /proc/meminfo");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
perf_begin(_cycle_perf);
|
||||
|
||||
cpuload();
|
||||
@@ -100,6 +111,9 @@ void LoadMon::Run()
|
||||
|
||||
if (should_exit()) {
|
||||
ScheduleClear();
|
||||
#if defined (__PX4_LINUX)
|
||||
fclose(_proc_fd);
|
||||
#endif
|
||||
exit_and_cleanup();
|
||||
}
|
||||
|
||||
@@ -142,17 +156,74 @@ void LoadMon::cpuload()
|
||||
const float interval_idletime = total_runtime - _last_idle_time;
|
||||
#endif
|
||||
|
||||
// get ram usage
|
||||
struct mallinfo mem = mallinfo();
|
||||
float ram_usage = (float)mem.uordblks / mem.arena;
|
||||
|
||||
cpuload_s cpuload{};
|
||||
#if defined(__PX4_LINUX)
|
||||
/* following calculation is based on free(1)
|
||||
* https://gitlab.com/procps-ng/procps/-/blob/master/proc/sysinfo.c */
|
||||
char line[256];
|
||||
int32_t kb_main_total = -1;
|
||||
int32_t kb_main_free = -1;
|
||||
int32_t kb_page_cache = -1;
|
||||
int32_t kb_slab_reclaimable = -1;
|
||||
int32_t kb_main_buffers = -1;
|
||||
int parsedCount = 0;
|
||||
|
||||
if (_proc_fd != nullptr) {
|
||||
while (fgets(line, sizeof(line), _proc_fd)) {
|
||||
if (sscanf(line, "MemTotal: %d kB", &kb_main_total) == 1) {
|
||||
++parsedCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sscanf(line, "MemFree: %d kB", &kb_main_free) == 1) {
|
||||
++parsedCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sscanf(line, "Cached: %d kB", &kb_page_cache) == 1) {
|
||||
++parsedCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sscanf(line, "SReclaimable: %d kB", &kb_slab_reclaimable) == 1) {
|
||||
++parsedCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sscanf(line, "Buffers: %d kB", &kb_main_buffers) == 1) {
|
||||
++parsedCount;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
fseek(_proc_fd, 0, SEEK_END);
|
||||
|
||||
if (parsedCount == 5) {
|
||||
int32_t kb_main_cached = kb_page_cache + kb_slab_reclaimable;
|
||||
int32_t mem_used = kb_main_total - kb_main_free - kb_main_cached - kb_main_buffers;
|
||||
|
||||
if (mem_used < 0) {
|
||||
mem_used = kb_main_total - kb_main_free;
|
||||
}
|
||||
|
||||
cpuload.ram_usage = (float)mem_used / kb_main_total;
|
||||
|
||||
} else {
|
||||
PX4_ERR("Could not parse /proc/meminfo");
|
||||
cpuload.ram_usage = -1;
|
||||
}
|
||||
|
||||
} else {
|
||||
cpuload.ram_usage = -1;
|
||||
}
|
||||
|
||||
cpuload.load = interval_spent_time / interval;
|
||||
#elif defined(__PX4_NUTTX)
|
||||
// get ram usage
|
||||
struct mallinfo mem = mallinfo();
|
||||
cpuload.ram_usage = (float)mem.uordblks / mem.arena;
|
||||
cpuload.load = 1.f - interval_idletime / interval;
|
||||
#endif
|
||||
cpuload.ram_usage = ram_usage;
|
||||
cpuload.timestamp = hrt_absolute_time();
|
||||
|
||||
_cpuload_pub.publish(cpuload);
|
||||
|
||||
Reference in New Issue
Block a user