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 4
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_reserved            :17;
77 };
78 
79 struct task_effective_policy {
80 	uint64_t        tep_darwinbg            :1, /* marked as 'background', and sockets are marked bg when created */
81 	    tep_lowpri_cpu          :1,             /* cpu priority == MAXPRI_THROTTLE */
82 	    tep_io_tier             :2,             /* effective throttle tier */
83 	    tep_io_passive          :1,             /* should IOs cause lower tiers to be throttled */
84 	    tep_all_sockets_bg      :1,             /* All existing sockets in process are marked as bg (thread: all created by thread) */
85 	    tep_new_sockets_bg      :1,             /* Newly created sockets should be marked as bg */
86 	    tep_bg_iotier           :2,             /* What throttle tier should I be in when darwinbg is set? */
87 	    tep_terminated          :1,             /* all throttles have been removed for quick exit or SIGTERM handling */
88 	    tep_qos_ui_is_urgent    :1,             /* bump UI-Interactive QoS up to the urgent preemption band */
89 	    tep_latency_qos         :3,             /* Timer latency QoS level */
90 	    tep_through_qos         :3,             /* Computation throughput QoS level */
91 
92 	    tep_tal_engaged         :1,             /* TAL mode is in effect */
93 	    tep_watchers_bg         :1,             /* watchers are BG-ed */
94 	    tep_sup_active          :1,             /* suppression behaviors are in effect */
95 	    tep_role                :4,             /* task's system role */
96 	    tep_suppressed_cpu      :1,             /* cpu priority == MAXPRI_SUPPRESSED (trumped by lowpri_cpu) */
97 	    tep_sfi_managed         :1,             /* SFI Managed task */
98 	    tep_live_donor          :1,             /* task is a live importance boost donor */
99 	    tep_qos_clamp           :3,             /* task qos clamp (applies to qos-disabled threads too) */
100 	    tep_qos_ceiling         :3,             /* task qos ceiling (applies to only qos-participating threads) */
101 	    tep_adaptive_bg         :1,             /* task is bg solely due to the adaptive daemon clamp */
102 	    tep_coalition_bg        :1,             /* task is bg due to coalition suppresssion */
103 
104 	    tep_reserved            :29;
105 };
106 
107 /*
108  * Control structure for applying suppression behaviors to tasks
109  */
110 struct task_suppression_policy {
111 	integer_t active;
112 	integer_t lowpri_cpu; /* priority MAXPRI_THROTTLE cpu */
113 	integer_t timer_throttle;
114 	integer_t disk_throttle;
115 	integer_t cpu_limit;
116 	integer_t suspend;
117 	integer_t throughput_qos;
118 	integer_t suppressed_cpu; /* priority MAXPRI_SUPPRESSED cpu */
119 	integer_t background_sockets;
120 	integer_t reserved[7];
121 };
122 
123 typedef struct task_suppression_policy *task_suppression_policy_t;
124 
125 #define TASK_SUPPRESSION_POLICY_COUNT   ((mach_msg_type_number_t) \
126     (sizeof (struct task_suppression_policy) / sizeof (integer_t)))
127 
128 struct task_policy_state {
129 	uint64_t requested;
130 	uint64_t effective;
131 	uint64_t pending;
132 	uint32_t imp_assertcnt;
133 	uint32_t imp_externcnt;
134 	uint64_t flags;
135 	uint64_t imp_transitions;
136 	uint64_t tps_requested_policy;
137 	uint64_t tps_effective_policy;
138 };
139 
140 typedef struct task_policy_state *task_policy_state_t;
141 
142 #define TASK_POLICY_STATE_COUNT ((mach_msg_type_number_t) \
143     (sizeof (struct task_policy_state) / sizeof (integer_t)))
144 
145 
146 /*
147  * Definitions for munging and unmunging a policy struct
148  * Used in task_policy_state and in tracepoints
149  *
150  * Note: this is tightly bound to the implementation of task policy
151  * and the values exported through this API may change or change meaning at any time
152  *
153  * Do not rely on these values, especially apptype, to decide behaviors at runtime.
154  *
155  * All per-thread state must be in the first 32 bits of the bitfield.
156  */
157 
158 #define TASK_APPTYPE_NONE                0
159 #define TASK_APPTYPE_DAEMON_INTERACTIVE  1
160 #define TASK_APPTYPE_DAEMON_STANDARD     2
161 #define TASK_APPTYPE_DAEMON_ADAPTIVE     3
162 #define TASK_APPTYPE_DAEMON_BACKGROUND   4
163 #define TASK_APPTYPE_APP_DEFAULT         5
164 #define TASK_APPTYPE_APP_TAL             6 /* unused */
165 #define TASK_APPTYPE_DRIVER              7
166 
167 /* task policy state flags */
168 #define TASK_IMP_RECEIVER                    0x00000001
169 #define TASK_IMP_DONOR                       0x00000002
170 #define TASK_IMP_LIVE_DONOR                  0x00000004
171 #define TASK_DENAP_RECEIVER                  0x00000008
172 #define TASK_IS_PIDSUSPENDED                 0x00000010
173 
174 /* requested_policy */
175 #define POLICY_REQ_INT_DARWIN_BG             0x00000001
176 #define POLICY_REQ_EXT_DARWIN_BG             0x00000002
177 #define POLICY_REQ_INT_IO_TIER_MASK          0x0000000c /* 2 bits */
178 #define POLICY_REQ_INT_IO_TIER_SHIFT         2
179 #define POLICY_REQ_EXT_IO_TIER_MASK          0x00000030 /* 2 bits */
180 #define POLICY_REQ_EXT_IO_TIER_SHIFT         4
181 #define POLICY_REQ_INT_PASSIVE_IO            0x00000040
182 #define POLICY_REQ_EXT_PASSIVE_IO            0x00000080
183 #define POLICY_REQ_BG_IOTIER_MASK            0x00000300 /* 2 bits */
184 #define POLICY_REQ_BG_IOTIER_SHIFT           8
185 
186 /* thread requested policy */
187 #define POLICY_REQ_PIDBIND_BG                0x00000400
188 #define POLICY_REQ_WORKQ_BG                  0x00000800 /* deprecated */
189 #define POLICY_REQ_TH_QOS_MASK               0x07000000 /* 3 bits (overlaps with ROLE) */
190 #define POLICY_REQ_TH_QOS_SHIFT              24
191 #define POLICY_REQ_TH_QOS_OVER_MASK          0x70000000 /* 3 bits (overlaps with TAL and SFI) */
192 #define POLICY_REQ_TH_QOS_OVER_SHIFT         28
193 
194 /* task_requested_policy */
195 #define POLICY_REQ_TERMINATED                0x00001000
196 #define POLICY_REQ_BOOSTED                   0x00002000
197 #define POLICY_REQ_INT_GPU_DENY              0x00004000
198 #define POLICY_REQ_EXT_GPU_DENY              0x00008000
199 #define POLICY_REQ_APPTYPE_MASK              0x00070000 /* 3 bits */
200 #define POLICY_REQ_APPTYPE_SHIFT             16
201 #define POLICY_REQ_BASE_LATENCY_QOS_MASK     0x00700000 /* 3 bits */
202 #define POLICY_REQ_BASE_LATENCY_QOS_SHIFT    20
203 #define POLICY_REQ_ROLE_MASK                 0x07000000 /* 3 bits */
204 #define POLICY_REQ_ROLE_SHIFT                24
205 #define POLICY_REQ_TAL_ENABLED               0x40000000
206 #define POLICY_REQ_SFI_MANAGED               0x80000000
207 
208 /* requested suppression behaviors (note: clipped off in 32-bit tracepoints) */
209 #define POLICY_REQ_SUP_ACTIVE                0x0000000100000000
210 #define POLICY_REQ_SUP_LOWPRI_CPU            0x0000000200000000
211 #define POLICY_REQ_SUP_CPU                   0x0000000400000000
212 #define POLICY_REQ_SUP_DISK_THROTTLE         0x0000003000000000 /* 2 bits */
213 #define POLICY_REQ_SUP_CPU_LIMIT             0x0000004000000000
214 #define POLICY_REQ_SUP_SUSPEND               0x0000008000000000
215 #define POLICY_REQ_OVER_LATENCY_QOS_MASK     0x0000070000000000 /* 3 bits */
216 #define POLICY_REQ_OVER_LATENCY_QOS_SHIFT    40
217 #define POLICY_REQ_BASE_THROUGH_QOS_MASK     0x0000700000000000 /* 3 bits */
218 #define POLICY_REQ_BASE_THROUGH_QOS_SHIFT    44
219 #define POLICY_REQ_OVER_THROUGH_QOS_MASK     0x0007000000000000 /* 3 bits */
220 #define POLICY_REQ_OVER_THROUGH_QOS_SHIFT    48
221 #define POLICY_REQ_SUP_TIMER_THROTTLE_MASK   0x0070000000000000 /* 3 bits */
222 #define POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT  52
223 #define POLICY_REQ_SUP_THROUGHPUT_MASK       0x0700000000000000 /* 3 bits */
224 #define POLICY_REQ_SUP_THROUGHPUT_SHIFT      56
225 #define POLICY_REQ_SUP_BG_SOCKETS            0x0800008000000000
226 #define POLICY_REQ_QOS_CLAMP_MASK            0x7000000000000000 /* 3 bits */
227 #define POLICY_REQ_QOS_CLAMP_SHIFT           60
228 
229 /* effective policy */
230 #define POLICY_EFF_IO_TIER_MASK              0x00000003 /* 2 bits */
231 #define POLICY_EFF_IO_TIER_SHIFT             0
232 #define POLICY_EFF_IO_PASSIVE                0x00000008
233 #define POLICY_EFF_DARWIN_BG                 0x00000010
234 #define POLICY_EFF_LOWPRI_CPU                0x00000020
235 #define POLICY_EFF_ALL_SOCKETS_BG            0x00000040
236 #define POLICY_EFF_NEW_SOCKETS_BG            0x00000080
237 #define POLICY_EFF_BG_IOTIER_MASK            0x00000300 /* 2 bits */
238 #define POLICY_EFF_BG_IOTIER_SHIFT           8
239 #define POLICY_EFF_TERMINATED                0x00000400
240 #define POLICY_EFF_QOS_UI_IS_URGENT          0x80000000
241 
242 /* thread effective policy */
243 #define POLICY_EFF_TH_QOS_MASK               0x00700000 /* 3 bits (overlaps with ROLE) */
244 #define POLICY_EFF_TH_QOS_SHIFT              20
245 #define POLICY_EFF_LATENCY_QOS_MASK          0x00070000 /* 3 bits */
246 #define POLICY_EFF_LATENCY_QOS_SHIFT         16
247 #define POLICY_EFF_THROUGH_QOS_MASK          0x07000000 /* 3 bits */
248 #define POLICY_EFF_THROUGH_QOS_SHIFT         24
249 
250 /* task effective policy */
251 #define POLICY_EFF_GPU_DENY                  0x00001000
252 #define POLICY_EFF_TAL_ENGAGED               0x00002000
253 #define POLICY_EFF_SUSPENDED                 0x00004000
254 #define POLICY_EFF_WATCHERS_BG               0x00008000
255 #define POLICY_EFF_SUP_ACTIVE                0x00080000
256 #define POLICY_EFF_ROLE_MASK                 0x00700000 /* 3 bits */
257 #define POLICY_EFF_ROLE_SHIFT                20
258 #define POLICY_EFF_SUP_CPU                   0x00800000
259 #define POLICY_EFF_SFI_MANAGED               0x08000000
260 #define POLICY_EFF_QOS_CEILING_MASK          0x70000000
261 #define POLICY_EFF_QOS_CEILING_SHIFT         28
262 
263 /* pending policy */
264 #define POLICY_PEND_UPDATING                 0x00000001
265 #define POLICY_PEND_SOCKETS                  0x00000002
266 #define POLICY_PEND_TIMERS                   0x00000004
267 #define POLICY_PEND_WATCHERS                 0x00000008
268 
269 #ifdef MACH_KERNEL_PRIVATE
270 
271 extern const struct task_requested_policy default_task_requested_policy;
272 extern const struct task_effective_policy default_task_effective_policy;
273 
274 extern kern_return_t
275     qos_latency_policy_validate(task_latency_qos_t);
276 
277 extern kern_return_t
278     qos_throughput_policy_validate(task_throughput_qos_t);
279 
280 extern uint32_t
281     qos_extract(uint32_t);
282 
283 extern uint32_t
284     qos_latency_policy_package(uint32_t);
285 extern uint32_t
286     qos_throughput_policy_package(uint32_t);
287 
288 #endif /* MACH_KERNEL_PRIVATE */
289 
290 #endif  /* _MACH_TASK_POLICY_PRIVATE_H_ */
291