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; |
| 63 | |
| 64 | /** |
| 65 | * An opaque type representing a handle to a performance hint manager. |
| 66 | * It must be released after use. |
| 67 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 68 | * To use:<ul> |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 69 | * <li>Obtain the performance hint manager instance by calling |
| 70 | * {@link APerformanceHint_getManager} function.</li> |
| 71 | * <li>Create an {@link APerformanceHintSession} with |
| 72 | * {@link APerformanceHint_createSession}.</li> |
| 73 | * <li>Get the preferred update rate in nanoseconds with |
| 74 | * {@link APerformanceHint_getPreferredUpdateRateNanos}.</li> |
| 75 | */ |
| 76 | typedef struct APerformanceHintManager APerformanceHintManager; |
| 77 | |
| 78 | /** |
| 79 | * An opaque type representing a handle to a performance hint session. |
| 80 | * A session can only be acquired from a {@link APerformanceHintManager} |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 81 | * with {@link APerformanceHint_createSession}. It must be |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 82 | * freed with {@link APerformanceHint_closeSession} after use. |
| 83 | * |
| 84 | * A Session represents a group of threads with an inter-related workload such that hints for |
| 85 | * 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^] | 86 | * long-lived and not created or destroyed dynamically. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 87 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 88 | * The work duration API can be used with periodic workloads to dynamically adjust thread |
| 89 | * performance and keep the work on schedule while optimizing the available power budget. |
| 90 | * When using the work duration API, the starting target duration should be specified |
| 91 | * while creating the session, and can later be adjusted with |
| 92 | * {@link APerformanceHint_updateTargetWorkDuration}. While using the work duration |
| 93 | * API, the client is expected to call {@link APerformanceHint_reportActualWorkDuration} each |
| 94 | * cycle to report the actual time taken to complete to the system. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 95 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 96 | * 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] | 97 | */ |
| 98 | typedef struct APerformanceHintSession APerformanceHintSession; |
| 99 | |
| 100 | /** |
| 101 | * Acquire an instance of the performance hint manager. |
| 102 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 103 | * @return APerformanceHintManager instance on success, nullptr on failure. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 104 | */ |
| 105 | APerformanceHintManager* APerformanceHint_getManager() __INTRODUCED_IN(__ANDROID_API_T__); |
| 106 | |
| 107 | /** |
| 108 | * Creates a session for the given set of threads and sets their initial target work |
| 109 | * duration. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 110 | * |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 111 | * @param manager The performance hint manager instance. |
| 112 | * @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^] | 113 | * this process' thread group. |
| 114 | * @param size The size of the list of threadIds. |
| 115 | * @param initialTargetWorkDurationNanos The target duration in nanoseconds for the new session. |
| 116 | * This must be positive if using the work duration API, or 0 otherwise. |
| 117 | * @return APerformanceHintManager instance on success, nullptr on failure. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 118 | */ |
| 119 | APerformanceHintSession* APerformanceHint_createSession( |
| 120 | APerformanceHintManager* manager, |
| 121 | const int32_t* threadIds, size_t size, |
| 122 | int64_t initialTargetWorkDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 123 | |
| 124 | /** |
| 125 | * Get preferred update rate information for this device. |
| 126 | * |
| 127 | * @param manager The performance hint manager instance. |
| 128 | * @return the preferred update rate supported by device software. |
| 129 | */ |
| 130 | int64_t APerformanceHint_getPreferredUpdateRateNanos( |
| 131 | APerformanceHintManager* manager) __INTRODUCED_IN(__ANDROID_API_T__); |
| 132 | |
| 133 | /** |
| 134 | * Updates this session's target duration for each cycle of work. |
| 135 | * |
| 136 | * @param session The performance hint session instance to update. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 137 | * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive. |
| 138 | * @return 0 on success. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 139 | * EINVAL if targetDurationNanos is not positive. |
| 140 | * EPIPE if communication with the system service has failed. |
| 141 | */ |
| 142 | int APerformanceHint_updateTargetWorkDuration( |
| 143 | APerformanceHintSession* session, |
| 144 | int64_t targetDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 145 | |
| 146 | /** |
| 147 | * Reports the actual duration for the last cycle of work. |
| 148 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 149 | * The system will attempt to adjust the scheduling and performance of the |
| 150 | * 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] | 151 | * |
| 152 | * @param session The performance hint session instance to update. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 153 | * @param actualDurationNanos The duration of time the thread group took to complete its last |
| 154 | * task in nanoseconds. This must be positive. |
| 155 | * @return 0 on success. |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 156 | * EINVAL if actualDurationNanos is not positive. |
| 157 | * EPIPE if communication with the system service has failed. |
| 158 | */ |
| 159 | int APerformanceHint_reportActualWorkDuration( |
| 160 | APerformanceHintSession* session, |
| 161 | int64_t actualDurationNanos) __INTRODUCED_IN(__ANDROID_API_T__); |
| 162 | |
| 163 | /** |
| 164 | * Release the performance hint manager pointer acquired via |
| 165 | * {@link APerformanceHint_createSession}. |
| 166 | * |
| 167 | * @param session The performance hint session instance to release. |
| 168 | */ |
| 169 | void APerformanceHint_closeSession( |
| 170 | APerformanceHintSession* session) __INTRODUCED_IN(__ANDROID_API_T__); |
| 171 | |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 172 | /** |
| 173 | * Set a list of threads to the performance hint session. This operation will replace |
| 174 | * the current list of threads with the given list of threads. |
| 175 | * |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 176 | * @param session The performance hint session instance to update. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 177 | * @param threadIds The list of threads to be associated with this session. They must be part of |
| 178 | * this app's thread group. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 179 | * @param size The size of the list of threadIds. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 180 | * @return 0 on success. |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 181 | * EINVAL if the list of thread ids is empty or if any of the thread ids are not part of |
| 182 | the thread group. |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 183 | * EPIPE if communication with the system service has failed. |
| 184 | */ |
| 185 | int APerformanceHint_setThreads( |
| 186 | APerformanceHintSession* session, |
Peiyong Lin | c1041d4 | 2023-01-26 00:51:33 +0000 | [diff] [blame] | 187 | const pid_t* threadIds, |
Peiyong Lin | f9c984f | 2022-11-11 18:28:20 +0000 | [diff] [blame] | 188 | size_t size) __INTRODUCED_IN(__ANDROID_API_U__); |
| 189 | |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 190 | /** |
| 191 | * This tells the session that these threads can be |
| 192 | * safely scheduled to prefer power efficiency over performance. |
| 193 | * |
| 194 | * @param session The performance hint session instance to update. |
| 195 | * @param enabled The flag which sets whether this session will use power-efficient scheduling. |
| 196 | * @return 0 on success. |
| 197 | * EPIPE if communication with the system service has failed. |
| 198 | */ |
| 199 | int APerformanceHint_setPreferPowerEfficiency( |
| 200 | APerformanceHintSession* session, |
| 201 | bool enabled) __INTRODUCED_IN(__ANDROID_API_V__); |
| 202 | |
Bo Liu | 406c8ab | 2021-11-10 19:20:40 -0500 | [diff] [blame] | 203 | __END_DECLS |
| 204 | |
| 205 | #endif // ANDROID_NATIVE_PERFORMANCE_HINT_H |
Matt Buckley | cc14642 | 2023-06-28 19:14:02 +0000 | [diff] [blame^] | 206 | |
| 207 | /** @} */ |