blob: 22eab9410c014c0d2699591ff662ec62ff7d50ec [file] [log] [blame]
Bo Liu406c8ab2021-11-10 19:20:40 -05001/*
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
Matt Buckleycc146422023-06-28 19:14:02 +000017 /**
18 * @defgroup APerformanceHint Performance Hint Manager
19 *
20 * APerformanceHint allows apps to create performance hint sessions for groups
21 * of threads, and provide hints to the system about the workload of those threads,
Matt Buckley122a1172024-10-21 12:16:36 -070022 * to help the system more accurately allocate resources for them. It is the NDK
Matt Buckleycc146422023-06-28 19:14:02 +000023 * counterpart to the Java PerformanceHintManager SDK API.
24 *
Matt Buckley122a1172024-10-21 12:16:36 -070025 * This API is intended for periodic workloads, such as frame production. Clients are
26 * expected to create an instance of APerformanceHintManager, create a session with
27 * that, and then set a target duration for the session. Then, they can report the actual
28 * work duration at the end of each cycle to inform the framework about how long those
29 * workloads are taking. The framework will then compare the actual durations to the target
30 * duration and attempt to help the client reach a steady state under the target.
31 *
32 * Unlike reportActualWorkDuration, the "notify..." hints are intended to be sent in
33 * advance of large changes in the workload, to prevent them from going over the target
34 * when there is a sudden, unforseen change. Their effects are intended to last for only
35 * one cycle, after which reportActualWorkDuration will have a chance to catch up.
36 * These hints should be used judiciously, only in cases where the workload is changing
37 * substantially. To enforce that, they are tracked using a per-app rate limiter to avoid
38 * excessive hinting and encourage clients to be mindful about when to send them.
Matt Buckleycc146422023-06-28 19:14:02 +000039 * @{
40 */
41
42/**
43 * @file performance_hint.h
44 * @brief API for creating and managing a hint session.
45 */
46
47
Bo Liu406c8ab2021-11-10 19:20:40 -050048#ifndef ANDROID_NATIVE_PERFORMANCE_HINT_H
49#define ANDROID_NATIVE_PERFORMANCE_HINT_H
50
51#include <sys/cdefs.h>
Matt Buckleyfeb30df2024-11-17 01:56:15 +000052#include <jni.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050053
54/******************************************************************
55 *
56 * IMPORTANT NOTICE:
57 *
58 * This file is part of Android's set of stable system headers
59 * exposed by the Android NDK (Native Development Kit).
60 *
61 * Third-party source AND binary code relies on the definitions
62 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
63 *
64 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
65 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
66 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
67 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
68 */
69
Dan Albert8d4bea12024-08-01 22:31:09 +000070#include <stdbool.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050071#include <stdint.h>
Peiyong Linc1041d42023-01-26 00:51:33 +000072#include <unistd.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050073
74__BEGIN_DECLS
75
76struct APerformanceHintManager;
77struct APerformanceHintSession;
Peiyong Lin81780c42023-10-08 21:11:26 +000078struct AWorkDuration;
79
80/**
81 * {@link AWorkDuration} is an opaque type that represents the breakdown of the
82 * actual workload duration in each component internally.
83 *
84 * A new {@link AWorkDuration} can be obtained using
85 * {@link AWorkDuration_create()}, when the client finishes using
86 * {@link AWorkDuration}, {@link AWorkDuration_release()} must be
87 * called to destroy and free up the resources associated with
88 * {@link AWorkDuration}.
89 *
90 * This file provides a set of functions to allow clients to set the measured
91 * work duration of each component on {@link AWorkDuration}.
92 *
93 * - AWorkDuration_setWorkPeriodStartTimestampNanos()
94 * - AWorkDuration_setActualTotalDurationNanos()
95 * - AWorkDuration_setActualCpuDurationNanos()
96 * - AWorkDuration_setActualGpuDurationNanos()
97 */
98typedef struct AWorkDuration AWorkDuration;
Bo Liu406c8ab2021-11-10 19:20:40 -050099
100/**
101 * An opaque type representing a handle to a performance hint manager.
Bo Liu406c8ab2021-11-10 19:20:40 -0500102 *
Matt Buckleycc146422023-06-28 19:14:02 +0000103 * To use:<ul>
Bo Liu406c8ab2021-11-10 19:20:40 -0500104 * <li>Obtain the performance hint manager instance by calling
105 * {@link APerformanceHint_getManager} function.</li>
106 * <li>Create an {@link APerformanceHintSession} with
107 * {@link APerformanceHint_createSession}.</li>
108 * <li>Get the preferred update rate in nanoseconds with
109 * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li>
110 */
111typedef struct APerformanceHintManager APerformanceHintManager;
112
113/**
114 * An opaque type representing a handle to a performance hint session.
115 * A session can only be acquired from a {@link APerformanceHintManager}
Matt Buckleycc146422023-06-28 19:14:02 +0000116 * with {@link APerformanceHint_createSession}. It must be
Bo Liu406c8ab2021-11-10 19:20:40 -0500117 * freed with {@link APerformanceHint_closeSession} after use.
118 *
119 * A Session represents a group of threads with an inter-related workload such that hints for
120 * their performance should be considered as a unit. The threads in a given session should be
Matt Buckleycc146422023-06-28 19:14:02 +0000121 * long-lived and not created or destroyed dynamically.
Bo Liu406c8ab2021-11-10 19:20:40 -0500122 *
Matt Buckleycc146422023-06-28 19:14:02 +0000123 * The work duration API can be used with periodic workloads to dynamically adjust thread
124 * performance and keep the work on schedule while optimizing the available power budget.
125 * When using the work duration API, the starting target duration should be specified
126 * while creating the session, and can later be adjusted with
127 * {@link APerformanceHint_updateTargetWorkDuration}. While using the work duration
128 * API, the client is expected to call {@link APerformanceHint_reportActualWorkDuration} each
129 * cycle to report the actual time taken to complete to the system.
Bo Liu406c8ab2021-11-10 19:20:40 -0500130 *
Xiang Wangc8681f82024-04-08 12:45:59 -0700131 * Note, methods of {@link APerformanceHintSession_*} are not thread safe so callers must
132 * ensure thread safety.
133 *
Matt Buckleycc146422023-06-28 19:14:02 +0000134 * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)`
Bo Liu406c8ab2021-11-10 19:20:40 -0500135 */
136typedef struct APerformanceHintSession APerformanceHintSession;
137
138/**
139 * Acquire an instance of the performance hint manager.
140 *
Matt Buckleycc146422023-06-28 19:14:02 +0000141 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500142 */
Xiang Wang352ff402024-01-09 13:27:05 -0800143APerformanceHintManager* _Nullable APerformanceHint_getManager()
144 __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500145
146/**
147 * Creates a session for the given set of threads and sets their initial target work
148 * duration.
Matt Buckleycc146422023-06-28 19:14:02 +0000149 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500150 * @param manager The performance hint manager instance.
151 * @param threadIds The list of threads to be associated with this session. They must be part of
Matt Buckleycc146422023-06-28 19:14:02 +0000152 * this process' thread group.
153 * @param size The size of the list of threadIds.
154 * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session.
155 * This must be positive if using the work duration API, or 0 otherwise.
156 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500157 */
Peiyong Lin81780c42023-10-08 21:11:26 +0000158APerformanceHintSession* _Nullable APerformanceHint_createSession(
159 APerformanceHintManager* _Nonnull manager,
160 const int32_t* _Nonnull threadIds, size_t size,
Bo Liu406c8ab2021-11-10 19:20:40 -0500161 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
162
163/**
164 * Get preferred update rate information for this device.
165 *
166 * @param manager The performance hint manager instance.
167 * @return the preferred update rate supported by device software.
168 */
169int64_t APerformanceHint_getPreferredUpdateRateNanos(
Peiyong Lin81780c42023-10-08 21:11:26 +0000170 APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500171
172/**
173 * Updates this session's target duration for each cycle of work.
174 *
175 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000176 * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive.
177 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500178 * EINVAL if targetDurationNanos is not positive.
179 * EPIPE if communication with the system service has failed.
180 */
181int APerformanceHint_updateTargetWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000182 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500183 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
184
185/**
186 * Reports the actual duration for the last cycle of work.
187 *
Matt Buckleycc146422023-06-28 19:14:02 +0000188 * The system will attempt to adjust the scheduling and performance of the
189 * threads within the thread group to bring the actual duration close to the target duration.
Bo Liu406c8ab2021-11-10 19:20:40 -0500190 *
191 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000192 * @param actualDurationNanos The duration of time the thread group took to complete its last
193 * task in nanoseconds. This must be positive.
194 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500195 * EINVAL if actualDurationNanos is not positive.
196 * EPIPE if communication with the system service has failed.
197 */
198int APerformanceHint_reportActualWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000199 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500200 int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
201
202/**
203 * Release the performance hint manager pointer acquired via
204 * {@link APerformanceHint_createSession}.
205 *
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000206 * This cannot be used to close a Java PerformanceHintManager.Session, as its
207 * lifecycle is tied to the object in the SDK.
208 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500209 * @param session The performance hint session instance to release.
210 */
211void APerformanceHint_closeSession(
Peiyong Lin81780c42023-10-08 21:11:26 +0000212 APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500213
Peiyong Linf9c984f2022-11-11 18:28:20 +0000214/**
215 * Set a list of threads to the performance hint session. This operation will replace
216 * the current list of threads with the given list of threads.
217 *
Matt Buckleycc146422023-06-28 19:14:02 +0000218 * @param session The performance hint session instance to update.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000219 * @param threadIds The list of threads to be associated with this session. They must be part of
220 * this app's thread group.
Matt Buckleycc146422023-06-28 19:14:02 +0000221 * @param size The size of the list of threadIds.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000222 * @return 0 on success.
Matt Buckleycc146422023-06-28 19:14:02 +0000223 * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of
224 the thread group.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000225 * EPIPE if communication with the system service has failed.
Xiang Wangf6d21b52023-07-25 17:34:01 -0700226 * EPERM if any thread id doesn't belong to the application.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000227 */
228int APerformanceHint_setThreads(
Peiyong Lin81780c42023-10-08 21:11:26 +0000229 APerformanceHintSession* _Nonnull session,
230 const pid_t* _Nonnull threadIds,
Peiyong Linf9c984f2022-11-11 18:28:20 +0000231 size_t size) __INTRODUCED_IN(__ANDROID_API_U__);
232
Matt Buckleycc146422023-06-28 19:14:02 +0000233/**
234 * This tells the session that these threads can be
235 * safely scheduled to prefer power efficiency over performance.
236 *
237 * @param session The performance hint session instance to update.
238 * @param enabled The flag which sets whether this session will use power-efficient scheduling.
239 * @return 0 on success.
240 * EPIPE if communication with the system service has failed.
241 */
242int APerformanceHint_setPreferPowerEfficiency(
Peiyong Lin81780c42023-10-08 21:11:26 +0000243 APerformanceHintSession* _Nonnull session,
Matt Buckleycc146422023-06-28 19:14:02 +0000244 bool enabled) __INTRODUCED_IN(__ANDROID_API_V__);
245
Peiyong Lin81780c42023-10-08 21:11:26 +0000246/**
247 * Reports the durations for the last cycle of work.
248 *
249 * The system will attempt to adjust the scheduling and performance of the
250 * threads within the thread group to bring the actual duration close to the target duration.
251 *
252 * @param session The {@link APerformanceHintSession} instance to update.
253 * @param workDuration The {@link AWorkDuration} structure of times the thread group took to
254 * complete its last task in nanoseconds breaking down into different components.
255 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000256 * The work period start timestamp and actual total duration must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000257 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000258 * The actual CPU and GPU durations must be greater than or equal to zero, and at least one
259 * of them must be greater than zero. When one of them is equal to zero, it means that type
260 * of work was not measured for this workload.
Peiyong Lin81780c42023-10-08 21:11:26 +0000261 *
262 * @return 0 on success.
Matt Buckley81cc0932024-01-18 19:56:31 +0000263 * EINVAL if any duration is an invalid number.
Peiyong Lin81780c42023-10-08 21:11:26 +0000264 * EPIPE if communication with the system service has failed.
265 */
266int APerformanceHint_reportActualWorkDuration2(
267 APerformanceHintSession* _Nonnull session,
268 AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__);
269
270/**
Matt Buckley122a1172024-10-21 12:16:36 -0700271 * Informs the framework of an upcoming increase in the workload of a graphics pipeline
272 * bound to this session. The user can specify whether the increase is expected to be
273 * on the CPU, GPU, or both.
274 *
275 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
276 * rate limiter.
277 *
278 * @param cpu Indicates if the workload increase is expected to affect the CPU.
279 * @param gpu Indicates if the workload increase is expected to affect the GPU.
280 * @param debugName A required string used to identify this specific hint during
281 * tracing. This debug string will only be held for the duration of the
282 * method, and can be safely discarded after.
283 *
284 * @return 0 on success.
285 * EINVAL if no hints were requested.
286 * EBUSY if the hint was rate limited.
287 * EPIPE if communication with the system service has failed.
288 * ENOTSUP if the hint is not supported.
289 */
290int APerformanceHint_notifyWorkloadIncrease(
291 APerformanceHintSession* _Nonnull session,
292 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
293
294/**
295 * Informs the framework of an upcoming reset in the workload of a graphics pipeline
296 * bound to this session, or the imminent start of a new workload. The user can specify
297 * whether the reset is expected to affect the CPU, GPU, or both.
298 *
299 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
300 * this load tracking.
301 *
302 * @param cpu Indicates if the workload reset is expected to affect the CPU.
303 * @param gpu Indicates if the workload reset is expected to affect the GPU.
304 * @param debugName A required string used to identify this specific hint during
305 * tracing. This debug string will only be held for the duration of the
306 * method, and can be safely discarded after.
307 *
308 * @return 0 on success.
309 * EINVAL if no hints were requested.
310 * EBUSY if the hint was rate limited.
311 * EPIPE if communication with the system service has failed.
312 * ENOTSUP if the hint is not supported.
313 */
314int APerformanceHint_notifyWorkloadReset(
315 APerformanceHintSession* _Nonnull session,
316 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
317
318/**
Peiyong Lin81780c42023-10-08 21:11:26 +0000319 * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should
320 * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources
321 * associated with it.
322 *
323 * @return AWorkDuration on success and nullptr otherwise.
324 */
325AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__);
326
327/**
328 * Destroys {@link AWorkDuration} and free all resources associated to it.
329 *
330 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
331 */
Xiang Wang352ff402024-01-09 13:27:05 -0800332void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration)
333 __INTRODUCED_IN(__ANDROID_API_V__);
Peiyong Lin81780c42023-10-08 21:11:26 +0000334
335/**
336 * Sets the work period start timestamp in nanoseconds.
337 *
338 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
339 * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on
Matt Buckley81cc0932024-01-18 19:56:31 +0000340 * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000341 */
342void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration,
343 int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__);
344
345/**
346 * Sets the actual total work duration in nanoseconds.
347 *
348 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000349 * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must
350 * be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000351 */
352void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
353 int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
354
355/**
356 * Sets the actual CPU work duration in nanoseconds.
357 *
358 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000359 * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000360 * greater than or equal to zero. If it is equal to zero, that means the CPU was not
361 * measured.
Peiyong Lin81780c42023-10-08 21:11:26 +0000362 */
363void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
364 int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
365
366/**
367 * Sets the actual GPU work duration in nanoseconds.
368 *
369 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}.
370 * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000371 * greater than or equal to zero. If it is equal to zero, that means the GPU was not
Peiyong Lin81780c42023-10-08 21:11:26 +0000372 * measured.
373 */
374void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
375 int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
376
Matt Buckleyfeb30df2024-11-17 01:56:15 +0000377/**
378 * Return the APerformanceHintSession wrapped by a Java PerformanceHintManager.Session object.
379 *
380 * The Java session maintains ownership over the wrapped native session, so it cannot be
381 * closed using {@link APerformanceHint_closeSession}.
382 *
383 * @param env The Java environment where the PerformanceHintManager.Session lives.
384 * @param sessionObj The Java Session to unwrap.
385 *
386 * @return A pointer to the APerformanceHintManager that backs the Java Session.
387 */
388APerformanceHintSession* _Nonnull APerformanceHint_borrowSessionFromJava(
389 JNIEnv* _Nonnull env, jobject _Nonnull sessionObj) __INTRODUCED_IN(36);
390
391
Bo Liu406c8ab2021-11-10 19:20:40 -0500392__END_DECLS
393
394#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000395
Peiyong Lin81780c42023-10-08 21:11:26 +0000396/** @} */