blob: aec77241520939e3b6c52dc5bf8734491f166ddb [file] [log] [blame]
Darren Hsu673d3b02021-12-28 21:31:24 +08001/*
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
25using android::base::ParseInt;
26using android::base::Split;
27using android::base::StartsWith;
28using android::base::Trim;
29
30namespace aidl {
31namespace android {
32namespace hardware {
33namespace power {
34namespace stats {
35
36const int32_t HIBERNATE_STATE_ID = 0;
37const std::string UFS_NAME = "UFS";
38
39UfsStateResidencyDataProvider::UfsStateResidencyDataProvider(std::string prefix) : kPrefix(prefix) {}
40
41bool 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
57std::unordered_map<std::string, std::vector<State>> UfsStateResidencyDataProvider::getInfo() {
58 return {{UFS_NAME, std::vector<State>{{HIBERNATE_STATE_ID, "HIBERN8"}} }};
59}
60
61int64_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