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