mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-31 10:26:52 +08:00
load_mon: get mem_usage by reading /proc/meminfo on Linux
This commit is contained in:
@@ -216,6 +216,7 @@ if ! replay tryapplyparams
|
|||||||
then
|
then
|
||||||
simulator start -c $simulator_tcp_port
|
simulator start -c $simulator_tcp_port
|
||||||
fi
|
fi
|
||||||
|
load_mon start
|
||||||
battery_simulator start
|
battery_simulator start
|
||||||
tone_alarm start
|
tone_alarm start
|
||||||
rc_update start
|
rc_update start
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ px4_add_board(
|
|||||||
fw_pos_control_l1
|
fw_pos_control_l1
|
||||||
land_detector
|
land_detector
|
||||||
landing_target_estimator
|
landing_target_estimator
|
||||||
#load_mon
|
load_mon
|
||||||
local_position_estimator
|
local_position_estimator
|
||||||
logger
|
logger
|
||||||
mavlink
|
mavlink
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ px4_add_board(
|
|||||||
fw_pos_control_l1
|
fw_pos_control_l1
|
||||||
land_detector
|
land_detector
|
||||||
landing_target_estimator
|
landing_target_estimator
|
||||||
#load_mon
|
load_mon
|
||||||
local_position_estimator
|
local_position_estimator
|
||||||
logger
|
logger
|
||||||
mavlink
|
mavlink
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ px4_add_board(
|
|||||||
fw_pos_control_l1
|
fw_pos_control_l1
|
||||||
land_detector
|
land_detector
|
||||||
landing_target_estimator
|
landing_target_estimator
|
||||||
#load_mon
|
load_mon
|
||||||
local_position_estimator
|
local_position_estimator
|
||||||
logger
|
logger
|
||||||
mavlink
|
mavlink
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ px4_add_board(
|
|||||||
fw_pos_control_l1
|
fw_pos_control_l1
|
||||||
land_detector
|
land_detector
|
||||||
landing_target_estimator
|
landing_target_estimator
|
||||||
#load_mon
|
load_mon
|
||||||
local_position_estimator
|
local_position_estimator
|
||||||
logger
|
logger
|
||||||
mavlink
|
mavlink
|
||||||
|
|||||||
@@ -86,6 +86,17 @@ void LoadMon::start()
|
|||||||
|
|
||||||
void LoadMon::Run()
|
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);
|
perf_begin(_cycle_perf);
|
||||||
|
|
||||||
cpuload();
|
cpuload();
|
||||||
@@ -100,6 +111,9 @@ void LoadMon::Run()
|
|||||||
|
|
||||||
if (should_exit()) {
|
if (should_exit()) {
|
||||||
ScheduleClear();
|
ScheduleClear();
|
||||||
|
#if defined (__PX4_LINUX)
|
||||||
|
fclose(_proc_fd);
|
||||||
|
#endif
|
||||||
exit_and_cleanup();
|
exit_and_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,17 +156,74 @@ void LoadMon::cpuload()
|
|||||||
const float interval_idletime = total_runtime - _last_idle_time;
|
const float interval_idletime = total_runtime - _last_idle_time;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// get ram usage
|
|
||||||
struct mallinfo mem = mallinfo();
|
|
||||||
float ram_usage = (float)mem.uordblks / mem.arena;
|
|
||||||
|
|
||||||
cpuload_s cpuload{};
|
cpuload_s cpuload{};
|
||||||
#if defined(__PX4_LINUX)
|
#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;
|
cpuload.load = interval_spent_time / interval;
|
||||||
#elif defined(__PX4_NUTTX)
|
#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;
|
cpuload.load = 1.f - interval_idletime / interval;
|
||||||
#endif
|
#endif
|
||||||
cpuload.ram_usage = ram_usage;
|
|
||||||
cpuload.timestamp = hrt_absolute_time();
|
cpuload.timestamp = hrt_absolute_time();
|
||||||
|
|
||||||
_cpuload_pub.publish(cpuload);
|
_cpuload_pub.publish(cpuload);
|
||||||
|
|||||||
@@ -33,7 +33,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(__PX4_NUTTX)
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
#endif
|
||||||
#include <drivers/drv_hrt.h>
|
#include <drivers/drv_hrt.h>
|
||||||
#include <lib/perf/perf_counter.h>
|
#include <lib/perf/perf_counter.h>
|
||||||
#include <px4_platform_common/px4_config.h>
|
#include <px4_platform_common/px4_config.h>
|
||||||
@@ -48,7 +50,6 @@
|
|||||||
|
|
||||||
#if defined(__PX4_LINUX)
|
#if defined(__PX4_LINUX)
|
||||||
#include <sys/times.h>
|
#include <sys/times.h>
|
||||||
#include <malloc.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace load_mon
|
namespace load_mon
|
||||||
@@ -92,6 +93,7 @@ private:
|
|||||||
uORB::Publication<cpuload_s> _cpuload_pub {ORB_ID(cpuload)};
|
uORB::Publication<cpuload_s> _cpuload_pub {ORB_ID(cpuload)};
|
||||||
|
|
||||||
#if defined(__PX4_LINUX)
|
#if defined(__PX4_LINUX)
|
||||||
|
FILE *_proc_fd = nullptr;
|
||||||
/* calculate usage directly from clock ticks on Linux */
|
/* calculate usage directly from clock ticks on Linux */
|
||||||
clock_t _last_total_time_stamp{};
|
clock_t _last_total_time_stamp{};
|
||||||
clock_t _last_spent_time_stamp{};
|
clock_t _last_spent_time_stamp{};
|
||||||
|
|||||||
Reference in New Issue
Block a user