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