xref: /xnu-8792.61.2/osfmk/mach/coalition.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * Copyright (c) 2014 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _MACH_COALITION_H_
30 #define _MACH_COALITION_H_
31 
32 /* code shared by userspace and xnu */
33 
34 #define COALITION_SPAWN_ENTITLEMENT "com.apple.private.coalition-spawn"
35 
36 #define COALITION_CREATE_FLAGS_MASK       ((uint32_t)0xFF3)
37 #define COALITION_CREATE_FLAGS_PRIVILEGED ((uint32_t)0x01)
38 #define COALITION_CREATE_FLAGS_EFFICIENT  ((uint32_t)0x02)
39 
40 #define COALITION_CREATE_FLAGS_TYPE_MASK  ((uint32_t)0xF0)
41 #define COALITION_CREATE_FLAGS_TYPE_SHIFT (4)
42 
43 #define COALITION_CREATE_FLAGS_GET_TYPE(flags) \
44 	(((flags) & COALITION_CREATE_FLAGS_TYPE_MASK) >> COALITION_CREATE_FLAGS_TYPE_SHIFT)
45 
46 #define COALITION_CREATE_FLAGS_SET_TYPE(flags, type) \
47 	do { \
48 	        flags &= ~COALITION_CREATE_FLAGS_TYPE_MASK; \
49 	        flags |= (((type) << COALITION_CREATE_FLAGS_TYPE_SHIFT) \
50 	                   & COALITION_CREATE_FLAGS_TYPE_MASK); \
51 	} while (0)
52 
53 #define COALITION_CREATE_FLAGS_ROLE_MASK  ((uint32_t)0xF00)
54 #define COALITION_CREATE_FLAGS_ROLE_SHIFT (8)
55 
56 #define COALITION_CREATE_FLAGS_GET_ROLE(flags) \
57     (((flags) & COALITION_CREATE_FLAGS_ROLE_MASK) >> COALITION_CREATE_FLAGS_ROLE_SHIFT)
58 
59 #define COALITION_CREATE_FLAGS_SET_ROLE(flags, role) \
60     do { \
61 	flags &= ~COALITION_CREATE_FLAGS_ROLE_MASK; \
62 	flags |= (((role) << COALITION_CREATE_FLAGS_ROLE_SHIFT) \
63 	       & COALITION_CREATE_FLAGS_ROLE_MASK); \
64     } while (0)
65 
66 /*
67  * Default scheduling policy of the lead/parent task in a coalition
68  */
69 #define COALITION_ROLE_UNDEF       (0)
70 #define COALITION_ROLE_SYSTEM      (1)
71 #define COALITION_ROLE_BACKGROUND  (2)
72 #define COALITION_ROLE_ADAPTIVE    (3)
73 #define COALITION_ROLE_INTERACTIVE (4)
74 #define COALITION_NUM_ROLES        (5)
75 
76 #define COALITION_TYPE_RESOURCE  (0)
77 #define COALITION_TYPE_JETSAM    (1)
78 #define COALITION_TYPE_MAX       (1)
79 
80 #define COALITION_NUM_TYPES      (COALITION_TYPE_MAX + 1)
81 
82 #define COALITION_TASKROLE_NONE   (-1) /* task plays no role in the given coalition */
83 #define COALITION_TASKROLE_UNDEF  (0)
84 #define COALITION_TASKROLE_LEADER (1)
85 #define COALITION_TASKROLE_XPC    (2)
86 #define COALITION_TASKROLE_EXT    (3)
87 
88 #define COALITION_NUM_TASKROLES   (4)
89 
90 #define COALITION_ROLEMASK_ALLROLES ((1 << COALITION_NUM_TASKROLES) - 1)
91 #define COALITION_ROLEMASK_UNDEF    (1 << COALITION_TASKROLE_UNDEF)
92 #define COALITION_ROLEMASK_LEADER   (1 << COALITION_TASKROLE_LEADER)
93 #define COALITION_ROLEMASK_XPC      (1 << COALITION_TASKROLE_XPC)
94 #define COALITION_ROLEMASK_EXT      (1 << COALITION_TASKROLE_EXT)
95 
96 #define COALITION_SORT_NOSORT     (0)
97 #define COALITION_SORT_DEFAULT    (1)
98 #define COALITION_SORT_MEM_ASC    (2)
99 #define COALITION_SORT_MEM_DEC    (3)
100 #define COALITION_SORT_USER_ASC   (4)
101 #define COALITION_SORT_USER_DEC   (5)
102 
103 #define COALITION_NUM_SORT        (6)
104 
105 #define COALITION_NUM_THREAD_QOS_TYPES   7
106 
107 /* Flags for coalition efficiency (Deprecated) */
108 #define COALITION_FLAGS_EFFICIENT       (0x1)
109 
110 struct coalition_resource_usage {
111 	uint64_t tasks_started;
112 	uint64_t tasks_exited;
113 	uint64_t time_nonempty;
114 	uint64_t cpu_time;
115 	uint64_t interrupt_wakeups;
116 	uint64_t platform_idle_wakeups;
117 	uint64_t bytesread;
118 	uint64_t byteswritten;
119 	uint64_t gpu_time;
120 	uint64_t cpu_time_billed_to_me;
121 	uint64_t cpu_time_billed_to_others;
122 	uint64_t energy;
123 	uint64_t logical_immediate_writes;
124 	uint64_t logical_deferred_writes;
125 	uint64_t logical_invalidated_writes;
126 	uint64_t logical_metadata_writes;
127 	uint64_t logical_immediate_writes_to_external;
128 	uint64_t logical_deferred_writes_to_external;
129 	uint64_t logical_invalidated_writes_to_external;
130 	uint64_t logical_metadata_writes_to_external;
131 	uint64_t energy_billed_to_me;
132 	uint64_t energy_billed_to_others;
133 	uint64_t cpu_ptime;
134 	uint64_t cpu_time_eqos_len;     /* Stores the number of thread QoS types */
135 	uint64_t cpu_time_eqos[COALITION_NUM_THREAD_QOS_TYPES];
136 	uint64_t cpu_instructions;
137 	uint64_t cpu_cycles;
138 	uint64_t fs_metadata_writes;
139 	uint64_t pm_writes;
140 	uint64_t cpu_pinstructions;
141 	uint64_t cpu_pcycles;
142 };
143 
144 #ifdef PRIVATE
145 /* definitions shared by only xnu + Libsyscall */
146 
147 /* coalition id for kernel task */
148 #define COALITION_ID_KERNEL 1
149 
150 /* Syscall flavors */
151 #define COALITION_OP_CREATE 1
152 #define COALITION_OP_TERMINATE 2
153 #define COALITION_OP_REAP 3
154 
155 /* coalition_info flavors */
156 #define COALITION_INFO_RESOURCE_USAGE 1
157 #define COALITION_INFO_SET_NAME 2
158 #define COALITION_INFO_SET_EFFICIENCY 3
159 
160 /* coalition_ledger_set operations */
161 #define COALITION_LEDGER_SET_LOGICAL_WRITES_LIMIT 1
162 
163 #define COALITION_EFFICIENCY_VALID_FLAGS    (COALITION_FLAGS_EFFICIENT)
164 
165 /* structure returned from libproc coalition listing interface */
166 struct procinfo_coalinfo {
167 	uint64_t coalition_id;
168 	uint32_t coalition_type;
169 	uint32_t coalition_tasks;
170 };
171 
172 #endif /* PRIVATE */
173 
174 #ifdef XNU_KERNEL_PRIVATE
175 #if COALITION_DEBUG
176 #define coal_dbg(fmt, ...) \
177 	printf("%s: " fmt "\n", __func__, ## __VA_ARGS__)
178 #else
179 #define coal_dbg(fmt, ...)
180 #endif
181 
182 #endif
183 
184 #endif /* _MACH_COALITION_H_ */
185