mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-27 06:43:32 +00:00
Bug 1848766 - Add PerformanceHintManager implementation to HAL. r=smaug,emilio,geckoview-reviewers,owlish
PerformanceHintManager is an Android API that allows the caller to create a PerformanceHintSession, representing a workload shared by a group of threads that should be completed within a target duration each cycle. The actual duration spent working is reported each cycle, and the system can then adjust scheduling of the threads accordingly in order to hit the target going forward. This patch adds the API to HAL along with an Android implementation (and a nop fallback implementation for other platforms). Differential Revision: https://phabricator.services.mozilla.com/D186238
This commit is contained in:
parent
fdf073cfa0
commit
f5ac319074
@ -417,6 +417,12 @@ const char* ProcessPriorityToString(ProcessPriority aPriority) {
|
||||
}
|
||||
}
|
||||
|
||||
UniquePtr<hal::PerformanceHintSession> CreatePerformanceHintSession(
|
||||
const nsTArray<PlatformThreadHandle>& aThreads,
|
||||
mozilla::TimeDuration aTargetWorkDuration) {
|
||||
return hal_impl::CreatePerformanceHintSession(aThreads, aTargetWorkDuration);
|
||||
}
|
||||
|
||||
void Init() {
|
||||
MOZ_ASSERT(!sInitialized);
|
||||
|
||||
|
15
hal/Hal.h
15
hal/Hal.h
@ -7,6 +7,7 @@
|
||||
#ifndef mozilla_Hal_h
|
||||
#define mozilla_Hal_h
|
||||
|
||||
#include "base/platform_thread.h"
|
||||
#include "nsTArray.h"
|
||||
#include "mozilla/hal_sandbox/PHal.h"
|
||||
#include "mozilla/HalScreenConfiguration.h"
|
||||
@ -236,6 +237,20 @@ void UnlockScreenOrientation();
|
||||
*/
|
||||
void SetProcessPriority(int aPid, hal::ProcessPriority aPriority);
|
||||
|
||||
/**
|
||||
* Creates a PerformanceHintSession.
|
||||
*
|
||||
* A PerformanceHintSession represents a workload shared by a group of threads
|
||||
* that should be completed in a target duration each cycle.
|
||||
*
|
||||
* Each cycle, the actual work duration should be reported using
|
||||
* PerformanceHintSession::ReportActualWorkDuration(). The system can then
|
||||
* adjust the scheduling accordingly in order to achieve the target.
|
||||
*/
|
||||
UniquePtr<hal::PerformanceHintSession> CreatePerformanceHintSession(
|
||||
const nsTArray<PlatformThreadHandle>& aThreads,
|
||||
mozilla::TimeDuration aTargetWorkDuration);
|
||||
|
||||
} // namespace MOZ_HAL_NAMESPACE
|
||||
} // namespace mozilla
|
||||
|
||||
|
@ -8,6 +8,8 @@
|
||||
|
||||
#include "ipc/EnumSerializer.h"
|
||||
#include "mozilla/Observer.h"
|
||||
#include "mozilla/TimeStamp.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace hal {
|
||||
@ -58,6 +60,26 @@ enum WakeLockControl {
|
||||
NUM_WAKE_LOCK
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents a workload shared by a group of threads that should be completed
|
||||
* in a target duration each cycle.
|
||||
*
|
||||
* This is created using hal::CreatePerformanceHintSession(). Each cycle, the
|
||||
* actual work duration should be reported using ReportActualWorkDuration(). The
|
||||
* system can then adjust the scheduling accordingly in order to achieve the
|
||||
* target.
|
||||
*/
|
||||
class PerformanceHintSession {
|
||||
public:
|
||||
virtual ~PerformanceHintSession() = default;
|
||||
|
||||
// Updates the session's target work duration for each cycle.
|
||||
virtual void UpdateTargetWorkDuration(TimeDuration aDuration) = 0;
|
||||
|
||||
// Reports the session's actual work duration for a cycle.
|
||||
virtual void ReportActualWorkDuration(TimeDuration aDuration) = 0;
|
||||
};
|
||||
|
||||
} // namespace hal
|
||||
} // namespace mozilla
|
||||
|
||||
|
206
hal/android/AndroidPerformanceHintManager.cpp
Normal file
206
hal/android/AndroidPerformanceHintManager.cpp
Normal file
@ -0,0 +1,206 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "Hal.h"
|
||||
#include "HalLog.h"
|
||||
#include "HalTypes.h"
|
||||
|
||||
#include "AndroidBuild.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct APerformanceHintManager APerformanceHintManager;
|
||||
typedef struct APerformanceHintSession APerformanceHintSession;
|
||||
|
||||
namespace mozilla {
|
||||
namespace hal_impl {
|
||||
|
||||
#define LOAD_FN(api, lib, name) \
|
||||
do { \
|
||||
api->m##name = reinterpret_cast<Fn##name>(dlsym(handle, #name)); \
|
||||
if (!api->m##name) { \
|
||||
HAL_ERR("Failed to load %s", #name); \
|
||||
return nullptr; \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
class PerformanceHintManagerApi final {
|
||||
public:
|
||||
static PerformanceHintManagerApi* Get() {
|
||||
// C++ guarantees local static variable initialization is thread safe
|
||||
static UniquePtr<PerformanceHintManagerApi> api = Create();
|
||||
return api.get();
|
||||
}
|
||||
|
||||
APerformanceHintManager* APerformanceHint_getManager() const {
|
||||
return mAPerformanceHint_getManager();
|
||||
}
|
||||
|
||||
APerformanceHintSession* APerformanceHint_createSession(
|
||||
APerformanceHintManager* manager, const int32_t* threadIds, size_t size,
|
||||
int64_t initialTargetWorkDurationNanos) const {
|
||||
return mAPerformanceHint_createSession(manager, threadIds, size,
|
||||
initialTargetWorkDurationNanos);
|
||||
}
|
||||
|
||||
int APerformanceHint_updateTargetWorkDuration(
|
||||
APerformanceHintSession* session, int64_t targetDurationNanos) const {
|
||||
return mAPerformanceHint_updateTargetWorkDuration(session,
|
||||
targetDurationNanos);
|
||||
}
|
||||
|
||||
int APerformanceHint_reportActualWorkDuration(
|
||||
APerformanceHintSession* session, int64_t actualDurationNanos) const {
|
||||
return mAPerformanceHint_reportActualWorkDuration(session,
|
||||
actualDurationNanos);
|
||||
}
|
||||
|
||||
void APerformanceHint_closeSession(APerformanceHintSession* session) const {
|
||||
mAPerformanceHint_closeSession(session);
|
||||
}
|
||||
|
||||
private:
|
||||
PerformanceHintManagerApi() = default;
|
||||
|
||||
static UniquePtr<PerformanceHintManagerApi> Create() {
|
||||
if (mozilla::jni::GetAPIVersion() < __ANDROID_API_T__) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* const handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
|
||||
if (!handle) {
|
||||
HAL_ERR("Failed to open libandroid.so");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto api = WrapUnique(new PerformanceHintManagerApi());
|
||||
LOAD_FN(api, handle, APerformanceHint_getManager);
|
||||
LOAD_FN(api, handle, APerformanceHint_createSession);
|
||||
LOAD_FN(api, handle, APerformanceHint_updateTargetWorkDuration);
|
||||
LOAD_FN(api, handle, APerformanceHint_reportActualWorkDuration);
|
||||
LOAD_FN(api, handle, APerformanceHint_closeSession);
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
using FnAPerformanceHint_getManager = APerformanceHintManager* (*)();
|
||||
using FnAPerformanceHint_createSession =
|
||||
APerformanceHintSession* (*)(APerformanceHintManager* manager,
|
||||
const int32_t* threadIds, size_t size,
|
||||
int64_t initialTargetWorkDurationNanos);
|
||||
using FnAPerformanceHint_updateTargetWorkDuration =
|
||||
int (*)(APerformanceHintSession* session, int64_t targetDurationNanos);
|
||||
using FnAPerformanceHint_reportActualWorkDuration =
|
||||
int (*)(APerformanceHintSession* session, int64_t actualDurationNanos);
|
||||
using FnAPerformanceHint_closeSession =
|
||||
void (*)(APerformanceHintSession* session);
|
||||
|
||||
FnAPerformanceHint_getManager mAPerformanceHint_getManager = nullptr;
|
||||
FnAPerformanceHint_createSession mAPerformanceHint_createSession = nullptr;
|
||||
FnAPerformanceHint_updateTargetWorkDuration
|
||||
mAPerformanceHint_updateTargetWorkDuration = nullptr;
|
||||
FnAPerformanceHint_reportActualWorkDuration
|
||||
mAPerformanceHint_reportActualWorkDuration = nullptr;
|
||||
FnAPerformanceHint_closeSession mAPerformanceHint_closeSession = nullptr;
|
||||
};
|
||||
|
||||
class AndroidPerformanceHintSession final : public hal::PerformanceHintSession {
|
||||
public:
|
||||
// Creates a PerformanceHintSession wrapping the provided NDK
|
||||
// APerformanceHintSession instance. This assumes ownership of aSession,
|
||||
// therefore the caller must not close the session itself.
|
||||
explicit AndroidPerformanceHintSession(APerformanceHintSession* aSession)
|
||||
: mSession(aSession) {}
|
||||
|
||||
AndroidPerformanceHintSession(AndroidPerformanceHintSession& aOther) = delete;
|
||||
AndroidPerformanceHintSession(AndroidPerformanceHintSession&& aOther) {
|
||||
mSession = aOther.mSession;
|
||||
aOther.mSession = nullptr;
|
||||
}
|
||||
|
||||
~AndroidPerformanceHintSession() {
|
||||
if (mSession) {
|
||||
PerformanceHintManagerApi::Get()->APerformanceHint_closeSession(mSession);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateTargetWorkDuration(TimeDuration aDuration) override {
|
||||
PerformanceHintManagerApi::Get()->APerformanceHint_updateTargetWorkDuration(
|
||||
mSession, aDuration.ToMicroseconds() * 1000);
|
||||
}
|
||||
|
||||
void ReportActualWorkDuration(TimeDuration aDuration) override {
|
||||
PerformanceHintManagerApi::Get()->APerformanceHint_reportActualWorkDuration(
|
||||
mSession, aDuration.ToMicroseconds() * 1000);
|
||||
}
|
||||
|
||||
private:
|
||||
APerformanceHintSession* mSession;
|
||||
};
|
||||
|
||||
static APerformanceHintManager* InitManager() {
|
||||
const auto* api = PerformanceHintManagerApi::Get();
|
||||
if (!api) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// At the time of writing we are only aware of PerformanceHintManager being
|
||||
// implemented on Tensor devices (Pixel 6 and 7 families). On most devices
|
||||
// createSession() will simply return null. However, on some devices
|
||||
// createSession() does return a session but scheduling does not appear to be
|
||||
// affected in any way. Rather than pretending to the caller that
|
||||
// PerformanceHintManager is available on such devices, return null allowing
|
||||
// them to use another means of achieving the performance they require.
|
||||
const auto socManufacturer = java::sdk::Build::SOC_MANUFACTURER()->ToString();
|
||||
if (!socManufacturer.EqualsASCII("Google")) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return api->APerformanceHint_getManager();
|
||||
}
|
||||
|
||||
UniquePtr<hal::PerformanceHintSession> CreatePerformanceHintSession(
|
||||
const nsTArray<PlatformThreadHandle>& aThreads,
|
||||
mozilla::TimeDuration aTargetWorkDuration) {
|
||||
#if __ANDROID_API__ < __ANDROID_API_L__
|
||||
// pthread_gettid_np() (used below) didn't exist prior to lollipop. Currently
|
||||
// 32-bit builds use an older NDK minimum version than this, meaning we cannot
|
||||
// use this feature. But that's okay since only the very most recent devices
|
||||
// support PerformanceHintManager, and users will likely be running 64 bit
|
||||
// builds on them. This limitation will be removed when we next update the
|
||||
// minimum version in bug 1820295.
|
||||
return nullptr;
|
||||
#else
|
||||
|
||||
// C++ guarantees local static variable initialization is thread safe
|
||||
static APerformanceHintManager* manager = InitManager();
|
||||
|
||||
if (!manager) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto* api = PerformanceHintManagerApi::Get();
|
||||
|
||||
nsTArray<pid_t> tids(aThreads.Length());
|
||||
std::transform(aThreads.cbegin(), aThreads.cend(), MakeBackInserter(tids),
|
||||
[](pthread_t handle) { return pthread_gettid_np(handle); });
|
||||
|
||||
APerformanceHintSession* session = api->APerformanceHint_createSession(
|
||||
manager, tids.Elements(), tids.Length(),
|
||||
aTargetWorkDuration.ToMicroseconds() * 1000);
|
||||
if (!session) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return MakeUnique<AndroidPerformanceHintSession>(session);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace hal_impl
|
||||
} // namespace mozilla
|
19
hal/fallback/FallbackPerformanceHintManager.cpp
Normal file
19
hal/fallback/FallbackPerformanceHintManager.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set sw=2 ts=8 et ft=cpp : */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "Hal.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace hal_impl {
|
||||
|
||||
UniquePtr<hal::PerformanceHintSession> CreatePerformanceHintSession(
|
||||
const nsTArray<PlatformThreadHandle>& aThreads,
|
||||
mozilla::TimeDuration aTargetWorkDuration) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace hal_impl
|
||||
} // namespace mozilla
|
@ -37,6 +37,7 @@ if CONFIG["MOZ_WIDGET_TOOLKIT"] == "android":
|
||||
"/widget/android",
|
||||
]
|
||||
UNIFIED_SOURCES += [
|
||||
"android/AndroidPerformanceHintManager.cpp",
|
||||
"android/AndroidProcessPriority.cpp",
|
||||
"android/AndroidSensor.cpp",
|
||||
]
|
||||
@ -108,6 +109,7 @@ else:
|
||||
if CONFIG["MOZ_WIDGET_TOOLKIT"] != "android":
|
||||
UNIFIED_SOURCES += [
|
||||
"fallback/FallbackNetwork.cpp",
|
||||
"fallback/FallbackPerformanceHintManager.cpp",
|
||||
]
|
||||
|
||||
if CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
|
||||
|
Loading…
Reference in New Issue
Block a user