blob: c842d3ec62f314a1b9e68ec39c30828101598b8d [file] [log] [blame]
Darren Hsu5f322222023-03-13 11:11:55 +08001/*
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 "AdaptiveDvfsStateResidencyDataProvider.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::Split;
26using android::base::Trim;
27
Darren Hsu529b6182023-03-14 14:31:43 +080028static const std::string pathSuffix = "/time_in_state";
29static const std::string stateSuffix = "MHz";
30
Darren Hsu5f322222023-03-13 11:11:55 +080031namespace aidl {
32namespace android {
33namespace hardware {
34namespace power {
35namespace stats {
36
37AdaptiveDvfsStateResidencyDataProvider::AdaptiveDvfsStateResidencyDataProvider(
38 std::string path,
39 uint64_t clockRate,
Darren Hsu529b6182023-03-14 14:31:43 +080040 std::vector<std::pair<std::string, std::string>> powerEntities)
Darren Hsu5f322222023-03-13 11:11:55 +080041 : DvfsStateResidencyDataProvider(path, clockRate, {}) {
Darren Hsu5f322222023-03-13 11:11:55 +080042 size_t len = 0;
43 char *line = nullptr;
Darren Hsu5f322222023-03-13 11:11:55 +080044 std::vector<std::pair<std::string, std::string>> states = {};
45 std::vector<std::string> parts;
Darren Hsu5f322222023-03-13 11:11:55 +080046
Darren Hsu529b6182023-03-14 14:31:43 +080047 for (int32_t i = 0; i < powerEntities.size(); i++) {
48 std::string freqPath = powerEntities[i].second + pathSuffix;
49 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(freqPath.c_str(), "r"), fclose);
50 if (!fp) {
51 PLOG(ERROR) << __func__ << ":Failed to open file " << freqPath;
52 continue;
Darren Hsu5f322222023-03-13 11:11:55 +080053 }
Darren Hsu529b6182023-03-14 14:31:43 +080054
55 while (getline(&line, &len, fp.get()) != -1) {
56 parts = Split(Trim(std::string(line)), " ");
57 if (parts.size() > 0) {
58 std::string freqStr = Trim(parts[0]);
59 states.push_back(std::make_pair(
60 freqStr.substr(0, freqStr.length() - 3) + stateSuffix,
61 freqStr));
62 }
63 }
64
Darren Hsue3e46d42023-12-22 23:34:42 +080065 // Cpufreq data is sorted in ascending order, but power stats are sorted
66 // in descending order. Reverse sorting to maintain consistency with
67 // other power stats.
68 if (states.size() > 1 &&
69 std::atoll(states[0].second.c_str()) < std::atoll(states[1].second.c_str())) {
70 std::reverse(states.begin(), states.end());
71 }
72
Darren Hsu529b6182023-03-14 14:31:43 +080073 mPowerEntities.push_back({powerEntities[i].first, std::move(states)});
Darren Hsu5f322222023-03-13 11:11:55 +080074 }
75
Darren Hsu529b6182023-03-14 14:31:43 +080076 free(line);
Darren Hsu5f322222023-03-13 11:11:55 +080077}
78
79bool AdaptiveDvfsStateResidencyDataProvider::getStateResidencies(
80 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
81 return DvfsStateResidencyDataProvider::getStateResidencies(residencies);
82}
83
84std::unordered_map<std::string, std::vector<State>>
85 AdaptiveDvfsStateResidencyDataProvider::getInfo() {
86 return DvfsStateResidencyDataProvider::getInfo();
87}
88
89} // namespace stats
90} // namespace power
91} // namespace hardware
92} // namespace android
93} // namespace aidl