blob: 7863a7dba1a7ca0b61a508dcd487f540b68bf9e4 [file] [log] [blame]
Bo Liu44267722021-07-16 17:03:20 -04001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "perf_hint"
18
Bo Liu44267722021-07-16 17:03:20 -040019#include <android/os/IHintManager.h>
20#include <android/os/IHintSession.h>
Bo Liu2b739bb2021-11-10 19:20:03 -050021#include <android/performance_hint.h>
Bo Liu44267722021-07-16 17:03:20 -040022#include <binder/Binder.h>
23#include <binder/IBinder.h>
24#include <binder/IServiceManager.h>
25#include <performance_hint_private.h>
26#include <utils/SystemClock.h>
27
Bo Liu2b739bb2021-11-10 19:20:03 -050028#include <utility>
29#include <vector>
30
Bo Liu44267722021-07-16 17:03:20 -040031using namespace android;
32using namespace android::os;
33
34struct APerformanceHintSession;
35
36struct APerformanceHintManager {
37public:
38 static APerformanceHintManager* getInstance();
39 APerformanceHintManager(sp<IHintManager> service, int64_t preferredRateNanos);
40 APerformanceHintManager() = delete;
41 ~APerformanceHintManager() = default;
42
43 APerformanceHintSession* createSession(const int32_t* threadIds, size_t size,
44 int64_t initialTargetWorkDurationNanos);
45 int64_t getPreferredRateNanos() const;
46
47private:
48 static APerformanceHintManager* create(sp<IHintManager> iHintManager);
49
50 sp<IHintManager> mHintManager;
Bo Liu9acc5582022-02-17 16:47:32 -050051 const sp<IBinder> mToken = sp<BBinder>::make();
Bo Liu44267722021-07-16 17:03:20 -040052 const int64_t mPreferredRateNanos;
53};
54
55struct APerformanceHintSession {
56public:
57 APerformanceHintSession(sp<IHintSession> session, int64_t preferredRateNanos,
58 int64_t targetDurationNanos);
59 APerformanceHintSession() = delete;
60 ~APerformanceHintSession();
61
62 int updateTargetWorkDuration(int64_t targetDurationNanos);
63 int reportActualWorkDuration(int64_t actualDurationNanos);
Matt Buckley354cc0a2022-09-28 20:54:46 +000064 int sendHint(int32_t hint);
Bo Liu44267722021-07-16 17:03:20 -040065
66private:
67 friend struct APerformanceHintManager;
68
69 sp<IHintSession> mHintSession;
70 // HAL preferred update rate
71 const int64_t mPreferredRateNanos;
72 // Target duration for choosing update rate
73 int64_t mTargetDurationNanos;
74 // Last update timestamp
75 int64_t mLastUpdateTimestamp;
76 // Cached samples
77 std::vector<int64_t> mActualDurationsNanos;
78 std::vector<int64_t> mTimestampsNanos;
79};
80
81static IHintManager* gIHintManagerForTesting = nullptr;
82static APerformanceHintManager* gHintManagerForTesting = nullptr;
83
84// ===================================== APerformanceHintManager implementation
85APerformanceHintManager::APerformanceHintManager(sp<IHintManager> manager,
86 int64_t preferredRateNanos)
87 : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) {}
88
89APerformanceHintManager* APerformanceHintManager::getInstance() {
90 if (gHintManagerForTesting) return gHintManagerForTesting;
91 if (gIHintManagerForTesting) {
92 APerformanceHintManager* manager = create(gIHintManagerForTesting);
93 gIHintManagerForTesting = nullptr;
94 return manager;
95 }
96 static APerformanceHintManager* instance = create(nullptr);
97 return instance;
98}
99
100APerformanceHintManager* APerformanceHintManager::create(sp<IHintManager> manager) {
101 if (!manager) {
102 manager = interface_cast<IHintManager>(
103 defaultServiceManager()->checkService(String16("performance_hint")));
104 }
105 if (manager == nullptr) {
106 ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__);
107 return nullptr;
108 }
109 int64_t preferredRateNanos = -1L;
110 binder::Status ret = manager->getHintSessionPreferredRate(&preferredRateNanos);
111 if (!ret.isOk()) {
112 ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__,
113 ret.exceptionMessage().c_str());
114 return nullptr;
115 }
116 if (preferredRateNanos <= 0) {
Bo Liud6a09602021-07-26 14:48:41 -0400117 preferredRateNanos = -1L;
Bo Liu44267722021-07-16 17:03:20 -0400118 }
119 return new APerformanceHintManager(std::move(manager), preferredRateNanos);
120}
121
122APerformanceHintSession* APerformanceHintManager::createSession(
123 const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) {
Bo Liu44267722021-07-16 17:03:20 -0400124 std::vector<int32_t> tids(threadIds, threadIds + size);
125 sp<IHintSession> session;
126 binder::Status ret =
Bo Liu9acc5582022-02-17 16:47:32 -0500127 mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session);
Bo Liu44267722021-07-16 17:03:20 -0400128 if (!ret.isOk() || !session) {
129 return nullptr;
130 }
131 return new APerformanceHintSession(std::move(session), mPreferredRateNanos,
132 initialTargetWorkDurationNanos);
133}
134
135int64_t APerformanceHintManager::getPreferredRateNanos() const {
136 return mPreferredRateNanos;
137}
138
139// ===================================== APerformanceHintSession implementation
140
141APerformanceHintSession::APerformanceHintSession(sp<IHintSession> session,
142 int64_t preferredRateNanos,
143 int64_t targetDurationNanos)
144 : mHintSession(std::move(session)),
145 mPreferredRateNanos(preferredRateNanos),
146 mTargetDurationNanos(targetDurationNanos),
147 mLastUpdateTimestamp(elapsedRealtimeNano()) {}
148
149APerformanceHintSession::~APerformanceHintSession() {
150 binder::Status ret = mHintSession->close();
151 if (!ret.isOk()) {
152 ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.exceptionMessage().c_str());
153 }
154}
155
156int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) {
157 if (targetDurationNanos <= 0) {
158 ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__);
159 return EINVAL;
160 }
161 binder::Status ret = mHintSession->updateTargetWorkDuration(targetDurationNanos);
162 if (!ret.isOk()) {
Matt Buckley354cc0a2022-09-28 20:54:46 +0000163 ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__,
Bo Liu44267722021-07-16 17:03:20 -0400164 ret.exceptionMessage().c_str());
165 return EPIPE;
166 }
167 mTargetDurationNanos = targetDurationNanos;
168 /**
169 * Most of the workload is target_duration dependent, so now clear the cached samples
170 * as they are most likely obsolete.
171 */
172 mActualDurationsNanos.clear();
173 mTimestampsNanos.clear();
174 mLastUpdateTimestamp = elapsedRealtimeNano();
175 return 0;
176}
177
178int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) {
179 if (actualDurationNanos <= 0) {
180 ALOGE("%s: actualDurationNanos must be positive", __FUNCTION__);
181 return EINVAL;
182 }
183 int64_t now = elapsedRealtimeNano();
184 mActualDurationsNanos.push_back(actualDurationNanos);
185 mTimestampsNanos.push_back(now);
186
187 /**
Wei Wang9f174f42021-11-11 22:56:05 -0800188 * Cache the hint if the hint is not overtime and the mLastUpdateTimestamp is
189 * still in the mPreferredRateNanos duration.
Bo Liu44267722021-07-16 17:03:20 -0400190 */
Wei Wang9f174f42021-11-11 22:56:05 -0800191 if (actualDurationNanos < mTargetDurationNanos &&
192 now - mLastUpdateTimestamp <= mPreferredRateNanos) {
193 return 0;
194 }
Bo Liu44267722021-07-16 17:03:20 -0400195
196 binder::Status ret =
197 mHintSession->reportActualWorkDuration(mActualDurationsNanos, mTimestampsNanos);
198 mActualDurationsNanos.clear();
199 mTimestampsNanos.clear();
200 if (!ret.isOk()) {
201 ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__,
202 ret.exceptionMessage().c_str());
203 return EPIPE;
204 }
205 mLastUpdateTimestamp = now;
206 return 0;
207}
208
Matt Buckley354cc0a2022-09-28 20:54:46 +0000209int APerformanceHintSession::sendHint(int32_t hint) {
210 if (hint < 0) {
211 ALOGE("%s: session hint value must be greater than zero", __FUNCTION__);
212 return EINVAL;
213 }
214
215 binder::Status ret = mHintSession->sendHint(hint);
216
217 if (!ret.isOk()) {
218 ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.exceptionMessage().c_str());
219 return EPIPE;
220 }
221 return 0;
222}
223
Bo Liu44267722021-07-16 17:03:20 -0400224// ===================================== C API
225APerformanceHintManager* APerformanceHint_getManager() {
226 return APerformanceHintManager::getInstance();
227}
228
229APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager,
230 const int32_t* threadIds, size_t size,
231 int64_t initialTargetWorkDurationNanos) {
232 return manager->createSession(threadIds, size, initialTargetWorkDurationNanos);
233}
234
235int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) {
236 return manager->getPreferredRateNanos();
237}
238
239int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session,
240 int64_t targetDurationNanos) {
241 return session->updateTargetWorkDuration(targetDurationNanos);
242}
243
244int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session,
245 int64_t actualDurationNanos) {
246 return session->reportActualWorkDuration(actualDurationNanos);
247}
248
Matt Buckley354cc0a2022-09-28 20:54:46 +0000249int APerformanceHint_sendHint(APerformanceHintSession* session, int32_t hint) {
250 return session->sendHint(hint);
251}
252
Bo Liu44267722021-07-16 17:03:20 -0400253void APerformanceHint_closeSession(APerformanceHintSession* session) {
254 delete session;
255}
256
257void APerformanceHint_setIHintManagerForTesting(void* iManager) {
258 delete gHintManagerForTesting;
259 gHintManagerForTesting = nullptr;
260 gIHintManagerForTesting = static_cast<IHintManager*>(iManager);
261}