Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 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 | */ |
Benjamin Schwartz | 7f27af7 | 2020-09-08 16:44:02 -0700 | [diff] [blame] | 16 | // TODO(b/167628903): Delete this file |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 17 | #define LOG_TAG "libpixelpowerstats" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Benjamin Schwartz | f161635 | 2019-03-26 15:07:02 -0700 | [diff] [blame] | 20 | #include <android-base/strings.h> |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 21 | #include <pixelpowerstats/GenericStateResidencyDataProvider.h> |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 22 | #include <pixelpowerstats/PowerStatsUtils.h> |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 23 | |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 24 | #include <cstdio> |
| 25 | #include <cstring> |
Benjamin Schwartz | f161635 | 2019-03-26 15:07:02 -0700 | [diff] [blame] | 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include <unordered_map> |
| 29 | #include <utility> |
| 30 | #include <vector> |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace google { |
| 35 | namespace pixel { |
| 36 | namespace powerstats { |
| 37 | |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 38 | std::vector<StateResidencyConfig> generateGenericStateResidencyConfigs( |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 39 | const StateResidencyConfig &stateConfig, |
| 40 | const std::vector<std::pair<std::string, std::string>> &stateHeaders) { |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 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 | |
| 52 | PowerEntityConfig::PowerEntityConfig(const std::vector<StateResidencyConfig> &stateResidencyConfigs) |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 53 | : PowerEntityConfig("", stateResidencyConfigs) {} |
| 54 | |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 55 | PowerEntityConfig::PowerEntityConfig(const std::string &header, |
| 56 | const std::vector<StateResidencyConfig> &stateResidencyConfigs) |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 57 | : PowerEntityConfig(0, header, stateResidencyConfigs) {} |
| 58 | |
| 59 | PowerEntityConfig::PowerEntityConfig(const uint32_t start_id, const std::string &header, |
| 60 | const std::vector<StateResidencyConfig> &stateResidencyConfigs) |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 61 | : mHeader(header) { |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 62 | mStateResidencyConfigs.reserve(stateResidencyConfigs.size()); |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 63 | for (uint32_t i = start_id; i < start_id + stateResidencyConfigs.size(); ++i) { |
| 64 | mStateResidencyConfigs.emplace_back(i, stateResidencyConfigs[i - start_id]); |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 68 | static bool parseState(PowerEntityStateResidencyData *data, const StateResidencyConfig &config, |
| 69 | FILE *fp, char **line, size_t *len) { |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 70 | size_t numFieldsRead = 0; |
| 71 | const size_t numFields = |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 72 | config.entryCountSupported + config.totalTimeSupported + config.lastEntrySupported; |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 73 | |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 74 | while ((numFieldsRead < numFields) && (getline(line, len, fp) != -1)) { |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 75 | uint64_t stat = 0; |
| 76 | // Attempt to extract data from the current line |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 77 | if (config.entryCountSupported && |
| 78 | utils::extractStat(*line, config.entryCountPrefix, stat)) { |
| 79 | data->totalStateEntryCount = |
| 80 | config.entryCountTransform ? config.entryCountTransform(stat) : stat; |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 81 | ++numFieldsRead; |
| 82 | } else if (config.totalTimeSupported && |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 83 | utils::extractStat(*line, config.totalTimePrefix, stat)) { |
| 84 | data->totalTimeInStateMs = |
| 85 | config.totalTimeTransform ? config.totalTimeTransform(stat) : stat; |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 86 | ++numFieldsRead; |
| 87 | } else if (config.lastEntrySupported && |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 88 | utils::extractStat(*line, config.lastEntryPrefix, stat)) { |
| 89 | data->lastEntryTimestampMs = |
| 90 | config.lastEntryTransform ? config.lastEntryTransform(stat) : stat; |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 91 | ++numFieldsRead; |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 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 | |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 105 | template <class T, class Func> |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 106 | static auto findNext(const std::vector<T> &collection, FILE *fp, char **line, size_t *len, |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 107 | Func pred) { |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 108 | // handling the case when there is no header to look for |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 109 | if (pred(collection.front(), "")) { |
| 110 | return collection.cbegin(); |
| 111 | } |
| 112 | |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 113 | while (getline(line, len, fp) != -1) { |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 114 | for (auto it = collection.cbegin(); it != collection.cend(); ++it) { |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 115 | if (pred(*it, *line)) { |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 116 | return it; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return collection.cend(); |
| 122 | } |
| 123 | |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 124 | static bool getStateData( |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 125 | PowerEntityStateResidencyResult *result, |
| 126 | const std::vector<std::pair<uint32_t, StateResidencyConfig>> &stateResidencyConfigs, |
| 127 | FILE *fp, char **line, size_t *len) { |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 128 | size_t numStatesRead = 0; |
| 129 | size_t numStates = stateResidencyConfigs.size(); |
| 130 | auto nextState = stateResidencyConfigs.cbegin(); |
| 131 | auto endState = stateResidencyConfigs.cend(); |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 132 | auto pred = [](auto a, char const *b) { |
| 133 | // return true if b matches the header contained in a, ignoring whitespace |
Benjamin Schwartz | f161635 | 2019-03-26 15:07:02 -0700 | [diff] [blame] | 134 | return (a.second.header == android::base::Trim(std::string(b))); |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 135 | }; |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 136 | |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 137 | result->stateResidencyData.resize(numStates); |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 138 | |
| 139 | // Search for state headers until we have found them all or can't find anymore |
| 140 | while ((numStatesRead < numStates) && |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 141 | (nextState = findNext<std::pair<uint32_t, StateResidencyConfig>>( |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 142 | stateResidencyConfigs, fp, line, len, pred)) != endState) { |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 143 | // Found a matching state header. Parse the contents |
| 144 | PowerEntityStateResidencyData data = {.powerEntityStateId = nextState->first}; |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 145 | if (parseState(&data, nextState->second, fp, line, len)) { |
| 146 | result->stateResidencyData[numStatesRead] = data; |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 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 | |
| 161 | bool GenericStateResidencyDataProvider::getResults( |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 162 | std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) { |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 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); |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 171 | size_t len = 0; |
| 172 | char *line = nullptr; |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 173 | size_t numEntitiesRead = 0; |
| 174 | size_t numEntities = mPowerEntityConfigs.size(); |
| 175 | auto nextConfig = mPowerEntityConfigs.cbegin(); |
| 176 | auto endConfig = mPowerEntityConfigs.cend(); |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 177 | auto pred = [](auto a, char const *b) { |
| 178 | // return true if b matches the header contained in a, ignoring whitespace |
Benjamin Schwartz | f161635 | 2019-03-26 15:07:02 -0700 | [diff] [blame] | 179 | return (a.second.mHeader == android::base::Trim(std::string(b))); |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 180 | }; |
Sujee Rajayogam | 7088881 | 2020-05-13 12:53:51 -0700 | [diff] [blame] | 181 | bool skipFindNext = false; |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 182 | |
| 183 | // Search for entity headers until we have found them all or can't find anymore |
| 184 | while ((numEntitiesRead < numEntities) && |
Sujee Rajayogam | 7088881 | 2020-05-13 12:53:51 -0700 | [diff] [blame] | 185 | (skipFindNext || |
| 186 | (nextConfig = findNext<decltype(mPowerEntityConfigs)::value_type>( |
| 187 | mPowerEntityConfigs, fp.get(), &line, &len, pred)) != endConfig)) { |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 188 | // Found a matching header. Retrieve its state data |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 189 | PowerEntityStateResidencyResult result = {.powerEntityId = nextConfig->first}; |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 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 | } |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 205 | ++numEntitiesRead; |
| 206 | } else { |
| 207 | break; |
| 208 | } |
Sujee Rajayogam | 7088881 | 2020-05-13 12:53:51 -0700 | [diff] [blame] | 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 | } |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 221 | free(line); |
| 222 | |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 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 | |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 232 | void GenericStateResidencyDataProvider::addEntity(uint32_t id, const PowerEntityConfig &config) { |
| 233 | mPowerEntityConfigs.emplace_back(id, config); |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | std::vector<PowerEntityStateSpace> GenericStateResidencyDataProvider::getStateSpaces() { |
| 237 | std::vector<PowerEntityStateSpace> stateSpaces; |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 238 | stateSpaces.reserve(mPowerEntityConfigs.size()); |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 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] = { |
Sujee Rajayogam | e78733b | 2020-04-15 13:58:51 -0700 | [diff] [blame] | 245 | .powerEntityStateId = config.second.mStateResidencyConfigs[i].first, |
| 246 | .powerEntityStateName = config.second.mStateResidencyConfigs[i].second.name}; |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 247 | } |
Benjamin Schwartz | a991ef5 | 2018-12-04 13:48:52 -0800 | [diff] [blame] | 248 | stateSpaces.emplace_back(s); |
Benjamin Schwartz | 6d14170 | 2018-11-14 11:56:43 -0800 | [diff] [blame] | 249 | } |
| 250 | return stateSpaces; |
| 251 | } |
| 252 | |
| 253 | } // namespace powerstats |
| 254 | } // namespace pixel |
| 255 | } // namespace google |
| 256 | } // namespace hardware |
Benjamin Schwartz | f161635 | 2019-03-26 15:07:02 -0700 | [diff] [blame] | 257 | } // namespace android |