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