diff --git a/power-libperfmgr/Android.mk b/power-libperfmgr/Android.mk index d979ccb..25d9a88 100644 --- a/power-libperfmgr/Android.mk +++ b/power-libperfmgr/Android.mk @@ -13,16 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # - ifneq (,$(findstring hardware/google/interfaces, $(PRODUCT_SOONG_NAMESPACES))) ifneq (,$(findstring hardware/google/pixel, $(PRODUCT_SOONG_NAMESPACES))) - LOCAL_PATH := $(call my-dir) - include $(CLEAR_VARS) - LOCAL_MODULE_RELATIVE_PATH := hw - LOCAL_SHARED_LIBRARIES := \ android.hardware.power-V2-ndk \ libbase \ @@ -34,7 +29,6 @@ LOCAL_SHARED_LIBRARIES := \ libprocessgroup \ libutils \ pixel-power-ext-V1-ndk - LOCAL_SRC_FILES := \ service.cpp \ InteractionHandler.cpp \ @@ -42,25 +36,19 @@ LOCAL_SRC_FILES := \ PowerExt.cpp \ PowerHintSession.cpp \ PowerSessionManager.cpp - LOCAL_CFLAGS := -Wno-unused-parameter -Wno-unused-variable - ifneq ($(TARGET_POWERHAL_MODE_EXT),) LOCAL_CFLAGS += -DMODE_EXT LOCAL_SRC_FILES += ../../../../$(TARGET_POWERHAL_MODE_EXT) endif - ifneq ($(TARGET_TAP_TO_WAKE_NODE),) LOCAL_CFLAGS += -DTAP_TO_WAKE_NODE=\"$(TARGET_TAP_TO_WAKE_NODE)\" endif - -LOCAL_MODULE := android.hardware.power-service.lenovo-sdm710-libperfmgr -LOCAL_INIT_RC := android.hardware.power-service.lenovo-sdm710-libperfmgr.rc +LOCAL_MODULE := android.hardware.power-service.xiaomi-libperfmgr +LOCAL_INIT_RC := android.hardware.power-service.xiaomi-libperfmgr.rc LOCAL_MODULE_TAGS := optional LOCAL_VENDOR_MODULE := true -LOCAL_VINTF_FRAGMENTS := android.hardware.power-service.lenovo-sdm710.xml - +LOCAL_VINTF_FRAGMENTS := android.hardware.power-service.xiaomi.xml include $(BUILD_EXECUTABLE) - endif endif diff --git a/power-libperfmgr/InteractionHandler.cpp b/power-libperfmgr/InteractionHandler.cpp index a9f908b..1db0c86 100644 --- a/power-libperfmgr/InteractionHandler.cpp +++ b/power-libperfmgr/InteractionHandler.cpp @@ -13,12 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL) #define LOG_TAG "powerhal-libperfmgr" - #include "InteractionHandler.h" - #include #include #include @@ -28,24 +25,18 @@ #include #include #include - #include #include - #define MAX_LENGTH 64 - #define MSINSEC 1000L #define NSINMS 1000000L - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - namespace { - static const bool kDisplayIdleSupport = ::android::base::GetBoolProperty("vendor.powerhal.disp.idle_support", true); static const std::array kDispIdlePath = {"/sys/class/drm/card0/device/idle_state", @@ -58,14 +49,12 @@ static const uint32_t kMaxDurationMs = ::android::base::GetUintProperty("vendor.powerhal.interaction.max", /*default*/ 5650U); static const uint32_t kDurationOffsetMs = ::android::base::GetUintProperty("vendor.powerhal.interaction.offset", /*default*/ 650U); - static size_t CalcTimespecDiffMs(struct timespec start, struct timespec end) { size_t diff_in_ms = 0; diff_in_ms += (end.tv_sec - start.tv_sec) * MSINSEC; diff_in_ms += (end.tv_nsec - start.tv_nsec) / NSINMS; return diff_in_ms; } - static int FbIdleOpen(void) { int fd; for (const auto &path : kDispIdlePath) { @@ -76,77 +65,58 @@ static int FbIdleOpen(void) { ALOGE("Unable to open fb idle state path (%d)", errno); return -1; } - } // namespace - using ::android::perfmgr::HintManager; - InteractionHandler::InteractionHandler() : mState(INTERACTION_STATE_UNINITIALIZED), mDurationMs(0) {} - InteractionHandler::~InteractionHandler() { Exit(); } - bool InteractionHandler::Init() { std::lock_guard lk(mLock); - if (mState != INTERACTION_STATE_UNINITIALIZED) return true; - int fd = FbIdleOpen(); if (fd < 0) return false; mIdleFd = fd; - mEventFd = eventfd(0, EFD_NONBLOCK); if (mEventFd < 0) { ALOGE("Unable to create event fd (%d)", errno); close(mIdleFd); return false; } - mState = INTERACTION_STATE_IDLE; mThread = std::unique_ptr(new std::thread(&InteractionHandler::Routine, this)); - return true; } - void InteractionHandler::Exit() { std::unique_lock lk(mLock); if (mState == INTERACTION_STATE_UNINITIALIZED) return; - AbortWaitLocked(); mState = INTERACTION_STATE_UNINITIALIZED; lk.unlock(); - mCond.notify_all(); mThread->join(); - close(mEventFd); close(mIdleFd); } - void InteractionHandler::PerfLock() { ALOGV("%s: acquiring perf lock", __func__); if (!HintManager::GetInstance()->DoHint("INTERACTION")) { ALOGE("%s: do hint INTERACTION failed", __func__); } } - void InteractionHandler::PerfRel() { ALOGV("%s: releasing perf lock", __func__); if (!HintManager::GetInstance()->EndHint("INTERACTION")) { ALOGE("%s: end hint INTERACTION failed", __func__); } } - void InteractionHandler::Acquire(int32_t duration) { ATRACE_CALL(); - std::lock_guard lk(mLock); - int inputDuration = duration + kDurationOffsetMs; int finalDuration; if (inputDuration > kMaxDurationMs) @@ -155,7 +125,6 @@ void InteractionHandler::Acquire(int32_t duration) { finalDuration = inputDuration; else finalDuration = kMinDurationMs; - // Fallback to do boost directly // 1) override property is set OR // 2) InteractionHandler not initialized @@ -163,7 +132,6 @@ void InteractionHandler::Acquire(int32_t duration) { HintManager::GetInstance()->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration)); return; } - struct timespec cur_timespec; clock_gettime(CLOCK_MONOTONIC, &cur_timespec); if (mState != INTERACTION_STATE_IDLE && finalDuration <= mDurationMs) { @@ -178,18 +146,14 @@ void InteractionHandler::Acquire(int32_t duration) { } mLastTimespec = cur_timespec; mDurationMs = finalDuration; - ALOGV("%s: input: %d final duration: %d", __func__, duration, finalDuration); - if (mState == INTERACTION_STATE_WAITING) AbortWaitLocked(); else if (mState == INTERACTION_STATE_IDLE) PerfLock(); - mState = INTERACTION_STATE_INTERACTION; mCond.notify_one(); } - void InteractionHandler::Release() { std::lock_guard lk(mLock); if (mState == INTERACTION_STATE_WAITING) { @@ -200,11 +164,9 @@ void InteractionHandler::Release() { // clear any wait aborts pending in event fd uint64_t val; ssize_t ret = read(mEventFd, &val, sizeof(val)); - ALOGW_IF(ret < 0, "%s: failed to clear eventfd (%zd, %d)", __func__, ret, errno); } } - // should be called while locked void InteractionHandler::AbortWaitLocked() { uint64_t val = 1; @@ -212,21 +174,16 @@ void InteractionHandler::AbortWaitLocked() { if (ret != sizeof(val)) ALOGW("Unable to write to event fd (%zd)", ret); } - void InteractionHandler::WaitForIdle(int32_t wait_ms, int32_t timeout_ms) { char data[MAX_LENGTH]; ssize_t ret; struct pollfd pfd[2]; - ATRACE_CALL(); - ALOGV("%s: wait:%d timeout:%d", __func__, wait_ms, timeout_ms); - pfd[0].fd = mEventFd; pfd[0].events = POLLIN; pfd[1].fd = mIdleFd; pfd[1].events = POLLPRI | POLLERR; - ret = poll(pfd, 1, wait_ms); if (ret > 0) { ALOGV("%s: wait aborted", __func__); @@ -235,18 +192,15 @@ void InteractionHandler::WaitForIdle(int32_t wait_ms, int32_t timeout_ms) { ALOGE("%s: error in poll while waiting", __func__); return; } - ret = pread(mIdleFd, data, sizeof(data), 0); if (!ret) { ALOGE("%s: Unexpected EOF!", __func__); return; } - if (!strncmp(data, "idle", 4)) { ALOGV("%s: already idle", __func__); return; } - ret = poll(pfd, 2, timeout_ms); if (ret < 0) ALOGE("%s: Error on waiting for idle (%zd)", __func__, ret); @@ -257,11 +211,9 @@ void InteractionHandler::WaitForIdle(int32_t wait_ms, int32_t timeout_ms) { else if (pfd[1].revents) ALOGV("%s: idle detected", __func__); } - void InteractionHandler::Routine() { pthread_setname_np(pthread_self(), "DispIdle"); std::unique_lock lk(mLock, std::defer_lock); - while (true) { lk.lock(); mCond.wait(lk, [&] { return mState != INTERACTION_STATE_IDLE; }); @@ -269,12 +221,10 @@ void InteractionHandler::Routine() { return; mState = INTERACTION_STATE_WAITING; lk.unlock(); - WaitForIdle(kWaitMs, mDurationMs); Release(); } } - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/InteractionHandler.h b/power-libperfmgr/InteractionHandler.h index c88f7d4..792f698 100644 --- a/power-libperfmgr/InteractionHandler.h +++ b/power-libperfmgr/InteractionHandler.h @@ -13,29 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once - #include #include #include #include #include - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - enum InteractionState { INTERACTION_STATE_UNINITIALIZED, INTERACTION_STATE_IDLE, INTERACTION_STATE_INTERACTION, INTERACTION_STATE_WAITING, }; - class InteractionHandler { public: InteractionHandler(); @@ -43,16 +38,13 @@ class InteractionHandler { bool Init(); void Exit(); void Acquire(int32_t duration); - private: void Release(); void WaitForIdle(int32_t wait_ms, int32_t timeout_ms); void AbortWaitLocked(); void Routine(); - void PerfLock(); void PerfRel(); - enum InteractionState mState; int mIdleFd; int mEventFd; @@ -62,7 +54,6 @@ class InteractionHandler { std::mutex mLock; std::condition_variable mCond; }; - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/Power.cpp b/power-libperfmgr/Power.cpp index 37b281d..4a2b1fd 100644 --- a/power-libperfmgr/Power.cpp +++ b/power-libperfmgr/Power.cpp @@ -13,11 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #define LOG_TAG "powerhal-libperfmgr" - #include "Power.h" - #include #include #include @@ -25,37 +22,29 @@ #include #include #include - #include - #include "PowerHintSession.h" #include "PowerSessionManager.h" - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using ::aidl::google::hardware::power::impl::pixel::PowerHintSession; using ::android::perfmgr::HintManager; - #ifdef MODE_EXT extern bool isDeviceSpecificModeSupported(Mode type, bool* _aidl_return); extern bool setDeviceSpecificMode(Mode type, bool enabled); #endif - constexpr char kPowerHalStateProp[] = "vendor.powerhal.state"; constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio"; constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering"; - Power::Power() : mInteractionHandler(nullptr), mSustainedPerfModeOn(false) { mInteractionHandler = std::make_unique(); mInteractionHandler->Init(); - std::string state = ::android::base::GetProperty(kPowerHalStateProp, ""); if (state == "SUSTAINED_PERFORMANCE") { LOG(INFO) << "Initialize with SUSTAINED_PERFORMANCE on"; @@ -64,20 +53,17 @@ Power::Power() } else { LOG(INFO) << "Initialize PowerHAL"; } - state = ::android::base::GetProperty(kPowerHalAudioProp, ""); if (state == "AUDIO_STREAMING_LOW_LATENCY") { LOG(INFO) << "Initialize with AUDIO_LOW_LATENCY on"; HintManager::GetInstance()->DoHint(state); } - state = ::android::base::GetProperty(kPowerHalRenderingProp, ""); if (state == "EXPENSIVE_RENDERING") { LOG(INFO) << "Initialize with EXPENSIVE_RENDERING on"; HintManager::GetInstance()->DoHint("EXPENSIVE_RENDERING"); } } - ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { LOG(DEBUG) << "Power setMode: " << toString(type) << " to: " << enabled; if (HintManager::GetInstance()->GetAdpfProfile() && @@ -130,17 +116,14 @@ ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { } break; } - return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus Power::isModeSupported(Mode type, bool *_aidl_return) { #ifdef MODE_EXT if (isDeviceSpecificModeSupported(type, _aidl_return)) { return ndk::ScopedAStatus::ok(); } #endif - bool supported = HintManager::GetInstance()->IsHintSupported(toString(type)); #ifdef TAP_TO_WAKE_NODE if (type == Mode::DOUBLE_TAP_TO_WAKE) { @@ -151,7 +134,6 @@ ndk::ScopedAStatus Power::isModeSupported(Mode type, bool *_aidl_return) { *_aidl_return = supported; return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) { LOG(DEBUG) << "Power setBoost: " << toString(type) << " duration: " << durationMs; if (HintManager::GetInstance()->GetAdpfProfile() && @@ -185,21 +167,17 @@ ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) { } break; } - return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus Power::isBoostSupported(Boost type, bool *_aidl_return) { bool supported = HintManager::GetInstance()->IsHintSupported(toString(type)); LOG(INFO) << "Power boost " << toString(type) << " isBoostSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); } - constexpr const char *boolToString(bool b) { return b ? "true" : "false"; } - binder_status_t Power::dump(int fd, const char **, uint32_t) { std::string buf(::android::base::StringPrintf( "HintManager Running: %s\n" @@ -215,7 +193,6 @@ binder_status_t Power::dump(int fd, const char **, uint32_t) { fsync(fd); return STATUS_OK; } - ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, int64_t durationNanos, @@ -235,7 +212,6 @@ ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid, *_aidl_return = session; return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t *outNanoseconds) { *outNanoseconds = HintManager::GetInstance()->GetAdpfProfile() ? HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs @@ -243,10 +219,8 @@ ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t *outNanoseconds) { if (*outNanoseconds <= 0) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } - return ndk::ScopedAStatus::ok(); } - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/Power.h b/power-libperfmgr/Power.h index c5bb2a8..e71b1e3 100644 --- a/power-libperfmgr/Power.h +++ b/power-libperfmgr/Power.h @@ -13,28 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once - #include - #include #include #include - #include "InteractionHandler.h" - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using ::aidl::android::hardware::power::Boost; using ::aidl::android::hardware::power::IPowerHintSession; using ::aidl::android::hardware::power::Mode; - class Power : public ::aidl::android::hardware::power::BnPower { public: Power(); @@ -48,12 +41,10 @@ class Power : public ::aidl::android::hardware::power::BnPower { std::shared_ptr *_aidl_return) override; ndk::ScopedAStatus getHintSessionPreferredRate(int64_t *outNanoseconds) override; binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; - private: std::unique_ptr mInteractionHandler; std::atomic mSustainedPerfModeOn; }; - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/PowerExt.cpp b/power-libperfmgr/PowerExt.cpp index 62195a4..1ed8ba3 100644 --- a/power-libperfmgr/PowerExt.cpp +++ b/power-libperfmgr/PowerExt.cpp @@ -13,11 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#define LOG_TAG "android.hardware.power-service.lenovo-sdm710-libperfmgr" - +#define LOG_TAG "android.hardware.power-service.xiaomi.ext-libperfmgr" #include "PowerExt.h" - #include #include #include @@ -25,23 +22,17 @@ #include #include #include - #include - #include "PowerSessionManager.h" - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using ::android::perfmgr::HintManager; - ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) { LOG(DEBUG) << "PowerExt setMode: " << mode << " to: " << enabled; - if (enabled) { HintManager::GetInstance()->DoHint(mode); } else { @@ -51,24 +42,20 @@ ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) { HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs > 0) { PowerSessionManager::getInstance()->updateHintMode(mode, enabled); } - return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerExt::isModeSupported(const std::string &mode, bool *_aidl_return) { bool supported = HintManager::GetInstance()->IsHintSupported(mode); LOG(INFO) << "PowerExt mode " << mode << " isModeSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerExt::setBoost(const std::string &boost, int32_t durationMs) { LOG(DEBUG) << "PowerExt setBoost: " << boost << " duration: " << durationMs; if (HintManager::GetInstance()->GetAdpfProfile() && HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs > 0) { PowerSessionManager::getInstance()->updateHintBoost(boost, durationMs); } - if (durationMs > 0) { HintManager::GetInstance()->DoHint(boost, std::chrono::milliseconds(durationMs)); } else if (durationMs == 0) { @@ -76,17 +63,14 @@ ndk::ScopedAStatus PowerExt::setBoost(const std::string &boost, int32_t duration } else { HintManager::GetInstance()->EndHint(boost); } - return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerExt::isBoostSupported(const std::string &boost, bool *_aidl_return) { bool supported = HintManager::GetInstance()->IsHintSupported(boost); LOG(INFO) << "PowerExt boost " << boost << " isBoostSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); } - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/PowerExt.h b/power-libperfmgr/PowerExt.h index bed12b5..4804cf4 100644 --- a/power-libperfmgr/PowerExt.h +++ b/power-libperfmgr/PowerExt.h @@ -13,23 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once - #include #include #include - #include #include - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - class PowerExt : public ::aidl::google::hardware::power::extension::pixel::BnPowerExt { public: PowerExt() {} @@ -37,10 +32,8 @@ class PowerExt : public ::aidl::google::hardware::power::extension::pixel::BnPow ndk::ScopedAStatus isModeSupported(const std::string &mode, bool *_aidl_return) override; ndk::ScopedAStatus setBoost(const std::string &boost, int32_t durationMs) override; ndk::ScopedAStatus isBoostSupported(const std::string &boost, bool *_aidl_return) override; - private: }; - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/PowerHintSession.cpp b/power-libperfmgr/PowerHintSession.cpp index c481977..3c888c0 100644 --- a/power-libperfmgr/PowerHintSession.cpp +++ b/power-libperfmgr/PowerHintSession.cpp @@ -13,12 +13,9 @@ * 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 "PowerHintSession.h" - #include #include #include @@ -28,32 +25,24 @@ #include #include #include - #include - #include "PowerSessionManager.h" - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using ::android::base::StringPrintf; using ::android::perfmgr::AdpfConfig; using ::android::perfmgr::HintManager; using std::chrono::duration_cast; using std::chrono::nanoseconds; - namespace { - static inline int64_t ns_to_100us(int64_t ns) { return ns / 100000; } - } // namespace - int64_t PowerHintSession::convertWorkDurationToBoostByPid( const std::vector &actualDurations) { std::shared_ptr adpfConfig = HintManager::GetInstance()->GetAdpfProfile(); @@ -101,7 +90,6 @@ int64_t PowerHintSession::convertWorkDurationToBoostByPid( int64_t dOut = static_cast((derivative_sum > 0 ? adpfConfig->mPidDo : adpfConfig->mPidDu) * derivative_sum / dt / (length - d_start)); - int64_t output = pOut + iOut + dOut; if (ATRACE_ENABLED()) { traceSessionVal("pid.err", err_sum / (length - p_start)); @@ -114,7 +102,6 @@ int64_t PowerHintSession::convertWorkDurationToBoostByPid( } return output; } - PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, int64_t durationNanos) { mDescriptor = new AppHintDesc(tgid, uid, threadIds); @@ -133,7 +120,6 @@ PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector< setSessionUclampMin(HintManager::GetInstance()->GetAdpfProfile()->mUclampMinInit); ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str()); } - PowerHintSession::~PowerHintSession() { close(); ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str()); @@ -144,16 +130,13 @@ PowerHintSession::~PowerHintSession() { } delete mDescriptor; } - void PowerHintSession::traceSessionVal(char const *identifier, int64_t val) const { ATRACE_INT(StringPrintf("adpf.%s-%s", mIdString.c_str(), identifier).c_str(), val); } - bool PowerHintSession::isAppSession() { // Check if uid is in range reserved for applications return mDescriptor->uid >= AID_APP_START; } - void PowerHintSession::updateUniveralBoostMode() { if (!isAppSession()) { return; @@ -167,7 +150,6 @@ void PowerHintSession::updateUniveralBoostMode() { ATRACE_END(); } } - void PowerHintSession::tryToSendPowerHint(std::string hint) { if (!mSupportedHints[hint].has_value()) { mSupportedHints[hint] = HintManager::GetInstance()->IsHintSupported(hint); @@ -176,7 +158,6 @@ void PowerHintSession::tryToSendPowerHint(std::string hint) { HintManager::GetInstance()->DoHint(hint); } } - int PowerHintSession::setSessionUclampMin(int32_t min) { { std::lock_guard guard(mSessionLock); @@ -186,24 +167,20 @@ int PowerHintSession::setSessionUclampMin(int32_t min) { mStaleTimerHandler->updateTimer(); } PowerSessionManager::getInstance()->setUclampMin(this, min); - if (ATRACE_ENABLED()) { traceSessionVal("min", min); } return 0; } - int PowerHintSession::getUclampMin() { return mDescriptor->current_min; } - void PowerHintSession::dumpToStream(std::ostream &stream) { stream << "ID.Min.Act.Timeout(" << mIdString; stream << ", " << mDescriptor->current_min; stream << ", " << mDescriptor->is_active; stream << ", " << isTimeout() << ")"; } - ndk::ScopedAStatus PowerHintSession::pause() { if (mSessionClosed) { ALOGE("Error: session is dead"); @@ -220,7 +197,6 @@ ndk::ScopedAStatus PowerHintSession::pause() { updateUniveralBoostMode(); return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerHintSession::resume() { if (mSessionClosed) { ALOGE("Error: session is dead"); @@ -237,7 +213,6 @@ ndk::ScopedAStatus PowerHintSession::resume() { updateUniveralBoostMode(); return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerHintSession::close() { bool sessionClosedExpectedToBe = false; if (!mSessionClosed.compare_exchange_strong(sessionClosedExpectedToBe, true)) { @@ -251,7 +226,6 @@ ndk::ScopedAStatus PowerHintSession::close() { updateUniveralBoostMode(); return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) { if (mSessionClosed) { ALOGE("Error: session is dead"); @@ -264,15 +238,12 @@ ndk::ScopedAStatus PowerHintSession::updateTargetWorkDuration(int64_t targetDura targetDurationNanos = targetDurationNanos * HintManager::GetInstance()->GetAdpfProfile()->mTargetTimeFactor; ALOGV("update target duration: %" PRId64 " ns", targetDurationNanos); - mDescriptor->duration = std::chrono::nanoseconds(targetDurationNanos); if (ATRACE_ENABLED()) { traceSessionVal("target", mDescriptor->duration.count()); } - return ndk::ScopedAStatus::ok(); } - ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration( const std::vector &actualDurations) { if (mSessionClosed) { @@ -303,7 +274,6 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration( actualDurations.back().durationNanos - mDescriptor->duration.count() > 0); traceSessionVal("session_hint", -1); } - mLastUpdatedTime.store(std::chrono::steady_clock::now()); if (isFirstFrame) { if (isAppSession()) { @@ -311,22 +281,18 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration( } updateUniveralBoostMode(); } - if (!adpfConfig->mPidOn) { setSessionUclampMin(adpfConfig->mUclampMinHigh); return ndk::ScopedAStatus::ok(); } int64_t output = convertWorkDurationToBoostByPid(actualDurations); - /* apply to all the threads in the group */ int next_min = std::min(static_cast(adpfConfig->mUclampMinHigh), mDescriptor->current_min + static_cast(output)); next_min = std::max(static_cast(adpfConfig->mUclampMinLow), next_min); setSessionUclampMin(next_min); - return ndk::ScopedAStatus::ok(); } - std::string AppHintDesc::toString() const { std::string out = StringPrintf("session %" PRIxPTR "\n", reinterpret_cast(this) & 0xffff); @@ -334,7 +300,6 @@ std::string AppHintDesc::toString() const { 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) { @@ -347,11 +312,9 @@ std::string AppHintDesc::toString() const { out.append("]\n"); return out; } - bool PowerHintSession::isActive() { return mDescriptor->is_active.load(); } - bool PowerHintSession::isTimeout() { auto now = std::chrono::steady_clock::now(); time_point staleTime = @@ -361,11 +324,9 @@ bool PowerHintSession::isTimeout() { HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor)); return now >= staleTime; } - const std::vector &PowerHintSession::getTidList() const { return mDescriptor->threadIds; } - void PowerHintSession::setStale() { // Reset to default uclamp value. PowerSessionManager::getInstance()->setUclampMin(this, 0); @@ -375,7 +336,6 @@ void PowerHintSession::setStale() { traceSessionVal("min", 0); } } - void PowerHintSession::StaleTimerHandler::updateTimer() { auto now = std::chrono::steady_clock::now(); nanoseconds staleDuration = std::chrono::nanoseconds( @@ -393,7 +353,6 @@ void PowerHintSession::StaleTimerHandler::updateTimer() { mSession->traceSessionVal("timer.stale", 0); } } - void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) { std::lock_guard guard(mClosedLock); if (mIsSessionDead) { @@ -418,13 +377,11 @@ void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) { mSession->traceSessionVal("timer.stale", next > 0 ? 0 : 1); } } - void PowerHintSession::StaleTimerHandler::setSessionDead() { std::lock_guard guard(mClosedLock); mIsSessionDead = true; PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler); } - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/PowerHintSession.h b/power-libperfmgr/PowerHintSession.h index 2a187ef..8d8856a 100644 --- a/power-libperfmgr/PowerHintSession.h +++ b/power-libperfmgr/PowerHintSession.h @@ -13,24 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once - #include #include #include #include - #include #include - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using aidl::android::hardware::power::BnPowerHintSession; using aidl::android::hardware::power::WorkDuration; using ::android::Message; @@ -40,7 +35,6 @@ using std::chrono::milliseconds; using std::chrono::nanoseconds; using std::chrono::steady_clock; using std::chrono::time_point; - struct AppHintDesc { AppHintDesc(int32_t tgid, int32_t uid, std::vector threadIds) : tgid(tgid), @@ -65,7 +59,6 @@ struct AppHintDesc { int64_t integral_error; int64_t previous_error; }; - class PowerHintSession : public BnPowerHintSession { public: explicit PowerHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, @@ -85,9 +78,7 @@ class PowerHintSession : public BnPowerHintSession { const std::vector &getTidList() const; int getUclampMin(); void dumpToStream(std::ostream &stream); - time_point getStaleTime(); - private: class StaleTimerHandler : public MessageHandler { public: @@ -95,7 +86,6 @@ class PowerHintSession : public BnPowerHintSession { void updateTimer(); void handleMessage(const Message &message) override; void setSessionDead(); - private: PowerHintSession *mSession; std::mutex mClosedLock; @@ -103,7 +93,6 @@ class PowerHintSession : public BnPowerHintSession { std::atomic> mStaleTime; bool mIsSessionDead; }; - private: void updateUniveralBoostMode(); int setSessionUclampMin(int32_t min); @@ -120,7 +109,6 @@ class PowerHintSession : public BnPowerHintSession { // To cache the status of whether ADPF hints are supported. std::unordered_map> mSupportedHints; }; - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/PowerSessionManager.cpp b/power-libperfmgr/PowerSessionManager.cpp index 35bf300..2bb05a9 100644 --- a/power-libperfmgr/PowerSessionManager.cpp +++ b/power-libperfmgr/PowerSessionManager.cpp @@ -13,29 +13,23 @@ * 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 "PowerSessionManager.h" - #include #include #include #include #include #include - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using ::android::perfmgr::AdpfConfig; using ::android::perfmgr::HintManager; - namespace { /* there is no glibc or bionic wrapper */ struct sched_attr { @@ -50,7 +44,6 @@ struct sched_attr { __u32 sched_util_min; __u32 sched_util_max; }; - static int sched_setattr(int pid, struct sched_attr *attr, unsigned int flags) { if (!HintManager::GetInstance()->GetAdpfProfile()->mUclampMinOn) { ALOGV("PowerSessionManager:%s: skip", __func__); @@ -58,25 +51,20 @@ static int sched_setattr(int pid, struct sched_attr *attr, unsigned int flags) { } return syscall(__NR_sched_setattr, pid, attr, flags); } - static void set_uclamp_min(int tid, int min) { static constexpr int32_t kMaxUclampValue = 1024; min = std::max(0, min); min = std::min(min, kMaxUclampValue); - sched_attr attr = {}; attr.size = sizeof(attr); - attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP_MIN); attr.sched_util_min = min; - int ret = sched_setattr(tid, &attr, 0); if (ret) { ALOGW("sched_setattr failed for thread %d, err=%d", tid, errno); } } } // namespace - void PowerSessionManager::updateHintMode(const std::string &mode, bool enabled) { ALOGV("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled); if (enabled && mode.compare(0, 8, "REFRESH_") == 0) { @@ -92,17 +80,14 @@ void PowerSessionManager::updateHintMode(const std::string &mode, bool enabled) HintManager::GetInstance()->SetAdpfProfile(mode); } } - void PowerSessionManager::updateHintBoost(const std::string &boost, int32_t durationMs) { ATRACE_CALL(); ALOGV("PowerSessionManager::updateHintBoost: boost: %s, durationMs: %d", boost.c_str(), durationMs); } - int PowerSessionManager::getDisplayRefreshRate() { return mDisplayRefreshRate; } - void PowerSessionManager::addPowerSession(PowerHintSession *session) { std::lock_guard guard(mLock); for (auto t : session->getTidList()) { @@ -123,7 +108,6 @@ void PowerSessionManager::addPowerSession(PowerHintSession *session) { } mSessions.insert(session); } - void PowerSessionManager::removePowerSession(PowerHintSession *session) { std::lock_guard guard(mLock); for (auto t : session->getTidList()) { @@ -142,12 +126,10 @@ void PowerSessionManager::removePowerSession(PowerHintSession *session) { } mSessions.erase(session); } - void PowerSessionManager::setUclampMin(PowerHintSession *session, int val) { std::lock_guard guard(mLock); setUclampMinLocked(session, val); } - void PowerSessionManager::setUclampMinLocked(PowerHintSession *session, int val) { for (auto t : session->getTidList()) { // Get thex max uclamp.min across sessions which include the tid. @@ -160,7 +142,6 @@ void PowerSessionManager::setUclampMinLocked(PowerHintSession *session, int val) set_uclamp_min(t, std::max(val, tidMax)); } } - std::optional PowerSessionManager::isAnyAppSessionActive() { std::lock_guard guard(mLock); bool active = false; @@ -176,10 +157,8 @@ std::optional PowerSessionManager::isAnyAppSessionActive() { } else { mActive = active; } - return active; } - void PowerSessionManager::handleMessage(const Message &) { auto active = isAnyAppSessionActive(); if (!active.has_value()) { @@ -191,7 +170,6 @@ void PowerSessionManager::handleMessage(const Message &) { enableSystemTopAppBoost(); } } - void PowerSessionManager::dumpToFd(int fd) { std::ostringstream dump_buf; std::lock_guard guard(mLock); @@ -213,39 +191,33 @@ void PowerSessionManager::dumpToFd(int fd) { ALOGE("Failed to dump one of session list to fd:%d", fd); } } - void PowerSessionManager::enableSystemTopAppBoost() { if (HintManager::GetInstance()->IsHintSupported(kDisableBoostHintName)) { ALOGV("PowerSessionManager::enableSystemTopAppBoost!!"); HintManager::GetInstance()->EndHint(kDisableBoostHintName); } } - void PowerSessionManager::disableSystemTopAppBoost() { if (HintManager::GetInstance()->IsHintSupported(kDisableBoostHintName)) { ALOGV("PowerSessionManager::disableSystemTopAppBoost!!"); HintManager::GetInstance()->DoHint(kDisableBoostHintName); } } - // =========== PowerHintMonitor implementation start from here =========== void PowerHintMonitor::start() { if (!isRunning()) { run("PowerHintMonitor", ::android::PRIORITY_HIGHEST); } } - bool PowerHintMonitor::threadLoop() { while (true) { mLooper->pollOnce(-1); } return true; } - sp PowerHintMonitor::getLooper() { return mLooper; } - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/PowerSessionManager.h b/power-libperfmgr/PowerSessionManager.h index e65e58e..c22bb83 100644 --- a/power-libperfmgr/PowerSessionManager.h +++ b/power-libperfmgr/PowerSessionManager.h @@ -13,34 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #pragma once - #include #include #include - #include #include #include - #include "PowerHintSession.h" - namespace aidl { namespace google { namespace hardware { namespace power { namespace impl { namespace pixel { - using ::android::Looper; using ::android::Message; using ::android::MessageHandler; using ::android::Thread; using ::android::perfmgr::HintManager; - constexpr char kPowerHalAdpfDisableTopAppBoost[] = "vendor.powerhal.adpf.disable.hint"; - class PowerSessionManager : public MessageHandler { public: // current hint info @@ -54,19 +46,16 @@ class PowerSessionManager : public MessageHandler { void setUclampMinLocked(PowerHintSession *session, int min); void handleMessage(const Message &message) override; void dumpToFd(int fd); - // Singleton static sp getInstance() { static sp instance = new PowerSessionManager(); return instance; } - private: std::optional isAnyAppSessionActive(); void disableSystemTopAppBoost(); void enableSystemTopAppBoost(); const std::string kDisableBoostHintName; - std::unordered_set mSessions; // protected by mLock std::unordered_map mTidRefCountMap; // protected by mLock std::unordered_map> mTidSessionListMap; @@ -85,7 +74,6 @@ class PowerSessionManager : public MessageHandler { PowerSessionManager(PowerSessionManager const &) = delete; void operator=(PowerSessionManager const &) = delete; }; - class PowerHintMonitor : public Thread { public: void start(); @@ -98,13 +86,11 @@ class PowerHintMonitor : public Thread { } PowerHintMonitor(PowerHintMonitor const &) = delete; void operator=(PowerHintMonitor const &) = delete; - private: sp mLooper; // Singleton PowerHintMonitor() : Thread(false), mLooper(new Looper(true)) {} }; - } // namespace pixel } // namespace impl } // namespace power diff --git a/power-libperfmgr/android.hardware.power-service.lenovo-sdm710-libperfmgr.rc b/power-libperfmgr/android.hardware.power-service.xiaomi-libperfmgr.rc similarity index 96% rename from power-libperfmgr/android.hardware.power-service.lenovo-sdm710-libperfmgr.rc rename to power-libperfmgr/android.hardware.power-service.xiaomi-libperfmgr.rc index 8c91b6e..e831c65 100644 --- a/power-libperfmgr/android.hardware.power-service.lenovo-sdm710-libperfmgr.rc +++ b/power-libperfmgr/android.hardware.power-service.xiaomi-libperfmgr.rc @@ -1,32 +1,25 @@ -service vendor.power-hal-aidl /vendor/bin/hw/android.hardware.power-service.lenovo-sdm710-libperfmgr +service vendor.power-hal-aidl /vendor/bin/hw/android.hardware.power-service.xiaomi-libperfmgr class hal user root group system priority -20 - on late-fs start vendor.power-hal-aidl - # Restart powerHAL when framework died on property:init.svc.zygote=restarting && property:vendor.powerhal.state=* setprop vendor.powerhal.state "" setprop vendor.powerhal.audio "" setprop vendor.powerhal.rendering "" restart vendor.power-hal-aidl - # Clean up after b/163539793 resolved on property:vendor.powerhal.dalvik.vm.dex2oat-threads=* setprop dalvik.vm.dex2oat-threads ${vendor.powerhal.dalvik.vm.dex2oat-threads} setprop dalvik.vm.restore-dex2oat-threads ${vendor.powerhal.dalvik.vm.dex2oat-threads} - on property:vendor.powerhal.dalvik.vm.dex2oat-cpu-set=* setprop dalvik.vm.dex2oat-cpu-set ${vendor.powerhal.dalvik.vm.dex2oat-cpu-set} setprop dalvik.vm.restore-dex2oat-cpu-set ${vendor.powerhal.dalvik.vm.dex2oat-cpu-set} - # Restart powerHAL when debug property set on property:ro.debuggable=1 && property:vendor.powerhal.config.debug=* restart vendor.power-hal-aidl - on property:persist.vendor.powerhal.config.debug=* setprop vendor.powerhal.config.debug ${persist.vendor.powerhal.config.debug} - diff --git a/power-libperfmgr/android.hardware.power-service.lenovo-sdm710.xml b/power-libperfmgr/android.hardware.power-service.xiaomi.xml similarity index 100% rename from power-libperfmgr/android.hardware.power-service.lenovo-sdm710.xml rename to power-libperfmgr/android.hardware.power-service.xiaomi.xml diff --git a/power-libperfmgr/service.cpp b/power-libperfmgr/service.cpp index 19ded1c..775fe3b 100644 --- a/power-libperfmgr/service.cpp +++ b/power-libperfmgr/service.cpp @@ -13,71 +13,55 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #define LOG_TAG "powerhal-libperfmgr" - #include #include #include #include #include #include - #include - #include "Power.h" #include "PowerExt.h" #include "PowerSessionManager.h" - using aidl::google::hardware::power::impl::pixel::Power; using aidl::google::hardware::power::impl::pixel::PowerExt; using aidl::google::hardware::power::impl::pixel::PowerHintMonitor; using aidl::google::hardware::power::impl::pixel::PowerSessionManager; using ::android::perfmgr::HintManager; - constexpr std::string_view kPowerHalInitProp("vendor.powerhal.init"); - int main() { // Parse config but do not start the looper - HintManager *hm = HintManager::GetInstance(); + std::shared_ptr hm = std::shared_ptr(HintManager::GetInstance()); if (!hm) { LOG(FATAL) << "HintManager Init failed"; } - // single thread ABinderProcess_setThreadPoolMaxThreadCount(0); - // core service std::shared_ptr pw = ndk::SharedRefBase::make(); ndk::SpAIBinder pwBinder = pw->asBinder(); AIBinder_setMinSchedulerPolicy(pwBinder.get(), SCHED_NORMAL, -20); - // extension service std::shared_ptr pwExt = ndk::SharedRefBase::make(); auto pwExtBinder = pwExt->asBinder(); AIBinder_setMinSchedulerPolicy(pwExtBinder.get(), SCHED_NORMAL, -20); - // attach the extension to the same binder we will be registering CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get())); - const std::string instance = std::string() + Power::descriptor + "/default"; binder_status_t status = AServiceManager_addService(pw->asBinder().get(), instance.c_str()); CHECK(status == STATUS_OK); - LOG(INFO) << "Lenovo Power HAL AIDL Service with Extension is started."; - + LOG(INFO) << "Xiaomi Power HAL AIDL Service with Extension is started."; if (HintManager::GetInstance()->GetAdpfProfile()) { PowerHintMonitor::getInstance()->start(); } - std::thread initThread([&]() { ::android::base::WaitForProperty(kPowerHalInitProp.data(), "1"); HintManager::GetInstance()->Start(); }); initThread.detach(); - ABinderProcess_joinThreadPool(); - // should not reach - LOG(ERROR) << "Lenovo Power HAL AIDL Service with Extension just died."; + LOG(ERROR) << "Xiaomi Power HAL AIDL Service with Extension just died."; return EXIT_FAILURE; } diff --git a/sdm710.mk b/sdm710.mk index 46972fe..4cf4162 100644 --- a/sdm710.mk +++ b/sdm710.mk @@ -371,7 +371,7 @@ PRODUCT_PACKAGES += \ android.hardware.power@1.2 \ android.hardware.power@1.2.vendor \ android.hardware.power.stats@1.0-service.mock \ - android.hardware.power-service.lenovo-sdm710-libperfmgr + android.hardware.power-service.xiaomi-libperfmgr PRODUCT_COPY_FILES += \ $(LOCAL_PATH)/configs/powerhint.json:$(TARGET_COPY_OUT_VENDOR)/etc/powerhint.json