load_mon: get mem_usage by reading /proc/meminfo on Linux

This commit is contained in:
SalimTerryLi
2020-10-28 06:49:25 +08:00
committed by GitHub
parent 91da194bd7
commit 05f1efd1a4
7 changed files with 84 additions and 10 deletions
+1
View File
@@ -216,6 +216,7 @@ if ! replay tryapplyparams
then
simulator start -c $simulator_tcp_port
fi
load_mon start
battery_simulator start
tone_alarm start
rc_update start
+1 -1
View File
@@ -36,7 +36,7 @@ px4_add_board(
fw_pos_control_l1
land_detector
landing_target_estimator
#load_mon
load_mon
local_position_estimator
logger
mavlink
+1 -1
View File
@@ -36,7 +36,7 @@ px4_add_board(
fw_pos_control_l1
land_detector
landing_target_estimator
#load_mon
load_mon
local_position_estimator
logger
mavlink
+1 -1
View File
@@ -36,7 +36,7 @@ px4_add_board(
fw_pos_control_l1
land_detector
landing_target_estimator
#load_mon
load_mon
local_position_estimator
logger
mavlink
+1 -1
View File
@@ -35,7 +35,7 @@ px4_add_board(
fw_pos_control_l1
land_detector
landing_target_estimator
#load_mon
load_mon
local_position_estimator
logger
mavlink
+76 -5
View File
@@ -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);
+3 -1
View File
@@ -33,7 +33,9 @@
#pragma once
#if defined(__PX4_NUTTX)
#include <malloc.h>
#endif
#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>
#include <px4_platform_common/px4_config.h>
@@ -48,7 +50,6 @@
#if defined(__PX4_LINUX)
#include <sys/times.h>
#include <malloc.h>
#endif
namespace load_mon
@@ -92,6 +93,7 @@ private:
uORB::Publication<cpuload_s> _cpuload_pub {ORB_ID(cpuload)};
#if defined(__PX4_LINUX)
FILE *_proc_fd = nullptr;
/* calculate usage directly from clock ticks on Linux */
clock_t _last_total_time_stamp{};
clock_t _last_spent_time_stamp{};