Darren Hsu | 40b75d5 | 2023-12-20 20:12:32 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #include "DisplayMrrStateResidencyDataProvider.h" |
| 17 | |
| 18 | #include <android-base/logging.h> |
| 19 | #include <android-base/parseint.h> |
| 20 | #include <android-base/strings.h> |
| 21 | |
| 22 | using android::base::ParseInt; |
| 23 | using android::base::ParseUint; |
| 24 | using android::base::Split; |
| 25 | using android::base::Trim; |
| 26 | |
| 27 | static const std::string TIME_IN_STATE = "time_in_state"; |
| 28 | static const std::string AVAILABLE_STATE = "available_disp_stats"; |
| 29 | static const std::vector<std::string> DISP_STATE = { "On", "HBM", "LP", "Off" }; |
| 30 | |
| 31 | namespace aidl { |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace power { |
| 35 | namespace stats { |
| 36 | |
| 37 | DisplayMrrStateResidencyDataProvider::DisplayMrrStateResidencyDataProvider( |
| 38 | const std::string& name, const std::string& path) : mName(name), mPath(path) { |
| 39 | mConfigs = std::vector<Config>(); |
| 40 | std::string statePath = mPath + AVAILABLE_STATE; |
| 41 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(statePath.c_str(), "r"), fclose); |
| 42 | if (fp) { |
| 43 | char *line = nullptr; |
| 44 | size_t len = 0; |
| 45 | Config config = { .state = 0, .resX = 0, .resY = 0, .rr = 0 }; |
| 46 | while (getline(&line, &len, fp.get()) != -1) { |
| 47 | if (parseAvailableState(line, &config)) { |
| 48 | mConfigs.push_back(config); |
| 49 | } else { |
| 50 | PLOG(ERROR) << "Failed to parse display config for [" << std::string(line) |
| 51 | << "] from " << statePath; |
| 52 | mConfigs.clear(); |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | free(line); |
| 57 | } else { |
| 58 | PLOG(ERROR) << "Failed to open file " << statePath; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool DisplayMrrStateResidencyDataProvider::parseConfig( |
| 63 | char const *line, Config *config, uint64_t *duration) { |
| 64 | std::vector<std::string> parts = Split(line, " "); |
| 65 | |
| 66 | if (duration == nullptr) { |
| 67 | if (parts.size() != 4) return false; |
| 68 | } else { |
| 69 | if (parts.size() != 5) return false; |
| 70 | |
| 71 | if (!ParseUint(Trim(parts[4]), duration)) return false; |
| 72 | } |
| 73 | |
| 74 | if (!ParseInt(Trim(parts[0]), &config->state)) return false; |
| 75 | if (!ParseInt(Trim(parts[1]), &config->resX)) return false; |
| 76 | if (!ParseInt(Trim(parts[2]), &config->resY)) return false; |
| 77 | if (!ParseInt(Trim(parts[3]), &config->rr)) return false; |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool DisplayMrrStateResidencyDataProvider::parseAvailableState( |
| 83 | char const *line, Config *config) { |
| 84 | return parseConfig(line, config, nullptr); |
| 85 | } |
| 86 | |
| 87 | bool DisplayMrrStateResidencyDataProvider::parseTimeInState( |
| 88 | char const *line, Config *config, uint64_t *duration) { |
| 89 | return parseConfig(line, config, duration); |
| 90 | } |
| 91 | |
| 92 | bool DisplayMrrStateResidencyDataProvider::getStateResidencies( |
| 93 | std::unordered_map<std::string, std::vector<StateResidency>> *residencies) { |
| 94 | if (mConfigs.empty()) { |
| 95 | LOG(ERROR) << "Display MRR state list is empty!"; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | std::string path = mPath + TIME_IN_STATE; |
| 100 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(path.c_str(), "r"), fclose); |
| 101 | if (!fp) { |
| 102 | PLOG(ERROR) << "Failed to open file " << path; |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | std::vector<StateResidency> stateResidencies; |
| 107 | for (int i = 0; i < mConfigs.size(); i++) { |
| 108 | StateResidency s = {.id = i, .totalTimeInStateMs = 0}; |
| 109 | stateResidencies.push_back(s); |
| 110 | } |
| 111 | |
| 112 | char *line = nullptr; |
| 113 | size_t len = 0; |
| 114 | Config config = { .state = 0, .resX = 0, .resY = 0, .rr = 0 }; |
| 115 | uint64_t duration; |
| 116 | std::vector<Config>::const_iterator found; |
| 117 | while (getline(&line, &len, fp.get()) != -1) { |
| 118 | if (parseTimeInState(line, &config, &duration)) { |
| 119 | found = std::find(mConfigs.begin(), mConfigs.end(), config); |
| 120 | if (found != mConfigs.end()) { |
| 121 | stateResidencies[found - mConfigs.begin()].totalTimeInStateMs = duration; |
| 122 | } else { |
| 123 | LOG(ERROR) << "Failed to find config for [" << std::string(line) |
| 124 | << "] in display MRR state list"; |
| 125 | } |
| 126 | } else { |
| 127 | LOG(ERROR) << "Failed to parse state and duration from [" << std::string(line) << "]"; |
| 128 | free(line); |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | residencies->emplace(mName, stateResidencies); |
| 134 | |
| 135 | free(line); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | std::unordered_map<std::string, std::vector<State>> DisplayMrrStateResidencyDataProvider::getInfo() |
| 141 | { |
| 142 | int32_t dispId; |
| 143 | std::string name; |
| 144 | std::vector<State> states; |
| 145 | for (int32_t id = 0; id < mConfigs.size(); id++) { |
| 146 | dispId = mConfigs[id].state; |
| 147 | if (dispId >= DISP_STATE.size()) { |
| 148 | LOG(ERROR) << "Display state id " << dispId << " is out of bound"; |
| 149 | return {}; |
| 150 | } |
| 151 | |
| 152 | name = DISP_STATE[dispId]; |
| 153 | if (dispId != DISP_STATE.size() - 1) { |
| 154 | name += ": " + std::to_string(mConfigs[id].resX) + |
| 155 | "x" + std::to_string(mConfigs[id].resY) + |
| 156 | "@" + std::to_string(mConfigs[id].rr); |
| 157 | } |
| 158 | State s = { .id = id, .name = name }; |
| 159 | states.push_back(s); |
| 160 | } |
| 161 | |
| 162 | return {{ mName, states }}; |
| 163 | } |
| 164 | |
| 165 | } // namespace stats |
| 166 | } // namespace power |
| 167 | } // namespace hardware |
| 168 | } // namespace android |
| 169 | } // namespace aidl |