2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _MACH_THREAD_POLICY_H_
30 #define _MACH_THREAD_POLICY_H_
32 #include <mach/mach_types.h>
35 * These are the calls for accessing the policy parameters
36 * of a particular thread.
38 * The extra 'get_default' parameter to the second call is
40 * 1) if asserted on the way in it indicates that the default
41 * values should be returned, not the ones currently set, in
42 * this case 'get_default' will always be asserted on return;
43 * 2) if unasserted on the way in, the current settings are
44 * desired and if still unasserted on return, then the info
45 * returned reflects the current settings, otherwise if
46 * 'get_default' returns asserted, it means that there are no
47 * current settings due to other parameters taking precedence,
48 * and the default ones are being returned instead.
51 typedef natural_t thread_policy_flavor_t
;
52 typedef integer_t
*thread_policy_t
;
55 kern_return_t thread_policy_set(
57 thread_policy_flavor_t flavor,
58 thread_policy_t policy_info,
59 mach_msg_type_number_t count);
61 kern_return_t thread_policy_get(
63 thread_policy_flavor_t flavor,
64 thread_policy_t policy_info,
65 mach_msg_type_number_t *count,
66 boolean_t *get_default);
73 * THREAD_STANDARD_POLICY:
75 * This is the standard (fair) scheduling mode, assigned to new
76 * threads. The thread will be given processor time in a manner
77 * which apportions approximately equal share to long running
84 #define THREAD_STANDARD_POLICY 1
86 struct thread_standard_policy
{
90 typedef struct thread_standard_policy thread_standard_policy_data_t
;
91 typedef struct thread_standard_policy
*thread_standard_policy_t
;
93 #define THREAD_STANDARD_POLICY_COUNT 0
96 * THREAD_EXTENDED_POLICY:
98 * Extended form of THREAD_STANDARD_POLICY, which supplies a
99 * hint indicating whether this is a long running computation.
103 * timeshare: TRUE (the default) results in identical scheduling
104 * behavior as THREAD_STANDARD_POLICY.
107 #define THREAD_EXTENDED_POLICY 1
109 struct thread_extended_policy
{
113 typedef struct thread_extended_policy thread_extended_policy_data_t
;
114 typedef struct thread_extended_policy
*thread_extended_policy_t
;
116 #define THREAD_EXTENDED_POLICY_COUNT ((mach_msg_type_number_t) \
117 (sizeof (thread_extended_policy_data_t) / sizeof (integer_t)))
120 * THREAD_TIME_CONSTRAINT_POLICY:
122 * This scheduling mode is for threads which have real time
123 * constraints on their execution.
127 * period: This is the nominal amount of time between separate
128 * processing arrivals, specified in absolute time units. A
129 * value of 0 indicates that there is no inherent periodicity in
132 * computation: This is the nominal amount of computation
133 * time needed during a separate processing arrival, specified
134 * in absolute time units.
136 * constraint: This is the maximum amount of real time that
137 * may elapse from the start of a separate processing arrival
138 * to the end of computation for logically correct functioning,
139 * specified in absolute time units. Must be (>= computation).
140 * Note that latency = (constraint - computation).
142 * preemptible: This indicates that the computation may be
143 * interrupted, subject to the constraint specified above.
146 #define THREAD_TIME_CONSTRAINT_POLICY 2
148 struct thread_time_constraint_policy
{
150 uint32_t computation
;
152 boolean_t preemptible
;
155 typedef struct thread_time_constraint_policy \
156 thread_time_constraint_policy_data_t
;
157 typedef struct thread_time_constraint_policy \
158 *thread_time_constraint_policy_t
;
160 #define THREAD_TIME_CONSTRAINT_POLICY_COUNT ((mach_msg_type_number_t) \
161 (sizeof (thread_time_constraint_policy_data_t) / sizeof (integer_t)))
164 * THREAD_PRECEDENCE_POLICY:
166 * This may be used to indicate the relative value of the
167 * computation compared to the other threads in the task.
171 * importance: The importance is specified as a signed value.
174 #define THREAD_PRECEDENCE_POLICY 3
176 struct thread_precedence_policy
{
177 integer_t importance
;
180 typedef struct thread_precedence_policy thread_precedence_policy_data_t
;
181 typedef struct thread_precedence_policy
*thread_precedence_policy_t
;
183 #define THREAD_PRECEDENCE_POLICY_COUNT ((mach_msg_type_number_t) \
184 (sizeof (thread_precedence_policy_data_t) / sizeof (integer_t)))
187 * THREAD_AFFINITY_POLICY:
189 * This policy is experimental.
190 * This may be used to express affinity relationships
191 * between threads in the task. Threads with the same affinity tag will
192 * be scheduled to share an L2 cache if possible. That is, affinity tags
193 * are a hint to the scheduler for thread placement.
195 * The namespace of affinity tags is generally local to one task. However,
196 * a child task created after the assignment of affinity tags by its parent
197 * will share that namespace. In particular, a family of forked processes
198 * may be created with a shared affinity namespace.
201 * tag: The affinity set identifier.
204 #define THREAD_AFFINITY_POLICY 4
206 struct thread_affinity_policy
{
207 integer_t affinity_tag
;
210 #define THREAD_AFFINITY_TAG_NULL 0
212 typedef struct thread_affinity_policy thread_affinity_policy_data_t
;
213 typedef struct thread_affinity_policy
*thread_affinity_policy_t
;
215 #define THREAD_AFFINITY_POLICY_COUNT ((mach_msg_type_number_t) \
216 (sizeof (thread_affinity_policy_data_t) / sizeof (integer_t)))
219 * THREAD_BACKGROUND_POLICY:
222 #define THREAD_BACKGROUND_POLICY 5
224 struct thread_background_policy
{
228 typedef struct thread_background_policy thread_background_policy_data_t
;
229 typedef struct thread_background_policy
*thread_background_policy_t
;
231 #define THREAD_BACKGROUND_POLICY_COUNT ((mach_msg_type_number_t) \
232 (sizeof (thread_background_policy_data_t) / sizeof (integer_t)))
235 #define THREAD_LATENCY_QOS_POLICY 7
236 typedef integer_t thread_latency_qos_t
;
238 struct thread_latency_qos_policy
{
239 thread_latency_qos_t thread_latency_qos_tier
;
242 typedef struct thread_latency_qos_policy thread_latency_qos_policy_data_t
;
243 typedef struct thread_latency_qos_policy
*thread_latency_qos_policy_t
;
245 #define THREAD_LATENCY_QOS_POLICY_COUNT ((mach_msg_type_number_t) \
246 (sizeof (thread_latency_qos_policy_data_t) / sizeof (integer_t)))
248 #define THREAD_THROUGHPUT_QOS_POLICY 8
249 typedef integer_t thread_throughput_qos_t
;
251 struct thread_throughput_qos_policy
{
252 thread_throughput_qos_t thread_throughput_qos_tier
;
255 typedef struct thread_throughput_qos_policy thread_throughput_qos_policy_data_t
;
256 typedef struct thread_throughput_qos_policy
*thread_throughput_qos_policy_t
;
258 #define THREAD_THROUGHPUT_QOS_POLICY_COUNT ((mach_msg_type_number_t) \
259 (sizeof (thread_throughput_qos_policy_data_t) / sizeof (integer_t)))
264 * THREAD_POLICY_STATE:
266 #define THREAD_POLICY_STATE 6
268 #define THREAD_POLICY_STATE_FLAG_STATIC_PARAM 0x1
270 struct thread_policy_state
{
275 integer_t reserved
[12];
278 typedef struct thread_policy_state thread_policy_state_data_t
;
279 typedef struct thread_policy_state
*thread_policy_state_t
;
281 #define THREAD_POLICY_STATE_COUNT ((mach_msg_type_number_t) \
282 (sizeof (thread_policy_state_data_t) / sizeof (integer_t)))
287 #define THREAD_QOS_POLICY 9
288 #define THREAD_QOS_POLICY_OVERRIDE 10
290 #define THREAD_QOS_UNSPECIFIED 0
291 #define THREAD_QOS_DEFAULT THREAD_QOS_UNSPECIFIED /* Temporary rename */
292 #define THREAD_QOS_MAINTENANCE 1
293 #define THREAD_QOS_BACKGROUND 2
294 #define THREAD_QOS_UTILITY 3
295 #define THREAD_QOS_LEGACY 4 /* i.e. default workq threads */
296 #define THREAD_QOS_USER_INITIATED 5
297 #define THREAD_QOS_USER_INTERACTIVE 6
299 #define THREAD_QOS_LAST 7
301 #define THREAD_QOS_MIN_TIER_IMPORTANCE (-15)
304 * Overrides are inputs to the task/thread policy engine that
305 * temporarily elevate the effective QoS of a thread without changing
306 * its steady-state (and round-trip-able) requested QoS. The
307 * interfaces into the kernel allow the caller to associate a resource
308 * and type that describe the reason/lifecycle of the override. For
309 * instance, a contended pthread_mutex_t held by a UTILITY thread
310 * might get an override to USER_INTERACTIVE, with the resource
311 * being the userspace address of the pthread_mutex_t. When the
312 * owning thread releases that resource, it can call into the
313 * task policy subsystem to drop the override because of that resource,
314 * although if more contended locks are held by the thread, the
315 * effective QoS may remain overridden for longer.
317 * THREAD_QOS_OVERRIDE_TYPE_PTHREAD_MUTEX is used for contended
318 * pthread_mutex_t's via the pthread kext. The holder gets an override
319 * with resource=&mutex and a count of 1 by the initial contender.
320 * Subsequent contenders raise the QoS value, until the holder
321 * decrements the count to 0 and the override is released.
323 * THREAD_QOS_OVERRIDE_TYPE_PTHREAD_RWLOCK is unimplemented and has no
324 * specified semantics.
326 * THREAD_QOS_OVERRIDE_TYPE_PTHREAD_EXPLICIT_OVERRIDE are explicitly
327 * paired start/end overrides on a target thread. The resource can
328 * either be a memory allocation in userspace, or the pthread_t of the
329 * overrider if no allocation was used.
331 * THREAD_QOS_OVERRIDE_TYPE_DISPATCH_ASYNCHRONOUS_OVERRIDE are used to
332 * override the QoS of a thread currently draining a serial dispatch
333 * queue, so that it can get to a block of higher QoS than its
334 * predecessors. The override is applied by a thread enqueueing work
335 * with resource=&queue, and reset by the thread that was overriden
336 * once it has drained the queue. Since the ++ and reset are
337 * asynchronous, there is the possibility of a ++ after the target
338 * thread has issued a reset, in which case the workqueue thread may
339 * issue a reset-all in its outermost scope before deciding whether it
340 * should return to dequeueing work from the global concurrent queues,
341 * or return to the kernel.
344 #define THREAD_QOS_OVERRIDE_TYPE_UNKNOWN (0)
345 #define THREAD_QOS_OVERRIDE_TYPE_PTHREAD_MUTEX (1)
346 #define THREAD_QOS_OVERRIDE_TYPE_PTHREAD_RWLOCK (2)
347 #define THREAD_QOS_OVERRIDE_TYPE_PTHREAD_EXPLICIT_OVERRIDE (3)
348 #define THREAD_QOS_OVERRIDE_TYPE_DISPATCH_ASYNCHRONOUS_OVERRIDE (4)
350 /* A special resource value to indicate a resource wildcard */
351 #define THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD (~((user_addr_t)0))
353 struct thread_qos_policy
{
355 integer_t tier_importance
;
358 typedef struct thread_qos_policy thread_qos_policy_data_t
;
359 typedef struct thread_qos_policy
*thread_qos_policy_t
;
361 #define THREAD_QOS_POLICY_COUNT ((mach_msg_type_number_t) \
362 (sizeof (thread_qos_policy_data_t) / sizeof (integer_t)))
366 #endif /* _MACH_THREAD_POLICY_H_ */