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