2021-05-04 23:17:41 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2021 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "powerhal-libperfmgr"
|
|
|
|
#define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
|
|
|
|
|
2021-10-26 05:48:06 -04:00
|
|
|
#include "PowerHintSession.h"
|
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/parsedouble.h>
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2021-10-26 05:48:06 -04:00
|
|
|
#include <perfmgr/AdpfConfig.h>
|
2022-04-29 17:05:38 -04:00
|
|
|
#include <private/android_filesystem_config.h>
|
2021-06-23 18:58:56 -04:00
|
|
|
#include <sys/syscall.h>
|
2021-05-04 23:17:41 -04:00
|
|
|
#include <time.h>
|
|
|
|
#include <utils/Trace.h>
|
2021-10-26 05:48:06 -04:00
|
|
|
|
2021-07-29 11:58:22 -04:00
|
|
|
#include <atomic>
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
#include "PowerSessionManager.h"
|
|
|
|
|
|
|
|
namespace aidl {
|
|
|
|
namespace google {
|
|
|
|
namespace hardware {
|
|
|
|
namespace power {
|
|
|
|
namespace impl {
|
|
|
|
namespace pixel {
|
|
|
|
|
|
|
|
using ::android::base::StringPrintf;
|
2021-10-26 05:48:06 -04:00
|
|
|
using ::android::perfmgr::AdpfConfig;
|
|
|
|
using ::android::perfmgr::HintManager;
|
2021-05-04 23:17:41 -04:00
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::nanoseconds;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-06-17 00:20:44 -04:00
|
|
|
static inline int64_t ns_to_100us(int64_t ns) {
|
2021-05-04 23:17:41 -04:00
|
|
|
return ns / 100000;
|
|
|
|
}
|
|
|
|
|
2021-10-26 05:48:06 -04:00
|
|
|
static int64_t convertWorkDurationToBoostByPid(std::shared_ptr<AdpfConfig> adpfConfig,
|
|
|
|
nanoseconds targetDuration,
|
|
|
|
const std::vector<WorkDuration> &actualDurations,
|
|
|
|
int64_t *integral_error, int64_t *previous_error,
|
|
|
|
const std::string &idstr) {
|
|
|
|
uint64_t samplingWindowP = adpfConfig->mSamplingWindowP;
|
|
|
|
uint64_t samplingWindowI = adpfConfig->mSamplingWindowI;
|
|
|
|
uint64_t samplingWindowD = adpfConfig->mSamplingWindowD;
|
|
|
|
int64_t targetDurationNanos = (int64_t)targetDuration.count();
|
|
|
|
int64_t length = actualDurations.size();
|
|
|
|
int64_t p_start =
|
|
|
|
samplingWindowP == 0 || samplingWindowP > length ? 0 : length - samplingWindowP;
|
|
|
|
int64_t i_start =
|
|
|
|
samplingWindowI == 0 || samplingWindowI > length ? 0 : length - samplingWindowI;
|
|
|
|
int64_t d_start =
|
|
|
|
samplingWindowD == 0 || samplingWindowD > length ? 0 : length - samplingWindowD;
|
|
|
|
int64_t dt = ns_to_100us(targetDurationNanos);
|
|
|
|
int64_t err_sum = 0;
|
|
|
|
int64_t derivative_sum = 0;
|
|
|
|
for (int64_t i = std::min({p_start, i_start, d_start}); i < length; i++) {
|
|
|
|
int64_t actualDurationNanos = actualDurations[i].durationNanos;
|
|
|
|
if (std::abs(actualDurationNanos) > targetDurationNanos * 20) {
|
|
|
|
ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")",
|
|
|
|
actualDurationNanos, targetDurationNanos);
|
|
|
|
}
|
|
|
|
// PID control algorithm
|
|
|
|
int64_t error = ns_to_100us(actualDurationNanos - targetDurationNanos);
|
|
|
|
if (i >= d_start) {
|
|
|
|
derivative_sum += error - (*previous_error);
|
|
|
|
}
|
|
|
|
if (i >= p_start) {
|
|
|
|
err_sum += error;
|
|
|
|
}
|
|
|
|
if (i >= i_start) {
|
|
|
|
*integral_error = *integral_error + error * dt;
|
|
|
|
*integral_error = std::min(adpfConfig->getPidIHighDivI(), *integral_error);
|
|
|
|
*integral_error = std::max(adpfConfig->getPidILowDivI(), *integral_error);
|
|
|
|
}
|
|
|
|
*previous_error = error;
|
|
|
|
}
|
|
|
|
int64_t pOut = static_cast<int64_t>((err_sum > 0 ? adpfConfig->mPidPo : adpfConfig->mPidPu) *
|
|
|
|
err_sum / (length - p_start));
|
|
|
|
int64_t iOut = static_cast<int64_t>(adpfConfig->mPidI * (*integral_error));
|
|
|
|
int64_t dOut =
|
|
|
|
static_cast<int64_t>((derivative_sum > 0 ? adpfConfig->mPidDo : adpfConfig->mPidDu) *
|
|
|
|
derivative_sum / dt / (length - d_start));
|
|
|
|
|
|
|
|
int64_t output = pOut + iOut + dOut;
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
std::string sz = StringPrintf("adpf.%s-pid.err", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), err_sum / (length - p_start));
|
|
|
|
sz = StringPrintf("adpf.%s-pid.integral", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), *integral_error);
|
|
|
|
sz = StringPrintf("adpf.%s-pid.derivative", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), derivative_sum / dt / (length - d_start));
|
|
|
|
sz = StringPrintf("adpf.%s-pid.pOut", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), pOut);
|
|
|
|
sz = StringPrintf("adpf.%s-pid.iOut", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), iOut);
|
|
|
|
sz = StringPrintf("adpf.%s-pid.dOut", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), dOut);
|
|
|
|
sz = StringPrintf("adpf.%s-pid.output", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), output);
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
2021-10-26 05:48:06 -04:00
|
|
|
return output;
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
|
2021-10-26 05:48:06 -04:00
|
|
|
int64_t durationNanos) {
|
2021-07-07 00:00:43 -04:00
|
|
|
mDescriptor = new AppHintDesc(tgid, uid, threadIds);
|
2021-05-04 23:17:41 -04:00
|
|
|
mDescriptor->duration = std::chrono::nanoseconds(durationNanos);
|
2022-06-02 05:30:18 -04:00
|
|
|
mStaleTimerHandler = sp<StaleTimerHandler>(new StaleTimerHandler(this));
|
|
|
|
mEarlyBoostHandler = sp<EarlyBoostHandler>(new EarlyBoostHandler(this));
|
2021-06-15 01:03:10 -04:00
|
|
|
mPowerManagerHandler = PowerSessionManager::getInstance();
|
2022-06-02 05:30:18 -04:00
|
|
|
mLastUpdatedTime.store(std::chrono::steady_clock::now());
|
|
|
|
mLastStartedTimeNs =
|
|
|
|
std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
(std::chrono::steady_clock::now() - mDescriptor->duration).time_since_epoch())
|
|
|
|
.count();
|
|
|
|
mLastDurationNs = durationNanos;
|
|
|
|
mWorkPeriodNs = durationNanos;
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-17 00:20:44 -04:00
|
|
|
const std::string idstr = getIdString();
|
2021-06-20 12:22:47 -04:00
|
|
|
std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
|
2021-06-20 12:22:47 -04:00
|
|
|
sz = StringPrintf("adpf.%s-active", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
|
|
|
|
}
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerSessionManager::getInstance()->addPowerSession(this);
|
2021-06-26 13:39:31 -04:00
|
|
|
// init boost
|
2022-06-10 06:14:56 -04:00
|
|
|
setSessionUclampMin(HintManager::GetInstance()->GetAdpfProfile()->mUclampMinInit);
|
2021-06-17 00:20:44 -04:00
|
|
|
ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PowerHintSession::~PowerHintSession() {
|
|
|
|
close();
|
2021-06-17 00:20:44 -04:00
|
|
|
ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-17 00:20:44 -04:00
|
|
|
const std::string idstr = getIdString();
|
2021-06-20 12:22:47 -04:00
|
|
|
std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
2021-06-20 12:22:47 -04:00
|
|
|
sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
2021-06-20 12:22:47 -04:00
|
|
|
sz = sz = StringPrintf("adpf.%s-active", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
|
|
|
}
|
|
|
|
delete mDescriptor;
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:20:44 -04:00
|
|
|
std::string PowerHintSession::getIdString() const {
|
|
|
|
std::string idstr = StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR, mDescriptor->tgid,
|
|
|
|
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
|
|
|
|
return idstr;
|
|
|
|
}
|
|
|
|
|
2022-04-29 17:05:38 -04:00
|
|
|
bool PowerHintSession::isAppSession() {
|
|
|
|
// Check if uid is in range reserved for applications
|
|
|
|
return mDescriptor->uid >= AID_APP_START;
|
|
|
|
}
|
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
void PowerHintSession::updateUniveralBoostMode() {
|
2022-04-29 17:05:38 -04:00
|
|
|
if (!isAppSession()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-17 08:02:41 -05:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string tag = StringPrintf("%s:updateUniveralBoostMode()", getIdString().c_str());
|
|
|
|
ATRACE_BEGIN(tag.c_str());
|
|
|
|
}
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
|
2022-01-17 08:02:41 -05:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
ATRACE_END();
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 02:43:55 -04:00
|
|
|
int PowerHintSession::setSessionUclampMin(int32_t min) {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mSessionLock);
|
|
|
|
mDescriptor->current_min = min;
|
|
|
|
}
|
2022-06-02 05:30:18 -04:00
|
|
|
if (min) {
|
|
|
|
mStaleTimerHandler->updateTimer();
|
|
|
|
}
|
2022-05-19 02:43:55 -04:00
|
|
|
PowerSessionManager::getInstance()->setUclampMin(this, min);
|
2022-06-02 05:30:18 -04:00
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-20 12:22:47 -04:00
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), min);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-19 02:43:55 -04:00
|
|
|
int PowerHintSession::getUclampMin() {
|
|
|
|
return mDescriptor->current_min;
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:39:51 -04:00
|
|
|
void PowerHintSession::dumpToStream(std::ostream &stream) {
|
2022-06-02 05:30:18 -04:00
|
|
|
stream << "ID.Min.Act.Timeout(" << getIdString();
|
2022-04-21 13:39:51 -04:00
|
|
|
stream << ", " << mDescriptor->current_min;
|
|
|
|
stream << ", " << mDescriptor->is_active;
|
2022-06-02 05:30:18 -04:00
|
|
|
stream << ", " << isTimeout() << ")";
|
2022-04-21 13:39:51 -04:00
|
|
|
}
|
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
ndk::ScopedAStatus PowerHintSession::pause() {
|
2022-03-30 02:43:21 -04:00
|
|
|
if (mSessionClosed) {
|
|
|
|
ALOGE("Error: session is dead");
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
if (!mDescriptor->is_active.load())
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
// Reset to default uclamp value.
|
|
|
|
mDescriptor->is_active.store(false);
|
2022-05-19 02:43:55 -04:00
|
|
|
setStale();
|
2021-05-04 23:17:41 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-20 12:22:47 -04:00
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-active", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
|
|
|
|
}
|
|
|
|
updateUniveralBoostMode();
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus PowerHintSession::resume() {
|
2022-03-30 02:43:21 -04:00
|
|
|
if (mSessionClosed) {
|
|
|
|
ALOGE("Error: session is dead");
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
if (mDescriptor->is_active.load())
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
mDescriptor->is_active.store(true);
|
2021-06-26 13:39:31 -04:00
|
|
|
// resume boost
|
2022-05-19 02:43:55 -04:00
|
|
|
setSessionUclampMin(mDescriptor->current_min);
|
2021-05-04 23:17:41 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-20 12:22:47 -04:00
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-active", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
|
|
|
|
}
|
|
|
|
updateUniveralBoostMode();
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus PowerHintSession::close() {
|
2021-07-29 11:58:22 -04:00
|
|
|
bool sessionClosedExpectedToBe = false;
|
|
|
|
if (!mSessionClosed.compare_exchange_strong(sessionClosedExpectedToBe, true)) {
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
2022-06-10 06:14:56 -04:00
|
|
|
// Remove the session from PowerSessionManager first to avoid racing.
|
|
|
|
PowerSessionManager::getInstance()->removePowerSession(this);
|
|
|
|
setSessionUclampMin(0);
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mSessionLock);
|
|
|
|
mSessionClosed.store(true);
|
|
|
|
}
|
2022-06-02 05:30:18 -04:00
|
|
|
mDescriptor->is_active.store(false);
|
|
|
|
mEarlyBoostHandler->setSessionDead();
|
|
|
|
mStaleTimerHandler->setSessionDead();
|
2021-05-04 23:17:41 -04:00
|
|
|
updateUniveralBoostMode();
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus PowerHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) {
|
2022-03-30 02:43:21 -04:00
|
|
|
if (mSessionClosed) {
|
|
|
|
ALOGE("Error: session is dead");
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
if (targetDurationNanos <= 0) {
|
|
|
|
ALOGE("Error: targetDurationNanos(%" PRId64 ") should bigger than 0", targetDurationNanos);
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
|
|
|
}
|
2021-10-26 05:48:06 -04:00
|
|
|
targetDurationNanos =
|
|
|
|
targetDurationNanos * HintManager::GetInstance()->GetAdpfProfile()->mTargetTimeFactor;
|
2021-05-04 23:17:41 -04:00
|
|
|
ALOGV("update target duration: %" PRId64 " ns", targetDurationNanos);
|
|
|
|
|
|
|
|
mDescriptor->duration = std::chrono::nanoseconds(targetDurationNanos);
|
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-20 12:22:47 -04:00
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-target", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration(
|
|
|
|
const std::vector<WorkDuration> &actualDurations) {
|
2022-03-30 02:43:21 -04:00
|
|
|
if (mSessionClosed) {
|
|
|
|
ALOGE("Error: session is dead");
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
if (mDescriptor->duration.count() == 0LL) {
|
|
|
|
ALOGE("Expect to call updateTargetWorkDuration() first.");
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
|
|
|
if (actualDurations.size() == 0) {
|
|
|
|
ALOGE("Error: duration.size() shouldn't be %zu.", actualDurations.size());
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
|
|
|
}
|
|
|
|
if (!mDescriptor->is_active.load()) {
|
|
|
|
ALOGE("Error: shouldn't report duration during pause state.");
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
|
|
|
}
|
2021-10-26 05:48:06 -04:00
|
|
|
std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
|
2021-11-09 09:44:46 -05:00
|
|
|
mDescriptor->update_count++;
|
2022-06-02 05:30:18 -04:00
|
|
|
bool isFirstFrame = isTimeout();
|
2021-11-09 09:44:46 -05:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = getIdString();
|
2021-10-26 05:48:06 -04:00
|
|
|
std::string sz = StringPrintf("adpf.%s-batch_size", idstr.c_str());
|
2021-11-09 09:44:46 -05:00
|
|
|
ATRACE_INT(sz.c_str(), actualDurations.size());
|
2021-10-26 05:48:06 -04:00
|
|
|
sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
|
2021-11-09 09:44:46 -05:00
|
|
|
ATRACE_INT(sz.c_str(), actualDurations.back().durationNanos);
|
2021-10-26 05:48:06 -04:00
|
|
|
sz = StringPrintf("adpf.%s-target", idstr.c_str());
|
2021-11-09 09:44:46 -05:00
|
|
|
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
|
2021-10-26 05:48:06 -04:00
|
|
|
sz = StringPrintf("adpf.%s-hint.count", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->update_count);
|
|
|
|
sz = StringPrintf("adpf.%s-hint.overtime", idstr.c_str());
|
2021-11-09 09:44:46 -05:00
|
|
|
ATRACE_INT(sz.c_str(),
|
|
|
|
actualDurations.back().durationNanos - mDescriptor->duration.count() > 0);
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
2021-11-09 09:44:46 -05:00
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
mLastUpdatedTime.store(std::chrono::steady_clock::now());
|
|
|
|
if (isFirstFrame) {
|
|
|
|
updateUniveralBoostMode();
|
|
|
|
}
|
2021-10-26 05:48:06 -04:00
|
|
|
|
|
|
|
if (!adpfConfig->mPidOn) {
|
2022-05-19 02:43:55 -04:00
|
|
|
setSessionUclampMin(adpfConfig->mUclampMinHigh);
|
2021-10-26 05:48:06 -04:00
|
|
|
return ndk::ScopedAStatus::ok();
|
2021-06-20 12:22:47 -04:00
|
|
|
}
|
2021-10-26 05:48:06 -04:00
|
|
|
int64_t output = convertWorkDurationToBoostByPid(
|
|
|
|
adpfConfig, mDescriptor->duration, actualDurations, &(mDescriptor->integral_error),
|
|
|
|
&(mDescriptor->previous_error), getIdString());
|
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
/* apply to all the threads in the group */
|
2022-05-12 12:54:25 -04:00
|
|
|
int next_min = std::min(static_cast<int>(adpfConfig->mUclampMinHigh),
|
|
|
|
mDescriptor->current_min + static_cast<int>(output));
|
|
|
|
next_min = std::max(static_cast<int>(adpfConfig->mUclampMinLow), next_min);
|
2022-05-19 02:43:55 -04:00
|
|
|
setSessionUclampMin(next_min);
|
2022-06-02 05:30:18 -04:00
|
|
|
mStaleTimerHandler->updateTimer(getStaleTime());
|
|
|
|
if (HintManager::GetInstance()->GetAdpfProfile()->mEarlyBoostOn) {
|
|
|
|
updateWorkPeriod(actualDurations);
|
|
|
|
mEarlyBoostHandler->updateTimer(getEarlyBoostTime());
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AppHintDesc::toString() const {
|
|
|
|
std::string out =
|
|
|
|
StringPrintf("session %" PRIxPTR "\n", reinterpret_cast<uintptr_t>(this) & 0xffff);
|
|
|
|
const int64_t durationNanos = duration.count();
|
|
|
|
out.append(StringPrintf(" duration: %" PRId64 " ns\n", durationNanos));
|
|
|
|
out.append(StringPrintf(" uclamp.min: %d \n", current_min));
|
|
|
|
out.append(StringPrintf(" uid: %d, tgid: %d\n", uid, tgid));
|
|
|
|
|
|
|
|
out.append(" threadIds: [");
|
|
|
|
bool first = true;
|
|
|
|
for (int tid : threadIds) {
|
|
|
|
if (!first) {
|
|
|
|
out.append(", ");
|
|
|
|
}
|
|
|
|
out.append(std::to_string(tid));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
out.append("]\n");
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PowerHintSession::isActive() {
|
|
|
|
return mDescriptor->is_active.load();
|
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
bool PowerHintSession::isTimeout() {
|
2021-05-04 23:17:41 -04:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2022-06-02 05:30:18 -04:00
|
|
|
return now >= getStaleTime();
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
2021-07-07 00:00:43 -04:00
|
|
|
const std::vector<int> &PowerHintSession::getTidList() const {
|
|
|
|
return mDescriptor->threadIds;
|
|
|
|
}
|
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
void PowerHintSession::setStale() {
|
|
|
|
// Reset to default uclamp value.
|
2022-05-19 02:43:55 -04:00
|
|
|
PowerSessionManager::getInstance()->setUclampMin(this, 0);
|
2021-05-04 23:17:41 -04:00
|
|
|
// Deliver a task to check if all sessions are inactive.
|
|
|
|
updateUniveralBoostMode();
|
2022-06-02 05:30:18 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 02:43:55 -04:00
|
|
|
void PowerHintSession::wakeup() {
|
2022-06-03 17:54:29 -04:00
|
|
|
std::lock_guard<std::mutex> guard(mSessionLock);
|
|
|
|
|
|
|
|
// We only wake up non-paused and stale sessions
|
2022-06-02 05:30:18 -04:00
|
|
|
if (mSessionClosed || !isActive() || !isTimeout())
|
2022-06-03 17:54:29 -04:00
|
|
|
return;
|
2022-05-19 02:43:55 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2022-06-02 05:30:18 -04:00
|
|
|
std::string tag = StringPrintf("wakeup.%s(a:%d,s:%d)", getIdString().c_str(), isActive(),
|
|
|
|
isTimeout());
|
2022-05-19 02:43:55 -04:00
|
|
|
ATRACE_NAME(tag.c_str());
|
|
|
|
}
|
|
|
|
std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
|
|
|
|
int min = std::max(mDescriptor->current_min, static_cast<int>(adpfConfig->mUclampMinInit));
|
2022-06-03 17:54:29 -04:00
|
|
|
mDescriptor->current_min = min;
|
2022-05-19 02:43:55 -04:00
|
|
|
PowerSessionManager::getInstance()->setUclampMinLocked(this, min);
|
2022-06-02 05:30:18 -04:00
|
|
|
mStaleTimerHandler->updateTimer();
|
2022-05-19 02:43:55 -04:00
|
|
|
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
void PowerHintSession::updateWorkPeriod(const std::vector<WorkDuration> &actualDurations) {
|
2022-01-17 08:02:41 -05:00
|
|
|
if (actualDurations.size() == 0)
|
|
|
|
return;
|
|
|
|
if (actualDurations.size() >= 2) {
|
|
|
|
const WorkDuration &last = actualDurations[actualDurations.size() - 2];
|
2022-06-02 05:30:18 -04:00
|
|
|
mLastStartedTimeNs = last.timeStampNanos - last.durationNanos;
|
2022-01-17 08:02:41 -05:00
|
|
|
}
|
|
|
|
const WorkDuration ¤t = actualDurations.back();
|
|
|
|
int64_t curr_start = current.timeStampNanos - current.durationNanos;
|
2022-06-10 06:14:56 -04:00
|
|
|
int64_t period = curr_start - mLastStartedTimeNs;
|
2022-06-02 05:30:18 -04:00
|
|
|
if (period > 0 && period < mDescriptor->duration.count() * 2) {
|
2022-01-17 08:02:41 -05:00
|
|
|
// Accounting workload period with moving average for the last 10 workload.
|
2022-06-02 05:30:18 -04:00
|
|
|
mWorkPeriodNs = 0.9 * mWorkPeriodNs + 0.1 * period;
|
2021-10-17 22:02:28 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2022-06-02 05:30:18 -04:00
|
|
|
const std::string idstr = getIdString();
|
2022-01-17 08:02:41 -05:00
|
|
|
std::string sz = StringPrintf("adpf.%s-timer.period", idstr.c_str());
|
2022-06-02 05:30:18 -04:00
|
|
|
ATRACE_INT(sz.c_str(), mWorkPeriodNs);
|
2021-10-17 22:02:28 -04:00
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
2022-06-02 05:30:18 -04:00
|
|
|
mLastStartedTimeNs = curr_start;
|
|
|
|
mLastDurationNs = current.durationNanos;
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
time_point<steady_clock> PowerHintSession::getEarlyBoostTime() {
|
2022-01-17 08:02:41 -05:00
|
|
|
std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
|
|
|
|
int64_t earlyBoostTimeoutNs =
|
2022-06-02 05:30:18 -04:00
|
|
|
(int64_t)mDescriptor->duration.count() * adpfConfig->mEarlyBoostTimeFactor;
|
|
|
|
time_point<steady_clock> nextStartTime =
|
|
|
|
mLastUpdatedTime.load() + nanoseconds(mWorkPeriodNs - mLastDurationNs);
|
|
|
|
return nextStartTime + nanoseconds(earlyBoostTimeoutNs);
|
2022-01-17 08:02:41 -05:00
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
time_point<steady_clock> PowerHintSession::getStaleTime() {
|
2021-06-17 00:20:44 -04:00
|
|
|
return mLastUpdatedTime.load() +
|
2021-10-26 05:48:06 -04:00
|
|
|
nanoseconds(static_cast<int64_t>(
|
2022-06-02 05:30:18 -04:00
|
|
|
mDescriptor->duration.count() *
|
2021-10-26 05:48:06 -04:00
|
|
|
HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor));
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
void PowerHintSession::StaleTimerHandler::updateTimer() {
|
|
|
|
time_point<steady_clock> staleTime =
|
|
|
|
std::chrono::steady_clock::now() +
|
|
|
|
nanoseconds(static_cast<int64_t>(
|
|
|
|
mSession->mDescriptor->duration.count() *
|
|
|
|
HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor));
|
|
|
|
updateTimer(staleTime);
|
2022-01-17 08:02:41 -05:00
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
void PowerHintSession::StaleTimerHandler::updateTimer(time_point<steady_clock> staleTime) {
|
|
|
|
mStaleTime.store(staleTime);
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mMessageLock);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessage(mSession->mStaleTimerHandler,
|
|
|
|
NULL);
|
2022-03-30 02:43:21 -04:00
|
|
|
}
|
2022-06-02 05:30:18 -04:00
|
|
|
mIsMonitoring.store(true);
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-timer.stale", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) {
|
|
|
|
if (mIsSessionDead) {
|
2022-05-19 02:43:55 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2022-06-02 05:30:18 -04:00
|
|
|
int64_t next =
|
|
|
|
static_cast<int64_t>(duration_cast<nanoseconds>(mStaleTime.load() - now).count());
|
|
|
|
if (next > 0) {
|
|
|
|
// Schedule for the stale timeout check.
|
|
|
|
std::lock_guard<std::mutex> guard(mMessageLock);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
|
2022-01-17 08:02:41 -05:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
|
2022-06-02 05:30:18 -04:00
|
|
|
next, mSession->mStaleTimerHandler, NULL);
|
|
|
|
} else {
|
|
|
|
mSession->setStale();
|
|
|
|
mIsMonitoring.store(false);
|
2022-01-17 08:02:41 -05:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
2022-06-02 05:30:18 -04:00
|
|
|
std::string sz = StringPrintf("adpf.%s-timer.earlyboost", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
2022-01-17 08:02:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
2022-06-02 05:30:18 -04:00
|
|
|
std::string sz = StringPrintf("adpf.%s-timer.stale", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), mIsMonitoring ? 0 : 1);
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
2022-03-30 02:43:21 -04:00
|
|
|
}
|
|
|
|
|
2022-06-02 05:30:18 -04:00
|
|
|
void PowerHintSession::StaleTimerHandler::setSessionDead() {
|
2022-03-30 02:43:21 -04:00
|
|
|
std::lock_guard<std::mutex> guard(mStaleLock);
|
|
|
|
mIsSessionDead = true;
|
2022-06-02 05:30:18 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::EarlyBoostHandler::updateTimer(time_point<steady_clock> boostTime) {
|
|
|
|
mBoostTime.store(boostTime);
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mMessageLock);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mEarlyBoostHandler);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessage(mSession->mEarlyBoostHandler,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
mIsMonitoring.store(true);
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-timer.earlyboost", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::EarlyBoostHandler::handleMessage(const Message &) {
|
|
|
|
std::lock_guard<std::mutex> guard(mBoostLock);
|
|
|
|
if (mIsSessionDead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
int64_t next =
|
|
|
|
static_cast<int64_t>(duration_cast<nanoseconds>(mBoostTime.load() - now).count());
|
|
|
|
if (next > 0) {
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-timer.earlyboost", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 1);
|
|
|
|
}
|
|
|
|
std::lock_guard<std::mutex> guard(mMessageLock);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mEarlyBoostHandler);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
|
|
|
|
next, mSession->mEarlyBoostHandler, NULL);
|
|
|
|
} else {
|
|
|
|
std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
|
|
|
|
PowerSessionManager::getInstance()->setUclampMin(mSession, adpfConfig->mUclampMinHigh);
|
|
|
|
mIsMonitoring.store(false);
|
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), adpfConfig->mUclampMinHigh);
|
|
|
|
sz = StringPrintf("adpf.%s-timer.earlyboost", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::EarlyBoostHandler::setSessionDead() {
|
|
|
|
std::lock_guard<std::mutex> guard(mBoostLock);
|
|
|
|
mIsSessionDead = true;
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mEarlyBoostHandler);
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pixel
|
|
|
|
} // namespace impl
|
|
|
|
} // namespace power
|
|
|
|
} // namespace hardware
|
|
|
|
} // namespace google
|
|
|
|
} // namespace aidl
|