Line data Source code
1 : #pragma once 2 : 3 : #include "AP_Scheduler_config.h" 4 : 5 : #if AP_SCHEDULER_ENABLED 6 : 7 : #include <stdint.h> 8 : #include <AP_Common/ExpandingString.h> 9 : 10 : namespace AP { 11 : 12 : class PerfInfo { 13 : public: 14 2098 : PerfInfo() {} 15 : 16 : // per-task timing information 17 : struct TaskInfo { 18 : uint16_t min_time_us; 19 : uint16_t max_time_us; 20 : uint32_t elapsed_time_us; 21 : uint32_t tick_count; 22 : uint16_t slip_count; 23 : uint16_t overrun_count; 24 : 25 : void update(uint16_t task_time_us, bool overrun); 26 : void print(const char* task_name, uint32_t total_time, ExpandingString& str) const; 27 : }; 28 : 29 : /* Do not allow copies */ 30 : CLASS_NO_COPY(PerfInfo); 31 : 32 : void reset(); 33 : void ignore_this_loop(); 34 : void check_loop_time(uint32_t time_in_micros); 35 : uint16_t get_num_loops() const; 36 : uint32_t get_max_time() const; 37 : uint32_t get_min_time() const; 38 : uint16_t get_num_long_running() const; 39 : uint32_t get_avg_time() const; 40 : uint32_t get_stddev_time() const; 41 : float get_filtered_time() const; 42 : float get_filtered_loop_rate_hz() const; 43 : void set_loop_rate(uint16_t rate_hz); 44 : 45 : void update_logging() const; 46 : 47 : // allocate the array of task statistics for use by @SYS/tasks.txt 48 : void allocate_task_info(uint8_t num_tasks); 49 : void free_task_info(); 50 : // whether or not we have task info allocated 51 19414 : bool has_task_info() { return _task_info != nullptr; } 52 : // return a task info 53 344 : const TaskInfo* get_task_info(uint8_t task_index) const { 54 344 : return (_task_info && task_index < _num_tasks) ? &_task_info[task_index] : nullptr; 55 : } 56 : // called after each run of a task to update its statistics based on measurements taken by the scheduler 57 : void update_task_info(uint8_t task_index, uint16_t task_time_us, bool overrun); 58 : // record that a task slipped 59 2158 : void task_slipped(uint8_t task_index) { 60 2158 : if (_task_info && task_index < _num_tasks) { 61 0 : _task_info[task_index].overrun_count++; 62 : } 63 2158 : } 64 : 65 : private: 66 : uint16_t loop_rate_hz; 67 : uint16_t overtime_threshold_micros; 68 : uint16_t loop_count; 69 : uint32_t max_time; // in microseconds 70 : uint32_t min_time; // in microseconds 71 : uint64_t sigma_time; 72 : uint64_t sigmasquared_time; 73 : uint16_t long_running; 74 : uint32_t last_check_us; 75 : float filtered_loop_time; 76 : bool ignore_loop; 77 : // performance monitoring 78 : uint8_t _num_tasks; 79 : TaskInfo* _task_info; 80 : }; 81 : 82 : }; 83 : 84 : #endif // AP_SCHEDULER_ENABLED