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, 2.0);
99 static double sPidPUnder = getDoubleProperty(kPowerHalAdpfPidPUnder, 1.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, -30) /
117                                             sPidI);
118 static const int32_t sUclampMinHighLimit =
119         ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinHighLimit, 384);
120 static const int32_t sUclampMinLowLimit =
121         ::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinLowLimit, 2);
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         sz = StringPrintf("adpf.%s-stale", idstr.c_str());
150         ATRACE_INT(sz.c_str(), isStale());
151     }
152     PowerSessionManager::getInstance()->addPowerSession(this);
153     // init boost
154     setUclamp(sUclampMinHighLimit);
155     ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str());
156 }
157 
~PowerHintSession()158 PowerHintSession::~PowerHintSession() {
159     close();
160     ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str());
161     if (ATRACE_ENABLED()) {
162         const std::string idstr = getIdString();
163         std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
164         ATRACE_INT(sz.c_str(), 0);
165         sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
166         ATRACE_INT(sz.c_str(), 0);
167         sz = sz = StringPrintf("adpf.%s-active", idstr.c_str());
168         ATRACE_INT(sz.c_str(), 0);
169     }
170     delete mDescriptor;
171 }
172 
getIdString() const173 std::string PowerHintSession::getIdString() const {
174     std::string idstr = StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR, mDescriptor->tgid,
175                                      mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
176     return idstr;
177 }
178 
updateUniveralBoostMode()179 void PowerHintSession::updateUniveralBoostMode() {
180     PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
181 }
182 
setUclamp(int32_t min,int32_t max)183 int PowerHintSession::setUclamp(int32_t min, int32_t max) {
184     std::lock_guard<std::mutex> guard(mLock);
185     min = std::max(0, min);
186     min = std::min(min, max);
187     max = std::max(0, max);
188     max = std::max(min, max);
189     if (ATRACE_ENABLED()) {
190         const std::string idstr = getIdString();
191         std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
192         ATRACE_INT(sz.c_str(), min);
193     }
194     for (const auto tid : mDescriptor->threadIds) {
195         sched_attr attr = {};
196         attr.size = sizeof(attr);
197 
198         attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP);
199         attr.sched_util_min = min;
200         attr.sched_util_max = max;
201 
202         int ret = sched_setattr(tid, &attr, 0);
203         if (ret) {
204             ALOGW("sched_setattr failed for thread %d, err=%d", tid, errno);
205         }
206         ALOGV("PowerHintSession tid: %d, uclamp(%d, %d)", tid, min, max);
207     }
208     mDescriptor->current_min = min;
209     return 0;
210 }
211 
pause()212 ndk::ScopedAStatus PowerHintSession::pause() {
213     if (!mDescriptor->is_active.load())
214         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
215     // Reset to default uclamp value.
216     setUclamp(0);
217     mDescriptor->is_active.store(false);
218     if (ATRACE_ENABLED()) {
219         const std::string idstr = getIdString();
220         std::string sz = StringPrintf("adpf.%s-active", idstr.c_str());
221         ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
222     }
223     updateUniveralBoostMode();
224     return ndk::ScopedAStatus::ok();
225 }
226 
resume()227 ndk::ScopedAStatus PowerHintSession::resume() {
228     if (mDescriptor->is_active.load())
229         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
230     mDescriptor->is_active.store(true);
231     mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
232     // resume boost
233     setUclamp(sUclampMinHighLimit);
234     if (ATRACE_ENABLED()) {
235         const std::string idstr = getIdString();
236         std::string sz = StringPrintf("adpf.%s-active", idstr.c_str());
237         ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
238     }
239     updateUniveralBoostMode();
240     return ndk::ScopedAStatus::ok();
241 }
242 
close()243 ndk::ScopedAStatus PowerHintSession::close() {
244     bool sessionClosedExpectedToBe = false;
245     if (!mSessionClosed.compare_exchange_strong(sessionClosedExpectedToBe, true)) {
246         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
247     }
248     PowerHintMonitor::getInstance()->getLooper()->removeMessages(mStaleHandler);
249     setUclamp(0);
250     PowerSessionManager::getInstance()->removePowerSession(this);
251     updateUniveralBoostMode();
252     return ndk::ScopedAStatus::ok();
253 }
254 
updateTargetWorkDuration(int64_t targetDurationNanos)255 ndk::ScopedAStatus PowerHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) {
256     if (targetDurationNanos <= 0) {
257         ALOGE("Error: targetDurationNanos(%" PRId64 ") should bigger than 0", targetDurationNanos);
258         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
259     }
260     ALOGV("update target duration: %" PRId64 " ns", targetDurationNanos);
261     double ratio =
262             targetDurationNanos == 0 ? 1.0 : mDescriptor->duration.count() / targetDurationNanos;
263     mDescriptor->integral_error =
264             std::max(sPidIInit, static_cast<int64_t>(mDescriptor->integral_error * ratio));
265 
266     mDescriptor->duration = std::chrono::nanoseconds(targetDurationNanos);
267     if (ATRACE_ENABLED()) {
268         const std::string idstr = getIdString();
269         std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
270         ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
271     }
272 
273     return ndk::ScopedAStatus::ok();
274 }
275 
reportActualWorkDuration(const std::vector<WorkDuration> & actualDurations)276 ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration(
277         const std::vector<WorkDuration> &actualDurations) {
278     if (mDescriptor->duration.count() == 0LL) {
279         ALOGE("Expect to call updateTargetWorkDuration() first.");
280         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
281     }
282     if (actualDurations.size() == 0) {
283         ALOGE("Error: duration.size() shouldn't be %zu.", actualDurations.size());
284         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
285     }
286     if (!mDescriptor->is_active.load()) {
287         ALOGE("Error: shouldn't report duration during pause state.");
288         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
289     }
290     if (PowerHintMonitor::getInstance()->isRunning() && isStale()) {
291         mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
292         if (ATRACE_ENABLED()) {
293             const std::string idstr = getIdString();
294             std::string sz = StringPrintf("adpf.%s-wakeup", idstr.c_str());
295             ATRACE_INT(sz.c_str(), mDescriptor->integral_error);
296             ATRACE_INT(sz.c_str(), 0);
297         }
298     }
299     int64_t targetDurationNanos = (int64_t)mDescriptor->duration.count();
300     int64_t length = actualDurations.size();
301     int64_t p_start =
302             sPSamplingWindow == 0 || sPSamplingWindow > length ? 0 : length - sPSamplingWindow;
303     int64_t i_start =
304             sISamplingWindow == 0 || sISamplingWindow > length ? 0 : length - sISamplingWindow;
305     int64_t d_start =
306             sDSamplingWindow == 0 || sDSamplingWindow > length ? 0 : length - sDSamplingWindow;
307     int64_t dt = ns_to_100us(targetDurationNanos);
308     int64_t err_sum = 0;
309     int64_t derivative_sum = 0;
310     for (int64_t i = std::min({p_start, i_start, d_start}); i < length; i++) {
311         int64_t actualDurationNanos = actualDurations[i].durationNanos;
312         if (std::abs(actualDurationNanos) > targetDurationNanos * 20) {
313             ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")",
314                   actualDurationNanos, targetDurationNanos);
315         }
316         // PID control algorithm
317         int64_t error = ns_to_100us(actualDurationNanos - targetDurationNanos);
318         if (i >= d_start) {
319             derivative_sum += error - mDescriptor->previous_error;
320         }
321         if (i >= p_start) {
322             err_sum += error;
323         }
324         if (i >= i_start) {
325             mDescriptor->integral_error = mDescriptor->integral_error + error * dt;
326             mDescriptor->integral_error = std::min(sPidIHighLimit, mDescriptor->integral_error);
327             mDescriptor->integral_error = std::max(sPidILowLimit, mDescriptor->integral_error);
328         }
329         mDescriptor->previous_error = error;
330     }
331     if (ATRACE_ENABLED()) {
332         const std::string idstr = getIdString();
333         std::string sz = StringPrintf("adpf.%s-err", idstr.c_str());
334         ATRACE_INT(sz.c_str(), err_sum / (length - p_start));
335         sz = StringPrintf("adpf.%s-integral", idstr.c_str());
336         ATRACE_INT(sz.c_str(), mDescriptor->integral_error);
337         sz = StringPrintf("adpf.%s-derivative", idstr.c_str());
338         ATRACE_INT(sz.c_str(), derivative_sum / dt / (length - d_start));
339     }
340     int64_t pOut = static_cast<int64_t>((err_sum > 0 ? sPidPOver : sPidPUnder) * err_sum /
341                                         (length - p_start));
342     int64_t iOut = static_cast<int64_t>(sPidI * mDescriptor->integral_error);
343     int64_t dOut = static_cast<int64_t>((derivative_sum > 0 ? sPidDOver : sPidDUnder) *
344                                         derivative_sum / dt / (length - d_start));
345 
346     int64_t output = pOut + iOut + dOut;
347 
348     if (ATRACE_ENABLED()) {
349         const std::string idstr = getIdString();
350         std::string sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
351         ATRACE_INT(sz.c_str(), actualDurations[length - 1].durationNanos);
352         sz = StringPrintf("adpf.%s-target", idstr.c_str());
353         ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
354         sz = StringPrintf("adpf.%s-sample_size", idstr.c_str());
355         ATRACE_INT(sz.c_str(), length);
356         sz = StringPrintf("adpf.%s-pid.count", idstr.c_str());
357         ATRACE_INT(sz.c_str(), mDescriptor->update_count);
358         sz = StringPrintf("adpf.%s-pid.pOut", idstr.c_str());
359         ATRACE_INT(sz.c_str(), pOut);
360         sz = StringPrintf("adpf.%s-pid.iOut", idstr.c_str());
361         ATRACE_INT(sz.c_str(), iOut);
362         sz = StringPrintf("adpf.%s-pid.dOut", idstr.c_str());
363         ATRACE_INT(sz.c_str(), dOut);
364         sz = StringPrintf("adpf.%s-pid.output", idstr.c_str());
365         ATRACE_INT(sz.c_str(), output);
366         sz = StringPrintf("adpf.%s-stale", idstr.c_str());
367         ATRACE_INT(sz.c_str(), isStale());
368         sz = StringPrintf("adpf.%s-pid.overtime", idstr.c_str());
369         ATRACE_INT(sz.c_str(), err_sum > 0);
370     }
371     mDescriptor->update_count++;
372 
373     mStaleHandler->updateStaleTimer();
374 
375     /* apply to all the threads in the group */
376     if (output != 0) {
377         int next_min = std::min(sUclampMinHighLimit, static_cast<int>(output));
378         next_min = std::max(sUclampMinLowLimit, next_min);
379         if (std::abs(mDescriptor->current_min - next_min) > sUclampMinGranularity) {
380             setUclamp(next_min);
381         }
382     }
383 
384     return ndk::ScopedAStatus::ok();
385 }
386 
toString() const387 std::string AppHintDesc::toString() const {
388     std::string out =
389             StringPrintf("session %" PRIxPTR "\n", reinterpret_cast<uintptr_t>(this) & 0xffff);
390     const int64_t durationNanos = duration.count();
391     out.append(StringPrintf("  duration: %" PRId64 " ns\n", durationNanos));
392     out.append(StringPrintf("  uclamp.min: %d \n", current_min));
393     out.append(StringPrintf("  uid: %d, tgid: %d\n", uid, tgid));
394 
395     out.append("  threadIds: [");
396     bool first = true;
397     for (int tid : threadIds) {
398         if (!first) {
399             out.append(", ");
400         }
401         out.append(std::to_string(tid));
402         first = false;
403     }
404     out.append("]\n");
405     return out;
406 }
407 
isActive()408 bool PowerHintSession::isActive() {
409     return mDescriptor->is_active.load();
410 }
411 
isStale()412 bool PowerHintSession::isStale() {
413     auto now = std::chrono::steady_clock::now();
414     return now >= mStaleHandler->getStaleTime();
415 }
416 
getTidList() const417 const std::vector<int> &PowerHintSession::getTidList() const {
418     return mDescriptor->threadIds;
419 }
420 
setStale()421 void PowerHintSession::setStale() {
422     if (ATRACE_ENABLED()) {
423         const std::string idstr = getIdString();
424         std::string sz = StringPrintf("adpf.%s-stale", idstr.c_str());
425         ATRACE_INT(sz.c_str(), 1);
426     }
427     // Reset to default uclamp value.
428     setUclamp(0);
429     // Deliver a task to check if all sessions are inactive.
430     updateUniveralBoostMode();
431 }
432 
updateStaleTimer()433 void PowerHintSession::StaleHandler::updateStaleTimer() {
434     std::lock_guard<std::mutex> guard(mStaleLock);
435     if (PowerHintMonitor::getInstance()->isRunning()) {
436         auto when = getStaleTime();
437         auto now = std::chrono::steady_clock::now();
438         mLastUpdatedTime.store(now);
439         if (now > when) {
440             mSession->updateUniveralBoostMode();
441         }
442         if (!mIsMonitoringStale.load()) {
443             auto next = getStaleTime();
444             PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
445                     duration_cast<nanoseconds>(next - now).count(), this, NULL);
446             mIsMonitoringStale.store(true);
447         }
448         if (ATRACE_ENABLED()) {
449             const std::string idstr = mSession->getIdString();
450             std::string sz = StringPrintf("adpf.%s-stale", idstr.c_str());
451             ATRACE_INT(sz.c_str(), 0);
452         }
453     }
454 }
455 
getStaleTime()456 time_point<steady_clock> PowerHintSession::StaleHandler::getStaleTime() {
457     return mLastUpdatedTime.load() +
458            std::chrono::duration_cast<milliseconds>(mSession->kAdpfRate) * sStaleTimeFactor;
459 }
460 
handleMessage(const Message &)461 void PowerHintSession::StaleHandler::handleMessage(const Message &) {
462     std::lock_guard<std::mutex> guard(mStaleLock);
463     auto now = std::chrono::steady_clock::now();
464     auto when = getStaleTime();
465     // Check if the session is stale based on the last_updated_time.
466     if (now > when) {
467         mSession->setStale();
468         mIsMonitoringStale.store(false);
469         return;
470     }
471     // Schedule for the next checking time.
472     PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
473             duration_cast<nanoseconds>(when - now).count(), this, NULL);
474 }
475 
476 }  // namespace pixel
477 }  // namespace impl
478 }  // namespace power
479 }  // namespace hardware
480 }  // namespace google
481 }  // namespace aidl
482