blob: 3486e9b1d7e118546338c51643739b84e0ad4794 [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,
22 * to help the system more accurately allocate power for them. It is the NDK
23 * counterpart to the Java PerformanceHintManager SDK API.
24 *
25 * @{
26 */
27
28/**
29 * @file performance_hint.h
30 * @brief API for creating and managing a hint session.
31 */
32
33
Bo Liu406c8ab2021-11-10 19:20:40 -050034#ifndef ANDROID_NATIVE_PERFORMANCE_HINT_H
35#define ANDROID_NATIVE_PERFORMANCE_HINT_H
36
37#include <sys/cdefs.h>
38
39/******************************************************************
40 *
41 * IMPORTANT NOTICE:
42 *
43 * This file is part of Android's set of stable system headers
44 * exposed by the Android NDK (Native Development Kit).
45 *
46 * Third-party source AND binary code relies on the definitions
47 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
48 *
49 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
50 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
51 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
52 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
53 */
54
Dan Albert8d4bea12024-08-01 22:31:09 +000055#include <stdbool.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050056#include <stdint.h>
Peiyong Linc1041d42023-01-26 00:51:33 +000057#include <unistd.h>
Bo Liu406c8ab2021-11-10 19:20:40 -050058
59__BEGIN_DECLS
60
61struct APerformanceHintManager;
62struct APerformanceHintSession;
Peiyong Lin81780c42023-10-08 21:11:26 +000063struct AWorkDuration;
64
65/**
66 * {@link AWorkDuration} is an opaque type that represents the breakdown of the
67 * actual workload duration in each component internally.
68 *
69 * A new {@link AWorkDuration} can be obtained using
70 * {@link AWorkDuration_create()}, when the client finishes using
71 * {@link AWorkDuration}, {@link AWorkDuration_release()} must be
72 * called to destroy and free up the resources associated with
73 * {@link AWorkDuration}.
74 *
75 * This file provides a set of functions to allow clients to set the measured
76 * work duration of each component on {@link AWorkDuration}.
77 *
78 * - AWorkDuration_setWorkPeriodStartTimestampNanos()
79 * - AWorkDuration_setActualTotalDurationNanos()
80 * - AWorkDuration_setActualCpuDurationNanos()
81 * - AWorkDuration_setActualGpuDurationNanos()
82 */
83typedef struct AWorkDuration AWorkDuration;
Bo Liu406c8ab2021-11-10 19:20:40 -050084
85/**
86 * An opaque type representing a handle to a performance hint manager.
Bo Liu406c8ab2021-11-10 19:20:40 -050087 *
Matt Buckleycc146422023-06-28 19:14:02 +000088 * To use:<ul>
Bo Liu406c8ab2021-11-10 19:20:40 -050089 * <li>Obtain the performance hint manager instance by calling
90 * {@link APerformanceHint_getManager} function.</li>
91 * <li>Create an {@link APerformanceHintSession} with
92 * {@link APerformanceHint_createSession}.</li>
93 * <li>Get the preferred update rate in nanoseconds with
94 * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li>
95 */
96typedef struct APerformanceHintManager APerformanceHintManager;
97
98/**
99 * An opaque type representing a handle to a performance hint session.
100 * A session can only be acquired from a {@link APerformanceHintManager}
Matt Buckleycc146422023-06-28 19:14:02 +0000101 * with {@link APerformanceHint_createSession}. It must be
Bo Liu406c8ab2021-11-10 19:20:40 -0500102 * freed with {@link APerformanceHint_closeSession} after use.
103 *
104 * A Session represents a group of threads with an inter-related workload such that hints for
105 * their performance should be considered as a unit. The threads in a given session should be
Matt Buckleycc146422023-06-28 19:14:02 +0000106 * long-lived and not created or destroyed dynamically.
Bo Liu406c8ab2021-11-10 19:20:40 -0500107 *
Matt Buckleycc146422023-06-28 19:14:02 +0000108 * The work duration API can be used with periodic workloads to dynamically adjust thread
109 * performance and keep the work on schedule while optimizing the available power budget.
110 * When using the work duration API, the starting target duration should be specified
111 * while creating the session, and can later be adjusted with
112 * {@link APerformanceHint_updateTargetWorkDuration}. While using the work duration
113 * API, the client is expected to call {@link APerformanceHint_reportActualWorkDuration} each
114 * cycle to report the actual time taken to complete to the system.
Bo Liu406c8ab2021-11-10 19:20:40 -0500115 *
Xiang Wangc8681f82024-04-08 12:45:59 -0700116 * Note, methods of {@link APerformanceHintSession_*} are not thread safe so callers must
117 * ensure thread safety.
118 *
Matt Buckleycc146422023-06-28 19:14:02 +0000119 * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)`
Bo Liu406c8ab2021-11-10 19:20:40 -0500120 */
121typedef struct APerformanceHintSession APerformanceHintSession;
122
123/**
124 * Acquire an instance of the performance hint manager.
125 *
Matt Buckleycc146422023-06-28 19:14:02 +0000126 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500127 */
Xiang Wang352ff402024-01-09 13:27:05 -0800128APerformanceHintManager* _Nullable APerformanceHint_getManager()
129 __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500130
131/**
132 * Creates a session for the given set of threads and sets their initial target work
133 * duration.
Matt Buckleycc146422023-06-28 19:14:02 +0000134 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500135 * @param manager The performance hint manager instance.
136 * @param threadIds The list of threads to be associated with this session. They must be part of
Matt Buckleycc146422023-06-28 19:14:02 +0000137 * this process' thread group.
138 * @param size The size of the list of threadIds.
139 * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session.
140 * This must be positive if using the work duration API, or 0 otherwise.
141 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500142 */
Peiyong Lin81780c42023-10-08 21:11:26 +0000143APerformanceHintSession* _Nullable APerformanceHint_createSession(
144 APerformanceHintManager* _Nonnull manager,
145 const int32_t* _Nonnull threadIds, size_t size,
Bo Liu406c8ab2021-11-10 19:20:40 -0500146 int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
147
148/**
149 * Get preferred update rate information for this device.
150 *
151 * @param manager The performance hint manager instance.
152 * @return the preferred update rate supported by device software.
153 */
154int64_t APerformanceHint_getPreferredUpdateRateNanos(
Peiyong Lin81780c42023-10-08 21:11:26 +0000155 APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500156
157/**
158 * Updates this session's target duration for each cycle of work.
159 *
160 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000161 * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive.
162 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500163 * EINVAL if targetDurationNanos is not positive.
164 * EPIPE if communication with the system service has failed.
165 */
166int APerformanceHint_updateTargetWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000167 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500168 int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
169
170/**
171 * Reports the actual duration for the last cycle of work.
172 *
Matt Buckleycc146422023-06-28 19:14:02 +0000173 * The system will attempt to adjust the scheduling and performance of the
174 * threads within the thread group to bring the actual duration close to the target duration.
Bo Liu406c8ab2021-11-10 19:20:40 -0500175 *
176 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000177 * @param actualDurationNanos The duration of time the thread group took to complete its last
178 * task in nanoseconds. This must be positive.
179 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500180 * EINVAL if actualDurationNanos is not positive.
181 * EPIPE if communication with the system service has failed.
182 */
183int APerformanceHint_reportActualWorkDuration(
Peiyong Lin81780c42023-10-08 21:11:26 +0000184 APerformanceHintSession* _Nonnull session,
Bo Liu406c8ab2021-11-10 19:20:40 -0500185 int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__);
186
187/**
188 * Release the performance hint manager pointer acquired via
189 * {@link APerformanceHint_createSession}.
190 *
191 * @param session The performance hint session instance to release.
192 */
193void APerformanceHint_closeSession(
Peiyong Lin81780c42023-10-08 21:11:26 +0000194 APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__);
Bo Liu406c8ab2021-11-10 19:20:40 -0500195
Peiyong Linf9c984f2022-11-11 18:28:20 +0000196/**
197 * Set a list of threads to the performance hint session. This operation will replace
198 * the current list of threads with the given list of threads.
199 *
Matt Buckleycc146422023-06-28 19:14:02 +0000200 * @param session The performance hint session instance to update.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000201 * @param threadIds The list of threads to be associated with this session. They must be part of
202 * this app's thread group.
Matt Buckleycc146422023-06-28 19:14:02 +0000203 * @param size The size of the list of threadIds.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000204 * @return 0 on success.
Matt Buckleycc146422023-06-28 19:14:02 +0000205 * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of
206 the thread group.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000207 * EPIPE if communication with the system service has failed.
Xiang Wangf6d21b52023-07-25 17:34:01 -0700208 * EPERM if any thread id doesn't belong to the application.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000209 */
210int APerformanceHint_setThreads(
Peiyong Lin81780c42023-10-08 21:11:26 +0000211 APerformanceHintSession* _Nonnull session,
212 const pid_t* _Nonnull threadIds,
Peiyong Linf9c984f2022-11-11 18:28:20 +0000213 size_t size) __INTRODUCED_IN(__ANDROID_API_U__);
214
Matt Buckleycc146422023-06-28 19:14:02 +0000215/**
216 * This tells the session that these threads can be
217 * safely scheduled to prefer power efficiency over performance.
218 *
219 * @param session The performance hint session instance to update.
220 * @param enabled The flag which sets whether this session will use power-efficient scheduling.
221 * @return 0 on success.
222 * EPIPE if communication with the system service has failed.
223 */
224int APerformanceHint_setPreferPowerEfficiency(
Peiyong Lin81780c42023-10-08 21:11:26 +0000225 APerformanceHintSession* _Nonnull session,
Matt Buckleycc146422023-06-28 19:14:02 +0000226 bool enabled) __INTRODUCED_IN(__ANDROID_API_V__);
227
Peiyong Lin81780c42023-10-08 21:11:26 +0000228/**
229 * Reports the durations for the last cycle of work.
230 *
231 * The system will attempt to adjust the scheduling and performance of the
232 * threads within the thread group to bring the actual duration close to the target duration.
233 *
234 * @param session The {@link APerformanceHintSession} instance to update.
235 * @param workDuration The {@link AWorkDuration} structure of times the thread group took to
236 * complete its last task in nanoseconds breaking down into different components.
237 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000238 * The work period start timestamp and actual total duration must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000239 *
Matt Buckley0bbd1772024-01-31 22:10:42 +0000240 * The actual CPU and GPU durations must be greater than or equal to zero, and at least one
241 * of them must be greater than zero. When one of them is equal to zero, it means that type
242 * of work was not measured for this workload.
Peiyong Lin81780c42023-10-08 21:11:26 +0000243 *
244 * @return 0 on success.
Matt Buckley81cc0932024-01-18 19:56:31 +0000245 * EINVAL if any duration is an invalid number.
Peiyong Lin81780c42023-10-08 21:11:26 +0000246 * EPIPE if communication with the system service has failed.
247 */
248int APerformanceHint_reportActualWorkDuration2(
249 APerformanceHintSession* _Nonnull session,
250 AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__);
251
252/**
253 * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should
254 * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources
255 * associated with it.
256 *
257 * @return AWorkDuration on success and nullptr otherwise.
258 */
259AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__);
260
261/**
262 * Destroys {@link AWorkDuration} and free all resources associated to it.
263 *
264 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
265 */
Xiang Wang352ff402024-01-09 13:27:05 -0800266void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration)
267 __INTRODUCED_IN(__ANDROID_API_V__);
Peiyong Lin81780c42023-10-08 21:11:26 +0000268
269/**
270 * Sets the work period start timestamp in nanoseconds.
271 *
272 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
273 * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on
Matt Buckley81cc0932024-01-18 19:56:31 +0000274 * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000275 */
276void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration,
277 int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__);
278
279/**
280 * Sets the actual total work duration in nanoseconds.
281 *
282 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000283 * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must
284 * be greater than zero.
Peiyong Lin81780c42023-10-08 21:11:26 +0000285 */
286void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
287 int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
288
289/**
290 * Sets the actual CPU work duration in nanoseconds.
291 *
292 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}
Matt Buckley81cc0932024-01-18 19:56:31 +0000293 * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000294 * greater than or equal to zero. If it is equal to zero, that means the CPU was not
295 * measured.
Peiyong Lin81780c42023-10-08 21:11:26 +0000296 */
297void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
298 int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
299
300/**
301 * Sets the actual GPU work duration in nanoseconds.
302 *
303 * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}.
304 * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be
Matt Buckley0bbd1772024-01-31 22:10:42 +0000305 * 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 +0000306 * measured.
307 */
308void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration,
309 int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__);
310
Bo Liu406c8ab2021-11-10 19:20:40 -0500311__END_DECLS
312
313#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000314
Peiyong Lin81780c42023-10-08 21:11:26 +0000315/** @} */