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 "DevfreqStateResidencyDataProvider.h"
17 
18 #include <android-base/logging.h>
19 
20 static const std::string nameSuffix = "-DVFS";
21 static const std::string pathSuffix = "/time_in_state";
22 
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace power {
27 namespace stats {
28 
DevfreqStateResidencyDataProvider(const std::string & name,const std::string & path)29 DevfreqStateResidencyDataProvider::DevfreqStateResidencyDataProvider(const std::string& name,
30         const std::string& path) : mName(name + nameSuffix), mPath(path + pathSuffix) {}
31 
extractNum(const char * str,char ** str_end,int base,int64_t * num)32 bool DevfreqStateResidencyDataProvider::extractNum(const char *str, char **str_end, int base,
33         int64_t* num) {
34     // errno can be set to any non-zero value by a library function call
35     // regardless of whether there was an error, so it needs to be cleared
36     // in order to check the error set by strtoll
37     errno = 0;
38     *num = std::strtoll(str, str_end, base);
39     return (errno != ERANGE);
40 }
41 
parseTimeInState()42 std::vector<std::pair<int64_t, int64_t>> DevfreqStateResidencyDataProvider::parseTimeInState() {
43     // Using FILE* instead of std::ifstream for performance reasons
44     std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
45     if (!fp) {
46         PLOG(ERROR) << "Failed to open file " << mPath;
47         return {};
48     }
49 
50     std::vector<std::pair<int64_t, int64_t>> timeInState;
51 
52     char *line = nullptr;
53     size_t len = 0;
54     while (getline(&line, &len, fp.get()) != -1) {
55         char* pEnd;
56         int64_t frequencyHz, totalTimeMs;
57         if (!extractNum(line, &pEnd, 10, &frequencyHz) ||
58             !extractNum(pEnd, &pEnd, 10, &totalTimeMs)) {
59             PLOG(ERROR) << "Failed to parse " << mPath;
60             free(line);
61             return {};
62         }
63 
64         timeInState.push_back({frequencyHz, totalTimeMs});
65     }
66 
67     free(line);
68     return timeInState;
69 }
70 
getStateResidencies(std::unordered_map<std::string,std::vector<StateResidency>> * residencies)71 bool DevfreqStateResidencyDataProvider::getStateResidencies(
72         std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
73     std::vector<std::pair<int64_t, int64_t>> timeInState = parseTimeInState();
74 
75     if (timeInState.empty()) {
76         return false;
77     }
78 
79     int32_t id = 0;
80     std::vector<StateResidency> stateResidencies;
81     for (const auto[frequencyHz, totalTimeMs] : timeInState) {
82         StateResidency s = {.id = id++, .totalTimeInStateMs = totalTimeMs};
83         stateResidencies.push_back(s);
84     }
85 
86     residencies->emplace(mName, stateResidencies);
87     return true;
88 }
89 
getInfo()90 std::unordered_map<std::string, std::vector<State>> DevfreqStateResidencyDataProvider::getInfo() {
91     std::vector<std::pair<int64_t, int64_t>> timeInState = parseTimeInState();
92 
93     if (timeInState.empty()) {
94         return {};
95     }
96 
97     int32_t id = 0;
98     std::vector<State> states;
99     for (const auto[frequencyHz, totalTimeMs] : timeInState) {
100         State s = {.id = id++, .name = std::to_string(frequencyHz / 1000) + "MHz"};
101         states.push_back(s);
102     }
103 
104     return {{mName, states}};
105 }
106 
107 }  // namespace stats
108 }  // namespace power
109 }  // namespace hardware
110 }  // namespace android
111 }  // namespace aidl
112