ESPHome 2026.3.3
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 <map>
8#include <cstdint>
9#include <cstring>
10#include "esphome/core/hal.h"
12#include "esphome/core/log.h"
13
14namespace esphome {
15
16class Component; // Forward declaration
17
18namespace runtime_stats {
19
20static const char *const TAG = "runtime_stats";
21
23 public:
31
32 void record_time(uint32_t duration_us) {
33 // Update period counters
34 this->period_count_++;
35 this->period_time_us_ += duration_us;
36 if (duration_us > this->period_max_time_us_)
37 this->period_max_time_us_ = duration_us;
38
39 // Update total counters (uint64_t to avoid overflow — uint32_t would overflow after ~10 hours)
40 this->total_count_++;
41 this->total_time_us_ += duration_us;
42 if (duration_us > this->total_max_time_us_)
43 this->total_max_time_us_ = duration_us;
44 }
45
47 this->period_count_ = 0;
48 this->period_time_us_ = 0;
49 this->period_max_time_us_ = 0;
50 }
51
52 // Period stats (reset each logging interval)
53 uint32_t get_period_count() const { return this->period_count_; }
56 float get_period_avg_time_us() const {
57 return this->period_count_ > 0 ? this->period_time_us_ / static_cast<float>(this->period_count_) : 0.0f;
58 }
59
60 // Total stats (persistent until reboot, uint64_t to avoid overflow)
61 uint32_t get_total_count() const { return this->total_count_; }
62 uint64_t get_total_time_us() const { return this->total_time_us_; }
64 float get_total_avg_time_us() const {
65 return this->total_count_ > 0 ? this->total_time_us_ / static_cast<float>(this->total_count_) : 0.0f;
66 }
67
68 protected:
69 // Period stats (reset each logging interval)
73
74 // Total stats (persistent until reboot)
78};
79
81 public:
83
84 void set_log_interval(uint32_t log_interval) {
85 this->log_interval_ = log_interval;
86 this->next_log_time_ = millis() + log_interval;
87 }
88 uint32_t get_log_interval() const { return this->log_interval_; }
89
91
92 // Process any pending stats printing (should be called after component loop)
93 void process_pending_stats(uint32_t current_time);
94
95 protected:
96 void log_stats_();
97
98 void reset_stats_() {
99 for (auto &it : this->component_stats_) {
100 it.second.reset_period_stats();
101 }
102 }
103
104 // Map from component to its stats
105 // We use Component* as the key since each component is unique
106 std::map<Component *, ComponentRuntimeStats> component_stats_;
109};
110
111} // namespace runtime_stats
112
113extern runtime_stats::RuntimeStatsCollector
114 *global_runtime_stats; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
115
116} // namespace esphome
117
118#endif // USE_RUNTIME_STATS
void record_component_time(Component *component, uint32_t duration_us)
void set_log_interval(uint32_t log_interval)
std::map< Component *, ComponentRuntimeStats > component_stats_
void process_pending_stats(uint32_t current_time)
const Component * component
Definition component.cpp:37
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