• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android {
31 namespace hardware {
32 namespace thermal {
33 namespace V2_0 {
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 // A helper class for monitoring power rails.
51 class PowerFiles {
52   public:
53     PowerFiles() = default;
54     ~PowerFiles() = default;
55     // Disallow copy and assign.
56     PowerFiles(const PowerFiles &) = delete;
57     void operator=(const PowerFiles &) = delete;
58     bool registerPowerRailsToWatch(std::string_view config_path);
59     // Update the power data from ODPM sysfs
60     bool refreshPowerStatus(void);
61     // Get power status map
GetPowerStatusMap()62     const std::unordered_map<std::string, PowerStatus> &GetPowerStatusMap() const {
63         std::shared_lock<std::shared_mutex> _lock(power_status_map_mutex_);
64         return power_status_map_;
65     }
66     // Get power rail info map
GetPowerRailInfoMap()67     const std::unordered_map<std::string, PowerRailInfo> &GetPowerRailInfoMap() const {
68         return power_rail_info_map_;
69     }
70 
71   private:
72     // Update energy value to energy_info_map_, return false if the value is failed to update.
73     bool updateEnergyValues(void);
74     // Compute the average power for physical power rail.
75     float updateAveragePower(std::string_view power_rail, std::queue<PowerSample> *power_history);
76     // Update the power data for the target power rail.
77     float updatePowerRail(std::string_view power_rail);
78     // Find the energy source path, return false if no energy source found.
79     bool findEnergySourceToWatch(void);
80     // The map to record the energy counter for each power rail.
81     std::unordered_map<std::string, PowerSample> energy_info_map_;
82     // The map to record the power data for each thermal sensor.
83     std::unordered_map<std::string, PowerStatus> power_status_map_;
84     mutable std::shared_mutex power_status_map_mutex_;
85     // The map to record the power rail information from thermal config
86     std::unordered_map<std::string, PowerRailInfo> power_rail_info_map_;
87     // The set to store the energy source paths
88     std::unordered_set<std::string> energy_path_set_;
89 };
90 
91 }  // namespace implementation
92 }  // namespace V2_0
93 }  // namespace thermal
94 }  // namespace hardware
95 }  // namespace android
96