Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 1 | /* |
| 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 Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 17 | /** |
| 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 Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 34 | #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 | |
| 55 | #include <android/api-level.h> |
Dan Albert | 8d4bea1 | 2024-08-01 22:31:09 +0000 | [diff] [blame] | 56 | #include <stdbool.h> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 57 | #include <stdint.h> |
Peiyong Lin | c1041d4 | 2023-01-26 00:51:33 +0000 | [diff] [blame] | 58 | #include <unistd.h> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 59 | |
| 60 | __BEGIN_DECLS |
| 61 | |
| 62 | struct APerformanceHintManager; |
| 63 | struct APerformanceHintSession; |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 64 | struct AWorkDuration; |
| 65 | |
| 66 | /** |
| 67 | * {@link AWorkDuration} is an opaque type that represents the breakdown of the |
| 68 | * actual workload duration in each component internally. |
| 69 | * |
| 70 | * A new {@link AWorkDuration} can be obtained using |
| 71 | * {@link AWorkDuration_create()}, when the client finishes using |
| 72 | * {@link AWorkDuration}, {@link AWorkDuration_release()} must be |
| 73 | * called to destroy and free up the resources associated with |
| 74 | * {@link AWorkDuration}. |
| 75 | * |
| 76 | * This file provides a set of functions to allow clients to set the measured |
| 77 | * work duration of each component on {@link AWorkDuration}. |
| 78 | * |
| 79 | * - AWorkDuration_setWorkPeriodStartTimestampNanos() |
| 80 | * - AWorkDuration_setActualTotalDurationNanos() |
| 81 | * - AWorkDuration_setActualCpuDurationNanos() |
| 82 | * - AWorkDuration_setActualGpuDurationNanos() |
| 83 | */ |
| 84 | typedef struct AWorkDuration AWorkDuration; |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * An opaque type representing a handle to a performance hint manager. |
| 88 | * It must be released after use. |
| 89 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 90 | * To use:<ul> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 91 | * <li>Obtain the performance hint manager instance by calling |
| 92 | * {@link APerformanceHint_getManager} function.</li> |
| 93 | * <li>Create an {@link APerformanceHintSession} with |
| 94 | * {@link APerformanceHint_createSession}.</li> |
| 95 | * <li>Get the preferred update rate in nanoseconds with |
| 96 | * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li> |
| 97 | */ |
| 98 | typedef struct APerformanceHintManager APerformanceHintManager; |
| 99 | |
| 100 | /** |
| 101 | * An opaque type representing a handle to a performance hint session. |
| 102 | * A session can only be acquired from a {@link APerformanceHintManager} |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 103 | * with {@link APerformanceHint_createSession}. It must be |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 104 | * freed with {@link APerformanceHint_closeSession} after use. |
| 105 | * |
| 106 | * A Session represents a group of threads with an inter-related workload such that hints for |
| 107 | * their performance should be considered as a unit. The threads in a given session should be |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 108 | * long-lived and not created or destroyed dynamically. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 109 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 110 | * The work duration API can be used with periodic workloads to dynamically adjust thread |
| 111 | * performance and keep the work on schedule while optimizing the available power budget. |
| 112 | * When using the work duration API, the starting target duration should be specified |
| 113 | * while creating the session, and can later be adjusted with |
| 114 | * {@link APerformanceHint_updateTargetWorkDuration}. While using the work duration |
| 115 | * API, the client is expected to call {@link APerformanceHint_reportActualWorkDuration} each |
| 116 | * cycle to report the actual time taken to complete to the system. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 117 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 118 | * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)` |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 119 | */ |
| 120 | typedef struct APerformanceHintSession APerformanceHintSession; |
| 121 | |
| 122 | /** |
| 123 | * Acquire an instance of the performance hint manager. |
| 124 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 125 | * @return APerformanceHintManager instance on success, nullptr on failure. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 126 | */ |
Xiang Wang | 352ff40 | 2024-01-09 13:27:05 -0800 | [diff] [blame] | 127 | APerformanceHintManager* _Nullable APerformanceHint_getManager() |
| 128 | __INTRODUCED_IN(__ANDROID_API_T__); |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Creates a session for the given set of threads and sets their initial target work |
| 132 | * duration. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 133 | * |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 134 | * @param manager The performance hint manager instance. |
| 135 | * @param threadIds The list of threads to be associated with this session. They must be part of |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 136 | * this process' thread group. |
| 137 | * @param size The size of the list of threadIds. |
| 138 | * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session. |
| 139 | * This must be positive if using the work duration API, or 0 otherwise. |
| 140 | * @return APerformanceHintManager instance on success, nullptr on failure. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 141 | */ |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 142 | APerformanceHintSession* _Nullable APerformanceHint_createSession( |
| 143 | APerformanceHintManager* _Nonnull manager, |
| 144 | const int32_t* _Nonnull threadIds, size_t size, |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 145 | int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 146 | |
| 147 | /** |
| 148 | * Get preferred update rate information for this device. |
| 149 | * |
| 150 | * @param manager The performance hint manager instance. |
| 151 | * @return the preferred update rate supported by device software. |
| 152 | */ |
| 153 | int64_t APerformanceHint_getPreferredUpdateRateNanos( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 154 | APerformanceHintManager* _Nonnull manager) __INTRODUCED_IN(__ANDROID_API_T__); |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Updates this session's target duration for each cycle of work. |
| 158 | * |
| 159 | * @param session The performance hint session instance to update. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 160 | * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive. |
| 161 | * @return 0 on success. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 162 | * EINVAL if targetDurationNanos is not positive. |
| 163 | * EPIPE if communication with the system service has failed. |
| 164 | */ |
| 165 | int APerformanceHint_updateTargetWorkDuration( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 166 | APerformanceHintSession* _Nonnull session, |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 167 | int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 168 | |
| 169 | /** |
| 170 | * Reports the actual duration for the last cycle of work. |
| 171 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 172 | * The system will attempt to adjust the scheduling and performance of the |
| 173 | * threads within the thread group to bring the actual duration close to the target duration. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 174 | * |
| 175 | * @param session The performance hint session instance to update. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 176 | * @param actualDurationNanos The duration of time the thread group took to complete its last |
| 177 | * task in nanoseconds. This must be positive. |
| 178 | * @return 0 on success. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 179 | * EINVAL if actualDurationNanos is not positive. |
| 180 | * EPIPE if communication with the system service has failed. |
| 181 | */ |
| 182 | int APerformanceHint_reportActualWorkDuration( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 183 | APerformanceHintSession* _Nonnull session, |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 184 | int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 185 | |
| 186 | /** |
| 187 | * Release the performance hint manager pointer acquired via |
| 188 | * {@link APerformanceHint_createSession}. |
| 189 | * |
| 190 | * @param session The performance hint session instance to release. |
| 191 | */ |
| 192 | void APerformanceHint_closeSession( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 193 | APerformanceHintSession* _Nonnull session) __INTRODUCED_IN(__ANDROID_API_T__); |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 194 | |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 195 | /** |
| 196 | * Set a list of threads to the performance hint session. This operation will replace |
| 197 | * the current list of threads with the given list of threads. |
| 198 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 199 | * @param session The performance hint session instance to update. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 200 | * @param threadIds The list of threads to be associated with this session. They must be part of |
| 201 | * this app's thread group. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 202 | * @param size The size of the list of threadIds. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 203 | * @return 0 on success. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 204 | * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of |
| 205 | the thread group. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 206 | * EPIPE if communication with the system service has failed. |
Xiang Wang | f6d21b5 | 2023-07-25 17:34:01 -0700 | [diff] [blame] | 207 | * EPERM if any thread id doesn't belong to the application. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 208 | */ |
| 209 | int APerformanceHint_setThreads( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 210 | APerformanceHintSession* _Nonnull session, |
| 211 | const pid_t* _Nonnull threadIds, |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 212 | size_t size) __INTRODUCED_IN(__ANDROID_API_U__); |
| 213 | |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 214 | /** |
| 215 | * This tells the session that these threads can be |
| 216 | * safely scheduled to prefer power efficiency over performance. |
| 217 | * |
| 218 | * @param session The performance hint session instance to update. |
| 219 | * @param enabled The flag which sets whether this session will use power-efficient scheduling. |
| 220 | * @return 0 on success. |
| 221 | * EPIPE if communication with the system service has failed. |
| 222 | */ |
| 223 | int APerformanceHint_setPreferPowerEfficiency( |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 224 | APerformanceHintSession* _Nonnull session, |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 225 | bool enabled) __INTRODUCED_IN(__ANDROID_API_V__); |
| 226 | |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Reports the durations for the last cycle of work. |
| 229 | * |
| 230 | * The system will attempt to adjust the scheduling and performance of the |
| 231 | * threads within the thread group to bring the actual duration close to the target duration. |
| 232 | * |
| 233 | * @param session The {@link APerformanceHintSession} instance to update. |
| 234 | * @param workDuration The {@link AWorkDuration} structure of times the thread group took to |
| 235 | * complete its last task in nanoseconds breaking down into different components. |
| 236 | * |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 237 | * The work period start timestamp and actual total duration must be greater than zero. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 238 | * |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 239 | * The actual CPU and GPU durations must be greater than or equal to zero, and at least one |
| 240 | * of them must be greater than zero. When one of them is equal to zero, it means that type |
| 241 | * of work was not measured for this workload. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 242 | * |
| 243 | * @return 0 on success. |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 244 | * EINVAL if any duration is an invalid number. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 245 | * EPIPE if communication with the system service has failed. |
| 246 | */ |
| 247 | int APerformanceHint_reportActualWorkDuration2( |
| 248 | APerformanceHintSession* _Nonnull session, |
| 249 | AWorkDuration* _Nonnull workDuration) __INTRODUCED_IN(__ANDROID_API_V__); |
| 250 | |
| 251 | /** |
| 252 | * Creates a new AWorkDuration. When the client finishes using {@link AWorkDuration}, it should |
| 253 | * call {@link AWorkDuration_release()} to destroy {@link AWorkDuration} and release all resources |
| 254 | * associated with it. |
| 255 | * |
| 256 | * @return AWorkDuration on success and nullptr otherwise. |
| 257 | */ |
| 258 | AWorkDuration* _Nonnull AWorkDuration_create() __INTRODUCED_IN(__ANDROID_API_V__); |
| 259 | |
| 260 | /** |
| 261 | * Destroys {@link AWorkDuration} and free all resources associated to it. |
| 262 | * |
| 263 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
| 264 | */ |
Xiang Wang | 352ff40 | 2024-01-09 13:27:05 -0800 | [diff] [blame] | 265 | void AWorkDuration_release(AWorkDuration* _Nonnull aWorkDuration) |
| 266 | __INTRODUCED_IN(__ANDROID_API_V__); |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 267 | |
| 268 | /** |
| 269 | * Sets the work period start timestamp in nanoseconds. |
| 270 | * |
| 271 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
| 272 | * @param workPeriodStartTimestampNanos The work period start timestamp in nanoseconds based on |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 273 | * CLOCK_MONOTONIC about when the work starts. This timestamp must be greater than zero. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 274 | */ |
| 275 | void AWorkDuration_setWorkPeriodStartTimestampNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 276 | int64_t workPeriodStartTimestampNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 277 | |
| 278 | /** |
| 279 | * Sets the actual total work duration in nanoseconds. |
| 280 | * |
| 281 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 282 | * @param actualTotalDurationNanos The actual total work duration in nanoseconds. This number must |
| 283 | * be greater than zero. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 284 | */ |
| 285 | void AWorkDuration_setActualTotalDurationNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 286 | int64_t actualTotalDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 287 | |
| 288 | /** |
| 289 | * Sets the actual CPU work duration in nanoseconds. |
| 290 | * |
| 291 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()} |
Matt Buckley | 81cc093 | 2024-01-18 19:56:31 +0000 | [diff] [blame] | 292 | * @param actualCpuDurationNanos The actual CPU work duration in nanoseconds. This number must be |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 293 | * greater than or equal to zero. If it is equal to zero, that means the CPU was not |
| 294 | * measured. |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 295 | */ |
| 296 | void AWorkDuration_setActualCpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 297 | int64_t actualCpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 298 | |
| 299 | /** |
| 300 | * Sets the actual GPU work duration in nanoseconds. |
| 301 | * |
| 302 | * @param aWorkDuration The {@link AWorkDuration} created by calling {@link AWorkDuration_create()}. |
| 303 | * @param actualGpuDurationNanos The actual GPU work duration in nanoseconds, the number must be |
Matt Buckley | 0bbd177 | 2024-01-31 22:10:42 +0000 | [diff] [blame] | 304 | * greater than or equal to zero. If it is equal to zero, that means the GPU was not |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 305 | * measured. |
| 306 | */ |
| 307 | void AWorkDuration_setActualGpuDurationNanos(AWorkDuration* _Nonnull aWorkDuration, |
| 308 | int64_t actualGpuDurationNanos) __INTRODUCED_IN(__ANDROID_API_V__); |
| 309 | |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 310 | __END_DECLS |
| 311 | |
| 312 | #endif // ANDROID_NATIVE_PERFORMANCE_HINT_H |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame] | 313 | |
Peiyong Lin | 81780c4 | 2023-10-08 21:11:26 +0000 | [diff] [blame] | 314 | /** @} */ |