blob: 8d204baea3e1785759983f8a9817772f7f29f4a4 [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
55#include <android/api-level.h>
56#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;
63
64/**
65 * An opaque type representing a handle to a performance hint manager.
66 * It must be released after use.
67 *
Matt Buckleycc146422023-06-28 19:14:02 +000068 * To use:<ul>
Bo Liu406c8ab2021-11-10 19:20:40 -050069 * <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 */
76typedef 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 Buckleycc146422023-06-28 19:14:02 +000081 * with {@link APerformanceHint_createSession}. It must be
Bo Liu406c8ab2021-11-10 19:20:40 -050082 * 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 Buckleycc146422023-06-28 19:14:02 +000086 * long-lived and not created or destroyed dynamically.
Bo Liu406c8ab2021-11-10 19:20:40 -050087 *
Matt Buckleycc146422023-06-28 19:14:02 +000088 * 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 Liu406c8ab2021-11-10 19:20:40 -050095 *
Matt Buckleycc146422023-06-28 19:14:02 +000096 * All timings should be from `std::chrono::steady_clock` or `clock_gettime(CLOCK_MONOTONIC, ...)`
Bo Liu406c8ab2021-11-10 19:20:40 -050097 */
98typedef struct APerformanceHintSession APerformanceHintSession;
99
100/**
101 * Acquire an instance of the performance hint manager.
102 *
Matt Buckleycc146422023-06-28 19:14:02 +0000103 * @return APerformanceHintManager instance on success, nullptr on failure.
Bo Liu406c8ab2021-11-10 19:20:40 -0500104 */
105APerformanceHintManager* 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 Buckleycc146422023-06-28 19:14:02 +0000110 *
Bo Liu406c8ab2021-11-10 19:20:40 -0500111 * @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 Buckleycc146422023-06-28 19:14:02 +0000113 * 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 Liu406c8ab2021-11-10 19:20:40 -0500118 */
119APerformanceHintSession* 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 */
130int64_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 Buckleycc146422023-06-28 19:14:02 +0000137 * @param targetDurationNanos The new desired duration in nanoseconds. This must be positive.
138 * @return 0 on success.
Bo Liu406c8ab2021-11-10 19:20:40 -0500139 * EINVAL if targetDurationNanos is not positive.
140 * EPIPE if communication with the system service has failed.
141 */
142int 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 Buckleycc146422023-06-28 19:14:02 +0000149 * 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 Liu406c8ab2021-11-10 19:20:40 -0500151 *
152 * @param session The performance hint session instance to update.
Matt Buckleycc146422023-06-28 19:14:02 +0000153 * @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 Liu406c8ab2021-11-10 19:20:40 -0500156 * EINVAL if actualDurationNanos is not positive.
157 * EPIPE if communication with the system service has failed.
158 */
159int 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 */
169void APerformanceHint_closeSession(
170 APerformanceHintSession* session) __INTRODUCED_IN(__ANDROID_API_T__);
171
Peiyong Linf9c984f2022-11-11 18:28:20 +0000172/**
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 Buckleycc146422023-06-28 19:14:02 +0000176 * @param session The performance hint session instance to update.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000177 * @param threadIds The list of threads to be associated with this session. They must be part of
178 * this app's thread group.
Matt Buckleycc146422023-06-28 19:14:02 +0000179 * @param size The size of the list of threadIds.
Peiyong Linf9c984f2022-11-11 18:28:20 +0000180 * @return 0 on success.
Matt Buckleycc146422023-06-28 19:14:02 +0000181 * 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 Linf9c984f2022-11-11 18:28:20 +0000183 * EPIPE if communication with the system service has failed.
184 */
185int APerformanceHint_setThreads(
186 APerformanceHintSession* session,
Peiyong Linc1041d42023-01-26 00:51:33 +0000187 const pid_t* threadIds,
Peiyong Linf9c984f2022-11-11 18:28:20 +0000188 size_t size) __INTRODUCED_IN(__ANDROID_API_U__);
189
Matt Buckleycc146422023-06-28 19:14:02 +0000190/**
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 */
199int APerformanceHint_setPreferPowerEfficiency(
200 APerformanceHintSession* session,
201 bool enabled) __INTRODUCED_IN(__ANDROID_API_V__);
202
Bo Liu406c8ab2021-11-10 19:20:40 -0500203__END_DECLS
204
205#endif // ANDROID_NATIVE_PERFORMANCE_HINT_H
Matt Buckleycc146422023-06-28 19:14:02 +0000206
207/** @} */