blob: c5729444507e5ae462c2606f6df4133959e41f60 [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
Matt Buckley56093a72022-11-07 21:50:50 +000019#include <aidl/android/hardware/power/SessionHint.h>
Matt Buckley423c1b32023-06-28 19:13:42 +000020#include <aidl/android/hardware/power/SessionMode.h>
Matt Buckleye3d5c3a2023-12-01 23:10:32 +000021#include <android-base/stringprintf.h>
Peiyong Lin70de0852023-10-25 21:12:35 +000022#include <android/WorkDuration.h>
Bo Liu44267722021-07-16 17:03:20 -040023#include <android/os/IHintManager.h>
24#include <android/os/IHintSession.h>
Bo Liu2b739bb2021-11-10 19:20:03 -050025#include <android/performance_hint.h>
Matt Buckleye3d5c3a2023-12-01 23:10:32 +000026#include <android/trace.h>
Bo Liu44267722021-07-16 17:03:20 -040027#include <binder/Binder.h>
28#include <binder/IBinder.h>
29#include <binder/IServiceManager.h>
Peiyong Lin70de0852023-10-25 21:12:35 +000030#include <inttypes.h>
Bo Liu44267722021-07-16 17:03:20 -040031#include <performance_hint_private.h>
32#include <utils/SystemClock.h>
33
Matt Buckley56093a72022-11-07 21:50:50 +000034#include <chrono>
Matt Buckleye3d5c3a2023-12-01 23:10:32 +000035#include <set>
Bo Liu2b739bb2021-11-10 19:20:03 -050036#include <utility>
37#include <vector>
38
Bo Liu44267722021-07-16 17:03:20 -040039using namespace android;
40using namespace android::os;
41
Matt Buckley56093a72022-11-07 21:50:50 +000042using namespace std::chrono_literals;
43
44using AidlSessionHint = aidl::android::hardware::power::SessionHint;
Matt Buckley423c1b32023-06-28 19:13:42 +000045using AidlSessionMode = aidl::android::hardware::power::SessionMode;
Matt Buckleye3d5c3a2023-12-01 23:10:32 +000046using android::base::StringPrintf;
Matt Buckley56093a72022-11-07 21:50:50 +000047
Bo Liu44267722021-07-16 17:03:20 -040048struct APerformanceHintSession;
49
Matt Buckley56093a72022-11-07 21:50:50 +000050constexpr int64_t SEND_HINT_TIMEOUT = std::chrono::nanoseconds(100ms).count();
51
Bo Liu44267722021-07-16 17:03:20 -040052struct APerformanceHintManager {
53public:
54 static APerformanceHintManager* getInstance();
55 APerformanceHintManager(sp<IHintManager> service, int64_t preferredRateNanos);
56 APerformanceHintManager() = delete;
57 ~APerformanceHintManager() = default;
58
59 APerformanceHintSession* createSession(const int32_t* threadIds, size_t size,
60 int64_t initialTargetWorkDurationNanos);
61 int64_t getPreferredRateNanos() const;
62
63private:
64 static APerformanceHintManager* create(sp<IHintManager> iHintManager);
65
66 sp<IHintManager> mHintManager;
Bo Liu9acc5582022-02-17 16:47:32 -050067 const sp<IBinder> mToken = sp<BBinder>::make();
Bo Liu44267722021-07-16 17:03:20 -040068 const int64_t mPreferredRateNanos;
69};
70
71struct APerformanceHintSession {
72public:
Peiyong Lin095de762022-11-11 18:28:12 +000073 APerformanceHintSession(sp<IHintManager> hintManager, sp<IHintSession> session,
74 int64_t preferredRateNanos, int64_t targetDurationNanos);
Bo Liu44267722021-07-16 17:03:20 -040075 APerformanceHintSession() = delete;
76 ~APerformanceHintSession();
77
78 int updateTargetWorkDuration(int64_t targetDurationNanos);
79 int reportActualWorkDuration(int64_t actualDurationNanos);
Steven Moreland42a8cce2023-03-03 23:31:17 +000080 int sendHint(SessionHint hint);
Peiyong Lin095de762022-11-11 18:28:12 +000081 int setThreads(const int32_t* threadIds, size_t size);
82 int getThreadIds(int32_t* const threadIds, size_t* size);
Matt Buckley423c1b32023-06-28 19:13:42 +000083 int setPreferPowerEfficiency(bool enabled);
Peiyong Lin70de0852023-10-25 21:12:35 +000084 int reportActualWorkDuration(AWorkDuration* workDuration);
Bo Liu44267722021-07-16 17:03:20 -040085
86private:
87 friend struct APerformanceHintManager;
88
Peiyong Lin70de0852023-10-25 21:12:35 +000089 int reportActualWorkDurationInternal(WorkDuration* workDuration);
90
Peiyong Lin095de762022-11-11 18:28:12 +000091 sp<IHintManager> mHintManager;
Bo Liu44267722021-07-16 17:03:20 -040092 sp<IHintSession> mHintSession;
93 // HAL preferred update rate
94 const int64_t mPreferredRateNanos;
95 // Target duration for choosing update rate
96 int64_t mTargetDurationNanos;
Wei Wang00feb502022-10-18 10:56:59 -070097 // First target hit timestamp
98 int64_t mFirstTargetMetTimestamp;
99 // Last target hit timestamp
100 int64_t mLastTargetMetTimestamp;
Matt Buckley56093a72022-11-07 21:50:50 +0000101 // Last hint reported from sendHint indexed by hint value
102 std::vector<int64_t> mLastHintSentTimestamp;
Bo Liu44267722021-07-16 17:03:20 -0400103 // Cached samples
Peiyong Lin70de0852023-10-25 21:12:35 +0000104 std::vector<WorkDuration> mActualWorkDurations;
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000105 std::string mSessionName;
106 static int32_t sIDCounter;
107 // The most recent set of thread IDs
108 std::vector<int32_t> mLastThreadIDs;
109 // Tracing helpers
110 void traceThreads(std::vector<int32_t>& tids);
111 void tracePowerEfficient(bool powerEfficient);
112 void traceActualDuration(int64_t actualDuration);
113 void traceBatchSize(size_t batchSize);
114 void traceTargetDuration(int64_t targetDuration);
Bo Liu44267722021-07-16 17:03:20 -0400115};
116
117static IHintManager* gIHintManagerForTesting = nullptr;
118static APerformanceHintManager* gHintManagerForTesting = nullptr;
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000119int32_t APerformanceHintSession::sIDCounter = 0;
Bo Liu44267722021-07-16 17:03:20 -0400120
121// ===================================== APerformanceHintManager implementation
122APerformanceHintManager::APerformanceHintManager(sp<IHintManager> manager,
123 int64_t preferredRateNanos)
124 : mHintManager(std::move(manager)), mPreferredRateNanos(preferredRateNanos) {}
125
126APerformanceHintManager* APerformanceHintManager::getInstance() {
127 if (gHintManagerForTesting) return gHintManagerForTesting;
128 if (gIHintManagerForTesting) {
129 APerformanceHintManager* manager = create(gIHintManagerForTesting);
130 gIHintManagerForTesting = nullptr;
131 return manager;
132 }
133 static APerformanceHintManager* instance = create(nullptr);
134 return instance;
135}
136
137APerformanceHintManager* APerformanceHintManager::create(sp<IHintManager> manager) {
138 if (!manager) {
139 manager = interface_cast<IHintManager>(
140 defaultServiceManager()->checkService(String16("performance_hint")));
141 }
142 if (manager == nullptr) {
143 ALOGE("%s: PerformanceHint service is not ready ", __FUNCTION__);
144 return nullptr;
145 }
146 int64_t preferredRateNanos = -1L;
147 binder::Status ret = manager->getHintSessionPreferredRate(&preferredRateNanos);
148 if (!ret.isOk()) {
149 ALOGE("%s: PerformanceHint cannot get preferred rate. %s", __FUNCTION__,
150 ret.exceptionMessage().c_str());
151 return nullptr;
152 }
153 if (preferredRateNanos <= 0) {
Bo Liud6a09602021-07-26 14:48:41 -0400154 preferredRateNanos = -1L;
Bo Liu44267722021-07-16 17:03:20 -0400155 }
156 return new APerformanceHintManager(std::move(manager), preferredRateNanos);
157}
158
159APerformanceHintSession* APerformanceHintManager::createSession(
160 const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) {
Bo Liu44267722021-07-16 17:03:20 -0400161 std::vector<int32_t> tids(threadIds, threadIds + size);
162 sp<IHintSession> session;
163 binder::Status ret =
Bo Liu9acc5582022-02-17 16:47:32 -0500164 mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session);
Bo Liu44267722021-07-16 17:03:20 -0400165 if (!ret.isOk() || !session) {
166 return nullptr;
167 }
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000168 auto out = new APerformanceHintSession(mHintManager, std::move(session), mPreferredRateNanos,
169 initialTargetWorkDurationNanos);
170 out->traceThreads(tids);
171 out->traceTargetDuration(initialTargetWorkDurationNanos);
172 out->tracePowerEfficient(false);
173 return out;
Bo Liu44267722021-07-16 17:03:20 -0400174}
175
176int64_t APerformanceHintManager::getPreferredRateNanos() const {
177 return mPreferredRateNanos;
178}
179
180// ===================================== APerformanceHintSession implementation
181
Peiyong Lin095de762022-11-11 18:28:12 +0000182APerformanceHintSession::APerformanceHintSession(sp<IHintManager> hintManager,
183 sp<IHintSession> session,
Bo Liu44267722021-07-16 17:03:20 -0400184 int64_t preferredRateNanos,
185 int64_t targetDurationNanos)
Peiyong Lin095de762022-11-11 18:28:12 +0000186 : mHintManager(hintManager),
187 mHintSession(std::move(session)),
Bo Liu44267722021-07-16 17:03:20 -0400188 mPreferredRateNanos(preferredRateNanos),
189 mTargetDurationNanos(targetDurationNanos),
Wei Wang00feb502022-10-18 10:56:59 -0700190 mFirstTargetMetTimestamp(0),
Matt Buckley56093a72022-11-07 21:50:50 +0000191 mLastTargetMetTimestamp(0) {
192 const std::vector<AidlSessionHint> sessionHintRange{ndk::enum_range<AidlSessionHint>().begin(),
193 ndk::enum_range<AidlSessionHint>().end()};
194
195 mLastHintSentTimestamp = std::vector<int64_t>(sessionHintRange.size(), 0);
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000196 mSessionName = android::base::StringPrintf("ADPF Session %" PRId32, ++sIDCounter);
Matt Buckley56093a72022-11-07 21:50:50 +0000197}
Bo Liu44267722021-07-16 17:03:20 -0400198
199APerformanceHintSession::~APerformanceHintSession() {
200 binder::Status ret = mHintSession->close();
201 if (!ret.isOk()) {
202 ALOGE("%s: HintSession close failed: %s", __FUNCTION__, ret.exceptionMessage().c_str());
203 }
204}
205
206int APerformanceHintSession::updateTargetWorkDuration(int64_t targetDurationNanos) {
207 if (targetDurationNanos <= 0) {
208 ALOGE("%s: targetDurationNanos must be positive", __FUNCTION__);
209 return EINVAL;
210 }
211 binder::Status ret = mHintSession->updateTargetWorkDuration(targetDurationNanos);
212 if (!ret.isOk()) {
Matt Buckley354cc0a2022-09-28 20:54:46 +0000213 ALOGE("%s: HintSession updateTargetWorkDuration failed: %s", __FUNCTION__,
Bo Liu44267722021-07-16 17:03:20 -0400214 ret.exceptionMessage().c_str());
215 return EPIPE;
216 }
217 mTargetDurationNanos = targetDurationNanos;
218 /**
219 * Most of the workload is target_duration dependent, so now clear the cached samples
220 * as they are most likely obsolete.
221 */
Peiyong Lin70de0852023-10-25 21:12:35 +0000222 mActualWorkDurations.clear();
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000223 traceBatchSize(0);
224 traceTargetDuration(targetDurationNanos);
Wei Wang00feb502022-10-18 10:56:59 -0700225 mFirstTargetMetTimestamp = 0;
226 mLastTargetMetTimestamp = 0;
Bo Liu44267722021-07-16 17:03:20 -0400227 return 0;
228}
229
230int APerformanceHintSession::reportActualWorkDuration(int64_t actualDurationNanos) {
Peiyong Lin70de0852023-10-25 21:12:35 +0000231 WorkDuration workDuration(0, actualDurationNanos, actualDurationNanos, 0);
Bo Liu44267722021-07-16 17:03:20 -0400232
Peiyong Lin70de0852023-10-25 21:12:35 +0000233 return reportActualWorkDurationInternal(&workDuration);
Bo Liu44267722021-07-16 17:03:20 -0400234}
235
Steven Moreland42a8cce2023-03-03 23:31:17 +0000236int APerformanceHintSession::sendHint(SessionHint hint) {
Matt Buckley56093a72022-11-07 21:50:50 +0000237 if (hint < 0 || hint >= static_cast<int32_t>(mLastHintSentTimestamp.size())) {
238 ALOGE("%s: invalid session hint %d", __FUNCTION__, hint);
Matt Buckley354cc0a2022-09-28 20:54:46 +0000239 return EINVAL;
240 }
Matt Buckley56093a72022-11-07 21:50:50 +0000241 int64_t now = elapsedRealtimeNano();
242
243 // Limit sendHint to a pre-detemined rate for safety
244 if (now < (mLastHintSentTimestamp[hint] + SEND_HINT_TIMEOUT)) {
245 return 0;
246 }
Matt Buckley354cc0a2022-09-28 20:54:46 +0000247
248 binder::Status ret = mHintSession->sendHint(hint);
249
250 if (!ret.isOk()) {
251 ALOGE("%s: HintSession sendHint failed: %s", __FUNCTION__, ret.exceptionMessage().c_str());
252 return EPIPE;
253 }
Matt Buckley56093a72022-11-07 21:50:50 +0000254 mLastHintSentTimestamp[hint] = now;
Matt Buckley354cc0a2022-09-28 20:54:46 +0000255 return 0;
256}
257
Peiyong Lin095de762022-11-11 18:28:12 +0000258int APerformanceHintSession::setThreads(const int32_t* threadIds, size_t size) {
259 if (size == 0) {
260 ALOGE("%s: the list of thread ids must not be empty.", __FUNCTION__);
261 return EINVAL;
262 }
263 std::vector<int32_t> tids(threadIds, threadIds + size);
264 binder::Status ret = mHintManager->setHintSessionThreads(mHintSession, tids);
265 if (!ret.isOk()) {
266 ALOGE("%s: failed: %s", __FUNCTION__, ret.exceptionMessage().c_str());
Xiang Wangbee6f162023-07-18 17:58:10 -0700267 if (ret.exceptionCode() == binder::Status::Exception::EX_ILLEGAL_ARGUMENT) {
Peiyong Lin095de762022-11-11 18:28:12 +0000268 return EINVAL;
Xiang Wangbee6f162023-07-18 17:58:10 -0700269 } else if (ret.exceptionCode() == binder::Status::Exception::EX_SECURITY) {
270 return EPERM;
Peiyong Lin095de762022-11-11 18:28:12 +0000271 }
272 return EPIPE;
273 }
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000274
275 traceThreads(tids);
276
Peiyong Lin095de762022-11-11 18:28:12 +0000277 return 0;
278}
279
280int APerformanceHintSession::getThreadIds(int32_t* const threadIds, size_t* size) {
281 std::vector<int32_t> tids;
282 binder::Status ret = mHintManager->getHintSessionThreadIds(mHintSession, &tids);
283 if (!ret.isOk()) {
284 ALOGE("%s: failed: %s", __FUNCTION__, ret.exceptionMessage().c_str());
285 return EPIPE;
286 }
287
288 // When threadIds is nullptr, this is the first call to determine the size
289 // of the thread ids list.
290 if (threadIds == nullptr) {
291 *size = tids.size();
292 return 0;
293 }
294
295 // Second call to return the actual list of thread ids.
296 *size = tids.size();
297 for (size_t i = 0; i < *size; ++i) {
298 threadIds[i] = tids[i];
299 }
300 return 0;
301}
302
Matt Buckley423c1b32023-06-28 19:13:42 +0000303int APerformanceHintSession::setPreferPowerEfficiency(bool enabled) {
304 binder::Status ret =
305 mHintSession->setMode(static_cast<int32_t>(AidlSessionMode::POWER_EFFICIENCY), enabled);
306
307 if (!ret.isOk()) {
308 ALOGE("%s: HintSession setPreferPowerEfficiency failed: %s", __FUNCTION__,
309 ret.exceptionMessage().c_str());
310 return EPIPE;
311 }
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000312 tracePowerEfficient(enabled);
Matt Buckley423c1b32023-06-28 19:13:42 +0000313 return OK;
314}
315
Peiyong Lin70de0852023-10-25 21:12:35 +0000316int APerformanceHintSession::reportActualWorkDuration(AWorkDuration* aWorkDuration) {
317 WorkDuration* workDuration = static_cast<WorkDuration*>(aWorkDuration);
Peiyong Lin70de0852023-10-25 21:12:35 +0000318 return reportActualWorkDurationInternal(workDuration);
319}
320
321int APerformanceHintSession::reportActualWorkDurationInternal(WorkDuration* workDuration) {
322 int64_t actualTotalDurationNanos = workDuration->actualTotalDurationNanos;
323 int64_t now = uptimeNanos();
324 workDuration->timestampNanos = now;
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000325 traceActualDuration(workDuration->actualTotalDurationNanos);
Peiyong Lin70de0852023-10-25 21:12:35 +0000326 mActualWorkDurations.push_back(std::move(*workDuration));
327
328 if (actualTotalDurationNanos >= mTargetDurationNanos) {
329 // Reset timestamps if we are equal or over the target.
330 mFirstTargetMetTimestamp = 0;
331 } else {
332 // Set mFirstTargetMetTimestamp for first time meeting target.
333 if (!mFirstTargetMetTimestamp || !mLastTargetMetTimestamp ||
334 (now - mLastTargetMetTimestamp > 2 * mPreferredRateNanos)) {
335 mFirstTargetMetTimestamp = now;
336 }
337 /**
338 * Rate limit the change if the update is over mPreferredRateNanos since first
339 * meeting target and less than mPreferredRateNanos since last meeting target.
340 */
341 if (now - mFirstTargetMetTimestamp > mPreferredRateNanos &&
342 now - mLastTargetMetTimestamp <= mPreferredRateNanos) {
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000343 traceBatchSize(mActualWorkDurations.size());
Peiyong Lin70de0852023-10-25 21:12:35 +0000344 return 0;
345 }
346 mLastTargetMetTimestamp = now;
347 }
348
349 binder::Status ret = mHintSession->reportActualWorkDuration2(mActualWorkDurations);
350 if (!ret.isOk()) {
351 ALOGE("%s: HintSession reportActualWorkDuration failed: %s", __FUNCTION__,
352 ret.exceptionMessage().c_str());
353 mFirstTargetMetTimestamp = 0;
354 mLastTargetMetTimestamp = 0;
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000355 traceBatchSize(mActualWorkDurations.size());
Peiyong Lin70de0852023-10-25 21:12:35 +0000356 return ret.exceptionCode() == binder::Status::EX_ILLEGAL_ARGUMENT ? EINVAL : EPIPE;
357 }
358 mActualWorkDurations.clear();
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000359 traceBatchSize(0);
Peiyong Lin70de0852023-10-25 21:12:35 +0000360
361 return 0;
362}
Matt Buckleye3d5c3a2023-12-01 23:10:32 +0000363// ===================================== Tracing helpers
364
365void APerformanceHintSession::traceThreads(std::vector<int32_t>& tids) {
366 std::set<int32_t> tidSet{tids.begin(), tids.end()};
367
368 // Disable old TID tracing
369 for (int32_t tid : mLastThreadIDs) {
370 if (!tidSet.count(tid)) {
371 std::string traceName =
372 android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid);
373 ATrace_setCounter(traceName.c_str(), 0);
374 }
375 }
376
377 // Add new TID tracing
378 for (int32_t tid : tids) {
379 std::string traceName =
380 android::base::StringPrintf("%s TID: %" PRId32, mSessionName.c_str(), tid);
381 ATrace_setCounter(traceName.c_str(), 1);
382 }
383
384 mLastThreadIDs = std::move(tids);
385}
386
387void APerformanceHintSession::tracePowerEfficient(bool powerEfficient) {
388 ATrace_setCounter((mSessionName + " power efficiency mode").c_str(), powerEfficient);
389}
390
391void APerformanceHintSession::traceActualDuration(int64_t actualDuration) {
392 ATrace_setCounter((mSessionName + " actual duration").c_str(), actualDuration);
393}
394
395void APerformanceHintSession::traceBatchSize(size_t batchSize) {
396 std::string traceName = StringPrintf("%s batch size", mSessionName.c_str());
397 ATrace_setCounter((mSessionName + " batch size").c_str(), batchSize);
398}
399
400void APerformanceHintSession::traceTargetDuration(int64_t targetDuration) {
401 ATrace_setCounter((mSessionName + " target duration").c_str(), targetDuration);
402}
Peiyong Lin70de0852023-10-25 21:12:35 +0000403
Bo Liu44267722021-07-16 17:03:20 -0400404// ===================================== C API
405APerformanceHintManager* APerformanceHint_getManager() {
406 return APerformanceHintManager::getInstance();
407}
408
Matt Buckley83f77092024-01-18 19:57:29 +0000409#define VALIDATE_PTR(ptr) \
410 LOG_ALWAYS_FATAL_IF(ptr == nullptr, "%s: " #ptr " is nullptr", __FUNCTION__);
411
412#define VALIDATE_INT(value, cmp) \
413 if (!(value cmp)) { \
414 ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \
415 __FUNCTION__, value); \
416 return EINVAL; \
417 }
418
419#define WARN_INT(value, cmp) \
420 if (!(value cmp)) { \
421 ALOGE("%s: Invalid value. Check failed: (" #value " " #cmp ") with value: %" PRIi64, \
422 __FUNCTION__, value); \
423 }
424
Bo Liu44267722021-07-16 17:03:20 -0400425APerformanceHintSession* APerformanceHint_createSession(APerformanceHintManager* manager,
426 const int32_t* threadIds, size_t size,
427 int64_t initialTargetWorkDurationNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000428 VALIDATE_PTR(manager)
429 VALIDATE_PTR(threadIds)
Bo Liu44267722021-07-16 17:03:20 -0400430 return manager->createSession(threadIds, size, initialTargetWorkDurationNanos);
431}
432
433int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) {
Matt Buckley83f77092024-01-18 19:57:29 +0000434 VALIDATE_PTR(manager)
Bo Liu44267722021-07-16 17:03:20 -0400435 return manager->getPreferredRateNanos();
436}
437
438int APerformanceHint_updateTargetWorkDuration(APerformanceHintSession* session,
439 int64_t targetDurationNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000440 VALIDATE_PTR(session)
Bo Liu44267722021-07-16 17:03:20 -0400441 return session->updateTargetWorkDuration(targetDurationNanos);
442}
443
444int APerformanceHint_reportActualWorkDuration(APerformanceHintSession* session,
445 int64_t actualDurationNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000446 VALIDATE_PTR(session)
447 VALIDATE_INT(actualDurationNanos, > 0)
Bo Liu44267722021-07-16 17:03:20 -0400448 return session->reportActualWorkDuration(actualDurationNanos);
449}
450
Bo Liu44267722021-07-16 17:03:20 -0400451void APerformanceHint_closeSession(APerformanceHintSession* session) {
Matt Buckley83f77092024-01-18 19:57:29 +0000452 VALIDATE_PTR(session)
Bo Liu44267722021-07-16 17:03:20 -0400453 delete session;
454}
455
Steven Moreland42a8cce2023-03-03 23:31:17 +0000456int APerformanceHint_sendHint(void* session, SessionHint hint) {
Matt Buckley83f77092024-01-18 19:57:29 +0000457 VALIDATE_PTR(session)
Matt Buckley61726a32022-12-06 23:44:45 +0000458 return reinterpret_cast<APerformanceHintSession*>(session)->sendHint(hint);
459}
460
Peiyong Lin7ed6de32023-01-26 00:52:54 +0000461int APerformanceHint_setThreads(APerformanceHintSession* session, const pid_t* threadIds,
Peiyong Lin095de762022-11-11 18:28:12 +0000462 size_t size) {
Matt Buckley83f77092024-01-18 19:57:29 +0000463 VALIDATE_PTR(session)
464 VALIDATE_PTR(threadIds)
Peiyong Lin095de762022-11-11 18:28:12 +0000465 return session->setThreads(threadIds, size);
466}
467
468int APerformanceHint_getThreadIds(void* aPerformanceHintSession, int32_t* const threadIds,
469 size_t* const size) {
Matt Buckley83f77092024-01-18 19:57:29 +0000470 VALIDATE_PTR(aPerformanceHintSession)
Peiyong Lin095de762022-11-11 18:28:12 +0000471 return static_cast<APerformanceHintSession*>(aPerformanceHintSession)
472 ->getThreadIds(threadIds, size);
473}
474
Matt Buckley423c1b32023-06-28 19:13:42 +0000475int APerformanceHint_setPreferPowerEfficiency(APerformanceHintSession* session, bool enabled) {
Matt Buckley83f77092024-01-18 19:57:29 +0000476 VALIDATE_PTR(session)
Matt Buckley423c1b32023-06-28 19:13:42 +0000477 return session->setPreferPowerEfficiency(enabled);
478}
479
Peiyong Lin70de0852023-10-25 21:12:35 +0000480int APerformanceHint_reportActualWorkDuration2(APerformanceHintSession* session,
Matt Buckley83f77092024-01-18 19:57:29 +0000481 AWorkDuration* workDurationPtr) {
482 VALIDATE_PTR(session)
483 VALIDATE_PTR(workDurationPtr)
484 WorkDuration& workDuration = *static_cast<WorkDuration*>(workDurationPtr);
485 VALIDATE_INT(workDuration.workPeriodStartTimestampNanos, > 0)
486 VALIDATE_INT(workDuration.actualTotalDurationNanos, > 0)
487 VALIDATE_INT(workDuration.actualCpuDurationNanos, > 0)
488 VALIDATE_INT(workDuration.actualGpuDurationNanos, >= 0)
489 return session->reportActualWorkDuration(workDurationPtr);
Peiyong Lin70de0852023-10-25 21:12:35 +0000490}
491
492AWorkDuration* AWorkDuration_create() {
493 WorkDuration* workDuration = new WorkDuration();
494 return static_cast<AWorkDuration*>(workDuration);
495}
496
497void AWorkDuration_release(AWorkDuration* aWorkDuration) {
Matt Buckley83f77092024-01-18 19:57:29 +0000498 VALIDATE_PTR(aWorkDuration)
Peiyong Lin70de0852023-10-25 21:12:35 +0000499 delete aWorkDuration;
500}
501
502void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* aWorkDuration,
503 int64_t workPeriodStartTimestampNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000504 VALIDATE_PTR(aWorkDuration)
505 WARN_INT(workPeriodStartTimestampNanos, > 0)
Peiyong Lin70de0852023-10-25 21:12:35 +0000506 static_cast<WorkDuration*>(aWorkDuration)->workPeriodStartTimestampNanos =
507 workPeriodStartTimestampNanos;
508}
509
510void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* aWorkDuration,
511 int64_t actualTotalDurationNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000512 VALIDATE_PTR(aWorkDuration)
513 WARN_INT(actualTotalDurationNanos, > 0)
Peiyong Lin70de0852023-10-25 21:12:35 +0000514 static_cast<WorkDuration*>(aWorkDuration)->actualTotalDurationNanos = actualTotalDurationNanos;
515}
516
517void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* aWorkDuration,
518 int64_t actualCpuDurationNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000519 VALIDATE_PTR(aWorkDuration)
520 WARN_INT(actualCpuDurationNanos, > 0)
Peiyong Lin70de0852023-10-25 21:12:35 +0000521 static_cast<WorkDuration*>(aWorkDuration)->actualCpuDurationNanos = actualCpuDurationNanos;
522}
523
524void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* aWorkDuration,
525 int64_t actualGpuDurationNanos) {
Matt Buckley83f77092024-01-18 19:57:29 +0000526 VALIDATE_PTR(aWorkDuration)
527 WARN_INT(actualGpuDurationNanos, >= 0)
Peiyong Lin70de0852023-10-25 21:12:35 +0000528 static_cast<WorkDuration*>(aWorkDuration)->actualGpuDurationNanos = actualGpuDurationNanos;
529}
530
Bo Liu44267722021-07-16 17:03:20 -0400531void APerformanceHint_setIHintManagerForTesting(void* iManager) {
532 delete gHintManagerForTesting;
533 gHintManagerForTesting = nullptr;
534 gIHintManagerForTesting = static_cast<IHintManager*>(iManager);
535}