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