1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <android-base/chrono_utils.h> 20 21 #include <chrono> 22 #include <queue> 23 #include <shared_mutex> 24 #include <string> 25 #include <unordered_map> 26 #include <unordered_set> 27 28 #include "thermal_info.h" 29 30 namespace aidl { 31 namespace android { 32 namespace hardware { 33 namespace thermal { 34 namespace implementation { 35 36 using ::android::base::boot_clock; 37 38 struct PowerSample { 39 uint64_t energy_counter; 40 uint64_t duration; 41 }; 42 43 struct PowerStatus { 44 boot_clock::time_point last_update_time; 45 // A vector to record the queues of power sample history. 46 std::vector<std::queue<PowerSample>> power_history; 47 float last_updated_avg_power; 48 bool enabled; 49 }; 50 51 struct PowerStatusLog { 52 boot_clock::time_point prev_log_time; 53 // energy sample at last logging 54 std::unordered_map<std::string, PowerSample> prev_energy_info_map; 55 }; 56 57 // A helper class for monitoring power rails. 58 class PowerFiles { 59 public: 60 PowerFiles() = default; 61 ~PowerFiles() = default; 62 // Disallow copy and assign. 63 PowerFiles(const PowerFiles &) = delete; 64 void operator=(const PowerFiles &) = delete; 65 bool registerPowerRailsToWatch( 66 const Json::Value &config, 67 std::unordered_map<std::string, std::vector<std::string>> *power_rail_switch_map); 68 // Update the power data from ODPM sysfs 69 bool refreshPowerStatus(void); 70 // Log the power data for the duration 71 void logPowerStatus(const boot_clock::time_point &now); 72 // OnOff the power calculation 73 void powerSamplingSwitch(std::string_view power_rail, const bool enabled); 74 // Get previous power log time_point GetPrevPowerLogTime()75 const boot_clock::time_point &GetPrevPowerLogTime() const { 76 return power_status_log_.prev_log_time; 77 } 78 // Get power status map GetPowerStatusMap()79 const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const { 80 std::shared_lock<std::shared_mutex> _lock(power_status_map_mutex_); 81 return power_status_map_; 82 } 83 // Get power rail info map GetPowerRailInfoMap()84 const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const { 85 return power_rail_info_map_; 86 } 87 88 private: 89 // Update energy value to energy_info_map_, return false if the value is failed to update. 90 bool updateEnergyValues(void); 91 // Compute the average power for physical power rail. 92 float updateAveragePower(std::string_view power_rail, std::queue<PowerSample> *power_history); 93 // Update the power data for the target power rail. 94 float updatePowerRail(std::string_view power_rail); 95 // Find the energy source path, return false if no energy source found. 96 bool findEnergySourceToWatch(void); 97 // The map to record the energy counter for each power rail. 98 std::unordered_map<std::string, PowerSample> energy_info_map_; 99 // The map to record the power data for each thermal sensor. 100 std::unordered_map<std::string, PowerStatus> power_status_map_; 101 mutable std::shared_mutex power_status_map_mutex_; 102 // The map to record the power rail information from thermal config 103 std::unordered_map<std::string, PowerRailInfo> power_rail_info_map_; 104 // The set to store the energy source paths 105 std::unordered_set<std::string> energy_path_set_; 106 PowerStatusLog power_status_log_; 107 }; 108 109 } // namespace implementation 110 } // namespace thermal 111 } // namespace hardware 112 } // namespace android 113 } // namespace aidl 114