blob: 976c7d6fb39c3b11dc12485c939bda4e845555f4 [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>
52
53/******************************************************************
54 *
55 * IMPORTANT NOTICE:
56 *
57 * This file is part of Android's set of stable system headers
58 * exposed by the Android NDK (Native Development Kit).
59 *
60 * Third-party source AND binary code relies on the definitions
61 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
62 *
63 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
64 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
65 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
66 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
67 */
68
Dan Albert8d4bea12024-08-01 22:31:09 +000069#include <stdbool.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050070#include <stdint.h>
Peiyong Linc1041d42023-01-26 00:51:33 +000071#include <unistd.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050072
73__BEGIN_DECLS
74
75struct APerformanceHintManager;
76struct APerformanceHintSession;
Peiyong Lin81780c42023-10-08 21:11:26 +000077struct AWorkDuration;
78
79/**
80 * {@link AWorkDuration} is an opaque type that represents the breakdown of the
81 * actual workload duration in each component internally.
82 *
83 * A new {@link AWorkDuration} can be obtained using
84 * {@link AWorkDuration_create()}, when the client finishes using
85 * {@link AWorkDuration}, {@link AWorkDuration_release()} must be
86 * called to destroy and free up the resources associated with
87 * {@link AWorkDuration}.
88 *
89 * This file provides a set of functions to allow clients to set the measured
90 * work duration of each component on {@link AWorkDuration}.
91 *
92 * - AWorkDuration_setWorkPeriodStartTimestampNanos()
93 * - AWorkDuration_setActualTotalDurationNanos()
94 * - AWorkDuration_setActualCpuDurationNanos()
95 * - AWorkDuration_setActualGpuDurationNanos()
96 */
97typedef struct AWorkDuration AWorkDuration;
Bo Liu406c8ab2021-11-10 19:20:40 -050098
99/**
100 * An opaque type representing a handle to a performance hint manager.
Bo Liu406c8ab2021-11-10 19:20:40 -0500101 *
Matt Buckleycc146422023-06-28 19:14:02 +0000102 * To use:<ul>
Bo Liu406c8ab2021-11-10 19:20:40 -0500103 * <li>Obtain the performance hint manager instance by calling
104 * {@link APerformanceHint_getManager} function.</li>
105 * <li>Create an {@link APerformanceHintSession} with
106 * {@link APerformanceHint_createSession}.</li>
107 * <li>Get the preferred update rate in nanoseconds with
108 * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li>
109 */
110typedef struct APerformanceHintManager APerformanceHintManager;
111
112/**
113 * An opaque type representing a handle to a performance hint session.
114 * A session can only be acquired from a {@link APerformanceHintManager}
Matt Buckleycc146422023-06-28 19:14:02 +0000115 * with {@link APerformanceHint_createSession}. It must be
Bo Liu406c8ab2021-11-10 19:20:40 -0500116 * freed with {@link APerformanceHint_closeSession} after use.
117 *
118 * A Session represents a group of threads with an inter-related workload such that hints for
119 * their performance should be considered as a unit. The threads in a given session should be
Matt Buckleycc146422023-06-28 19:14:02 +0000120 * long-lived and not created or destroyed dynamically.
Bo Liu406c8ab2021-11-10 19:20:40 -0500121 *
Matt Buckleycc146422023-06-28 19:14:02 +0000122 * The work duration API can be used with periodic workloads to dynamically adjust thread
123 * performance and keep the work on schedule while optimizing the available power budget.
124 * When using the work duration API, the starting target duration should be specified
125 * while creating the session, and can later be adjusted with
126 * {@link APerformanceHint_updateTargetWorkDuration}. While using the work duration
127 * API, the client is expected to call {@link APerformanceHint_reportActualWorkDuration} each
128 * cycle to report the actual time taken to complete to the system.
Bo Liu406c8ab2021-11-10 19:20:40 -0500129 *
Xiang Wangc8681f82024-04-08 12:45:59 -0700130 * Note, methods of {@link APerformanceHintSession_*} are not thread safe so callers must
131 * ensure thread safety.
132 *
Matt Buckleycc146422023-06-28 19:14:02 +0000133 * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)`
Bo Liu406c8ab2021-11-10 19:20:40 -0500134 */
135typedef struct APerformanceHintSession APerformanceHintSession;
136
137/**
138 * Acquire an instance of the performance hint manager.
139 *
Matt Buckleycc146422023-06-28 19:14:02 +0000140 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500141 */
Xiang Wang352ff402024-01-09 13:27:05 -0800142APerformanceHintManager* _Nullable APerformanceHint_getManager()
143 __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500144
145/**
146 * Creates a session for the given set of threads and sets their initial target work
147 * duration.
Matt Buckleycc146422023-06-28 19:14:02 +0000148 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500149 * @param manager The performance hint manager instance.
150 * @param threadIds The list of threads to be associated with this session. They must be part of
Matt Buckleycc146422023-06-28 19:14:02 +0000151 * this process' thread group.
152 * @param size The size of the list of threadIds.
153 * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session.
154 * This must be positive if using the work duration API, or 0 otherwise.
155 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500156 */
Peiyong Lin81780c42023-10-08 21:11:26 +0000157APerformanceHintSession* _Nullable APerformanceHint_createSession(
158 APerformanceHintManager* _Nonnull manager,
159 const int32_t* _Nonnull threadIds, size_t size,
Bo Liu406c8ab2021-11-10 19:20:40 -0500160 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
161
162/**
163 * Get preferred update rate information for this device.
164 *
165 * @param manager The performance hint manager instance.
166 * @return the preferred update rate supported by device software.
167 */
168int64_t APerformanceHint_getPreferredUpdateRateNanos(
Peiyong Lin81780c42023-10-08 21:11:26 +0000169 APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500170
171/**
172 * Updates this session's target duration for each cycle of work.
173 *
174 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000175 * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive.
176 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500177 * EINVAL if targetDurationNanos is not positive.
178 * EPIPE if communication with the system service has failed.
179 */
180int APerformanceHint_updateTargetWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000181 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500182 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
183
184/**
185 * Reports the actual duration for the last cycle of work.
186 *
Matt Buckleycc146422023-06-28 19:14:02 +0000187 * The system will attempt to adjust the scheduling and performance of the
188 * threads within the thread group to bring the actual duration close to the target duration.
Bo Liu406c8ab2021-11-10 19:20:40 -0500189 *
190 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000191 * @param actualDurationNanos The duration of time the thread group took to complete its last
192 * task in nanoseconds. This must be positive.
193 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500194 * EINVAL if actualDurationNanos is not positive.
195 * EPIPE if communication with the system service has failed.
196 */
197int APerformanceHint_reportActualWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000198 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500199 int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
200
201/**
202 * Release the performance hint manager pointer acquired via
203 * {@link APerformanceHint_createSession}.
204 *
205 * @param session The performance hint session instance to release.
206 */
207void APerformanceHint_closeSession(
Peiyong Lin81780c42023-10-08 21:11:26 +0000208 APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500209
Peiyong Linf9c984f2022-11-11 18:28:20 +0000210/**
211 * Set a list of threads to the performance hint session. This operation will replace
212 * the current list of threads with the given list of threads.
213 *
Matt Buckleycc146422023-06-28 19:14:02 +0000214 * @param session The performance hint session instance to update.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000215 * @param threadIds The list of threads to be associated with this session. They must be part of
216 * this app's thread group.
Matt Buckleycc146422023-06-28 19:14:02 +0000217 * @param size The size of the list of threadIds.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000218 * @return 0 on success.
Matt Buckleycc146422023-06-28 19:14:02 +0000219 * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of
220 the thread group.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000221 * EPIPE if communication with the system service has failed.
Xiang Wangf6d21b52023-07-25 17:34:01 -0700222 * EPERM if any thread id doesn't belong to the application.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000223 */
224int APerformanceHint_setThreads(
Peiyong Lin81780c42023-10-08 21:11:26 +0000225 APerformanceHintSession* _Nonnull session,
226 const pid_t* _Nonnull threadIds,
Peiyong Linf9c984f2022-11-11 18:28:20 +0000227 size_t size) __INTRODUCED_IN(__ANDROID_API_U__);
228
Matt Buckleycc146422023-06-28 19:14:02 +0000229/**
230 * This tells the session that these threads can be
231 * safely scheduled to prefer power efficiency over performance.
232 *
233 * @param session The performance hint session instance to update.
234 * @param enabled The flag which sets whether this session will use power-efficient scheduling.
235 * @return 0 on success.
236 * EPIPE if communication with the system service has failed.
237 */
238int APerformanceHint_setPreferPowerEfficiency(
Peiyong Lin81780c42023-10-08 21:11:26 +0000239 APerformanceHintSession* _Nonnull session,
Matt Buckleycc146422023-06-28 19:14:02 +0000240 bool enabled) __INTRODUCED_IN(__ANDROID_API_V__);
241
Peiyong Lin81780c42023-10-08 21:11:26 +0000242/**
243 * Reports the durations for the last cycle of work.
244 *
245 * The system will attempt to adjust the scheduling and performance of the
246 * threads within the thread group to bring the actual duration close to the target duration.
247 *
248 * @param session The {@link APerformanceHintSession} instance to update.
249 * @param workDuration The {@link AWorkDuration} structure of times the thread group took to
250 * complete its last task in nanoseconds breaking down into different components.
251 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000252 * The work period start timestamp and actual total duration must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000253 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000254 * The actual CPU and GPU durations must be greater than or equal to zero, and at least one
255 * of them must be greater than zero. When one of them is equal to zero, it means that type
256 * of work was not measured for this workload.
Peiyong Lin81780c42023-10-08 21:11:26 +0000257 *
258 * @return 0 on success.
Matt Buckley81cc0932024-01-18 19:56:31 +0000259 * EINVAL if any duration is an invalid number.
Peiyong Lin81780c42023-10-08 21:11:26 +0000260 * EPIPE if communication with the system service has failed.
261 */
262int APerformanceHint_reportActualWorkDuration2(
263 APerformanceHintSession* _Nonnull session,
264 AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__);
265
266/**
Matt Buckley122a1172024-10-21 12:16:36 -0700267 * Informs the framework of an upcoming increase in the workload of a graphics pipeline
268 * bound to this session. The user can specify whether the increase is expected to be
269 * on the CPU, GPU, or both.
270 *
271 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
272 * rate limiter.
273 *
274 * @param cpu Indicates if the workload increase is expected to affect the CPU.
275 * @param gpu Indicates if the workload increase is expected to affect the GPU.
276 * @param debugName A required string used to identify this specific hint during
277 * tracing. This debug string will only be held for the duration of the
278 * method, and can be safely discarded after.
279 *
280 * @return 0 on success.
281 * EINVAL if no hints were requested.
282 * EBUSY if the hint was rate limited.
283 * EPIPE if communication with the system service has failed.
284 * ENOTSUP if the hint is not supported.
285 */
286int APerformanceHint_notifyWorkloadIncrease(
287 APerformanceHintSession* _Nonnull session,
288 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
289
290/**
291 * Informs the framework of an upcoming reset in the workload of a graphics pipeline
292 * bound to this session, or the imminent start of a new workload. The user can specify
293 * whether the reset is expected to affect the CPU, GPU, or both.
294 *
295 * Sending hints for both CPU and GPU counts as two separate hints for the purposes of the
296 * this load tracking.
297 *
298 * @param cpu Indicates if the workload reset is expected to affect the CPU.
299 * @param gpu Indicates if the workload reset is expected to affect the GPU.
300 * @param debugName A required string used to identify this specific hint during
301 * tracing. This debug string will only be held for the duration of the
302 * method, and can be safely discarded after.
303 *
304 * @return 0 on success.
305 * EINVAL if no hints were requested.
306 * EBUSY if the hint was rate limited.
307 * EPIPE if communication with the system service has failed.
308 * ENOTSUP if the hint is not supported.
309 */
310int APerformanceHint_notifyWorkloadReset(
311 APerformanceHintSession* _Nonnull session,
312 bool cpu, bool gpu, const char* _Nonnull debugName) __INTRODUCED_IN(36);
313
314/**
Peiyong Lin81780c42023-10-08 21:11:26 +0000315 * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should
316 * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources
317 * associated with it.
318 *
319 * @return AWorkDuration on success and nullptr otherwise.
320 */
321AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__);
322
323/**
324 * Destroys {@link AWorkDuration} and free all resources associated to it.
325 *
326 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
327 */
Xiang Wang352ff402024-01-09 13:27:05 -0800328void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration)
329 __INTRODUCED_IN(__ANDROID_API_V__);
Peiyong Lin81780c42023-10-08 21:11:26 +0000330
331/**
332 * Sets the work period start timestamp in nanoseconds.
333 *
334 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
335 * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on
Matt Buckley81cc0932024-01-18 19:56:31 +0000336 * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000337 */
338void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration,
339 int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__);
340
341/**
342 * Sets the actual total work duration in nanoseconds.
343 *
344 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000345 * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must
346 * be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000347 */
348void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
349 int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
350
351/**
352 * Sets the actual CPU work duration in nanoseconds.
353 *
354 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000355 * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000356 * greater than or equal to zero. If it is equal to zero, that means the CPU was not
357 * measured.
Peiyong Lin81780c42023-10-08 21:11:26 +0000358 */
359void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
360 int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
361
362/**
363 * Sets the actual GPU work duration in nanoseconds.
364 *
365 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}.
366 * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000367 * 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 +0000368 * measured.
369 */
370void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
371 int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
372
Bo Liu406c8ab2021-11-10 19:20:40 -0500373__END_DECLS
374
375#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000376
Peiyong Lin81780c42023-10-08 21:11:26 +0000377/** @} */