xref: /xnu-12377.41.6/osfmk/mach/task_policy_private.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2000-2005 Apple Computer, 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_TASK_POLICY_PRIVATE_H_
30 #define _MACH_TASK_POLICY_PRIVATE_H_
31 
32 #include <mach/mach_types.h>
33 #include <mach/task_policy.h>
34 
35 /*
36  * Internal bitfields are privately exported for *revlocked*
37  * tools like msa to decode tracepoints and taskinfo to dump state
38  *
39  * These struct definitions *will* change in the future.
40  * When they do, we will update TASK_POLICY_INTERNAL_STRUCT_VERSION.
41  */
42 
43 #define TASK_POLICY_INTERNAL_STRUCT_VERSION 5
44 
45 #define trp_tal_enabled trp_reserved  /*  trp_tal_enabled is unused, reuse its slot to grow trp_role */
46 
47 struct task_requested_policy {
48 	uint64_t        trp_int_darwinbg        :1, /* marked as darwinbg via setpriority */
49 	    trp_ext_darwinbg        :1,
50 	    trp_int_iotier          :2,             /* IO throttle tier */
51 	    trp_ext_iotier          :2,
52 	    trp_int_iopassive       :1,             /* should IOs cause lower tiers to be throttled */
53 	    trp_ext_iopassive       :1,
54 	    trp_bg_iotier           :2,             /* what IO throttle tier should apply to me when I'm darwinbg? (pushed to threads) */
55 	    trp_terminated          :1,             /* all throttles should be removed for quick exit or SIGTERM handling */
56 	    trp_base_latency_qos    :3,             /* Timer latency QoS */
57 	    trp_base_through_qos    :3,             /* Computation throughput QoS */
58 
59 	    trp_apptype             :3,             /* What apptype did launchd tell us this was (inherited) */
60 	    trp_boosted             :1,             /* Has a non-zero importance assertion count */
61 	    trp_role                :5,             /* task's system role */
62 	    trp_over_latency_qos    :3,             /* Timer latency QoS override */
63 	    trp_over_through_qos    :3,             /* Computation throughput QoS override */
64 	    trp_sfi_managed         :1,             /* SFI Managed task */
65 	    trp_qos_clamp           :3,             /* task qos clamp */
66 
67 	/* suppression policies (non-embedded only) */
68 	    trp_sup_active          :1,             /* Suppression is on */
69 	    trp_sup_lowpri_cpu      :1,             /* Wants low priority CPU (MAXPRI_THROTTLE) */
70 	    trp_sup_timer           :3,             /* Wanted timer throttling QoS tier */
71 	    trp_sup_disk            :1,             /* Wants disk throttling */
72 	    trp_sup_throughput      :3,             /* Wants throughput QoS tier */
73 	    trp_sup_cpu             :1,             /* Wants suppressed CPU priority (MAXPRI_SUPPRESSED) */
74 	    trp_sup_bg_sockets      :1,             /* Wants background sockets */
75 
76 	    trp_runaway_mitigation  :1,             /* Is runaway-mitigated */
77 
78 	    trp_reserved            :16;
79 };
80 
81 struct task_effective_policy {
82 	uint64_t        tep_darwinbg            :1, /* marked as 'background', and sockets are marked bg when created */
83 	    tep_lowpri_cpu          :1,             /* cpu priority == MAXPRI_THROTTLE */
84 	    tep_io_tier             :2,             /* effective throttle tier */
85 	    tep_io_passive          :1,             /* should IOs cause lower tiers to be throttled */
86 	    tep_all_sockets_bg      :1,             /* All existing sockets in process are marked as bg (thread: all created by thread) */
87 	    tep_new_sockets_bg      :1,             /* Newly created sockets should be marked as bg */
88 	    tep_bg_iotier           :2,             /* What throttle tier should I be in when darwinbg is set? */
89 	    tep_terminated          :1,             /* all throttles have been removed for quick exit or SIGTERM handling */
90 	    tep_qos_ui_is_urgent    :1,             /* bump UI-Interactive QoS up to the urgent preemption band */
91 	    tep_latency_qos         :3,             /* Timer latency QoS level */
92 	    tep_through_qos         :3,             /* Computation throughput QoS level */
93 
94 	    tep_tal_engaged         :1,             /* TAL mode is in effect */
95 	    tep_watchers_bg         :1,             /* watchers are BG-ed */
96 	    tep_sup_active          :1,             /* suppression behaviors are in effect */
97 	    tep_role                :4,             /* task's system role */
98 	    tep_suppressed_cpu      :1,             /* cpu priority == MAXPRI_SUPPRESSED (trumped by lowpri_cpu) */
99 	    tep_sfi_managed         :1,             /* SFI Managed task */
100 	    tep_live_donor          :1,             /* task is a live importance boost donor */
101 	    tep_qos_clamp           :3,             /* task qos clamp (applies to qos-disabled threads too) */
102 	    tep_qos_ceiling         :3,             /* task qos ceiling (applies to only qos-participating threads) */
103 	    tep_promote_above_task  :1,             /* task should allow turnstiles to boost above BG clamp */
104 	    tep_coalition_bg        :1,             /* task is bg due to coalition suppresssion */
105 	    tep_runaway_mitigation  :1,             /* task is bg due to runaway-mitigation */
106 
107 	    tep_reserved            :28;
108 };
109 
110 /*
111  * Control structure for applying suppression behaviors to tasks
112  */
113 struct task_suppression_policy {
114 	integer_t active;
115 	integer_t lowpri_cpu; /* priority MAXPRI_THROTTLE cpu */
116 	integer_t timer_throttle;
117 	integer_t disk_throttle;
118 	integer_t cpu_limit;
119 	integer_t suspend;
120 	integer_t throughput_qos;
121 	integer_t suppressed_cpu; /* priority MAXPRI_SUPPRESSED cpu */
122 	integer_t background_sockets;
123 	integer_t reserved[7];
124 };
125 
126 typedef struct task_suppression_policy *task_suppression_policy_t;
127 
128 #define TASK_SUPPRESSION_POLICY_COUNT   ((mach_msg_type_number_t) \
129     (sizeof (struct task_suppression_policy) / sizeof (integer_t)))
130 
131 struct task_policy_state {
132 	uint64_t requested;
133 	uint64_t effective;
134 	uint64_t pending;
135 	uint32_t imp_assertcnt;
136 	uint32_t imp_externcnt;
137 	uint64_t flags;
138 	uint64_t imp_transitions;
139 	uint64_t tps_requested_policy;
140 	uint64_t tps_effective_policy;
141 };
142 
143 typedef struct task_policy_state *task_policy_state_t;
144 
145 #define TASK_POLICY_STATE_COUNT ((mach_msg_type_number_t) \
146     (sizeof (struct task_policy_state) / sizeof (integer_t)))
147 
148 
149 /*
150  * Definitions for munging and unmunging a policy struct
151  * Used in task_policy_state and in tracepoints
152  *
153  * Note: this is tightly bound to the implementation of task policy
154  * and the values exported through this API may change or change meaning at any time
155  *
156  * Do not rely on these values, especially apptype, to decide behaviors at runtime.
157  *
158  * All per-thread state must be in the first 32 bits of the bitfield.
159  */
160 
161 #define TASK_APPTYPE_NONE                0
162 #define TASK_APPTYPE_DAEMON_INTERACTIVE  1
163 #define TASK_APPTYPE_DAEMON_STANDARD     2
164 #define TASK_APPTYPE_DAEMON_ADAPTIVE     3
165 #define TASK_APPTYPE_DAEMON_BACKGROUND   4
166 #define TASK_APPTYPE_APP_DEFAULT         5
167 #define TASK_APPTYPE_APP_NONUI           6
168 #define TASK_APPTYPE_APP_TAL             TASK_APPTYPE_APP_NONUI /* unused */
169 #define TASK_APPTYPE_DRIVER              7
170 
171 /* task policy state flags */
172 #define TASK_IMP_RECEIVER                    0x00000001
173 #define TASK_IMP_DONOR                       0x00000002
174 #define TASK_IMP_LIVE_DONOR                  0x00000004
175 #define TASK_DENAP_RECEIVER                  0x00000008
176 #define TASK_IS_PIDSUSPENDED                 0x00000010
177 
178 /* requested_policy */
179 #define POLICY_REQ_INT_DARWIN_BG             0x00000001
180 #define POLICY_REQ_EXT_DARWIN_BG             0x00000002
181 #define POLICY_REQ_INT_IO_TIER_MASK          0x0000000c /* 2 bits */
182 #define POLICY_REQ_INT_IO_TIER_SHIFT         2
183 #define POLICY_REQ_EXT_IO_TIER_MASK          0x00000030 /* 2 bits */
184 #define POLICY_REQ_EXT_IO_TIER_SHIFT         4
185 #define POLICY_REQ_INT_PASSIVE_IO            0x00000040
186 #define POLICY_REQ_EXT_PASSIVE_IO            0x00000080
187 #define POLICY_REQ_BG_IOTIER_MASK            0x00000300 /* 2 bits */
188 #define POLICY_REQ_BG_IOTIER_SHIFT           8
189 
190 /* thread requested policy */
191 #define POLICY_REQ_PIDBIND_BG                0x00000400
192 #define POLICY_REQ_WORKQ_BG                  0x00000800 /* deprecated */
193 #define POLICY_REQ_TH_QOS_MASK               0x07000000 /* 3 bits (overlaps with ROLE) */
194 #define POLICY_REQ_TH_QOS_SHIFT              24
195 #define POLICY_REQ_TH_QOS_OVER_MASK          0x70000000 /* 3 bits (overlaps with TAL and SFI) */
196 #define POLICY_REQ_TH_QOS_OVER_SHIFT         28
197 
198 /* task_requested_policy */
199 #define POLICY_REQ_TERMINATED                0x00001000
200 #define POLICY_REQ_BOOSTED                   0x00002000
201 #define POLICY_REQ_INT_GPU_DENY              0x00004000
202 #define POLICY_REQ_EXT_GPU_DENY              0x00008000
203 #define POLICY_REQ_APPTYPE_MASK              0x00070000 /* 3 bits */
204 #define POLICY_REQ_APPTYPE_SHIFT             16
205 #define POLICY_REQ_BASE_LATENCY_QOS_MASK     0x00700000 /* 3 bits */
206 #define POLICY_REQ_BASE_LATENCY_QOS_SHIFT    20
207 #define POLICY_REQ_ROLE_MASK                 0x07000000 /* 3 bits */
208 #define POLICY_REQ_ROLE_SHIFT                24
209 #define POLICY_REQ_TAL_ENABLED               0x40000000
210 #define POLICY_REQ_SFI_MANAGED               0x80000000
211 
212 /* requested suppression behaviors (note: clipped off in 32-bit tracepoints) */
213 #define POLICY_REQ_SUP_ACTIVE                0x0000000100000000
214 #define POLICY_REQ_SUP_LOWPRI_CPU            0x0000000200000000
215 #define POLICY_REQ_SUP_CPU                   0x0000000400000000
216 #define POLICY_REQ_SUP_DISK_THROTTLE         0x0000003000000000 /* 2 bits */
217 #define POLICY_REQ_SUP_CPU_LIMIT             0x0000004000000000
218 #define POLICY_REQ_SUP_SUSPEND               0x0000008000000000
219 #define POLICY_REQ_OVER_LATENCY_QOS_MASK     0x0000070000000000 /* 3 bits */
220 #define POLICY_REQ_OVER_LATENCY_QOS_SHIFT    40
221 #define POLICY_REQ_BASE_THROUGH_QOS_MASK     0x0000700000000000 /* 3 bits */
222 #define POLICY_REQ_BASE_THROUGH_QOS_SHIFT    44
223 #define POLICY_REQ_OVER_THROUGH_QOS_MASK     0x0007000000000000 /* 3 bits */
224 #define POLICY_REQ_OVER_THROUGH_QOS_SHIFT    48
225 #define POLICY_REQ_SUP_TIMER_THROTTLE_MASK   0x0070000000000000 /* 3 bits */
226 #define POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT  52
227 #define POLICY_REQ_SUP_THROUGHPUT_MASK       0x0700000000000000 /* 3 bits */
228 #define POLICY_REQ_SUP_THROUGHPUT_SHIFT      56
229 #define POLICY_REQ_SUP_BG_SOCKETS            0x0800008000000000
230 #define POLICY_REQ_QOS_CLAMP_MASK            0x7000000000000000 /* 3 bits */
231 #define POLICY_REQ_QOS_CLAMP_SHIFT           60
232 
233 /* effective policy */
234 #define POLICY_EFF_IO_TIER_MASK              0x00000003 /* 2 bits */
235 #define POLICY_EFF_IO_TIER_SHIFT             0
236 #define POLICY_EFF_IO_PASSIVE                0x00000008
237 #define POLICY_EFF_DARWIN_BG                 0x00000010
238 #define POLICY_EFF_LOWPRI_CPU                0x00000020
239 #define POLICY_EFF_ALL_SOCKETS_BG            0x00000040
240 #define POLICY_EFF_NEW_SOCKETS_BG            0x00000080
241 #define POLICY_EFF_BG_IOTIER_MASK            0x00000300 /* 2 bits */
242 #define POLICY_EFF_BG_IOTIER_SHIFT           8
243 #define POLICY_EFF_TERMINATED                0x00000400
244 #define POLICY_EFF_QOS_UI_IS_URGENT          0x80000000
245 
246 /* thread effective policy */
247 #define POLICY_EFF_TH_QOS_MASK               0x00700000 /* 3 bits (overlaps with ROLE) */
248 #define POLICY_EFF_TH_QOS_SHIFT              20
249 #define POLICY_EFF_LATENCY_QOS_MASK          0x00070000 /* 3 bits */
250 #define POLICY_EFF_LATENCY_QOS_SHIFT         16
251 #define POLICY_EFF_THROUGH_QOS_MASK          0x07000000 /* 3 bits */
252 #define POLICY_EFF_THROUGH_QOS_SHIFT         24
253 
254 /* task effective policy */
255 #define POLICY_EFF_GPU_DENY                  0x00001000
256 #define POLICY_EFF_TAL_ENGAGED               0x00002000
257 #define POLICY_EFF_SUSPENDED                 0x00004000
258 #define POLICY_EFF_WATCHERS_BG               0x00008000
259 #define POLICY_EFF_SUP_ACTIVE                0x00080000
260 #define POLICY_EFF_ROLE_MASK                 0x00700000 /* 3 bits */
261 #define POLICY_EFF_ROLE_SHIFT                20
262 #define POLICY_EFF_SUP_CPU                   0x00800000
263 #define POLICY_EFF_SFI_MANAGED               0x08000000
264 #define POLICY_EFF_QOS_CEILING_MASK          0x70000000
265 #define POLICY_EFF_QOS_CEILING_SHIFT         28
266 
267 /* pending policy */
268 #define POLICY_PEND_UPDATING                 0x00000001
269 #define POLICY_PEND_SOCKETS                  0x00000002
270 #define POLICY_PEND_TIMERS                   0x00000004
271 #define POLICY_PEND_WATCHERS                 0x00000008
272 
273 #ifdef MACH_KERNEL_PRIVATE
274 
275 extern const struct task_requested_policy default_task_requested_policy;
276 extern const struct task_effective_policy default_task_effective_policy;
277 
278 extern kern_return_t
279     qos_latency_policy_validate(task_latency_qos_t);
280 
281 extern kern_return_t
282     qos_throughput_policy_validate(task_throughput_qos_t);
283 
284 extern uint32_t
285     qos_extract(uint32_t);
286 
287 extern uint32_t
288     qos_latency_policy_package(uint32_t);
289 extern uint32_t
290     qos_throughput_policy_package(uint32_t);
291 
292 #endif /* MACH_KERNEL_PRIVATE */
293 
294 #endif  /* _MACH_TASK_POLICY_PRIVATE_H_ */
295