2016-02-26 18:03:18 -08:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
2020-07-14 17:38:32 -04:00
|
|
|
* Copyright (c) 2012-2020 PX4 Development Team. All rights reserved.
|
2016-02-26 18:03:18 -08:00
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
|
* distribution.
|
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-29 09:57:59 +02:00
|
|
|
* @file load_mon.cpp
|
2016-02-26 18:03:18 -08:00
|
|
|
*
|
|
|
|
|
* @author Jonathan Challinger <jonathan@3drobotics.com>
|
2016-04-29 14:29:31 +02:00
|
|
|
* @author Julian Oes <julian@oes.ch
|
2016-11-21 14:30:51 +01:00
|
|
|
* @author Andreas Antener <andreas@uaventure.com>
|
2016-02-26 18:03:18 -08:00
|
|
|
*/
|
|
|
|
|
|
2018-12-21 22:27:58 -08:00
|
|
|
#include <drivers/drv_hrt.h>
|
|
|
|
|
#include <lib/perf/perf_counter.h>
|
2019-10-25 10:56:32 +02:00
|
|
|
#include <px4_platform_common/px4_config.h>
|
|
|
|
|
#include <px4_platform_common/defines.h>
|
|
|
|
|
#include <px4_platform_common/module.h>
|
|
|
|
|
#include <px4_platform_common/module_params.h>
|
2019-08-27 10:57:38 +02:00
|
|
|
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
2016-02-26 18:03:18 -08:00
|
|
|
#include <systemlib/cpuload.h>
|
2019-09-02 20:41:34 -04:00
|
|
|
#include <uORB/Publication.hpp>
|
2016-02-26 18:03:18 -08:00
|
|
|
#include <uORB/topics/cpuload.h>
|
2017-02-03 18:22:24 +01:00
|
|
|
#include <uORB/topics/task_stack_info.h>
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2018-11-29 22:05:48 +00:00
|
|
|
#if defined(__PX4_NUTTX) && !defined(CONFIG_SCHED_INSTRUMENTATION)
|
|
|
|
|
# error load_mon support requires CONFIG_SCHED_INSTRUMENTATION
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-06 10:28:00 -04:00
|
|
|
// if free stack space falls below this, print a warning
|
|
|
|
|
#if defined(CONFIG_ARMV7M_STACKCHECK)
|
|
|
|
|
static constexpr unsigned STACK_LOW_WARNING_THRESHOLD = 100;
|
|
|
|
|
#else
|
|
|
|
|
static constexpr unsigned STACK_LOW_WARNING_THRESHOLD = 300;
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
static constexpr unsigned FDS_LOW_WARNING_THRESHOLD = 2; ///< if free file descriptors fall below this, print a warning
|
|
|
|
|
|
|
|
|
|
using namespace time_literals;
|
2016-04-29 14:29:31 +02:00
|
|
|
|
|
|
|
|
namespace load_mon
|
|
|
|
|
{
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2019-05-28 09:40:36 -04:00
|
|
|
class LoadMon : public ModuleBase<LoadMon>, public ModuleParams, public px4::ScheduledWorkItem
|
2016-04-29 14:29:31 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
LoadMon();
|
2019-10-27 20:59:17 -04:00
|
|
|
~LoadMon() override;
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
static int task_spawn(int argc, char *argv[]);
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
/** @see ModuleBase */
|
|
|
|
|
static int custom_command(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
return print_usage("unknown command");
|
|
|
|
|
}
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
/** @see ModuleBase */
|
|
|
|
|
static int print_usage(const char *reason = nullptr);
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2019-05-28 09:40:36 -04:00
|
|
|
void start();
|
2016-12-04 18:27:44 +01:00
|
|
|
|
2016-04-29 14:29:31 +02:00
|
|
|
private:
|
2017-05-12 09:59:55 +02:00
|
|
|
/** Do a compute and schedule the next cycle. */
|
2019-05-28 09:40:36 -04:00
|
|
|
void Run() override;
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
/** Do a calculation of the CPU load and publish it. */
|
2020-07-14 17:38:32 -04:00
|
|
|
void cpuload();
|
2016-08-15 14:53:48 +02:00
|
|
|
|
2016-11-21 14:30:51 +01:00
|
|
|
/* Calculate stack usage */
|
2020-07-14 17:38:32 -04:00
|
|
|
void stack_usage();
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2018-12-21 22:27:58 -08:00
|
|
|
int _stack_task_index{0};
|
2019-09-02 20:41:34 -04:00
|
|
|
uORB::PublicationQueued<task_stack_info_s> _task_stack_info_pub{ORB_ID(task_stack_info)};
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2018-12-21 22:27:58 -08:00
|
|
|
DEFINE_PARAMETERS(
|
2019-03-25 11:19:09 +01:00
|
|
|
(ParamBool<px4::params::SYS_STCK_EN>) _param_sys_stck_en
|
2018-12-21 22:27:58 -08:00
|
|
|
)
|
|
|
|
|
|
2019-09-02 20:41:34 -04:00
|
|
|
uORB::Publication<cpuload_s> _cpuload_pub{ORB_ID(cpuload)};
|
2018-12-21 22:27:58 -08:00
|
|
|
|
|
|
|
|
hrt_abstime _last_idle_time{0};
|
|
|
|
|
hrt_abstime _last_idle_time_sample{0};
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
perf_counter_t _cycle_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")};
|
2016-04-29 14:29:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LoadMon::LoadMon() :
|
2018-12-21 22:27:58 -08:00
|
|
|
ModuleParams(nullptr),
|
2020-07-14 17:38:32 -04:00
|
|
|
ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::lp_default)
|
2016-12-04 19:05:57 +01:00
|
|
|
{
|
|
|
|
|
}
|
2016-04-29 14:29:31 +02:00
|
|
|
|
|
|
|
|
LoadMon::~LoadMon()
|
2016-02-26 18:03:18 -08:00
|
|
|
{
|
2019-05-28 09:40:36 -04:00
|
|
|
ScheduleClear();
|
|
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
perf_free(_cycle_perf);
|
2016-04-29 14:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
int LoadMon::task_spawn(int argc, char *argv[])
|
2016-04-29 14:29:31 +02:00
|
|
|
{
|
2017-05-12 09:59:55 +02:00
|
|
|
LoadMon *obj = new LoadMon();
|
|
|
|
|
|
|
|
|
|
if (!obj) {
|
|
|
|
|
PX4_ERR("alloc failed");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 07:19:22 +01:00
|
|
|
_object.store(obj);
|
2017-05-12 09:59:55 +02:00
|
|
|
_task_id = task_id_is_work_queue;
|
2019-05-28 09:40:36 -04:00
|
|
|
|
|
|
|
|
/* Schedule a cycle to start things. */
|
|
|
|
|
obj->start();
|
|
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
return 0;
|
2016-04-29 14:29:31 +02:00
|
|
|
}
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
void LoadMon::start()
|
2016-04-29 14:29:31 +02:00
|
|
|
{
|
2020-07-14 17:38:32 -04:00
|
|
|
ScheduleOnInterval(300_ms);
|
2016-04-29 14:29:31 +02:00
|
|
|
}
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2019-05-28 09:40:36 -04:00
|
|
|
void LoadMon::Run()
|
2016-04-29 14:29:31 +02:00
|
|
|
{
|
2020-07-14 17:38:32 -04:00
|
|
|
perf_begin(_cycle_perf);
|
2018-12-21 22:27:58 -08:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
cpuload();
|
2018-12-21 22:27:58 -08:00
|
|
|
|
2019-03-25 11:19:09 +01:00
|
|
|
if (_param_sys_stck_en.get()) {
|
2020-07-14 17:38:32 -04:00
|
|
|
stack_usage();
|
2018-12-21 22:27:58 -08:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 09:40:36 -04:00
|
|
|
if (should_exit()) {
|
|
|
|
|
ScheduleClear();
|
2017-05-12 09:59:55 +02:00
|
|
|
exit_and_cleanup();
|
2016-02-26 18:03:18 -08:00
|
|
|
}
|
2020-07-14 17:38:32 -04:00
|
|
|
|
|
|
|
|
perf_end(_cycle_perf);
|
2016-04-29 14:29:31 +02:00
|
|
|
}
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
void LoadMon::cpuload()
|
2016-04-29 14:29:31 +02:00
|
|
|
{
|
|
|
|
|
if (_last_idle_time == 0) {
|
2020-07-14 17:38:32 -04:00
|
|
|
// Just get the time in the first iteration */
|
2016-04-29 14:29:31 +02:00
|
|
|
_last_idle_time = system_load.tasks[0].total_runtime;
|
2020-07-14 17:38:32 -04:00
|
|
|
_last_idle_time_sample = hrt_absolute_time();
|
2016-04-29 14:29:31 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
irqstate_t irqstate = enter_critical_section();
|
|
|
|
|
const hrt_abstime now = hrt_absolute_time();
|
2018-12-21 22:27:58 -08:00
|
|
|
const hrt_abstime total_runtime = system_load.tasks[0].total_runtime;
|
2020-07-14 17:38:32 -04:00
|
|
|
leave_critical_section(irqstate);
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
// compute system load
|
|
|
|
|
const float interval = now - _last_idle_time_sample;
|
|
|
|
|
const float interval_idletime = total_runtime - _last_idle_time;
|
|
|
|
|
|
|
|
|
|
// get ram usage
|
|
|
|
|
struct mallinfo mem = mallinfo();
|
|
|
|
|
float ram_usage = (float)mem.uordblks / mem.arena;
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2019-09-02 20:41:34 -04:00
|
|
|
cpuload_s cpuload{};
|
2020-07-14 17:38:32 -04:00
|
|
|
cpuload.load = 1.f - interval_idletime / interval;
|
|
|
|
|
cpuload.ram_usage = ram_usage;
|
2018-12-21 22:27:58 -08:00
|
|
|
cpuload.timestamp = hrt_absolute_time();
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2019-09-02 20:41:34 -04:00
|
|
|
_cpuload_pub.publish(cpuload);
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
// store for next iteration
|
|
|
|
|
_last_idle_time = total_runtime;
|
|
|
|
|
_last_idle_time_sample = now;
|
2016-08-15 14:53:48 +02:00
|
|
|
}
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
void LoadMon::stack_usage()
|
2016-11-21 14:30:51 +01:00
|
|
|
{
|
2020-07-14 17:38:32 -04:00
|
|
|
unsigned stack_free = 0;
|
|
|
|
|
unsigned fds_free = FDS_LOW_WARNING_THRESHOLD + 1;
|
2016-12-04 18:04:35 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
bool checked_task = false;
|
2017-02-03 18:22:24 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
task_stack_info_s task_stack_info{};
|
|
|
|
|
static_assert(sizeof(task_stack_info.task_name) == CONFIG_TASK_NAME_SIZE,
|
|
|
|
|
"task_stack_info.task_name must match NuttX CONFIG_TASK_NAME_SIZE");
|
2016-12-07 19:58:13 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
sched_lock();
|
2016-12-04 18:04:35 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
if (system_load.tasks[_stack_task_index].valid && (system_load.tasks[_stack_task_index].tcb->pid > 0)) {
|
2018-12-21 22:27:58 -08:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
stack_free = up_check_tcbstack_remain(system_load.tasks[_stack_task_index].tcb);
|
2016-12-07 19:58:13 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
strncpy((char *)task_stack_info.task_name, system_load.tasks[_stack_task_index].tcb->name, CONFIG_TASK_NAME_SIZE - 1);
|
|
|
|
|
task_stack_info.task_name[CONFIG_TASK_NAME_SIZE - 1] = '\0';
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
checked_task = true;
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2018-01-01 16:36:35 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2020-07-14 17:38:32 -04:00
|
|
|
FAR struct task_group_s *group = system_load.tasks[_stack_task_index].tcb->group;
|
2018-01-01 16:36:35 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
unsigned tcb_num_used_fds = 0;
|
2018-01-01 16:36:35 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
if (group) {
|
|
|
|
|
for (int fd_index = 0; fd_index < CONFIG_NFILE_DESCRIPTORS; ++fd_index) {
|
|
|
|
|
if (group->tg_filelist.fl_files[fd_index].f_inode) {
|
|
|
|
|
++tcb_num_used_fds;
|
2018-01-01 16:36:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
fds_free = CONFIG_NFILE_DESCRIPTORS - tcb_num_used_fds;
|
2016-12-07 19:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
#endif // CONFIG_NFILE_DESCRIPTORS
|
|
|
|
|
}
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
sched_unlock();
|
2016-11-21 14:30:51 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
if (checked_task) {
|
|
|
|
|
task_stack_info.stack_free = stack_free;
|
|
|
|
|
task_stack_info.timestamp = hrt_absolute_time();
|
2017-02-03 18:22:24 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
_task_stack_info_pub.publish(task_stack_info);
|
2016-12-04 18:04:35 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
// Found task low on stack, report and exit. Continue here in next cycle.
|
|
|
|
|
if (stack_free < STACK_LOW_WARNING_THRESHOLD) {
|
|
|
|
|
PX4_WARN("%s low on stack! (%i bytes left)", task_stack_info.task_name, stack_free);
|
|
|
|
|
}
|
2018-01-01 16:36:35 +01:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
// Found task low on file descriptors, report and exit. Continue here in next cycle.
|
|
|
|
|
if (fds_free < FDS_LOW_WARNING_THRESHOLD) {
|
|
|
|
|
PX4_WARN("%s low on FDs! (%i FDs left)", task_stack_info.task_name, fds_free);
|
2016-11-21 14:30:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
// Continue after last checked task next cycle
|
|
|
|
|
_stack_task_index = (_stack_task_index + 1) % CONFIG_MAX_TASKS;
|
2016-11-21 14:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
int LoadMon::print_usage(const char *reason)
|
2016-02-26 18:03:18 -08:00
|
|
|
{
|
|
|
|
|
if (reason) {
|
2017-05-12 09:59:55 +02:00
|
|
|
PX4_ERR("%s\n", reason);
|
2016-02-26 18:03:18 -08:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
PRINT_MODULE_DESCRIPTION(
|
|
|
|
|
R"DESCR_STR(
|
|
|
|
|
### Description
|
2020-07-14 17:38:32 -04:00
|
|
|
Background process running periodically with 1 Hz on the low priority work queue to calculate the CPU load and RAM
|
2017-05-12 09:59:55 +02:00
|
|
|
usage and publish the `cpuload` topic.
|
2016-02-26 18:03:18 -08:00
|
|
|
|
2017-05-12 09:59:55 +02:00
|
|
|
On NuttX it also checks the stack usage of each process and if it falls below 300 bytes, a warning is output,
|
|
|
|
|
which will also appear in the log file.
|
|
|
|
|
)DESCR_STR");
|
|
|
|
|
|
|
|
|
|
PRINT_MODULE_USAGE_NAME("load_mon", "system");
|
|
|
|
|
PRINT_MODULE_USAGE_COMMAND_DESCR("start", "Start the background task");
|
|
|
|
|
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-04-29 14:29:31 +02:00
|
|
|
|
2020-07-14 17:38:32 -04:00
|
|
|
extern "C" __EXPORT int load_mon_main(int argc, char *argv[])
|
2016-02-26 18:03:18 -08:00
|
|
|
{
|
2017-05-12 09:59:55 +02:00
|
|
|
return LoadMon::main(argc, argv);
|
2016-02-26 18:03:18 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 14:29:31 +02:00
|
|
|
} // namespace load_mon
|