power-libperfmgr: improve adpf logic

Cache active state and reduce log spam
Add value tracing into libperfmgr
Use adaptive stale timeout based on rate limit

Bug: 191331719
Bug: 191296994
Bug: 177493042
Test: boot
Signed-off-by: Wei Wang <wvw@google.com>
Change-Id: I1c1484c9277209bf68bd287ceae83e2b37684c62
This commit is contained in:
Wei Wang 2021-06-16 21:20:44 -07:00 committed by Arian
parent b3ea64c377
commit 7c42612574
No known key found for this signature in database
GPG Key ID: 48029380598CE3B9
6 changed files with 84 additions and 73 deletions

View File

@ -58,7 +58,8 @@ Power::Power(std::shared_ptr<HintManager> hm)
: mHintManager(hm),
mInteractionHandler(nullptr),
mSustainedPerfModeOn(false),
mAdpfRate(::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) {
mAdpfRateNs(
::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) {
mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
mInteractionHandler->Init();
@ -84,7 +85,7 @@ Power::Power(std::shared_ptr<HintManager> hm)
}
// Now start to take powerhint
LOG(INFO) << "PowerHAL ready to take hints, Adpf update rate: " << mAdpfRate;
LOG(INFO) << "PowerHAL ready to take hints, Adpf update rate: " << mAdpfRateNs;
}
ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
@ -214,7 +215,6 @@ binder_status_t Power::dump(int fd, const char **, uint32_t) {
if (!::android::base::WriteStringToFd(buf, fd)) {
PLOG(ERROR) << "Failed to dump state to fd";
}
// TODO(jimmyshiu@): dump weak_ptr of PowerHintSession
fsync(fd);
return STATUS_OK;
}
@ -223,7 +223,7 @@ ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid,
const std::vector<int32_t> &threadIds,
int64_t durationNanos,
std::shared_ptr<IPowerHintSession> *_aidl_return) {
if (mAdpfRate == -1) {
if (mAdpfRateNs <= 0) {
*_aidl_return = nullptr;
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
@ -232,15 +232,15 @@ ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid,
*_aidl_return = nullptr;
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
std::shared_ptr<IPowerHintSession> session =
ndk::SharedRefBase::make<PowerHintSession>(tgid, uid, threadIds, durationNanos);
std::shared_ptr<IPowerHintSession> session = ndk::SharedRefBase::make<PowerHintSession>(
tgid, uid, threadIds, durationNanos, nanoseconds(mAdpfRateNs));
*_aidl_return = session;
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t *outNanoseconds) {
*outNanoseconds = mAdpfRate;
if (mAdpfRate == -1) {
*outNanoseconds = mAdpfRateNs;
if (mAdpfRateNs <= 0) {
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}

View File

@ -55,7 +55,7 @@ class Power : public ::aidl::android::hardware::power::BnPower {
std::shared_ptr<HintManager> mHintManager;
std::unique_ptr<InteractionHandler> mInteractionHandler;
std::atomic<bool> mSustainedPerfModeOn;
const int64_t mAdpfRate;
const int64_t mAdpfRateNs;
};
} // namespace pixel

View File

@ -51,7 +51,7 @@ constexpr char kPowerHalAdpfPidInitialIntegral[] = "vendor.powerhal.adpf.pid.i_i
constexpr char kPowerHalAdpfUclampEnable[] = "vendor.powerhal.adpf.uclamp";
constexpr char kPowerHalAdpfUclampCapRatio[] = "vendor.powerhal.adpf.uclamp.cap_ratio";
constexpr char kPowerHalAdpfUclampGranularity[] = "vendor.powerhal.adpf.uclamp.granularity";
constexpr char kPowerHalAdpfStaleTimeout[] = "vendor.powerhal.adpf.stale_timeout_ms";
constexpr char kPowerHalAdpfStaleTimeFactor[] = "vendor.powerhal.adpf.stale_timeout_factor";
constexpr char kPowerHalAdpfSamplingWindow[] = "vendor.powerhal.adpf.sampling_window";
namespace {
@ -83,8 +83,8 @@ static inline void TRACE_ADPF_PID(uintptr_t session_id, int32_t uid, int32_t tgi
int64_t err, int64_t integral, int64_t previous, int64_t p,
int64_t i, int64_t d, int32_t output) {
if (ATRACE_ENABLED()) {
std::string idstr = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR, tgid, uid,
session_id & 0xffff);
const std::string idstr = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR, tgid, uid,
session_id & 0xffff);
std::string sz = StringPrintf("%s-pid.count", idstr.c_str());
ATRACE_INT(sz.c_str(), count);
sz = StringPrintf("%s-pid.err", idstr.c_str());
@ -104,11 +104,11 @@ static inline void TRACE_ADPF_PID(uintptr_t session_id, int32_t uid, int32_t tgi
}
}
static int64_t ns_to_100us(int64_t ns) {
static inline int64_t ns_to_100us(int64_t ns) {
return ns / 100000;
}
double getDoubleProperty(const char *prop, double value) {
static double getDoubleProperty(const char *prop, double value) {
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);
@ -134,51 +134,53 @@ static const int sUclampCap =
static_cast<int>(getDoubleProperty(kPowerHalAdpfUclampCapRatio, 0.5) * 1024);
static const uint32_t sUclampGranularity =
::android::base::GetUintProperty<uint32_t>(kPowerHalAdpfUclampGranularity, 5);
static const int64_t sStaleTimeoutMs =
::android::base::GetIntProperty<int64_t>(kPowerHalAdpfStaleTimeout, 3000);
static const int64_t sStaleTimeFactor =
::android::base::GetIntProperty<int64_t>(kPowerHalAdpfStaleTimeFactor, 20);
static const size_t sSamplingWindow =
::android::base::GetUintProperty<size_t>(kPowerHalAdpfSamplingWindow, 1);
} // namespace
PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
int64_t durationNanos) {
int64_t durationNanos, const nanoseconds adpfRate)
: kAdpfRate(adpfRate) {
mDescriptor = new AppHintDesc(tgid, uid, threadIds, sUclampCap);
mDescriptor->duration = std::chrono::nanoseconds(durationNanos);
mStaleHandler = sp<StaleHandler>(new StaleHandler(this, sStaleTimeoutMs));
mStaleHandler = sp<StaleHandler>(new StaleHandler(this));
mPowerManagerHandler = PowerSessionManager::getInstance();
if (ATRACE_ENABLED()) {
std::string sz =
StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
const std::string idstr = getIdString();
std::string sz = StringPrintf("%s-target", idstr.c_str());
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
sz = StringPrintf("%s-active", idstr.c_str());
ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
}
PowerSessionManager::getInstance()->addPowerSession(this);
ALOGD("PowerHintSession created: %s", mDescriptor->toString().c_str());
ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str());
}
PowerHintSession::~PowerHintSession() {
close();
ALOGD("PowerHintSession deleted: %s", mDescriptor->toString().c_str());
ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str());
if (ATRACE_ENABLED()) {
std::string sz =
StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
const std::string idstr = getIdString();
std::string sz = StringPrintf("%s-target", idstr.c_str());
ATRACE_INT(sz.c_str(), 0);
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-actl_last", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
sz = StringPrintf("%s-actl_last", idstr.c_str());
ATRACE_INT(sz.c_str(), 0);
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
sz = sz = StringPrintf("%s-active", idstr.c_str());
ATRACE_INT(sz.c_str(), 0);
}
delete mDescriptor;
}
std::string PowerHintSession::getIdString() const {
std::string idstr = StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR, mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
return idstr;
}
void PowerHintSession::updateUniveralBoostMode() {
PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
}
@ -194,9 +196,6 @@ int PowerHintSession::setUclamp(int32_t min, int32_t max) {
StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-min", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), min);
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-max", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), max);
}
for (const auto tid : mDescriptor->threadIds) {
sched_attr attr = {};
@ -223,7 +222,7 @@ ndk::ScopedAStatus PowerHintSession::pause() {
mDescriptor->is_active.store(false);
if (ATRACE_ENABLED()) {
std::string sz =
StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid,
StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
}
@ -238,7 +237,7 @@ ndk::ScopedAStatus PowerHintSession::resume() {
mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error);
if (ATRACE_ENABLED()) {
std::string sz =
StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid,
StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
}
@ -303,25 +302,15 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration(
int64_t targetDurationNanos = (int64_t)mDescriptor->duration.count();
size_t length = actualDurations.size();
size_t start = sSamplingWindow == 0 || sSamplingWindow > length ? 0 : length - sSamplingWindow;
int64_t actualDurationNanos = 0;
int64_t dt = ns_to_100us(targetDurationNanos);
int64_t error = 0;
int64_t derivative = 0;
for (size_t i = start; i < length; i++) {
actualDurationNanos = actualDurations[i].durationNanos;
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);
}
if (ATRACE_ENABLED()) {
std::string sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-actl_last",
mDescriptor->tgid, mDescriptor->uid,
reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), actualDurationNanos);
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
}
// PID control algorithm
error = ns_to_100us(actualDurationNanos - targetDurationNanos) +
static_cast<int64_t>(sPidOffset);
@ -331,6 +320,19 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration(
derivative = (error - mDescriptor->previous_error) / dt;
mDescriptor->previous_error = error;
}
if (ATRACE_ENABLED()) {
std::string sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-actl_last",
mDescriptor->tgid, mDescriptor->uid,
reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), actualDurations[length - 1].durationNanos);
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid,
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count());
sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-sample_size",
mDescriptor->tgid, mDescriptor->uid,
reinterpret_cast<uintptr_t>(this) & 0xffff);
ATRACE_INT(sz.c_str(), length);
}
int64_t pOut = static_cast<int64_t>(sPidP * error);
int64_t iOut = static_cast<int64_t>(sPidI * mDescriptor->integral_error);
int64_t dOut = static_cast<int64_t>(sPidD * derivative);
@ -418,7 +420,8 @@ void PowerHintSession::StaleHandler::updateStaleTimer() {
}
time_point<steady_clock> PowerHintSession::StaleHandler::getStaleTime() {
return mLastUpdatedTime.load() + kStaleTimeout;
return mLastUpdatedTime.load() +
std::chrono::duration_cast<milliseconds>(mSession->kAdpfRate) * sStaleTimeFactor;
}
void PowerHintSession::StaleHandler::handleMessage(const Message &) {

View File

@ -68,8 +68,8 @@ struct AppHintDesc {
class PowerHintSession : public BnPowerHintSession {
public:
PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
int64_t durationNanos);
explicit PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
int64_t durationNanos, nanoseconds adpfRate);
~PowerHintSession();
ndk::ScopedAStatus close() override;
ndk::ScopedAStatus pause() override;
@ -83,17 +83,13 @@ class PowerHintSession : public BnPowerHintSession {
private:
class StaleHandler : public MessageHandler {
public:
StaleHandler(PowerHintSession *session, int64_t timeout_ms)
: kStaleTimeout(timeout_ms),
mSession(session),
mIsMonitoringStale(false),
mLastUpdatedTime(steady_clock::now()) {}
StaleHandler(PowerHintSession *session)
: mSession(session), mIsMonitoringStale(false), mLastUpdatedTime(steady_clock::now()) {}
void handleMessage(const Message &message) override;
void updateStaleTimer();
time_point<steady_clock> getStaleTime();
private:
const milliseconds kStaleTimeout;
PowerHintSession *mSession;
std::atomic<bool> mIsMonitoringStale;
std::atomic<time_point<steady_clock>> mLastUpdatedTime;
@ -104,10 +100,12 @@ class PowerHintSession : public BnPowerHintSession {
void setStale();
void updateUniveralBoostMode();
int setUclamp(int32_t max, int32_t min);
std::string getIdString() const;
AppHintDesc *mDescriptor = nullptr;
sp<StaleHandler> mStaleHandler;
sp<MessageHandler> mPowerManagerHandler;
std::mutex mLock;
const nanoseconds kAdpfRate;
};
} // namespace pixel

View File

@ -37,7 +37,7 @@ void PowerSessionManager::setHintManager(std::shared_ptr<HintManager> const &hin
}
void PowerSessionManager::updateHintMode(const std::string &mode, bool enabled) {
ALOGD("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled);
ALOGV("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled);
if (enabled && mode.compare(0, 8, "REFRESH_") == 0) {
if (mode.compare("REFRESH_120FPS") == 0) {
mDisplayRefreshRate = 120;
@ -63,19 +63,31 @@ void PowerSessionManager::removePowerSession(PowerHintSession *session) {
mSessions.erase(session);
}
bool PowerSessionManager::isAnySessionActive() {
std::optional<bool> PowerSessionManager::isAnySessionActive() {
std::lock_guard<std::mutex> guard(mLock);
bool active = false;
for (PowerHintSession *s : mSessions) {
// session active and not stale is actually active.
if (s->isActive() && !s->isStale()) {
return true;
active = true;
break;
}
}
return false;
if (active == mActive) {
return std::nullopt;
} else {
mActive = active;
}
return active;
}
void PowerSessionManager::handleMessage(const Message &) {
if (isAnySessionActive()) {
auto active = isAnySessionActive();
if (!active.has_value()) {
return;
}
if (active.value()) {
disableSystemTopAppBoost();
} else {
enableSystemTopAppBoost();
@ -84,20 +96,14 @@ void PowerSessionManager::handleMessage(const Message &) {
void PowerSessionManager::enableSystemTopAppBoost() {
if (mHintManager) {
ALOGD("PowerSessionManager::enableSystemTopAppBoost!!");
if (ATRACE_ENABLED()) {
ATRACE_INT(kDisableBoostHintName.c_str(), 0);
}
ALOGV("PowerSessionManager::enableSystemTopAppBoost!!");
mHintManager->EndHint(kDisableBoostHintName);
}
}
void PowerSessionManager::disableSystemTopAppBoost() {
if (mHintManager) {
ALOGD("PowerSessionManager::disableSystemTopAppBoost!!");
if (ATRACE_ENABLED()) {
ATRACE_INT(kDisableBoostHintName.c_str(), 1);
}
ALOGV("PowerSessionManager::disableSystemTopAppBoost!!");
mHintManager->DoHint(kDisableBoostHintName);
}
}

View File

@ -23,6 +23,7 @@
#include <utils/Looper.h>
#include <mutex>
#include <optional>
#include <unordered_set>
namespace aidl {
@ -48,7 +49,7 @@ class PowerSessionManager : public MessageHandler {
// monitoring session status
void addPowerSession(PowerHintSession *session);
void removePowerSession(PowerHintSession *session);
bool isAnySessionActive();
void handleMessage(const Message &message) override;
void setHintManager(std::shared_ptr<HintManager> const &hint_manager);
@ -59,19 +60,22 @@ class PowerSessionManager : public MessageHandler {
}
private:
std::optional<bool> isAnySessionActive();
void disableSystemTopAppBoost();
void enableSystemTopAppBoost();
const std::string kDisableBoostHintName;
std::shared_ptr<HintManager> mHintManager;
std::unordered_set<PowerHintSession *> mSessions;
std::unordered_set<PowerHintSession *> mSessions; // protected by mLock
std::mutex mLock;
int mDisplayRefreshRate;
bool mActive; // protected by mLock
// Singleton
PowerSessionManager()
: kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost,
"ADPF_DISABLE_TA_BOOST")),
mHintManager(nullptr),
mDisplayRefreshRate(60) {}
mDisplayRefreshRate(60),
mActive(false) {}
PowerSessionManager(PowerSessionManager const &) = delete;
void operator=(PowerSessionManager const &) = delete;
};