ESPHome 2026.2.1
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>
11#include "esphome/core/log.h"
12
13namespace esphome {
14
15class Component; // Forward declaration
16
17namespace runtime_stats {
18
19static const char *const TAG = "runtime_stats";
20
22 public:
30
31 void record_time(uint32_t duration_ms) {
32 // Update period counters
33 this->period_count_++;
34 this->period_time_ms_ += duration_ms;
35 if (duration_ms > this->period_max_time_ms_)
36 this->period_max_time_ms_ = duration_ms;
37
38 // Update total counters
39 this->total_count_++;
40 this->total_time_ms_ += duration_ms;
41 if (duration_ms > this->total_max_time_ms_)
42 this->total_max_time_ms_ = duration_ms;
43 }
44
46 this->period_count_ = 0;
47 this->period_time_ms_ = 0;
48 this->period_max_time_ms_ = 0;
49 }
50
51 // Period stats (reset each logging interval)
52 uint32_t get_period_count() const { return this->period_count_; }
53 uint32_t get_period_time_ms() const { return this->period_time_ms_; }
54 uint32_t get_period_max_time_ms() const { return this->period_max_time_ms_; }
55 float get_period_avg_time_ms() const {
56 return this->period_count_ > 0 ? this->period_time_ms_ / static_cast<float>(this->period_count_) : 0.0f;
57 }
58
59 // Total stats (persistent until reboot)
60 uint32_t get_total_count() const { return this->total_count_; }
61 uint32_t get_total_time_ms() const { return this->total_time_ms_; }
62 uint32_t get_total_max_time_ms() const { return this->total_max_time_ms_; }
63 float get_total_avg_time_ms() const {
64 return this->total_count_ > 0 ? this->total_time_ms_ / static_cast<float>(this->total_count_) : 0.0f;
65 }
66
67 protected:
68 // Period stats (reset each logging interval)
69 uint32_t period_count_;
72
73 // Total stats (persistent until reboot)
74 uint32_t total_count_;
77};
78
80 public:
82
83 void set_log_interval(uint32_t log_interval) { this->log_interval_ = log_interval; }
84 uint32_t get_log_interval() const { return this->log_interval_; }
85
86 void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time);
87
88 // Process any pending stats printing (should be called after component loop)
89 void process_pending_stats(uint32_t current_time);
90
91 protected:
92 void log_stats_();
93
94 void reset_stats_() {
95 for (auto &it : this->component_stats_) {
96 it.second.reset_period_stats();
97 }
98 }
99
100 // Map from component to its stats
101 // We use Component* as the key since each component is unique
102 std::map<Component *, ComponentRuntimeStats> component_stats_;
105};
106
107} // namespace runtime_stats
108
110 *global_runtime_stats; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
111
112} // namespace esphome
113
114#endif // USE_RUNTIME_STATS
void set_log_interval(uint32_t log_interval)
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time)
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