blob: 6e377aa237bd739678e03a3457d3951a25efbcc7 [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
65 mPowerEntities.push_back({powerEntities[i].first, std::move(states)});
Darren Hsu5f322222023-03-13 11:11:55 +080066 }
67
Darren Hsu529b6182023-03-14 14:31:43 +080068 free(line);
Darren Hsu5f322222023-03-13 11:11:55 +080069}
70
71bool AdaptiveDvfsStateResidencyDataProvider::getStateResidencies(
72 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
73 return DvfsStateResidencyDataProvider::getStateResidencies(residencies);
74}
75
76std::unordered_map<std::string, std::vector<State>>
77 AdaptiveDvfsStateResidencyDataProvider::getInfo() {
78 return DvfsStateResidencyDataProvider::getInfo();
79}
80
81} // namespace stats
82} // namespace power
83} // namespace hardware
84} // namespace android
85} // namespace aidl