diff options
author | sashwinbalaji <sashwinbalaji@google.com> | 2023-04-27 20:34:14 +0800 |
---|---|---|
committer | S Ashwin Balaji <sashwinbalaji@google.com> | 2023-05-25 03:26:28 +0000 |
commit | efddbd32335663335b0dc6f0e17030a35b23e73f (patch) | |
tree | f183d2cc7027a4a7f79043f5a496de22b67e3317 | |
parent | 291ae93b23825549956a80bb1425e1998f6fb8af (diff) | |
download | pixel-efddbd32335663335b0dc6f0e17030a35b23e73f.tar.gz |
thermal: HAL add support for max and min temp in residency stats
Bug: 193833982
Test: Local build and verify statsD logs and lshal
adb shell cmd stats print-logs && adb logcat -b all | grep -i 105045
Change-Id: I9a38b7724fb6e9e2b4c7cce0daa50fd04c1133e5
-rw-r--r-- | thermal/Thermal.cpp | 4 | ||||
-rw-r--r-- | thermal/thermal-helper.h | 2 | ||||
-rw-r--r-- | thermal/utils/thermal_stats_helper.cpp | 110 | ||||
-rw-r--r-- | thermal/utils/thermal_stats_helper.h | 19 |
4 files changed, 88 insertions, 47 deletions
diff --git a/thermal/Thermal.cpp b/thermal/Thermal.cpp index caab2b52..776341dd 100644 --- a/thermal/Thermal.cpp +++ b/thermal/Thermal.cpp @@ -545,6 +545,10 @@ void Thermal::dumpThermalStats(std::ostringstream *dump_buf) { for (const auto &sensor_temp_stats_pair : sensor_temp_stats_map_) { *dump_buf << " Sensor Name: " << sensor_temp_stats_pair.first << std::endl; const auto &sensor_temp_stats = sensor_temp_stats_pair.second; + *dump_buf << " Max Temp: " << sensor_temp_stats.max_temp << ", TimeStamp: " + << system_clock::to_time_t(sensor_temp_stats.max_temp_timestamp) << std::endl; + *dump_buf << " Min Temp: " << sensor_temp_stats.min_temp << ", TimeStamp: " + << system_clock::to_time_t(sensor_temp_stats.min_temp_timestamp) << std::endl; for (const auto &stats_by_threshold : sensor_temp_stats.stats_by_custom_threshold) { *dump_buf << " Record by Threshold: ["; for (const auto &threshold : stats_by_threshold.thresholds) { diff --git a/thermal/thermal-helper.h b/thermal/thermal-helper.h index 5d9a203a..3b790e3e 100644 --- a/thermal/thermal-helper.h +++ b/thermal/thermal-helper.h @@ -131,7 +131,7 @@ class ThermalHelper { } // Get Thermal Stats Sensor Map - const std::unordered_map<std::string, ThermalStats<float>> GetSensorTempStatsSnapshot() { + const std::unordered_map<std::string, SensorTempStats> GetSensorTempStatsSnapshot() { return thermal_stats_helper_.GetSensorTempStatsSnapshot(); } // Get Thermal Stats Sensor, Binded Cdev State Request Map diff --git a/thermal/utils/thermal_stats_helper.cpp b/thermal/utils/thermal_stats_helper.cpp index d9a2a65a..34446b98 100644 --- a/thermal/utils/thermal_stats_helper.cpp +++ b/thermal/utils/thermal_stats_helper.cpp @@ -235,8 +235,8 @@ void ThermalStatsHelper::updateSensorTempStatsByThreshold(std::string_view senso if (!sensor_temp_stats_map_.count(sensor.data())) { return; } - for (auto &stats_by_threshold : - sensor_temp_stats_map_[sensor.data()].stats_by_custom_threshold) { + auto &sensor_temp_stats = sensor_temp_stats_map_[sensor.data()]; + for (auto &stats_by_threshold : sensor_temp_stats.stats_by_custom_threshold) { int value = calculateThresholdBucket(stats_by_threshold.thresholds, temperature); if (value != stats_by_threshold.stats_record.cur_state) { LOG(VERBOSE) << "Updating sensor stats for sensor: " << sensor.data() @@ -244,6 +244,14 @@ void ThermalStatsHelper::updateSensorTempStatsByThreshold(std::string_view senso updateStatsRecord(&stats_by_threshold.stats_record, value); } } + if (temperature > sensor_temp_stats.max_temp) { + sensor_temp_stats.max_temp = temperature; + sensor_temp_stats.max_temp_timestamp = system_clock::now(); + } + if (temperature < sensor_temp_stats.min_temp) { + sensor_temp_stats.min_temp = temperature; + sensor_temp_stats.min_temp_timestamp = system_clock::now(); + } } void ThermalStatsHelper::updateSensorTempStatsBySeverity(std::string_view sensor, @@ -294,13 +302,13 @@ int ThermalStatsHelper::reportAllSensorTempStats(const std::shared_ptr<IStats> & auto &stats_by_threshold = temp_stats.stats_by_custom_threshold[threshold_set_idx]; std::string sensor_name = stats_by_threshold.logging_name.value_or( sensor + kCustomThresholdSetSuffix.data() + std::to_string(threshold_set_idx)); - if (!reportSensorTempStats(stats_client, sensor_name, + if (!reportSensorTempStats(stats_client, sensor_name, temp_stats, &stats_by_threshold.stats_record)) { count_failed_reporting++; } } if (temp_stats.stats_by_default_threshold.has_value()) { - if (!reportSensorTempStats(stats_client, sensor, + if (!reportSensorTempStats(stats_client, sensor, temp_stats, &temp_stats.stats_by_default_threshold.value())) { count_failed_reporting++; } @@ -310,18 +318,48 @@ int ThermalStatsHelper::reportAllSensorTempStats(const std::shared_ptr<IStats> & } bool ThermalStatsHelper::reportSensorTempStats(const std::shared_ptr<IStats> &stats_client, - std::string_view sensor, StatsRecord *stats_record) { + std::string_view sensor, + const SensorTempStats &sensor_temp_stats, + StatsRecord *stats_record) { LOG(VERBOSE) << "Reporting sensor stats for " << sensor; - // Load values array - std::vector<VendorAtomValue> values(1); + // maintain a copy in case reporting fails + StatsRecord thermal_stats_before_reporting = *stats_record; + std::vector<VendorAtomValue> values(2); values[0].set<VendorAtomValue::stringValue>(sensor); - if (!reportThermalStats(stats_client, PixelAtoms::Atom::kVendorTempResidencyStats, values, - stats_record)) { + std::vector<int64_t> time_in_state_ms = processStatsRecordForReporting(stats_record); + const auto since_last_update_ms = std::chrono::duration_cast<std::chrono::milliseconds>( + stats_record->cur_state_start_time - stats_record->last_stats_report_time); + values[1].set<VendorAtomValue::longValue>(since_last_update_ms.count()); + VendorAtomValue tmp; + for (auto &time_in_state : time_in_state_ms) { + tmp.set<VendorAtomValue::longValue>(time_in_state); + values.push_back(tmp); + } + auto remaining_residency_buckets_count = kMaxStatsResidencyCount - time_in_state_ms.size(); + if (remaining_residency_buckets_count > 0) { + tmp.set<VendorAtomValue::longValue>(0); + values.insert(values.end(), remaining_residency_buckets_count, tmp); + } + tmp.set<VendorAtomValue::floatValue>(sensor_temp_stats.max_temp); + values.push_back(tmp); + tmp.set<VendorAtomValue::longValue>( + system_clock::to_time_t(sensor_temp_stats.max_temp_timestamp)); + values.push_back(tmp); + tmp.set<VendorAtomValue::floatValue>(sensor_temp_stats.min_temp); + values.push_back(tmp); + tmp.set<VendorAtomValue::longValue>( + system_clock::to_time_t(sensor_temp_stats.min_temp_timestamp)); + values.push_back(tmp); + + if (!reportAtom(stats_client, PixelAtoms::Atom::kVendorTempResidencyStats, std::move(values))) { LOG(ERROR) << "Unable to report VendorTempResidencyStats to Stats service for " "sensor: " << sensor; + *stats_record = restoreStatsRecordOnFailure(std::move(thermal_stats_before_reporting)); return false; } + // Update last time of stats reporting + stats_record->last_stats_report_time = boot_clock::now(); return true; } @@ -363,17 +401,31 @@ bool ThermalStatsHelper::reportSensorCdevRequestStats(const std::shared_ptr<ISta StatsRecord *stats_record) { LOG(VERBOSE) << "Reporting bindedCdev stats for sensor: " << sensor << " cooling_device: " << cdev; - // Load values array - std::vector<VendorAtomValue> values(2); + // maintain a copy in case reporting fails + StatsRecord thermal_stats_before_reporting = *stats_record; + std::vector<VendorAtomValue> values(3); values[0].set<VendorAtomValue::stringValue>(sensor); values[1].set<VendorAtomValue::stringValue>(cdev); - if (!reportThermalStats(stats_client, PixelAtoms::Atom::kVendorSensorCoolingDeviceStats, values, - stats_record)) { + std::vector<int64_t> time_in_state_ms = processStatsRecordForReporting(stats_record); + const auto since_last_update_ms = std::chrono::duration_cast<std::chrono::milliseconds>( + stats_record->cur_state_start_time - stats_record->last_stats_report_time); + values[3].set<VendorAtomValue::longValue>(since_last_update_ms.count()); + VendorAtomValue tmp; + for (auto &time_in_state : time_in_state_ms) { + tmp.set<VendorAtomValue::longValue>(time_in_state); + values.push_back(tmp); + } + + if (!reportAtom(stats_client, PixelAtoms::Atom::kVendorSensorCoolingDeviceStats, + std::move(values))) { LOG(ERROR) << "Unable to report VendorSensorCoolingDeviceStats to Stats " "service for sensor: " << sensor << " cooling_device: " << cdev; + *stats_record = restoreStatsRecordOnFailure(std::move(thermal_stats_before_reporting)); return false; } + // Update last time of stats reporting + stats_record->last_stats_report_time = boot_clock::now(); return true; } @@ -390,36 +442,13 @@ std::vector<int64_t> ThermalStatsHelper::processStatsRecordForReporting(StatsRec return stats_residency; } -bool ThermalStatsHelper::reportThermalStats(const std::shared_ptr<IStats> &stats_client, - const int32_t &atom_id, - std::vector<VendorAtomValue> values, - StatsRecord *stats_record) { - // maintain a copy in case reporting fails - StatsRecord thermal_stats_before_reporting = *stats_record; - std::vector<int64_t> time_in_state_ms = processStatsRecordForReporting(stats_record); - const auto since_last_update_ms = std::chrono::duration_cast<std::chrono::milliseconds>( - stats_record->cur_state_start_time - stats_record->last_stats_report_time); - VendorAtomValue tmp; - tmp.set<VendorAtomValue::longValue>(since_last_update_ms.count()); - values.push_back(tmp); - for (auto &time_in_state : time_in_state_ms) { - tmp.set<VendorAtomValue::longValue>(time_in_state); - values.push_back(tmp); - } - +bool ThermalStatsHelper::reportAtom(const std::shared_ptr<IStats> &stats_client, + const int32_t &atom_id, std::vector<VendorAtomValue> &&values) { LOG(VERBOSE) << "Reporting thermal stats for atom_id " << atom_id; // Send vendor atom to IStats HAL VendorAtom event = {.reverseDomainName = "", .atomId = atom_id, .values = std::move(values)}; const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event); - if (!ret.isOk()) { - LOG(ERROR) << "Unable to report to Stats service for atom " << atom_id; - *stats_record = restoreStatsRecordOnFailure(std::move(thermal_stats_before_reporting)); - return false; - } else { - // Update last time of stats reporting - stats_record->last_stats_report_time = boot_clock::now(); - } - return true; + return ret.isOk(); } StatsRecord ThermalStatsHelper::restoreStatsRecordOnFailure( @@ -434,8 +463,7 @@ StatsRecord ThermalStatsHelper::restoreStatsRecordOnFailure( } } -std::unordered_map<std::string, ThermalStats<float>> -ThermalStatsHelper::GetSensorTempStatsSnapshot() { +std::unordered_map<std::string, SensorTempStats> ThermalStatsHelper::GetSensorTempStatsSnapshot() { auto sensor_temp_stats_snapshot = sensor_temp_stats_map_; for (auto &sensor_temp_stats_pair : sensor_temp_stats_snapshot) { for (auto &temp_stats : sensor_temp_stats_pair.second.stats_by_custom_threshold) { diff --git a/thermal/utils/thermal_stats_helper.h b/thermal/utils/thermal_stats_helper.h index 93d972f9..ef2f811f 100644 --- a/thermal/utils/thermal_stats_helper.h +++ b/thermal/utils/thermal_stats_helper.h @@ -37,6 +37,8 @@ namespace implementation { using aidl::android::frameworks::stats::IStats; using aidl::android::frameworks::stats::VendorAtomValue; using ::android::base::boot_clock; +using std::chrono::system_clock; +using SystemTimePoint = std::chrono::time_point<std::chrono::system_clock>; constexpr int kMaxStatsReportingFailCount = 3; @@ -89,6 +91,13 @@ struct ThermalStats { std::optional<StatsRecord> stats_by_default_threshold; }; +struct SensorTempStats : ThermalStats<float> { + float max_temp = std::numeric_limits<float>::min(); + SystemTimePoint max_temp_timestamp = SystemTimePoint::min(); + float min_temp = std::numeric_limits<float>::max(); + SystemTimePoint min_temp_timestamp = SystemTimePoint::min(); +}; + class ThermalStatsHelper { public: ThermalStatsHelper() = default; @@ -114,7 +123,7 @@ class ThermalStatsHelper { */ int reportStats(); // Get a snapshot of Thermal Stats Sensor Map till that point in time - std::unordered_map<std::string, ThermalStats<float>> GetSensorTempStatsSnapshot(); + std::unordered_map<std::string, SensorTempStats> GetSensorTempStatsSnapshot(); // Get a snapshot of Thermal Stats Sensor Map till that point in time std::unordered_map<std::string, std::unordered_map<std::string, ThermalStats<int>>> GetSensorCoolingDeviceRequestStatsSnapshot(); @@ -126,7 +135,7 @@ class ThermalStatsHelper { mutable std::shared_mutex sensor_temp_stats_map_mutex_; // Temperature stats for each sensor being watched - std::unordered_map<std::string, ThermalStats<float>> sensor_temp_stats_map_; + std::unordered_map<std::string, SensorTempStats> sensor_temp_stats_map_; mutable std::shared_mutex sensor_cdev_request_stats_map_mutex_; // userVote request stat for the sensor to the corresponding cdev (sensor -> cdev -> // StatsRecord) @@ -143,13 +152,13 @@ class ThermalStatsHelper { void updateStatsRecord(StatsRecord *stats_record, int new_state); int reportAllSensorTempStats(const std::shared_ptr<IStats> &stats_client); bool reportSensorTempStats(const std::shared_ptr<IStats> &stats_client, std::string_view sensor, - StatsRecord *stats_record); + const SensorTempStats &sensor_temp_stats, StatsRecord *stats_record); int reportAllSensorCdevRequestStats(const std::shared_ptr<IStats> &stats_client); bool reportSensorCdevRequestStats(const std::shared_ptr<IStats> &stats_client, std::string_view sensor, std::string_view cdev, StatsRecord *stats_record); - bool reportThermalStats(const std::shared_ptr<IStats> &stats_client, const int32_t &atom_id, - std::vector<VendorAtomValue> values, StatsRecord *stats_record); + bool reportAtom(const std::shared_ptr<IStats> &stats_client, const int32_t &atom_id, + std::vector<VendorAtomValue> &&values); std::vector<int64_t> processStatsRecordForReporting(StatsRecord *stats_record); StatsRecord restoreStatsRecordOnFailure(StatsRecord &&stats_record_before_failure); }; |