ESPHome 2025.8.0b2
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 <vector>
9#include <cstdint>
10#include <cstring>
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_ms) {
33 // Update period counters
34 this->period_count_++;
35 this->period_time_ms_ += duration_ms;
36 if (duration_ms > this->period_max_time_ms_)
37 this->period_max_time_ms_ = duration_ms;
38
39 // Update total counters
40 this->total_count_++;
41 this->total_time_ms_ += duration_ms;
42 if (duration_ms > this->total_max_time_ms_)
43 this->total_max_time_ms_ = duration_ms;
44 }
45
47 this->period_count_ = 0;
48 this->period_time_ms_ = 0;
49 this->period_max_time_ms_ = 0;
50 }
51
52 // Period stats (reset each logging interval)
53 uint32_t get_period_count() const { return this->period_count_; }
54 uint32_t get_period_time_ms() const { return this->period_time_ms_; }
55 uint32_t get_period_max_time_ms() const { return this->period_max_time_ms_; }
56 float get_period_avg_time_ms() const {
57 return this->period_count_ > 0 ? this->period_time_ms_ / static_cast<float>(this->period_count_) : 0.0f;
58 }
59
60 // Total stats (persistent until reboot)
61 uint32_t get_total_count() const { return this->total_count_; }
62 uint32_t get_total_time_ms() const { return this->total_time_ms_; }
63 uint32_t get_total_max_time_ms() const { return this->total_max_time_ms_; }
64 float get_total_avg_time_ms() const {
65 return this->total_count_ > 0 ? this->total_time_ms_ / static_cast<float>(this->total_count_) : 0.0f;
66 }
67
68 protected:
69 // Period stats (reset each logging interval)
70 uint32_t period_count_;
73
74 // Total stats (persistent until reboot)
75 uint32_t total_count_;
78};
79
80// For sorting components by run time
82 const char *name;
84
85 bool operator>(const ComponentStatPair &other) const {
86 // Sort by period time as that's what we're displaying in the logs
88 }
89};
90
92 public:
94
95 void set_log_interval(uint32_t log_interval) { this->log_interval_ = log_interval; }
96 uint32_t get_log_interval() const { return this->log_interval_; }
97
98 void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time);
99
100 // Process any pending stats printing (should be called after component loop)
101 void process_pending_stats(uint32_t current_time);
102
103 protected:
104 void log_stats_();
105
107 for (auto &it : this->component_stats_) {
108 it.second.reset_period_stats();
109 }
110 }
111
112 // Use const char* keys for efficiency
113 // Custom comparator for const char* keys in map
114 // Without this, std::map would compare pointer addresses instead of string contents,
115 // causing identical component names at different addresses to be treated as different keys
116 struct CStrCompare {
117 bool operator()(const char *a, const char *b) const { return std::strcmp(a, b) < 0; }
118 };
119 std::map<const char *, ComponentRuntimeStats, CStrCompare> component_stats_;
120 std::map<Component *, const char *> component_names_cache_;
123};
124
125} // namespace runtime_stats
126
128 *global_runtime_stats; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
129
130} // namespace esphome
131
132#endif // USE_RUNTIME_STATS
void set_log_interval(uint32_t log_interval)
std::map< const char *, ComponentRuntimeStats, CStrCompare > component_stats_
void record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time)
std::map< Component *, const char * > component_names_cache_
void process_pending_stats(uint32_t current_time)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
runtime_stats::RuntimeStatsCollector * global_runtime_stats
bool operator>(const ComponentStatPair &other) const
const ComponentRuntimeStats * stats
bool operator()(const char *a, const char *b) const