power: Make HintManager a singleton class

HintManager is unique and widely used in many different components.
This is for making it easiler to be reloaded for debugging.

Bug: 172285365
Test: adb push \
      out/target/product/raven/data/nativetest64/libperfmgr_test/libperfmgr_test \
      /data/nativetest64/libperfmgr_test/libperfmgr_test && \
      adb shell /data/nativetest64/libperfmgr_test/libperfmgr_test
Change-Id: I3affdfe780073ebbc50fac7bfbdd1530ee9dc8c2
This commit is contained in:
Jimmy Shiu 2022-02-12 01:05:23 +08:00 committed by Bruno Martins
parent 34cf7df6f0
commit c998138db6
9 changed files with 68 additions and 96 deletions

View File

@ -17,20 +17,20 @@
#define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
#define LOG_TAG "powerhal-libperfmgr"
#include <array>
#include <memory>
#include "InteractionHandler.h"
#include <android-base/properties.h>
#include <fcntl.h>
#include <perfmgr/HintManager.h>
#include <poll.h>
#include <sys/eventfd.h>
#include <time.h>
#include <unistd.h>
#include <android-base/properties.h>
#include <utils/Log.h>
#include <utils/Trace.h>
#include "InteractionHandler.h"
#include <array>
#include <memory>
#define MAX_LENGTH 64
@ -79,10 +79,10 @@ static int FbIdleOpen(void) {
} // namespace
InteractionHandler::InteractionHandler(std::shared_ptr<HintManager> const &hint_manager)
: mState(INTERACTION_STATE_UNINITIALIZED),
mDurationMs(0),
mHintManager(hint_manager) {}
using ::android::perfmgr::HintManager;
InteractionHandler::InteractionHandler()
: mState(INTERACTION_STATE_UNINITIALIZED), mDurationMs(0) {}
InteractionHandler::~InteractionHandler() {
Exit();
@ -130,14 +130,14 @@ void InteractionHandler::Exit() {
void InteractionHandler::PerfLock() {
ALOGV("%s: acquiring perf lock", __func__);
if (!mHintManager->DoHint("INTERACTION")) {
if (!HintManager::GetInstance()->DoHint("INTERACTION")) {
ALOGE("%s: do hint INTERACTION failed", __func__);
}
}
void InteractionHandler::PerfRel() {
ALOGV("%s: releasing perf lock", __func__);
if (!mHintManager->EndHint("INTERACTION")) {
if (!HintManager::GetInstance()->EndHint("INTERACTION")) {
ALOGE("%s: end hint INTERACTION failed", __func__);
}
}
@ -160,7 +160,7 @@ void InteractionHandler::Acquire(int32_t duration) {
// 1) override property is set OR
// 2) InteractionHandler not initialized
if (!kDisplayIdleSupport || mState == INTERACTION_STATE_UNINITIALIZED) {
mHintManager->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration));
HintManager::GetInstance()->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration));
return;
}

View File

@ -22,8 +22,6 @@
#include <string>
#include <thread>
#include <perfmgr/HintManager.h>
namespace aidl {
namespace google {
namespace hardware {
@ -31,8 +29,6 @@ namespace power {
namespace impl {
namespace pixel {
using ::android::perfmgr::HintManager;
enum InteractionState {
INTERACTION_STATE_UNINITIALIZED,
INTERACTION_STATE_IDLE,
@ -42,7 +38,7 @@ enum InteractionState {
class InteractionHandler {
public:
InteractionHandler(std::shared_ptr<HintManager> const &hint_manager);
InteractionHandler();
~InteractionHandler();
bool Init();
void Exit();
@ -65,7 +61,6 @@ class InteractionHandler {
std::unique_ptr<std::thread> mThread;
std::mutex mLock;
std::condition_variable mCond;
std::shared_ptr<HintManager> mHintManager;
};
} // namespace pixel

View File

@ -18,16 +18,16 @@
#include "Power.h"
#include <mutex>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <perfmgr/HintManager.h>
#include <utils/Log.h>
#include <mutex>
#include "PowerHintSession.h"
#include "PowerSessionManager.h"
@ -39,6 +39,7 @@ 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);
@ -51,19 +52,18 @@ constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering";
constexpr char kPowerHalAdpfRateProp[] = "vendor.powerhal.adpf.rate";
constexpr int64_t kPowerHalAdpfRateDefault = -1;
Power::Power(std::shared_ptr<HintManager> hm)
: mHintManager(hm),
mInteractionHandler(nullptr),
Power::Power()
: mInteractionHandler(nullptr),
mSustainedPerfModeOn(false),
mAdpfRateNs(
::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) {
mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
mInteractionHandler = std::make_unique<InteractionHandler>();
mInteractionHandler->Init();
std::string state = ::android::base::GetProperty(kPowerHalStateProp, "");
if (state == "SUSTAINED_PERFORMANCE") {
LOG(INFO) << "Initialize with SUSTAINED_PERFORMANCE on";
mHintManager->DoHint("SUSTAINED_PERFORMANCE");
HintManager::GetInstance()->DoHint("SUSTAINED_PERFORMANCE");
mSustainedPerfModeOn = true;
} else {
LOG(INFO) << "Initialize PowerHAL";
@ -72,13 +72,13 @@ Power::Power(std::shared_ptr<HintManager> hm)
state = ::android::base::GetProperty(kPowerHalAudioProp, "");
if (state == "AUDIO_STREAMING_LOW_LATENCY") {
LOG(INFO) << "Initialize with AUDIO_LOW_LATENCY on";
mHintManager->DoHint(state);
HintManager::GetInstance()->DoHint(state);
}
state = ::android::base::GetProperty(kPowerHalRenderingProp, "");
if (state == "EXPENSIVE_RENDERING") {
LOG(INFO) << "Initialize with EXPENSIVE_RENDERING on";
mHintManager->DoHint("EXPENSIVE_RENDERING");
HintManager::GetInstance()->DoHint("EXPENSIVE_RENDERING");
}
// Now start to take powerhint
@ -101,7 +101,7 @@ ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
#endif
case Mode::SUSTAINED_PERFORMANCE:
if (enabled) {
mHintManager->DoHint("SUSTAINED_PERFORMANCE");
HintManager::GetInstance()->DoHint("SUSTAINED_PERFORMANCE");
}
mSustainedPerfModeOn = true;
break;
@ -128,9 +128,9 @@ ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
[[fallthrough]];
default:
if (enabled) {
mHintManager->DoHint(toString(type));
HintManager::GetInstance()->DoHint(toString(type));
} else {
mHintManager->EndHint(toString(type));
HintManager::GetInstance()->EndHint(toString(type));
}
break;
}
@ -145,7 +145,7 @@ ndk::ScopedAStatus Power::isModeSupported(Mode type, bool *_aidl_return) {
}
#endif
bool supported = mHintManager->IsHintSupported(toString(type));
bool supported = HintManager::GetInstance()->IsHintSupported(toString(type));
#ifdef TAP_TO_WAKE_NODE
if (type == Mode::DOUBLE_TAP_TO_WAKE) {
supported = true;
@ -176,11 +176,12 @@ ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
break;
}
if (durationMs > 0) {
mHintManager->DoHint(toString(type), std::chrono::milliseconds(durationMs));
HintManager::GetInstance()->DoHint(toString(type),
std::chrono::milliseconds(durationMs));
} else if (durationMs == 0) {
mHintManager->DoHint(toString(type));
HintManager::GetInstance()->DoHint(toString(type));
} else {
mHintManager->EndHint(toString(type));
HintManager::GetInstance()->EndHint(toString(type));
}
break;
}
@ -189,7 +190,7 @@ ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
}
ndk::ScopedAStatus Power::isBoostSupported(Boost type, bool *_aidl_return) {
bool supported = mHintManager->IsHintSupported(toString(type));
bool supported = HintManager::GetInstance()->IsHintSupported(toString(type));
LOG(INFO) << "Power boost " << toString(type) << " isBoostSupported: " << supported;
*_aidl_return = supported;
return ndk::ScopedAStatus::ok();
@ -203,10 +204,10 @@ binder_status_t Power::dump(int fd, const char **, uint32_t) {
std::string buf(::android::base::StringPrintf(
"HintManager Running: %s\n"
"SustainedPerformanceMode: %s\n",
boolToString(mHintManager->IsRunning()),
boolToString(HintManager::GetInstance()->IsRunning()),
boolToString(mSustainedPerfModeOn)));
// Dump nodes through libperfmgr
mHintManager->DumpToFd(fd);
HintManager::GetInstance()->DumpToFd(fd);
if (!::android::base::WriteStringToFd(buf, fd)) {
PLOG(ERROR) << "Failed to dump state to fd";
}

View File

@ -16,13 +16,12 @@
#pragma once
#include <aidl/android/hardware/power/BnPower.h>
#include <atomic>
#include <memory>
#include <thread>
#include <aidl/android/hardware/power/BnPower.h>
#include <perfmgr/HintManager.h>
#include "InteractionHandler.h"
namespace aidl {
@ -35,11 +34,10 @@ namespace pixel {
using ::aidl::android::hardware::power::Boost;
using ::aidl::android::hardware::power::IPowerHintSession;
using ::aidl::android::hardware::power::Mode;
using ::android::perfmgr::HintManager;
class Power : public ::aidl::android::hardware::power::BnPower {
public:
Power(std::shared_ptr<HintManager> hm);
Power();
ndk::ScopedAStatus setMode(Mode type, bool enabled) override;
ndk::ScopedAStatus isModeSupported(Mode type, bool *_aidl_return) override;
ndk::ScopedAStatus setBoost(Boost type, int32_t durationMs) override;
@ -52,7 +50,6 @@ class Power : public ::aidl::android::hardware::power::BnPower {
binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
private:
std::shared_ptr<HintManager> mHintManager;
std::unique_ptr<InteractionHandler> mInteractionHandler;
std::atomic<bool> mSustainedPerfModeOn;
const int64_t mAdpfRateNs;

View File

@ -17,18 +17,19 @@
#define LOG_TAG "android.hardware.power-service.xiaomi.ext-libperfmgr"
#include "PowerExt.h"
#include "PowerSessionManager.h"
#include <mutex>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <perfmgr/HintManager.h>
#include <utils/Log.h>
#include <mutex>
#include "PowerSessionManager.h"
namespace aidl {
namespace google {
namespace hardware {
@ -36,13 +37,15 @@ 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) {
mHintManager->DoHint(mode);
HintManager::GetInstance()->DoHint(mode);
} else {
mHintManager->EndHint(mode);
HintManager::GetInstance()->EndHint(mode);
}
PowerSessionManager::getInstance()->updateHintMode(mode, enabled);
@ -50,7 +53,7 @@ ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) {
}
ndk::ScopedAStatus PowerExt::isModeSupported(const std::string &mode, bool *_aidl_return) {
bool supported = mHintManager->IsHintSupported(mode);
bool supported = HintManager::GetInstance()->IsHintSupported(mode);
LOG(INFO) << "PowerExt mode " << mode << " isModeSupported: " << supported;
*_aidl_return = supported;
return ndk::ScopedAStatus::ok();
@ -60,18 +63,18 @@ ndk::ScopedAStatus PowerExt::setBoost(const std::string &boost, int32_t duration
LOG(DEBUG) << "PowerExt setBoost: " << boost << " duration: " << durationMs;
if (durationMs > 0) {
mHintManager->DoHint(boost, std::chrono::milliseconds(durationMs));
HintManager::GetInstance()->DoHint(boost, std::chrono::milliseconds(durationMs));
} else if (durationMs == 0) {
mHintManager->DoHint(boost);
HintManager::GetInstance()->DoHint(boost);
} else {
mHintManager->EndHint(boost);
HintManager::GetInstance()->EndHint(boost);
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerExt::isBoostSupported(const std::string &boost, bool *_aidl_return) {
bool supported = mHintManager->IsHintSupported(boost);
bool supported = HintManager::GetInstance()->IsHintSupported(boost);
LOG(INFO) << "PowerExt boost " << boost << " isBoostSupported: " << supported;
*_aidl_return = supported;
return ndk::ScopedAStatus::ok();

View File

@ -30,18 +30,15 @@ namespace power {
namespace impl {
namespace pixel {
using ::android::perfmgr::HintManager;
class PowerExt : public ::aidl::google::hardware::power::extension::pixel::BnPowerExt {
public:
PowerExt(std::shared_ptr<HintManager> hm) : mHintManager(hm) {}
PowerExt() {}
ndk::ScopedAStatus setMode(const std::string &mode, bool enabled) override;
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:
std::shared_ptr<HintManager> mHintManager;
};
} // namespace pixel

View File

@ -17,12 +17,13 @@
#define LOG_TAG "powerhal-libperfmgr"
#define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
#include "PowerSessionManager.h"
#include <log/log.h>
#include <perfmgr/HintManager.h>
#include <processgroup/processgroup.h>
#include <utils/Trace.h>
#include "PowerSessionManager.h"
namespace aidl {
namespace google {
namespace hardware {
@ -30,12 +31,7 @@ namespace power {
namespace impl {
namespace pixel {
void PowerSessionManager::setHintManager(std::shared_ptr<HintManager> const &hint_manager) {
// Only initialize hintmanager instance if hint is supported.
if (hint_manager->IsHintSupported(kDisableBoostHintName)) {
mHintManager = hint_manager;
}
}
using ::android::perfmgr::HintManager;
void PowerSessionManager::updateHintMode(const std::string &mode, bool enabled) {
ALOGV("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled);
@ -124,16 +120,16 @@ void PowerSessionManager::handleMessage(const Message &) {
}
void PowerSessionManager::enableSystemTopAppBoost() {
if (mHintManager) {
if (HintManager::GetInstance()->IsHintSupported(kDisableBoostHintName)) {
ALOGV("PowerSessionManager::enableSystemTopAppBoost!!");
mHintManager->EndHint(kDisableBoostHintName);
HintManager::GetInstance()->EndHint(kDisableBoostHintName);
}
}
void PowerSessionManager::disableSystemTopAppBoost() {
if (mHintManager) {
if (HintManager::GetInstance()->IsHintSupported(kDisableBoostHintName)) {
ALOGV("PowerSessionManager::disableSystemTopAppBoost!!");
mHintManager->DoHint(kDisableBoostHintName);
HintManager::GetInstance()->DoHint(kDisableBoostHintName);
}
}

View File

@ -51,7 +51,6 @@ class PowerSessionManager : public MessageHandler {
void removePowerSession(PowerHintSession *session);
void handleMessage(const Message &message) override;
void setHintManager(std::shared_ptr<HintManager> const &hint_manager);
// Singleton
static sp<PowerSessionManager> getInstance() {
@ -64,7 +63,6 @@ class PowerSessionManager : public MessageHandler {
void disableSystemTopAppBoost();
void enableSystemTopAppBoost();
const std::string kDisableBoostHintName;
std::shared_ptr<HintManager> mHintManager;
std::unordered_set<PowerHintSession *> mSessions; // protected by mLock
std::unordered_map<int, int> mTidRefCountMap; // protected by mLock
std::mutex mLock;
@ -74,7 +72,6 @@ class PowerSessionManager : public MessageHandler {
PowerSessionManager()
: kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost,
"ADPF_DISABLE_TA_BOOST")),
mHintManager(nullptr),
mDisplayRefreshRate(60),
mActive(false) {}
PowerSessionManager(PowerSessionManager const &) = delete;

View File

@ -16,12 +16,13 @@
#define LOG_TAG "powerhal-libperfmgr"
#include <thread>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <perfmgr/HintManager.h>
#include <thread>
#include "Power.h"
#include "PowerExt.h"
@ -34,37 +35,23 @@ using aidl::google::hardware::power::impl::pixel::PowerSessionManager;
using ::android::perfmgr::HintManager;
constexpr std::string_view kPowerHalInitProp("vendor.powerhal.init");
constexpr std::string_view kConfigDebugPathProperty("vendor.powerhal.config.debug");
constexpr std::string_view kConfigProperty("vendor.powerhal.config");
constexpr std::string_view kConfigDefaultFileName("powerhint.json");
int main() {
std::string config_path = "/vendor/etc/";
if (android::base::GetBoolProperty(kConfigDebugPathProperty.data(), false)) {
config_path = "/data/vendor/etc/";
LOG(WARNING) << "Xiaomi Power HAL AIDL Service is using debug config from: " << config_path;
}
config_path.append(
android::base::GetProperty(kConfigProperty.data(), kConfigDefaultFileName.data()));
LOG(INFO) << "Xiaomi Power HAL AIDL Service with Extension is starting with config: "
<< config_path;
// Parse config but do not start the looper
std::shared_ptr<HintManager> hm = HintManager::GetFromJSON(config_path, false);
std::shared_ptr<HintManager> hm = HintManager::GetInstance();
if (!hm) {
LOG(FATAL) << "Invalid config: " << config_path;
LOG(FATAL) << "HintManager Init failed";
}
// single thread
ABinderProcess_setThreadPoolMaxThreadCount(0);
// core service
std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(hm);
std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>();
ndk::SpAIBinder pwBinder = pw->asBinder();
// extension service
std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(hm);
std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>();
// attach the extension to the same binder we will be registering
CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get()));
@ -76,12 +63,11 @@ int main() {
if (::android::base::GetIntProperty("vendor.powerhal.adpf.rate", -1) != -1) {
PowerHintMonitor::getInstance()->start();
PowerSessionManager::getInstance()->setHintManager(hm);
}
std::thread initThread([&]() {
::android::base::WaitForProperty(kPowerHalInitProp.data(), "1");
hm->Start();
HintManager::GetInstance()->Start();
});
initThread.detach();