xref: /xnu-11417.140.69/bsd/sys/coalition.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2013 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions #ifndef _SYS_COALITION_H_
30*43a90889SApple OSS Distributions #define _SYS_COALITION_H_
31*43a90889SApple OSS Distributions 
32*43a90889SApple OSS Distributions #include <stdint.h>
33*43a90889SApple OSS Distributions #include <sys/cdefs.h>
34*43a90889SApple OSS Distributions #include <sys/types.h>
35*43a90889SApple OSS Distributions 
36*43a90889SApple OSS Distributions #include <mach/coalition.h>
37*43a90889SApple OSS Distributions 
38*43a90889SApple OSS Distributions __BEGIN_DECLS
39*43a90889SApple OSS Distributions 
40*43a90889SApple OSS Distributions #ifndef KERNEL
41*43a90889SApple OSS Distributions /* Userspace syscall prototypes */
42*43a90889SApple OSS Distributions 
43*43a90889SApple OSS Distributions /* Syscalls */
44*43a90889SApple OSS Distributions int coalition_create(uint64_t *cid_out, uint32_t flags);
45*43a90889SApple OSS Distributions int coalition_terminate(uint64_t cid, uint32_t flags);
46*43a90889SApple OSS Distributions int coalition_reap(uint64_t cid, uint32_t flags);
47*43a90889SApple OSS Distributions 
48*43a90889SApple OSS Distributions /* Wrappers around __coalition_info syscall (with proper struct types) */
49*43a90889SApple OSS Distributions int coalition_info_resource_usage(uint64_t cid, struct coalition_resource_usage *cru, size_t sz);
50*43a90889SApple OSS Distributions int coalition_info_set_name(uint64_t cid, const char *name, size_t size);
51*43a90889SApple OSS Distributions int coalition_info_set_efficiency(uint64_t cid, uint64_t flags);
52*43a90889SApple OSS Distributions int coalition_ledger_set_logical_writes_limit(uint64_t cid, int64_t limit);
53*43a90889SApple OSS Distributions 
54*43a90889SApple OSS Distributions #ifdef PRIVATE
55*43a90889SApple OSS Distributions int coalition_info_debug_info(uint64_t cid, struct coalinfo_debuginfo *cru, size_t sz);
56*43a90889SApple OSS Distributions #endif /* PRIVATE */
57*43a90889SApple OSS Distributions 
58*43a90889SApple OSS Distributions #else /* KERNEL */
59*43a90889SApple OSS Distributions 
60*43a90889SApple OSS Distributions #if CONFIG_COALITIONS
61*43a90889SApple OSS Distributions /* in-kernel BSD interfaces */
62*43a90889SApple OSS Distributions 
63*43a90889SApple OSS Distributions /*
64*43a90889SApple OSS Distributions  * coalition_id:
65*43a90889SApple OSS Distributions  * Get the unique 64-bit identifier associated with the given coalition
66*43a90889SApple OSS Distributions  */
67*43a90889SApple OSS Distributions uint64_t coalition_id(coalition_t coal);
68*43a90889SApple OSS Distributions 
69*43a90889SApple OSS Distributions 
70*43a90889SApple OSS Distributions /*
71*43a90889SApple OSS Distributions  * coalitions_get_list:
72*43a90889SApple OSS Distributions  * Get a list of coalitions as procinfo_coalinfo structures
73*43a90889SApple OSS Distributions  *
74*43a90889SApple OSS Distributions  * This interface is primarily to support libproc.
75*43a90889SApple OSS Distributions  *
76*43a90889SApple OSS Distributions  * Parameters:
77*43a90889SApple OSS Distributions  *      type      : The COALITION_TYPE of the coalitions to investigate.
78*43a90889SApple OSS Distributions  *                  Valid types can be found in <mach/coalition.h>
79*43a90889SApple OSS Distributions  *      coal_list : Pointer to an array of procinfo_coalinfo structures
80*43a90889SApple OSS Distributions  *                  that will be filled with information about each
81*43a90889SApple OSS Distributions  *                  coalition whose type matches 'type'
82*43a90889SApple OSS Distributions  *                  NOTE: This can be NULL to perform a simple query of
83*43a90889SApple OSS Distributions  *                  the total number of coalitions.
84*43a90889SApple OSS Distributions  *      list_sz   : The size (in number of structures) of 'coal_list'
85*43a90889SApple OSS Distributions  *
86*43a90889SApple OSS Distributions  * Returns: 0 if no coalitions matching 'type' are found
87*43a90889SApple OSS Distributions  *          Otherwise: the number of coalitions whose type matches
88*43a90889SApple OSS Distributions  *                     the 'type' parameter (all coalitions if type == -1)
89*43a90889SApple OSS Distributions  */
90*43a90889SApple OSS Distributions extern size_t coalitions_get_list(int type, struct procinfo_coalinfo *coal_list, size_t list_sz);
91*43a90889SApple OSS Distributions 
92*43a90889SApple OSS Distributions 
93*43a90889SApple OSS Distributions /*
94*43a90889SApple OSS Distributions  * task_get_coalition:
95*43a90889SApple OSS Distributions  * Return the coalition of a task.
96*43a90889SApple OSS Distributions  *
97*43a90889SApple OSS Distributions  * Parameters:
98*43a90889SApple OSS Distributions  *      task      : The task to investigate
99*43a90889SApple OSS Distributions  *      coal_type : The COALITION_TYPE of the coalition to investigate.
100*43a90889SApple OSS Distributions  *                  Valid types can be found in <mach/coalition.h>
101*43a90889SApple OSS Distributions  *
102*43a90889SApple OSS Distributions  * Returns: valid coalition_t or COALITION_NULL
103*43a90889SApple OSS Distributions  */
104*43a90889SApple OSS Distributions extern coalition_t task_get_coalition(task_t task, int coal_type);
105*43a90889SApple OSS Distributions 
106*43a90889SApple OSS Distributions 
107*43a90889SApple OSS Distributions /*
108*43a90889SApple OSS Distributions  * coalition_is_leader:
109*43a90889SApple OSS Distributions  * Determine if a task is a coalition leader.
110*43a90889SApple OSS Distributions  *
111*43a90889SApple OSS Distributions  * Parameters:
112*43a90889SApple OSS Distributions  *      task      : The task to investigate
113*43a90889SApple OSS Distributions  *      coal      : The coalition to test against.
114*43a90889SApple OSS Distributions  *                  NOTE: This can be COALITION_NULL, in case FALSE is returned.
115*43a90889SApple OSS Distributions  *
116*43a90889SApple OSS Distributions  * Returns: TRUE if 'task' is the coalition's leader, FALSE otherwise.
117*43a90889SApple OSS Distributions  */
118*43a90889SApple OSS Distributions extern boolean_t coalition_is_leader(task_t task, coalition_t coal);
119*43a90889SApple OSS Distributions 
120*43a90889SApple OSS Distributions /*
121*43a90889SApple OSS Distributions  * coalition_get_leader:
122*43a90889SApple OSS Distributions  * Get a task reference on the leader of a given coalition
123*43a90889SApple OSS Distributions  *
124*43a90889SApple OSS Distributions  * Parameters:
125*43a90889SApple OSS Distributions  *	coal      : The coalition to investigate
126*43a90889SApple OSS Distributions  *
127*43a90889SApple OSS Distributions  * Returns: A referenced task pointer of the leader of the given coalition.
128*43a90889SApple OSS Distributions  *          This could be TASK_NULL if the coalition doesn't have a leader.
129*43a90889SApple OSS Distributions  *          If the return value is non-null, the caller is responsible to call
130*43a90889SApple OSS Distributions  *          task_deallocate on the returned value.
131*43a90889SApple OSS Distributions  */
132*43a90889SApple OSS Distributions extern task_t coalition_get_leader(coalition_t coal);
133*43a90889SApple OSS Distributions 
134*43a90889SApple OSS Distributions 
135*43a90889SApple OSS Distributions /*
136*43a90889SApple OSS Distributions  * coalition_get_task_count:
137*43a90889SApple OSS Distributions  * Sum up the number of tasks in the given coalition
138*43a90889SApple OSS Distributions  *
139*43a90889SApple OSS Distributions  * Parameters:
140*43a90889SApple OSS Distributions  *      coal     : The coalition to investigate
141*43a90889SApple OSS Distributions  *
142*43a90889SApple OSS Distributions  * Returns: The number of tasks in the coalition
143*43a90889SApple OSS Distributions  */
144*43a90889SApple OSS Distributions extern int coalition_get_task_count(coalition_t coal);
145*43a90889SApple OSS Distributions 
146*43a90889SApple OSS Distributions /*
147*43a90889SApple OSS Distributions  * coalition_get_page_count:
148*43a90889SApple OSS Distributions  * Sum up the page count for each task in the coalition specified by 'coal'
149*43a90889SApple OSS Distributions  *
150*43a90889SApple OSS Distributions  * Parameters:
151*43a90889SApple OSS Distributions  *      coal     : The coalition to investigate
152*43a90889SApple OSS Distributions  *      ntasks   : If non-NULL, this will be filled in with the number of
153*43a90889SApple OSS Distributions  *                 tasks in the coalition.
154*43a90889SApple OSS Distributions  *
155*43a90889SApple OSS Distributions  * Returns: The sum of all pages used by all members of the coalition
156*43a90889SApple OSS Distributions  */
157*43a90889SApple OSS Distributions extern uint64_t coalition_get_page_count(coalition_t coal, int *ntasks);
158*43a90889SApple OSS Distributions 
159*43a90889SApple OSS Distributions /*
160*43a90889SApple OSS Distributions  * coalition_get_pid_list:
161*43a90889SApple OSS Distributions  * Gather a list of constituent PIDs of tasks within a coalition playing a
162*43a90889SApple OSS Distributions  * given role.
163*43a90889SApple OSS Distributions  *
164*43a90889SApple OSS Distributions  * Parameters:
165*43a90889SApple OSS Distributions  *      coal       : The coalition to investigate
166*43a90889SApple OSS Distributions  *      rolemask   : The set of coalition task roles used to filter the list
167*43a90889SApple OSS Distributions  *                   of PIDs returned in 'pid_list'. Roles can be combined
168*43a90889SApple OSS Distributions  *                   using the COALITION_ROLEMASK_* tokens found in
169*43a90889SApple OSS Distributions  *                   <mach/coalition.h>. Each PID returned is guaranteed to
170*43a90889SApple OSS Distributions  *                   be tagged with one of the task roles specified by this
171*43a90889SApple OSS Distributions  *                   mask.
172*43a90889SApple OSS Distributions  *      sort_order : The order in which the returned PIDs should be sorted
173*43a90889SApple OSS Distributions  *                   by default this is in descending page count.
174*43a90889SApple OSS Distributions  *      pid_list   : Pointer to an array of PIDs that will be filled with
175*43a90889SApple OSS Distributions  *                   members of the coalition tagged with the given 'taskrole'
176*43a90889SApple OSS Distributions  *      list_sz    : The size (in number of PIDs) of 'pid_list'
177*43a90889SApple OSS Distributions  *
178*43a90889SApple OSS Distributions  * Note:
179*43a90889SApple OSS Distributions  * This function will return the list of PIDs in a sorted order. By default
180*43a90889SApple OSS Distributions  * the PIDs will be sorted by task page count in descending order. In the
181*43a90889SApple OSS Distributions  * future it may be possible for user space to specify a level of importance
182*43a90889SApple OSS Distributions  * for each coalition member. If there is a user space specified importance,
183*43a90889SApple OSS Distributions  * then the list of PIDs returned will be sorted in _ascending_ importance,
184*43a90889SApple OSS Distributions  * i.e., pid_list[0] will be the least important task (or the largest consumer
185*43a90889SApple OSS Distributions  * of memory). The desired sort order can be specified using the
186*43a90889SApple OSS Distributions  * COALITION_SORT_* definitions in osfmk/mach/coalition.h
187*43a90889SApple OSS Distributions  *
188*43a90889SApple OSS Distributions  * It is also possible to return an unsorted list of PIDs using the special
189*43a90889SApple OSS Distributions  * sort type 'COALITION_SORT_NOSORT'
190*43a90889SApple OSS Distributions  *
191*43a90889SApple OSS Distributions  * Returns: < 0 on ERROR
192*43a90889SApple OSS Distributions  *          0 if 'coal' contains no tasks whose role is 'taskrole'
193*43a90889SApple OSS Distributions  *              (or if the coalition is being deallocated)
194*43a90889SApple OSS Distributions  *          Otherwise: the number of PIDs in the coalition whose role is
195*43a90889SApple OSS Distributions  *                     'taskrole'. NOTE: This may be larger or smaller than
196*43a90889SApple OSS Distributions  *                     the 'pid_list' array.
197*43a90889SApple OSS Distributions  *
198*43a90889SApple OSS Distributions  */
199*43a90889SApple OSS Distributions extern int coalition_get_pid_list(coalition_t coal, uint32_t rolemask,
200*43a90889SApple OSS Distributions     int sort_order, int *pid_list, int list_sz);
201*43a90889SApple OSS Distributions 
202*43a90889SApple OSS Distributions /*
203*43a90889SApple OSS Distributions  * task_coalition_role_for_type;
204*43a90889SApple OSS Distributions  * Get the role of the given task within the given type of coalition.
205*43a90889SApple OSS Distributions  *
206*43a90889SApple OSS Distributions  * Parameters:
207*43a90889SApple OSS Distributions  *      task           : The task to investigate
208*43a90889SApple OSS Distributions  *      coalition_type : The coalition type to check
209*43a90889SApple OSS Distributions  *
210*43a90889SApple OSS Distributions  * Returns: This task's role or COALITION_TASKROLE_NONE.
211*43a90889SApple OSS Distributions  */
212*43a90889SApple OSS Distributions extern int task_coalition_role_for_type(task_t task, int coalition_type);
213*43a90889SApple OSS Distributions #else /* !CONFIG_COALITIONS */
214*43a90889SApple OSS Distributions static inline uint64_t
215*43a90889SApple OSS Distributions coalition_id(__unused coalition_t coal)
216*43a90889SApple OSS Distributions {
217*43a90889SApple OSS Distributions 	return 0;
218*43a90889SApple OSS Distributions }
219*43a90889SApple OSS Distributions 
220*43a90889SApple OSS Distributions static inline size_t
221*43a90889SApple OSS Distributions coalitions_get_list(__unused int type,
222*43a90889SApple OSS Distributions     __unused struct procinfo_coalinfo *coal_list,
223*43a90889SApple OSS Distributions     __unused size_t list_sz)
224*43a90889SApple OSS Distributions {
225*43a90889SApple OSS Distributions 	return 0;
226*43a90889SApple OSS Distributions }
227*43a90889SApple OSS Distributions 
228*43a90889SApple OSS Distributions static inline coalition_t
229*43a90889SApple OSS Distributions coalition_get_leader(__unused task_t task,
230*43a90889SApple OSS Distributions     __unused int coal_type)
231*43a90889SApple OSS Distributions {
232*43a90889SApple OSS Distributions 	return COALITION_NULL;
233*43a90889SApple OSS Distributions }
234*43a90889SApple OSS Distributions 
235*43a90889SApple OSS Distributions static inline boolean_t
236*43a90889SApple OSS Distributions coalition_is_leader(__unused task_t task,
237*43a90889SApple OSS Distributions     __unused coalition_t coal)
238*43a90889SApple OSS Distributions {
239*43a90889SApple OSS Distributions 	return FALSE;
240*43a90889SApple OSS Distributions }
241*43a90889SApple OSS Distributions 
242*43a90889SApple OSS Distributions static inline int
243*43a90889SApple OSS Distributions coalition_get_task_count(__unused coalition_t coal)
244*43a90889SApple OSS Distributions {
245*43a90889SApple OSS Distributions 	return 0;
246*43a90889SApple OSS Distributions }
247*43a90889SApple OSS Distributions 
248*43a90889SApple OSS Distributions static inline uint64_t
249*43a90889SApple OSS Distributions coalition_get_page_count(__unused coalition_t coal,
250*43a90889SApple OSS Distributions     __unused int *ntasks)
251*43a90889SApple OSS Distributions {
252*43a90889SApple OSS Distributions 	return 0;
253*43a90889SApple OSS Distributions }
254*43a90889SApple OSS Distributions 
255*43a90889SApple OSS Distributions static inline int
256*43a90889SApple OSS Distributions coalition_get_pid_list(__unused coalition_t coal,
257*43a90889SApple OSS Distributions     __unused uint32_t rolemask,
258*43a90889SApple OSS Distributions     __unused int sort_order,
259*43a90889SApple OSS Distributions     __unused int *pid_list,
260*43a90889SApple OSS Distributions     __unused int list_sz)
261*43a90889SApple OSS Distributions {
262*43a90889SApple OSS Distributions 	return 0;
263*43a90889SApple OSS Distributions }
264*43a90889SApple OSS Distributions #endif
265*43a90889SApple OSS Distributions 
266*43a90889SApple OSS Distributions #endif /* KERNEL */
267*43a90889SApple OSS Distributions 
268*43a90889SApple OSS Distributions __END_DECLS
269*43a90889SApple OSS Distributions 
270*43a90889SApple OSS Distributions #endif /* _SYS_COALITION_H_ */
271