1 /*
2 * Copyright (c) 2021-2022 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 <fstream>
19 #include "ios"
20 #include "json/reader.h"
21 #include "json/value.h"
22
23 #include "stats_utils.h"
24
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 static const std::string VENDOR_POWER_AVERAGE_FILE = "/vendor/etc/profile/power_average.json";
29 static const std::string SYSTEM_POWER_AVERAGE_FILE = "/system/etc/profile/power_average.json";
30 } // namespace
Init()31 bool BatteryStatsParser::Init()
32 {
33 if (!LoadAveragePowerFromFile(VENDOR_POWER_AVERAGE_FILE)) {
34 STATS_HILOGE(COMP_SVC, "Failed to load vendor average power file");
35 if (!LoadAveragePowerFromFile(SYSTEM_POWER_AVERAGE_FILE)) {
36 STATS_HILOGE(COMP_SVC, "Failed to load system average power file");
37 return false;
38 }
39 }
40 return true;
41 }
42
GetSpeedNum(uint16_t cluster)43 uint16_t BatteryStatsParser::GetSpeedNum(uint16_t cluster)
44 {
45 for (uint16_t i = 0; i < speedNum_.size(); i++) {
46 if (cluster == i) {
47 STATS_HILOGD(COMP_SVC, "Get speed num: %{public}d, for cluster: %{public}d", speedNum_[i],
48 cluster);
49 return speedNum_[i];
50 }
51 }
52 STATS_HILOGW(COMP_SVC, "No related speed number, return 0");
53 return StatsUtils::DEFAULT_VALUE;
54 }
55
LoadAveragePowerFromFile(const std::string & path)56 bool BatteryStatsParser::LoadAveragePowerFromFile(const std::string& path)
57 {
58 Json::CharReaderBuilder reader;
59 Json::Value root;
60 std::string errors;
61 std::ifstream ifs(path, std::ios::binary);
62 if (!ifs.is_open()) {
63 STATS_HILOGE(COMP_SVC, "Json file doesn't exist");
64 return false;
65 }
66 if (!parseFromStream(reader, ifs, &root, &errors)) {
67 STATS_HILOGE(COMP_SVC, "Failed to parse the JSON file");
68 ifs.close();
69 return false;
70 }
71 ifs.close();
72
73 Json::Value::Members members = root.getMemberNames();
74 for (auto iter = members.begin(); iter != members.end(); iter++) {
75 std::string type = *iter;
76 Json::Value value = root[type];
77
78 if (type == StatsUtils::CURRENT_CPU_CLUSTER) {
79 clusterNum_ = value.size();
80 STATS_HILOGD(COMP_SVC, "Read cluster num: %{public}d", clusterNum_);
81 }
82
83 if (type.find(StatsUtils::CURRENT_CPU_SPEED) != std::string::npos) {
84 STATS_HILOGD(COMP_SVC, "Read speed num: %{public}d", static_cast<int32_t>(value.size()));
85 speedNum_.push_back(value.size());
86 }
87
88 if (value.isArray()) {
89 std::vector<double> listValues;
90 for (uint16_t i = 0; i < value.size(); i++) {
91 listValues.push_back(value[i].asDouble());
92 STATS_HILOGD(COMP_SVC, "Read list value: %{public}lf of %{public}s", value[i].asDouble(),
93 type.c_str());
94 }
95 averageVecMap_.insert(std::pair<std::string, std::vector<double>>(type, listValues));
96 } else {
97 double singleValue = value.asDouble();
98 averageMap_.insert(std::pair<std::string, double>(type, singleValue));
99 STATS_HILOGD(COMP_SVC, "Read single value: %{public}lf of %{public}s", singleValue,
100 type.c_str());
101 }
102 }
103 return true;
104 }
105
GetAveragePowerMa(std::string type)106 double BatteryStatsParser::GetAveragePowerMa(std::string type)
107 {
108 double average = 0.0;
109 auto iter = averageMap_.find(type);
110 if (iter != averageMap_.end()) {
111 average = iter->second;
112 }
113 STATS_HILOGD(COMP_SVC, "Get average power: %{public}lfma of %{public}s", average, type.c_str());
114 return average;
115 }
116
GetAveragePowerMa(std::string type,uint16_t level)117 double BatteryStatsParser::GetAveragePowerMa(std::string type, uint16_t level)
118 {
119 double average = 0.0;
120 auto iter = averageVecMap_.find(type);
121 if (iter != averageVecMap_.end()) {
122 if (level < iter->second.size()) {
123 average = iter->second[level];
124 }
125 }
126 STATS_HILOGD(COMP_SVC, "Get average power: %{public}lf of %{public}s, level: %{public}d",
127 average, type.c_str(), level);
128 return average;
129 }
130
GetClusterNum()131 uint16_t BatteryStatsParser::GetClusterNum()
132 {
133 return clusterNum_;
134 }
135 } // namespace PowerMgr
136 } // namespace OHOS
137