Darren Hsu | 673d3b0 | 2021-12-28 21:31:24 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "UfsStateResidencyDataProvider.h" |
| 17 | |
| 18 | #include <android-base/logging.h> |
| 19 | #include <android-base/parseint.h> |
| 20 | #include <android-base/strings.h> |
| 21 | |
| 22 | #include <string> |
| 23 | #include <utility> |
| 24 | |
| 25 | using android::base::ParseInt; |
| 26 | using android::base::Split; |
| 27 | using android::base::StartsWith; |
| 28 | using android::base::Trim; |
| 29 | |
| 30 | namespace aidl { |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace power { |
| 34 | namespace stats { |
| 35 | |
| 36 | const int32_t HIBERNATE_STATE_ID = 0; |
| 37 | const std::string UFS_NAME = "UFS"; |
| 38 | |
| 39 | UfsStateResidencyDataProvider::UfsStateResidencyDataProvider(std::string prefix) : kPrefix(prefix) {} |
| 40 | |
| 41 | bool UfsStateResidencyDataProvider::getStateResidencies( |
| 42 | std::unordered_map<std::string, std::vector<StateResidency>> *residencies) { |
| 43 | StateResidency residency; |
| 44 | residency.id = HIBERNATE_STATE_ID; |
| 45 | |
| 46 | // The transform function converts microseconds to milliseconds. |
| 47 | std::function<uint64_t(uint64_t)> usecToMs = [](uint64_t a) { return a / 1000; }; |
| 48 | |
| 49 | residency.totalTimeInStateMs = usecToMs(readStat(kPrefix + "hibern8_total_us")); |
| 50 | residency.totalStateEntryCount = readStat(kPrefix + "hibern8_exit_cnt"); |
| 51 | residency.lastEntryTimestampMs = usecToMs(readStat(kPrefix + "last_hibern8_enter_time")); |
| 52 | |
| 53 | residencies->emplace(UFS_NAME, std::vector<StateResidency>{residency}); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | std::unordered_map<std::string, std::vector<State>> UfsStateResidencyDataProvider::getInfo() { |
| 58 | return {{UFS_NAME, std::vector<State>{{HIBERNATE_STATE_ID, "HIBERN8"}} }}; |
| 59 | } |
| 60 | |
| 61 | int64_t UfsStateResidencyDataProvider::readStat(std::string path) { |
| 62 | std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(path.c_str(), "r"), fclose); |
| 63 | if (!fp) { |
| 64 | PLOG(ERROR) << __func__ << ":Failed to open file " << path |
| 65 | << " Error = " << strerror(errno); |
| 66 | return 0; |
| 67 | } |
| 68 | const size_t size = 20; |
| 69 | char buf[size]; |
| 70 | (void)fread(&buf, sizeof(char), size, fp.get()); |
| 71 | int64_t ret; |
| 72 | if (!ParseInt(Trim(std::string(buf)), &ret)) { |
| 73 | LOG(ERROR) << "Failed to parse int64 from [" << std::string(buf) << "]"; |
| 74 | } |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | } // namespace stats |
| 79 | } // namespace power |
| 80 | } // namespace hardware |
| 81 | } // namespace android |
| 82 | } // namespace aidl |