ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
runtime_stats.cpp
Go to the documentation of this file.
1#include "runtime_stats.h"
2
3#ifdef USE_RUNTIME_STATS
4
6#include <algorithm>
7
8namespace esphome {
9
10namespace runtime_stats {
11
12RuntimeStatsCollector::RuntimeStatsCollector() : log_interval_(60000), next_log_time_(0) {
14}
15
16void RuntimeStatsCollector::record_component_time(Component *component, uint32_t duration_ms, uint32_t current_time) {
17 if (component == nullptr)
18 return;
19
20 // Record stats using component pointer as key
21 this->component_stats_[component].record_time(duration_ms);
22
23 if (this->next_log_time_ == 0) {
24 this->next_log_time_ = current_time + this->log_interval_;
25 return;
26 }
27}
28
30 ESP_LOGI(TAG, "Component Runtime Statistics");
31 ESP_LOGI(TAG, "Period stats (last %" PRIu32 "ms):", this->log_interval_);
32
33 // First collect stats we want to display
34 std::vector<ComponentStatPair> stats_to_display;
35
36 for (const auto &it : this->component_stats_) {
37 Component *component = it.first;
38 const ComponentRuntimeStats &stats = it.second;
39 if (stats.get_period_count() > 0) {
40 ComponentStatPair pair = {component, &stats};
41 stats_to_display.push_back(pair);
42 }
43 }
44
45 // Sort by period runtime (descending)
46 std::sort(stats_to_display.begin(), stats_to_display.end(), std::greater<ComponentStatPair>());
47
48 // Log top components by period runtime
49 for (const auto &it : stats_to_display) {
50 ESP_LOGI(TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms",
51 LOG_STR_ARG(it.component->get_component_log_str()), it.stats->get_period_count(),
52 it.stats->get_period_avg_time_ms(), it.stats->get_period_max_time_ms(), it.stats->get_period_time_ms());
53 }
54
55 // Log total stats since boot
56 ESP_LOGI(TAG, "Total stats (since boot):");
57
58 // Re-sort by total runtime for all-time stats
59 std::sort(stats_to_display.begin(), stats_to_display.end(),
60 [](const ComponentStatPair &a, const ComponentStatPair &b) {
61 return a.stats->get_total_time_ms() > b.stats->get_total_time_ms();
62 });
63
64 for (const auto &it : stats_to_display) {
65 ESP_LOGI(TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms",
66 LOG_STR_ARG(it.component->get_component_log_str()), it.stats->get_total_count(),
67 it.stats->get_total_avg_time_ms(), it.stats->get_total_max_time_ms(), it.stats->get_total_time_ms());
68 }
69}
70
72 if (this->next_log_time_ == 0)
73 return;
74
75 if (current_time >= this->next_log_time_) {
76 this->log_stats_();
77 this->reset_stats_();
78 this->next_log_time_ = current_time + this->log_interval_;
79 }
80}
81
82} // namespace runtime_stats
83
85 nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
86
87} // namespace esphome
88
89#endif // USE_RUNTIME_STATS
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
runtime_stats::RuntimeStatsCollector * global_runtime_stats