1 /*
2 * Copyright (C) 2020 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 #include <android-base/logging.h>
18 #include <android-base/strings.h>
19
20 #include <dataproviders/GenericStateResidencyDataProvider.h>
21
22 namespace aidl {
23 namespace android {
24 namespace hardware {
25 namespace power {
26 namespace stats {
27
28 std::vector<GenericStateResidencyDataProvider::StateResidencyConfig>
generateGenericStateResidencyConfigs(const GenericStateResidencyDataProvider::StateResidencyConfig & stateConfig,const std::vector<std::pair<std::string,std::string>> & stateHeaders)29 generateGenericStateResidencyConfigs(
30 const GenericStateResidencyDataProvider::StateResidencyConfig &stateConfig,
31 const std::vector<std::pair<std::string, std::string>> &stateHeaders) {
32 std::vector<GenericStateResidencyDataProvider::StateResidencyConfig> stateResidencyConfigs;
33 stateResidencyConfigs.reserve(stateHeaders.size());
34 for (auto h : stateHeaders) {
35 GenericStateResidencyDataProvider::StateResidencyConfig cfg = {stateConfig};
36 cfg.name = h.first;
37 cfg.header = h.second;
38 stateResidencyConfigs.emplace_back(cfg);
39 }
40 return stateResidencyConfigs;
41 }
42
extractStat(const char * line,const std::string & prefix,uint64_t * stat)43 static bool extractStat(const char *line, const std::string &prefix, uint64_t *stat) {
44 char const *prefixStart = strstr(line, prefix.c_str());
45 if (prefixStart == nullptr) {
46 // Did not find the given prefix
47 return false;
48 }
49
50 *stat = strtoull(prefixStart + prefix.length(), nullptr, 0);
51 return true;
52 }
53
parseState(StateResidency * data,const GenericStateResidencyDataProvider::StateResidencyConfig & config,FILE * fp,char ** line,size_t * len)54 static bool parseState(StateResidency *data,
55 const GenericStateResidencyDataProvider::StateResidencyConfig &config,
56 FILE *fp, char **line, size_t *len) {
57 size_t numFieldsRead = 0;
58 const size_t numFields =
59 config.entryCountSupported + config.totalTimeSupported + config.lastEntrySupported;
60
61 while ((numFieldsRead < numFields) && (getline(line, len, fp) != -1)) {
62 uint64_t stat = 0;
63 // Attempt to extract data from the current line
64 if (config.entryCountSupported && extractStat(*line, config.entryCountPrefix, &stat)) {
65 data->totalStateEntryCount =
66 config.entryCountTransform ? config.entryCountTransform(stat) : stat;
67 ++numFieldsRead;
68 } else if (config.totalTimeSupported && extractStat(*line, config.totalTimePrefix, &stat)) {
69 data->totalTimeInStateMs =
70 config.totalTimeTransform ? config.totalTimeTransform(stat) : stat;
71 ++numFieldsRead;
72 } else if (config.lastEntrySupported && extractStat(*line, config.lastEntryPrefix, &stat)) {
73 data->lastEntryTimestampMs =
74 config.lastEntryTransform ? config.lastEntryTransform(stat) : stat;
75 ++numFieldsRead;
76 }
77 }
78
79 // End of file was reached and not all state data was parsed. Something
80 // went wrong
81 if (numFieldsRead != numFields) {
82 LOG(ERROR) << "Failed to parse stats for " << config.name;
83 return false;
84 }
85
86 return true;
87 }
88
89 template <class T, class Func>
findNextIndex(const std::vector<T> & collection,FILE * fp,char ** line,size_t * len,Func pred)90 static int32_t findNextIndex(const std::vector<T> &collection, FILE *fp, char **line, size_t *len,
91 Func pred) {
92 // handling the case when there is no header to look for
93 if (pred(collection[0], "")) {
94 return 0;
95 }
96
97 while (getline(line, len, fp) != -1) {
98 for (int32_t i = 0; i < collection.size(); ++i) {
99 if (pred(collection[i], *line)) {
100 return i;
101 }
102 }
103 }
104
105 return -1;
106 }
107
getStateData(std::vector<StateResidency> * result,const std::vector<GenericStateResidencyDataProvider::StateResidencyConfig> & stateResidencyConfigs,FILE * fp,char ** line,size_t * len)108 static bool getStateData(std::vector<StateResidency> *result,
109 const std::vector<GenericStateResidencyDataProvider::StateResidencyConfig>
110 &stateResidencyConfigs,
111 FILE *fp, char **line, size_t *len) {
112 size_t numStatesRead = 0;
113 size_t numStates = stateResidencyConfigs.size();
114 int32_t nextState = -1;
115 auto pred = [](auto a, char const *b) {
116 // return true if b matches the header contained in a, ignoring whitespace
117 return (a.header == ::android::base::Trim(std::string(b)));
118 };
119
120 result->reserve(numStates);
121
122 // Search for state headers until we have found them all or can't find anymore
123 while ((numStatesRead < numStates) &&
124 (nextState = findNextIndex<GenericStateResidencyDataProvider::StateResidencyConfig>(
125 stateResidencyConfigs, fp, line, len, pred)) >= 0) {
126 // Found a matching state header. Parse the contents
127 StateResidency data = {.id = nextState};
128 if (parseState(&data, stateResidencyConfigs[nextState], fp, line, len)) {
129 result->emplace_back(data);
130 ++numStatesRead;
131 } else {
132 break;
133 }
134 }
135
136 // There was a problem parsing and we failed to get data for all of the states
137 if (numStatesRead != numStates) {
138 return false;
139 }
140
141 return true;
142 }
143
getStateResidencies(std::unordered_map<std::string,std::vector<StateResidency>> * residencies)144 bool GenericStateResidencyDataProvider::getStateResidencies(
145 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
146 // Using FILE* instead of std::ifstream for performance reasons
147 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
148 if (!fp) {
149 PLOG(ERROR) << "Failed to open file " << mPath;
150 return false;
151 }
152
153 size_t len = 0;
154 char *line = nullptr;
155 size_t numEntitiesRead = 0;
156 size_t numEntities = mPowerEntityConfigs.size();
157 int32_t nextConfig = -1;
158 auto pred = [](auto a, char const *b) {
159 // return true if b matches the header contained in a, ignoring whitespace
160 return (a.mHeader == ::android::base::Trim(std::string(b)));
161 };
162
163 // Search for entity headers until we have found them all or can't find anymore
164 while ((numEntitiesRead < numEntities) &&
165 (nextConfig = findNextIndex<decltype(mPowerEntityConfigs)::value_type>(
166 mPowerEntityConfigs, fp.get(), &line, &len, pred)) >= 0) {
167 // Found a matching header. Retrieve its state data
168 std::vector<StateResidency> result;
169 if (getStateData(&result, mPowerEntityConfigs[nextConfig].mStateResidencyConfigs, fp.get(),
170 &line, &len)) {
171 residencies->emplace(mPowerEntityConfigs[nextConfig].mName, result);
172 ++numEntitiesRead;
173 } else {
174 break;
175 }
176 }
177
178 free(line);
179
180 // There was a problem gathering state residency data for one or more entities
181 if (numEntitiesRead != numEntities) {
182 LOG(ERROR) << "Failed to get results for " << mPath;
183 return false;
184 }
185
186 return true;
187 }
188
getInfo()189 std::unordered_map<std::string, std::vector<State>> GenericStateResidencyDataProvider::getInfo() {
190 std::unordered_map<std::string, std::vector<State>> ret;
191 for (const auto &entityConfig : mPowerEntityConfigs) {
192 int32_t stateId = 0;
193 std::vector<State> stateInfos;
194 for (const auto &stateConfig : entityConfig.mStateResidencyConfigs) {
195 State stateInfo = {.id = stateId++, .name = stateConfig.name};
196 stateInfos.emplace_back(stateInfo);
197 }
198
199 ret.emplace(entityConfig.mName, stateInfos);
200 }
201 return ret;
202 }
203
204 } // namespace stats
205 } // namespace power
206 } // namespace hardware
207 } // namespace android
208 } // namespace aidl
209