forked from donjohanliebert/hardware_xiaomi
Move to common power-libperfmgr
Change-Id: Icaa3b265fbc0fb71ae57a5cbde1654f62d07aef8
This commit is contained in:
parent
1bdeed2029
commit
44cf4e0f85
21
Android.mk
21
Android.mk
@ -1,21 +0,0 @@
|
||||
#
|
||||
# Copyright (C) 2021 The LineageOS 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.
|
||||
#
|
||||
|
||||
ifeq ($(BOARD_VENDOR),xiaomi)
|
||||
|
||||
include $(call all-subdir-makefiles)
|
||||
|
||||
endif
|
@ -1,66 +0,0 @@
|
||||
#
|
||||
# Copyright (C) 2021 The LineageOS 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.
|
||||
#
|
||||
|
||||
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 \
|
||||
libbinder_ndk \
|
||||
libcutils \
|
||||
libdl \
|
||||
liblog \
|
||||
libperfmgr \
|
||||
libprocessgroup \
|
||||
libutils \
|
||||
pixel-power-ext-V1-ndk
|
||||
|
||||
LOCAL_SRC_FILES := \
|
||||
service.cpp \
|
||||
InteractionHandler.cpp \
|
||||
Power.cpp \
|
||||
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.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.xiaomi.xml
|
||||
|
||||
include $(BUILD_EXECUTABLE)
|
||||
|
||||
endif
|
||||
endif
|
@ -1,283 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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 ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
|
||||
#define LOG_TAG "powerhal-libperfmgr"
|
||||
|
||||
#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 <utils/Log.h>
|
||||
#include <utils/Trace.h>
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
#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<const char *, 2> kDispIdlePath = {"/sys/class/drm/card0/device/idle_state",
|
||||
"/sys/class/graphics/fb0/idle_state"};
|
||||
static const uint32_t kWaitMs =
|
||||
::android::base::GetUintProperty("vendor.powerhal.disp.idle_wait", /*default*/ 100U);
|
||||
static const uint32_t kMinDurationMs =
|
||||
::android::base::GetUintProperty("vendor.powerhal.interaction.min", /*default*/ 1400U);
|
||||
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) {
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd >= 0)
|
||||
return fd;
|
||||
}
|
||||
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<std::mutex> 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<std::thread>(new std::thread(&InteractionHandler::Routine, this));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void InteractionHandler::Exit() {
|
||||
std::unique_lock<std::mutex> 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<std::mutex> lk(mLock);
|
||||
|
||||
int inputDuration = duration + kDurationOffsetMs;
|
||||
int finalDuration;
|
||||
if (inputDuration > kMaxDurationMs)
|
||||
finalDuration = kMaxDurationMs;
|
||||
else if (inputDuration > kMinDurationMs)
|
||||
finalDuration = inputDuration;
|
||||
else
|
||||
finalDuration = kMinDurationMs;
|
||||
|
||||
// Fallback to do boost directly
|
||||
// 1) override property is set OR
|
||||
// 2) InteractionHandler not initialized
|
||||
if (!kDisplayIdleSupport || mState == INTERACTION_STATE_UNINITIALIZED) {
|
||||
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) {
|
||||
size_t elapsed_time = CalcTimespecDiffMs(mLastTimespec, cur_timespec);
|
||||
// don't hint if previous hint's duration covers this hint's duration
|
||||
if (elapsed_time <= (mDurationMs - finalDuration)) {
|
||||
ALOGV("%s: Previous duration (%d) cover this (%d) elapsed: %lld", __func__,
|
||||
static_cast<int>(mDurationMs), static_cast<int>(finalDuration),
|
||||
static_cast<long long>(elapsed_time));
|
||||
return;
|
||||
}
|
||||
}
|
||||
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<std::mutex> lk(mLock);
|
||||
if (mState == INTERACTION_STATE_WAITING) {
|
||||
ATRACE_CALL();
|
||||
PerfRel();
|
||||
mState = INTERACTION_STATE_IDLE;
|
||||
} else {
|
||||
// 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;
|
||||
ssize_t ret = write(mEventFd, &val, sizeof(val));
|
||||
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__);
|
||||
return;
|
||||
} else if (ret < 0) {
|
||||
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);
|
||||
else if (ret == 0)
|
||||
ALOGV("%s: timed out waiting for idle", __func__);
|
||||
else if (pfd[0].revents)
|
||||
ALOGV("%s: wait for idle aborted", __func__);
|
||||
else if (pfd[1].revents)
|
||||
ALOGV("%s: idle detected", __func__);
|
||||
}
|
||||
|
||||
void InteractionHandler::Routine() {
|
||||
pthread_setname_np(pthread_self(), "DispIdle");
|
||||
std::unique_lock<std::mutex> lk(mLock, std::defer_lock);
|
||||
|
||||
while (true) {
|
||||
lk.lock();
|
||||
mCond.wait(lk, [&] { return mState != INTERACTION_STATE_IDLE; });
|
||||
if (mState == INTERACTION_STATE_UNINITIALIZED)
|
||||
return;
|
||||
mState = INTERACTION_STATE_WAITING;
|
||||
lk.unlock();
|
||||
|
||||
WaitForIdle(kWaitMs, mDurationMs);
|
||||
Release();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
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();
|
||||
~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;
|
||||
int32_t mDurationMs;
|
||||
struct timespec mLastTimespec;
|
||||
std::unique_ptr<std::thread> mThread;
|
||||
std::mutex mLock;
|
||||
std::condition_variable mCond;
|
||||
};
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,255 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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"
|
||||
|
||||
#include "Power.h"
|
||||
|
||||
#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"
|
||||
|
||||
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<InteractionHandler>();
|
||||
mInteractionHandler->Init();
|
||||
|
||||
std::string state = ::android::base::GetProperty(kPowerHalStateProp, "");
|
||||
if (state == "SUSTAINED_PERFORMANCE") {
|
||||
LOG(INFO) << "Initialize with SUSTAINED_PERFORMANCE on";
|
||||
HintManager::GetInstance()->DoHint("SUSTAINED_PERFORMANCE");
|
||||
mSustainedPerfModeOn = true;
|
||||
} 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() &&
|
||||
HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs > 0) {
|
||||
PowerSessionManager::getInstance()->updateHintMode(toString(type), enabled);
|
||||
}
|
||||
#ifdef MODE_EXT
|
||||
if (setDeviceSpecificMode(type, enabled)) {
|
||||
return ndk::ScopedAStatus::ok();
|
||||
}
|
||||
#endif
|
||||
switch (type) {
|
||||
#ifdef TAP_TO_WAKE_NODE
|
||||
case Mode::DOUBLE_TAP_TO_WAKE:
|
||||
::android::base::WriteStringToFile(enabled ? "1" : "0", TAP_TO_WAKE_NODE, true);
|
||||
break;
|
||||
#endif
|
||||
case Mode::SUSTAINED_PERFORMANCE:
|
||||
if (enabled) {
|
||||
HintManager::GetInstance()->DoHint("SUSTAINED_PERFORMANCE");
|
||||
}
|
||||
mSustainedPerfModeOn = true;
|
||||
break;
|
||||
case Mode::LAUNCH:
|
||||
if (mSustainedPerfModeOn) {
|
||||
break;
|
||||
}
|
||||
[[fallthrough]];
|
||||
#ifndef TAP_TO_WAKE_NODE
|
||||
case Mode::DOUBLE_TAP_TO_WAKE:
|
||||
[[fallthrough]];
|
||||
#endif
|
||||
case Mode::FIXED_PERFORMANCE:
|
||||
[[fallthrough]];
|
||||
case Mode::EXPENSIVE_RENDERING:
|
||||
[[fallthrough]];
|
||||
case Mode::INTERACTIVE:
|
||||
[[fallthrough]];
|
||||
case Mode::DEVICE_IDLE:
|
||||
[[fallthrough]];
|
||||
case Mode::DISPLAY_INACTIVE:
|
||||
[[fallthrough]];
|
||||
case Mode::AUDIO_STREAMING_LOW_LATENCY:
|
||||
[[fallthrough]];
|
||||
default:
|
||||
if (enabled) {
|
||||
HintManager::GetInstance()->DoHint(toString(type));
|
||||
} else {
|
||||
HintManager::GetInstance()->EndHint(toString(type));
|
||||
}
|
||||
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) {
|
||||
supported = true;
|
||||
}
|
||||
#endif
|
||||
LOG(INFO) << "Power mode " << toString(type) << " isModeSupported: " << supported;
|
||||
*_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() &&
|
||||
HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs > 0) {
|
||||
PowerSessionManager::getInstance()->updateHintBoost(toString(type), durationMs);
|
||||
}
|
||||
switch (type) {
|
||||
case Boost::INTERACTION:
|
||||
if (mSustainedPerfModeOn) {
|
||||
break;
|
||||
}
|
||||
mInteractionHandler->Acquire(durationMs);
|
||||
break;
|
||||
case Boost::DISPLAY_UPDATE_IMMINENT:
|
||||
[[fallthrough]];
|
||||
case Boost::ML_ACC:
|
||||
[[fallthrough]];
|
||||
case Boost::AUDIO_LAUNCH:
|
||||
[[fallthrough]];
|
||||
default:
|
||||
if (mSustainedPerfModeOn) {
|
||||
break;
|
||||
}
|
||||
if (durationMs > 0) {
|
||||
HintManager::GetInstance()->DoHint(toString(type),
|
||||
std::chrono::milliseconds(durationMs));
|
||||
} else if (durationMs == 0) {
|
||||
HintManager::GetInstance()->DoHint(toString(type));
|
||||
} else {
|
||||
HintManager::GetInstance()->EndHint(toString(type));
|
||||
}
|
||||
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"
|
||||
"SustainedPerformanceMode: %s\n",
|
||||
boolToString(HintManager::GetInstance()->IsRunning()),
|
||||
boolToString(mSustainedPerfModeOn)));
|
||||
// Dump nodes through libperfmgr
|
||||
HintManager::GetInstance()->DumpToFd(fd);
|
||||
PowerSessionManager::getInstance()->dumpToFd(fd);
|
||||
if (!::android::base::WriteStringToFd(buf, fd)) {
|
||||
PLOG(ERROR) << "Failed to dump state to fd";
|
||||
}
|
||||
fsync(fd);
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
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 (!HintManager::GetInstance()->GetAdpfProfile() ||
|
||||
HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs <= 0) {
|
||||
*_aidl_return = nullptr;
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
|
||||
}
|
||||
if (threadIds.size() == 0) {
|
||||
LOG(ERROR) << "Error: threadIds.size() shouldn't be " << threadIds.size();
|
||||
*_aidl_return = nullptr;
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
||||
}
|
||||
std::shared_ptr<IPowerHintSession> session = ndk::SharedRefBase::make<PowerHintSession>(
|
||||
tgid, uid, threadIds, durationNanos);
|
||||
*_aidl_return = session;
|
||||
return ndk::ScopedAStatus::ok();
|
||||
}
|
||||
|
||||
ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t *outNanoseconds) {
|
||||
*outNanoseconds = HintManager::GetInstance()->GetAdpfProfile()
|
||||
? HintManager::GetInstance()->GetAdpfProfile()->mReportingRateLimitNs
|
||||
: 0;
|
||||
if (*outNanoseconds <= 0) {
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
|
||||
}
|
||||
|
||||
return ndk::ScopedAStatus::ok();
|
||||
}
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <aidl/android/hardware/power/BnPower.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#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();
|
||||
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;
|
||||
ndk::ScopedAStatus isBoostSupported(Boost type, bool *_aidl_return) override;
|
||||
ndk::ScopedAStatus createHintSession(int32_t tgid, int32_t uid,
|
||||
const std::vector<int32_t> &threadIds,
|
||||
int64_t durationNanos,
|
||||
std::shared_ptr<IPowerHintSession> *_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<InteractionHandler> mInteractionHandler;
|
||||
std::atomic<bool> mSustainedPerfModeOn;
|
||||
};
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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 "android.hardware.power-service.xiaomi.ext-libperfmgr"
|
||||
|
||||
#include "PowerExt.h"
|
||||
|
||||
#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 {
|
||||
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 {
|
||||
HintManager::GetInstance()->EndHint(mode);
|
||||
}
|
||||
if (HintManager::GetInstance()->GetAdpfProfile() &&
|
||||
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) {
|
||||
HintManager::GetInstance()->DoHint(boost);
|
||||
} 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
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include <aidl/google/hardware/power/extension/pixel/BnPowerExt.h>
|
||||
#include <perfmgr/HintManager.h>
|
||||
|
||||
namespace aidl {
|
||||
namespace google {
|
||||
namespace hardware {
|
||||
namespace power {
|
||||
namespace impl {
|
||||
namespace pixel {
|
||||
|
||||
class PowerExt : public ::aidl::google::hardware::power::extension::pixel::BnPowerExt {
|
||||
public:
|
||||
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:
|
||||
};
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,433 +0,0 @@
|
||||
/*
|
||||
* 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 "PowerHintSession.h"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/parsedouble.h>
|
||||
#include <android-base/properties.h>
|
||||
#include <android-base/stringprintf.h>
|
||||
#include <perfmgr/AdpfConfig.h>
|
||||
#include <private/android_filesystem_config.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <time.h>
|
||||
#include <utils/Trace.h>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#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<WorkDuration> &actualDurations) {
|
||||
std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
|
||||
const nanoseconds &targetDuration = mDescriptor->duration;
|
||||
int64_t &integral_error = mDescriptor->integral_error;
|
||||
int64_t &previous_error = mDescriptor->previous_error;
|
||||
uint64_t samplingWindowP = adpfConfig->mSamplingWindowP;
|
||||
uint64_t samplingWindowI = adpfConfig->mSamplingWindowI;
|
||||
uint64_t samplingWindowD = adpfConfig->mSamplingWindowD;
|
||||
int64_t targetDurationNanos = (int64_t)targetDuration.count();
|
||||
int64_t length = actualDurations.size();
|
||||
int64_t p_start =
|
||||
samplingWindowP == 0 || samplingWindowP > length ? 0 : length - samplingWindowP;
|
||||
int64_t i_start =
|
||||
samplingWindowI == 0 || samplingWindowI > length ? 0 : length - samplingWindowI;
|
||||
int64_t d_start =
|
||||
samplingWindowD == 0 || samplingWindowD > length ? 0 : length - samplingWindowD;
|
||||
int64_t dt = ns_to_100us(targetDurationNanos);
|
||||
int64_t err_sum = 0;
|
||||
int64_t derivative_sum = 0;
|
||||
for (int64_t i = std::min({p_start, i_start, d_start}); i < length; i++) {
|
||||
int64_t actualDurationNanos = actualDurations[i].durationNanos;
|
||||
if (std::abs(actualDurationNanos) > targetDurationNanos * 20) {
|
||||
ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")",
|
||||
actualDurationNanos, targetDurationNanos);
|
||||
}
|
||||
// PID control algorithm
|
||||
int64_t error = ns_to_100us(actualDurationNanos - targetDurationNanos);
|
||||
if (i >= d_start) {
|
||||
derivative_sum += error - previous_error;
|
||||
}
|
||||
if (i >= p_start) {
|
||||
err_sum += error;
|
||||
}
|
||||
if (i >= i_start) {
|
||||
integral_error += error * dt;
|
||||
integral_error = std::min(adpfConfig->getPidIHighDivI(), integral_error);
|
||||
integral_error = std::max(adpfConfig->getPidILowDivI(), integral_error);
|
||||
}
|
||||
previous_error = error;
|
||||
}
|
||||
int64_t pOut = static_cast<int64_t>((err_sum > 0 ? adpfConfig->mPidPo : adpfConfig->mPidPu) *
|
||||
err_sum / (length - p_start));
|
||||
int64_t iOut = static_cast<int64_t>(adpfConfig->mPidI * integral_error);
|
||||
int64_t dOut =
|
||||
static_cast<int64_t>((derivative_sum > 0 ? adpfConfig->mPidDo : adpfConfig->mPidDu) *
|
||||
derivative_sum / dt / (length - d_start));
|
||||
|
||||
int64_t output = pOut + iOut + dOut;
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("pid.err", err_sum / (length - p_start));
|
||||
traceSessionVal("pid.integral", integral_error);
|
||||
traceSessionVal("pid.derivative", derivative_sum / dt / (length - d_start));
|
||||
traceSessionVal("pid.pOut", pOut);
|
||||
traceSessionVal("pid.iOut", iOut);
|
||||
traceSessionVal("pid.dOut", dOut);
|
||||
traceSessionVal("pid.output", output);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
|
||||
int64_t durationNanos) {
|
||||
mDescriptor = new AppHintDesc(tgid, uid, threadIds);
|
||||
mDescriptor->duration = std::chrono::nanoseconds(durationNanos);
|
||||
mIdString = StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR, mDescriptor->tgid,
|
||||
mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
|
||||
mStaleTimerHandler = sp<StaleTimerHandler>(new StaleTimerHandler(this));
|
||||
mPowerManagerHandler = PowerSessionManager::getInstance();
|
||||
mLastUpdatedTime.store(std::chrono::steady_clock::now());
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("target", mDescriptor->duration.count());
|
||||
traceSessionVal("active", mDescriptor->is_active.load());
|
||||
}
|
||||
PowerSessionManager::getInstance()->addPowerSession(this);
|
||||
// init boost
|
||||
setSessionUclampMin(HintManager::GetInstance()->GetAdpfProfile()->mUclampMinInit);
|
||||
ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str());
|
||||
}
|
||||
|
||||
PowerHintSession::~PowerHintSession() {
|
||||
close();
|
||||
ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str());
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("target", 0);
|
||||
traceSessionVal("actl_last", 0);
|
||||
traceSessionVal("active", 0);
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (ATRACE_ENABLED()) {
|
||||
const std::string tag = StringPrintf("%s:updateUniveralBoostMode()", mIdString.c_str());
|
||||
ATRACE_BEGIN(tag.c_str());
|
||||
}
|
||||
PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
|
||||
if (ATRACE_ENABLED()) {
|
||||
ATRACE_END();
|
||||
}
|
||||
}
|
||||
|
||||
void PowerHintSession::tryToSendPowerHint(std::string hint) {
|
||||
if (!mSupportedHints[hint].has_value()) {
|
||||
mSupportedHints[hint] = HintManager::GetInstance()->IsHintSupported(hint);
|
||||
}
|
||||
if (mSupportedHints[hint].value()) {
|
||||
HintManager::GetInstance()->DoHint(hint);
|
||||
}
|
||||
}
|
||||
|
||||
int PowerHintSession::setSessionUclampMin(int32_t min) {
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(mSessionLock);
|
||||
mDescriptor->current_min = min;
|
||||
}
|
||||
if (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");
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
}
|
||||
if (!mDescriptor->is_active.load())
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
// Reset to default uclamp value.
|
||||
mDescriptor->is_active.store(false);
|
||||
setStale();
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("active", mDescriptor->is_active.load());
|
||||
}
|
||||
updateUniveralBoostMode();
|
||||
return ndk::ScopedAStatus::ok();
|
||||
}
|
||||
|
||||
ndk::ScopedAStatus PowerHintSession::resume() {
|
||||
if (mSessionClosed) {
|
||||
ALOGE("Error: session is dead");
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
}
|
||||
if (mDescriptor->is_active.load())
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
mDescriptor->is_active.store(true);
|
||||
// resume boost
|
||||
setSessionUclampMin(mDescriptor->current_min);
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("active", mDescriptor->is_active.load());
|
||||
}
|
||||
updateUniveralBoostMode();
|
||||
return ndk::ScopedAStatus::ok();
|
||||
}
|
||||
|
||||
ndk::ScopedAStatus PowerHintSession::close() {
|
||||
bool sessionClosedExpectedToBe = false;
|
||||
if (!mSessionClosed.compare_exchange_strong(sessionClosedExpectedToBe, true)) {
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
}
|
||||
// Remove the session from PowerSessionManager first to avoid racing.
|
||||
PowerSessionManager::getInstance()->removePowerSession(this);
|
||||
mStaleTimerHandler->setSessionDead();
|
||||
setSessionUclampMin(0);
|
||||
mDescriptor->is_active.store(false);
|
||||
updateUniveralBoostMode();
|
||||
return ndk::ScopedAStatus::ok();
|
||||
}
|
||||
|
||||
ndk::ScopedAStatus PowerHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) {
|
||||
if (mSessionClosed) {
|
||||
ALOGE("Error: session is dead");
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
}
|
||||
if (targetDurationNanos <= 0) {
|
||||
ALOGE("Error: targetDurationNanos(%" PRId64 ") should bigger than 0", targetDurationNanos);
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
||||
}
|
||||
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<WorkDuration> &actualDurations) {
|
||||
if (mSessionClosed) {
|
||||
ALOGE("Error: session is dead");
|
||||
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
|
||||
}
|
||||
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);
|
||||
}
|
||||
std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
|
||||
mDescriptor->update_count++;
|
||||
bool isFirstFrame = isTimeout();
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("batch_size", actualDurations.size());
|
||||
traceSessionVal("actl_last", actualDurations.back().durationNanos);
|
||||
traceSessionVal("target", mDescriptor->duration.count());
|
||||
traceSessionVal("hint.count", mDescriptor->update_count);
|
||||
traceSessionVal("hint.overtime",
|
||||
actualDurations.back().durationNanos - mDescriptor->duration.count() > 0);
|
||||
traceSessionVal("session_hint", -1);
|
||||
}
|
||||
|
||||
mLastUpdatedTime.store(std::chrono::steady_clock::now());
|
||||
if (isFirstFrame) {
|
||||
if (isAppSession()) {
|
||||
tryToSendPowerHint("ADPF_FIRST_FRAME");
|
||||
}
|
||||
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<int>(adpfConfig->mUclampMinHigh),
|
||||
mDescriptor->current_min + static_cast<int>(output));
|
||||
next_min = std::max(static_cast<int>(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<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::isTimeout() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
time_point<steady_clock> staleTime =
|
||||
mLastUpdatedTime.load() +
|
||||
nanoseconds(static_cast<int64_t>(
|
||||
mDescriptor->duration.count() *
|
||||
HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor));
|
||||
return now >= staleTime;
|
||||
}
|
||||
|
||||
const std::vector<int> &PowerHintSession::getTidList() const {
|
||||
return mDescriptor->threadIds;
|
||||
}
|
||||
|
||||
void PowerHintSession::setStale() {
|
||||
// Reset to default uclamp value.
|
||||
PowerSessionManager::getInstance()->setUclampMin(this, 0);
|
||||
// Deliver a task to check if all sessions are inactive.
|
||||
updateUniveralBoostMode();
|
||||
if (ATRACE_ENABLED()) {
|
||||
traceSessionVal("min", 0);
|
||||
}
|
||||
}
|
||||
|
||||
void PowerHintSession::StaleTimerHandler::updateTimer() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
nanoseconds staleDuration = std::chrono::nanoseconds(
|
||||
static_cast<int64_t>(mSession->mDescriptor->duration.count() *
|
||||
HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor));
|
||||
mStaleTime.store(now + staleDuration);
|
||||
int64_t next = static_cast<int64_t>(staleDuration.count());
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(mMessageLock);
|
||||
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
|
||||
PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
|
||||
next, mSession->mStaleTimerHandler, NULL);
|
||||
}
|
||||
if (ATRACE_ENABLED()) {
|
||||
mSession->traceSessionVal("timer.stale", 0);
|
||||
}
|
||||
}
|
||||
|
||||
void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) {
|
||||
std::lock_guard<std::mutex> guard(mClosedLock);
|
||||
if (mIsSessionDead) {
|
||||
return;
|
||||
}
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
int64_t next =
|
||||
static_cast<int64_t>(duration_cast<nanoseconds>(mStaleTime.load() - now).count());
|
||||
if (next > 0) {
|
||||
// Schedule for the stale timeout check.
|
||||
std::lock_guard<std::mutex> guard(mMessageLock);
|
||||
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
|
||||
PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
|
||||
next, mSession->mStaleTimerHandler, NULL);
|
||||
} else {
|
||||
mSession->setStale();
|
||||
if (ATRACE_ENABLED()) {
|
||||
mSession->traceSessionVal("session_hint", -1);
|
||||
}
|
||||
}
|
||||
if (ATRACE_ENABLED()) {
|
||||
mSession->traceSessionVal("timer.stale", next > 0 ? 0 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
void PowerHintSession::StaleTimerHandler::setSessionDead() {
|
||||
std::lock_guard<std::mutex> guard(mClosedLock);
|
||||
mIsSessionDead = true;
|
||||
PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler);
|
||||
}
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,129 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <aidl/android/hardware/power/BnPowerHintSession.h>
|
||||
#include <aidl/android/hardware/power/WorkDuration.h>
|
||||
#include <utils/Looper.h>
|
||||
#include <utils/Thread.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
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;
|
||||
using ::android::MessageHandler;
|
||||
using ::android::sp;
|
||||
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<int> threadIds)
|
||||
: tgid(tgid),
|
||||
uid(uid),
|
||||
threadIds(std::move(threadIds)),
|
||||
duration(0LL),
|
||||
current_min(0),
|
||||
is_active(true),
|
||||
update_count(0),
|
||||
integral_error(0),
|
||||
previous_error(0) {}
|
||||
std::string toString() const;
|
||||
const int32_t tgid;
|
||||
const int32_t uid;
|
||||
const std::vector<int> threadIds;
|
||||
nanoseconds duration;
|
||||
int current_min;
|
||||
// status
|
||||
std::atomic<bool> is_active;
|
||||
// pid
|
||||
uint64_t update_count;
|
||||
int64_t integral_error;
|
||||
int64_t previous_error;
|
||||
};
|
||||
|
||||
class PowerHintSession : public BnPowerHintSession {
|
||||
public:
|
||||
explicit PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
|
||||
int64_t durationNanos);
|
||||
~PowerHintSession();
|
||||
ndk::ScopedAStatus close() override;
|
||||
ndk::ScopedAStatus pause() override;
|
||||
ndk::ScopedAStatus resume() override;
|
||||
ndk::ScopedAStatus updateTargetWorkDuration(int64_t targetDurationNanos) override;
|
||||
ndk::ScopedAStatus reportActualWorkDuration(
|
||||
const std::vector<WorkDuration> &actualDurations) override;
|
||||
bool isActive();
|
||||
bool isTimeout();
|
||||
void setStale();
|
||||
// Is this hint session for a user application
|
||||
bool isAppSession();
|
||||
const std::vector<int> &getTidList() const;
|
||||
int getUclampMin();
|
||||
void dumpToStream(std::ostream &stream);
|
||||
|
||||
time_point<steady_clock> getStaleTime();
|
||||
|
||||
private:
|
||||
class StaleTimerHandler : public MessageHandler {
|
||||
public:
|
||||
StaleTimerHandler(PowerHintSession *session) : mSession(session), mIsSessionDead(false) {}
|
||||
void updateTimer();
|
||||
void handleMessage(const Message &message) override;
|
||||
void setSessionDead();
|
||||
|
||||
private:
|
||||
PowerHintSession *mSession;
|
||||
std::mutex mClosedLock;
|
||||
std::mutex mMessageLock;
|
||||
std::atomic<time_point<steady_clock>> mStaleTime;
|
||||
bool mIsSessionDead;
|
||||
};
|
||||
|
||||
private:
|
||||
void updateUniveralBoostMode();
|
||||
int setSessionUclampMin(int32_t min);
|
||||
void tryToSendPowerHint(std::string hint);
|
||||
int64_t convertWorkDurationToBoostByPid(const std::vector<WorkDuration> &actualDurations);
|
||||
void traceSessionVal(char const *identifier, int64_t val) const;
|
||||
AppHintDesc *mDescriptor = nullptr;
|
||||
sp<StaleTimerHandler> mStaleTimerHandler;
|
||||
std::atomic<time_point<steady_clock>> mLastUpdatedTime;
|
||||
sp<MessageHandler> mPowerManagerHandler;
|
||||
std::mutex mSessionLock;
|
||||
std::atomic<bool> mSessionClosed = false;
|
||||
std::string mIdString;
|
||||
// To cache the status of whether ADPF hints are supported.
|
||||
std::unordered_map<std::string, std::optional<bool>> mSupportedHints;
|
||||
};
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,254 +0,0 @@
|
||||
/*
|
||||
* 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 "PowerSessionManager.h"
|
||||
|
||||
#include <android-base/file.h>
|
||||
#include <log/log.h>
|
||||
#include <perfmgr/HintManager.h>
|
||||
#include <processgroup/processgroup.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <utils/Trace.h>
|
||||
|
||||
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 {
|
||||
__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) {
|
||||
if (!HintManager::GetInstance()->GetAdpfProfile()->mUclampMinOn) {
|
||||
ALOGV("PowerSessionManager:%s: skip", __func__);
|
||||
return 0;
|
||||
}
|
||||
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) {
|
||||
if (mode.compare("REFRESH_120FPS") == 0) {
|
||||
mDisplayRefreshRate = 120;
|
||||
} else if (mode.compare("REFRESH_90FPS") == 0) {
|
||||
mDisplayRefreshRate = 90;
|
||||
} else if (mode.compare("REFRESH_60FPS") == 0) {
|
||||
mDisplayRefreshRate = 60;
|
||||
}
|
||||
}
|
||||
if (HintManager::GetInstance()->GetAdpfProfile()) {
|
||||
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<std::mutex> guard(mLock);
|
||||
for (auto t : session->getTidList()) {
|
||||
mTidSessionListMap[t].insert(session);
|
||||
if (mTidRefCountMap.find(t) == mTidRefCountMap.end()) {
|
||||
if (!SetTaskProfiles(t, {"ResetUclampGrp"})) {
|
||||
ALOGW("Failed to set ResetUclampGrp task profile for tid:%d", t);
|
||||
} else {
|
||||
mTidRefCountMap[t] = 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (mTidRefCountMap[t] <= 0) {
|
||||
ALOGE("Error! Unexpected zero/negative RefCount:%d for tid:%d", mTidRefCountMap[t], t);
|
||||
continue;
|
||||
}
|
||||
mTidRefCountMap[t]++;
|
||||
}
|
||||
mSessions.insert(session);
|
||||
}
|
||||
|
||||
void PowerSessionManager::removePowerSession(PowerHintSession *session) {
|
||||
std::lock_guard<std::mutex> guard(mLock);
|
||||
for (auto t : session->getTidList()) {
|
||||
if (mTidRefCountMap.find(t) == mTidRefCountMap.end()) {
|
||||
ALOGE("Unexpected Error! Failed to look up tid:%d in TidRefCountMap", t);
|
||||
continue;
|
||||
}
|
||||
mTidSessionListMap[t].erase(session);
|
||||
mTidRefCountMap[t]--;
|
||||
if (mTidRefCountMap[t] <= 0) {
|
||||
if (!SetTaskProfiles(t, {"NoResetUclampGrp"})) {
|
||||
ALOGW("Failed to set NoResetUclampGrp task profile for tid:%d", t);
|
||||
}
|
||||
mTidRefCountMap.erase(t);
|
||||
}
|
||||
}
|
||||
mSessions.erase(session);
|
||||
}
|
||||
|
||||
void PowerSessionManager::setUclampMin(PowerHintSession *session, int val) {
|
||||
std::lock_guard<std::mutex> 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.
|
||||
int tidMax = 0;
|
||||
for (PowerHintSession *s : mTidSessionListMap[t]) {
|
||||
if (!s->isActive() || s->isTimeout())
|
||||
continue;
|
||||
tidMax = std::max(tidMax, s->getUclampMin());
|
||||
}
|
||||
set_uclamp_min(t, std::max(val, tidMax));
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<bool> PowerSessionManager::isAnyAppSessionActive() {
|
||||
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->isTimeout() && s->isAppSession()) {
|
||||
active = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (active == mActive) {
|
||||
return std::nullopt;
|
||||
} else {
|
||||
mActive = active;
|
||||
}
|
||||
|
||||
return active;
|
||||
}
|
||||
|
||||
void PowerSessionManager::handleMessage(const Message &) {
|
||||
auto active = isAnyAppSessionActive();
|
||||
if (!active.has_value()) {
|
||||
return;
|
||||
}
|
||||
if (active.value()) {
|
||||
disableSystemTopAppBoost();
|
||||
} else {
|
||||
enableSystemTopAppBoost();
|
||||
}
|
||||
}
|
||||
|
||||
void PowerSessionManager::dumpToFd(int fd) {
|
||||
std::ostringstream dump_buf;
|
||||
std::lock_guard<std::mutex> guard(mLock);
|
||||
dump_buf << "========== Begin PowerSessionManager ADPF list ==========\n";
|
||||
for (PowerHintSession *s : mSessions) {
|
||||
s->dumpToStream(dump_buf);
|
||||
dump_buf << " Tid:Ref[";
|
||||
for (size_t i = 0, len = s->getTidList().size(); i < len; i++) {
|
||||
int t = s->getTidList()[i];
|
||||
dump_buf << t << ":" << mTidSessionListMap[t].size();
|
||||
if (i < len - 1) {
|
||||
dump_buf << ", ";
|
||||
}
|
||||
}
|
||||
dump_buf << "]\n";
|
||||
}
|
||||
dump_buf << "========== End PowerSessionManager ADPF list ==========\n";
|
||||
if (!::android::base::WriteStringToFd(dump_buf.str(), 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<Looper> PowerHintMonitor::getLooper() {
|
||||
return mLooper;
|
||||
}
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,113 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <android-base/properties.h>
|
||||
#include <perfmgr/HintManager.h>
|
||||
#include <utils/Looper.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <unordered_set>
|
||||
|
||||
#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
|
||||
void updateHintMode(const std::string &mode, bool enabled);
|
||||
void updateHintBoost(const std::string &boost, int32_t durationMs);
|
||||
int getDisplayRefreshRate();
|
||||
// monitoring session status
|
||||
void addPowerSession(PowerHintSession *session);
|
||||
void removePowerSession(PowerHintSession *session);
|
||||
void setUclampMin(PowerHintSession *session, int min);
|
||||
void setUclampMinLocked(PowerHintSession *session, int min);
|
||||
void handleMessage(const Message &message) override;
|
||||
void dumpToFd(int fd);
|
||||
|
||||
// Singleton
|
||||
static sp<PowerSessionManager> getInstance() {
|
||||
static sp<PowerSessionManager> instance = new PowerSessionManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<bool> isAnyAppSessionActive();
|
||||
void disableSystemTopAppBoost();
|
||||
void enableSystemTopAppBoost();
|
||||
const std::string kDisableBoostHintName;
|
||||
|
||||
std::unordered_set<PowerHintSession *> mSessions; // protected by mLock
|
||||
std::unordered_map<int, int> mTidRefCountMap; // protected by mLock
|
||||
std::unordered_map<int, std::unordered_set<PowerHintSession *>> mTidSessionListMap;
|
||||
bool mActive; // protected by mLock
|
||||
/**
|
||||
* mLock to pretect the above data objects opertions.
|
||||
**/
|
||||
std::mutex mLock;
|
||||
int mDisplayRefreshRate;
|
||||
// Singleton
|
||||
PowerSessionManager()
|
||||
: kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost,
|
||||
"ADPF_DISABLE_TA_BOOST")),
|
||||
mActive(false),
|
||||
mDisplayRefreshRate(60) {}
|
||||
PowerSessionManager(PowerSessionManager const &) = delete;
|
||||
void operator=(PowerSessionManager const &) = delete;
|
||||
};
|
||||
|
||||
class PowerHintMonitor : public Thread {
|
||||
public:
|
||||
void start();
|
||||
bool threadLoop() override;
|
||||
sp<Looper> getLooper();
|
||||
// Singleton
|
||||
static sp<PowerHintMonitor> getInstance() {
|
||||
static sp<PowerHintMonitor> instance = new PowerHintMonitor();
|
||||
return instance;
|
||||
}
|
||||
PowerHintMonitor(PowerHintMonitor const &) = delete;
|
||||
void operator=(PowerHintMonitor const &) = delete;
|
||||
|
||||
private:
|
||||
sp<Looper> mLooper;
|
||||
// Singleton
|
||||
PowerHintMonitor() : Thread(false), mLooper(new Looper(true)) {}
|
||||
};
|
||||
|
||||
} // namespace pixel
|
||||
} // namespace impl
|
||||
} // namespace power
|
||||
} // namespace hardware
|
||||
} // namespace google
|
||||
} // namespace aidl
|
@ -1,32 +0,0 @@
|
||||
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}
|
||||
|
@ -1,7 +0,0 @@
|
||||
<manifest version="1.0" type="device">
|
||||
<hal format="aidl">
|
||||
<name>android.hardware.power</name>
|
||||
<version>2</version>
|
||||
<fqname>IPower/default</fqname>
|
||||
</hal>
|
||||
</manifest>
|
@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/properties.h>
|
||||
#include <android/binder_ibinder_platform.h>
|
||||
#include <android/binder_manager.h>
|
||||
#include <android/binder_process.h>
|
||||
#include <perfmgr/HintManager.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
#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
|
||||
std::shared_ptr<HintManager> hm = HintManager::GetInstance();
|
||||
if (!hm) {
|
||||
LOG(FATAL) << "HintManager Init failed";
|
||||
}
|
||||
|
||||
// single thread
|
||||
ABinderProcess_setThreadPoolMaxThreadCount(0);
|
||||
|
||||
// core service
|
||||
std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>();
|
||||
ndk::SpAIBinder pwBinder = pw->asBinder();
|
||||
AIBinder_setMinSchedulerPolicy(pwBinder.get(), SCHED_NORMAL, -20);
|
||||
|
||||
// extension service
|
||||
std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>();
|
||||
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) << "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) << "Xiaomi Power HAL AIDL Service with Extension just died.";
|
||||
return EXIT_FAILURE;
|
||||
}
|
Loading…
Reference in New Issue
Block a user