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(26);
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.batoilo2_count - prev_count.batoilo2_count);
73 values[PowerMitigationStats::kBatoilo2CountFieldNumber - kVendorAtomOffset] = tmp;
74 tmp.set<VendorAtomValue::intValue>(last_count.vdroop1_count - prev_count.vdroop1_count);
75 values[PowerMitigationStats::kVdroop1CountFieldNumber - kVendorAtomOffset] = tmp;
76 tmp.set<VendorAtomValue::intValue>(last_count.vdroop2_count - prev_count.vdroop2_count);
77 values[PowerMitigationStats::kVdroop2CountFieldNumber - kVendorAtomOffset] = tmp;
78 tmp.set<VendorAtomValue::intValue>(last_count.smpl_warn_count - prev_count.smpl_warn_count);
79 values[PowerMitigationStats::kSmplWarnCountFieldNumber - kVendorAtomOffset] = tmp;
80 tmp.set<VendorAtomValue::intValue>(last_count.ocp_cpu1_count - prev_count.ocp_cpu1_count);
81 values[PowerMitigationStats::kOcpCpu1CountFieldNumber - kVendorAtomOffset] = tmp;
82 tmp.set<VendorAtomValue::intValue>(last_count.ocp_cpu2_count - prev_count.ocp_cpu2_count);
83 values[PowerMitigationStats::kOcpCpu2CountFieldNumber - kVendorAtomOffset] = tmp;
84 tmp.set<VendorAtomValue::intValue>(last_count.ocp_gpu_count - prev_count.ocp_gpu_count);
85 values[PowerMitigationStats::kOcpGpuCountFieldNumber - kVendorAtomOffset] = tmp;
86 tmp.set<VendorAtomValue::intValue>(last_count.ocp_tpu_count - prev_count.ocp_tpu_count);
87 values[PowerMitigationStats::kOcpTpuCountFieldNumber - kVendorAtomOffset] = tmp;
88 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_cpu1_count -
89 prev_count.soft_ocp_cpu1_count);
90 values[PowerMitigationStats::kSoftOcpCpu1CountFieldNumber - kVendorAtomOffset] = tmp;
91 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_cpu2_count -
92 prev_count.soft_ocp_cpu2_count);
93 values[PowerMitigationStats::kSoftOcpCpu2CountFieldNumber - kVendorAtomOffset] = tmp;
94 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_gpu_count -
95 prev_count.soft_ocp_gpu_count);
96 values[PowerMitigationStats::kSoftOcpGpuCountFieldNumber - kVendorAtomOffset] = tmp;
97 tmp.set<VendorAtomValue::intValue>(last_count.soft_ocp_tpu_count -
98 prev_count.soft_ocp_tpu_count);
99 values[PowerMitigationStats::kSoftOcpTpuCountFieldNumber - kVendorAtomOffset] = tmp;
100 tmp.set<VendorAtomValue::intValue>(last_cap.batoilo_cap);
101 values[PowerMitigationStats::kBatoiloCapFieldNumber - kVendorAtomOffset] = tmp;
102 tmp.set<VendorAtomValue::intValue>(last_cap.batoilo2_cap);
103 values[PowerMitigationStats::kBatoilo2CapFieldNumber - kVendorAtomOffset] = tmp;
104 tmp.set<VendorAtomValue::intValue>(last_cap.vdroop1_cap);
105 values[PowerMitigationStats::kVdroop1CapFieldNumber - kVendorAtomOffset] = tmp;
106 tmp.set<VendorAtomValue::intValue>(last_cap.vdroop2_cap);
107 values[PowerMitigationStats::kVdroop2CapFieldNumber - kVendorAtomOffset] = tmp;
108 tmp.set<VendorAtomValue::intValue>(last_cap.smpl_warn_cap);
109 values[PowerMitigationStats::kSmplWarnCapFieldNumber - kVendorAtomOffset] = tmp;
110 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_cpu1_cap);
111 values[PowerMitigationStats::kOcpCpu1CapFieldNumber - kVendorAtomOffset] = tmp;
112 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_cpu2_cap);
113 values[PowerMitigationStats::kOcpCpu2CapFieldNumber - kVendorAtomOffset] = tmp;
114 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_gpu_cap);
115 values[PowerMitigationStats::kOcpGpuCapFieldNumber - kVendorAtomOffset] = tmp;
116 tmp.set<VendorAtomValue::intValue>(last_cap.ocp_tpu_cap);
117 values[PowerMitigationStats::kOcpTpuCapFieldNumber - kVendorAtomOffset] = tmp;
118 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_cpu1_cap);
119 values[PowerMitigationStats::kSoftOcpCpu1CapFieldNumber - kVendorAtomOffset] = tmp;
120 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_cpu2_cap);
121 values[PowerMitigationStats::kSoftOcpCpu2CapFieldNumber - kVendorAtomOffset] = tmp;
122 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_gpu_cap);
123 values[PowerMitigationStats::kSoftOcpGpuCapFieldNumber - kVendorAtomOffset] = tmp;
124 tmp.set<VendorAtomValue::intValue>(last_cap.soft_ocp_tpu_cap);
125 values[PowerMitigationStats::kSoftOcpTpuCapFieldNumber - kVendorAtomOffset] = tmp;
126
127 prev_count = last_count;
128 // Send vendor atom to IStats HAL
129 VendorAtom event = {.reverseDomainName = "",
130 .atomId = PixelAtoms::Atom::kMitigationStats,
131 .values = std::move(values)};
132 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
133 if (!ret.isOk())
134 ALOGE("Unable to report to Stats service");
135 }
136
logMitigationCap(const std::string kMitigationDir,struct MitigationCap * last_cap)137 void MitigationStatsReporter::logMitigationCap(const std::string kMitigationDir,
138 struct MitigationCap *last_cap) {
139 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/batoilo_cap",
140 &(last_cap->batoilo_cap));
141 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/batoilo2_cap",
142 &(last_cap->batoilo2_cap));
143 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_cpu1_cap",
144 &(last_cap->ocp_cpu1_cap));
145 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_cpu2_cap",
146 &(last_cap->ocp_cpu2_cap));
147 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_gpu_cap",
148 &(last_cap->ocp_gpu_cap));
149 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/ocp_tpu_cap",
150 &(last_cap->ocp_tpu_cap));
151 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/smpl_warn_cap",
152 &(last_cap->smpl_warn_cap));
153 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_cpu1_cap",
154 &(last_cap->soft_ocp_cpu1_cap));
155 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_cpu2_cap",
156 &(last_cap->soft_ocp_cpu2_cap));
157 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_gpu_cap",
158 &(last_cap->soft_ocp_gpu_cap));
159 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/soft_ocp_tpu_cap",
160 &(last_cap->soft_ocp_tpu_cap));
161 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/vdroop1_cap",
162 &(last_cap->vdroop1_cap));
163 ReadFileToInt(kMitigationDir + "/last_triggered_capacity/vdroop2_cap",
164 &(last_cap->vdroop2_cap));
165 }
166
logMitigationCount(const std::string kMitigationDir,struct MitigationCount * last_count)167 bool MitigationStatsReporter::logMitigationCount(const std::string kMitigationDir,
168 struct MitigationCount *last_count) {
169 bool send_stats = false;
170 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/batoilo_count",
171 &(last_count->batoilo_count)))
172 return false;
173 send_stats |= (last_count->batoilo_count - prev_count.batoilo_count) > 0;
174 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/batoilo2_count",
175 &(last_count->batoilo2_count)))
176 return false;
177 send_stats |= (last_count->batoilo2_count - prev_count.batoilo2_count) > 0;
178 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_cpu1_count",
179 &(last_count->ocp_cpu1_count)))
180 return false;
181 send_stats |= (last_count->ocp_cpu1_count - prev_count.ocp_cpu1_count) > 0;
182 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_cpu2_count",
183 &(last_count->ocp_cpu2_count)))
184 return false;
185 send_stats |= (last_count->ocp_cpu2_count - prev_count.ocp_cpu2_count) > 0;
186 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_gpu_count",
187 &(last_count->ocp_gpu_count)))
188 return false;
189 send_stats |= (last_count->ocp_gpu_count - prev_count.ocp_gpu_count) > 0;
190 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/ocp_tpu_count",
191 &(last_count->ocp_tpu_count)))
192 return false;
193 send_stats |= (last_count->ocp_tpu_count - prev_count.ocp_tpu_count) > 0;
194 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/smpl_warn_count",
195 &(last_count->smpl_warn_count)))
196 return false;
197 send_stats |= (last_count->smpl_warn_count - prev_count.smpl_warn_count) > 0;
198 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_cpu1_count",
199 &(last_count->soft_ocp_cpu1_count)))
200 return false;
201 send_stats |= (last_count->soft_ocp_cpu1_count - prev_count.soft_ocp_cpu1_count) > 0;
202 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_cpu2_count",
203 &(last_count->soft_ocp_cpu2_count)))
204 return false;
205 send_stats |= (last_count->soft_ocp_cpu2_count - prev_count.soft_ocp_cpu2_count) > 0;
206 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_gpu_count",
207 &(last_count->soft_ocp_gpu_count)))
208 return false;
209 send_stats |= (last_count->soft_ocp_gpu_count - prev_count.soft_ocp_gpu_count) > 0;
210 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/soft_ocp_tpu_count",
211 &(last_count->soft_ocp_tpu_count)))
212 return false;
213 send_stats |= (last_count->soft_ocp_tpu_count - prev_count.soft_ocp_tpu_count) > 0;
214 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/vdroop1_count",
215 &(last_count->vdroop1_count)))
216 return false;
217 send_stats |= (last_count->vdroop1_count - prev_count.vdroop1_count) > 0;
218 if (!ReadFileToInt(kMitigationDir + "/last_triggered_count/vdroop2_count",
219 &(last_count->vdroop2_count)))
220 return false;
221 send_stats |= (last_count->vdroop2_count - prev_count.vdroop2_count) > 0;
222 return send_stats;
223 }
224
225 } // namespace pixel
226 } // namespace google
227 } // namespace hardware
228 } // namespace android
229