2024-03-07 03:52:58 -04:00
|
|
|
#include "AP_Scheduler_config.h"
|
|
|
|
|
|
|
|
#if AP_SCHEDULER_ENABLED
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
#include "PerfInfo.h"
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2017-11-13 04:25:54 -04:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2020-09-04 15:55:45 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2019-09-17 05:13:28 -03:00
|
|
|
#include "AP_Scheduler.h"
|
2015-05-29 23:12:49 -03:00
|
|
|
|
2018-02-09 21:20:45 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2012-11-18 12:16:07 -04:00
|
|
|
//
|
|
|
|
// high level performance monitoring
|
|
|
|
//
|
|
|
|
// we measure the main loop time
|
|
|
|
//
|
|
|
|
|
2024-08-31 17:10:13 -03:00
|
|
|
// loops over time by this amount or more won't be counted in filtered loop time (and thus loop rate)
|
|
|
|
#ifndef AP_SCHEDULER_OVERTIME_MARGIN_US
|
|
|
|
#define AP_SCHEDULER_OVERTIME_MARGIN_US 10000UL
|
|
|
|
#endif
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// reset - reset all records of loop time to zero
|
2017-11-15 23:20:04 -04:00
|
|
|
void AP::PerfInfo::reset()
|
2012-11-18 12:16:07 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
loop_count = 0;
|
|
|
|
max_time = 0;
|
|
|
|
min_time = 0;
|
|
|
|
long_running = 0;
|
|
|
|
sigma_time = 0;
|
|
|
|
sigmasquared_time = 0;
|
2020-09-30 15:05:08 -03:00
|
|
|
if (_task_info != nullptr) {
|
2022-04-01 16:57:03 -03:00
|
|
|
memset(_task_info, 0, (_num_tasks) * sizeof(TaskInfo));
|
2020-09-30 15:05:08 -03:00
|
|
|
}
|
2012-11-18 12:16:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// ignore_loop - ignore this loop from performance measurements (used to reduce false positive when arming)
|
|
|
|
void AP::PerfInfo::ignore_this_loop()
|
2015-02-09 10:07:18 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
ignore_loop = true;
|
2015-02-09 10:07:18 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 15:55:45 -03:00
|
|
|
// allocate the array of task statistics for use by @SYS/tasks.txt
|
|
|
|
void AP::PerfInfo::allocate_task_info(uint8_t num_tasks)
|
|
|
|
{
|
2024-05-26 22:24:14 -03:00
|
|
|
_task_info = NEW_NOTHROW TaskInfo[num_tasks];
|
2020-09-04 15:55:45 -03:00
|
|
|
if (_task_info == nullptr) {
|
2022-03-21 06:39:00 -03:00
|
|
|
DEV_PRINTF("Unable to allocate scheduler TaskInfo\n");
|
2020-09-04 15:55:45 -03:00
|
|
|
_num_tasks = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_num_tasks = num_tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP::PerfInfo::free_task_info()
|
|
|
|
{
|
|
|
|
delete[] _task_info;
|
|
|
|
_task_info = nullptr;
|
|
|
|
_num_tasks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called after each run of a task to update its statistics based on measurements taken by the scheduler
|
|
|
|
void AP::PerfInfo::update_task_info(uint8_t task_index, uint16_t task_time_us, bool overrun)
|
|
|
|
{
|
|
|
|
if (_task_info == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:57:03 -03:00
|
|
|
if (task_index >= _num_tasks) {
|
2020-09-04 15:55:45 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TaskInfo& ti = _task_info[task_index];
|
2022-04-01 16:57:03 -03:00
|
|
|
ti.update(task_time_us, overrun);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP::PerfInfo::TaskInfo::update(uint16_t task_time_us, bool overrun)
|
|
|
|
{
|
|
|
|
max_time_us = MAX(max_time_us, task_time_us);
|
|
|
|
if (min_time_us == 0) {
|
|
|
|
min_time_us = task_time_us;
|
2020-09-04 15:55:45 -03:00
|
|
|
} else {
|
2022-04-01 16:57:03 -03:00
|
|
|
min_time_us = MIN(min_time_us, task_time_us);
|
2020-09-04 15:55:45 -03:00
|
|
|
}
|
2022-04-01 16:57:03 -03:00
|
|
|
elapsed_time_us += task_time_us;
|
|
|
|
tick_count++;
|
2020-09-04 15:55:45 -03:00
|
|
|
if (overrun) {
|
2022-04-01 16:57:03 -03:00
|
|
|
overrun_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP::PerfInfo::TaskInfo::print(const char* task_name, uint32_t total_time, ExpandingString& str) const
|
|
|
|
{
|
|
|
|
uint16_t avg = 0;
|
|
|
|
float pct = 0.0f;
|
|
|
|
if (tick_count > 0) {
|
|
|
|
pct = elapsed_time_us * 100.0f / total_time;
|
|
|
|
avg = MIN(uint16_t(elapsed_time_us / tick_count), 9999);
|
2020-09-04 15:55:45 -03:00
|
|
|
}
|
2023-06-15 06:55:49 -03:00
|
|
|
#if AP_SCHEDULER_EXTENDED_TASKINFO_ENABLED
|
2022-04-01 16:57:03 -03:00
|
|
|
const char* fmt = "%-32.32s MIN=%4u MAX=%4u AVG=%4u OVR=%3u SLP=%3u, TOT=%4.1f%%\n";
|
2023-06-15 06:55:49 -03:00
|
|
|
#else
|
|
|
|
const char* fmt = "%-16.16s MIN=%4u MAX=%4u AVG=%4u OVR=%3u SLP=%3u, TOT=%4.1f%%\n";
|
2022-04-01 16:57:03 -03:00
|
|
|
#endif
|
|
|
|
str.printf(fmt, task_name,
|
|
|
|
unsigned(MIN(min_time_us, 9999)), unsigned(MIN(max_time_us, 9999)), unsigned(avg),
|
|
|
|
unsigned(MIN(overrun_count, 999)), unsigned(MIN(slip_count, 999)), pct);
|
2020-09-04 15:55:45 -03:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// check_loop_time - check latest loop time vs min, max and overtime threshold
|
|
|
|
void AP::PerfInfo::check_loop_time(uint32_t time_in_micros)
|
2012-11-18 12:16:07 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
loop_count++;
|
2015-02-09 10:07:18 -04:00
|
|
|
|
|
|
|
// exit if this loop should be ignored
|
2017-11-12 21:47:22 -04:00
|
|
|
if (ignore_loop) {
|
|
|
|
ignore_loop = false;
|
2015-02-09 10:07:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
if( time_in_micros > max_time) {
|
|
|
|
max_time = time_in_micros;
|
2012-11-18 12:16:07 -04:00
|
|
|
}
|
2017-11-12 21:47:22 -04:00
|
|
|
if( min_time == 0 || time_in_micros < min_time) {
|
|
|
|
min_time = time_in_micros;
|
2015-02-15 19:02:53 -04:00
|
|
|
}
|
2018-02-09 20:48:20 -04:00
|
|
|
if (time_in_micros > overtime_threshold_micros) {
|
2017-11-12 21:47:22 -04:00
|
|
|
long_running++;
|
2012-11-18 12:16:07 -04:00
|
|
|
}
|
2017-11-12 21:47:22 -04:00
|
|
|
sigma_time += time_in_micros;
|
|
|
|
sigmasquared_time += time_in_micros * time_in_micros;
|
2018-02-09 20:48:20 -04:00
|
|
|
|
2018-02-09 21:20:45 -04:00
|
|
|
/* we keep a filtered loop time for use as G_Dt which is the
|
|
|
|
predicted time for the next loop. We remove really excessive
|
|
|
|
times from this calculation so as not to throw it off too far
|
|
|
|
in case we get a single long loop
|
|
|
|
|
|
|
|
Note that the time we use here is the time between calls to
|
|
|
|
check_loop_time() not the time from loop start to loop
|
|
|
|
end. This is because we are using the time for time between
|
|
|
|
calls to controllers, which has nothing to do with cpu speed.
|
|
|
|
*/
|
|
|
|
const uint32_t now = AP_HAL::micros();
|
|
|
|
const uint32_t loop_time_us = now - last_check_us;
|
|
|
|
last_check_us = now;
|
2024-08-31 17:10:13 -03:00
|
|
|
if (loop_time_us < overtime_threshold_micros + AP_SCHEDULER_OVERTIME_MARGIN_US) {
|
2018-02-12 16:51:37 -04:00
|
|
|
filtered_loop_time = 0.99f * filtered_loop_time + 0.01f * loop_time_us * 1.0e-6f;
|
2024-08-31 17:10:13 -03:00
|
|
|
} else {
|
|
|
|
// esp32 is most likely to regularly trigger long loops, might be
|
|
|
|
// helpful for bringup of other boards too
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_ESP32
|
|
|
|
#ifdef SCHEDDEBUG
|
|
|
|
DEV_PRINTF("way overtime: %dus\n", loop_time_us);
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-02-09 21:20:45 -04:00
|
|
|
}
|
2012-11-18 12:16:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// get_num_loops: return number of loops used for recording performance
|
|
|
|
uint16_t AP::PerfInfo::get_num_loops() const
|
2012-11-18 12:16:07 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
return loop_count;
|
2012-11-18 12:16:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// get_max_time - return maximum loop time (in microseconds)
|
|
|
|
uint32_t AP::PerfInfo::get_max_time() const
|
2012-11-18 12:16:07 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
return max_time;
|
2012-11-18 12:16:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// get_min_time - return minumum loop time (in microseconds)
|
|
|
|
uint32_t AP::PerfInfo::get_min_time() const
|
2015-02-15 19:02:53 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
return min_time;
|
2015-02-15 19:02:53 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// get_num_long_running - get number of long running loops
|
|
|
|
uint16_t AP::PerfInfo::get_num_long_running() const
|
2012-11-18 12:16:07 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
return long_running;
|
2014-03-08 02:16:28 -04:00
|
|
|
}
|
2016-04-21 03:42:25 -03:00
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// get_avg_time - return average loop time (in microseconds)
|
|
|
|
uint32_t AP::PerfInfo::get_avg_time() const
|
2015-11-04 01:42:05 -04:00
|
|
|
{
|
2017-11-12 21:47:22 -04:00
|
|
|
return (sigma_time / loop_count);
|
2015-11-04 01:42:05 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 21:47:22 -04:00
|
|
|
// get_stddev_time - return stddev of average loop time (in us)
|
|
|
|
uint32_t AP::PerfInfo::get_stddev_time() const
|
2015-11-04 01:42:05 -04:00
|
|
|
{
|
2018-05-03 23:12:29 -03:00
|
|
|
return sqrtf((sigmasquared_time - (sigma_time*sigma_time)/loop_count) / loop_count);
|
2015-11-04 01:42:05 -04:00
|
|
|
}
|
2017-11-13 04:25:54 -04:00
|
|
|
|
2018-02-09 20:48:20 -04:00
|
|
|
// get_filtered_time - return low pass filtered loop time in seconds
|
|
|
|
float AP::PerfInfo::get_filtered_time() const
|
|
|
|
{
|
|
|
|
return filtered_loop_time;
|
|
|
|
}
|
|
|
|
|
2022-11-29 23:27:06 -04:00
|
|
|
// return low pass filtered loop rate in hz
|
|
|
|
float AP::PerfInfo::get_filtered_loop_rate_hz() const
|
|
|
|
{
|
|
|
|
const float filt_time_s = get_filtered_time();
|
|
|
|
if (filt_time_s <= 0) {
|
|
|
|
return loop_rate_hz;
|
|
|
|
}
|
|
|
|
return 1.0 / filt_time_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-01 12:26:34 -04:00
|
|
|
void AP::PerfInfo::update_logging() const
|
2017-11-13 04:25:54 -04:00
|
|
|
{
|
2023-09-02 02:21:35 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO,
|
2019-09-17 05:13:28 -03:00
|
|
|
"PERF: %u/%u [%lu:%lu] F=%uHz sd=%lu Ex=%lu",
|
2017-11-13 04:25:54 -04:00
|
|
|
(unsigned)get_num_long_running(),
|
|
|
|
(unsigned)get_num_loops(),
|
|
|
|
(unsigned long)get_max_time(),
|
|
|
|
(unsigned long)get_min_time(),
|
2022-11-29 23:27:06 -04:00
|
|
|
(unsigned)(0.5+get_filtered_loop_rate_hz()),
|
2019-09-17 05:13:28 -03:00
|
|
|
(unsigned long)get_stddev_time(),
|
|
|
|
(unsigned long)AP::scheduler().get_extra_loop_us());
|
2017-11-13 04:25:54 -04:00
|
|
|
}
|
2018-02-09 20:48:20 -04:00
|
|
|
|
|
|
|
void AP::PerfInfo::set_loop_rate(uint16_t rate_hz)
|
|
|
|
{
|
|
|
|
// allow a 20% overrun before we consider a loop "slow":
|
|
|
|
overtime_threshold_micros = 1000000/rate_hz * 1.2f;
|
|
|
|
|
|
|
|
if (loop_rate_hz != rate_hz) {
|
|
|
|
loop_rate_hz = rate_hz;
|
2018-02-12 16:51:37 -04:00
|
|
|
filtered_loop_time = 1.0f / rate_hz;
|
2018-02-09 20:48:20 -04:00
|
|
|
}
|
|
|
|
}
|
2024-03-07 03:52:58 -04:00
|
|
|
|
|
|
|
#endif // AP_SCHEDULER_ENABLED
|