xref: /xnu-12377.41.6/osfmk/mach/coalition.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
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 #include <stdint.h>
33 #include <stdbool.h>
34 
35 /* code shared by userspace and xnu */
36 
37 #define COALITION_SPAWN_ENTITLEMENT "com.apple.private.coalition-spawn"
38 
39 #define COALITION_CREATE_FLAGS_MASK       ((uint32_t)0xFF3)
40 #define COALITION_CREATE_FLAGS_PRIVILEGED ((uint32_t)0x01)
41 #define COALITION_CREATE_FLAGS_EFFICIENT  ((uint32_t)0x02)
42 
43 #define COALITION_CREATE_FLAGS_TYPE_MASK  ((uint32_t)0xF0)
44 #define COALITION_CREATE_FLAGS_TYPE_SHIFT (4)
45 
46 #define COALITION_CREATE_FLAGS_GET_TYPE(flags) \
47 	(((flags) & COALITION_CREATE_FLAGS_TYPE_MASK) >> COALITION_CREATE_FLAGS_TYPE_SHIFT)
48 
49 #define COALITION_CREATE_FLAGS_SET_TYPE(flags, type) \
50 	do { \
51 	        flags &= ~COALITION_CREATE_FLAGS_TYPE_MASK; \
52 	        flags |= (((type) << COALITION_CREATE_FLAGS_TYPE_SHIFT) \
53 	                   & COALITION_CREATE_FLAGS_TYPE_MASK); \
54 	} while (0)
55 
56 #define COALITION_CREATE_FLAGS_ROLE_MASK  ((uint32_t)0xF00)
57 #define COALITION_CREATE_FLAGS_ROLE_SHIFT (8)
58 
59 #define COALITION_CREATE_FLAGS_GET_ROLE(flags) \
60     (((flags) & COALITION_CREATE_FLAGS_ROLE_MASK) >> COALITION_CREATE_FLAGS_ROLE_SHIFT)
61 
62 #define COALITION_CREATE_FLAGS_SET_ROLE(flags, role) \
63     do { \
64 	flags &= ~COALITION_CREATE_FLAGS_ROLE_MASK; \
65 	flags |= (((role) << COALITION_CREATE_FLAGS_ROLE_SHIFT) \
66 	       & COALITION_CREATE_FLAGS_ROLE_MASK); \
67     } while (0)
68 
69 /*
70  * Default scheduling policy of the lead/parent task in a coalition
71  */
72 #define COALITION_ROLE_UNDEF       (0)
73 #define COALITION_ROLE_SYSTEM      (1)
74 #define COALITION_ROLE_BACKGROUND  (2)
75 #define COALITION_ROLE_ADAPTIVE    (3)
76 #define COALITION_ROLE_INTERACTIVE (4)
77 #define COALITION_NUM_ROLES        (5)
78 
79 #define COALITION_TYPE_RESOURCE  (0)
80 #define COALITION_TYPE_JETSAM    (1)
81 #define COALITION_TYPE_MAX       (1)
82 
83 #define COALITION_NUM_TYPES      (COALITION_TYPE_MAX + 1)
84 
85 #define COALITION_TASKROLE_NONE   (-1) /* task plays no role in the given coalition */
86 #define COALITION_TASKROLE_UNDEF  (0)
87 #define COALITION_TASKROLE_LEADER (1)
88 #define COALITION_TASKROLE_XPC    (2)
89 #define COALITION_TASKROLE_EXT    (3)
90 
91 #define COALITION_NUM_TASKROLES   (4)
92 
93 #define COALITION_ROLEMASK_ALLROLES ((1 << COALITION_NUM_TASKROLES) - 1)
94 #define COALITION_ROLEMASK_UNDEF    (1 << COALITION_TASKROLE_UNDEF)
95 #define COALITION_ROLEMASK_LEADER   (1 << COALITION_TASKROLE_LEADER)
96 #define COALITION_ROLEMASK_XPC      (1 << COALITION_TASKROLE_XPC)
97 #define COALITION_ROLEMASK_EXT      (1 << COALITION_TASKROLE_EXT)
98 
99 #define COALITION_SORT_NOSORT     (0)
100 #define COALITION_SORT_DEFAULT    (1)
101 #define COALITION_SORT_MEM_ASC    (2)
102 #define COALITION_SORT_MEM_DEC    (3)
103 #define COALITION_SORT_USER_ASC   (4)
104 #define COALITION_SORT_USER_DEC   (5)
105 
106 #define COALITION_NUM_SORT        (6)
107 
108 #define COALITION_NUM_THREAD_QOS_TYPES   7
109 
110 /* Flags for coalition efficiency (Deprecated) */
111 #define COALITION_FLAGS_EFFICIENT       (0x1)
112 
113 struct coalition_resource_usage {
114 	uint64_t tasks_started;
115 	uint64_t tasks_exited;
116 	uint64_t time_nonempty;
117 	uint64_t cpu_time; /* mach_absolute_time units */
118 	uint64_t interrupt_wakeups;
119 	uint64_t platform_idle_wakeups;
120 	uint64_t bytesread;
121 	uint64_t byteswritten;
122 	uint64_t gpu_time; /* nanoseconds */
123 	uint64_t cpu_time_billed_to_me; /* mach_absolute_time units */
124 	uint64_t cpu_time_billed_to_others; /* mach_absolute_time units */
125 	uint64_t energy; /* nanojoules */
126 	uint64_t logical_immediate_writes;
127 	uint64_t logical_deferred_writes;
128 	uint64_t logical_invalidated_writes;
129 	uint64_t logical_metadata_writes;
130 	uint64_t logical_immediate_writes_to_external;
131 	uint64_t logical_deferred_writes_to_external;
132 	uint64_t logical_invalidated_writes_to_external;
133 	uint64_t logical_metadata_writes_to_external;
134 	uint64_t energy_billed_to_me; /* nanojoules */
135 	uint64_t energy_billed_to_others; /* nanojoules */
136 	uint64_t cpu_ptime; /* mach_absolute_time units */
137 	uint64_t cpu_time_eqos_len;     /* Stores the number of thread QoS types */
138 	uint64_t cpu_time_eqos[COALITION_NUM_THREAD_QOS_TYPES];
139 	uint64_t cpu_instructions;
140 	uint64_t cpu_cycles;
141 	uint64_t fs_metadata_writes;
142 	uint64_t pm_writes;
143 	uint64_t cpu_pinstructions;
144 	uint64_t cpu_pcycles;
145 	uint64_t conclave_mem;
146 	uint64_t ane_mach_time; /* mach_absolute_time units */
147 	uint64_t ane_energy_nj; /* nanojoules */
148 	uint64_t phys_footprint;        /* Sum of instantaneous process phys_footprint */
149 	uint64_t gpu_energy_nj; /* nanojoules that I did */
150 	uint64_t gpu_energy_nj_billed_to_me; /* nanojoules that others did on my behalf */
151 	uint64_t gpu_energy_nj_billed_to_others; /* nanojoules that I did on others' behalf */
152 	uint64_t swapins;
153 };
154 
155 #ifdef PRIVATE
156 /* definitions shared by only xnu + Libsyscall */
157 
158 /* coalition id for kernel task */
159 #define COALITION_ID_KERNEL 1
160 
161 /* Syscall flavors */
162 #define COALITION_OP_CREATE 1
163 #define COALITION_OP_TERMINATE 2
164 #define COALITION_OP_REAP 3
165 
166 /* coalition_info flavors */
167 #define COALITION_INFO_RESOURCE_USAGE 1
168 #define COALITION_INFO_SET_NAME 2
169 #define COALITION_INFO_SET_EFFICIENCY 3
170 #define COALITION_INFO_GET_DEBUG_INFO 4
171 #define COALITION_INFO_PID_LIST 5
172 
173 struct coalinfo_debuginfo {
174 	uint64_t thread_group_id;
175 	uint32_t thread_group_recommendation;
176 	uint32_t thread_group_flags;
177 	uint32_t focal_task_count;
178 	uint32_t nonfocal_task_count;
179 	uint32_t game_task_count;
180 	uint32_t carplay_task_count;
181 };
182 
183 /* coalition_ledger_set operations */
184 #define COALITION_LEDGER_SET_LOGICAL_WRITES_LIMIT 1
185 
186 #define COALITION_EFFICIENCY_VALID_FLAGS    (COALITION_FLAGS_EFFICIENT)
187 
188 /* structure returned from libproc coalition listing interface */
189 struct procinfo_coalinfo {
190 	uint64_t coalition_id;
191 	uint32_t coalition_type;
192 	uint32_t coalition_tasks;
193 };
194 
195 #endif /* PRIVATE */
196 
197 #ifdef XNU_KERNEL_PRIVATE
198 
199 #if COALITION_DEBUG
200 #define coal_dbg(fmt, ...) \
201 	printf("%s: " fmt "\n", __func__, ## __VA_ARGS__)
202 #else
203 #define coal_dbg(fmt, ...)
204 #endif
205 
206 __options_decl(coalition_gpu_energy_t, uint32_t, {
207 	CGE_SELF    = 0x1,
208 	CGE_BILLED  = 0x2,
209 	CGE_OTHERS  = 0x4,
210 });
211 
212 extern bool coalition_add_to_gpu_energy(uint64_t coalition_id, coalition_gpu_energy_t which, uint64_t energy);
213 
214 #endif /* XNU_KERNEL_PRIVATE */
215 
216 #ifdef MACH_KERNEL_PRIVATE
217 
218 typedef struct coalition_pend_token {
219 	uint32_t        cpt_update_timers      :1,
220 	    cpt_update_j_coal_tasks :1;
221 } *coalition_pend_token_t;
222 
223 #endif /* MACH_KERNEL_PRIVATE */
224 
225 #endif /* _MACH_COALITION_H_ */
226