1 /*
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 "TpuDvfsStateResidencyDataProvider.h"
17 
18 #include <android-base/logging.h>
19 
20 static const std::string ENTITY_NAME = "TPU-DVFS";
21 
22 namespace aidl {
23 namespace android {
24 namespace hardware {
25 namespace power {
26 namespace stats {
27 
TpuDvfsStateResidencyDataProvider(const std::string & path,std::vector<std::string> frequencies,uint64_t clockRate)28 TpuDvfsStateResidencyDataProvider::TpuDvfsStateResidencyDataProvider(
29         const std::string& path,
30         std::vector<std::string> frequencies,
31         uint64_t clockRate)
32     : mPath(path), mFrequencies(std::move(frequencies)), mClockRate(clockRate) {}
33 
getStateResidencies(std::unordered_map<std::string,std::vector<StateResidency>> * residencies)34 bool TpuDvfsStateResidencyDataProvider::getStateResidencies(
35         std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
36     // Using FILE* instead of std::ifstream for performance reasons
37     std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
38     if (!fp) {
39         PLOG(ERROR) << "Failed to open file " << mPath;
40         return false;
41     }
42 
43     std::vector<StateResidency> stateResidencies;
44     for (int i = 0; i < mFrequencies.size(); i++) {
45         StateResidency s = {.id = i, .totalTimeInStateMs = 0};
46         stateResidencies.push_back(s);
47     }
48 
49     char *line = nullptr;
50     size_t len = 0;
51     std::istringstream ssLine;
52     std::string split;
53     int32_t lineIdx = 0;
54     std::vector<int32_t> stateIdxMap;
55     int32_t colIdx;
56     std::vector<std::string>::const_iterator found;
57     while (getline(&line, &len, fp.get()) != -1) {
58         ssLine.clear();
59         ssLine.str(line);
60         colIdx = 0;
61         for (std::string split; std::getline(ssLine, split, ' ');) {
62             // Skip first column
63             if (split.find(':') != std::string::npos)
64                 continue;
65 
66             if (lineIdx == 0) {
67                 // Build the state index map by first row
68                 split.erase(split.find_last_not_of(" \n\r\t") + 1);
69                 found = std::find(mFrequencies.begin(), mFrequencies.end(), split);
70                 if (found != mFrequencies.end()) {
71                     stateIdxMap.push_back(found - mFrequencies.begin());
72                 } else {
73                     PLOG(ERROR) << "TPU frequency " << split << " is not found in " << mPath;
74                     stateIdxMap.push_back(0);
75                 }
76             } else {
77                 // Add up time in frequency per uid
78                 stateResidencies[stateIdxMap[colIdx]].totalTimeInStateMs +=
79                         std::atoll(split.c_str()) / mClockRate;
80             }
81             colIdx++;
82         }
83         lineIdx++;
84     }
85 
86     residencies->emplace(ENTITY_NAME, stateResidencies);
87     return true;
88 }
89 
getInfo()90 std::unordered_map<std::string, std::vector<State>> TpuDvfsStateResidencyDataProvider::getInfo() {
91     std::vector<State> states;
92     for (int32_t id = 0; id < mFrequencies.size(); id++) {
93         State s =
94             {.id = id, .name = std::to_string(std::atol(mFrequencies[id].c_str()) / 1000) + "MHz"};
95         states.push_back(s);
96     }
97 
98     return {{ENTITY_NAME, states}};
99 }
100 
101 }  // namespace stats
102 }  // namespace power
103 }  // namespace hardware
104 }  // namespace android
105 }  // namespace aidl
106