1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "battery_stats_parser.h"
17
18 #include <algorithm>
19 #include <fstream>
20 #include "ios"
21 #include "json/reader.h"
22 #include "string_ex.h"
23
24 #include "stats_utils.h"
25 #include "config_policy_utils.h"
26
27 namespace OHOS {
28 namespace PowerMgr {
29 namespace {
30 static const std::string POWER_AVERAGE_FILE = "etc/power_config/power_average.json";
31 static const std::string VENDOR_POWER_AVERAGE_FILE = "/vendor/etc/power_config/power_average.json";
32 static const std::string SYSTEM_POWER_AVERAGE_FILE = "/system/etc/power_config/power_average.json";
33 } // namespace
Init()34 bool BatteryStatsParser::Init()
35 {
36 char buf[MAX_PATH_LEN];
37 char* path = GetOneCfgFile(POWER_AVERAGE_FILE.c_str(), buf, MAX_PATH_LEN);
38 if (path != nullptr && *path != '\0') {
39 if (LoadAveragePowerFromFile(path)) {
40 return true;
41 }
42 return false;
43 }
44 if (!LoadAveragePowerFromFile(VENDOR_POWER_AVERAGE_FILE)) {
45 STATS_HILOGE(COMP_SVC, "Failed to load vendor average power file");
46 if (!LoadAveragePowerFromFile(SYSTEM_POWER_AVERAGE_FILE)) {
47 STATS_HILOGE(COMP_SVC, "Failed to load system average power file");
48 return false;
49 }
50 }
51 return true;
52 }
53
GetSpeedNum(uint16_t cluster)54 uint16_t BatteryStatsParser::GetSpeedNum(uint16_t cluster)
55 {
56 for (uint16_t i = 0; i < speedNum_.size(); i++) {
57 if (cluster == i) {
58 STATS_HILOGD(COMP_SVC, "Get speed num: %{public}d, for cluster: %{public}d", speedNum_[i],
59 cluster);
60 return speedNum_[i];
61 }
62 }
63 STATS_HILOGW(COMP_SVC, "No related speed number, return 0");
64 return StatsUtils::DEFAULT_VALUE;
65 }
66
LoadAveragePowerFromFile(const std::string & path)67 bool BatteryStatsParser::LoadAveragePowerFromFile(const std::string& path)
68 {
69 Json::CharReaderBuilder reader;
70 Json::Value root;
71 std::string errors;
72 std::ifstream ifs(path, std::ios::binary);
73 if (!ifs.is_open()) {
74 STATS_HILOGE(COMP_SVC, "Json file doesn't exist");
75 return false;
76 }
77 if (!parseFromStream(reader, ifs, &root, &errors)) {
78 STATS_HILOGE(COMP_SVC, "Failed to parse the JSON file");
79 ifs.close();
80 return false;
81 }
82 ifs.close();
83
84 if (root.isNull() || !root.isObject()) {
85 STATS_HILOGE(COMP_SVC, "root invalid[%{public}s]", path.c_str());
86 return false;
87 }
88
89 Json::Value::Members members = root.getMemberNames();
90 for (auto iter = members.begin(); iter != members.end(); iter++) {
91 std::string type = *iter;
92 Json::Value value = root[type];
93
94 if (value.isNull()) {
95 STATS_HILOGE(COMP_SVC, "value invalid[%{public}s]", type.c_str());
96 continue;
97 }
98
99 if (type == StatsUtils::CURRENT_CPU_CLUSTER) {
100 clusterNum_ = value.size();
101 STATS_HILOGD(COMP_SVC, "Read cluster num: %{public}d", clusterNum_);
102 }
103
104 if (type.find(StatsUtils::CURRENT_CPU_SPEED) != std::string::npos) {
105 STATS_HILOGD(COMP_SVC, "Read speed num: %{public}d", static_cast<int32_t>(value.size()));
106 speedNum_.push_back(value.size());
107 }
108
109 if (value.isArray()) {
110 ParsingArray(type, value);
111 } else if (value.isDouble()) {
112 averageMap_.insert(std::pair<std::string, double>(type, value.asDouble()));
113 }
114 }
115 return true;
116 }
117
ParsingArray(const std::string & type,const Json::Value & array)118 void BatteryStatsParser::ParsingArray(const std::string& type, const Json::Value& array)
119 {
120 std::vector<double> listValues;
121 std::for_each(array.begin(), array.end(), [&](const Json::Value& value) {
122 if (value.isDouble()) {
123 listValues.push_back(value.asDouble());
124 }
125 });
126 averageVecMap_.insert(std::pair<std::string, std::vector<double>>(type, listValues));
127 }
128
GetAveragePowerMa(std::string type)129 double BatteryStatsParser::GetAveragePowerMa(std::string type)
130 {
131 double average = 0.0;
132 auto iter = averageMap_.find(type);
133 if (iter != averageMap_.end()) {
134 average = iter->second;
135 }
136 STATS_HILOGD(COMP_SVC, "Get average power: %{public}lfma of %{public}s", average, type.c_str());
137 return average;
138 }
139
GetAveragePowerMa(std::string type,uint16_t level)140 double BatteryStatsParser::GetAveragePowerMa(std::string type, uint16_t level)
141 {
142 double average = 0.0;
143 auto iter = averageVecMap_.find(type);
144 if (iter != averageVecMap_.end()) {
145 if (level < iter->second.size()) {
146 average = iter->second[level];
147 }
148 }
149 STATS_HILOGD(COMP_SVC, "Get average power: %{public}lf of %{public}s, level: %{public}d",
150 average, type.c_str(), level);
151 return average;
152 }
153
GetClusterNum()154 uint16_t BatteryStatsParser::GetClusterNum()
155 {
156 return clusterNum_;
157 }
158
DumpInfo(std::string & result)159 void BatteryStatsParser::DumpInfo(std::string& result)
160 {
161 result.append("POWER AVERAGE CONFIGATION DUMP:\n");
162 result.append("\n");
163 for (auto iter : averageMap_) {
164 result.append(iter.first).append(" : ").append(ToString(iter.second)).append("\n");
165 }
166 for (auto vecIter : averageVecMap_) {
167 result.append(vecIter.first).append(" : [");
168 for (auto levelIter : vecIter.second) {
169 result.append(" ").append(ToString(levelIter));
170 }
171 result.append(" ]").append("\n");
172 }
173 }
174 } // namespace PowerMgr
175 } // namespace OHOS
176