1 /*
2 * Copyright 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 "powerhal-libperfmgr"
18 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
19
20 #include <android-base/logging.h>
21 #include <android-base/parsedouble.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <sys/syscall.h>
25 #include <time.h>
26 #include <utils/Trace.h>
27 #include <atomic>
28
29 #include "PowerHintSession.h"
30 #include "PowerSessionManager.h"
31
32 namespace aidl {
33 namespace google {
34 namespace hardware {
35 namespace power {
36 namespace impl {
37 namespace pixel {
38
39 using ::android::base::StringPrintf;
40 using std::chrono::duration_cast;
41 using std::chrono::nanoseconds;
42 using std::literals::chrono_literals::operator""s;
43
44 constexpr char kPowerHalAdpfPidPOver[] = "vendor.powerhal.adpf.pid_p.over";
45 constexpr char kPowerHalAdpfPidPUnder[] = "vendor.powerhal.adpf.pid_p.under";
46 constexpr char kPowerHalAdpfPidI[] = "vendor.powerhal.adpf.pid_i";
47 constexpr char kPowerHalAdpfPidDOver[] = "vendor.powerhal.adpf.pid_d.over";
48 constexpr char kPowerHalAdpfPidDUnder[] = "vendor.powerhal.adpf.pid_d.under";
49 constexpr char kPowerHalAdpfPidIInit[] = "vendor.powerhal.adpf.pid_i.init";
50 constexpr char kPowerHalAdpfPidIHighLimit[] = "vendor.powerhal.adpf.pid_i.high_limit";
51 constexpr char kPowerHalAdpfPidILowLimit[] = "vendor.powerhal.adpf.pid_i.low_limit";
52 constexpr char kPowerHalAdpfUclampEnable[] = "vendor.powerhal.adpf.uclamp";
53 constexpr char kPowerHalAdpfUclampMinGranularity[] = "vendor.powerhal.adpf.uclamp_min.granularity";
54 constexpr char kPowerHalAdpfUclampMinHighLimit[] = "vendor.powerhal.adpf.uclamp_min.high_limit";
55 constexpr char kPowerHalAdpfUclampMinLowLimit[] = "vendor.powerhal.adpf.uclamp_min.low_limit";
56 constexpr char kPowerHalAdpfStaleTimeFactor[] = "vendor.powerhal.adpf.stale_timeout_factor";
57 constexpr char kPowerHalAdpfPSamplingWindow[] = "vendor.powerhal.adpf.p.window";
58 constexpr char kPowerHalAdpfISamplingWindow[] = "vendor.powerhal.adpf.i.window";
59 constexpr char kPowerHalAdpfDSamplingWindow[] = "vendor.powerhal.adpf.d.window";
60
61 namespace {
62 /* there is no glibc or bionic wrapper */
63 struct sched_attr {
64 __u32 size;
65 __u32 sched_policy;
66 __u64 sched_flags;
67 __s32 sched_nice;
68 __u32 sched_priority;
69 __u64 sched_runtime;
70 __u64 sched_deadline;
71 __u64 sched_period;
72 __u32 sched_util_min;
73 __u32 sched_util_max;
74 };
75
sched_setattr(int pid,struct sched_attr * attr,unsigned int flags)76 static int sched_setattr(int pid, struct sched_attr *attr, unsigned int flags) {
77 static const bool kPowerHalAdpfUclamp =
78 ::android::base::GetBoolProperty(kPowerHalAdpfUclampEnable, true);
79 if (!kPowerHalAdpfUclamp) {
80 ALOGV("PowerHintSession:%s: skip", __func__);
81 return 0;
82 }
83 return syscall(__NR_sched_setattr, pid, attr, flags);
84 }
85
ns_to_100us(int64_t ns)86 static inline int64_t ns_to_100us(int64_t ns) {
87 return ns / 100000;
88 }
89
getDoubleProperty(const char * prop,double value)90 static double getDoubleProperty(const char *prop, double value) {
91 std::string result = ::android::base::GetProperty(prop, std::to_string(value).c_str());
92 if (!::android::base::ParseDouble(result.c_str(), &value)) {
93 ALOGE("PowerHintSession : failed to parse double in %s", prop);
94 }
95 return value;
96 }
97
98 static double sPidPOver = getDoubleProperty(kPowerHalAdpfPidPOver, 5.0);
99 static double sPidPUnder = getDoubleProperty(kPowerHalAdpfPidPUnder, 3.0);
100 static double sPidI = getDoubleProperty(kPowerHalAdpfPidI, 0.001);
101 static double sPidDOver = getDoubleProperty(kPowerHalAdpfPidDOver, 500.0);
102 static double sPidDUnder = getDoubleProperty(kPowerHalAdpfPidDUnder, 0.0);
103 static const int64_t sPidIInit =
104 (sPidI == 0) ? 0
105 : static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
106 kPowerHalAdpfPidIInit, 200) /
107 sPidI);
108 static const int64_t sPidIHighLimit =
109 (sPidI == 0) ? 0
110 : static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
111 kPowerHalAdpfPidIHighLimit, 512) /
112 sPidI);
113 static const int64_t sPidILowLimit =
114 (sPidI == 0) ? 0
115 : static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
116 kPowerHalAdpfPidILowLimit, -120) /
117 sPidI);
118 static const int32_t sUclampMinHighLimit =
119 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinHighLimit, 512);
120 static const int32_t sUclampMinLowLimit =
121 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinLowLimit, 0);
122 static const uint32_t sUclampMinGranularity =
123 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinGranularity, 5);
124 static const int64_t sStaleTimeFactor =
125 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfStaleTimeFactor, 20);
126 static const int64_t sPSamplingWindow =
127 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfPSamplingWindow, 1);
128 static const int64_t sISamplingWindow =
129 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfISamplingWindow, 0);
130 static const int64_t sDSamplingWindow =
131 ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfDSamplingWindow, 1);
132
133 } // namespace
134
PowerHintSession(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos,const nanoseconds adpfRate)135 PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
136 int64_t durationNanos, const nanoseconds adpfRate)
137 : kAdpfRate(adpfRate) {
138 mDescriptor = new AppHintDesc(tgid, uid, threadIds);
139 mDescriptor->duration = std::chrono::nanoseconds(durationNanos);
140 mStaleHandler = sp<StaleHandler>(new StaleHandler(this));
141 mPowerManagerHandler = PowerSessionManager::getInstance();
142
143 if (ATRACE_ENABLED()) {
144 const std::string idstr = getIdString();
145 std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
146 ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
147 sz = StringPrintf("adpf.%s-active", idstr.c_str());
148 ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
149 }
150 PowerSessionManager::getInstance()->addPowerSession(this);
151 // init boost
152 setUclamp(sUclampMinHighLimit);
153 ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str());
154 }
155
~PowerHintSession()156 PowerHintSession::~PowerHintSession() {
157 close();
158 ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str());
159 if (ATRACE_ENABLED()) {
160 const std::string idstr = getIdString();
161 std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
162 ATRACE_INT(sz.c_str(), 0);
163 sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
164 ATRACE_INT(sz.c_str(), 0);
165 sz = sz = StringPrintf("adpf.%s-active", idstr.c_str());
166 ATRACE_INT(sz.c_str(), 0);
167 }
168 delete mDescriptor;
169 }
170
getIdString() const171 std::string PowerHintSession::getIdString() const {
172 std::string idstr = StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR, mDescriptor->tgid,
173 mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
174 return idstr;
175 }
176
updateUniveralBoostMode()177 void PowerHintSession::updateUniveralBoostMode() {
178 PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
179 }
180
setUclamp(int32_t min,int32_t max)181 int PowerHintSession::setUclamp(int32_t min, int32_t max) {
182 std::lock_guard<std::mutex> guard(mLock);
183 min = std::max(0, min);
184 min = std::min(min, max);
185 max = std::max(0, max);
186 max = std::max(min, max);
187 if (ATRACE_ENABLED()) {
188 const std::string idstr = getIdString();
189 std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
190 ATRACE_INT(sz.c_str(), min);
191 }
192 for (const auto tid : mDescriptor->threadIds) {
193 sched_attr attr = {};
194 attr.size = sizeof(attr);
195
196 attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP);
197 attr.sched_util_min = min;
198 attr.sched_util_max = max;
199
200 int ret = sched_setattr(tid, &attr, 0);
201 if (ret) {
202 ALOGW("sched_setattr failed for thread %d, err=%d", tid, errno);
203 }
204 ALOGV("PowerHintSession tid: %d, uclamp(%d, %d)", tid, min, max);
205 }
206 mDescriptor->current_min = min;
207 return 0;
208 }
209
pause()210 ndk::ScopedAStatus PowerHintSession::pause() {
211 if (!mDescriptor->is_active.load())
212 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
213 // Reset to default uclamp value.
214 setUclamp(0);
215 mDescriptor->is_active.store(false);
216 if (ATRACE_ENABLED()) {
217 const std::string idstr = getIdString();
218 std::string sz = StringPrintf("adpf.%s-active", idstr.c_str());
219 ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
220 }
221 updateUniveralBoostMode();
222 return ndk::ScopedAStatus::ok();
223 }
224
resume()225 ndk::ScopedAStatus PowerHintSession::resume() {
226 if (mDescriptor->is_active.load())
227 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
228 mDescriptor->is_active.store(true);
229 mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
230 // resume boost
231 setUclamp(sUclampMinHighLimit);
232 if (ATRACE_ENABLED()) {
233 const std::string idstr = getIdString();
234 std::string sz = StringPrintf("adpf.%s-active", idstr.c_str());
235 ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
236 }
237 updateUniveralBoostMode();
238 return ndk::ScopedAStatus::ok();
239 }
240
close()241 ndk::ScopedAStatus PowerHintSession::close() {
242 bool sessionClosedExpectedToBe = false;
243 if (!mSessionClosed.compare_exchange_strong(sessionClosedExpectedToBe, true)) {
244 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
245 }
246 PowerHintMonitor::getInstance()->getLooper()->removeMessages(mStaleHandler);
247 setUclamp(0);
248 PowerSessionManager::getInstance()->removePowerSession(this);
249 updateUniveralBoostMode();
250 return ndk::ScopedAStatus::ok();
251 }
252
updateTargetWorkDuration(int64_t targetDurationNanos)253 ndk::ScopedAStatus PowerHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) {
254 if (targetDurationNanos <= 0) {
255 ALOGE("Error: targetDurationNanos(%" PRId64 ") should bigger than 0", targetDurationNanos);
256 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
257 }
258 ALOGV("update target duration: %" PRId64 " ns", targetDurationNanos);
259 double ratio =
260 targetDurationNanos == 0 ? 1.0 : mDescriptor->duration.count() / targetDurationNanos;
261 mDescriptor->integral_error =
262 std::max(sPidIInit, static_cast<int64_t>(mDescriptor->integral_error * ratio));
263
264 mDescriptor->duration = std::chrono::nanoseconds(targetDurationNanos);
265 if (ATRACE_ENABLED()) {
266 const std::string idstr = getIdString();
267 std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
268 ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
269 }
270
271 return ndk::ScopedAStatus::ok();
272 }
273
reportActualWorkDuration(const std::vector<WorkDuration> & actualDurations)274 ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration(
275 const std::vector<WorkDuration> &actualDurations) {
276 if (mDescriptor->duration.count() == 0LL) {
277 ALOGE("Expect to call updateTargetWorkDuration() first.");
278 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
279 }
280 if (actualDurations.size() == 0) {
281 ALOGE("Error: duration.size() shouldn't be %zu.", actualDurations.size());
282 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
283 }
284 if (!mDescriptor->is_active.load()) {
285 ALOGE("Error: shouldn't report duration during pause state.");
286 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
287 }
288 if (PowerHintMonitor::getInstance()->isRunning() && isStale()) {
289 if (ATRACE_ENABLED()) {
290 const std::string idstr = getIdString();
291 std::string sz = StringPrintf("adpf.%s-stale", idstr.c_str());
292 ATRACE_INT(sz.c_str(), 0);
293 }
294 mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
295 }
296 int64_t targetDurationNanos = (int64_t)mDescriptor->duration.count();
297 int64_t length = actualDurations.size();
298 int64_t p_start =
299 sPSamplingWindow == 0 || sPSamplingWindow > length ? 0 : length - sPSamplingWindow;
300 int64_t i_start =
301 sISamplingWindow == 0 || sISamplingWindow > length ? 0 : length - sISamplingWindow;
302 int64_t d_start =
303 sDSamplingWindow == 0 || sDSamplingWindow > length ? 0 : length - sDSamplingWindow;
304 int64_t dt = ns_to_100us(targetDurationNanos);
305 int64_t err_sum = 0;
306 int64_t derivative_sum = 0;
307 for (int64_t i = std::min({p_start, i_start, d_start}); i < length; i++) {
308 int64_t actualDurationNanos = actualDurations[i].durationNanos;
309 if (std::abs(actualDurationNanos) > targetDurationNanos * 20) {
310 ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")",
311 actualDurationNanos, targetDurationNanos);
312 }
313 // PID control algorithm
314 int64_t error = ns_to_100us(actualDurationNanos - targetDurationNanos);
315 if (i >= d_start) {
316 derivative_sum += error - mDescriptor->previous_error;
317 }
318 if (i >= p_start) {
319 err_sum += error;
320 }
321 if (i >= i_start) {
322 mDescriptor->integral_error = mDescriptor->integral_error + error * dt;
323 mDescriptor->integral_error = std::min(sPidIHighLimit, mDescriptor->integral_error);
324 mDescriptor->integral_error = std::max(sPidILowLimit, mDescriptor->integral_error);
325 }
326 mDescriptor->previous_error = error;
327 }
328 int64_t pOut = static_cast<int64_t>((err_sum > 0 ? sPidPOver : sPidPUnder) * err_sum /
329 (length - p_start));
330 int64_t iOut = static_cast<int64_t>(sPidI * mDescriptor->integral_error);
331 int64_t dOut = static_cast<int64_t>((derivative_sum > 0 ? sPidDOver : sPidDUnder) *
332 derivative_sum / dt / (length - d_start));
333
334 int64_t output = pOut + iOut + dOut;
335 if (ATRACE_ENABLED()) {
336 const std::string idstr = getIdString();
337 std::string sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
338 ATRACE_INT(sz.c_str(), actualDurations[length - 1].durationNanos);
339 sz = StringPrintf("adpf.%s-target", idstr.c_str());
340 ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
341 sz = StringPrintf("adpf.%s-sample_size", idstr.c_str());
342 ATRACE_INT(sz.c_str(), length);
343 sz = StringPrintf("adpf.%s-pid.count", idstr.c_str());
344 ATRACE_INT(sz.c_str(), mDescriptor->update_count);
345 sz = StringPrintf("adpf.%s-pid.pOut", idstr.c_str());
346 ATRACE_INT(sz.c_str(), pOut);
347 sz = StringPrintf("adpf.%s-pid.iOut", idstr.c_str());
348 ATRACE_INT(sz.c_str(), iOut);
349 sz = StringPrintf("adpf.%s-pid.dOut", idstr.c_str());
350 ATRACE_INT(sz.c_str(), dOut);
351 sz = StringPrintf("adpf.%s-pid.output", idstr.c_str());
352 ATRACE_INT(sz.c_str(), output);
353 }
354 mDescriptor->update_count++;
355
356 mStaleHandler->updateStaleTimer();
357
358 /* apply to all the threads in the group */
359 if (output != 0) {
360 int next_min =
361 std::min(sUclampMinHighLimit, mDescriptor->current_min + static_cast<int>(output));
362 next_min = std::max(sUclampMinLowLimit, next_min);
363 if (std::abs(mDescriptor->current_min - next_min) > sUclampMinGranularity) {
364 setUclamp(next_min);
365 }
366 }
367
368 return ndk::ScopedAStatus::ok();
369 }
370
toString() const371 std::string AppHintDesc::toString() const {
372 std::string out =
373 StringPrintf("session %" PRIxPTR "\n", reinterpret_cast<uintptr_t>(this) & 0xffff);
374 const int64_t durationNanos = duration.count();
375 out.append(StringPrintf(" duration: %" PRId64 " ns\n", durationNanos));
376 out.append(StringPrintf(" uclamp.min: %d \n", current_min));
377 out.append(StringPrintf(" uid: %d, tgid: %d\n", uid, tgid));
378
379 out.append(" threadIds: [");
380 bool first = true;
381 for (int tid : threadIds) {
382 if (!first) {
383 out.append(", ");
384 }
385 out.append(std::to_string(tid));
386 first = false;
387 }
388 out.append("]\n");
389 return out;
390 }
391
isActive()392 bool PowerHintSession::isActive() {
393 return mDescriptor->is_active.load();
394 }
395
isStale()396 bool PowerHintSession::isStale() {
397 auto now = std::chrono::steady_clock::now();
398 return now >= mStaleHandler->getStaleTime();
399 }
400
getTidList() const401 const std::vector<int> &PowerHintSession::getTidList() const {
402 return mDescriptor->threadIds;
403 }
404
setStale()405 void PowerHintSession::setStale() {
406 if (ATRACE_ENABLED()) {
407 const std::string idstr = getIdString();
408 std::string sz = StringPrintf("adpf.%s-stale", idstr.c_str());
409 ATRACE_INT(sz.c_str(), 1);
410 }
411 // Reset to default uclamp value.
412 setUclamp(0);
413 // Deliver a task to check if all sessions are inactive.
414 updateUniveralBoostMode();
415 }
416
updateStaleTimer()417 void PowerHintSession::StaleHandler::updateStaleTimer() {
418 std::lock_guard<std::mutex> guard(mStaleLock);
419 if (PowerHintMonitor::getInstance()->isRunning()) {
420 auto when = getStaleTime();
421 auto now = std::chrono::steady_clock::now();
422 mLastUpdatedTime.store(now);
423 if (now > when) {
424 mSession->updateUniveralBoostMode();
425 }
426 if (!mIsMonitoringStale.load()) {
427 auto next = getStaleTime();
428 PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
429 duration_cast<nanoseconds>(next - now).count(), this, NULL);
430 mIsMonitoringStale.store(true);
431 }
432 }
433 }
434
getStaleTime()435 time_point<steady_clock> PowerHintSession::StaleHandler::getStaleTime() {
436 return mLastUpdatedTime.load() +
437 std::chrono::duration_cast<milliseconds>(mSession->kAdpfRate) * sStaleTimeFactor;
438 }
439
handleMessage(const Message &)440 void PowerHintSession::StaleHandler::handleMessage(const Message &) {
441 std::lock_guard<std::mutex> guard(mStaleLock);
442 auto now = std::chrono::steady_clock::now();
443 auto when = getStaleTime();
444 // Check if the session is stale based on the last_updated_time.
445 if (now > when) {
446 mSession->setStale();
447 mIsMonitoringStale.store(false);
448 return;
449 }
450 // Schedule for the next checking time.
451 PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
452 duration_cast<nanoseconds>(when - now).count(), this, NULL);
453 }
454
455 } // namespace pixel
456 } // namespace impl
457 } // namespace power
458 } // namespace hardware
459 } // namespace google
460 } // namespace aidl
461