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