Skip to content

Commit fc6f906

Browse files
xwxw-gAndroid (Google) Code Review
authored andcommitted
Merge "Add NDK CPU/GPU headroom APIs" into main
2 parents 8a19587 + 565c1f9 commit fc6f906

1 file changed

Lines changed: 349 additions & 0 deletions

File tree

include/android/system_health.h

Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
1+
/*
2+
* Copyright (C) 2024 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+
17+
/**
18+
* @defgroup SystemHealth
19+
* @{
20+
*/
21+
22+
/**
23+
* @file system_health.h
24+
*/
25+
26+
#ifndef _ANDROID_SYSTEM_HEALTH_H
27+
#define _ANDROID_SYSTEM_HEALTH_H
28+
29+
#include <sys/cdefs.h>
30+
31+
/******************************************************************
32+
*
33+
* IMPORTANT NOTICE:
34+
*
35+
* This file is part of Android's set of stable system headers
36+
* exposed by the Android NDK (Native Development Kit).
37+
*
38+
* Third-party source AND binary code relies on the definitions
39+
* here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
40+
*
41+
* - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
42+
* - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
43+
* - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
44+
* - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
45+
*/
46+
47+
48+
#include <stdint.h>
49+
#include <sys/types.h>
50+
51+
#if !defined(__INTRODUCED_IN)
52+
#define __INTRODUCED_IN(__api_level) /* nothing */
53+
#endif
54+
55+
#ifdef __cplusplus
56+
extern "C" {
57+
#endif
58+
59+
/**
60+
* Params used to customize the calculation of CPU headroom.
61+
*
62+
* Also see {@link ASystemHealth_getCpuHeadroom}.
63+
*/
64+
typedef struct ACpuHeadroomParams ACpuHeadroomParams;
65+
66+
/**
67+
* Params used to customize the calculation of GPU headroom.
68+
*
69+
* Also see {@link ASystemHealth_getGpuHeadroom}.
70+
*/
71+
typedef struct AGpuHeadroomParams AGpuHeadroomParams;
72+
73+
/**
74+
* Creates a new instance of ACpuHeadroomParams.
75+
*
76+
* When the client finishes using {@link ACpuHeadroomParams},
77+
* {@link ACpuHeadroomParams_destroy()} must be called to destroy
78+
* and free up the resources associated with {@link ACpuHeadroomParams}.
79+
*
80+
* Available since API level 36.
81+
*
82+
* @return A new instance of ACpuHeadroomParams.
83+
*/
84+
ACpuHeadroomParams *_Nonnull ACpuHeadroomParams_create()
85+
__INTRODUCED_IN(36);
86+
87+
enum ACpuHeadroomCalculationType {
88+
/**
89+
* Use the minimum headroom value within the calculation window.
90+
* Introduced in API level 36.
91+
*/
92+
ACPU_HEADROOM_CALCULATION_TYPE_MIN = 0,
93+
/**
94+
* Use the average headroom value within the calculation window.
95+
* Introduced in API level 36.
96+
*/
97+
ACPU_HEADROOM_CALCULATION_TYPE_AVERAGE = 1,
98+
};
99+
100+
enum AGpuHeadroomCalculationType {
101+
/**
102+
* Use the minimum headroom value within the calculation window.
103+
* Introduced in API level 36.
104+
*/
105+
AGPU_HEADROOM_CALCULATION_TYPE_MIN = 0,
106+
/**
107+
* Use the average headroom value within the calculation window.
108+
* Introduced in API level 36.
109+
*/
110+
AGPU_HEADROOM_CALCULATION_TYPE_AVERAGE = 1,
111+
};
112+
113+
/**
114+
* Sets the headroom calculation window size in ACpuHeadroomParams.
115+
*
116+
* Available since API level 36.
117+
*
118+
* @param params The params to be set.
119+
* @param windowMillis The window size in milliseconds ranged from [50, 10000]. The smaller the
120+
* window size, the larger fluctuation in the headroom value should be expected.
121+
* The default value can be retrieved from the
122+
* {@link #ACpuHeadroomParams_getCalculationWindowMillis} if not set. The device
123+
* will try to use the closest feasible window size to this param.
124+
*/
125+
void ACpuHeadroomParams_setCalculationWindowMillis(ACpuHeadroomParams *_Nonnull params,
126+
int windowMillis)
127+
__INTRODUCED_IN(36);
128+
129+
/**
130+
* Gets the headroom calculation window size in ACpuHeadroomParams.
131+
*
132+
* Available since API level 36.
133+
*
134+
* @param params The params to be set.
135+
* @return This will return the default value chosen by the device if the params is not set.
136+
*/
137+
int ACpuHeadroomParams_getCalculationWindowMillis(ACpuHeadroomParams *_Nonnull params)
138+
__INTRODUCED_IN(36);
139+
140+
/**
141+
* Sets the headroom calculation window size in AGpuHeadroomParams.
142+
*
143+
* Available since API level 36.
144+
*
145+
* @param params The params to be set.
146+
* @param windowMillis The window size in milliseconds ranged from [50, 10000]. The smaller the
147+
* window size, the larger fluctuation in the headroom value should be expected.
148+
* The default value can be retrieved from the
149+
* {@link #AGpuHeadroomParams_getCalculationWindowMillis} if not set. The device
150+
* will try to use the closest feasible window size to this param.
151+
*/
152+
void AGpuHeadroomParams_setCalculationWindowMillis(AGpuHeadroomParams *_Nonnull params,
153+
int windowMillis)
154+
__INTRODUCED_IN(36);
155+
156+
/**
157+
* Gets the headroom calculation window size in AGpuHeadroomParams.
158+
*
159+
* Available since API level 36.
160+
*
161+
* @param params The params to be set.
162+
* @return This will return the default value chosen by the device if the params is not set.
163+
*/
164+
int AGpuHeadroomParams_getCalculationWindowMillis(AGpuHeadroomParams *_Nonnull params)
165+
__INTRODUCED_IN(36);
166+
167+
/**
168+
* Sets the headroom calculation type in ACpuHeadroomParams.
169+
*
170+
* Available since API level 36.
171+
*
172+
* @param params The params to be set.
173+
* @param calculationType The headroom calculation type.
174+
*/
175+
void ACpuHeadroomParams_setCalculationType(ACpuHeadroomParams *_Nonnull params,
176+
ACpuHeadroomCalculationType calculationType)
177+
__INTRODUCED_IN(36);
178+
179+
/**
180+
* Gets the headroom calculation type in ACpuHeadroomParams.
181+
*
182+
* Available since API level 36.
183+
*
184+
* @param params The params to be set.
185+
* @return The headroom calculation type.
186+
*/
187+
ACpuHeadroomCalculationType
188+
ACpuHeadroomParams_getCalculationType(ACpuHeadroomParams *_Nonnull params)
189+
__INTRODUCED_IN(36);
190+
191+
/**
192+
* Sets the headroom calculation type in AGpuHeadroomParams.
193+
*
194+
* Available since API level 36.
195+
*
196+
* @param params The params to be set.
197+
* @param calculationType The headroom calculation type.
198+
*/
199+
void AGpuHeadroomParams_setCalculationType(AGpuHeadroomParams *_Nonnull params,
200+
AGpuHeadroomCalculationType calculationType)
201+
__INTRODUCED_IN(36);
202+
203+
/**
204+
* Gets the headroom calculation type in AGpuHeadroomParams.
205+
*
206+
* Available since API level 36.
207+
*
208+
* @param params The params to be set.
209+
* @return The headroom calculation type.
210+
*/
211+
AGpuHeadroomCalculationType
212+
AGpuHeadroomParams_getCalculationType(AGpuHeadroomParams *_Nonnull params)
213+
__INTRODUCED_IN(36);
214+
215+
/**
216+
* Sets the thread TIDs to track in ACpuHeadroomParams.
217+
*
218+
* Available since API level 36.
219+
*
220+
* @param params The params to be set.
221+
* @param tids Non-null array of TIDs, maximum 5.
222+
* @param tidsSize The size of the tids array.
223+
*/
224+
void ACpuHeadroomParams_setTids(ACpuHeadroomParams *_Nonnull params, const int *_Nonnull tids,
225+
int tidsSize)
226+
__INTRODUCED_IN(36);
227+
228+
/**
229+
* Creates a new instance of AGpuHeadroomParams.
230+
*
231+
* When the client finishes using {@link AGpuHeadroomParams},
232+
* {@link AGpuHeadroomParams_destroy()} must be called to destroy
233+
* and free up the resources associated with {@link AGpuHeadroomParams}.
234+
*
235+
* Available since API level 36.
236+
*
237+
* @return A new instance of AGpuHeadroomParams.
238+
*/
239+
AGpuHeadroomParams *_Nonnull AGpuHeadroomParams_create()
240+
__INTRODUCED_IN(36);
241+
242+
/**
243+
* Deletes the ACpuHeadroomParams instance.
244+
*
245+
* Available since API level 36.
246+
*
247+
* @param params The params to be deleted.
248+
*/
249+
void ACpuHeadroomParams_destroy(ACpuHeadroomParams *_Nonnull params)
250+
__INTRODUCED_IN(36);
251+
252+
/**
253+
* Deletes the AGpuHeadroomParams instance.
254+
*
255+
* Available since API level 36.
256+
*
257+
* @param params The params to be deleted.
258+
*/
259+
void AGpuHeadroomParams_destroy(AGpuHeadroomParams *_Nonnull params)
260+
__INTRODUCED_IN(36);
261+
262+
/**
263+
* Provides an estimate of available CPU capacity headroom of the device.
264+
*
265+
* The value can be used by the calling application to determine if the workload was CPU bound and
266+
* then take action accordingly to ensure that the workload can be completed smoothly. It can also
267+
* be used with the thermal status and headroom to determine if reducing the CPU bound workload can
268+
* help reduce the device temperature to avoid thermal throttling.
269+
*
270+
* Available since API level 36.
271+
*
272+
* @param params The params to customize the CPU headroom calculation, or nullptr to use the default
273+
* @param outHeadroom Non-null output pointer to a single float, which will be set to the CPU
274+
* headroom value. The value will be a single value or `Float.NaN` if it's
275+
* temporarily unavailable.
276+
* Each valid value ranges from [0, 100], where 0 indicates no more cpu resources
277+
* can be granted.
278+
* @return 0 on success
279+
* EPIPE if failed to get the CPU headroom.
280+
* EPERM if the TIDs do not belong to the same process.
281+
* ENOTSUP if API or requested params is unsupported.
282+
*/
283+
int ASystemHealth_getCpuHeadroom(const ACpuHeadroomParams *_Nullable params,
284+
float *_Nonnull outHeadroom)
285+
__INTRODUCED_IN(36);
286+
287+
/**
288+
* Provides an estimate of available GPU capacity headroom of the device.
289+
*
290+
* The value can be used by the calling application to determine if the workload was GPU bound and
291+
* then take action accordingly to ensure that the workload can be completed smoothly. It can also
292+
* be used with the thermal status and headroom to determine if reducing the GPU bound workload can
293+
* help reduce the device temperature to avoid thermal throttling.
294+
*
295+
* Available since API level 36
296+
*
297+
* @param params The params to customize the GPU headroom calculation, or nullptr to use the default
298+
* @param outHeadroom Non-null output pointer to a single float, which will be set to the GPU
299+
* headroom value. The value will be a single value or `Float.NaN` if it's
300+
* temporarily unavailable.
301+
* Each valid value ranges from [0, 100], where 0 indicates no more gpu resources
302+
* can be granted.
303+
* @return 0 on success
304+
* EPIPE if failed to get the GPU headroom.
305+
* ENOTSUP if API or requested params is unsupported.
306+
*/
307+
int ASystemHealth_getGpuHeadroom(const AGpuHeadroomParams *_Nullable params,
308+
float *_Nonnull outHeadroom)
309+
__INTRODUCED_IN(36);
310+
311+
/**
312+
* Gets minimum polling interval for calling {@link ASystemHealth_getCpuHeadroom} in milliseconds.
313+
*
314+
* The getCpuHeadroom API may return cached result if called more frequently than the interval.
315+
*
316+
* Available since API level 36.
317+
*
318+
* @param outMinIntervalMillis Non-null output pointer to a int64_t, which
319+
* will be set to the minimum polling interval in milliseconds.
320+
* @return 0 on success
321+
* EPIPE if failed to get the minimum polling interval.
322+
* ENOTSUP if API is unsupported.
323+
*/
324+
int ASystemHealth_getCpuHeadroomMinIntervalMillis(int64_t *_Nonnull outMinIntervalMillis)
325+
__INTRODUCED_IN(36);
326+
327+
/**
328+
* Gets minimum polling interval for calling {@link ASystemHealth_getGpuHeadroom} in milliseconds.
329+
*
330+
* The getGpuHeadroom API may return cached result if called more frequent than the interval.
331+
*
332+
* Available since API level 36.
333+
*
334+
* @param outMinIntervalMillis Non-null output pointer to a int64_t, which
335+
* will be set to the minimum polling interval in milliseconds.
336+
* @return 0 on success
337+
* EPIPE if failed to get the minimum polling interval.
338+
* ENOTSUP if API is unsupported.
339+
*/
340+
int ASystemHealth_getGpuHeadroomMinIntervalMillis(int64_t *_Nonnull outMinIntervalMillis)
341+
__INTRODUCED_IN(36);
342+
343+
#ifdef __cplusplus
344+
}
345+
#endif
346+
347+
#endif // _ANDROID_SYSTEM_HEALTH_H
348+
349+
/** @} */

0 commit comments

Comments
 (0)