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
17 #define LOG_TAG "pixelstats: PowerMitigationStats"
18
19 #include <aidl/android/frameworks/stats/IStats.h>
20 #include <android-base/file.h>
21 #include <android-base/parseint.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <android/binder_manager.h>
26 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
27 #include <pixelstats/MitigationStatsReporter.h>
28 #include <utils/Log.h>
29
30 namespace android {
31 namespace hardware {
32 namespace google {
33 namespace pixel {
34
35 using aidl::android::frameworks::stats::IStats;
36 using aidl::android::frameworks::stats::VendorAtom;
37 using aidl::android::frameworks::stats::VendorAtomValue;
38 using android::base::ReadFileToString;
39 using android::hardware::google::pixel::PixelAtoms::PowerMitigationStats;
40
MitigationStatsReporter()41 MitigationStatsReporter::MitigationStatsReporter() {}
42
ReadFileToInt(const std::string & path,int * val)43 bool MitigationStatsReporter::ReadFileToInt(const std::string &path, int *val) {
44 std::string file_contents;
45
46 if (!ReadFileToString(path.c_str(), &file_contents)) {
47 ALOGI("Unable to read %s - %s", path.c_str(), strerror(errno));
48 return false;
49 } else {
50 file_contents = android::base::Trim(file_contents);
51 if (!android::base::ParseInt(file_contents, val)) {
52 ALOGI("Unable to convert %s to int - %s", path.c_str(), strerror(errno));
53 return false;
54 }
55 }
56 return true;
57 }
58
logMitigationStatsPerHour(const std::shared_ptr<IStats> & stats_client,const std::string & path)59 void MitigationStatsReporter::logMitigationStatsPerHour(const std::shared_ptr<IStats> &stats_client,
60 const std::string &path) {
61 struct MitigationCount last_count = {};
62 struct MitigationCap last_cap = {};
63
64 if (!logMitigationCount(path, &last_count))
65 return;
66 logMitigationCap(path, &last_cap);
67
68 VendorAtomValue tmp;
69 std::vector<VendorAtomValue> values(24);
70 tmp.set<VendorAtomValue::intValue>(last_count.batoilo_count - prev_count.batoilo_count);
71 values[PowerMitigationStats::kBatoiloCountFieldNumber - kVendorAtomOffset] = tmp;
72 tmp.set<VendorAtomValue::intValue>(last_count.vdroop1_count - prev_count.vdroop1_count);
73 values[PowerMitigationStats::kVdroop1CountFieldNumber - kVendorAtomOffset] = tmp;
74 tmp.set<VendorAtomValue::intValue>(last_count.vdroop2_count - prev_count.vdroop2_count);
75 values[PowerMitigationStats::kVdroop2CountFieldNumber - kVendorAtomOffset] = tmp;
76 tmp.set<VendorAtomValue::intValue>(last_count.smpl_warn_count - prev_count.smpl_warn_count);
77 values[PowerMitigationStats::kSmplWarnCountFieldNumber - kVendorAtomOffset] = tmp;
78 tmp.set<VendorAtomValue::intValue>(last_count.ocp_cpu1_count - prev_count.ocp_cpu1_count);
79 values[PowerMitigationStats::kOcpCpu1CountFieldNumber - kVendorAtomOffset] = tmp;
80 tmp.set<VendorAtomValue::intValue>(last_count.ocp_cpu2_count - prev_count.ocp_cpu2_count);
81 values[PowerMitigationStats::kOcpCpu2CountFieldNumber - kVendorAtomOffset] = tmp;
82 tmp.set<VendorAtomValue::intValue>(last_count.ocp_gpu_count - prev_count.ocp_gpu_count);
83 values[PowerMitigationStats::kOcpGpuCountFieldNumber - kVendorAtomOffset] = tmp;
84 tmp.set<VendorAtomValue::intValue>(last_count.ocp_tpu_count - prev_count.ocp_tpu_count);
85 values[PowerMitigationStats::kOcpTpuCountFieldNumber - kVendorAtomOffset] = tmp;
86 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_cpu1_count -
87 prev_count.soft_ocp_cpu1_count);
88 values[PowerMitigationStats::kSoftOcpCpu1CountFieldNumber - kVendorAtomOffset] = tmp;
89 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_cpu2_count -
90 prev_count.soft_ocp_cpu2_count);
91 values[PowerMitigationStats::kSoftOcpCpu2CountFieldNumber - kVendorAtomOffset] = tmp;
92 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_gpu_count -
93 prev_count.soft_ocp_gpu_count);
94 values[PowerMitigationStats::kSoftOcpGpuCountFieldNumber - kVendorAtomOffset] = tmp;
95 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_tpu_count -
96 prev_count.soft_ocp_tpu_count);
97 values[PowerMitigationStats::kSoftOcpTpuCountFieldNumber - kVendorAtomOffset] = tmp;
98 tmp.set<VendorAtomValue::intValue>(last_cap.batoilo_cap);
99 values[PowerMitigationStats::kBatoiloCapFieldNumber - kVendorAtomOffset] = tmp;
100 tmp.set<VendorAtomValue::intValue>(last_cap.vdroop1_cap);
101 values[PowerMitigationStats::kVdroop1CapFieldNumber - kVendorAtomOffset] = tmp;
102 tmp.set<VendorAtomValue::intValue>(last_cap.vdroop2_cap);
103 values[PowerMitigationStats::kVdroop2CapFieldNumber - kVendorAtomOffset] = tmp;
104 tmp.set<VendorAtomValue::intValue>(last_cap.smpl_warn_cap);
105 values[PowerMitigationStats::kSmplWarnCapFieldNumber - kVendorAtomOffset] = tmp;
106 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_cpu1_cap);
107 values[PowerMitigationStats::kOcpCpu1CapFieldNumber - kVendorAtomOffset] = tmp;
108 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_cpu2_cap);
109 values[PowerMitigationStats::kOcpCpu2CapFieldNumber - kVendorAtomOffset] = tmp;
110 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_gpu_cap);
111 values[PowerMitigationStats::kOcpGpuCapFieldNumber - kVendorAtomOffset] = tmp;
112 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_tpu_cap);
113 values[PowerMitigationStats::kOcpTpuCapFieldNumber - kVendorAtomOffset] = tmp;
114 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_cpu1_cap);
115 values[PowerMitigationStats::kSoftOcpCpu1CapFieldNumber - kVendorAtomOffset] = tmp;
116 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_cpu2_cap);
117 values[PowerMitigationStats::kSoftOcpCpu2CapFieldNumber - kVendorAtomOffset] = tmp;
118 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_gpu_cap);
119 values[PowerMitigationStats::kSoftOcpGpuCapFieldNumber - kVendorAtomOffset] = tmp;
120 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_tpu_cap);
121 values[PowerMitigationStats::kSoftOcpTpuCapFieldNumber - kVendorAtomOffset] = tmp;
122
123 prev_count = last_count;
124 // Send vendor atom to IStats HAL
125 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
126 .atomId = PixelAtoms::Atom::kMitigationStats,
127 .values = std::move(values)};
128 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
129 if (!ret.isOk())
130 ALOGE("Unable to report to Stats service");
131 }
132
logMitigationCap(const std::string kMitigationDir,struct MitigationCap * last_cap)133 void MitigationStatsReporter::logMitigationCap(const std::string kMitigationDir,
134 struct MitigationCap *last_cap) {
135 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/batoilo_cap",
136 &(last_cap->batoilo_cap));
137 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_cpu1_cap",
138 &(last_cap->ocp_cpu1_cap));
139 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_cpu2_cap",
140 &(last_cap->ocp_cpu2_cap));
141 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_gpu_cap",
142 &(last_cap->ocp_gpu_cap));
143 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_tpu_cap",
144 &(last_cap->ocp_tpu_cap));
145 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/smpl_warn_cap",
146 &(last_cap->smpl_warn_cap));
147 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_cpu1_cap",
148 &(last_cap->soft_ocp_cpu1_cap));
149 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_cpu2_cap",
150 &(last_cap->soft_ocp_cpu2_cap));
151 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_gpu_cap",
152 &(last_cap->soft_ocp_gpu_cap));
153 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_tpu_cap",
154 &(last_cap->soft_ocp_tpu_cap));
155 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/vdroop1_cap",
156 &(last_cap->vdroop1_cap));
157 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/vdroop2_cap",
158 &(last_cap->vdroop2_cap));
159 }
160
logMitigationCount(const std::string kMitigationDir,struct MitigationCount * last_count)161 bool MitigationStatsReporter::logMitigationCount(const std::string kMitigationDir,
162 struct MitigationCount *last_count) {
163 bool send_stats = false;
164 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/batoilo_count",
165 &(last_count->batoilo_count)))
166 return false;
167 send_stats |= (last_count->batoilo_count - prev_count.batoilo_count) > 0;
168 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_cpu1_count",
169 &(last_count->ocp_cpu1_count)))
170 return false;
171 send_stats |= (last_count->ocp_cpu1_count - prev_count.ocp_cpu1_count) > 0;
172 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_cpu2_count",
173 &(last_count->ocp_cpu2_count)))
174 return false;
175 send_stats |= (last_count->ocp_cpu2_count - prev_count.ocp_cpu2_count) > 0;
176 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_gpu_count",
177 &(last_count->ocp_gpu_count)))
178 return false;
179 send_stats |= (last_count->ocp_gpu_count - prev_count.ocp_gpu_count) > 0;
180 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_tpu_count",
181 &(last_count->ocp_tpu_count)))
182 return false;
183 send_stats |= (last_count->ocp_tpu_count - prev_count.ocp_tpu_count) > 0;
184 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/smpl_warn_count",
185 &(last_count->smpl_warn_count)))
186 return false;
187 send_stats |= (last_count->smpl_warn_count - prev_count.smpl_warn_count) > 0;
188 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_cpu1_count",
189 &(last_count->soft_ocp_cpu1_count)))
190 return false;
191 send_stats |= (last_count->soft_ocp_cpu1_count - prev_count.soft_ocp_cpu1_count) > 0;
192 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_cpu2_count",
193 &(last_count->soft_ocp_cpu2_count)))
194 return false;
195 send_stats |= (last_count->soft_ocp_cpu2_count - prev_count.soft_ocp_cpu2_count) > 0;
196 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_gpu_count",
197 &(last_count->soft_ocp_gpu_count)))
198 return false;
199 send_stats |= (last_count->soft_ocp_gpu_count - prev_count.soft_ocp_gpu_count) > 0;
200 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_tpu_count",
201 &(last_count->soft_ocp_tpu_count)))
202 return false;
203 send_stats |= (last_count->soft_ocp_tpu_count - prev_count.soft_ocp_tpu_count) > 0;
204 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/vdroop1_count",
205 &(last_count->vdroop1_count)))
206 return false;
207 send_stats |= (last_count->vdroop1_count - prev_count.vdroop1_count) > 0;
208 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/vdroop2_count",
209 &(last_count->vdroop2_count)))
210 return false;
211 send_stats |= (last_count->vdroop2_count - prev_count.vdroop2_count) > 0;
212 return send_stats;
213 }
214
215 } // namespace pixel
216 } // namespace google
217 } // namespace hardware
218 } // namespace android
219