2 * Copyright (c) 2000-2015 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 #include <mach/mach_types.h>
30 #include <mach/thread_act_server.h>
32 #include <kern/kern_types.h>
33 #include <kern/processor.h>
34 #include <kern/thread.h>
35 #include <kern/affinity.h>
36 #include <mach/task_policy.h>
38 #include <kern/policy_internal.h>
39 #include <sys/errno.h>
40 #include <sys/ulock.h>
42 #include <mach/machine/sdt.h>
45 extern int proc_selfpid(void);
46 extern char * proc_name_address(void *p
);
47 extern void rethrottle_thread(void * uthread
);
50 #define QOS_EXTRACT(q) ((q) & 0xff)
52 uint32_t qos_override_mode
;
53 #define QOS_OVERRIDE_MODE_OVERHANG_PEAK 0
54 #define QOS_OVERRIDE_MODE_IGNORE_OVERRIDE 1
55 #define QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE 2
56 #define QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_SINGLE_MUTEX_OVERRIDE 3
58 extern zone_t thread_qos_override_zone
;
61 proc_thread_qos_remove_override_internal(thread_t thread
, user_addr_t resource
, int resource_type
, boolean_t reset
);
64 * THREAD_QOS_UNSPECIFIED is assigned the highest tier available, so it does not provide a limit
65 * to threads that don't have a QoS class set.
67 const qos_policy_params_t thread_qos_policy_params
= {
69 * This table defines the starting base priority of the thread,
70 * which will be modified by the thread importance and the task max priority
71 * before being applied.
73 .qos_pri
[THREAD_QOS_UNSPECIFIED
] = 0, /* not consulted */
74 .qos_pri
[THREAD_QOS_USER_INTERACTIVE
] = BASEPRI_BACKGROUND
, /* i.e. 46 */
75 .qos_pri
[THREAD_QOS_USER_INITIATED
] = BASEPRI_USER_INITIATED
,
76 .qos_pri
[THREAD_QOS_LEGACY
] = BASEPRI_DEFAULT
,
77 .qos_pri
[THREAD_QOS_UTILITY
] = BASEPRI_UTILITY
,
78 .qos_pri
[THREAD_QOS_BACKGROUND
] = MAXPRI_THROTTLE
,
79 .qos_pri
[THREAD_QOS_MAINTENANCE
] = MAXPRI_THROTTLE
,
82 * This table defines the highest IO priority that a thread marked with this
85 .qos_iotier
[THREAD_QOS_UNSPECIFIED
] = THROTTLE_LEVEL_TIER0
,
86 .qos_iotier
[THREAD_QOS_USER_INTERACTIVE
] = THROTTLE_LEVEL_TIER0
,
87 .qos_iotier
[THREAD_QOS_USER_INITIATED
] = THROTTLE_LEVEL_TIER0
,
88 .qos_iotier
[THREAD_QOS_LEGACY
] = THROTTLE_LEVEL_TIER0
,
89 .qos_iotier
[THREAD_QOS_UTILITY
] = THROTTLE_LEVEL_TIER1
,
90 .qos_iotier
[THREAD_QOS_BACKGROUND
] = THROTTLE_LEVEL_TIER2
, /* possibly overridden by bg_iotier */
91 .qos_iotier
[THREAD_QOS_MAINTENANCE
] = THROTTLE_LEVEL_TIER3
,
94 * This table defines the highest QoS level that
95 * a thread marked with this QoS class can have.
98 .qos_through_qos
[THREAD_QOS_UNSPECIFIED
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_UNSPECIFIED
),
99 .qos_through_qos
[THREAD_QOS_USER_INTERACTIVE
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_0
),
100 .qos_through_qos
[THREAD_QOS_USER_INITIATED
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_1
),
101 .qos_through_qos
[THREAD_QOS_LEGACY
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_1
),
102 .qos_through_qos
[THREAD_QOS_UTILITY
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_2
),
103 .qos_through_qos
[THREAD_QOS_BACKGROUND
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_5
),
104 .qos_through_qos
[THREAD_QOS_MAINTENANCE
] = QOS_EXTRACT(THROUGHPUT_QOS_TIER_5
),
106 .qos_latency_qos
[THREAD_QOS_UNSPECIFIED
] = QOS_EXTRACT(LATENCY_QOS_TIER_UNSPECIFIED
),
107 .qos_latency_qos
[THREAD_QOS_USER_INTERACTIVE
] = QOS_EXTRACT(LATENCY_QOS_TIER_0
),
108 .qos_latency_qos
[THREAD_QOS_USER_INITIATED
] = QOS_EXTRACT(LATENCY_QOS_TIER_1
),
109 .qos_latency_qos
[THREAD_QOS_LEGACY
] = QOS_EXTRACT(LATENCY_QOS_TIER_1
),
110 .qos_latency_qos
[THREAD_QOS_UTILITY
] = QOS_EXTRACT(LATENCY_QOS_TIER_3
),
111 .qos_latency_qos
[THREAD_QOS_BACKGROUND
] = QOS_EXTRACT(LATENCY_QOS_TIER_3
),
112 .qos_latency_qos
[THREAD_QOS_MAINTENANCE
] = QOS_EXTRACT(LATENCY_QOS_TIER_3
),
116 thread_set_user_sched_mode_and_recompute_pri(thread_t thread
, sched_mode_t mode
);
119 thread_qos_scaled_relative_priority(int qos
, int qos_relprio
);
122 proc_get_thread_policy_bitfield(thread_t thread
, thread_policy_state_t info
);
125 proc_set_thread_policy_locked(thread_t thread
, int category
, int flavor
, int value
, int value2
, task_pend_token_t pend_token
);
128 proc_set_thread_policy_spinlocked(thread_t thread
, int category
, int flavor
, int value
, int value2
, task_pend_token_t pend_token
);
131 thread_set_requested_policy_spinlocked(thread_t thread
, int category
, int flavor
, int value
, int value2
, task_pend_token_t pend_token
);
134 thread_get_requested_policy_spinlocked(thread_t thread
, int category
, int flavor
, int* value2
);
137 proc_get_thread_policy_locked(thread_t thread
, int category
, int flavor
, int* value2
);
140 thread_policy_update_spinlocked(thread_t thread
, boolean_t recompute_priority
, task_pend_token_t pend_token
);
143 thread_policy_update_internal_spinlocked(thread_t thread
, boolean_t recompute_priority
, task_pend_token_t pend_token
);
146 thread_policy_init(void)
148 if (PE_parse_boot_argn("qos_override_mode", &qos_override_mode
, sizeof(qos_override_mode
))) {
149 printf("QOS override mode: 0x%08x\n", qos_override_mode
);
151 qos_override_mode
= QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_SINGLE_MUTEX_OVERRIDE
;
156 thread_has_qos_policy(thread_t thread
)
158 return (proc_get_thread_policy(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS
) != THREAD_QOS_UNSPECIFIED
) ? TRUE
: FALSE
;
163 thread_remove_qos_policy_locked(thread_t thread
,
164 task_pend_token_t pend_token
)
166 __unused
int prev_qos
= thread
->requested_policy
.thrp_qos
;
168 DTRACE_PROC2(qos__remove
, thread_t
, thread
, int, prev_qos
);
170 proc_set_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS_AND_RELPRIO
,
171 THREAD_QOS_UNSPECIFIED
, 0, pend_token
);
175 thread_remove_qos_policy(thread_t thread
)
177 struct task_pend_token pend_token
= {};
179 thread_mtx_lock(thread
);
180 if (!thread
->active
) {
181 thread_mtx_unlock(thread
);
182 return KERN_TERMINATED
;
185 thread_remove_qos_policy_locked(thread
, &pend_token
);
187 thread_mtx_unlock(thread
);
189 thread_policy_update_complete_unlocked(thread
, &pend_token
);
196 thread_is_static_param(thread_t thread
)
198 if (thread
->static_param
) {
199 DTRACE_PROC1(qos__legacy__denied
, thread_t
, thread
);
206 * Relative priorities can range between 0REL and -15REL. These
207 * map to QoS-specific ranges, to create non-overlapping priority
211 thread_qos_scaled_relative_priority(int qos
, int qos_relprio
)
215 /* Fast path, since no validation or scaling is needed */
216 if (qos_relprio
== 0) {
221 case THREAD_QOS_USER_INTERACTIVE
:
222 next_lower_qos
= THREAD_QOS_USER_INITIATED
;
224 case THREAD_QOS_USER_INITIATED
:
225 next_lower_qos
= THREAD_QOS_LEGACY
;
227 case THREAD_QOS_LEGACY
:
228 next_lower_qos
= THREAD_QOS_UTILITY
;
230 case THREAD_QOS_UTILITY
:
231 next_lower_qos
= THREAD_QOS_BACKGROUND
;
233 case THREAD_QOS_MAINTENANCE
:
234 case THREAD_QOS_BACKGROUND
:
238 panic("Unrecognized QoS %d", qos
);
242 int prio_range_max
= thread_qos_policy_params
.qos_pri
[qos
];
243 int prio_range_min
= next_lower_qos
? thread_qos_policy_params
.qos_pri
[next_lower_qos
] : 0;
246 * We now have the valid range that the scaled relative priority can map to. Note
247 * that the lower bound is exclusive, but the upper bound is inclusive. If the
248 * range is (21,31], 0REL should map to 31 and -15REL should map to 22. We use the
249 * fact that the max relative priority is -15 and use ">>4" to divide by 16 and discard
252 int scaled_relprio
= -(((prio_range_max
- prio_range_min
) * (-qos_relprio
)) >> 4);
254 return scaled_relprio
;
258 * flag set by -qos-policy-allow boot-arg to allow
259 * testing thread qos policy from userspace
261 boolean_t allow_qos_policy_set
= FALSE
;
266 thread_policy_flavor_t flavor
,
267 thread_policy_t policy_info
,
268 mach_msg_type_number_t count
)
270 thread_qos_policy_data_t req_qos
;
273 req_qos
.qos_tier
= THREAD_QOS_UNSPECIFIED
;
275 if (thread
== THREAD_NULL
) {
276 return KERN_INVALID_ARGUMENT
;
279 if (allow_qos_policy_set
== FALSE
) {
280 if (thread_is_static_param(thread
)) {
281 return KERN_POLICY_STATIC
;
284 if (flavor
== THREAD_QOS_POLICY
) {
285 return KERN_INVALID_ARGUMENT
;
289 /* Threads without static_param set reset their QoS when other policies are applied. */
290 if (thread
->requested_policy
.thrp_qos
!= THREAD_QOS_UNSPECIFIED
) {
291 /* Store the existing tier, if we fail this call it is used to reset back. */
292 req_qos
.qos_tier
= thread
->requested_policy
.thrp_qos
;
293 req_qos
.tier_importance
= thread
->requested_policy
.thrp_qos_relprio
;
295 kr
= thread_remove_qos_policy(thread
);
296 if (kr
!= KERN_SUCCESS
) {
301 kr
= thread_policy_set_internal(thread
, flavor
, policy_info
, count
);
303 /* Return KERN_QOS_REMOVED instead of KERN_SUCCESS if we succeeded. */
304 if (req_qos
.qos_tier
!= THREAD_QOS_UNSPECIFIED
) {
305 if (kr
!= KERN_SUCCESS
) {
306 /* Reset back to our original tier as the set failed. */
307 (void)thread_policy_set_internal(thread
, THREAD_QOS_POLICY
, (thread_policy_t
)&req_qos
, THREAD_QOS_POLICY_COUNT
);
315 thread_policy_set_internal(
317 thread_policy_flavor_t flavor
,
318 thread_policy_t policy_info
,
319 mach_msg_type_number_t count
)
321 kern_return_t result
= KERN_SUCCESS
;
322 struct task_pend_token pend_token
= {};
324 thread_mtx_lock(thread
);
325 if (!thread
->active
) {
326 thread_mtx_unlock(thread
);
328 return KERN_TERMINATED
;
332 case THREAD_EXTENDED_POLICY
:
334 boolean_t timeshare
= TRUE
;
336 if (count
>= THREAD_EXTENDED_POLICY_COUNT
) {
337 thread_extended_policy_t info
;
339 info
= (thread_extended_policy_t
)policy_info
;
340 timeshare
= info
->timeshare
;
343 sched_mode_t mode
= (timeshare
== TRUE
) ? TH_MODE_TIMESHARE
: TH_MODE_FIXED
;
345 spl_t s
= splsched();
348 thread_set_user_sched_mode_and_recompute_pri(thread
, mode
);
350 thread_unlock(thread
);
353 pend_token
.tpt_update_thread_sfi
= 1;
358 case THREAD_TIME_CONSTRAINT_POLICY
:
360 thread_time_constraint_policy_t info
;
362 if (count
< THREAD_TIME_CONSTRAINT_POLICY_COUNT
) {
363 result
= KERN_INVALID_ARGUMENT
;
367 info
= (thread_time_constraint_policy_t
)policy_info
;
368 if (info
->constraint
< info
->computation
||
369 info
->computation
> max_rt_quantum
||
370 info
->computation
< min_rt_quantum
) {
371 result
= KERN_INVALID_ARGUMENT
;
375 spl_t s
= splsched();
378 thread
->realtime
.period
= info
->period
;
379 thread
->realtime
.computation
= info
->computation
;
380 thread
->realtime
.constraint
= info
->constraint
;
381 thread
->realtime
.preemptible
= info
->preemptible
;
383 thread_set_user_sched_mode_and_recompute_pri(thread
, TH_MODE_REALTIME
);
385 thread_unlock(thread
);
388 pend_token
.tpt_update_thread_sfi
= 1;
393 case THREAD_PRECEDENCE_POLICY
:
395 thread_precedence_policy_t info
;
397 if (count
< THREAD_PRECEDENCE_POLICY_COUNT
) {
398 result
= KERN_INVALID_ARGUMENT
;
401 info
= (thread_precedence_policy_t
)policy_info
;
403 spl_t s
= splsched();
406 thread
->importance
= info
->importance
;
408 thread_recompute_priority(thread
);
410 thread_unlock(thread
);
416 case THREAD_AFFINITY_POLICY
:
418 thread_affinity_policy_t info
;
420 if (!thread_affinity_is_supported()) {
421 result
= KERN_NOT_SUPPORTED
;
424 if (count
< THREAD_AFFINITY_POLICY_COUNT
) {
425 result
= KERN_INVALID_ARGUMENT
;
429 info
= (thread_affinity_policy_t
) policy_info
;
431 * Unlock the thread mutex here and
432 * return directly after calling thread_affinity_set().
433 * This is necessary for correct lock ordering because
434 * thread_affinity_set() takes the task lock.
436 thread_mtx_unlock(thread
);
437 return thread_affinity_set(thread
, info
->affinity_tag
);
441 case THREAD_BACKGROUND_POLICY
:
443 thread_background_policy_t info
;
445 if (count
< THREAD_BACKGROUND_POLICY_COUNT
) {
446 result
= KERN_INVALID_ARGUMENT
;
450 if (thread
->task
!= current_task()) {
451 result
= KERN_PROTECTION_FAILURE
;
455 info
= (thread_background_policy_t
) policy_info
;
459 if (info
->priority
== THREAD_BACKGROUND_POLICY_DARWIN_BG
) {
460 enable
= TASK_POLICY_ENABLE
;
462 enable
= TASK_POLICY_DISABLE
;
465 int category
= (current_thread() == thread
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
467 proc_set_thread_policy_locked(thread
, category
, TASK_POLICY_DARWIN_BG
, enable
, 0, &pend_token
);
471 #endif /* CONFIG_EMBEDDED */
473 case THREAD_THROUGHPUT_QOS_POLICY
:
475 thread_throughput_qos_policy_t info
= (thread_throughput_qos_policy_t
) policy_info
;
476 thread_throughput_qos_t tqos
;
478 if (count
< THREAD_THROUGHPUT_QOS_POLICY_COUNT
) {
479 result
= KERN_INVALID_ARGUMENT
;
483 if ((result
= qos_throughput_policy_validate(info
->thread_throughput_qos_tier
)) != KERN_SUCCESS
) {
487 tqos
= qos_extract(info
->thread_throughput_qos_tier
);
489 proc_set_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
,
490 TASK_POLICY_THROUGH_QOS
, tqos
, 0, &pend_token
);
495 case THREAD_LATENCY_QOS_POLICY
:
497 thread_latency_qos_policy_t info
= (thread_latency_qos_policy_t
) policy_info
;
498 thread_latency_qos_t lqos
;
500 if (count
< THREAD_LATENCY_QOS_POLICY_COUNT
) {
501 result
= KERN_INVALID_ARGUMENT
;
505 if ((result
= qos_latency_policy_validate(info
->thread_latency_qos_tier
)) != KERN_SUCCESS
) {
509 lqos
= qos_extract(info
->thread_latency_qos_tier
);
511 proc_set_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
,
512 TASK_POLICY_LATENCY_QOS
, lqos
, 0, &pend_token
);
517 case THREAD_QOS_POLICY
:
519 thread_qos_policy_t info
= (thread_qos_policy_t
)policy_info
;
521 if (count
< THREAD_QOS_POLICY_COUNT
) {
522 result
= KERN_INVALID_ARGUMENT
;
526 if (info
->qos_tier
< 0 || info
->qos_tier
>= THREAD_QOS_LAST
) {
527 result
= KERN_INVALID_ARGUMENT
;
531 if (info
->tier_importance
> 0 || info
->tier_importance
< THREAD_QOS_MIN_TIER_IMPORTANCE
) {
532 result
= KERN_INVALID_ARGUMENT
;
536 if (info
->qos_tier
== THREAD_QOS_UNSPECIFIED
&& info
->tier_importance
!= 0) {
537 result
= KERN_INVALID_ARGUMENT
;
541 proc_set_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS_AND_RELPRIO
,
542 info
->qos_tier
, -info
->tier_importance
, &pend_token
);
548 result
= KERN_INVALID_ARGUMENT
;
552 thread_mtx_unlock(thread
);
554 thread_policy_update_complete_unlocked(thread
, &pend_token
);
560 * Note that there is no implemented difference between POLICY_RR and POLICY_FIFO.
561 * Both result in FIXED mode scheduling.
564 convert_policy_to_sched_mode(integer_t policy
)
567 case POLICY_TIMESHARE
:
568 return TH_MODE_TIMESHARE
;
571 return TH_MODE_FIXED
;
573 panic("unexpected sched policy: %d", policy
);
579 * Called either with the thread mutex locked
580 * or from the pthread kext in a 'safe place'.
583 thread_set_mode_and_absolute_pri_internal(thread_t thread
,
586 task_pend_token_t pend_token
)
588 kern_return_t kr
= KERN_SUCCESS
;
590 spl_t s
= splsched();
593 /* This path isn't allowed to change a thread out of realtime. */
594 if ((thread
->sched_mode
== TH_MODE_REALTIME
) ||
595 (thread
->saved_mode
== TH_MODE_REALTIME
)) {
600 if (thread
->policy_reset
) {
605 sched_mode_t old_mode
= thread
->sched_mode
;
608 * Reverse engineer and apply the correct importance value
609 * from the requested absolute priority value.
611 * TODO: Store the absolute priority value instead
614 if (priority
>= thread
->max_priority
) {
615 priority
= thread
->max_priority
- thread
->task_priority
;
616 } else if (priority
>= MINPRI_KERNEL
) {
617 priority
-= MINPRI_KERNEL
;
618 } else if (priority
>= MINPRI_RESERVED
) {
619 priority
-= MINPRI_RESERVED
;
621 priority
-= BASEPRI_DEFAULT
;
624 priority
+= thread
->task_priority
;
626 if (priority
> thread
->max_priority
) {
627 priority
= thread
->max_priority
;
628 } else if (priority
< MINPRI
) {
632 thread
->importance
= priority
- thread
->task_priority
;
634 thread_set_user_sched_mode_and_recompute_pri(thread
, mode
);
636 if (mode
!= old_mode
) {
637 pend_token
->tpt_update_thread_sfi
= 1;
641 thread_unlock(thread
);
648 thread_freeze_base_pri(thread_t thread
)
650 assert(thread
== current_thread());
652 spl_t s
= splsched();
655 assert((thread
->sched_flags
& TH_SFLAG_BASE_PRI_FROZEN
) == 0);
656 thread
->sched_flags
|= TH_SFLAG_BASE_PRI_FROZEN
;
658 thread_unlock(thread
);
663 thread_unfreeze_base_pri(thread_t thread
)
665 assert(thread
== current_thread());
669 spl_t s
= splsched();
672 assert(thread
->sched_flags
& TH_SFLAG_BASE_PRI_FROZEN
);
673 thread
->sched_flags
&= ~TH_SFLAG_BASE_PRI_FROZEN
;
675 base_pri
= thread
->req_base_pri
;
676 if (base_pri
!= thread
->base_pri
) {
678 * This function returns "true" if the base pri change
679 * is the most likely cause for the preemption.
681 sched_set_thread_base_priority(thread
, base_pri
);
682 ast
= ast_peek(AST_PREEMPT
);
685 thread_unlock(thread
);
692 thread_workq_pri_for_qos(thread_qos_t qos
)
694 assert(qos
< THREAD_QOS_LAST
);
695 return (uint8_t)thread_qos_policy_params
.qos_pri
[qos
];
699 thread_workq_qos_for_pri(int priority
)
702 if (priority
> thread_qos_policy_params
.qos_pri
[THREAD_QOS_USER_INTERACTIVE
]) {
703 // indicate that workq should map >UI threads to workq's
704 // internal notation for above-UI work.
705 return THREAD_QOS_UNSPECIFIED
;
707 for (qos
= THREAD_QOS_USER_INTERACTIVE
; qos
> THREAD_QOS_MAINTENANCE
; qos
--) {
708 // map a given priority up to the next nearest qos band.
709 if (thread_qos_policy_params
.qos_pri
[qos
- 1] < priority
) {
713 return THREAD_QOS_MAINTENANCE
;
717 * private interface for pthread workqueues
719 * Set scheduling policy & absolute priority for thread
720 * May be called with spinlocks held
721 * Thread mutex lock is not held
724 thread_reset_workq_qos(thread_t thread
, uint32_t qos
)
726 struct task_pend_token pend_token
= {};
728 assert(qos
< THREAD_QOS_LAST
);
730 spl_t s
= splsched();
733 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
734 TASK_POLICY_QOS_AND_RELPRIO
, qos
, 0, &pend_token
);
735 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
736 TASK_POLICY_QOS_WORKQ_OVERRIDE
, THREAD_QOS_UNSPECIFIED
, 0,
739 assert(pend_token
.tpt_update_sockets
== 0);
741 thread_unlock(thread
);
744 thread_policy_update_complete_unlocked(thread
, &pend_token
);
748 * private interface for pthread workqueues
750 * Set scheduling policy & absolute priority for thread
751 * May be called with spinlocks held
752 * Thread mutex lock is held
755 thread_set_workq_override(thread_t thread
, uint32_t qos
)
757 struct task_pend_token pend_token
= {};
759 assert(qos
< THREAD_QOS_LAST
);
761 spl_t s
= splsched();
764 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
765 TASK_POLICY_QOS_WORKQ_OVERRIDE
, qos
, 0, &pend_token
);
767 assert(pend_token
.tpt_update_sockets
== 0);
769 thread_unlock(thread
);
772 thread_policy_update_complete_unlocked(thread
, &pend_token
);
776 * private interface for pthread workqueues
778 * Set scheduling policy & absolute priority for thread
779 * May be called with spinlocks held
780 * Thread mutex lock is not held
783 thread_set_workq_pri(thread_t thread
,
788 struct task_pend_token pend_token
= {};
789 sched_mode_t mode
= convert_policy_to_sched_mode(policy
);
791 assert(qos
< THREAD_QOS_LAST
);
792 assert(thread
->static_param
);
794 if (!thread
->static_param
|| !thread
->active
) {
798 spl_t s
= splsched();
801 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
802 TASK_POLICY_QOS_AND_RELPRIO
, qos
, 0, &pend_token
);
803 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
804 TASK_POLICY_QOS_WORKQ_OVERRIDE
, THREAD_QOS_UNSPECIFIED
,
807 thread_unlock(thread
);
810 /* Concern: this doesn't hold the mutex... */
812 __assert_only kern_return_t kr
;
813 kr
= thread_set_mode_and_absolute_pri_internal(thread
, mode
, priority
,
815 assert(kr
== KERN_SUCCESS
);
817 if (pend_token
.tpt_update_thread_sfi
) {
818 sfi_reevaluate(thread
);
823 * thread_set_mode_and_absolute_pri:
825 * Set scheduling policy & absolute priority for thread, for deprecated
826 * thread_set_policy and thread_policy interfaces.
828 * Called with nothing locked.
831 thread_set_mode_and_absolute_pri(thread_t thread
,
835 kern_return_t kr
= KERN_SUCCESS
;
836 struct task_pend_token pend_token
= {};
838 sched_mode_t mode
= convert_policy_to_sched_mode(policy
);
840 thread_mtx_lock(thread
);
842 if (!thread
->active
) {
843 kr
= KERN_TERMINATED
;
847 if (thread_is_static_param(thread
)) {
848 kr
= KERN_POLICY_STATIC
;
852 /* Setting legacy policies on threads kills the current QoS */
853 if (thread
->requested_policy
.thrp_qos
!= THREAD_QOS_UNSPECIFIED
) {
854 thread_remove_qos_policy_locked(thread
, &pend_token
);
857 kr
= thread_set_mode_and_absolute_pri_internal(thread
, mode
, priority
, &pend_token
);
860 thread_mtx_unlock(thread
);
862 thread_policy_update_complete_unlocked(thread
, &pend_token
);
868 * Set the thread's requested mode and recompute priority
869 * Called with thread mutex and thread locked
871 * TODO: Mitigate potential problems caused by moving thread to end of runq
872 * whenever its priority is recomputed
873 * Only remove when it actually changes? Attempt to re-insert at appropriate location?
876 thread_set_user_sched_mode_and_recompute_pri(thread_t thread
, sched_mode_t mode
)
878 if (thread
->policy_reset
) {
882 boolean_t removed
= thread_run_queue_remove(thread
);
885 * TODO: Instead of having saved mode, have 'user mode' and 'true mode'.
886 * That way there's zero confusion over which the user wants
887 * and which the kernel wants.
889 if (thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
) {
890 thread
->saved_mode
= mode
;
892 sched_set_thread_mode(thread
, mode
);
895 thread_recompute_priority(thread
);
898 thread_run_queue_reinsert(thread
, SCHED_TAILQ
);
902 /* called at splsched with thread lock locked */
904 thread_update_qos_cpu_time_locked(thread_t thread
)
906 task_t task
= thread
->task
;
907 uint64_t timer_sum
, timer_delta
;
910 * This is only as accurate as the distance between
911 * last context switch (embedded) or last user/kernel boundary transition (desktop)
912 * because user_timer and system_timer are only updated then.
914 * TODO: Consider running a timer_update operation here to update it first.
915 * Maybe doable with interrupts disabled from current thread.
916 * If the thread is on a different core, may not be easy to get right.
918 * TODO: There should be a function for this in timer.c
921 timer_sum
= timer_grab(&thread
->user_timer
);
922 timer_sum
+= timer_grab(&thread
->system_timer
);
923 timer_delta
= timer_sum
- thread
->vtimer_qos_save
;
925 thread
->vtimer_qos_save
= timer_sum
;
927 uint64_t* task_counter
= NULL
;
929 /* Update the task-level effective and requested qos stats atomically, because we don't have the task lock. */
930 switch (thread
->effective_policy
.thep_qos
) {
931 case THREAD_QOS_UNSPECIFIED
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_default
; break;
932 case THREAD_QOS_MAINTENANCE
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_maintenance
; break;
933 case THREAD_QOS_BACKGROUND
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_background
; break;
934 case THREAD_QOS_UTILITY
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_utility
; break;
935 case THREAD_QOS_LEGACY
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_legacy
; break;
936 case THREAD_QOS_USER_INITIATED
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_user_initiated
; break;
937 case THREAD_QOS_USER_INTERACTIVE
: task_counter
= &task
->cpu_time_eqos_stats
.cpu_time_qos_user_interactive
; break;
939 panic("unknown effective QoS: %d", thread
->effective_policy
.thep_qos
);
942 OSAddAtomic64(timer_delta
, task_counter
);
944 /* Update the task-level qos stats atomically, because we don't have the task lock. */
945 switch (thread
->requested_policy
.thrp_qos
) {
946 case THREAD_QOS_UNSPECIFIED
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_default
; break;
947 case THREAD_QOS_MAINTENANCE
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_maintenance
; break;
948 case THREAD_QOS_BACKGROUND
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_background
; break;
949 case THREAD_QOS_UTILITY
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_utility
; break;
950 case THREAD_QOS_LEGACY
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_legacy
; break;
951 case THREAD_QOS_USER_INITIATED
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_user_initiated
; break;
952 case THREAD_QOS_USER_INTERACTIVE
: task_counter
= &task
->cpu_time_rqos_stats
.cpu_time_qos_user_interactive
; break;
954 panic("unknown requested QoS: %d", thread
->requested_policy
.thrp_qos
);
957 OSAddAtomic64(timer_delta
, task_counter
);
961 * called with no thread locks held
965 thread_update_qos_cpu_time(thread_t thread
)
967 thread_mtx_lock(thread
);
969 spl_t s
= splsched();
972 thread_update_qos_cpu_time_locked(thread
);
974 thread_unlock(thread
);
977 thread_mtx_unlock(thread
);
981 * Calculate base priority from thread attributes, and set it on the thread
983 * Called with thread_lock and thread mutex held.
985 extern thread_t vm_pageout_scan_thread
;
986 extern boolean_t vps_dynamic_priority_enabled
;
989 thread_recompute_priority(
994 if (thread
->policy_reset
) {
998 if (thread
->sched_mode
== TH_MODE_REALTIME
) {
999 sched_set_thread_base_priority(thread
, BASEPRI_RTQUEUES
);
1001 } else if (thread
->effective_policy
.thep_qos
!= THREAD_QOS_UNSPECIFIED
) {
1002 int qos
= thread
->effective_policy
.thep_qos
;
1003 int qos_ui_is_urgent
= thread
->effective_policy
.thep_qos_ui_is_urgent
;
1004 int qos_relprio
= -(thread
->effective_policy
.thep_qos_relprio
); /* stored in task policy inverted */
1005 int qos_scaled_relprio
;
1007 assert(qos
>= 0 && qos
< THREAD_QOS_LAST
);
1008 assert(qos_relprio
<= 0 && qos_relprio
>= THREAD_QOS_MIN_TIER_IMPORTANCE
);
1010 priority
= thread_qos_policy_params
.qos_pri
[qos
];
1011 qos_scaled_relprio
= thread_qos_scaled_relative_priority(qos
, qos_relprio
);
1013 if (qos
== THREAD_QOS_USER_INTERACTIVE
&& qos_ui_is_urgent
== 1) {
1014 /* Bump priority 46 to 47 when in a frontmost app */
1015 qos_scaled_relprio
+= 1;
1018 /* TODO: factor in renice priority here? */
1020 priority
+= qos_scaled_relprio
;
1022 if (thread
->importance
> MAXPRI
) {
1024 } else if (thread
->importance
< -MAXPRI
) {
1027 priority
= thread
->importance
;
1030 priority
+= thread
->task_priority
;
1033 priority
= MAX(priority
, thread
->user_promotion_basepri
);
1036 * Clamp priority back into the allowed range for this task.
1037 * The initial priority value could be out of this range due to:
1038 * Task clamped to BG or Utility (max-pri is 4, or 20)
1039 * Task is user task (max-pri is 63)
1040 * Task is kernel task (max-pri is 95)
1041 * Note that thread->importance is user-settable to any integer
1042 * via THREAD_PRECEDENCE_POLICY.
1044 if (priority
> thread
->max_priority
) {
1045 priority
= thread
->max_priority
;
1046 } else if (priority
< MINPRI
) {
1050 if (thread
->saved_mode
== TH_MODE_REALTIME
&&
1051 thread
->sched_flags
& TH_SFLAG_FAILSAFE
) {
1052 priority
= DEPRESSPRI
;
1055 if (thread
->effective_policy
.thep_terminated
== TRUE
) {
1057 * We temporarily want to override the expected priority to
1058 * ensure that the thread exits in a timely manner.
1059 * Note that this is allowed to exceed thread->max_priority
1060 * so that the thread is no longer clamped to background
1061 * during the final exit phase.
1063 if (priority
< thread
->task_priority
) {
1064 priority
= thread
->task_priority
;
1066 if (priority
< BASEPRI_DEFAULT
) {
1067 priority
= BASEPRI_DEFAULT
;
1072 /* No one can have a base priority less than MAXPRI_THROTTLE */
1073 if (priority
< MAXPRI_THROTTLE
) {
1074 priority
= MAXPRI_THROTTLE
;
1076 #endif /* CONFIG_EMBEDDED */
1078 sched_set_thread_base_priority(thread
, priority
);
1081 /* Called with the task lock held, but not the thread mutex or spinlock */
1083 thread_policy_update_tasklocked(
1086 integer_t max_priority
,
1087 task_pend_token_t pend_token
)
1089 thread_mtx_lock(thread
);
1091 if (!thread
->active
|| thread
->policy_reset
) {
1092 thread_mtx_unlock(thread
);
1096 spl_t s
= splsched();
1097 thread_lock(thread
);
1100 integer_t old_max_priority
= thread
->max_priority
;
1102 thread
->task_priority
= priority
;
1103 thread
->max_priority
= max_priority
;
1107 * When backgrounding a thread, iOS has the semantic that
1108 * realtime and fixed priority threads should be demoted
1109 * to timeshare background threads.
1111 * On OSX, realtime and fixed priority threads don't lose their mode.
1113 * TODO: Do this inside the thread policy update routine in order to avoid double
1114 * remove/reinsert for a runnable thread
1116 if ((max_priority
<= MAXPRI_THROTTLE
) && (old_max_priority
> MAXPRI_THROTTLE
)) {
1117 sched_thread_mode_demote(thread
, TH_SFLAG_THROTTLED
);
1118 } else if ((max_priority
> MAXPRI_THROTTLE
) && (old_max_priority
<= MAXPRI_THROTTLE
)) {
1119 sched_thread_mode_undemote(thread
, TH_SFLAG_THROTTLED
);
1121 #endif /* CONFIG_EMBEDDED */
1123 thread_policy_update_spinlocked(thread
, TRUE
, pend_token
);
1125 thread_unlock(thread
);
1128 thread_mtx_unlock(thread
);
1132 * Reset thread to default state in preparation for termination
1133 * Called with thread mutex locked
1135 * Always called on current thread, so we don't need a run queue remove
1138 thread_policy_reset(
1143 assert(thread
== current_thread());
1146 thread_lock(thread
);
1148 if (thread
->sched_flags
& TH_SFLAG_FAILSAFE
) {
1149 sched_thread_mode_undemote(thread
, TH_SFLAG_FAILSAFE
);
1152 if (thread
->sched_flags
& TH_SFLAG_THROTTLED
) {
1153 sched_thread_mode_undemote(thread
, TH_SFLAG_THROTTLED
);
1156 /* At this point, the various demotions should be inactive */
1157 assert(!(thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
));
1158 assert(!(thread
->sched_flags
& TH_SFLAG_THROTTLED
));
1159 assert(!(thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
));
1161 /* Reset thread back to task-default basepri and mode */
1162 sched_mode_t newmode
= SCHED(initial_thread_sched_mode
)(thread
->task
);
1164 sched_set_thread_mode(thread
, newmode
);
1166 thread
->importance
= 0;
1168 /* Prevent further changes to thread base priority or mode */
1169 thread
->policy_reset
= 1;
1171 sched_set_thread_base_priority(thread
, thread
->task_priority
);
1173 thread_unlock(thread
);
1180 thread_policy_flavor_t flavor
,
1181 thread_policy_t policy_info
,
1182 mach_msg_type_number_t
*count
,
1183 boolean_t
*get_default
)
1185 kern_return_t result
= KERN_SUCCESS
;
1187 if (thread
== THREAD_NULL
) {
1188 return KERN_INVALID_ARGUMENT
;
1191 thread_mtx_lock(thread
);
1192 if (!thread
->active
) {
1193 thread_mtx_unlock(thread
);
1195 return KERN_TERMINATED
;
1199 case THREAD_EXTENDED_POLICY
:
1201 boolean_t timeshare
= TRUE
;
1203 if (!(*get_default
)) {
1204 spl_t s
= splsched();
1205 thread_lock(thread
);
1207 if ((thread
->sched_mode
!= TH_MODE_REALTIME
) &&
1208 (thread
->saved_mode
!= TH_MODE_REALTIME
)) {
1209 if (!(thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
)) {
1210 timeshare
= (thread
->sched_mode
== TH_MODE_TIMESHARE
) != 0;
1212 timeshare
= (thread
->saved_mode
== TH_MODE_TIMESHARE
) != 0;
1215 *get_default
= TRUE
;
1218 thread_unlock(thread
);
1222 if (*count
>= THREAD_EXTENDED_POLICY_COUNT
) {
1223 thread_extended_policy_t info
;
1225 info
= (thread_extended_policy_t
)policy_info
;
1226 info
->timeshare
= timeshare
;
1232 case THREAD_TIME_CONSTRAINT_POLICY
:
1234 thread_time_constraint_policy_t info
;
1236 if (*count
< THREAD_TIME_CONSTRAINT_POLICY_COUNT
) {
1237 result
= KERN_INVALID_ARGUMENT
;
1241 info
= (thread_time_constraint_policy_t
)policy_info
;
1243 if (!(*get_default
)) {
1244 spl_t s
= splsched();
1245 thread_lock(thread
);
1247 if ((thread
->sched_mode
== TH_MODE_REALTIME
) ||
1248 (thread
->saved_mode
== TH_MODE_REALTIME
)) {
1249 info
->period
= thread
->realtime
.period
;
1250 info
->computation
= thread
->realtime
.computation
;
1251 info
->constraint
= thread
->realtime
.constraint
;
1252 info
->preemptible
= thread
->realtime
.preemptible
;
1254 *get_default
= TRUE
;
1257 thread_unlock(thread
);
1263 info
->computation
= default_timeshare_computation
;
1264 info
->constraint
= default_timeshare_constraint
;
1265 info
->preemptible
= TRUE
;
1271 case THREAD_PRECEDENCE_POLICY
:
1273 thread_precedence_policy_t info
;
1275 if (*count
< THREAD_PRECEDENCE_POLICY_COUNT
) {
1276 result
= KERN_INVALID_ARGUMENT
;
1280 info
= (thread_precedence_policy_t
)policy_info
;
1282 if (!(*get_default
)) {
1283 spl_t s
= splsched();
1284 thread_lock(thread
);
1286 info
->importance
= thread
->importance
;
1288 thread_unlock(thread
);
1291 info
->importance
= 0;
1297 case THREAD_AFFINITY_POLICY
:
1299 thread_affinity_policy_t info
;
1301 if (!thread_affinity_is_supported()) {
1302 result
= KERN_NOT_SUPPORTED
;
1305 if (*count
< THREAD_AFFINITY_POLICY_COUNT
) {
1306 result
= KERN_INVALID_ARGUMENT
;
1310 info
= (thread_affinity_policy_t
)policy_info
;
1312 if (!(*get_default
)) {
1313 info
->affinity_tag
= thread_affinity_get(thread
);
1315 info
->affinity_tag
= THREAD_AFFINITY_TAG_NULL
;
1321 case THREAD_POLICY_STATE
:
1323 thread_policy_state_t info
;
1325 if (*count
< THREAD_POLICY_STATE_COUNT
) {
1326 result
= KERN_INVALID_ARGUMENT
;
1330 /* Only root can get this info */
1331 if (current_task()->sec_token
.val
[0] != 0) {
1332 result
= KERN_PROTECTION_FAILURE
;
1336 info
= (thread_policy_state_t
)(void*)policy_info
;
1338 if (!(*get_default
)) {
1341 spl_t s
= splsched();
1342 thread_lock(thread
);
1344 info
->flags
|= (thread
->static_param
? THREAD_POLICY_STATE_FLAG_STATIC_PARAM
: 0);
1346 info
->thps_requested_policy
= *(uint64_t*)(void*)(&thread
->requested_policy
);
1347 info
->thps_effective_policy
= *(uint64_t*)(void*)(&thread
->effective_policy
);
1349 info
->thps_user_promotions
= 0;
1350 info
->thps_user_promotion_basepri
= thread
->user_promotion_basepri
;
1351 info
->thps_ipc_overrides
= thread
->kevent_overrides
;
1353 proc_get_thread_policy_bitfield(thread
, info
);
1355 thread_unlock(thread
);
1358 info
->requested
= 0;
1359 info
->effective
= 0;
1366 case THREAD_LATENCY_QOS_POLICY
:
1368 thread_latency_qos_policy_t info
= (thread_latency_qos_policy_t
) policy_info
;
1369 thread_latency_qos_t plqos
;
1371 if (*count
< THREAD_LATENCY_QOS_POLICY_COUNT
) {
1372 result
= KERN_INVALID_ARGUMENT
;
1379 plqos
= proc_get_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_LATENCY_QOS
, NULL
);
1382 info
->thread_latency_qos_tier
= qos_latency_policy_package(plqos
);
1386 case THREAD_THROUGHPUT_QOS_POLICY
:
1388 thread_throughput_qos_policy_t info
= (thread_throughput_qos_policy_t
) policy_info
;
1389 thread_throughput_qos_t ptqos
;
1391 if (*count
< THREAD_THROUGHPUT_QOS_POLICY_COUNT
) {
1392 result
= KERN_INVALID_ARGUMENT
;
1399 ptqos
= proc_get_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_THROUGH_QOS
, NULL
);
1402 info
->thread_throughput_qos_tier
= qos_throughput_policy_package(ptqos
);
1406 case THREAD_QOS_POLICY
:
1408 thread_qos_policy_t info
= (thread_qos_policy_t
)policy_info
;
1410 if (*count
< THREAD_QOS_POLICY_COUNT
) {
1411 result
= KERN_INVALID_ARGUMENT
;
1415 if (!(*get_default
)) {
1416 int relprio_value
= 0;
1417 info
->qos_tier
= proc_get_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
,
1418 TASK_POLICY_QOS_AND_RELPRIO
, &relprio_value
);
1420 info
->tier_importance
= -relprio_value
;
1422 info
->qos_tier
= THREAD_QOS_UNSPECIFIED
;
1423 info
->tier_importance
= 0;
1430 result
= KERN_INVALID_ARGUMENT
;
1434 thread_mtx_unlock(thread
);
1440 thread_policy_create(thread_t thread
)
1442 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1443 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_THREAD
))) | DBG_FUNC_START
,
1444 thread_tid(thread
), theffective_0(thread
),
1445 theffective_1(thread
), thread
->base_pri
, 0);
1447 /* We pass a pend token but ignore it */
1448 struct task_pend_token pend_token
= {};
1450 thread_policy_update_internal_spinlocked(thread
, TRUE
, &pend_token
);
1452 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1453 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_THREAD
))) | DBG_FUNC_END
,
1454 thread_tid(thread
), theffective_0(thread
),
1455 theffective_1(thread
), thread
->base_pri
, 0);
1459 thread_policy_update_spinlocked(thread_t thread
, boolean_t recompute_priority
, task_pend_token_t pend_token
)
1461 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1462 (IMPORTANCE_CODE(IMP_UPDATE
, TASK_POLICY_THREAD
) | DBG_FUNC_START
),
1463 thread_tid(thread
), theffective_0(thread
),
1464 theffective_1(thread
), thread
->base_pri
, 0);
1466 thread_policy_update_internal_spinlocked(thread
, recompute_priority
, pend_token
);
1468 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1469 (IMPORTANCE_CODE(IMP_UPDATE
, TASK_POLICY_THREAD
)) | DBG_FUNC_END
,
1470 thread_tid(thread
), theffective_0(thread
),
1471 theffective_1(thread
), thread
->base_pri
, 0);
1477 * One thread state update function TO RULE THEM ALL
1479 * This function updates the thread effective policy fields
1480 * and pushes the results to the relevant subsystems.
1482 * Returns TRUE if a pended action needs to be run.
1484 * Called with thread spinlock locked, task may be locked, thread mutex may be locked
1487 thread_policy_update_internal_spinlocked(thread_t thread
, boolean_t recompute_priority
,
1488 task_pend_token_t pend_token
)
1492 * Gather requested policy and effective task state
1495 struct thread_requested_policy requested
= thread
->requested_policy
;
1496 struct task_effective_policy task_effective
= thread
->task
->effective_policy
;
1500 * Calculate new effective policies from requested policy, task and thread state
1502 * Don't change requested, it won't take effect
1505 struct thread_effective_policy next
= {};
1507 next
.thep_qos_ui_is_urgent
= task_effective
.tep_qos_ui_is_urgent
;
1509 uint32_t next_qos
= requested
.thrp_qos
;
1511 if (requested
.thrp_qos
!= THREAD_QOS_UNSPECIFIED
) {
1512 next_qos
= MAX(requested
.thrp_qos_override
, next_qos
);
1513 next_qos
= MAX(requested
.thrp_qos_promote
, next_qos
);
1514 next_qos
= MAX(requested
.thrp_qos_kevent_override
, next_qos
);
1515 next_qos
= MAX(requested
.thrp_qos_wlsvc_override
, next_qos
);
1516 next_qos
= MAX(requested
.thrp_qos_workq_override
, next_qos
);
1519 next
.thep_qos
= next_qos
;
1521 /* A task clamp will result in an effective QoS even when requested is UNSPECIFIED */
1522 if (task_effective
.tep_qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
1523 if (next
.thep_qos
!= THREAD_QOS_UNSPECIFIED
) {
1524 next
.thep_qos
= MIN(task_effective
.tep_qos_clamp
, next
.thep_qos
);
1526 next
.thep_qos
= task_effective
.tep_qos_clamp
;
1531 * Extract outbound-promotion QoS before applying task ceiling or BG clamp
1532 * This allows QoS promotions to work properly even after the process is unclamped.
1534 next
.thep_qos_promote
= next
.thep_qos
;
1536 /* The ceiling only applies to threads that are in the QoS world */
1537 if (task_effective
.tep_qos_ceiling
!= THREAD_QOS_UNSPECIFIED
&&
1538 next
.thep_qos
!= THREAD_QOS_UNSPECIFIED
) {
1539 next
.thep_qos
= MIN(task_effective
.tep_qos_ceiling
, next
.thep_qos
);
1542 /* Apply the sync ipc qos override */
1543 assert(requested
.thrp_qos_sync_ipc_override
== THREAD_QOS_UNSPECIFIED
);
1546 * The QoS relative priority is only applicable when the original programmer's
1547 * intended (requested) QoS is in effect. When the QoS is clamped (e.g.
1548 * USER_INITIATED-13REL clamped to UTILITY), the relative priority is not honored,
1549 * since otherwise it would be lower than unclamped threads. Similarly, in the
1550 * presence of boosting, the programmer doesn't know what other actors
1551 * are boosting the thread.
1553 if ((requested
.thrp_qos
!= THREAD_QOS_UNSPECIFIED
) &&
1554 (requested
.thrp_qos
== next
.thep_qos
) &&
1555 (requested
.thrp_qos_override
== THREAD_QOS_UNSPECIFIED
)) {
1556 next
.thep_qos_relprio
= requested
.thrp_qos_relprio
;
1558 next
.thep_qos_relprio
= 0;
1561 /* Calculate DARWIN_BG */
1562 boolean_t wants_darwinbg
= FALSE
;
1563 boolean_t wants_all_sockets_bg
= FALSE
; /* Do I want my existing sockets to be bg */
1566 * If DARWIN_BG has been requested at either level, it's engaged.
1567 * darwinbg threads always create bg sockets,
1568 * but only some types of darwinbg change the sockets
1569 * after they're created
1571 if (requested
.thrp_int_darwinbg
|| requested
.thrp_ext_darwinbg
) {
1572 wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
1575 if (requested
.thrp_pidbind_bg
) {
1576 wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
1579 if (task_effective
.tep_darwinbg
) {
1580 wants_darwinbg
= TRUE
;
1583 if (next
.thep_qos
== THREAD_QOS_BACKGROUND
||
1584 next
.thep_qos
== THREAD_QOS_MAINTENANCE
) {
1585 wants_darwinbg
= TRUE
;
1588 /* Calculate side effects of DARWIN_BG */
1590 if (wants_darwinbg
) {
1591 next
.thep_darwinbg
= 1;
1594 if (next
.thep_darwinbg
|| task_effective
.tep_new_sockets_bg
) {
1595 next
.thep_new_sockets_bg
= 1;
1598 /* Don't use task_effective.tep_all_sockets_bg here */
1599 if (wants_all_sockets_bg
) {
1600 next
.thep_all_sockets_bg
= 1;
1603 /* darwinbg implies background QOS (or lower) */
1604 if (next
.thep_darwinbg
&&
1605 (next
.thep_qos
> THREAD_QOS_BACKGROUND
|| next
.thep_qos
== THREAD_QOS_UNSPECIFIED
)) {
1606 next
.thep_qos
= THREAD_QOS_BACKGROUND
;
1607 next
.thep_qos_relprio
= 0;
1610 /* Calculate IO policy */
1612 int iopol
= THROTTLE_LEVEL_TIER0
;
1614 /* Factor in the task's IO policy */
1615 if (next
.thep_darwinbg
) {
1616 iopol
= MAX(iopol
, task_effective
.tep_bg_iotier
);
1619 iopol
= MAX(iopol
, task_effective
.tep_io_tier
);
1621 /* Look up the associated IO tier value for the QoS class */
1622 iopol
= MAX(iopol
, thread_qos_policy_params
.qos_iotier
[next
.thep_qos
]);
1624 iopol
= MAX(iopol
, requested
.thrp_int_iotier
);
1625 iopol
= MAX(iopol
, requested
.thrp_ext_iotier
);
1627 next
.thep_io_tier
= iopol
;
1630 * If a QoS override is causing IO to go into a lower tier, we also set
1631 * the passive bit so that a thread doesn't end up stuck in its own throttle
1632 * window when the override goes away.
1634 boolean_t qos_io_override_active
= FALSE
;
1635 if (thread_qos_policy_params
.qos_iotier
[next
.thep_qos
] <
1636 thread_qos_policy_params
.qos_iotier
[requested
.thrp_qos
]) {
1637 qos_io_override_active
= TRUE
;
1640 /* Calculate Passive IO policy */
1641 if (requested
.thrp_ext_iopassive
||
1642 requested
.thrp_int_iopassive
||
1643 qos_io_override_active
||
1644 task_effective
.tep_io_passive
) {
1645 next
.thep_io_passive
= 1;
1648 /* Calculate timer QOS */
1649 uint32_t latency_qos
= requested
.thrp_latency_qos
;
1651 latency_qos
= MAX(latency_qos
, task_effective
.tep_latency_qos
);
1652 latency_qos
= MAX(latency_qos
, thread_qos_policy_params
.qos_latency_qos
[next
.thep_qos
]);
1654 next
.thep_latency_qos
= latency_qos
;
1656 /* Calculate throughput QOS */
1657 uint32_t through_qos
= requested
.thrp_through_qos
;
1659 through_qos
= MAX(through_qos
, task_effective
.tep_through_qos
);
1660 through_qos
= MAX(through_qos
, thread_qos_policy_params
.qos_through_qos
[next
.thep_qos
]);
1662 next
.thep_through_qos
= through_qos
;
1664 if (task_effective
.tep_terminated
|| requested
.thrp_terminated
) {
1665 /* Shoot down the throttles that slow down exit or response to SIGTERM */
1666 next
.thep_terminated
= 1;
1667 next
.thep_darwinbg
= 0;
1668 next
.thep_io_tier
= THROTTLE_LEVEL_TIER0
;
1669 next
.thep_qos
= THREAD_QOS_UNSPECIFIED
;
1670 next
.thep_latency_qos
= LATENCY_QOS_TIER_UNSPECIFIED
;
1671 next
.thep_through_qos
= THROUGHPUT_QOS_TIER_UNSPECIFIED
;
1676 * Swap out old policy for new policy
1679 struct thread_effective_policy prev
= thread
->effective_policy
;
1681 thread_update_qos_cpu_time_locked(thread
);
1683 /* This is the point where the new values become visible to other threads */
1684 thread
->effective_policy
= next
;
1688 * Pend updates that can't be done while holding the thread lock
1691 if (prev
.thep_all_sockets_bg
!= next
.thep_all_sockets_bg
) {
1692 pend_token
->tpt_update_sockets
= 1;
1695 /* TODO: Doesn't this only need to be done if the throttle went up? */
1696 if (prev
.thep_io_tier
!= next
.thep_io_tier
) {
1697 pend_token
->tpt_update_throttle
= 1;
1701 * Check for the attributes that sfi_thread_classify() consults,
1702 * and trigger SFI re-evaluation.
1704 if (prev
.thep_qos
!= next
.thep_qos
||
1705 prev
.thep_darwinbg
!= next
.thep_darwinbg
) {
1706 pend_token
->tpt_update_thread_sfi
= 1;
1709 integer_t old_base_pri
= thread
->base_pri
;
1713 * Update other subsystems as necessary if something has changed
1716 /* Check for the attributes that thread_recompute_priority() consults */
1717 if (prev
.thep_qos
!= next
.thep_qos
||
1718 prev
.thep_qos_relprio
!= next
.thep_qos_relprio
||
1719 prev
.thep_qos_ui_is_urgent
!= next
.thep_qos_ui_is_urgent
||
1720 prev
.thep_terminated
!= next
.thep_terminated
||
1721 pend_token
->tpt_force_recompute_pri
== 1 ||
1722 recompute_priority
) {
1723 thread_recompute_priority(thread
);
1727 * Check if the thread is waiting on a turnstile and needs priority propagation.
1729 if (pend_token
->tpt_update_turnstile
&&
1730 ((old_base_pri
== thread
->base_pri
) ||
1731 !thread_get_waiting_turnstile(thread
))) {
1733 * Reset update turnstile pend token since either
1734 * the thread priority did not change or thread is
1735 * not blocked on a turnstile.
1737 pend_token
->tpt_update_turnstile
= 0;
1743 * Initiate a thread policy state transition on a thread with its TID
1744 * Useful if you cannot guarantee the thread won't get terminated
1745 * Precondition: No locks are held
1746 * Will take task lock - using the non-tid variant is faster
1747 * if you already have a thread ref.
1750 proc_set_thread_policy_with_tid(task_t task
,
1756 /* takes task lock, returns ref'ed thread or NULL */
1757 thread_t thread
= task_findtid(task
, tid
);
1759 if (thread
== THREAD_NULL
) {
1763 proc_set_thread_policy(thread
, category
, flavor
, value
);
1765 thread_deallocate(thread
);
1769 * Initiate a thread policy transition on a thread
1770 * This path supports networking transitions (i.e. darwinbg transitions)
1771 * Precondition: No locks are held
1774 proc_set_thread_policy(thread_t thread
,
1779 struct task_pend_token pend_token
= {};
1781 thread_mtx_lock(thread
);
1783 proc_set_thread_policy_locked(thread
, category
, flavor
, value
, 0, &pend_token
);
1785 thread_mtx_unlock(thread
);
1787 thread_policy_update_complete_unlocked(thread
, &pend_token
);
1791 * Do the things that can't be done while holding a thread mutex.
1792 * These are set up to call back into thread policy to get the latest value,
1793 * so they don't have to be synchronized with the update.
1794 * The only required semantic is 'call this sometime after updating effective policy'
1796 * Precondition: Thread mutex is not held
1798 * This may be called with the task lock held, but in that case it won't be
1799 * called with tpt_update_sockets set.
1802 thread_policy_update_complete_unlocked(thread_t thread
, task_pend_token_t pend_token
)
1805 if (pend_token
->tpt_update_sockets
) {
1806 proc_apply_task_networkbg(thread
->task
->bsd_info
, thread
);
1808 #endif /* MACH_BSD */
1810 if (pend_token
->tpt_update_throttle
) {
1811 rethrottle_thread(thread
->uthread
);
1814 if (pend_token
->tpt_update_thread_sfi
) {
1815 sfi_reevaluate(thread
);
1818 if (pend_token
->tpt_update_turnstile
) {
1819 turnstile_update_thread_priority_chain(thread
);
1824 * Set and update thread policy
1825 * Thread mutex might be held
1828 proc_set_thread_policy_locked(thread_t thread
,
1833 task_pend_token_t pend_token
)
1835 spl_t s
= splsched();
1836 thread_lock(thread
);
1838 proc_set_thread_policy_spinlocked(thread
, category
, flavor
, value
, value2
, pend_token
);
1840 thread_unlock(thread
);
1845 * Set and update thread policy
1846 * Thread spinlock is held
1849 proc_set_thread_policy_spinlocked(thread_t thread
,
1854 task_pend_token_t pend_token
)
1856 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1857 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_THREAD
))) | DBG_FUNC_START
,
1858 thread_tid(thread
), threquested_0(thread
),
1859 threquested_1(thread
), value
, 0);
1861 thread_set_requested_policy_spinlocked(thread
, category
, flavor
, value
, value2
, pend_token
);
1863 thread_policy_update_spinlocked(thread
, FALSE
, pend_token
);
1865 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1866 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_THREAD
))) | DBG_FUNC_END
,
1867 thread_tid(thread
), threquested_0(thread
),
1868 threquested_1(thread
), tpending(pend_token
), 0);
1872 * Set the requested state for a specific flavor to a specific value.
1875 thread_set_requested_policy_spinlocked(thread_t thread
,
1880 task_pend_token_t pend_token
)
1884 struct thread_requested_policy requested
= thread
->requested_policy
;
1887 /* Category: EXTERNAL and INTERNAL, thread and task */
1889 case TASK_POLICY_DARWIN_BG
:
1890 if (category
== TASK_POLICY_EXTERNAL
) {
1891 requested
.thrp_ext_darwinbg
= value
;
1893 requested
.thrp_int_darwinbg
= value
;
1897 case TASK_POLICY_IOPOL
:
1898 proc_iopol_to_tier(value
, &tier
, &passive
);
1899 if (category
== TASK_POLICY_EXTERNAL
) {
1900 requested
.thrp_ext_iotier
= tier
;
1901 requested
.thrp_ext_iopassive
= passive
;
1903 requested
.thrp_int_iotier
= tier
;
1904 requested
.thrp_int_iopassive
= passive
;
1908 case TASK_POLICY_IO
:
1909 if (category
== TASK_POLICY_EXTERNAL
) {
1910 requested
.thrp_ext_iotier
= value
;
1912 requested
.thrp_int_iotier
= value
;
1916 case TASK_POLICY_PASSIVE_IO
:
1917 if (category
== TASK_POLICY_EXTERNAL
) {
1918 requested
.thrp_ext_iopassive
= value
;
1920 requested
.thrp_int_iopassive
= value
;
1924 /* Category: ATTRIBUTE, thread only */
1926 case TASK_POLICY_PIDBIND_BG
:
1927 assert(category
== TASK_POLICY_ATTRIBUTE
);
1928 requested
.thrp_pidbind_bg
= value
;
1931 case TASK_POLICY_LATENCY_QOS
:
1932 assert(category
== TASK_POLICY_ATTRIBUTE
);
1933 requested
.thrp_latency_qos
= value
;
1936 case TASK_POLICY_THROUGH_QOS
:
1937 assert(category
== TASK_POLICY_ATTRIBUTE
);
1938 requested
.thrp_through_qos
= value
;
1941 case TASK_POLICY_QOS_OVERRIDE
:
1942 assert(category
== TASK_POLICY_ATTRIBUTE
);
1943 requested
.thrp_qos_override
= value
;
1944 pend_token
->tpt_update_turnstile
= 1;
1947 case TASK_POLICY_QOS_AND_RELPRIO
:
1948 assert(category
== TASK_POLICY_ATTRIBUTE
);
1949 requested
.thrp_qos
= value
;
1950 requested
.thrp_qos_relprio
= value2
;
1951 pend_token
->tpt_update_turnstile
= 1;
1952 DTRACE_BOOST3(qos_set
, uint64_t, thread
->thread_id
, int, requested
.thrp_qos
, int, requested
.thrp_qos_relprio
);
1955 case TASK_POLICY_QOS_WORKQ_OVERRIDE
:
1956 assert(category
== TASK_POLICY_ATTRIBUTE
);
1957 requested
.thrp_qos_workq_override
= value
;
1958 pend_token
->tpt_update_turnstile
= 1;
1961 case TASK_POLICY_QOS_PROMOTE
:
1962 assert(category
== TASK_POLICY_ATTRIBUTE
);
1963 requested
.thrp_qos_promote
= value
;
1966 case TASK_POLICY_QOS_KEVENT_OVERRIDE
:
1967 assert(category
== TASK_POLICY_ATTRIBUTE
);
1968 requested
.thrp_qos_kevent_override
= value
;
1969 pend_token
->tpt_update_turnstile
= 1;
1972 case TASK_POLICY_QOS_SERVICER_OVERRIDE
:
1973 assert(category
== TASK_POLICY_ATTRIBUTE
);
1974 requested
.thrp_qos_wlsvc_override
= value
;
1975 pend_token
->tpt_update_turnstile
= 1;
1978 case TASK_POLICY_TERMINATED
:
1979 assert(category
== TASK_POLICY_ATTRIBUTE
);
1980 requested
.thrp_terminated
= value
;
1984 panic("unknown task policy: %d %d %d", category
, flavor
, value
);
1988 thread
->requested_policy
= requested
;
1992 * Gets what you set. Effective values may be different.
1993 * Precondition: No locks are held
1996 proc_get_thread_policy(thread_t thread
,
2001 thread_mtx_lock(thread
);
2002 value
= proc_get_thread_policy_locked(thread
, category
, flavor
, NULL
);
2003 thread_mtx_unlock(thread
);
2008 proc_get_thread_policy_locked(thread_t thread
,
2015 spl_t s
= splsched();
2016 thread_lock(thread
);
2018 value
= thread_get_requested_policy_spinlocked(thread
, category
, flavor
, value2
);
2020 thread_unlock(thread
);
2027 * Gets what you set. Effective values may be different.
2030 thread_get_requested_policy_spinlocked(thread_t thread
,
2037 struct thread_requested_policy requested
= thread
->requested_policy
;
2040 case TASK_POLICY_DARWIN_BG
:
2041 if (category
== TASK_POLICY_EXTERNAL
) {
2042 value
= requested
.thrp_ext_darwinbg
;
2044 value
= requested
.thrp_int_darwinbg
;
2047 case TASK_POLICY_IOPOL
:
2048 if (category
== TASK_POLICY_EXTERNAL
) {
2049 value
= proc_tier_to_iopol(requested
.thrp_ext_iotier
,
2050 requested
.thrp_ext_iopassive
);
2052 value
= proc_tier_to_iopol(requested
.thrp_int_iotier
,
2053 requested
.thrp_int_iopassive
);
2056 case TASK_POLICY_IO
:
2057 if (category
== TASK_POLICY_EXTERNAL
) {
2058 value
= requested
.thrp_ext_iotier
;
2060 value
= requested
.thrp_int_iotier
;
2063 case TASK_POLICY_PASSIVE_IO
:
2064 if (category
== TASK_POLICY_EXTERNAL
) {
2065 value
= requested
.thrp_ext_iopassive
;
2067 value
= requested
.thrp_int_iopassive
;
2070 case TASK_POLICY_QOS
:
2071 assert(category
== TASK_POLICY_ATTRIBUTE
);
2072 value
= requested
.thrp_qos
;
2074 case TASK_POLICY_QOS_OVERRIDE
:
2075 assert(category
== TASK_POLICY_ATTRIBUTE
);
2076 value
= requested
.thrp_qos_override
;
2078 case TASK_POLICY_LATENCY_QOS
:
2079 assert(category
== TASK_POLICY_ATTRIBUTE
);
2080 value
= requested
.thrp_latency_qos
;
2082 case TASK_POLICY_THROUGH_QOS
:
2083 assert(category
== TASK_POLICY_ATTRIBUTE
);
2084 value
= requested
.thrp_through_qos
;
2086 case TASK_POLICY_QOS_WORKQ_OVERRIDE
:
2087 assert(category
== TASK_POLICY_ATTRIBUTE
);
2088 value
= requested
.thrp_qos_workq_override
;
2090 case TASK_POLICY_QOS_AND_RELPRIO
:
2091 assert(category
== TASK_POLICY_ATTRIBUTE
);
2092 assert(value2
!= NULL
);
2093 value
= requested
.thrp_qos
;
2094 *value2
= requested
.thrp_qos_relprio
;
2096 case TASK_POLICY_QOS_PROMOTE
:
2097 assert(category
== TASK_POLICY_ATTRIBUTE
);
2098 value
= requested
.thrp_qos_promote
;
2100 case TASK_POLICY_QOS_KEVENT_OVERRIDE
:
2101 assert(category
== TASK_POLICY_ATTRIBUTE
);
2102 value
= requested
.thrp_qos_kevent_override
;
2104 case TASK_POLICY_QOS_SERVICER_OVERRIDE
:
2105 assert(category
== TASK_POLICY_ATTRIBUTE
);
2106 value
= requested
.thrp_qos_wlsvc_override
;
2108 case TASK_POLICY_TERMINATED
:
2109 assert(category
== TASK_POLICY_ATTRIBUTE
);
2110 value
= requested
.thrp_terminated
;
2114 panic("unknown policy_flavor %d", flavor
);
2122 * Gets what is actually in effect, for subsystems which pull policy instead of receive updates.
2124 * NOTE: This accessor does not take the task or thread lock.
2125 * Notifications of state updates need to be externally synchronized with state queries.
2126 * This routine *MUST* remain interrupt safe, as it is potentially invoked
2127 * within the context of a timer interrupt.
2129 * TODO: I think we can get away with architecting this such that we don't need to look at the task ever.
2130 * Is that a good idea? Maybe it's best to avoid evaluate-all-the-threads updates.
2131 * I don't think that cost is worth not having the right answer.
2134 proc_get_effective_thread_policy(thread_t thread
,
2140 case TASK_POLICY_DARWIN_BG
:
2142 * This call is used within the timer layer, as well as
2143 * prioritizing requests to the graphics system.
2144 * It also informs SFI and originator-bg-state.
2145 * Returns 1 for background mode, 0 for normal mode
2148 value
= thread
->effective_policy
.thep_darwinbg
? 1 : 0;
2150 case TASK_POLICY_IO
:
2152 * The I/O system calls here to find out what throttling tier to apply to an operation.
2153 * Returns THROTTLE_LEVEL_* values
2155 value
= thread
->effective_policy
.thep_io_tier
;
2156 if (thread
->iotier_override
!= THROTTLE_LEVEL_NONE
) {
2157 value
= MIN(value
, thread
->iotier_override
);
2160 case TASK_POLICY_PASSIVE_IO
:
2162 * The I/O system calls here to find out whether an operation should be passive.
2163 * (i.e. not cause operations with lower throttle tiers to be throttled)
2164 * Returns 1 for passive mode, 0 for normal mode
2166 * If an override is causing IO to go into a lower tier, we also set
2167 * the passive bit so that a thread doesn't end up stuck in its own throttle
2168 * window when the override goes away.
2170 value
= thread
->effective_policy
.thep_io_passive
? 1 : 0;
2171 if (thread
->iotier_override
!= THROTTLE_LEVEL_NONE
&&
2172 thread
->iotier_override
< thread
->effective_policy
.thep_io_tier
) {
2176 case TASK_POLICY_ALL_SOCKETS_BG
:
2178 * do_background_socket() calls this to determine whether
2179 * it should change the thread's sockets
2180 * Returns 1 for background mode, 0 for normal mode
2181 * This consults both thread and task so un-DBGing a thread while the task is BG
2182 * doesn't get you out of the network throttle.
2184 value
= (thread
->effective_policy
.thep_all_sockets_bg
||
2185 thread
->task
->effective_policy
.tep_all_sockets_bg
) ? 1 : 0;
2187 case TASK_POLICY_NEW_SOCKETS_BG
:
2189 * socreate() calls this to determine if it should mark a new socket as background
2190 * Returns 1 for background mode, 0 for normal mode
2192 value
= thread
->effective_policy
.thep_new_sockets_bg
? 1 : 0;
2194 case TASK_POLICY_LATENCY_QOS
:
2196 * timer arming calls into here to find out the timer coalescing level
2197 * Returns a latency QoS tier (0-6)
2199 value
= thread
->effective_policy
.thep_latency_qos
;
2201 case TASK_POLICY_THROUGH_QOS
:
2203 * This value is passed into the urgency callout from the scheduler
2204 * to the performance management subsystem.
2206 * Returns a throughput QoS tier (0-6)
2208 value
= thread
->effective_policy
.thep_through_qos
;
2210 case TASK_POLICY_QOS
:
2212 * This is communicated to the performance management layer and SFI.
2214 * Returns a QoS policy tier
2216 value
= thread
->effective_policy
.thep_qos
;
2219 panic("unknown thread policy flavor %d", flavor
);
2228 * (integer_t) casts limit the number of bits we can fit here
2229 * this interface is deprecated and replaced by the _EXT struct ?
2232 proc_get_thread_policy_bitfield(thread_t thread
, thread_policy_state_t info
)
2235 struct thread_requested_policy requested
= thread
->requested_policy
;
2237 bits
|= (requested
.thrp_int_darwinbg
? POLICY_REQ_INT_DARWIN_BG
: 0);
2238 bits
|= (requested
.thrp_ext_darwinbg
? POLICY_REQ_EXT_DARWIN_BG
: 0);
2239 bits
|= (requested
.thrp_int_iotier
? (((uint64_t)requested
.thrp_int_iotier
) << POLICY_REQ_INT_IO_TIER_SHIFT
) : 0);
2240 bits
|= (requested
.thrp_ext_iotier
? (((uint64_t)requested
.thrp_ext_iotier
) << POLICY_REQ_EXT_IO_TIER_SHIFT
) : 0);
2241 bits
|= (requested
.thrp_int_iopassive
? POLICY_REQ_INT_PASSIVE_IO
: 0);
2242 bits
|= (requested
.thrp_ext_iopassive
? POLICY_REQ_EXT_PASSIVE_IO
: 0);
2244 bits
|= (requested
.thrp_qos
? (((uint64_t)requested
.thrp_qos
) << POLICY_REQ_TH_QOS_SHIFT
) : 0);
2245 bits
|= (requested
.thrp_qos_override
? (((uint64_t)requested
.thrp_qos_override
) << POLICY_REQ_TH_QOS_OVER_SHIFT
) : 0);
2247 bits
|= (requested
.thrp_pidbind_bg
? POLICY_REQ_PIDBIND_BG
: 0);
2249 bits
|= (requested
.thrp_latency_qos
? (((uint64_t)requested
.thrp_latency_qos
) << POLICY_REQ_BASE_LATENCY_QOS_SHIFT
) : 0);
2250 bits
|= (requested
.thrp_through_qos
? (((uint64_t)requested
.thrp_through_qos
) << POLICY_REQ_BASE_THROUGH_QOS_SHIFT
) : 0);
2252 info
->requested
= (integer_t
) bits
;
2255 struct thread_effective_policy effective
= thread
->effective_policy
;
2257 bits
|= (effective
.thep_darwinbg
? POLICY_EFF_DARWIN_BG
: 0);
2259 bits
|= (effective
.thep_io_tier
? (((uint64_t)effective
.thep_io_tier
) << POLICY_EFF_IO_TIER_SHIFT
) : 0);
2260 bits
|= (effective
.thep_io_passive
? POLICY_EFF_IO_PASSIVE
: 0);
2261 bits
|= (effective
.thep_all_sockets_bg
? POLICY_EFF_ALL_SOCKETS_BG
: 0);
2262 bits
|= (effective
.thep_new_sockets_bg
? POLICY_EFF_NEW_SOCKETS_BG
: 0);
2264 bits
|= (effective
.thep_qos
? (((uint64_t)effective
.thep_qos
) << POLICY_EFF_TH_QOS_SHIFT
) : 0);
2266 bits
|= (effective
.thep_latency_qos
? (((uint64_t)effective
.thep_latency_qos
) << POLICY_EFF_LATENCY_QOS_SHIFT
) : 0);
2267 bits
|= (effective
.thep_through_qos
? (((uint64_t)effective
.thep_through_qos
) << POLICY_EFF_THROUGH_QOS_SHIFT
) : 0);
2269 info
->effective
= (integer_t
)bits
;
2276 * Sneakily trace either the task and thread requested
2277 * or just the thread requested, depending on if we have enough room.
2278 * We do have room on LP64. On LP32, we have to split it between two uintptr_t's.
2281 * threquested_0(thread) thread[0] task[0]
2282 * threquested_1(thread) thread[1] thread[0]
2287 threquested_0(thread_t thread
)
2289 static_assert(sizeof(struct thread_requested_policy
) == sizeof(uint64_t), "size invariant violated");
2291 uintptr_t* raw
= (uintptr_t*)(void*)&thread
->requested_policy
;
2297 threquested_1(thread_t thread
)
2299 #if defined __LP64__
2300 return *(uintptr_t*)&thread
->task
->requested_policy
;
2302 uintptr_t* raw
= (uintptr_t*)(void*)&thread
->requested_policy
;
2308 theffective_0(thread_t thread
)
2310 static_assert(sizeof(struct thread_effective_policy
) == sizeof(uint64_t), "size invariant violated");
2312 uintptr_t* raw
= (uintptr_t*)(void*)&thread
->effective_policy
;
2317 theffective_1(thread_t thread
)
2319 #if defined __LP64__
2320 return *(uintptr_t*)&thread
->task
->effective_policy
;
2322 uintptr_t* raw
= (uintptr_t*)(void*)&thread
->effective_policy
;
2329 * Set an override on the thread which is consulted with a
2330 * higher priority than the task/thread policy. This should
2331 * only be set for temporary grants until the thread
2332 * returns to the userspace boundary
2334 * We use atomic operations to swap in the override, with
2335 * the assumption that the thread itself can
2336 * read the override and clear it on return to userspace.
2338 * No locking is performed, since it is acceptable to see
2339 * a stale override for one loop through throttle_lowpri_io().
2340 * However a thread reference must be held on the thread.
2344 set_thread_iotier_override(thread_t thread
, int policy
)
2346 int current_override
;
2348 /* Let most aggressive I/O policy win until user boundary */
2350 current_override
= thread
->iotier_override
;
2352 if (current_override
!= THROTTLE_LEVEL_NONE
) {
2353 policy
= MIN(current_override
, policy
);
2356 if (current_override
== policy
) {
2357 /* no effective change */
2360 } while (!OSCompareAndSwap(current_override
, policy
, &thread
->iotier_override
));
2363 * Since the thread may be currently throttled,
2364 * re-evaluate tiers and potentially break out
2367 rethrottle_thread(thread
->uthread
);
2371 * Userspace synchronization routines (like pthread mutexes, pthread reader-writer locks,
2372 * semaphores, dispatch_sync) may result in priority inversions where a higher priority
2373 * (i.e. scheduler priority, I/O tier, QoS tier) is waiting on a resource owned by a lower
2374 * priority thread. In these cases, we attempt to propagate the priority token, as long
2375 * as the subsystem informs us of the relationships between the threads. The userspace
2376 * synchronization subsystem should maintain the information of owner->resource and
2377 * resource->waiters itself.
2381 * This helper canonicalizes the resource/resource_type given the current qos_override_mode
2382 * in effect. Note that wildcards (THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD) may need
2383 * to be handled specially in the future, but for now it's fine to slam
2384 * *resource to USER_ADDR_NULL even if it was previously a wildcard.
2387 canonicalize_resource_and_type(user_addr_t
*resource
, int *resource_type
)
2389 if (qos_override_mode
== QOS_OVERRIDE_MODE_OVERHANG_PEAK
|| qos_override_mode
== QOS_OVERRIDE_MODE_IGNORE_OVERRIDE
) {
2390 /* Map all input resource/type to a single one */
2391 *resource
= USER_ADDR_NULL
;
2392 *resource_type
= THREAD_QOS_OVERRIDE_TYPE_UNKNOWN
;
2393 } else if (qos_override_mode
== QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE
) {
2395 } else if (qos_override_mode
== QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_SINGLE_MUTEX_OVERRIDE
) {
2396 /* Map all mutex overrides to a single one, to avoid memory overhead */
2397 if (*resource_type
== THREAD_QOS_OVERRIDE_TYPE_PTHREAD_MUTEX
) {
2398 *resource
= USER_ADDR_NULL
;
2403 /* This helper routine finds an existing override if known. Locking should be done by caller */
2404 static struct thread_qos_override
*
2405 find_qos_override(thread_t thread
,
2406 user_addr_t resource
,
2409 struct thread_qos_override
*override
;
2411 override
= thread
->overrides
;
2413 if (override
->override_resource
== resource
&&
2414 override
->override_resource_type
== resource_type
) {
2418 override
= override
->override_next
;
2425 find_and_decrement_qos_override(thread_t thread
,
2426 user_addr_t resource
,
2429 struct thread_qos_override
**free_override_list
)
2431 struct thread_qos_override
*override
, *override_prev
;
2433 override_prev
= NULL
;
2434 override
= thread
->overrides
;
2436 struct thread_qos_override
*override_next
= override
->override_next
;
2438 if ((THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD
== resource
|| override
->override_resource
== resource
) &&
2439 (THREAD_QOS_OVERRIDE_TYPE_WILDCARD
== resource_type
|| override
->override_resource_type
== resource_type
)) {
2441 override
->override_contended_resource_count
= 0;
2443 override
->override_contended_resource_count
--;
2446 if (override
->override_contended_resource_count
== 0) {
2447 if (override_prev
== NULL
) {
2448 thread
->overrides
= override_next
;
2450 override_prev
->override_next
= override_next
;
2453 /* Add to out-param for later zfree */
2454 override
->override_next
= *free_override_list
;
2455 *free_override_list
= override
;
2457 override_prev
= override
;
2460 if (THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD
!= resource
) {
2464 override_prev
= override
;
2467 override
= override_next
;
2471 /* This helper recalculates the current requested override using the policy selected at boot */
2473 calculate_requested_qos_override(thread_t thread
)
2475 if (qos_override_mode
== QOS_OVERRIDE_MODE_IGNORE_OVERRIDE
) {
2476 return THREAD_QOS_UNSPECIFIED
;
2479 /* iterate over all overrides and calculate MAX */
2480 struct thread_qos_override
*override
;
2481 int qos_override
= THREAD_QOS_UNSPECIFIED
;
2483 override
= thread
->overrides
;
2485 qos_override
= MAX(qos_override
, override
->override_qos
);
2486 override
= override
->override_next
;
2489 return qos_override
;
2495 * - EINVAL if some invalid input was passed
2498 proc_thread_qos_add_override_internal(thread_t thread
,
2500 boolean_t first_override_for_resource
,
2501 user_addr_t resource
,
2504 struct task_pend_token pend_token
= {};
2507 thread_mtx_lock(thread
);
2509 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_ADD_OVERRIDE
)) | DBG_FUNC_START
,
2510 thread_tid(thread
), override_qos
, first_override_for_resource
? 1 : 0, 0, 0);
2512 DTRACE_BOOST5(qos_add_override_pre
, uint64_t, thread_tid(thread
),
2513 uint64_t, thread
->requested_policy
.thrp_qos
,
2514 uint64_t, thread
->effective_policy
.thep_qos
,
2515 int, override_qos
, boolean_t
, first_override_for_resource
);
2517 struct thread_qos_override
*override
;
2518 struct thread_qos_override
*override_new
= NULL
;
2519 int new_qos_override
, prev_qos_override
;
2520 int new_effective_qos
;
2522 canonicalize_resource_and_type(&resource
, &resource_type
);
2524 override
= find_qos_override(thread
, resource
, resource_type
);
2525 if (first_override_for_resource
&& !override
) {
2526 /* We need to allocate a new object. Drop the thread lock and
2527 * recheck afterwards in case someone else added the override
2529 thread_mtx_unlock(thread
);
2530 override_new
= zalloc(thread_qos_override_zone
);
2531 thread_mtx_lock(thread
);
2532 override
= find_qos_override(thread
, resource
, resource_type
);
2534 if (first_override_for_resource
&& override
) {
2535 /* Someone else already allocated while the thread lock was dropped */
2536 override
->override_contended_resource_count
++;
2537 } else if (!override
&& override_new
) {
2538 override
= override_new
;
2539 override_new
= NULL
;
2540 override
->override_next
= thread
->overrides
;
2541 /* since first_override_for_resource was TRUE */
2542 override
->override_contended_resource_count
= 1;
2543 override
->override_resource
= resource
;
2544 override
->override_resource_type
= resource_type
;
2545 override
->override_qos
= THREAD_QOS_UNSPECIFIED
;
2546 thread
->overrides
= override
;
2550 if (override
->override_qos
== THREAD_QOS_UNSPECIFIED
) {
2551 override
->override_qos
= override_qos
;
2553 override
->override_qos
= MAX(override
->override_qos
, override_qos
);
2557 /* Determine how to combine the various overrides into a single current
2558 * requested override
2560 new_qos_override
= calculate_requested_qos_override(thread
);
2562 prev_qos_override
= proc_get_thread_policy_locked(thread
,
2563 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS_OVERRIDE
, NULL
);
2565 if (new_qos_override
!= prev_qos_override
) {
2566 proc_set_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
,
2567 TASK_POLICY_QOS_OVERRIDE
,
2568 new_qos_override
, 0, &pend_token
);
2571 new_effective_qos
= proc_get_effective_thread_policy(thread
, TASK_POLICY_QOS
);
2573 thread_mtx_unlock(thread
);
2575 thread_policy_update_complete_unlocked(thread
, &pend_token
);
2578 zfree(thread_qos_override_zone
, override_new
);
2581 DTRACE_BOOST4(qos_add_override_post
, int, prev_qos_override
,
2582 int, new_qos_override
, int, new_effective_qos
, int, rc
);
2584 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_ADD_OVERRIDE
)) | DBG_FUNC_END
,
2585 new_qos_override
, resource
, resource_type
, 0, 0);
2591 proc_thread_qos_add_override(task_t task
,
2595 boolean_t first_override_for_resource
,
2596 user_addr_t resource
,
2599 boolean_t has_thread_reference
= FALSE
;
2602 if (thread
== THREAD_NULL
) {
2603 thread
= task_findtid(task
, tid
);
2604 /* returns referenced thread */
2606 if (thread
== THREAD_NULL
) {
2607 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_ADD_OVERRIDE
)) | DBG_FUNC_NONE
,
2608 tid
, 0, 0xdead, 0, 0);
2611 has_thread_reference
= TRUE
;
2613 assert(thread
->task
== task
);
2615 rc
= proc_thread_qos_add_override_internal(thread
, override_qos
,
2616 first_override_for_resource
, resource
, resource_type
);
2617 if (has_thread_reference
) {
2618 thread_deallocate(thread
);
2625 proc_thread_qos_remove_override_internal(thread_t thread
,
2626 user_addr_t resource
,
2630 struct task_pend_token pend_token
= {};
2632 struct thread_qos_override
*deferred_free_override_list
= NULL
;
2633 int new_qos_override
, prev_qos_override
, new_effective_qos
;
2635 thread_mtx_lock(thread
);
2637 canonicalize_resource_and_type(&resource
, &resource_type
);
2639 find_and_decrement_qos_override(thread
, resource
, resource_type
, reset
, &deferred_free_override_list
);
2641 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_REMOVE_OVERRIDE
)) | DBG_FUNC_START
,
2642 thread_tid(thread
), resource
, reset
, 0, 0);
2644 DTRACE_BOOST3(qos_remove_override_pre
, uint64_t, thread_tid(thread
),
2645 uint64_t, thread
->requested_policy
.thrp_qos
,
2646 uint64_t, thread
->effective_policy
.thep_qos
);
2648 /* Determine how to combine the various overrides into a single current requested override */
2649 new_qos_override
= calculate_requested_qos_override(thread
);
2651 spl_t s
= splsched();
2652 thread_lock(thread
);
2655 * The override chain and therefore the value of the current override is locked with thread mutex,
2656 * so we can do a get/set without races. However, the rest of thread policy is locked under the spinlock.
2657 * This means you can't change the current override from a spinlock-only setter.
2659 prev_qos_override
= thread_get_requested_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS_OVERRIDE
, NULL
);
2661 if (new_qos_override
!= prev_qos_override
) {
2662 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS_OVERRIDE
, new_qos_override
, 0, &pend_token
);
2665 new_effective_qos
= proc_get_effective_thread_policy(thread
, TASK_POLICY_QOS
);
2667 thread_unlock(thread
);
2670 thread_mtx_unlock(thread
);
2672 thread_policy_update_complete_unlocked(thread
, &pend_token
);
2674 while (deferred_free_override_list
) {
2675 struct thread_qos_override
*override_next
= deferred_free_override_list
->override_next
;
2677 zfree(thread_qos_override_zone
, deferred_free_override_list
);
2678 deferred_free_override_list
= override_next
;
2681 DTRACE_BOOST3(qos_remove_override_post
, int, prev_qos_override
,
2682 int, new_qos_override
, int, new_effective_qos
);
2684 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_REMOVE_OVERRIDE
)) | DBG_FUNC_END
,
2685 thread_tid(thread
), 0, 0, 0, 0);
2689 proc_thread_qos_remove_override(task_t task
,
2692 user_addr_t resource
,
2695 boolean_t has_thread_reference
= FALSE
;
2697 if (thread
== THREAD_NULL
) {
2698 thread
= task_findtid(task
, tid
);
2699 /* returns referenced thread */
2701 if (thread
== THREAD_NULL
) {
2702 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_REMOVE_OVERRIDE
)) | DBG_FUNC_NONE
,
2703 tid
, 0, 0xdead, 0, 0);
2706 has_thread_reference
= TRUE
;
2708 assert(task
== thread
->task
);
2711 proc_thread_qos_remove_override_internal(thread
, resource
, resource_type
, FALSE
);
2713 if (has_thread_reference
) {
2714 thread_deallocate(thread
);
2720 /* Deallocate before thread termination */
2722 proc_thread_qos_deallocate(thread_t thread
)
2724 /* This thread must have no more IPC overrides. */
2725 assert(thread
->kevent_overrides
== 0);
2726 assert(thread
->requested_policy
.thrp_qos_kevent_override
== THREAD_QOS_UNSPECIFIED
);
2727 assert(thread
->requested_policy
.thrp_qos_wlsvc_override
== THREAD_QOS_UNSPECIFIED
);
2730 * Clear out any lingering override objects.
2732 struct thread_qos_override
*override
;
2734 thread_mtx_lock(thread
);
2735 override
= thread
->overrides
;
2736 thread
->overrides
= NULL
;
2737 thread
->requested_policy
.thrp_qos_override
= THREAD_QOS_UNSPECIFIED
;
2738 /* We don't need to re-evaluate thread policy here because the thread has already exited */
2739 thread_mtx_unlock(thread
);
2742 struct thread_qos_override
*override_next
= override
->override_next
;
2744 zfree(thread_qos_override_zone
, override
);
2745 override
= override_next
;
2750 * Set up the primordial thread's QoS
2753 task_set_main_thread_qos(task_t task
, thread_t thread
)
2755 struct task_pend_token pend_token
= {};
2757 assert(thread
->task
== task
);
2759 thread_mtx_lock(thread
);
2761 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2762 (IMPORTANCE_CODE(IMP_MAIN_THREAD_QOS
, 0)) | DBG_FUNC_START
,
2763 thread_tid(thread
), threquested_0(thread
), threquested_1(thread
),
2764 thread
->requested_policy
.thrp_qos
, 0);
2766 int primordial_qos
= task_compute_main_thread_qos(task
);
2768 proc_set_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_QOS_AND_RELPRIO
,
2769 primordial_qos
, 0, &pend_token
);
2771 thread_mtx_unlock(thread
);
2773 thread_policy_update_complete_unlocked(thread
, &pend_token
);
2775 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2776 (IMPORTANCE_CODE(IMP_MAIN_THREAD_QOS
, 0)) | DBG_FUNC_END
,
2777 thread_tid(thread
), threquested_0(thread
), threquested_1(thread
),
2782 * KPI for pthread kext
2784 * Return a good guess at what the initial manager QoS will be
2785 * Dispatch can override this in userspace if it so chooses
2788 task_get_default_manager_qos(task_t task
)
2790 int primordial_qos
= task_compute_main_thread_qos(task
);
2792 if (primordial_qos
== THREAD_QOS_LEGACY
) {
2793 primordial_qos
= THREAD_QOS_USER_INITIATED
;
2796 return primordial_qos
;
2800 * Check if the kernel promotion on thread has changed
2803 * thread locked on entry and exit
2806 thread_recompute_kernel_promotion_locked(thread_t thread
)
2808 boolean_t needs_update
= FALSE
;
2809 int kern_promotion_schedpri
= thread_get_inheritor_turnstile_sched_priority(thread
);
2812 * For now just assert that kern_promotion_schedpri <= MAXPRI_PROMOTE.
2813 * TURNSTILE_KERNEL_PROMOTE adds threads on the waitq already capped to MAXPRI_PROMOTE
2814 * and propagates the priority through the chain with the same cap, because as of now it does
2815 * not differenciate on the kernel primitive.
2817 * If this assumption will change with the adoption of a kernel primitive that does not
2818 * cap the when adding/propagating,
2819 * then here is the place to put the generic cap for all kernel primitives
2820 * (converts the assert to kern_promotion_schedpri = MIN(priority, MAXPRI_PROMOTE))
2822 assert(kern_promotion_schedpri
<= MAXPRI_PROMOTE
);
2824 if (kern_promotion_schedpri
!= thread
->kern_promotion_schedpri
) {
2826 DBG_MACH_SCHED
, MACH_TURNSTILE_KERNEL_CHANGE
) | DBG_FUNC_NONE
,
2828 kern_promotion_schedpri
,
2829 thread
->kern_promotion_schedpri
);
2831 needs_update
= TRUE
;
2832 thread
->kern_promotion_schedpri
= kern_promotion_schedpri
;
2833 thread_recompute_sched_pri(thread
, SETPRI_DEFAULT
);
2836 return needs_update
;
2840 * Check if the user promotion on thread has changed
2843 * thread locked on entry, might drop the thread lock
2847 thread_recompute_user_promotion_locked(thread_t thread
)
2849 boolean_t needs_update
= FALSE
;
2850 struct task_pend_token pend_token
= {};
2851 int user_promotion_basepri
= MIN(thread_get_inheritor_turnstile_base_priority(thread
), MAXPRI_USER
);
2852 int old_base_pri
= thread
->base_pri
;
2853 thread_qos_t qos_promotion
;
2855 /* Check if user promotion has changed */
2856 if (thread
->user_promotion_basepri
== user_promotion_basepri
) {
2857 return needs_update
;
2859 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2860 (TURNSTILE_CODE(TURNSTILE_PRIORITY_OPERATIONS
, (THREAD_USER_PROMOTION_CHANGE
))) | DBG_FUNC_NONE
,
2862 user_promotion_basepri
,
2863 thread
->user_promotion_basepri
,
2866 DBG_MACH_SCHED
, MACH_TURNSTILE_USER_CHANGE
) | DBG_FUNC_NONE
,
2868 user_promotion_basepri
,
2869 thread
->user_promotion_basepri
);
2872 /* Update the user promotion base pri */
2873 thread
->user_promotion_basepri
= user_promotion_basepri
;
2874 pend_token
.tpt_force_recompute_pri
= 1;
2876 if (user_promotion_basepri
<= MAXPRI_THROTTLE
) {
2877 qos_promotion
= THREAD_QOS_UNSPECIFIED
;
2879 qos_promotion
= thread_user_promotion_qos_for_pri(user_promotion_basepri
);
2882 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
2883 TASK_POLICY_QOS_PROMOTE
, qos_promotion
, 0, &pend_token
);
2885 if (thread_get_waiting_turnstile(thread
) &&
2886 thread
->base_pri
!= old_base_pri
) {
2887 needs_update
= TRUE
;
2890 thread_unlock(thread
);
2892 thread_policy_update_complete_unlocked(thread
, &pend_token
);
2894 thread_lock(thread
);
2896 return needs_update
;
2900 * Convert the thread user promotion base pri to qos for threads in qos world.
2901 * For priority above UI qos, the qos would be set to UI.
2904 thread_user_promotion_qos_for_pri(int priority
)
2907 for (qos
= THREAD_QOS_USER_INTERACTIVE
; qos
> THREAD_QOS_MAINTENANCE
; qos
--) {
2908 if (thread_qos_policy_params
.qos_pri
[qos
] <= priority
) {
2912 return THREAD_QOS_MAINTENANCE
;
2916 * Set the thread's QoS Kevent override
2917 * Owned by the Kevent subsystem
2919 * May be called with spinlocks held, but not spinlocks
2920 * that may deadlock against the thread lock, the throttle lock, or the SFI lock.
2922 * One 'add' must be balanced by one 'drop'.
2923 * Between 'add' and 'drop', the overide QoS value may be updated with an 'update'.
2924 * Before the thread is deallocated, there must be 0 remaining overrides.
2927 thread_kevent_override(thread_t thread
,
2928 uint32_t qos_override
,
2929 boolean_t is_new_override
)
2931 struct task_pend_token pend_token
= {};
2932 boolean_t needs_update
;
2934 spl_t s
= splsched();
2935 thread_lock(thread
);
2937 uint32_t old_override
= thread
->requested_policy
.thrp_qos_kevent_override
;
2939 assert(qos_override
> THREAD_QOS_UNSPECIFIED
);
2940 assert(qos_override
< THREAD_QOS_LAST
);
2942 if (is_new_override
) {
2943 if (thread
->kevent_overrides
++ == 0) {
2944 /* This add is the first override for this thread */
2945 assert(old_override
== THREAD_QOS_UNSPECIFIED
);
2947 /* There are already other overrides in effect for this thread */
2948 assert(old_override
> THREAD_QOS_UNSPECIFIED
);
2951 /* There must be at least one override (the previous add call) in effect */
2952 assert(thread
->kevent_overrides
> 0);
2953 assert(old_override
> THREAD_QOS_UNSPECIFIED
);
2957 * We can't allow lowering if there are several IPC overrides because
2958 * the caller can't possibly know the whole truth
2960 if (thread
->kevent_overrides
== 1) {
2961 needs_update
= qos_override
!= old_override
;
2963 needs_update
= qos_override
> old_override
;
2967 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
2968 TASK_POLICY_QOS_KEVENT_OVERRIDE
,
2969 qos_override
, 0, &pend_token
);
2970 assert(pend_token
.tpt_update_sockets
== 0);
2973 thread_unlock(thread
);
2976 thread_policy_update_complete_unlocked(thread
, &pend_token
);
2980 thread_add_kevent_override(thread_t thread
, uint32_t qos_override
)
2982 thread_kevent_override(thread
, qos_override
, TRUE
);
2986 thread_update_kevent_override(thread_t thread
, uint32_t qos_override
)
2988 thread_kevent_override(thread
, qos_override
, FALSE
);
2992 thread_drop_kevent_override(thread_t thread
)
2994 struct task_pend_token pend_token
= {};
2996 spl_t s
= splsched();
2997 thread_lock(thread
);
2999 assert(thread
->kevent_overrides
> 0);
3001 if (--thread
->kevent_overrides
== 0) {
3003 * There are no more overrides for this thread, so we should
3004 * clear out the saturated override value
3007 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
3008 TASK_POLICY_QOS_KEVENT_OVERRIDE
, THREAD_QOS_UNSPECIFIED
,
3012 thread_unlock(thread
);
3015 thread_policy_update_complete_unlocked(thread
, &pend_token
);
3019 * Set the thread's QoS Workloop Servicer override
3020 * Owned by the Kevent subsystem
3022 * May be called with spinlocks held, but not spinlocks
3023 * that may deadlock against the thread lock, the throttle lock, or the SFI lock.
3025 * One 'add' must be balanced by one 'drop'.
3026 * Between 'add' and 'drop', the overide QoS value may be updated with an 'update'.
3027 * Before the thread is deallocated, there must be 0 remaining overrides.
3030 thread_servicer_override(thread_t thread
,
3031 uint32_t qos_override
,
3032 boolean_t is_new_override
)
3034 struct task_pend_token pend_token
= {};
3036 spl_t s
= splsched();
3037 thread_lock(thread
);
3039 if (is_new_override
) {
3040 assert(!thread
->requested_policy
.thrp_qos_wlsvc_override
);
3042 assert(thread
->requested_policy
.thrp_qos_wlsvc_override
);
3045 proc_set_thread_policy_spinlocked(thread
, TASK_POLICY_ATTRIBUTE
,
3046 TASK_POLICY_QOS_SERVICER_OVERRIDE
,
3047 qos_override
, 0, &pend_token
);
3049 thread_unlock(thread
);
3052 assert(pend_token
.tpt_update_sockets
== 0);
3053 thread_policy_update_complete_unlocked(thread
, &pend_token
);
3057 thread_add_servicer_override(thread_t thread
, uint32_t qos_override
)
3059 assert(qos_override
> THREAD_QOS_UNSPECIFIED
);
3060 assert(qos_override
< THREAD_QOS_LAST
);
3062 thread_servicer_override(thread
, qos_override
, TRUE
);
3066 thread_update_servicer_override(thread_t thread
, uint32_t qos_override
)
3068 assert(qos_override
> THREAD_QOS_UNSPECIFIED
);
3069 assert(qos_override
< THREAD_QOS_LAST
);
3071 thread_servicer_override(thread
, qos_override
, FALSE
);
3075 thread_drop_servicer_override(thread_t thread
)
3077 thread_servicer_override(thread
, THREAD_QOS_UNSPECIFIED
, FALSE
);
3081 /* Get current requested qos / relpri, may be called from spinlock context */
3083 thread_get_requested_qos(thread_t thread
, int *relpri
)
3085 int relprio_value
= 0;
3088 qos
= proc_get_thread_policy_locked(thread
, TASK_POLICY_ATTRIBUTE
,
3089 TASK_POLICY_QOS_AND_RELPRIO
, &relprio_value
);
3091 *relpri
= -relprio_value
;
3097 * This function will promote the thread priority
3098 * since exec could block other threads calling
3099 * proc_find on the proc. This boost must be removed
3100 * via call to thread_clear_exec_promotion.
3102 * This should be replaced with a generic 'priority inheriting gate' mechanism (24194397)
3105 thread_set_exec_promotion(thread_t thread
)
3107 spl_t s
= splsched();
3108 thread_lock(thread
);
3110 sched_thread_promote_reason(thread
, TH_SFLAG_EXEC_PROMOTED
, 0);
3112 thread_unlock(thread
);
3117 * This function will clear the exec thread
3118 * promotion set on the thread by thread_set_exec_promotion.
3121 thread_clear_exec_promotion(thread_t thread
)
3123 spl_t s
= splsched();
3124 thread_lock(thread
);
3126 sched_thread_unpromote_reason(thread
, TH_SFLAG_EXEC_PROMOTED
, 0);
3128 thread_unlock(thread
);