1 /*
2 * Copyright (C) 2020 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
17 #include <dataproviders/PowerStatsEnergyAttribution.h>
18
19 #include <android-base/logging.h>
20 #include <android-base/parseint.h>
21 #include <android-base/strings.h>
22
23 #include <utility>
24
25 using android::base::Split;
26 using android::base::Trim;
27
28 namespace aidl {
29 namespace android {
30 namespace hardware {
31 namespace power {
32 namespace stats {
33
readUidTimeInState(AttributionStats * attrStats,std::string path)34 bool PowerStatsEnergyAttribution::readUidTimeInState(AttributionStats *attrStats,
35 std::string path) {
36 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(path.c_str(), "r"), fclose);
37 if (!fp) {
38 PLOG(ERROR) << __func__ << ":Failed to open file " << path;
39 return false;
40 }
41
42 char *buf = nullptr;
43 size_t size = 0;
44 getline(&buf, &size, fp.get());
45 attrStats->uidTimeInStateNames = Split(Trim(buf), " ");
46 // first element will be "uid:" and it's useless
47 attrStats->uidTimeInStateNames.erase(attrStats->uidTimeInStateNames.begin());
48
49 while (getline(&buf, &size, fp.get()) != -1) {
50 std::vector<std::string> uidInfos = Split(Trim(buf), " ");
51 uidInfos[0].pop_back();
52 int32_t uid;
53 if (!::android::base::ParseInt(uidInfos[0], &uid)) {
54 PLOG(ERROR) << __func__ << "Failed to parse uid from " << path;
55 free(buf);
56 return false;
57 }
58
59 std::vector<long> uidStats;
60 // first element will be uid number so skipping it
61 for (auto it = ++uidInfos.begin(); it != uidInfos.end(); ++it) {
62 long uidStat;
63 if (!::android::base::ParseInt(*it, &uidStat)) {
64 PLOG(ERROR) << __func__ << "Failed to parse uidStat from " << path;
65 free(buf);
66 return false;
67 }
68 uidStats.push_back(uidStat);
69 }
70 attrStats->uidTimeInStats.emplace(uid, uidStats);
71 }
72
73 free(buf);
74 return true;
75 }
76
getAttributionStats(std::unordered_map<int32_t,std::string> paths)77 AttributionStats PowerStatsEnergyAttribution::getAttributionStats(
78 std::unordered_map<int32_t,
79 std::string> paths) {
80 AttributionStats attrStats;
81
82 if (paths.count(UID_TIME_IN_STATE) &&
83 !readUidTimeInState(&attrStats, paths.at(UID_TIME_IN_STATE))) {
84 PLOG(ERROR) << ":Failed to read uid_time_in_state";
85 return {};
86 }
87
88 return attrStats;
89 }
90
91 } // namespace stats
92 } // namespace power
93 } // namespace hardware
94 } // namespace android
95 } // namespace aidl
96