• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
49 
50 struct PowerStatusLog {
51     boot_clock::time_point prev_log_time;
52     // energy sample at last logging
53     std::unordered_map<std::string, PowerSample> prev_energy_info_map;
54 };
55 
56 // A helper class for monitoring power rails.
57 class PowerFiles {
58   public:
59     PowerFiles() = default;
60     ~PowerFiles() = default;
61     // Disallow copy and assign.
62     PowerFiles(const PowerFiles &) = delete;
63     void operator=(const PowerFiles &) = delete;
64     bool registerPowerRailsToWatch(const Json::Value &config);
65     // Update the power data from ODPM sysfs
66     bool refreshPowerStatus(void);
67     // Log the power data for the duration
68     void logPowerStatus(const boot_clock::time_point &now);
69     // Get previous power log time_point
GetPrevPowerLogTime()70     const boot_clock::time_point &GetPrevPowerLogTime() const {
71         return power_status_log_.prev_log_time;
72     }
73     // Get power status map
GetPowerStatusMap()74     const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const {
75         std::shared_lock<std::shared_mutex> _lock(power_status_map_mutex_);
76         return power_status_map_;
77     }
78     // Get power rail info map
GetPowerRailInfoMap()79     const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const {
80         return power_rail_info_map_;
81     }
82 
83   private:
84     // Update energy value to energy_info_map_, return false if the value is failed to update.
85     bool updateEnergyValues(void);
86     // Compute the average power for physical power rail.
87     float updateAveragePower(std::string_view power_rail, std::queue<PowerSample> *power_history);
88     // Update the power data for the target power rail.
89     float updatePowerRail(std::string_view power_rail);
90     // Find the energy source path, return false if no energy source found.
91     bool findEnergySourceToWatch(void);
92     // The map to record the energy counter for each power rail.
93     std::unordered_map<std::string, PowerSample> energy_info_map_;
94     // The map to record the power data for each thermal sensor.
95     std::unordered_map<std::string, PowerStatus> power_status_map_;
96     mutable std::shared_mutex power_status_map_mutex_;
97     // The map to record the power rail information from thermal config
98     std::unordered_map<std::string, PowerRailInfo> power_rail_info_map_;
99     // The set to store the energy source paths
100     std::unordered_set<std::string> energy_path_set_;
101     PowerStatusLog power_status_log_;
102 };
103 
104 }  // namespace implementation
105 }  // namespace thermal
106 }  // namespace hardware
107 }  // namespace android
108 }  // namespace aidl
109