ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
runtime_stats.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_RUNTIME_STATS
6
7#include <cstdint>
8#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
10
11namespace esphome {
12
13class Component; // Forward declaration
14
15namespace runtime_stats {
16
17static const char *const TAG = "runtime_stats";
18
20 public:
22
23 void set_log_interval(uint32_t log_interval) {
24 this->log_interval_ = log_interval;
25 this->next_log_time_ = millis() + log_interval;
26 }
27 uint32_t get_log_interval() const { return this->log_interval_; }
28
29 // Process any pending stats printing (should be called after component loop)
30 void process_pending_stats(uint32_t current_time);
31
32 // Record the wall time of one main loop iteration excluding the yield/sleep.
33 // Called once per loop from Application::loop().
34 // active_us = total time between loop start and just before yield.
35 // before_us = time spent in before_loop_tasks_ (scheduler + ISR enable_loop).
36 // tail_us = time spent in after_loop_tasks_ + the trailing record/stats prefix.
37 // Residual overhead at log time = active − Σ(component) − before − tail,
38 // which captures per-iteration inter-component bookkeeping (set_current_component,
39 // WarnIfComponentBlockingGuard construction/destruction, feed_wdt_with_time calls,
40 // the for-loop itself).
41 void record_loop_active(uint32_t active_us, uint32_t before_us, uint32_t tail_us) {
43 this->period_active_time_us_ += active_us;
44 if (active_us > this->period_active_max_us_)
45 this->period_active_max_us_ = active_us;
46 this->total_active_count_++;
47 this->total_active_time_us_ += active_us;
48 if (active_us > this->total_active_max_us_)
49 this->total_active_max_us_ = active_us;
50
51 this->period_before_time_us_ += before_us;
52 this->total_before_time_us_ += before_us;
53 this->period_tail_time_us_ += tail_us;
54 this->total_tail_time_us_ += tail_us;
55 }
56
57 protected:
58 void log_stats_();
59 // Static comparators — member functions have friend access, lambdas do not
60 static bool compare_period_time(Component *a, Component *b);
61 static bool compare_total_time(Component *a, Component *b);
62
65
66 // Main loop active-time stats (wall time per iteration, excluding yield/sleep).
67 // Counters are uint64_t — at sub-millisecond loop times a uint32_t can wrap in
68 // a few weeks of uptime, which is well within ESPHome device lifetimes.
75
76 // Split of overhead sections — accumulated per iteration.
81};
82
83} // namespace runtime_stats
84
85extern runtime_stats::RuntimeStatsCollector
86 *global_runtime_stats; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
87
88} // namespace esphome
89
90#endif // USE_RUNTIME_STATS
void set_log_interval(uint32_t log_interval)
static bool compare_total_time(Component *a, Component *b)
static bool compare_period_time(Component *a, Component *b)
void process_pending_stats(uint32_t current_time)
void record_loop_active(uint32_t active_us, uint32_t before_us, uint32_t tail_us)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
runtime_stats::RuntimeStatsCollector * global_runtime_stats
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t