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)
|
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/parsedouble.h>
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.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-07-29 11:58:22 -04:00
|
|
|
#include <atomic>
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
#include "PowerHintSession.h"
|
|
|
|
#include "PowerSessionManager.h"
|
|
|
|
|
|
|
|
namespace aidl {
|
|
|
|
namespace google {
|
|
|
|
namespace hardware {
|
|
|
|
namespace power {
|
|
|
|
namespace impl {
|
|
|
|
namespace pixel {
|
|
|
|
|
|
|
|
using ::android::base::StringPrintf;
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::nanoseconds;
|
|
|
|
|
2021-07-07 00:00:43 -04:00
|
|
|
constexpr char kPowerHalAdpfPidPOver[] = "vendor.powerhal.adpf.pid_p.over";
|
|
|
|
constexpr char kPowerHalAdpfPidPUnder[] = "vendor.powerhal.adpf.pid_p.under";
|
|
|
|
constexpr char kPowerHalAdpfPidI[] = "vendor.powerhal.adpf.pid_i";
|
|
|
|
constexpr char kPowerHalAdpfPidDOver[] = "vendor.powerhal.adpf.pid_d.over";
|
|
|
|
constexpr char kPowerHalAdpfPidDUnder[] = "vendor.powerhal.adpf.pid_d.under";
|
|
|
|
constexpr char kPowerHalAdpfPidIInit[] = "vendor.powerhal.adpf.pid_i.init";
|
|
|
|
constexpr char kPowerHalAdpfPidIHighLimit[] = "vendor.powerhal.adpf.pid_i.high_limit";
|
|
|
|
constexpr char kPowerHalAdpfPidILowLimit[] = "vendor.powerhal.adpf.pid_i.low_limit";
|
2021-05-04 23:17:41 -04:00
|
|
|
constexpr char kPowerHalAdpfUclampEnable[] = "vendor.powerhal.adpf.uclamp";
|
2021-07-07 00:00:43 -04:00
|
|
|
constexpr char kPowerHalAdpfUclampMinGranularity[] = "vendor.powerhal.adpf.uclamp_min.granularity";
|
|
|
|
constexpr char kPowerHalAdpfUclampMinHighLimit[] = "vendor.powerhal.adpf.uclamp_min.high_limit";
|
|
|
|
constexpr char kPowerHalAdpfUclampMinLowLimit[] = "vendor.powerhal.adpf.uclamp_min.low_limit";
|
2021-06-17 00:20:44 -04:00
|
|
|
constexpr char kPowerHalAdpfStaleTimeFactor[] = "vendor.powerhal.adpf.stale_timeout_factor";
|
2021-06-23 18:58:56 -04:00
|
|
|
constexpr char kPowerHalAdpfPSamplingWindow[] = "vendor.powerhal.adpf.p.window";
|
|
|
|
constexpr char kPowerHalAdpfISamplingWindow[] = "vendor.powerhal.adpf.i.window";
|
|
|
|
constexpr char kPowerHalAdpfDSamplingWindow[] = "vendor.powerhal.adpf.d.window";
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/* there is no glibc or bionic wrapper */
|
|
|
|
struct sched_attr {
|
|
|
|
__u32 size;
|
|
|
|
__u32 sched_policy;
|
|
|
|
__u64 sched_flags;
|
|
|
|
__s32 sched_nice;
|
|
|
|
__u32 sched_priority;
|
|
|
|
__u64 sched_runtime;
|
|
|
|
__u64 sched_deadline;
|
|
|
|
__u64 sched_period;
|
|
|
|
__u32 sched_util_min;
|
|
|
|
__u32 sched_util_max;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sched_setattr(int pid, struct sched_attr *attr, unsigned int flags) {
|
|
|
|
static const bool kPowerHalAdpfUclamp =
|
|
|
|
::android::base::GetBoolProperty(kPowerHalAdpfUclampEnable, true);
|
|
|
|
if (!kPowerHalAdpfUclamp) {
|
|
|
|
ALOGV("PowerHintSession:%s: skip", __func__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return syscall(__NR_sched_setattr, pid, attr, flags);
|
|
|
|
}
|
|
|
|
|
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-06-17 00:20:44 -04:00
|
|
|
static double getDoubleProperty(const char *prop, double value) {
|
2021-05-04 23:17:41 -04:00
|
|
|
std::string result = ::android::base::GetProperty(prop, std::to_string(value).c_str());
|
|
|
|
if (!::android::base::ParseDouble(result.c_str(), &value)) {
|
|
|
|
ALOGE("PowerHintSession : failed to parse double in %s", prop);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-09-08 04:37:34 -04:00
|
|
|
static double sPidPOver = getDoubleProperty(kPowerHalAdpfPidPOver, 2.0);
|
|
|
|
static double sPidPUnder = getDoubleProperty(kPowerHalAdpfPidPUnder, 1.0);
|
2021-05-04 23:17:41 -04:00
|
|
|
static double sPidI = getDoubleProperty(kPowerHalAdpfPidI, 0.001);
|
2021-07-26 22:45:07 -04:00
|
|
|
static double sPidDOver = getDoubleProperty(kPowerHalAdpfPidDOver, 500.0);
|
2021-07-07 00:00:43 -04:00
|
|
|
static double sPidDUnder = getDoubleProperty(kPowerHalAdpfPidDUnder, 0.0);
|
2021-05-04 23:17:41 -04:00
|
|
|
static const int64_t sPidIInit =
|
|
|
|
(sPidI == 0) ? 0
|
|
|
|
: static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
|
2021-07-07 00:00:43 -04:00
|
|
|
kPowerHalAdpfPidIInit, 200) /
|
2021-05-04 23:17:41 -04:00
|
|
|
sPidI);
|
2021-07-07 00:00:43 -04:00
|
|
|
static const int64_t sPidIHighLimit =
|
2021-05-04 23:17:41 -04:00
|
|
|
(sPidI == 0) ? 0
|
2021-07-07 00:00:43 -04:00
|
|
|
: static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
|
|
|
|
kPowerHalAdpfPidIHighLimit, 512) /
|
|
|
|
sPidI);
|
|
|
|
static const int64_t sPidILowLimit =
|
|
|
|
(sPidI == 0) ? 0
|
|
|
|
: static_cast<int64_t>(::android::base::GetIntProperty<int64_t>(
|
2021-09-08 04:37:34 -04:00
|
|
|
kPowerHalAdpfPidILowLimit, -30) /
|
2021-07-07 00:00:43 -04:00
|
|
|
sPidI);
|
|
|
|
static const int32_t sUclampMinHighLimit =
|
2021-09-08 04:37:34 -04:00
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinHighLimit, 384);
|
2021-07-07 00:00:43 -04:00
|
|
|
static const int32_t sUclampMinLowLimit =
|
2021-09-08 04:37:34 -04:00
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinLowLimit, 2);
|
2021-07-07 00:00:43 -04:00
|
|
|
static const uint32_t sUclampMinGranularity =
|
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampMinGranularity, 5);
|
2021-06-17 00:20:44 -04:00
|
|
|
static const int64_t sStaleTimeFactor =
|
2021-06-23 18:58:56 -04:00
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfStaleTimeFactor, 20);
|
|
|
|
static const int64_t sPSamplingWindow =
|
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfPSamplingWindow, 1);
|
|
|
|
static const int64_t sISamplingWindow =
|
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfISamplingWindow, 0);
|
|
|
|
static const int64_t sDSamplingWindow =
|
|
|
|
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfDSamplingWindow, 1);
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
|
2021-06-17 00:20:44 -04:00
|
|
|
int64_t durationNanos, const nanoseconds adpfRate)
|
|
|
|
: kAdpfRate(adpfRate) {
|
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);
|
2021-06-17 00:20:44 -04:00
|
|
|
mStaleHandler = sp<StaleHandler>(new StaleHandler(this));
|
2021-06-15 01:03:10 -04:00
|
|
|
mPowerManagerHandler = PowerSessionManager::getInstance();
|
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-10-17 22:02:28 -04:00
|
|
|
sz = StringPrintf("adpf.%s-stale", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), isStale());
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerSessionManager::getInstance()->addPowerSession(this);
|
2021-06-26 13:39:31 -04:00
|
|
|
// init boost
|
2021-07-07 00:00:43 -04:00
|
|
|
setUclamp(sUclampMinHighLimit);
|
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);
|
|
|
|
}
|
2022-03-30 02:43:21 -04:00
|
|
|
mStaleHandler->setSessionDead();
|
2021-05-04 23:17:41 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-04 23:17:41 -04:00
|
|
|
void PowerHintSession::updateUniveralBoostMode() {
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int PowerHintSession::setUclamp(int32_t min, int32_t max) {
|
|
|
|
std::lock_guard<std::mutex> guard(mLock);
|
|
|
|
min = std::max(0, min);
|
|
|
|
min = std::min(min, max);
|
|
|
|
max = std::max(0, max);
|
|
|
|
max = std::max(min, max);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
for (const auto tid : mDescriptor->threadIds) {
|
|
|
|
sched_attr attr = {};
|
|
|
|
attr.size = sizeof(attr);
|
|
|
|
|
|
|
|
attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP);
|
|
|
|
attr.sched_util_min = min;
|
|
|
|
attr.sched_util_max = max;
|
|
|
|
|
|
|
|
int ret = sched_setattr(tid, &attr, 0);
|
|
|
|
if (ret) {
|
|
|
|
ALOGW("sched_setattr failed for thread %d, err=%d", tid, errno);
|
|
|
|
}
|
|
|
|
ALOGV("PowerHintSession tid: %d, uclamp(%d, %d)", tid, min, max);
|
|
|
|
}
|
2021-07-07 00:00:43 -04:00
|
|
|
mDescriptor->current_min = min;
|
2021-05-04 23:17:41 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-07-07 00:00:43 -04:00
|
|
|
setUclamp(0);
|
2021-05-04 23:17:41 -04:00
|
|
|
mDescriptor->is_active.store(false);
|
|
|
|
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);
|
|
|
|
mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
|
2021-06-26 13:39:31 -04:00
|
|
|
// resume boost
|
2021-07-07 00:00:43 -04:00
|
|
|
setUclamp(sUclampMinHighLimit);
|
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);
|
|
|
|
}
|
2021-07-07 00:00:43 -04:00
|
|
|
setUclamp(0);
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerSessionManager::getInstance()->removePowerSession(this);
|
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);
|
|
|
|
}
|
|
|
|
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-06-15 01:03:10 -04:00
|
|
|
if (PowerHintMonitor::getInstance()->isRunning() && isStale()) {
|
2021-09-08 04:37:34 -04:00
|
|
|
mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
|
2021-05-04 23:17:41 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-20 12:22:47 -04:00
|
|
|
const std::string idstr = getIdString();
|
2021-09-08 04:37:34 -04:00
|
|
|
std::string sz = StringPrintf("adpf.%s-wakeup", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->integral_error);
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int64_t targetDurationNanos = (int64_t)mDescriptor->duration.count();
|
2021-06-23 18:58:56 -04:00
|
|
|
int64_t length = actualDurations.size();
|
|
|
|
int64_t p_start =
|
|
|
|
sPSamplingWindow == 0 || sPSamplingWindow > length ? 0 : length - sPSamplingWindow;
|
|
|
|
int64_t i_start =
|
|
|
|
sISamplingWindow == 0 || sISamplingWindow > length ? 0 : length - sISamplingWindow;
|
|
|
|
int64_t d_start =
|
|
|
|
sDSamplingWindow == 0 || sDSamplingWindow > length ? 0 : length - sDSamplingWindow;
|
2021-05-04 23:17:41 -04:00
|
|
|
int64_t dt = ns_to_100us(targetDurationNanos);
|
2021-06-18 00:32:18 -04:00
|
|
|
int64_t err_sum = 0;
|
|
|
|
int64_t derivative_sum = 0;
|
2021-06-23 18:58:56 -04:00
|
|
|
for (int64_t i = std::min({p_start, i_start, d_start}); i < length; i++) {
|
2021-06-17 00:20:44 -04:00
|
|
|
int64_t actualDurationNanos = actualDurations[i].durationNanos;
|
2021-05-04 23:17:41 -04:00
|
|
|
if (std::abs(actualDurationNanos) > targetDurationNanos * 20) {
|
|
|
|
ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")",
|
|
|
|
actualDurationNanos, targetDurationNanos);
|
|
|
|
}
|
|
|
|
// PID control algorithm
|
2021-07-07 00:00:43 -04:00
|
|
|
int64_t error = ns_to_100us(actualDurationNanos - targetDurationNanos);
|
2021-06-23 18:58:56 -04:00
|
|
|
if (i >= d_start) {
|
|
|
|
derivative_sum += error - mDescriptor->previous_error;
|
|
|
|
}
|
|
|
|
if (i >= p_start) {
|
|
|
|
err_sum += error;
|
|
|
|
}
|
|
|
|
if (i >= i_start) {
|
|
|
|
mDescriptor->integral_error = mDescriptor->integral_error + error * dt;
|
2021-07-07 00:00:43 -04:00
|
|
|
mDescriptor->integral_error = std::min(sPidIHighLimit, mDescriptor->integral_error);
|
|
|
|
mDescriptor->integral_error = std::max(sPidILowLimit, mDescriptor->integral_error);
|
2021-06-23 18:58:56 -04:00
|
|
|
}
|
2021-06-18 00:32:18 -04:00
|
|
|
mDescriptor->previous_error = error;
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
2021-09-08 04:37:34 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-err", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), err_sum / (length - p_start));
|
|
|
|
sz = StringPrintf("adpf.%s-integral", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->integral_error);
|
|
|
|
sz = StringPrintf("adpf.%s-derivative", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), derivative_sum / dt / (length - d_start));
|
|
|
|
}
|
2021-07-07 00:00:43 -04:00
|
|
|
int64_t pOut = static_cast<int64_t>((err_sum > 0 ? sPidPOver : sPidPUnder) * err_sum /
|
|
|
|
(length - p_start));
|
2021-05-04 23:17:41 -04:00
|
|
|
int64_t iOut = static_cast<int64_t>(sPidI * mDescriptor->integral_error);
|
2021-07-07 00:00:43 -04:00
|
|
|
int64_t dOut = static_cast<int64_t>((derivative_sum > 0 ? sPidDOver : sPidDUnder) *
|
|
|
|
derivative_sum / dt / (length - d_start));
|
2021-05-04 23:17:41 -04:00
|
|
|
|
|
|
|
int64_t output = pOut + iOut + dOut;
|
2021-09-08 04:37:34 -04:00
|
|
|
|
2021-06-20 12:22:47 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-actl_last", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), actualDurations[length - 1].durationNanos);
|
|
|
|
sz = StringPrintf("adpf.%s-target", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
|
|
|
|
sz = StringPrintf("adpf.%s-sample_size", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), length);
|
|
|
|
sz = StringPrintf("adpf.%s-pid.count", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), mDescriptor->update_count);
|
|
|
|
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-09-08 04:37:34 -04:00
|
|
|
sz = StringPrintf("adpf.%s-stale", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), isStale());
|
|
|
|
sz = StringPrintf("adpf.%s-pid.overtime", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), err_sum > 0);
|
2021-06-20 12:22:47 -04:00
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
mDescriptor->update_count++;
|
|
|
|
|
|
|
|
mStaleHandler->updateStaleTimer();
|
|
|
|
|
|
|
|
/* apply to all the threads in the group */
|
|
|
|
if (output != 0) {
|
2021-09-08 04:37:34 -04:00
|
|
|
int next_min = std::min(sUclampMinHighLimit, static_cast<int>(output));
|
2021-07-07 00:00:43 -04:00
|
|
|
next_min = std::max(sUclampMinLowLimit, next_min);
|
|
|
|
if (std::abs(mDescriptor->current_min - next_min) > sUclampMinGranularity) {
|
|
|
|
setUclamp(next_min);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PowerHintSession::isStale() {
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
return now >= mStaleHandler->getStaleTime();
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
if (ATRACE_ENABLED()) {
|
2021-06-20 12:22:47 -04:00
|
|
|
const std::string idstr = getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-stale", idstr.c_str());
|
2021-05-04 23:17:41 -04:00
|
|
|
ATRACE_INT(sz.c_str(), 1);
|
|
|
|
}
|
|
|
|
// Reset to default uclamp value.
|
2021-07-07 00:00:43 -04:00
|
|
|
setUclamp(0);
|
2021-05-04 23:17:41 -04:00
|
|
|
// Deliver a task to check if all sessions are inactive.
|
|
|
|
updateUniveralBoostMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::StaleHandler::updateStaleTimer() {
|
|
|
|
std::lock_guard<std::mutex> guard(mStaleLock);
|
2021-06-15 01:03:10 -04:00
|
|
|
if (PowerHintMonitor::getInstance()->isRunning()) {
|
2021-05-04 23:17:41 -04:00
|
|
|
auto when = getStaleTime();
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
mLastUpdatedTime.store(now);
|
|
|
|
if (now > when) {
|
|
|
|
mSession->updateUniveralBoostMode();
|
|
|
|
}
|
|
|
|
if (!mIsMonitoringStale.load()) {
|
|
|
|
auto next = getStaleTime();
|
2022-03-30 02:43:21 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleHandler);
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
|
2022-03-30 02:43:21 -04:00
|
|
|
duration_cast<nanoseconds>(next - now).count(), mSession->mStaleHandler, NULL);
|
2021-05-04 23:17:41 -04:00
|
|
|
mIsMonitoringStale.store(true);
|
|
|
|
}
|
2021-10-17 22:02:28 -04:00
|
|
|
if (ATRACE_ENABLED()) {
|
|
|
|
const std::string idstr = mSession->getIdString();
|
|
|
|
std::string sz = StringPrintf("adpf.%s-stale", idstr.c_str());
|
|
|
|
ATRACE_INT(sz.c_str(), 0);
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time_point<steady_clock> PowerHintSession::StaleHandler::getStaleTime() {
|
2021-06-17 00:20:44 -04:00
|
|
|
return mLastUpdatedTime.load() +
|
|
|
|
std::chrono::duration_cast<milliseconds>(mSession->kAdpfRate) * sStaleTimeFactor;
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::StaleHandler::handleMessage(const Message &) {
|
|
|
|
std::lock_guard<std::mutex> guard(mStaleLock);
|
2022-03-30 02:43:21 -04:00
|
|
|
if (mIsSessionDead) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-04 23:17:41 -04:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
auto when = getStaleTime();
|
|
|
|
// Check if the session is stale based on the last_updated_time.
|
|
|
|
if (now > when) {
|
|
|
|
mSession->setStale();
|
|
|
|
mIsMonitoringStale.store(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Schedule for the next checking time.
|
2022-03-30 02:43:21 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleHandler);
|
2021-06-15 01:03:10 -04:00
|
|
|
PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
|
2022-03-30 02:43:21 -04:00
|
|
|
duration_cast<nanoseconds>(when - now).count(), mSession->mStaleHandler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PowerHintSession::StaleHandler::setSessionDead() {
|
|
|
|
std::lock_guard<std::mutex> guard(mStaleLock);
|
|
|
|
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleHandler);
|
|
|
|
mIsSessionDead = true;
|
2021-05-04 23:17:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pixel
|
|
|
|
} // namespace impl
|
|
|
|
} // namespace power
|
|
|
|
} // namespace hardware
|
|
|
|
} // namespace google
|
|
|
|
} // namespace aidl
|