2 * Copyright (c) 2000-2004 Apple Computer, 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/task_server.h>
32 #include <kern/sched.h>
33 #include <kern/task.h>
34 #include <mach/thread_policy.h>
35 #include <sys/errno.h>
36 #include <sys/resource.h>
37 #include <machine/limits.h>
38 #include <kern/ledger.h>
39 #include <kern/thread_call.h>
41 #include <kern/coalition.h>
43 #include <kern/telemetry.h>
46 #if IMPORTANCE_INHERITANCE
47 #include <ipc/ipc_importance.h>
49 #include <mach/machine/sdt.h>
50 #endif /* IMPORTANCE_DEBUG */
51 #endif /* IMPORTANCE_INHERITACE */
53 #include <sys/kdebug.h>
58 * This subsystem manages task and thread IO priority and backgrounding,
59 * as well as importance inheritance, process suppression, task QoS, and apptype.
60 * These properties have a suprising number of complex interactions, so they are
61 * centralized here in one state machine to simplify the implementation of those interactions.
64 * Threads and tasks have three policy fields: requested, effective, and pending.
65 * Requested represents the wishes of each interface that influences task policy.
66 * Effective represents the distillation of that policy into a set of behaviors.
67 * Pending represents updates that haven't been applied yet.
69 * Each interface that has an input into the task policy state machine controls a field in requested.
70 * If the interface has a getter, it returns what is in the field in requested, but that is
71 * not necessarily what is actually in effect.
73 * All kernel subsystems that behave differently based on task policy call into
74 * the get_effective_policy function, which returns the decision of the task policy state machine
75 * for that subsystem by querying only the 'effective' field.
77 * Policy change operations:
78 * Here are the steps to change a policy on a task or thread:
80 * 2) Change requested field for the relevant policy
81 * 3) Run a task policy update, which recalculates effective based on requested,
82 * then takes a diff between the old and new versions of requested and calls the relevant
83 * other subsystems to apply these changes, and updates the pending field.
85 * 5) Run task policy update complete, which looks at the pending field to update
86 * subsystems which cannot be touched while holding the task lock.
88 * To add a new requested policy, add the field in the requested struct, the flavor in task.h,
89 * the setter and getter in proc_(set|get)_task_policy*, and dump the state in task_requested_bitfield,
90 * then set up the effects of that behavior in task_policy_update*. If the policy manifests
91 * itself as a distinct effective policy, add it to the effective struct and add it to the
92 * proc_get_effective_policy accessor.
94 * Most policies are set via proc_set_task_policy, but policies that don't fit that interface
95 * roll their own lock/set/update/unlock/complete code inside this file.
100 * These are a set of behaviors that can be requested for a task. They currently have specific
101 * implied actions when they're enabled, but they may be made customizable in the future.
103 * When the affected task is boosted, we temporarily disable the suppression behaviors
104 * so that the affected process has a chance to run so it can call the API to permanently
105 * disable the suppression behaviors.
109 * Changing task policy on a task or thread takes the task lock, and not the thread lock.
110 * TODO: Should changing policy on a thread take the thread lock instead?
112 * Querying the effective policy does not take the task lock, to prevent deadlocks or slowdown in sensitive code.
113 * This means that any notification of state change needs to be externally synchronized.
117 extern const qos_policy_params_t thread_qos_policy_params
;
119 /* for task holds without dropping the lock */
120 extern void task_hold_locked(task_t task
);
121 extern void task_release_locked(task_t task
);
122 extern void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
124 extern void thread_recompute_qos(thread_t thread
);
126 /* Task policy related helper functions */
127 static void proc_set_task_policy_locked(task_t task
, thread_t thread
, int category
, int flavor
, int value
);
128 static void proc_set_task_policy2_locked(task_t task
, thread_t thread
, int category
, int flavor
, int value1
, int value2
);
130 static void task_policy_update_locked(task_t task
, thread_t thread
, task_pend_token_t pend_token
);
131 static void task_policy_update_internal_locked(task_t task
, thread_t thread
, boolean_t in_create
, task_pend_token_t pend_token
);
132 static void task_policy_update_task_locked(task_t task
, boolean_t update_throttle
, boolean_t update_bg_throttle
, boolean_t update_sfi
);
133 static void task_policy_update_thread_locked(thread_t thread
, int update_cpu
, boolean_t update_throttle
, boolean_t update_sfi
, boolean_t update_qos
);
134 static boolean_t
task_policy_update_coalition_focal_tasks(task_t task
, int prev_role
, int next_role
);
136 static int proc_get_effective_policy(task_t task
, thread_t thread
, int policy
);
138 static void proc_iopol_to_tier(int iopolicy
, int *tier
, int *passive
);
139 static int proc_tier_to_iopol(int tier
, int passive
);
141 static uintptr_t trequested_0(task_t task
, thread_t thread
);
142 static uintptr_t trequested_1(task_t task
, thread_t thread
);
143 static uintptr_t teffective_0(task_t task
, thread_t thread
);
144 static uintptr_t teffective_1(task_t task
, thread_t thread
);
145 static uint32_t tpending(task_pend_token_t pend_token
);
146 static uint64_t task_requested_bitfield(task_t task
, thread_t thread
);
147 static uint64_t task_effective_bitfield(task_t task
, thread_t thread
);
149 void proc_get_thread_policy(thread_t thread
, thread_policy_state_t info
);
151 /* CPU Limits related helper functions */
152 static int task_get_cpuusage(task_t task
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
, int *scope
);
153 int task_set_cpuusage(task_t task
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
, int scope
, int entitled
);
154 static int task_clear_cpuusage_locked(task_t task
, int cpumon_entitled
);
155 int task_disable_cpumon(task_t task
);
156 static int task_apply_resource_actions(task_t task
, int type
);
157 void task_action_cpuusage(thread_call_param_t param0
, thread_call_param_t param1
);
158 void proc_init_cpumon_params(void);
161 int proc_pid(void *proc
);
162 extern int proc_selfpid(void);
163 extern char * proc_name_address(void *p
);
164 extern void rethrottle_thread(void * uthread
);
165 extern void proc_apply_task_networkbg(void * bsd_info
, thread_t thread
);
166 #endif /* MACH_BSD */
168 extern zone_t thread_qos_override_zone
;
169 static boolean_t
_proc_thread_qos_remove_override_internal(task_t task
, thread_t thread
, uint64_t tid
, user_addr_t resource
, int resource_type
, boolean_t reset
);
172 /* Importance Inheritance related helper functions */
174 #if IMPORTANCE_INHERITANCE
176 static void task_add_importance_watchport(task_t task
, mach_port_t port
, int *boostp
);
177 static void task_importance_update_live_donor(task_t target_task
);
179 #endif /* IMPORTANCE_INHERITANCE */
182 #define __impdebug_only
184 #define __impdebug_only __unused
187 #if IMPORTANCE_INHERITANCE
190 #define __imp_only __unused
193 #define TASK_LOCKED 1
194 #define TASK_UNLOCKED 0
196 #define DO_LOWPRI_CPU 1
197 #define UNDO_LOWPRI_CPU 2
199 /* Macros for making tracing simpler */
201 #define tpriority(task, thread) ((uintptr_t)(thread == THREAD_NULL ? (task->priority) : (thread->priority)))
202 #define tisthread(thread) (thread == THREAD_NULL ? TASK_POLICY_TASK : TASK_POLICY_THREAD)
203 #define targetid(task, thread) ((uintptr_t)(thread == THREAD_NULL ? (audit_token_pid_from_task(task)) : (thread->thread_id)))
206 * Default parameters for certain policies
209 int proc_standard_daemon_tier
= THROTTLE_LEVEL_TIER1
;
210 int proc_suppressed_disk_tier
= THROTTLE_LEVEL_TIER1
;
211 int proc_tal_disk_tier
= THROTTLE_LEVEL_TIER1
;
213 int proc_graphics_timer_qos
= (LATENCY_QOS_TIER_0
& 0xFF);
215 const int proc_default_bg_iotier
= THROTTLE_LEVEL_TIER2
;
217 /* Latency/throughput QoS fields remain zeroed, i.e. TIER_UNSPECIFIED at creation */
218 const struct task_requested_policy default_task_requested_policy
= {
219 .bg_iotier
= proc_default_bg_iotier
221 const struct task_effective_policy default_task_effective_policy
= {};
222 const struct task_pended_policy default_task_pended_policy
= {};
225 * Default parameters for CPU usage monitor.
227 * Default setting is 50% over 3 minutes.
229 #define DEFAULT_CPUMON_PERCENTAGE 50
230 #define DEFAULT_CPUMON_INTERVAL (3 * 60)
232 uint8_t proc_max_cpumon_percentage
;
233 uint64_t proc_max_cpumon_interval
;
236 qos_latency_policy_validate(task_latency_qos_t ltier
) {
237 if ((ltier
!= LATENCY_QOS_TIER_UNSPECIFIED
) &&
238 ((ltier
> LATENCY_QOS_TIER_5
) || (ltier
< LATENCY_QOS_TIER_0
)))
239 return KERN_INVALID_ARGUMENT
;
245 qos_throughput_policy_validate(task_throughput_qos_t ttier
) {
246 if ((ttier
!= THROUGHPUT_QOS_TIER_UNSPECIFIED
) &&
247 ((ttier
> THROUGHPUT_QOS_TIER_5
) || (ttier
< THROUGHPUT_QOS_TIER_0
)))
248 return KERN_INVALID_ARGUMENT
;
254 task_qos_policy_validate(task_qos_policy_t qosinfo
, mach_msg_type_number_t count
) {
255 if (count
< TASK_QOS_POLICY_COUNT
)
256 return KERN_INVALID_ARGUMENT
;
258 task_latency_qos_t ltier
= qosinfo
->task_latency_qos_tier
;
259 task_throughput_qos_t ttier
= qosinfo
->task_throughput_qos_tier
;
261 kern_return_t kr
= qos_latency_policy_validate(ltier
);
263 if (kr
!= KERN_SUCCESS
)
266 kr
= qos_throughput_policy_validate(ttier
);
272 qos_extract(uint32_t qv
) {
277 qos_latency_policy_package(uint32_t qv
) {
278 return (qv
== LATENCY_QOS_TIER_UNSPECIFIED
) ? LATENCY_QOS_TIER_UNSPECIFIED
: ((0xFF << 16) | qv
);
282 qos_throughput_policy_package(uint32_t qv
) {
283 return (qv
== THROUGHPUT_QOS_TIER_UNSPECIFIED
) ? THROUGHPUT_QOS_TIER_UNSPECIFIED
: ((0xFE << 16) | qv
);
286 /* TEMPORARY boot-arg controlling task_policy suppression (App Nap) */
287 static boolean_t task_policy_suppression_disable
= FALSE
;
292 task_policy_flavor_t flavor
,
293 task_policy_t policy_info
,
294 mach_msg_type_number_t count
)
296 kern_return_t result
= KERN_SUCCESS
;
298 if (task
== TASK_NULL
|| task
== kernel_task
)
299 return (KERN_INVALID_ARGUMENT
);
303 case TASK_CATEGORY_POLICY
: {
304 task_category_policy_t info
= (task_category_policy_t
)policy_info
;
306 if (count
< TASK_CATEGORY_POLICY_COUNT
)
307 return (KERN_INVALID_ARGUMENT
);
311 case TASK_FOREGROUND_APPLICATION
:
312 case TASK_BACKGROUND_APPLICATION
:
313 case TASK_DEFAULT_APPLICATION
:
314 proc_set_task_policy(task
, THREAD_NULL
,
315 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
319 case TASK_CONTROL_APPLICATION
:
320 if (task
!= current_task() || task
->sec_token
.val
[0] != 0)
321 result
= KERN_INVALID_ARGUMENT
;
323 proc_set_task_policy(task
, THREAD_NULL
,
324 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
328 case TASK_GRAPHICS_SERVER
:
329 /* TODO: Restrict this role to FCFS <rdar://problem/12552788> */
330 if (task
!= current_task() || task
->sec_token
.val
[0] != 0)
331 result
= KERN_INVALID_ARGUMENT
;
333 proc_set_task_policy(task
, THREAD_NULL
,
334 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
338 result
= KERN_INVALID_ARGUMENT
;
340 } /* switch (info->role) */
345 /* Desired energy-efficiency/performance "quality-of-service" */
346 case TASK_BASE_QOS_POLICY
:
347 case TASK_OVERRIDE_QOS_POLICY
:
349 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
350 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
352 if (kr
!= KERN_SUCCESS
)
356 uint32_t lqos
= qos_extract(qosinfo
->task_latency_qos_tier
);
357 uint32_t tqos
= qos_extract(qosinfo
->task_throughput_qos_tier
);
359 proc_set_task_policy2(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
,
360 flavor
== TASK_BASE_QOS_POLICY
? TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
: TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
,
365 case TASK_BASE_LATENCY_QOS_POLICY
:
367 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
368 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
370 if (kr
!= KERN_SUCCESS
)
373 uint32_t lqos
= qos_extract(qosinfo
->task_latency_qos_tier
);
375 proc_set_task_policy(task
, NULL
, TASK_POLICY_ATTRIBUTE
, TASK_BASE_LATENCY_QOS_POLICY
, lqos
);
379 case TASK_BASE_THROUGHPUT_QOS_POLICY
:
381 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
382 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
384 if (kr
!= KERN_SUCCESS
)
387 uint32_t tqos
= qos_extract(qosinfo
->task_throughput_qos_tier
);
389 proc_set_task_policy(task
, NULL
, TASK_POLICY_ATTRIBUTE
, TASK_BASE_THROUGHPUT_QOS_POLICY
, tqos
);
393 case TASK_SUPPRESSION_POLICY
:
396 task_suppression_policy_t info
= (task_suppression_policy_t
)policy_info
;
398 if (count
< TASK_SUPPRESSION_POLICY_COUNT
)
399 return (KERN_INVALID_ARGUMENT
);
401 struct task_qos_policy qosinfo
;
403 qosinfo
.task_latency_qos_tier
= info
->timer_throttle
;
404 qosinfo
.task_throughput_qos_tier
= info
->throughput_qos
;
406 kern_return_t kr
= task_qos_policy_validate(&qosinfo
, TASK_QOS_POLICY_COUNT
);
408 if (kr
!= KERN_SUCCESS
)
411 /* TEMPORARY disablement of task suppression */
412 if (task_policy_suppression_disable
&& info
->active
)
415 struct task_pend_token pend_token
= {};
419 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
420 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION
, info
->active
)) | DBG_FUNC_START
,
421 proc_selfpid(), audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
),
422 trequested_1(task
, THREAD_NULL
), 0);
424 task
->requested_policy
.t_sup_active
= (info
->active
) ? 1 : 0;
425 task
->requested_policy
.t_sup_lowpri_cpu
= (info
->lowpri_cpu
) ? 1 : 0;
426 task
->requested_policy
.t_sup_timer
= qos_extract(info
->timer_throttle
);
427 task
->requested_policy
.t_sup_disk
= (info
->disk_throttle
) ? 1 : 0;
428 task
->requested_policy
.t_sup_cpu_limit
= (info
->cpu_limit
) ? 1 : 0;
429 task
->requested_policy
.t_sup_suspend
= (info
->suspend
) ? 1 : 0;
430 task
->requested_policy
.t_sup_throughput
= qos_extract(info
->throughput_qos
);
431 task
->requested_policy
.t_sup_cpu
= (info
->suppressed_cpu
) ? 1 : 0;
432 task
->requested_policy
.t_sup_bg_sockets
= (info
->background_sockets
) ? 1 : 0;
434 task_policy_update_locked(task
, THREAD_NULL
, &pend_token
);
438 task_policy_update_complete_unlocked(task
, THREAD_NULL
, &pend_token
);
440 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
441 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION
, info
->active
)) | DBG_FUNC_END
,
442 proc_selfpid(), audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
),
443 trequested_1(task
, THREAD_NULL
), 0);
450 result
= KERN_INVALID_ARGUMENT
;
457 /* Sets BSD 'nice' value on the task */
461 integer_t importance
)
463 if (task
== TASK_NULL
|| task
== kernel_task
)
464 return (KERN_INVALID_ARGUMENT
);
471 return (KERN_TERMINATED
);
474 if (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
) >= TASK_CONTROL_APPLICATION
) {
477 return (KERN_INVALID_ARGUMENT
);
480 task
->importance
= importance
;
482 /* TODO: tracepoint? */
484 /* Redrive only the task priority calculation */
485 task_policy_update_task_locked(task
, FALSE
, FALSE
, FALSE
);
489 return (KERN_SUCCESS
);
495 task_policy_flavor_t flavor
,
496 task_policy_t policy_info
,
497 mach_msg_type_number_t
*count
,
498 boolean_t
*get_default
)
500 if (task
== TASK_NULL
|| task
== kernel_task
)
501 return (KERN_INVALID_ARGUMENT
);
505 case TASK_CATEGORY_POLICY
:
507 task_category_policy_t info
= (task_category_policy_t
)policy_info
;
509 if (*count
< TASK_CATEGORY_POLICY_COUNT
)
510 return (KERN_INVALID_ARGUMENT
);
513 info
->role
= TASK_UNSPECIFIED
;
515 info
->role
= proc_get_task_policy(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
519 case TASK_BASE_QOS_POLICY
: /* FALLTHRU */
520 case TASK_OVERRIDE_QOS_POLICY
:
522 task_qos_policy_t info
= (task_qos_policy_t
)policy_info
;
524 if (*count
< TASK_QOS_POLICY_COUNT
)
525 return (KERN_INVALID_ARGUMENT
);
528 info
->task_latency_qos_tier
= LATENCY_QOS_TIER_UNSPECIFIED
;
529 info
->task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_UNSPECIFIED
;
530 } else if (flavor
== TASK_BASE_QOS_POLICY
) {
533 proc_get_task_policy2(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
, &value1
, &value2
);
535 info
->task_latency_qos_tier
= qos_latency_policy_package(value1
);
536 info
->task_throughput_qos_tier
= qos_throughput_policy_package(value2
);
538 } else if (flavor
== TASK_OVERRIDE_QOS_POLICY
) {
541 proc_get_task_policy2(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
, &value1
, &value2
);
543 info
->task_latency_qos_tier
= qos_latency_policy_package(value1
);
544 info
->task_throughput_qos_tier
= qos_throughput_policy_package(value2
);
550 case TASK_POLICY_STATE
:
552 task_policy_state_t info
= (task_policy_state_t
)policy_info
;
554 if (*count
< TASK_POLICY_STATE_COUNT
)
555 return (KERN_INVALID_ARGUMENT
);
557 /* Only root can get this info */
558 if (current_task()->sec_token
.val
[0] != 0)
559 return KERN_PROTECTION_FAILURE
;
565 info
->imp_assertcnt
= 0;
566 info
->imp_externcnt
= 0;
568 info
->imp_transitions
= 0;
572 info
->requested
= task_requested_bitfield(task
, THREAD_NULL
);
573 info
->effective
= task_effective_bitfield(task
, THREAD_NULL
);
577 if (task
->task_imp_base
!= NULL
) {
578 info
->imp_assertcnt
= task
->task_imp_base
->iit_assertcnt
;
579 info
->imp_externcnt
= IIT_EXTERN(task
->task_imp_base
);
580 info
->flags
|= (task_is_marked_importance_receiver(task
) ? TASK_IMP_RECEIVER
: 0);
581 info
->flags
|= (task_is_marked_importance_denap_receiver(task
) ? TASK_DENAP_RECEIVER
: 0);
582 info
->flags
|= (task_is_marked_importance_donor(task
) ? TASK_IMP_DONOR
: 0);
583 info
->flags
|= (task_is_marked_live_importance_donor(task
) ? TASK_IMP_LIVE_DONOR
: 0);
584 info
->imp_transitions
= task
->task_imp_base
->iit_transitions
;
586 info
->imp_assertcnt
= 0;
587 info
->imp_externcnt
= 0;
588 info
->imp_transitions
= 0;
593 info
->reserved
[0] = 0;
594 info
->reserved
[1] = 0;
599 case TASK_SUPPRESSION_POLICY
:
601 task_suppression_policy_t info
= (task_suppression_policy_t
)policy_info
;
603 if (*count
< TASK_SUPPRESSION_POLICY_COUNT
)
604 return (KERN_INVALID_ARGUMENT
);
610 info
->lowpri_cpu
= 0;
611 info
->timer_throttle
= LATENCY_QOS_TIER_UNSPECIFIED
;
612 info
->disk_throttle
= 0;
615 info
->throughput_qos
= 0;
616 info
->suppressed_cpu
= 0;
618 info
->active
= task
->requested_policy
.t_sup_active
;
619 info
->lowpri_cpu
= task
->requested_policy
.t_sup_lowpri_cpu
;
620 info
->timer_throttle
= qos_latency_policy_package(task
->requested_policy
.t_sup_timer
);
621 info
->disk_throttle
= task
->requested_policy
.t_sup_disk
;
622 info
->cpu_limit
= task
->requested_policy
.t_sup_cpu_limit
;
623 info
->suspend
= task
->requested_policy
.t_sup_suspend
;
624 info
->throughput_qos
= qos_throughput_policy_package(task
->requested_policy
.t_sup_throughput
);
625 info
->suppressed_cpu
= task
->requested_policy
.t_sup_cpu
;
626 info
->background_sockets
= task
->requested_policy
.t_sup_bg_sockets
;
634 return (KERN_INVALID_ARGUMENT
);
637 return (KERN_SUCCESS
);
641 * Called at task creation
642 * We calculate the correct effective but don't apply it to anything yet.
643 * The threads, etc will inherit from the task as they get created.
646 task_policy_create(task_t task
, int parent_boosted
)
648 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
) {
649 if (parent_boosted
) {
650 task
->requested_policy
.t_apptype
= TASK_APPTYPE_DAEMON_INTERACTIVE
;
651 task_importance_mark_donor(task
, TRUE
);
653 task
->requested_policy
.t_apptype
= TASK_APPTYPE_DAEMON_BACKGROUND
;
654 task_importance_mark_receiver(task
, FALSE
);
658 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
659 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
660 audit_token_pid_from_task(task
), teffective_0(task
, THREAD_NULL
),
661 teffective_1(task
, THREAD_NULL
), tpriority(task
, THREAD_NULL
), 0);
663 task_policy_update_internal_locked(task
, THREAD_NULL
, TRUE
, NULL
);
665 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
666 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
667 audit_token_pid_from_task(task
), teffective_0(task
, THREAD_NULL
),
668 teffective_1(task
, THREAD_NULL
), tpriority(task
, THREAD_NULL
), 0);
670 task_importance_update_live_donor(task
);
671 task_policy_update_task_locked(task
, FALSE
, FALSE
, FALSE
);
675 thread_policy_create(thread_t thread
)
677 task_t task
= thread
->task
;
679 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
680 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_THREAD
))) | DBG_FUNC_START
,
681 targetid(task
, thread
), teffective_0(task
, thread
),
682 teffective_1(task
, thread
), tpriority(task
, thread
), 0);
684 task_policy_update_internal_locked(task
, thread
, TRUE
, NULL
);
686 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
687 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_THREAD
))) | DBG_FUNC_END
,
688 targetid(task
, thread
), teffective_0(task
, thread
),
689 teffective_1(task
, thread
), tpriority(task
, thread
), 0);
693 task_policy_update_locked(task_t task
, thread_t thread
, task_pend_token_t pend_token
)
695 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
696 (IMPORTANCE_CODE(IMP_UPDATE
, tisthread(thread
)) | DBG_FUNC_START
),
697 targetid(task
, thread
), teffective_0(task
, thread
),
698 teffective_1(task
, thread
), tpriority(task
, thread
), 0);
700 task_policy_update_internal_locked(task
, thread
, FALSE
, pend_token
);
702 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
703 (IMPORTANCE_CODE(IMP_UPDATE
, tisthread(thread
))) | DBG_FUNC_END
,
704 targetid(task
, thread
), teffective_0(task
, thread
),
705 teffective_1(task
, thread
), tpriority(task
, thread
), 0);
709 * One state update function TO RULE THEM ALL
711 * This function updates the task or thread effective policy fields
712 * and pushes the results to the relevant subsystems.
714 * Must call update_complete after unlocking the task,
715 * as some subsystems cannot be updated while holding the task lock.
717 * Called with task locked, not thread
721 task_policy_update_internal_locked(task_t task
, thread_t thread
, boolean_t in_create
, task_pend_token_t pend_token
)
723 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
727 * Gather requested policy
730 struct task_requested_policy requested
=
731 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
736 * Calculate new effective policies from requested policy and task state
738 * If in an 'on_task' block, must only look at and set fields starting with t_
739 * If operating on a task, don't touch anything starting with th_
740 * If operating on a thread, don't touch anything starting with t_
741 * Don't change requested, it won't take effect
744 struct task_effective_policy next
= {};
745 struct task_effective_policy task_effective
;
747 /* Calculate QoS policies */
750 /* Update task role */
751 next
.t_role
= requested
.t_role
;
753 /* Set task qos clamp and ceiling */
754 next
.t_qos_clamp
= requested
.t_qos_clamp
;
756 if (requested
.t_apptype
== TASK_APPTYPE_APP_DEFAULT
||
757 requested
.t_apptype
== TASK_APPTYPE_APP_TAL
) {
759 switch (next
.t_role
) {
760 case TASK_FOREGROUND_APPLICATION
:
761 /* Foreground apps get urgent scheduler priority */
762 next
.qos_ui_is_urgent
= 1;
763 next
.t_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
766 case TASK_BACKGROUND_APPLICATION
:
767 /* This is really 'non-focal but on-screen' */
768 next
.t_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
771 case TASK_DEFAULT_APPLICATION
:
772 /* This is 'may render UI but we don't know if it's focal/nonfocal' */
773 next
.t_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
776 case TASK_NONUI_APPLICATION
:
777 /* i.e. 'off-screen' */
778 next
.t_qos_ceiling
= THREAD_QOS_LEGACY
;
781 case TASK_CONTROL_APPLICATION
:
782 case TASK_GRAPHICS_SERVER
:
783 next
.qos_ui_is_urgent
= 1;
784 next
.t_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
787 case TASK_UNSPECIFIED
:
789 /* Apps that don't have an application role get
790 * USER_INTERACTIVE and USER_INITIATED squashed to LEGACY */
791 next
.t_qos_ceiling
= THREAD_QOS_LEGACY
;
795 /* Daemons get USER_INTERACTIVE squashed to USER_INITIATED */
796 next
.t_qos_ceiling
= THREAD_QOS_USER_INITIATED
;
800 * Set thread qos tier
801 * Note that an override only overrides the QoS field, not other policy settings.
802 * A thread must already be participating in QoS for override to take effect
805 /* Snapshot the task's effective policy */
806 task_effective
= task
->effective_policy
;
808 next
.qos_ui_is_urgent
= task_effective
.qos_ui_is_urgent
;
810 if ((requested
.thrp_qos_override
!= THREAD_QOS_UNSPECIFIED
) && (requested
.thrp_qos
!= THREAD_QOS_UNSPECIFIED
))
811 next
.thep_qos
= MAX(requested
.thrp_qos_override
, requested
.thrp_qos
);
813 next
.thep_qos
= requested
.thrp_qos
;
815 /* A task clamp will result in an effective QoS even when requested is UNSPECIFIED */
816 if (task_effective
.t_qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
817 if (next
.thep_qos
!= THREAD_QOS_UNSPECIFIED
)
818 next
.thep_qos
= MIN(task_effective
.t_qos_clamp
, next
.thep_qos
);
820 next
.thep_qos
= task_effective
.t_qos_clamp
;
823 /* The ceiling only applies to threads that are in the QoS world */
824 if (task_effective
.t_qos_ceiling
!= THREAD_QOS_UNSPECIFIED
&&
825 next
.thep_qos
!= THREAD_QOS_UNSPECIFIED
) {
826 next
.thep_qos
= MIN(task_effective
.t_qos_ceiling
, next
.thep_qos
);
830 * The QoS relative priority is only applicable when the original programmer's
831 * intended (requested) QoS is in effect. When the QoS is clamped (e.g.
832 * USER_INITIATED-13REL clamped to UTILITY), the relative priority is not honored,
833 * since otherwise it would be lower than unclamped threads. Similarly, in the
834 * presence of boosting, the programmer doesn't know what other actors
835 * are boosting the thread.
837 if ((requested
.thrp_qos
!= THREAD_QOS_UNSPECIFIED
) &&
838 (requested
.thrp_qos
== next
.thep_qos
) &&
839 (requested
.thrp_qos_override
== THREAD_QOS_UNSPECIFIED
)) {
840 next
.thep_qos_relprio
= requested
.thrp_qos_relprio
;
842 next
.thep_qos_relprio
= 0;
846 /* Calculate DARWIN_BG */
847 boolean_t wants_darwinbg
= FALSE
;
848 boolean_t wants_all_sockets_bg
= FALSE
; /* Do I want my existing sockets to be bg */
849 boolean_t wants_watchersbg
= FALSE
; /* Do I want my pidbound threads to be bg */
850 boolean_t wants_tal
= FALSE
; /* Do I want the effects of TAL mode */
853 * If DARWIN_BG has been requested at either level, it's engaged.
854 * Only true DARWIN_BG changes cause watchers to transition.
856 * Backgrounding due to apptype does.
858 if (requested
.int_darwinbg
|| requested
.ext_darwinbg
)
859 wants_watchersbg
= wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
862 /* Background TAL apps are throttled when TAL is enabled */
863 if (requested
.t_apptype
== TASK_APPTYPE_APP_TAL
&&
864 requested
.t_role
== TASK_BACKGROUND_APPLICATION
&&
865 requested
.t_tal_enabled
== 1) {
867 next
.t_tal_engaged
= 1;
870 /* Adaptive daemons are DARWIN_BG unless boosted, and don't get network throttled. */
871 if (requested
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&&
872 requested
.t_boosted
== 0)
873 wants_darwinbg
= TRUE
;
875 /* Background daemons are always DARWIN_BG, no exceptions, and don't get network throttled. */
876 if (requested
.t_apptype
== TASK_APPTYPE_DAEMON_BACKGROUND
)
877 wants_darwinbg
= TRUE
;
879 if (next
.t_qos_clamp
== THREAD_QOS_BACKGROUND
|| next
.t_qos_clamp
== THREAD_QOS_MAINTENANCE
)
880 wants_darwinbg
= TRUE
;
882 if (requested
.th_pidbind_bg
)
883 wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
885 if (requested
.th_workq_bg
)
886 wants_darwinbg
= TRUE
;
888 if (next
.thep_qos
== THREAD_QOS_BACKGROUND
|| next
.thep_qos
== THREAD_QOS_MAINTENANCE
)
889 wants_darwinbg
= TRUE
;
892 /* Calculate side effects of DARWIN_BG */
894 if (wants_darwinbg
) {
896 /* darwinbg threads/tasks always create bg sockets, but we don't always loop over all sockets */
897 next
.new_sockets_bg
= 1;
901 if (wants_all_sockets_bg
)
902 next
.all_sockets_bg
= 1;
904 if (on_task
&& wants_watchersbg
)
905 next
.t_watchers_bg
= 1;
907 /* darwinbg on either task or thread implies background QOS (or lower) */
909 (wants_darwinbg
|| task_effective
.darwinbg
) &&
910 (next
.thep_qos
> THREAD_QOS_BACKGROUND
|| next
.thep_qos
== THREAD_QOS_UNSPECIFIED
)){
911 next
.thep_qos
= THREAD_QOS_BACKGROUND
;
912 next
.thep_qos_relprio
= 0;
915 /* Calculate low CPU priority */
917 boolean_t wants_lowpri_cpu
= FALSE
;
919 if (wants_darwinbg
|| wants_tal
)
920 wants_lowpri_cpu
= TRUE
;
922 if (on_task
&& requested
.t_sup_lowpri_cpu
&& requested
.t_boosted
== 0)
923 wants_lowpri_cpu
= TRUE
;
925 if (wants_lowpri_cpu
)
928 /* Calculate IO policy */
930 /* Update BG IO policy (so we can see if it has changed) */
931 next
.bg_iotier
= requested
.bg_iotier
;
933 int iopol
= THROTTLE_LEVEL_TIER0
;
936 iopol
= MAX(iopol
, requested
.bg_iotier
);
939 if (requested
.t_apptype
== TASK_APPTYPE_DAEMON_STANDARD
)
940 iopol
= MAX(iopol
, proc_standard_daemon_tier
);
942 if (requested
.t_sup_disk
&& requested
.t_boosted
== 0)
943 iopol
= MAX(iopol
, proc_suppressed_disk_tier
);
946 iopol
= MAX(iopol
, proc_tal_disk_tier
);
948 if (next
.t_qos_clamp
!= THREAD_QOS_UNSPECIFIED
)
949 iopol
= MAX(iopol
, thread_qos_policy_params
.qos_iotier
[next
.t_qos_clamp
]);
952 /* Look up the associated IO tier value for the QoS class */
953 iopol
= MAX(iopol
, thread_qos_policy_params
.qos_iotier
[next
.thep_qos
]);
956 iopol
= MAX(iopol
, requested
.int_iotier
);
957 iopol
= MAX(iopol
, requested
.ext_iotier
);
959 next
.io_tier
= iopol
;
961 /* Calculate Passive IO policy */
963 if (requested
.ext_iopassive
|| requested
.int_iopassive
)
966 /* Calculate miscellaneous policy */
969 /* Calculate suppression-active flag */
970 if (requested
.t_sup_active
&& requested
.t_boosted
== 0)
971 next
.t_sup_active
= 1;
973 /* Calculate suspend policy */
974 if (requested
.t_sup_suspend
&& requested
.t_boosted
== 0)
975 next
.t_suspended
= 1;
977 /* Calculate timer QOS */
978 int latency_qos
= requested
.t_base_latency_qos
;
980 if (requested
.t_sup_timer
&& requested
.t_boosted
== 0)
981 latency_qos
= requested
.t_sup_timer
;
983 if (next
.t_qos_clamp
!= THREAD_QOS_UNSPECIFIED
)
984 latency_qos
= MAX(latency_qos
, (int)thread_qos_policy_params
.qos_latency_qos
[next
.t_qos_clamp
]);
986 if (requested
.t_over_latency_qos
!= 0)
987 latency_qos
= requested
.t_over_latency_qos
;
989 /* Treat the windowserver special */
990 if (requested
.t_role
== TASK_GRAPHICS_SERVER
)
991 latency_qos
= proc_graphics_timer_qos
;
993 next
.t_latency_qos
= latency_qos
;
995 /* Calculate throughput QOS */
996 int through_qos
= requested
.t_base_through_qos
;
998 if (requested
.t_sup_throughput
&& requested
.t_boosted
== 0)
999 through_qos
= requested
.t_sup_throughput
;
1001 if (next
.t_qos_clamp
!= THREAD_QOS_UNSPECIFIED
)
1002 through_qos
= MAX(through_qos
, (int)thread_qos_policy_params
.qos_through_qos
[next
.t_qos_clamp
]);
1004 if (requested
.t_over_through_qos
!= 0)
1005 through_qos
= requested
.t_over_through_qos
;
1007 next
.t_through_qos
= through_qos
;
1009 /* Calculate suppressed CPU priority */
1010 if (requested
.t_sup_cpu
&& requested
.t_boosted
== 0)
1011 next
.t_suppressed_cpu
= 1;
1014 * Calculate background sockets
1015 * Don't take into account boosting to limit transition frequency.
1017 if (requested
.t_sup_bg_sockets
){
1018 next
.all_sockets_bg
= 1;
1019 next
.new_sockets_bg
= 1;
1022 /* Apply SFI Managed class bit */
1023 next
.t_sfi_managed
= requested
.t_sfi_managed
;
1025 /* Calculate 'live donor' status for live importance */
1026 switch (requested
.t_apptype
) {
1027 case TASK_APPTYPE_APP_TAL
:
1028 case TASK_APPTYPE_APP_DEFAULT
:
1029 if (requested
.ext_darwinbg
== 0)
1030 next
.t_live_donor
= 1;
1032 next
.t_live_donor
= 0;
1035 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
1036 case TASK_APPTYPE_DAEMON_STANDARD
:
1037 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
1038 case TASK_APPTYPE_DAEMON_BACKGROUND
:
1040 next
.t_live_donor
= 0;
1045 if (requested
.terminated
) {
1047 * Shoot down the throttles that slow down exit or response to SIGTERM
1048 * We don't need to shoot down:
1049 * passive (don't want to cause others to throttle)
1050 * all_sockets_bg (don't need to iterate FDs on every exit)
1051 * new_sockets_bg (doesn't matter for exiting process)
1052 * pidsuspend (jetsam-ed BG process shouldn't run again)
1053 * watchers_bg (watcher threads don't need to be unthrottled)
1054 * t_latency_qos (affects userspace timers only)
1057 next
.terminated
= 1;
1059 next
.lowpri_cpu
= 0;
1060 next
.io_tier
= THROTTLE_LEVEL_TIER0
;
1062 next
.t_tal_engaged
= 0;
1063 next
.t_role
= TASK_UNSPECIFIED
;
1064 next
.t_suppressed_cpu
= 0;
1066 /* TODO: This should only be shot down on SIGTERM, not exit */
1067 next
.t_suspended
= 0;
1075 * Swap out old policy for new policy
1079 /* Acquire thread mutex to synchronize against
1080 * thread_policy_set(). Consider reworking to separate qos
1081 * fields, or locking the task in thread_policy_set.
1082 * A more efficient model would be to make the thread bits
1085 thread_mtx_lock(thread
);
1088 struct task_effective_policy prev
=
1089 (on_task
) ? task
->effective_policy
: thread
->effective_policy
;
1092 * Check for invalid transitions here for easier debugging
1093 * TODO: dump the structs as hex in the panic string
1095 if (task
== kernel_task
&& prev
.all_sockets_bg
!= next
.all_sockets_bg
)
1096 panic("unexpected network change for kernel task");
1098 /* This is the point where the new values become visible to other threads */
1100 task
->effective_policy
= next
;
1102 /* Preserve thread specific latency/throughput QoS modified via
1103 * thread_policy_set(). Inelegant in the extreme, to be reworked.
1105 * If thread QoS class is set, we don't need to preserve the previously set values.
1106 * We should ensure to not accidentally preserve previous thread QoS values if you set a thread
1107 * back to default QoS.
1109 uint32_t lqos
= thread
->effective_policy
.t_latency_qos
, tqos
= thread
->effective_policy
.t_through_qos
;
1111 if (prev
.thep_qos
== THREAD_QOS_UNSPECIFIED
&& next
.thep_qos
== THREAD_QOS_UNSPECIFIED
) {
1112 next
.t_latency_qos
= lqos
;
1113 next
.t_through_qos
= tqos
;
1114 } else if (prev
.thep_qos
!= THREAD_QOS_UNSPECIFIED
&& next
.thep_qos
== THREAD_QOS_UNSPECIFIED
) {
1115 next
.t_latency_qos
= 0;
1116 next
.t_through_qos
= 0;
1118 next
.t_latency_qos
= thread_qos_policy_params
.qos_latency_qos
[next
.thep_qos
];
1119 next
.t_through_qos
= thread_qos_policy_params
.qos_through_qos
[next
.thep_qos
];
1122 thread_update_qos_cpu_time(thread
, TRUE
);
1123 thread
->effective_policy
= next
;
1124 thread_mtx_unlock(thread
);
1127 /* Don't do anything further to a half-formed task or thread */
1133 * Pend updates that can't be done while holding the task lock
1136 if (prev
.all_sockets_bg
!= next
.all_sockets_bg
)
1137 pend_token
->tpt_update_sockets
= 1;
1140 /* Only re-scan the timer list if the qos level is getting less strong */
1141 if (prev
.t_latency_qos
> next
.t_latency_qos
)
1142 pend_token
->tpt_update_timers
= 1;
1145 if (prev
.t_live_donor
!= next
.t_live_donor
)
1146 pend_token
->tpt_update_live_donor
= 1;
1151 * Update other subsystems as necessary if something has changed
1154 boolean_t update_throttle
= (prev
.io_tier
!= next
.io_tier
) ? TRUE
: FALSE
;
1157 if (prev
.t_suspended
== 0 && next
.t_suspended
== 1 && task
->active
) {
1158 task_hold_locked(task
);
1159 task_wait_locked(task
, FALSE
);
1161 if (prev
.t_suspended
== 1 && next
.t_suspended
== 0 && task
->active
) {
1162 task_release_locked(task
);
1165 boolean_t update_threads
= FALSE
;
1166 boolean_t update_sfi
= FALSE
;
1168 if (prev
.bg_iotier
!= next
.bg_iotier
||
1169 prev
.terminated
!= next
.terminated
||
1170 prev
.t_qos_clamp
!= next
.t_qos_clamp
||
1171 prev
.t_qos_ceiling
!= next
.t_qos_ceiling
||
1172 prev
.qos_ui_is_urgent
!= next
.qos_ui_is_urgent
||
1173 prev
.darwinbg
!= next
.darwinbg
)
1174 update_threads
= TRUE
;
1177 * A bit of a layering violation. We know what task policy attributes
1178 * sfi_thread_classify() consults, so if they change, trigger SFI
1181 if ((prev
.t_latency_qos
!= next
.t_latency_qos
) ||
1182 (prev
.t_role
!= next
.t_role
) ||
1183 (prev
.darwinbg
!= next
.darwinbg
) ||
1184 (prev
.t_sfi_managed
!= next
.t_sfi_managed
))
1187 /* TODO: if CONFIG_SFI */
1188 if (prev
.t_role
!= next
.t_role
&& task_policy_update_coalition_focal_tasks(task
, prev
.t_role
, next
.t_role
)) {
1190 pend_token
->tpt_update_coal_sfi
= 1;
1193 task_policy_update_task_locked(task
, update_throttle
, update_threads
, update_sfi
);
1196 boolean_t update_sfi
= FALSE
;
1197 boolean_t update_qos
= FALSE
;
1199 if (prev
.lowpri_cpu
!= next
.lowpri_cpu
)
1200 update_cpu
= (next
.lowpri_cpu
? DO_LOWPRI_CPU
: UNDO_LOWPRI_CPU
);
1202 if (prev
.darwinbg
!= next
.darwinbg
||
1203 prev
.thep_qos
!= next
.thep_qos
)
1206 if (prev
.thep_qos
!= next
.thep_qos
||
1207 prev
.thep_qos_relprio
!= next
.thep_qos_relprio
||
1208 prev
.qos_ui_is_urgent
!= next
.qos_ui_is_urgent
) {
1212 task_policy_update_thread_locked(thread
, update_cpu
, update_throttle
, update_sfi
, update_qos
);
1217 * Yet another layering violation. We reach out and bang on the coalition directly.
1220 task_policy_update_coalition_focal_tasks(task_t task
,
1224 boolean_t sfi_transition
= FALSE
;
1226 if (prev_role
!= TASK_FOREGROUND_APPLICATION
&& next_role
== TASK_FOREGROUND_APPLICATION
) {
1227 if (coalition_adjust_focal_task_count(task
->coalition
, 1) == 1)
1228 sfi_transition
= TRUE
;
1229 } else if (prev_role
== TASK_FOREGROUND_APPLICATION
&& next_role
!= TASK_FOREGROUND_APPLICATION
) {
1230 if (coalition_adjust_focal_task_count(task
->coalition
, -1) == 0)
1231 sfi_transition
= TRUE
;
1234 if (prev_role
!= TASK_BACKGROUND_APPLICATION
&& next_role
== TASK_BACKGROUND_APPLICATION
) {
1235 if (coalition_adjust_non_focal_task_count(task
->coalition
, 1) == 1)
1236 sfi_transition
= TRUE
;
1237 } else if (prev_role
== TASK_BACKGROUND_APPLICATION
&& next_role
!= TASK_BACKGROUND_APPLICATION
) {
1238 if (coalition_adjust_non_focal_task_count(task
->coalition
, -1) == 0)
1239 sfi_transition
= TRUE
;
1242 return sfi_transition
;
1245 /* Despite the name, the thread's task is locked, the thread is not */
1247 task_policy_update_thread_locked(thread_t thread
,
1249 boolean_t update_throttle
,
1250 boolean_t update_sfi
,
1251 boolean_t update_qos
)
1253 thread_precedence_policy_data_t policy
;
1255 if (update_throttle
) {
1256 rethrottle_thread(thread
->uthread
);
1260 sfi_reevaluate(thread
);
1264 * TODO: pidbind needs to stuff remembered importance into saved_importance
1265 * properly deal with bg'ed threads being pidbound and unbging while pidbound
1267 * TODO: A BG thread's priority is 0 on desktop and 4 on embedded. Need to reconcile this.
1269 if (update_cpu
== DO_LOWPRI_CPU
) {
1270 thread
->saved_importance
= thread
->importance
;
1271 policy
.importance
= INT_MIN
;
1272 } else if (update_cpu
== UNDO_LOWPRI_CPU
) {
1273 policy
.importance
= thread
->saved_importance
;
1274 thread
->saved_importance
= 0;
1277 /* Takes thread lock and thread mtx lock */
1279 thread_policy_set_internal(thread
, THREAD_PRECEDENCE_POLICY
,
1280 (thread_policy_t
)&policy
,
1281 THREAD_PRECEDENCE_POLICY_COUNT
);
1284 thread_recompute_qos(thread
);
1288 * Calculate priority on a task, loop through its threads, and tell them about
1289 * priority changes and throttle changes.
1292 task_policy_update_task_locked(task_t task
,
1293 boolean_t update_throttle
,
1294 boolean_t update_threads
,
1295 boolean_t update_sfi
)
1297 boolean_t update_priority
= FALSE
;
1299 if (task
== kernel_task
)
1300 panic("Attempting to set task policy on kernel_task");
1302 int priority
= BASEPRI_DEFAULT
;
1303 int max_priority
= MAXPRI_USER
;
1305 if (proc_get_effective_task_policy(task
, TASK_POLICY_LOWPRI_CPU
)) {
1306 priority
= MAXPRI_THROTTLE
;
1307 max_priority
= MAXPRI_THROTTLE
;
1308 } else if (proc_get_effective_task_policy(task
, TASK_POLICY_SUPPRESSED_CPU
)) {
1309 priority
= MAXPRI_SUPPRESSED
;
1310 max_priority
= MAXPRI_SUPPRESSED
;
1312 switch (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
)) {
1313 case TASK_CONTROL_APPLICATION
:
1314 priority
= BASEPRI_CONTROL
;
1316 case TASK_GRAPHICS_SERVER
:
1317 priority
= BASEPRI_GRAPHICS
;
1318 max_priority
= MAXPRI_RESERVED
;
1324 /* factor in 'nice' value */
1325 priority
+= task
->importance
;
1327 if (task
->effective_policy
.t_qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
1328 int qos_clamp_priority
= thread_qos_policy_params
.qos_pri
[task
->effective_policy
.t_qos_clamp
];
1330 priority
= MIN(priority
, qos_clamp_priority
);
1331 max_priority
= MIN(max_priority
, qos_clamp_priority
);
1335 /* avoid extra work if priority isn't changing */
1336 if (task
->priority
!= priority
|| task
->max_priority
!= max_priority
) {
1337 update_priority
= TRUE
;
1339 /* update the scheduling priority for the task */
1340 task
->max_priority
= max_priority
;
1342 if (priority
> task
->max_priority
)
1343 priority
= task
->max_priority
;
1344 else if (priority
< MINPRI
)
1347 task
->priority
= priority
;
1350 /* Loop over the threads in the task only once, and only if necessary */
1351 if (update_threads
|| update_throttle
|| update_priority
|| update_sfi
) {
1354 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1355 if (update_priority
) {
1356 thread_mtx_lock(thread
);
1358 thread_task_priority(thread
, priority
, max_priority
);
1360 thread_mtx_unlock(thread
);
1363 if (update_throttle
) {
1364 rethrottle_thread(thread
->uthread
);
1368 sfi_reevaluate(thread
);
1371 if (update_threads
) {
1372 thread
->requested_policy
.bg_iotier
= task
->effective_policy
.bg_iotier
;
1373 thread
->requested_policy
.terminated
= task
->effective_policy
.terminated
;
1375 task_policy_update_internal_locked(task
, thread
, FALSE
, NULL
);
1376 /* The thread policy must not emit any completion actions due to this change. */
1383 * Called with task unlocked to do things that can't be done while holding the task lock
1386 task_policy_update_complete_unlocked(task_t task
, thread_t thread
, task_pend_token_t pend_token
)
1388 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1391 if (pend_token
->tpt_update_sockets
)
1392 proc_apply_task_networkbg(task
->bsd_info
, thread
);
1393 #endif /* MACH_BSD */
1396 /* The timer throttle has been removed or reduced, we need to look for expired timers and fire them */
1397 if (pend_token
->tpt_update_timers
)
1398 ml_timer_evaluate();
1401 if (pend_token
->tpt_update_live_donor
)
1402 task_importance_update_live_donor(task
);
1404 if (pend_token
->tpt_update_coal_sfi
)
1405 coalition_sfi_reevaluate(task
->coalition
, task
);
1410 * Initiate a task policy state transition
1412 * Everything that modifies requested except functions that need to hold the task lock
1413 * should use this function
1415 * Argument validation should be performed before reaching this point.
1417 * TODO: Do we need to check task->active or thread->active?
1420 proc_set_task_policy(task_t task
,
1426 struct task_pend_token pend_token
= {};
1430 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1431 (IMPORTANCE_CODE(flavor
, (category
| tisthread(thread
)))) | DBG_FUNC_START
,
1432 targetid(task
, thread
), trequested_0(task
, thread
), trequested_1(task
, thread
), value
, 0);
1434 proc_set_task_policy_locked(task
, thread
, category
, flavor
, value
);
1436 task_policy_update_locked(task
, thread
, &pend_token
);
1440 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1441 (IMPORTANCE_CODE(flavor
, (category
| tisthread(thread
)))) | DBG_FUNC_END
,
1442 targetid(task
, thread
), trequested_0(task
, thread
), trequested_1(task
, thread
), tpending(&pend_token
), 0);
1444 task_policy_update_complete_unlocked(task
, thread
, &pend_token
);
1448 * Initiate a task policy state transition on a thread with its TID
1449 * Useful if you cannot guarantee the thread won't get terminated
1452 proc_set_task_policy_thread(task_t task
,
1459 thread_t self
= current_thread();
1460 struct task_pend_token pend_token
= {};
1464 if (tid
== TID_NULL
|| tid
== self
->thread_id
)
1467 thread
= task_findtid(task
, tid
);
1469 if (thread
== THREAD_NULL
) {
1474 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1475 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_THREAD
))) | DBG_FUNC_START
,
1476 targetid(task
, thread
), trequested_0(task
, thread
), trequested_1(task
, thread
), value
, 0);
1478 proc_set_task_policy_locked(task
, thread
, category
, flavor
, value
);
1480 task_policy_update_locked(task
, thread
, &pend_token
);
1484 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1485 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_THREAD
))) | DBG_FUNC_END
,
1486 targetid(task
, thread
), trequested_0(task
, thread
), trequested_1(task
, thread
), tpending(&pend_token
), 0);
1488 task_policy_update_complete_unlocked(task
, thread
, &pend_token
);
1492 * Variant of proc_set_task_policy() that sets two scalars in the requested policy structure.
1493 * Same locking rules apply.
1496 proc_set_task_policy2(task_t task
, thread_t thread
, int category
, int flavor
, int value1
, int value2
)
1498 struct task_pend_token pend_token
= {};
1502 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1503 (IMPORTANCE_CODE(flavor
, (category
| tisthread(thread
)))) | DBG_FUNC_START
,
1504 targetid(task
, thread
), trequested_0(task
, thread
), trequested_1(task
, thread
), value1
, 0);
1506 proc_set_task_policy2_locked(task
, thread
, category
, flavor
, value1
, value2
);
1508 task_policy_update_locked(task
, thread
, &pend_token
);
1512 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1513 (IMPORTANCE_CODE(flavor
, (category
| tisthread(thread
)))) | DBG_FUNC_END
,
1514 targetid(task
, thread
), trequested_0(task
, thread
), trequested_0(task
, thread
), tpending(&pend_token
), 0);
1516 task_policy_update_complete_unlocked(task
, thread
, &pend_token
);
1520 * Set the requested state for a specific flavor to a specific value.
1523 * Verify that arguments to non iopol things are 1 or 0
1526 proc_set_task_policy_locked(task_t task
,
1532 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1536 struct task_requested_policy requested
=
1537 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
1541 /* Category: EXTERNAL and INTERNAL, thread and task */
1543 case TASK_POLICY_DARWIN_BG
:
1544 if (category
== TASK_POLICY_EXTERNAL
)
1545 requested
.ext_darwinbg
= value
;
1547 requested
.int_darwinbg
= value
;
1550 case TASK_POLICY_IOPOL
:
1551 proc_iopol_to_tier(value
, &tier
, &passive
);
1552 if (category
== TASK_POLICY_EXTERNAL
) {
1553 requested
.ext_iotier
= tier
;
1554 requested
.ext_iopassive
= passive
;
1556 requested
.int_iotier
= tier
;
1557 requested
.int_iopassive
= passive
;
1561 case TASK_POLICY_IO
:
1562 if (category
== TASK_POLICY_EXTERNAL
)
1563 requested
.ext_iotier
= value
;
1565 requested
.int_iotier
= value
;
1568 case TASK_POLICY_PASSIVE_IO
:
1569 if (category
== TASK_POLICY_EXTERNAL
)
1570 requested
.ext_iopassive
= value
;
1572 requested
.int_iopassive
= value
;
1575 /* Category: INTERNAL, task only */
1577 case TASK_POLICY_DARWIN_BG_IOPOL
:
1578 assert(on_task
&& category
== TASK_POLICY_INTERNAL
);
1579 proc_iopol_to_tier(value
, &tier
, &passive
);
1580 requested
.bg_iotier
= tier
;
1583 /* Category: ATTRIBUTE, task only */
1585 case TASK_POLICY_TAL
:
1586 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1587 requested
.t_tal_enabled
= value
;
1590 case TASK_POLICY_BOOST
:
1591 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1592 requested
.t_boosted
= value
;
1595 case TASK_POLICY_ROLE
:
1596 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1597 requested
.t_role
= value
;
1600 case TASK_POLICY_TERMINATED
:
1601 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1602 requested
.terminated
= value
;
1604 case TASK_BASE_LATENCY_QOS_POLICY
:
1605 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1606 requested
.t_base_latency_qos
= value
;
1608 case TASK_BASE_THROUGHPUT_QOS_POLICY
:
1609 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1610 requested
.t_base_through_qos
= value
;
1612 case TASK_POLICY_SFI_MANAGED
:
1613 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1614 requested
.t_sfi_managed
= value
;
1617 /* Category: ATTRIBUTE, thread only */
1619 case TASK_POLICY_PIDBIND_BG
:
1620 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1621 requested
.th_pidbind_bg
= value
;
1624 case TASK_POLICY_WORKQ_BG
:
1625 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1626 requested
.th_workq_bg
= value
;
1629 case TASK_POLICY_QOS
:
1630 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1631 requested
.thrp_qos
= value
;
1634 case TASK_POLICY_QOS_OVERRIDE
:
1635 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1636 requested
.thrp_qos_override
= value
;
1640 panic("unknown task policy: %d %d %d", category
, flavor
, value
);
1645 task
->requested_policy
= requested
;
1647 thread
->requested_policy
= requested
;
1651 * Variant of proc_set_task_policy_locked() that sets two scalars in the requested policy structure.
1654 proc_set_task_policy2_locked(task_t task
,
1661 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1663 struct task_requested_policy requested
=
1664 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
1668 /* Category: ATTRIBUTE, task only */
1670 case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
:
1671 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1672 requested
.t_base_latency_qos
= value1
;
1673 requested
.t_base_through_qos
= value2
;
1676 case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
:
1677 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1678 requested
.t_over_latency_qos
= value1
;
1679 requested
.t_over_through_qos
= value2
;
1682 /* Category: ATTRIBUTE, thread only */
1684 case TASK_POLICY_QOS_AND_RELPRIO
:
1686 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1687 requested
.thrp_qos
= value1
;
1688 requested
.thrp_qos_relprio
= value2
;
1689 DTRACE_BOOST3(qos_set
, uint64_t, thread
->thread_id
, int, requested
.thrp_qos
, int, requested
.thrp_qos_relprio
);
1693 panic("unknown task policy: %d %d %d %d", category
, flavor
, value1
, value2
);
1698 task
->requested_policy
= requested
;
1700 thread
->requested_policy
= requested
;
1705 * Gets what you set. Effective values may be different.
1708 proc_get_task_policy(task_t task
,
1713 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1719 struct task_requested_policy requested
=
1720 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
1723 case TASK_POLICY_DARWIN_BG
:
1724 if (category
== TASK_POLICY_EXTERNAL
)
1725 value
= requested
.ext_darwinbg
;
1727 value
= requested
.int_darwinbg
;
1729 case TASK_POLICY_IOPOL
:
1730 if (category
== TASK_POLICY_EXTERNAL
)
1731 value
= proc_tier_to_iopol(requested
.ext_iotier
,
1732 requested
.ext_iopassive
);
1734 value
= proc_tier_to_iopol(requested
.int_iotier
,
1735 requested
.int_iopassive
);
1737 case TASK_POLICY_IO
:
1738 if (category
== TASK_POLICY_EXTERNAL
)
1739 value
= requested
.ext_iotier
;
1741 value
= requested
.int_iotier
;
1743 case TASK_POLICY_PASSIVE_IO
:
1744 if (category
== TASK_POLICY_EXTERNAL
)
1745 value
= requested
.ext_iopassive
;
1747 value
= requested
.int_iopassive
;
1749 case TASK_POLICY_DARWIN_BG_IOPOL
:
1750 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1751 value
= proc_tier_to_iopol(requested
.bg_iotier
, 0);
1753 case TASK_POLICY_ROLE
:
1754 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1755 value
= requested
.t_role
;
1757 case TASK_POLICY_SFI_MANAGED
:
1758 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1759 value
= requested
.t_sfi_managed
;
1761 case TASK_POLICY_QOS
:
1762 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1763 value
= requested
.thrp_qos
;
1765 case TASK_POLICY_QOS_OVERRIDE
:
1766 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1767 value
= requested
.thrp_qos_override
;
1770 panic("unknown policy_flavor %d", flavor
);
1780 * Variant of proc_get_task_policy() that returns two scalar outputs.
1783 proc_get_task_policy2(task_t task
, thread_t thread
, int category __unused
, int flavor
, int *value1
, int *value2
)
1785 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1789 struct task_requested_policy requested
=
1790 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
1793 /* TASK attributes */
1794 case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
:
1795 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1796 *value1
= requested
.t_base_latency_qos
;
1797 *value2
= requested
.t_base_through_qos
;
1800 case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
:
1801 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1802 *value1
= requested
.t_over_latency_qos
;
1803 *value2
= requested
.t_over_through_qos
;
1806 /* THREAD attributes */
1807 case TASK_POLICY_QOS_AND_RELPRIO
:
1808 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1809 *value1
= requested
.thrp_qos
;
1810 *value2
= requested
.thrp_qos_relprio
;
1814 panic("unknown policy_flavor %d", flavor
);
1823 * Functions for querying effective state for relevant subsystems
1824 * ONLY the relevant subsystem should query these.
1825 * NEVER take a value from one of the 'effective' functions and stuff it into a setter.
1829 proc_get_effective_task_policy(task_t task
, int flavor
)
1831 return proc_get_effective_policy(task
, THREAD_NULL
, flavor
);
1835 proc_get_effective_thread_policy(thread_t thread
, int flavor
)
1837 return proc_get_effective_policy(thread
->task
, thread
, flavor
);
1841 * Gets what is actually in effect, for subsystems which pull policy instead of receive updates.
1843 * NOTE: This accessor does not take the task lock.
1844 * Notifications of state updates need to be externally synchronized with state queries.
1845 * This routine *MUST* remain interrupt safe, as it is potentially invoked
1846 * within the context of a timer interrupt. It is also called in KDP context for stackshot.
1849 proc_get_effective_policy(task_t task
,
1853 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1857 case TASK_POLICY_DARWIN_BG
:
1859 * This backs the KPI call proc_pidbackgrounded to find
1860 * out if a pid is backgrounded,
1861 * as well as proc_get_effective_thread_policy.
1862 * Its main use is within the timer layer, as well as
1863 * prioritizing requests to the graphics system.
1864 * Returns 1 for background mode, 0 for normal mode
1867 value
= task
->effective_policy
.darwinbg
;
1869 value
= (task
->effective_policy
.darwinbg
||
1870 thread
->effective_policy
.darwinbg
) ? 1 : 0;
1872 case TASK_POLICY_IO
:
1874 * The I/O system calls here to find out what throttling tier to apply to an operation.
1875 * Returns THROTTLE_LEVEL_* values. Some userspace spinlock operations can apply
1876 * a temporary iotier override to make the I/O more aggressive to get the lock
1877 * owner to release the spinlock.
1880 value
= task
->effective_policy
.io_tier
;
1882 value
= MAX(task
->effective_policy
.io_tier
,
1883 thread
->effective_policy
.io_tier
);
1884 if (thread
->iotier_override
!= THROTTLE_LEVEL_NONE
)
1885 value
= MIN(value
, thread
->iotier_override
);
1888 case TASK_POLICY_PASSIVE_IO
:
1890 * The I/O system calls here to find out whether an operation should be passive.
1891 * (i.e. not cause operations with lower throttle tiers to be throttled)
1892 * Returns 1 for passive mode, 0 for normal mode.
1893 * If a userspace spinlock has applied an override, that I/O should always
1894 * be passive to avoid self-throttling when the override is removed and lower
1895 * iotier I/Os are issued.
1898 value
= task
->effective_policy
.io_passive
;
1900 int io_tier
= MAX(task
->effective_policy
.io_tier
, thread
->effective_policy
.io_tier
);
1901 boolean_t override_in_effect
= (thread
->iotier_override
!= THROTTLE_LEVEL_NONE
) && (thread
->iotier_override
< io_tier
);
1903 value
= (task
->effective_policy
.io_passive
||
1904 thread
->effective_policy
.io_passive
|| override_in_effect
) ? 1 : 0;
1907 case TASK_POLICY_ALL_SOCKETS_BG
:
1909 * do_background_socket() calls this to determine what it should do to the proc's sockets
1910 * Returns 1 for background mode, 0 for normal mode
1912 * This consults both thread and task so un-DBGing a thread while the task is BG
1913 * doesn't get you out of the network throttle.
1916 value
= task
->effective_policy
.all_sockets_bg
;
1918 value
= (task
->effective_policy
.all_sockets_bg
||
1919 thread
->effective_policy
.all_sockets_bg
) ? 1 : 0;
1921 case TASK_POLICY_NEW_SOCKETS_BG
:
1923 * socreate() calls this to determine if it should mark a new socket as background
1924 * Returns 1 for background mode, 0 for normal mode
1927 value
= task
->effective_policy
.new_sockets_bg
;
1929 value
= (task
->effective_policy
.new_sockets_bg
||
1930 thread
->effective_policy
.new_sockets_bg
) ? 1 : 0;
1932 case TASK_POLICY_LOWPRI_CPU
:
1934 * Returns 1 for low priority cpu mode, 0 for normal mode
1937 value
= task
->effective_policy
.lowpri_cpu
;
1939 value
= (task
->effective_policy
.lowpri_cpu
||
1940 thread
->effective_policy
.lowpri_cpu
) ? 1 : 0;
1942 case TASK_POLICY_SUPPRESSED_CPU
:
1944 * Returns 1 for suppressed cpu mode, 0 for normal mode
1947 value
= task
->effective_policy
.t_suppressed_cpu
;
1949 case TASK_POLICY_LATENCY_QOS
:
1951 * timer arming calls into here to find out the timer coalescing level
1952 * Returns a QoS tier (0-6)
1955 value
= task
->effective_policy
.t_latency_qos
;
1957 value
= MAX(task
->effective_policy
.t_latency_qos
, thread
->effective_policy
.t_latency_qos
);
1960 case TASK_POLICY_THROUGH_QOS
:
1962 * Returns a QoS tier (0-6)
1965 value
= task
->effective_policy
.t_through_qos
;
1967 case TASK_POLICY_ROLE
:
1969 value
= task
->effective_policy
.t_role
;
1971 case TASK_POLICY_WATCHERS_BG
:
1973 value
= task
->effective_policy
.t_watchers_bg
;
1975 case TASK_POLICY_SFI_MANAGED
:
1977 value
= task
->effective_policy
.t_sfi_managed
;
1979 case TASK_POLICY_QOS
:
1981 value
= thread
->effective_policy
.thep_qos
;
1984 panic("unknown policy_flavor %d", flavor
);
1992 * Convert from IOPOL_* values to throttle tiers.
1994 * TODO: Can this be made more compact, like an array lookup
1995 * Note that it is possible to support e.g. IOPOL_PASSIVE_STANDARD in the future
1999 proc_iopol_to_tier(int iopolicy
, int *tier
, int *passive
)
2004 case IOPOL_IMPORTANT
:
2005 *tier
= THROTTLE_LEVEL_TIER0
;
2008 *tier
= THROTTLE_LEVEL_TIER0
;
2011 case IOPOL_STANDARD
:
2012 *tier
= THROTTLE_LEVEL_TIER1
;
2015 *tier
= THROTTLE_LEVEL_TIER2
;
2017 case IOPOL_THROTTLE
:
2018 *tier
= THROTTLE_LEVEL_TIER3
;
2021 panic("unknown I/O policy %d", iopolicy
);
2027 proc_tier_to_iopol(int tier
, int passive
)
2031 case THROTTLE_LEVEL_TIER0
:
2032 return IOPOL_PASSIVE
;
2035 panic("unknown passive tier %d", tier
);
2036 return IOPOL_DEFAULT
;
2041 case THROTTLE_LEVEL_NONE
:
2042 case THROTTLE_LEVEL_TIER0
:
2043 return IOPOL_DEFAULT
;
2045 case THROTTLE_LEVEL_TIER1
:
2046 return IOPOL_STANDARD
;
2048 case THROTTLE_LEVEL_TIER2
:
2049 return IOPOL_UTILITY
;
2051 case THROTTLE_LEVEL_TIER3
:
2052 return IOPOL_THROTTLE
;
2055 panic("unknown tier %d", tier
);
2056 return IOPOL_DEFAULT
;
2062 /* apply internal backgrounding for workqueue threads */
2064 proc_apply_workq_bgthreadpolicy(thread_t thread
)
2066 if (thread
== THREAD_NULL
)
2069 proc_set_task_policy(thread
->task
, thread
, TASK_POLICY_ATTRIBUTE
,
2070 TASK_POLICY_WORKQ_BG
, TASK_POLICY_ENABLE
);
2076 * remove internal backgrounding for workqueue threads
2077 * does NOT go find sockets created while BG and unbackground them
2080 proc_restore_workq_bgthreadpolicy(thread_t thread
)
2082 if (thread
== THREAD_NULL
)
2085 proc_set_task_policy(thread
->task
, thread
, TASK_POLICY_ATTRIBUTE
,
2086 TASK_POLICY_WORKQ_BG
, TASK_POLICY_DISABLE
);
2091 /* here for temporary compatibility */
2093 proc_setthread_saved_importance(__unused thread_t thread
, __unused
int importance
)
2099 * Set an override on the thread which is consulted with a
2100 * higher priority than the task/thread policy. This should
2101 * only be set for temporary grants until the thread
2102 * returns to the userspace boundary
2104 * We use atomic operations to swap in the override, with
2105 * the assumption that the thread itself can
2106 * read the override and clear it on return to userspace.
2108 * No locking is performed, since it is acceptable to see
2109 * a stale override for one loop through throttle_lowpri_io().
2110 * However a thread reference must be held on the thread.
2113 void set_thread_iotier_override(thread_t thread
, int policy
)
2115 int current_override
;
2117 /* Let most aggressive I/O policy win until user boundary */
2119 current_override
= thread
->iotier_override
;
2121 if (current_override
!= THROTTLE_LEVEL_NONE
)
2122 policy
= MIN(current_override
, policy
);
2124 if (current_override
== policy
) {
2125 /* no effective change */
2128 } while (!OSCompareAndSwap(current_override
, policy
, &thread
->iotier_override
));
2131 * Since the thread may be currently throttled,
2132 * re-evaluate tiers and potentially break out
2135 rethrottle_thread(thread
->uthread
);
2139 * Userspace synchronization routines (like pthread mutexes, pthread reader-writer locks,
2140 * semaphores, dispatch_sync) may result in priority inversions where a higher priority
2141 * (i.e. scheduler priority, I/O tier, QoS tier) is waiting on a resource owned by a lower
2142 * priority thread. In these cases, we attempt to propagate the priority token, as long
2143 * as the subsystem informs us of the relationships between the threads. The userspace
2144 * synchronization subsystem should maintain the information of owner->resource and
2145 * resource->waiters itself.
2149 * This helper canonicalizes the resource/resource_type given the current qos_override_mode
2150 * in effect. Note that wildcards (THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD) may need
2151 * to be handled specially in the future, but for now it's fine to slam
2152 * *resource to USER_ADDR_NULL even if it was previously a wildcard.
2154 static void _canonicalize_resource_and_type(user_addr_t
*resource
, int *resource_type
) {
2155 if (qos_override_mode
== QOS_OVERRIDE_MODE_OVERHANG_PEAK
|| qos_override_mode
== QOS_OVERRIDE_MODE_IGNORE_OVERRIDE
) {
2156 /* Map all input resource/type to a single one */
2157 *resource
= USER_ADDR_NULL
;
2158 *resource_type
= THREAD_QOS_OVERRIDE_TYPE_UNKNOWN
;
2159 } else if (qos_override_mode
== QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE
) {
2161 } else if (qos_override_mode
== QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_IGNORE_DISPATCH
) {
2162 /* Map all dispatch overrides to a single one, to avoid memory overhead */
2163 if (*resource_type
== THREAD_QOS_OVERRIDE_TYPE_DISPATCH_ASYNCHRONOUS_OVERRIDE
) {
2164 *resource
= USER_ADDR_NULL
;
2166 } else if (qos_override_mode
== QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_SINGLE_MUTEX_OVERRIDE
) {
2167 /* Map all mutex overrides to a single one, to avoid memory overhead */
2168 if (*resource_type
== THREAD_QOS_OVERRIDE_TYPE_PTHREAD_MUTEX
) {
2169 *resource
= USER_ADDR_NULL
;
2174 /* This helper routine finds an existing override if known. Locking should be done by caller */
2175 static struct thread_qos_override
*_find_qos_override(thread_t thread
, user_addr_t resource
, int resource_type
) {
2176 struct thread_qos_override
*override
;
2178 override
= thread
->overrides
;
2180 if (override
->override_resource
== resource
&&
2181 override
->override_resource_type
== resource_type
) {
2185 override
= override
->override_next
;
2191 static void _find_and_decrement_qos_override(thread_t thread
, user_addr_t resource
, int resource_type
, boolean_t reset
, struct thread_qos_override
**free_override_list
) {
2192 struct thread_qos_override
*override
, *override_prev
;
2194 override_prev
= NULL
;
2195 override
= thread
->overrides
;
2197 struct thread_qos_override
*override_next
= override
->override_next
;
2199 if ((THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD
== resource
|| override
->override_resource
== resource
) &&
2200 override
->override_resource_type
== resource_type
) {
2202 override
->override_contended_resource_count
= 0;
2204 override
->override_contended_resource_count
--;
2207 if (override
->override_contended_resource_count
== 0) {
2208 if (override_prev
== NULL
) {
2209 thread
->overrides
= override_next
;
2211 override_prev
->override_next
= override_next
;
2214 /* Add to out-param for later zfree */
2215 override
->override_next
= *free_override_list
;
2216 *free_override_list
= override
;
2218 override_prev
= override
;
2221 if (THREAD_QOS_OVERRIDE_RESOURCE_WILDCARD
!= resource
) {
2225 override_prev
= override
;
2228 override
= override_next
;
2232 /* This helper recalculates the current requested override using the policy selected at boot */
2233 static int _calculate_requested_qos_override(thread_t thread
)
2235 if (qos_override_mode
== QOS_OVERRIDE_MODE_IGNORE_OVERRIDE
) {
2236 return THREAD_QOS_UNSPECIFIED
;
2239 /* iterate over all overrides and calculate MAX */
2240 struct thread_qos_override
*override
;
2241 int qos_override
= THREAD_QOS_UNSPECIFIED
;
2243 override
= thread
->overrides
;
2245 if (qos_override_mode
!= QOS_OVERRIDE_MODE_FINE_GRAINED_OVERRIDE_BUT_IGNORE_DISPATCH
||
2246 override
->override_resource_type
!= THREAD_QOS_OVERRIDE_TYPE_DISPATCH_ASYNCHRONOUS_OVERRIDE
) {
2247 qos_override
= MAX(qos_override
, override
->override_qos
);
2250 override
= override
->override_next
;
2253 return qos_override
;
2256 boolean_t
proc_thread_qos_add_override(task_t task
, thread_t thread
, uint64_t tid
, int override_qos
, boolean_t first_override_for_resource
, user_addr_t resource
, int resource_type
)
2258 thread_t self
= current_thread();
2259 struct task_pend_token pend_token
= {};
2261 /* XXX move to thread mutex when thread policy does */
2265 * If thread is passed, it is assumed to be most accurate, since the caller must have an explicit (or implicit) reference
2269 if (thread
!= THREAD_NULL
) {
2270 assert(task
== thread
->task
);
2272 if (tid
== self
->thread_id
) {
2275 thread
= task_findtid(task
, tid
);
2277 if (thread
== THREAD_NULL
) {
2278 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_ADD_OVERRIDE
)) | DBG_FUNC_NONE
,
2279 tid
, 0, 0xdead, 0, 0);
2286 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_ADD_OVERRIDE
)) | DBG_FUNC_START
,
2287 thread_tid(thread
), override_qos
, first_override_for_resource
? 1 : 0, 0, 0);
2289 DTRACE_BOOST5(qos_add_override_pre
, uint64_t, tid
, uint64_t, thread
->requested_policy
.thrp_qos
,
2290 uint64_t, thread
->effective_policy
.thep_qos
, int, override_qos
, boolean_t
, first_override_for_resource
);
2292 struct task_requested_policy requested
= thread
->requested_policy
;
2293 struct thread_qos_override
*override
;
2294 struct thread_qos_override
*deferred_free_override
= NULL
;
2295 int new_qos_override
, prev_qos_override
;
2296 int new_effective_qos
;
2297 boolean_t has_thread_reference
= FALSE
;
2299 _canonicalize_resource_and_type(&resource
, &resource_type
);
2301 if (first_override_for_resource
) {
2302 override
= _find_qos_override(thread
, resource
, resource_type
);
2304 override
->override_contended_resource_count
++;
2306 struct thread_qos_override
*override_new
;
2308 /* We need to allocate a new object. Drop the task lock and recheck afterwards in case someone else added the override */
2309 thread_reference(thread
);
2310 has_thread_reference
= TRUE
;
2312 override_new
= zalloc(thread_qos_override_zone
);
2315 override
= _find_qos_override(thread
, resource
, resource_type
);
2317 /* Someone else already allocated while the task lock was dropped */
2318 deferred_free_override
= override_new
;
2319 override
->override_contended_resource_count
++;
2321 override
= override_new
;
2322 override
->override_next
= thread
->overrides
;
2323 override
->override_contended_resource_count
= 1 /* since first_override_for_resource was TRUE */;
2324 override
->override_resource
= resource
;
2325 override
->override_resource_type
= resource_type
;
2326 override
->override_qos
= THREAD_QOS_UNSPECIFIED
;
2327 thread
->overrides
= override
;
2331 override
= _find_qos_override(thread
, resource
, resource_type
);
2335 if (override
->override_qos
== THREAD_QOS_UNSPECIFIED
)
2336 override
->override_qos
= override_qos
;
2338 override
->override_qos
= MAX(override
->override_qos
, override_qos
);
2341 /* Determine how to combine the various overrides into a single current requested override */
2342 prev_qos_override
= requested
.thrp_qos_override
;
2343 new_qos_override
= _calculate_requested_qos_override(thread
);
2345 if (new_qos_override
!= prev_qos_override
) {
2346 requested
.thrp_qos_override
= new_qos_override
;
2348 thread
->requested_policy
= requested
;
2350 task_policy_update_locked(task
, thread
, &pend_token
);
2352 if (!has_thread_reference
) {
2353 thread_reference(thread
);
2358 task_policy_update_complete_unlocked(task
, thread
, &pend_token
);
2360 new_effective_qos
= thread
->effective_policy
.thep_qos
;
2362 thread_deallocate(thread
);
2364 new_effective_qos
= thread
->effective_policy
.thep_qos
;
2368 if (has_thread_reference
) {
2369 thread_deallocate(thread
);
2373 if (deferred_free_override
) {
2374 zfree(thread_qos_override_zone
, deferred_free_override
);
2377 DTRACE_BOOST3(qos_add_override_post
, int, prev_qos_override
, int, new_qos_override
,
2378 int, new_effective_qos
);
2380 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_ADD_OVERRIDE
)) | DBG_FUNC_END
,
2381 new_qos_override
, resource
, resource_type
, 0, 0);
2387 static boolean_t
_proc_thread_qos_remove_override_internal(task_t task
, thread_t thread
, uint64_t tid
, user_addr_t resource
, int resource_type
, boolean_t reset
)
2389 thread_t self
= current_thread();
2390 struct task_pend_token pend_token
= {};
2392 /* XXX move to thread mutex when thread policy does */
2396 * If thread is passed, it is assumed to be most accurate, since the caller must have an explicit (or implicit) reference
2399 if (thread
!= THREAD_NULL
) {
2400 assert(task
== thread
->task
);
2402 if (tid
== self
->thread_id
) {
2405 thread
= task_findtid(task
, tid
);
2407 if (thread
== THREAD_NULL
) {
2408 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_REMOVE_OVERRIDE
)) | DBG_FUNC_NONE
,
2409 tid
, 0, 0xdead, 0, 0);
2416 struct task_requested_policy requested
= thread
->requested_policy
;
2417 struct thread_qos_override
*deferred_free_override_list
= NULL
;
2418 int new_qos_override
, prev_qos_override
;
2420 _canonicalize_resource_and_type(&resource
, &resource_type
);
2422 _find_and_decrement_qos_override(thread
, resource
, resource_type
, reset
, &deferred_free_override_list
);
2424 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_REMOVE_OVERRIDE
)) | DBG_FUNC_START
,
2425 thread_tid(thread
), resource
, reset
, 0, 0);
2427 /* Determine how to combine the various overrides into a single current requested override */
2428 prev_qos_override
= requested
.thrp_qos_override
;
2429 new_qos_override
= _calculate_requested_qos_override(thread
);
2431 if (new_qos_override
!= prev_qos_override
) {
2432 requested
.thrp_qos_override
= new_qos_override
;
2434 thread
->requested_policy
= requested
;
2436 task_policy_update_locked(task
, thread
, &pend_token
);
2438 thread_reference(thread
);
2442 task_policy_update_complete_unlocked(task
, thread
, &pend_token
);
2444 thread_deallocate(thread
);
2449 while (deferred_free_override_list
) {
2450 struct thread_qos_override
*override_next
= deferred_free_override_list
->override_next
;
2452 zfree(thread_qos_override_zone
, deferred_free_override_list
);
2453 deferred_free_override_list
= override_next
;
2456 KERNEL_DEBUG_CONSTANT((IMPORTANCE_CODE(IMP_USYNCH_QOS_OVERRIDE
, IMP_USYNCH_REMOVE_OVERRIDE
)) | DBG_FUNC_END
,
2462 boolean_t
proc_thread_qos_remove_override(task_t task
, thread_t thread
, uint64_t tid
, user_addr_t resource
, int resource_type
)
2464 return _proc_thread_qos_remove_override_internal(task
, thread
, tid
, resource
, resource_type
, FALSE
);
2468 boolean_t
proc_thread_qos_reset_override(task_t task
, thread_t thread
, uint64_t tid
, user_addr_t resource
, int resource_type
)
2470 return _proc_thread_qos_remove_override_internal(task
, thread
, tid
, resource
, resource_type
, TRUE
);
2473 /* Deallocate before thread termination */
2474 void proc_thread_qos_deallocate(thread_t thread
)
2476 task_t task
= thread
->task
;
2477 struct thread_qos_override
*override
;
2479 /* XXX move to thread mutex when thread policy does */
2481 override
= thread
->overrides
;
2482 thread
->overrides
= NULL
; /* task policy re-evaluation needed? */
2483 thread
->requested_policy
.thrp_qos_override
= THREAD_QOS_UNSPECIFIED
;
2487 struct thread_qos_override
*override_next
= override
->override_next
;
2489 zfree(thread_qos_override_zone
, override
);
2490 override
= override_next
;
2494 /* TODO: remove this variable when interactive daemon audit period is over */
2495 extern boolean_t ipc_importance_interactive_receiver
;
2498 * Called at process exec to initialize the apptype, qos clamp, and qos seed of a process
2500 * TODO: Make this function more table-driven instead of ad-hoc
2503 proc_set_task_spawnpolicy(task_t task
, int apptype
, int qos_clamp
,
2504 ipc_port_t
* portwatch_ports
, int portwatch_count
)
2506 struct task_pend_token pend_token
= {};
2508 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2509 (IMPORTANCE_CODE(IMP_TASK_APPTYPE
, apptype
)) | DBG_FUNC_START
,
2510 audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
), trequested_1(task
, THREAD_NULL
),
2514 case TASK_APPTYPE_APP_TAL
:
2515 case TASK_APPTYPE_APP_DEFAULT
:
2516 /* Apps become donors via the 'live-donor' flag instead of the static donor flag */
2517 task_importance_mark_donor(task
, FALSE
);
2518 task_importance_mark_live_donor(task
, TRUE
);
2519 task_importance_mark_receiver(task
, FALSE
);
2520 /* Apps are de-nap recievers on desktop for suppression behaviors */
2521 task_importance_mark_denap_receiver(task
, TRUE
);
2524 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
2525 task_importance_mark_donor(task
, TRUE
);
2526 task_importance_mark_live_donor(task
, FALSE
);
2529 * A boot arg controls whether interactive daemons are importance receivers.
2530 * Normally, they are not. But for testing their behavior as an adaptive
2531 * daemon, the boot-arg can be set.
2533 * TODO: remove this when the interactive daemon audit period is over.
2535 task_importance_mark_receiver(task
, /* FALSE */ ipc_importance_interactive_receiver
);
2536 task_importance_mark_denap_receiver(task
, FALSE
);
2539 case TASK_APPTYPE_DAEMON_STANDARD
:
2540 task_importance_mark_donor(task
, TRUE
);
2541 task_importance_mark_live_donor(task
, FALSE
);
2542 task_importance_mark_receiver(task
, FALSE
);
2543 task_importance_mark_denap_receiver(task
, FALSE
);
2546 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
2547 task_importance_mark_donor(task
, FALSE
);
2548 task_importance_mark_live_donor(task
, FALSE
);
2549 task_importance_mark_receiver(task
, TRUE
);
2550 task_importance_mark_denap_receiver(task
, FALSE
);
2553 case TASK_APPTYPE_DAEMON_BACKGROUND
:
2554 task_importance_mark_donor(task
, FALSE
);
2555 task_importance_mark_live_donor(task
, FALSE
);
2556 task_importance_mark_receiver(task
, FALSE
);
2557 task_importance_mark_denap_receiver(task
, FALSE
);
2560 case TASK_APPTYPE_NONE
:
2564 if (portwatch_ports
!= NULL
&& apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
) {
2565 int portwatch_boosts
= 0;
2567 for (int i
= 0; i
< portwatch_count
; i
++) {
2568 ipc_port_t port
= NULL
;
2570 if ((port
= portwatch_ports
[i
]) != NULL
) {
2572 task_add_importance_watchport(task
, port
, &boost
);
2573 portwatch_boosts
+= boost
;
2577 if (portwatch_boosts
> 0) {
2578 task_importance_hold_internal_assertion(task
, portwatch_boosts
);
2584 if (apptype
== TASK_APPTYPE_APP_TAL
) {
2585 /* TAL starts off enabled by default */
2586 task
->requested_policy
.t_tal_enabled
= 1;
2589 if (apptype
!= TASK_APPTYPE_NONE
) {
2590 task
->requested_policy
.t_apptype
= apptype
;
2594 if (qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
2595 task
->requested_policy
.t_qos_clamp
= qos_clamp
;
2598 task_policy_update_locked(task
, THREAD_NULL
, &pend_token
);
2602 /* Ensure the donor bit is updated to be in sync with the new live donor status */
2603 pend_token
.tpt_update_live_donor
= 1;
2605 task_policy_update_complete_unlocked(task
, THREAD_NULL
, &pend_token
);
2607 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2608 (IMPORTANCE_CODE(IMP_TASK_APPTYPE
, apptype
)) | DBG_FUNC_END
,
2609 audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
), trequested_1(task
, THREAD_NULL
),
2610 task_is_importance_receiver(task
), 0);
2613 /* Set up the primordial thread's QoS */
2615 task_set_main_thread_qos(task_t task
, thread_t main_thread
) {
2616 struct task_pend_token pend_token
= {};
2618 assert(main_thread
->task
== task
);
2622 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2623 (IMPORTANCE_CODE(IMP_MAIN_THREAD_QOS
, 0)) | DBG_FUNC_START
,
2624 audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
), trequested_1(task
, THREAD_NULL
),
2625 main_thread
->requested_policy
.thrp_qos
, 0);
2627 int primordial_qos
= THREAD_QOS_UNSPECIFIED
;
2629 int qos_clamp
= task
->requested_policy
.t_qos_clamp
;
2631 switch (task
->requested_policy
.t_apptype
) {
2632 case TASK_APPTYPE_APP_TAL
:
2633 case TASK_APPTYPE_APP_DEFAULT
:
2634 primordial_qos
= THREAD_QOS_USER_INTERACTIVE
;
2637 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
2638 case TASK_APPTYPE_DAEMON_STANDARD
:
2639 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
2640 primordial_qos
= THREAD_QOS_LEGACY
;
2643 case TASK_APPTYPE_DAEMON_BACKGROUND
:
2644 primordial_qos
= THREAD_QOS_BACKGROUND
;
2648 if (qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
2649 if (primordial_qos
!= THREAD_QOS_UNSPECIFIED
) {
2650 primordial_qos
= MIN(qos_clamp
, primordial_qos
);
2652 primordial_qos
= qos_clamp
;
2656 main_thread
->requested_policy
.thrp_qos
= primordial_qos
;
2658 task_policy_update_locked(task
, main_thread
, &pend_token
);
2662 task_policy_update_complete_unlocked(task
, main_thread
, &pend_token
);
2664 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2665 (IMPORTANCE_CODE(IMP_MAIN_THREAD_QOS
, 0)) | DBG_FUNC_END
,
2666 audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
), trequested_1(task
, THREAD_NULL
),
2670 /* for process_policy to check before attempting to set */
2672 proc_task_is_tal(task_t task
)
2674 return (task
->requested_policy
.t_apptype
== TASK_APPTYPE_APP_TAL
) ? TRUE
: FALSE
;
2679 task_grab_latency_qos(task_t task
)
2681 return qos_latency_policy_package(proc_get_effective_task_policy(task
, TASK_POLICY_LATENCY_QOS
));
2684 /* update the darwin background action state in the flags field for libproc */
2686 proc_get_darwinbgstate(task_t task
, uint32_t * flagsp
)
2688 if (task
->requested_policy
.ext_darwinbg
)
2689 *flagsp
|= PROC_FLAG_EXT_DARWINBG
;
2691 if (task
->requested_policy
.int_darwinbg
)
2692 *flagsp
|= PROC_FLAG_DARWINBG
;
2695 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_APP_DEFAULT
||
2696 task
->requested_policy
.t_apptype
== TASK_APPTYPE_APP_TAL
)
2697 *flagsp
|= PROC_FLAG_APPLICATION
;
2699 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
)
2700 *flagsp
|= PROC_FLAG_ADAPTIVE
;
2702 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&& task
->requested_policy
.t_boosted
== 1)
2703 *flagsp
|= PROC_FLAG_ADAPTIVE_IMPORTANT
;
2705 if (task_is_importance_donor(task
))
2706 *flagsp
|= PROC_FLAG_IMPORTANCE_DONOR
;
2708 if (task
->effective_policy
.t_sup_active
)
2709 *flagsp
|= PROC_FLAG_SUPPRESSED
;
2714 /* All per-thread state is in the first 32-bits of the bitfield */
2716 proc_get_thread_policy(thread_t thread
, thread_policy_state_t info
)
2718 task_t task
= thread
->task
;
2720 info
->requested
= (integer_t
)task_requested_bitfield(task
, thread
);
2721 info
->effective
= (integer_t
)task_effective_bitfield(task
, thread
);
2727 * Tracepoint data... Reading the tracepoint data can be somewhat complicated.
2728 * The current scheme packs as much data into a single tracepoint as it can.
2730 * Each task/thread requested/effective structure is 64 bits in size. Any
2731 * given tracepoint will emit either requested or effective data, but not both.
2733 * A tracepoint may emit any of task, thread, or task & thread data.
2735 * The type of data emitted varies with pointer size. Where possible, both
2736 * task and thread data are emitted. In LP32 systems, the first and second
2737 * halves of either the task or thread data is emitted.
2739 * The code uses uintptr_t array indexes instead of high/low to avoid
2740 * confusion WRT big vs little endian.
2742 * The truth table for the tracepoint data functions is below, and has the
2743 * following invariants:
2745 * 1) task and thread are uintptr_t*
2746 * 2) task may never be NULL
2750 * trequested_0(task, NULL) task[0] task[0]
2751 * trequested_1(task, NULL) task[1] NULL
2752 * trequested_0(task, thread) thread[0] task[0]
2753 * trequested_1(task, thread) thread[1] thread[0]
2755 * Basically, you get a full task or thread on LP32, and both on LP64.
2757 * The uintptr_t munging here is squicky enough to deserve a comment.
2759 * The variables we are accessing are laid out in memory like this:
2761 * [ LP64 uintptr_t 0 ]
2762 * [ LP32 uintptr_t 0 ] [ LP32 uintptr_t 1 ]
2769 trequested_0(task_t task
, thread_t thread
)
2772 _Static_assert(sizeof(struct task_requested_policy
) == sizeof(uint64_t), "size invariant violated");
2773 _Static_assert(sizeof(task
->requested_policy
) == sizeof(thread
->requested_policy
), "size invariant violated");
2775 uintptr_t* raw
= (uintptr_t*)((thread
== THREAD_NULL
) ? &task
->requested_policy
: &thread
->requested_policy
);
2780 trequested_1(task_t task
, thread_t thread
)
2783 _Static_assert(sizeof(struct task_requested_policy
) == sizeof(uint64_t), "size invariant violated");
2784 _Static_assert(sizeof(task
->requested_policy
) == sizeof(thread
->requested_policy
), "size invariant violated");
2786 #if defined __LP64__
2787 return (thread
== NULL
) ? 0 : *(uintptr_t*)&thread
->requested_policy
;
2789 uintptr_t* raw
= (uintptr_t*)((thread
== THREAD_NULL
) ? &task
->requested_policy
: &thread
->requested_policy
);
2795 teffective_0(task_t task
, thread_t thread
)
2798 _Static_assert(sizeof(struct task_effective_policy
) == sizeof(uint64_t), "size invariant violated");
2799 _Static_assert(sizeof(task
->effective_policy
) == sizeof(thread
->effective_policy
), "size invariant violated");
2801 uintptr_t* raw
= (uintptr_t*)((thread
== THREAD_NULL
) ? &task
->effective_policy
: &thread
->effective_policy
);
2806 teffective_1(task_t task
, thread_t thread
)
2809 _Static_assert(sizeof(struct task_effective_policy
) == sizeof(uint64_t), "size invariant violated");
2810 _Static_assert(sizeof(task
->effective_policy
) == sizeof(thread
->effective_policy
), "size invariant violated");
2812 #if defined __LP64__
2813 return (thread
== NULL
) ? 0 : *(uintptr_t*)&thread
->effective_policy
;
2815 uintptr_t* raw
= (uintptr_t*)((thread
== THREAD_NULL
) ? &task
->effective_policy
: &thread
->effective_policy
);
2820 /* dump pending for tracepoint */
2821 static uint32_t tpending(task_pend_token_t pend_token
) { return *(uint32_t*)(void*)(pend_token
); }
2824 task_requested_bitfield(task_t task
, thread_t thread
)
2827 struct task_requested_policy requested
=
2828 (thread
== THREAD_NULL
) ? task
->requested_policy
: thread
->requested_policy
;
2830 bits
|= (requested
.int_darwinbg
? POLICY_REQ_INT_DARWIN_BG
: 0);
2831 bits
|= (requested
.ext_darwinbg
? POLICY_REQ_EXT_DARWIN_BG
: 0);
2832 bits
|= (requested
.int_iotier
? (((uint64_t)requested
.int_iotier
) << POLICY_REQ_INT_IO_TIER_SHIFT
) : 0);
2833 bits
|= (requested
.ext_iotier
? (((uint64_t)requested
.ext_iotier
) << POLICY_REQ_EXT_IO_TIER_SHIFT
) : 0);
2834 bits
|= (requested
.int_iopassive
? POLICY_REQ_INT_PASSIVE_IO
: 0);
2835 bits
|= (requested
.ext_iopassive
? POLICY_REQ_EXT_PASSIVE_IO
: 0);
2836 bits
|= (requested
.bg_iotier
? (((uint64_t)requested
.bg_iotier
) << POLICY_REQ_BG_IOTIER_SHIFT
) : 0);
2837 bits
|= (requested
.terminated
? POLICY_REQ_TERMINATED
: 0);
2839 bits
|= (requested
.th_pidbind_bg
? POLICY_REQ_PIDBIND_BG
: 0);
2840 bits
|= (requested
.th_workq_bg
? POLICY_REQ_WORKQ_BG
: 0);
2842 if (thread
!= THREAD_NULL
) {
2843 bits
|= (requested
.thrp_qos
? (((uint64_t)requested
.thrp_qos
) << POLICY_REQ_TH_QOS_SHIFT
) : 0);
2844 bits
|= (requested
.thrp_qos_override
? (((uint64_t)requested
.thrp_qos_override
) << POLICY_REQ_TH_QOS_OVER_SHIFT
) : 0);
2847 bits
|= (requested
.t_boosted
? POLICY_REQ_BOOSTED
: 0);
2848 bits
|= (requested
.t_tal_enabled
? POLICY_REQ_TAL_ENABLED
: 0);
2849 bits
|= (requested
.t_apptype
? (((uint64_t)requested
.t_apptype
) << POLICY_REQ_APPTYPE_SHIFT
) : 0);
2850 bits
|= (requested
.t_role
? (((uint64_t)requested
.t_role
) << POLICY_REQ_ROLE_SHIFT
) : 0);
2852 bits
|= (requested
.t_sup_active
? POLICY_REQ_SUP_ACTIVE
: 0);
2853 bits
|= (requested
.t_sup_lowpri_cpu
? POLICY_REQ_SUP_LOWPRI_CPU
: 0);
2854 bits
|= (requested
.t_sup_cpu
? POLICY_REQ_SUP_CPU
: 0);
2855 bits
|= (requested
.t_sup_timer
? (((uint64_t)requested
.t_sup_timer
) << POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT
) : 0);
2856 bits
|= (requested
.t_sup_throughput
? (((uint64_t)requested
.t_sup_throughput
) << POLICY_REQ_SUP_THROUGHPUT_SHIFT
) : 0);
2857 bits
|= (requested
.t_sup_disk
? POLICY_REQ_SUP_DISK_THROTTLE
: 0);
2858 bits
|= (requested
.t_sup_cpu_limit
? POLICY_REQ_SUP_CPU_LIMIT
: 0);
2859 bits
|= (requested
.t_sup_suspend
? POLICY_REQ_SUP_SUSPEND
: 0);
2860 bits
|= (requested
.t_sup_bg_sockets
? POLICY_REQ_SUP_BG_SOCKETS
: 0);
2861 bits
|= (requested
.t_base_latency_qos
? (((uint64_t)requested
.t_base_latency_qos
) << POLICY_REQ_BASE_LATENCY_QOS_SHIFT
) : 0);
2862 bits
|= (requested
.t_over_latency_qos
? (((uint64_t)requested
.t_over_latency_qos
) << POLICY_REQ_OVER_LATENCY_QOS_SHIFT
) : 0);
2863 bits
|= (requested
.t_base_through_qos
? (((uint64_t)requested
.t_base_through_qos
) << POLICY_REQ_BASE_THROUGH_QOS_SHIFT
) : 0);
2864 bits
|= (requested
.t_over_through_qos
? (((uint64_t)requested
.t_over_through_qos
) << POLICY_REQ_OVER_THROUGH_QOS_SHIFT
) : 0);
2865 bits
|= (requested
.t_sfi_managed
? POLICY_REQ_SFI_MANAGED
: 0);
2866 bits
|= (requested
.t_qos_clamp
? (((uint64_t)requested
.t_qos_clamp
) << POLICY_REQ_QOS_CLAMP_SHIFT
) : 0);
2872 task_effective_bitfield(task_t task
, thread_t thread
)
2875 struct task_effective_policy effective
=
2876 (thread
== THREAD_NULL
) ? task
->effective_policy
: thread
->effective_policy
;
2878 bits
|= (effective
.io_tier
? (((uint64_t)effective
.io_tier
) << POLICY_EFF_IO_TIER_SHIFT
) : 0);
2879 bits
|= (effective
.io_passive
? POLICY_EFF_IO_PASSIVE
: 0);
2880 bits
|= (effective
.darwinbg
? POLICY_EFF_DARWIN_BG
: 0);
2881 bits
|= (effective
.lowpri_cpu
? POLICY_EFF_LOWPRI_CPU
: 0);
2882 bits
|= (effective
.terminated
? POLICY_EFF_TERMINATED
: 0);
2883 bits
|= (effective
.all_sockets_bg
? POLICY_EFF_ALL_SOCKETS_BG
: 0);
2884 bits
|= (effective
.new_sockets_bg
? POLICY_EFF_NEW_SOCKETS_BG
: 0);
2885 bits
|= (effective
.bg_iotier
? (((uint64_t)effective
.bg_iotier
) << POLICY_EFF_BG_IOTIER_SHIFT
) : 0);
2886 bits
|= (effective
.qos_ui_is_urgent
? POLICY_EFF_QOS_UI_IS_URGENT
: 0);
2888 if (thread
!= THREAD_NULL
)
2889 bits
|= (effective
.thep_qos
? (((uint64_t)effective
.thep_qos
) << POLICY_EFF_TH_QOS_SHIFT
) : 0);
2891 bits
|= (effective
.t_tal_engaged
? POLICY_EFF_TAL_ENGAGED
: 0);
2892 bits
|= (effective
.t_suspended
? POLICY_EFF_SUSPENDED
: 0);
2893 bits
|= (effective
.t_watchers_bg
? POLICY_EFF_WATCHERS_BG
: 0);
2894 bits
|= (effective
.t_sup_active
? POLICY_EFF_SUP_ACTIVE
: 0);
2895 bits
|= (effective
.t_suppressed_cpu
? POLICY_EFF_SUP_CPU
: 0);
2896 bits
|= (effective
.t_role
? (((uint64_t)effective
.t_role
) << POLICY_EFF_ROLE_SHIFT
) : 0);
2897 bits
|= (effective
.t_latency_qos
? (((uint64_t)effective
.t_latency_qos
) << POLICY_EFF_LATENCY_QOS_SHIFT
) : 0);
2898 bits
|= (effective
.t_through_qos
? (((uint64_t)effective
.t_through_qos
) << POLICY_EFF_THROUGH_QOS_SHIFT
) : 0);
2899 bits
|= (effective
.t_sfi_managed
? POLICY_EFF_SFI_MANAGED
: 0);
2900 bits
|= (effective
.t_qos_ceiling
? (((uint64_t)effective
.t_qos_ceiling
) << POLICY_EFF_QOS_CEILING_SHIFT
) : 0);
2907 * Resource usage and CPU related routines
2911 proc_get_task_ruse_cpu(task_t task
, uint32_t *policyp
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
)
2920 error
= task_get_cpuusage(task
, percentagep
, intervalp
, deadlinep
, &scope
);
2924 * Reverse-map from CPU resource limit scopes back to policies (see comment below).
2926 if (scope
== TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
2927 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
;
2928 } else if (scope
== TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
2929 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE
;
2930 } else if (scope
== TASK_RUSECPU_FLAGS_DEADLINE
) {
2931 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
2938 * Configure the default CPU usage monitor parameters.
2940 * For tasks which have this mechanism activated: if any thread in the
2941 * process consumes more CPU than this, an EXC_RESOURCE exception will be generated.
2944 proc_init_cpumon_params(void)
2946 if (!PE_parse_boot_argn("max_cpumon_percentage", &proc_max_cpumon_percentage
,
2947 sizeof (proc_max_cpumon_percentage
))) {
2948 proc_max_cpumon_percentage
= DEFAULT_CPUMON_PERCENTAGE
;
2951 if (proc_max_cpumon_percentage
> 100) {
2952 proc_max_cpumon_percentage
= 100;
2955 /* The interval should be specified in seconds. */
2956 if (!PE_parse_boot_argn("max_cpumon_interval", &proc_max_cpumon_interval
,
2957 sizeof (proc_max_cpumon_interval
))) {
2958 proc_max_cpumon_interval
= DEFAULT_CPUMON_INTERVAL
;
2961 proc_max_cpumon_interval
*= NSEC_PER_SEC
;
2963 /* TEMPORARY boot arg to control App suppression */
2964 PE_parse_boot_argn("task_policy_suppression_disable",
2965 &task_policy_suppression_disable
,
2966 sizeof(task_policy_suppression_disable
));
2970 * Currently supported configurations for CPU limits.
2972 * Policy | Deadline-based CPU limit | Percentage-based CPU limit
2973 * -------------------------------------+--------------------------+------------------------------
2974 * PROC_POLICY_RSRCACT_THROTTLE | ENOTSUP | Task-wide scope only
2975 * PROC_POLICY_RSRCACT_SUSPEND | Task-wide scope only | ENOTSUP
2976 * PROC_POLICY_RSRCACT_TERMINATE | Task-wide scope only | ENOTSUP
2977 * PROC_POLICY_RSRCACT_NOTIFY_KQ | Task-wide scope only | ENOTSUP
2978 * PROC_POLICY_RSRCACT_NOTIFY_EXC | ENOTSUP | Per-thread scope only
2980 * A deadline-based CPU limit is actually a simple wallclock timer - the requested action is performed
2981 * after the specified amount of wallclock time has elapsed.
2983 * A percentage-based CPU limit performs the requested action after the specified amount of actual CPU time
2984 * has been consumed -- regardless of how much wallclock time has elapsed -- by either the task as an
2985 * aggregate entity (so-called "Task-wide" or "Proc-wide" scope, whereby the CPU time consumed by all threads
2986 * in the task are added together), or by any one thread in the task (so-called "per-thread" scope).
2988 * We support either deadline != 0 OR percentage != 0, but not both. The original intention in having them
2989 * share an API was to use actual CPU time as the basis of the deadline-based limit (as in: perform an action
2990 * after I have used some amount of CPU time; this is different than the recurring percentage/interval model)
2991 * but the potential consumer of the API at the time was insisting on wallclock time instead.
2993 * Currently, requesting notification via an exception is the only way to get per-thread scope for a
2994 * CPU limit. All other types of notifications force task-wide scope for the limit.
2997 proc_set_task_ruse_cpu(task_t task
, uint32_t policy
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
,
2998 int cpumon_entitled
)
3004 * Enforce the matrix of supported configurations for policy, percentage, and deadline.
3007 // If no policy is explicitly given, the default is to throttle.
3008 case TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
:
3009 case TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE
:
3012 scope
= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
3014 case TASK_POLICY_RESOURCE_ATTRIBUTE_SUSPEND
:
3015 case TASK_POLICY_RESOURCE_ATTRIBUTE_TERMINATE
:
3016 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_KQ
:
3017 if (percentage
!= 0)
3019 scope
= TASK_RUSECPU_FLAGS_DEADLINE
;
3021 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
:
3024 scope
= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
3025 #ifdef CONFIG_NOMONITORS
3027 #endif /* CONFIG_NOMONITORS */
3034 if (task
!= current_task()) {
3035 task
->policy_ru_cpu_ext
= policy
;
3037 task
->policy_ru_cpu
= policy
;
3039 error
= task_set_cpuusage(task
, percentage
, interval
, deadline
, scope
, cpumon_entitled
);
3045 proc_clear_task_ruse_cpu(task_t task
, int cpumon_entitled
)
3049 void * bsdinfo
= NULL
;
3052 if (task
!= current_task()) {
3053 task
->policy_ru_cpu_ext
= TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT
;
3055 task
->policy_ru_cpu
= TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT
;
3058 error
= task_clear_cpuusage_locked(task
, cpumon_entitled
);
3062 action
= task
->applied_ru_cpu
;
3063 if (task
->applied_ru_cpu_ext
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
3065 task
->applied_ru_cpu_ext
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
3067 if (action
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
3068 bsdinfo
= task
->bsd_info
;
3070 proc_restore_resource_actions(bsdinfo
, TASK_POLICY_CPU_RESOURCE_USAGE
, action
);
3081 /* used to apply resource limit related actions */
3083 task_apply_resource_actions(task_t task
, int type
)
3085 int action
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
3086 void * bsdinfo
= NULL
;
3089 case TASK_POLICY_CPU_RESOURCE_USAGE
:
3091 case TASK_POLICY_WIREDMEM_RESOURCE_USAGE
:
3092 case TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE
:
3093 case TASK_POLICY_DISK_RESOURCE_USAGE
:
3094 case TASK_POLICY_NETWORK_RESOURCE_USAGE
:
3095 case TASK_POLICY_POWER_RESOURCE_USAGE
:
3102 /* only cpu actions for now */
3105 if (task
->applied_ru_cpu_ext
== TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
3107 task
->applied_ru_cpu_ext
= task
->policy_ru_cpu_ext
;
3108 action
= task
->applied_ru_cpu_ext
;
3110 action
= task
->applied_ru_cpu_ext
;
3113 if (action
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
3114 bsdinfo
= task
->bsd_info
;
3116 proc_apply_resource_actions(bsdinfo
, TASK_POLICY_CPU_RESOURCE_USAGE
, action
);
3124 * XXX This API is somewhat broken; we support multiple simultaneous CPU limits, but the get/set API
3125 * only allows for one at a time. This means that if there is a per-thread limit active, the other
3126 * "scopes" will not be accessible via this API. We could change it to pass in the scope of interest
3127 * to the caller, and prefer that, but there's no need for that at the moment.
3130 task_get_cpuusage(task_t task
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
, int *scope
)
3136 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) {
3137 *scope
= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
3138 *percentagep
= task
->rusage_cpu_perthr_percentage
;
3139 *intervalp
= task
->rusage_cpu_perthr_interval
;
3140 } else if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PROC_LIMIT
) != 0) {
3141 *scope
= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
3142 *percentagep
= task
->rusage_cpu_percentage
;
3143 *intervalp
= task
->rusage_cpu_interval
;
3144 } else if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_DEADLINE
) != 0) {
3145 *scope
= TASK_RUSECPU_FLAGS_DEADLINE
;
3146 *deadlinep
= task
->rusage_cpu_deadline
;
3155 * Disable the CPU usage monitor for the task. Return value indicates
3156 * if the mechanism was actually enabled.
3159 task_disable_cpumon(task_t task
) {
3162 task_lock_assert_owned(task
);
3164 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
3165 return (KERN_INVALID_ARGUMENT
);
3168 #if CONFIG_TELEMETRY
3170 * Disable task-wide telemetry if it was ever enabled by the CPU usage
3171 * monitor's warning zone.
3173 telemetry_task_ctl_locked(task
, TF_CPUMON_WARNING
, 0);
3177 * Disable the monitor for the task, and propagate that change to each thread.
3179 task
->rusage_cpu_flags
&= ~(TASK_RUSECPU_FLAGS_PERTHR_LIMIT
| TASK_RUSECPU_FLAGS_FATAL_CPUMON
);
3180 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3181 set_astledger(thread
);
3183 task
->rusage_cpu_perthr_percentage
= 0;
3184 task
->rusage_cpu_perthr_interval
= 0;
3186 return (KERN_SUCCESS
);
3190 task_set_cpuusage(task_t task
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
, int scope
, int cpumon_entitled
)
3193 uint64_t abstime
= 0;
3194 uint64_t limittime
= 0;
3196 lck_mtx_assert(&task
->lock
, LCK_MTX_ASSERT_OWNED
);
3198 /* By default, refill once per second */
3200 interval
= NSEC_PER_SEC
;
3202 if (percentage
!= 0) {
3203 if (scope
== TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
3204 boolean_t warn
= FALSE
;
3207 * A per-thread CPU limit on a task generates an exception
3208 * (LEDGER_ACTION_EXCEPTION) if any one thread in the task
3209 * exceeds the limit.
3212 if (percentage
== TASK_POLICY_CPUMON_DISABLE
) {
3213 if (cpumon_entitled
) {
3214 task_disable_cpumon(task
);
3219 * This task wishes to disable the CPU usage monitor, but it's
3220 * missing the required entitlement:
3221 * com.apple.private.kernel.override-cpumon
3223 * Instead, treat this as a request to reset its params
3224 * back to the defaults.
3227 percentage
= TASK_POLICY_CPUMON_DEFAULTS
;
3230 if (percentage
== TASK_POLICY_CPUMON_DEFAULTS
) {
3231 percentage
= proc_max_cpumon_percentage
;
3232 interval
= proc_max_cpumon_interval
;
3235 if (percentage
> 100) {
3240 * Passing in an interval of -1 means either:
3241 * - Leave the interval as-is, if there's already a per-thread
3243 * - Use the system default.
3245 if (interval
== -1ULL) {
3246 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
3247 interval
= task
->rusage_cpu_perthr_interval
;
3249 interval
= proc_max_cpumon_interval
;
3254 * Enforce global caps on CPU usage monitor here if the process is not
3255 * entitled to escape the global caps.
3257 if ((percentage
> proc_max_cpumon_percentage
) && (cpumon_entitled
== 0)) {
3259 percentage
= proc_max_cpumon_percentage
;
3262 if ((interval
> proc_max_cpumon_interval
) && (cpumon_entitled
== 0)) {
3264 interval
= proc_max_cpumon_interval
;
3269 char *procname
= (char *)"unknown";
3272 pid
= proc_selfpid();
3273 if (current_task()->bsd_info
!= NULL
) {
3274 procname
= proc_name_address(current_task()->bsd_info
);
3278 printf("process %s[%d] denied attempt to escape CPU monitor"
3279 " (missing required entitlement).\n", procname
, pid
);
3282 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
3283 task
->rusage_cpu_perthr_percentage
= percentage
;
3284 task
->rusage_cpu_perthr_interval
= interval
;
3285 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
3286 set_astledger(thread
);
3288 } else if (scope
== TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
3290 * Currently, a proc-wide CPU limit always blocks if the limit is
3291 * exceeded (LEDGER_ACTION_BLOCK).
3293 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
3294 task
->rusage_cpu_percentage
= percentage
;
3295 task
->rusage_cpu_interval
= interval
;
3297 limittime
= (interval
* percentage
) / 100;
3298 nanoseconds_to_absolutetime(limittime
, &abstime
);
3300 ledger_set_limit(task
->ledger
, task_ledgers
.cpu_time
, abstime
, 0);
3301 ledger_set_period(task
->ledger
, task_ledgers
.cpu_time
, interval
);
3302 ledger_set_action(task
->ledger
, task_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
3306 if (deadline
!= 0) {
3307 assert(scope
== TASK_RUSECPU_FLAGS_DEADLINE
);
3309 /* if already in use, cancel and wait for it to cleanout */
3310 if (task
->rusage_cpu_callt
!= NULL
) {
3312 thread_call_cancel_wait(task
->rusage_cpu_callt
);
3315 if (task
->rusage_cpu_callt
== NULL
) {
3316 task
->rusage_cpu_callt
= thread_call_allocate_with_priority(task_action_cpuusage
, (thread_call_param_t
)task
, THREAD_CALL_PRIORITY_KERNEL
);
3319 if (task
->rusage_cpu_callt
!= 0) {
3320 uint64_t save_abstime
= 0;
3322 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_DEADLINE
;
3323 task
->rusage_cpu_deadline
= deadline
;
3325 nanoseconds_to_absolutetime(deadline
, &abstime
);
3326 save_abstime
= abstime
;
3327 clock_absolutetime_interval_to_deadline(save_abstime
, &abstime
);
3328 thread_call_enter_delayed(task
->rusage_cpu_callt
, abstime
);
3336 task_clear_cpuusage(task_t task
, int cpumon_entitled
)
3341 retval
= task_clear_cpuusage_locked(task
, cpumon_entitled
);
3348 task_clear_cpuusage_locked(task_t task
, int cpumon_entitled
)
3350 thread_call_t savecallt
;
3352 /* cancel percentage handling if set */
3353 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
3354 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PROC_LIMIT
;
3355 ledger_set_limit(task
->ledger
, task_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
3356 task
->rusage_cpu_percentage
= 0;
3357 task
->rusage_cpu_interval
= 0;
3361 * Disable the CPU usage monitor.
3363 if (cpumon_entitled
) {
3364 task_disable_cpumon(task
);
3367 /* cancel deadline handling if set */
3368 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_DEADLINE
) {
3369 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_DEADLINE
;
3370 if (task
->rusage_cpu_callt
!= 0) {
3371 savecallt
= task
->rusage_cpu_callt
;
3372 task
->rusage_cpu_callt
= NULL
;
3373 task
->rusage_cpu_deadline
= 0;
3375 thread_call_cancel_wait(savecallt
);
3376 thread_call_free(savecallt
);
3383 /* called by ledger unit to enforce action due to resource usage criteria being met */
3385 task_action_cpuusage(thread_call_param_t param0
, __unused thread_call_param_t param1
)
3387 task_t task
= (task_t
)param0
;
3388 (void)task_apply_resource_actions(task
, TASK_POLICY_CPU_RESOURCE_USAGE
);
3394 * Routines for taskwatch and pidbind
3399 * Routines for importance donation/inheritance/boosting
3403 task_importance_update_live_donor(task_t target_task
)
3405 #if IMPORTANCE_INHERITANCE
3407 ipc_importance_task_t task_imp
;
3409 task_imp
= ipc_importance_for_task(target_task
, FALSE
);
3410 if (IIT_NULL
!= task_imp
) {
3411 ipc_importance_task_update_live_donor(task_imp
);
3412 ipc_importance_task_release(task_imp
);
3414 #endif /* IMPORTANCE_INHERITANCE */
3418 task_importance_mark_donor(task_t task
, boolean_t donating
)
3420 #if IMPORTANCE_INHERITANCE
3421 ipc_importance_task_t task_imp
;
3423 task_imp
= ipc_importance_for_task(task
, FALSE
);
3424 if (IIT_NULL
!= task_imp
) {
3425 ipc_importance_task_mark_donor(task_imp
, donating
);
3426 ipc_importance_task_release(task_imp
);
3428 #endif /* IMPORTANCE_INHERITANCE */
3432 task_importance_mark_live_donor(task_t task
, boolean_t live_donating
)
3434 #if IMPORTANCE_INHERITANCE
3435 ipc_importance_task_t task_imp
;
3437 task_imp
= ipc_importance_for_task(task
, FALSE
);
3438 if (IIT_NULL
!= task_imp
) {
3439 ipc_importance_task_mark_live_donor(task_imp
, live_donating
);
3440 ipc_importance_task_release(task_imp
);
3442 #endif /* IMPORTANCE_INHERITANCE */
3446 task_importance_mark_receiver(task_t task
, boolean_t receiving
)
3448 #if IMPORTANCE_INHERITANCE
3449 ipc_importance_task_t task_imp
;
3451 task_imp
= ipc_importance_for_task(task
, FALSE
);
3452 if (IIT_NULL
!= task_imp
) {
3453 ipc_importance_task_mark_receiver(task_imp
, receiving
);
3454 ipc_importance_task_release(task_imp
);
3456 #endif /* IMPORTANCE_INHERITANCE */
3460 task_importance_mark_denap_receiver(task_t task
, boolean_t denap
)
3462 #if IMPORTANCE_INHERITANCE
3463 ipc_importance_task_t task_imp
;
3465 task_imp
= ipc_importance_for_task(task
, FALSE
);
3466 if (IIT_NULL
!= task_imp
) {
3467 ipc_importance_task_mark_denap_receiver(task_imp
, denap
);
3468 ipc_importance_task_release(task_imp
);
3470 #endif /* IMPORTANCE_INHERITANCE */
3474 task_importance_reset(__imp_only task_t task
)
3476 #if IMPORTANCE_INHERITANCE
3477 ipc_importance_task_t task_imp
;
3479 /* TODO: Lower importance downstream before disconnect */
3480 task_imp
= task
->task_imp_base
;
3481 ipc_importance_reset(task_imp
, FALSE
);
3482 task_importance_update_live_donor(task
);
3483 #endif /* IMPORTANCE_INHERITANCE */
3486 #if IMPORTANCE_INHERITANCE
3489 * Sets the task boost bit to the provided value. Does NOT run the update function.
3491 * Task lock must be held.
3494 task_set_boost_locked(task_t task
, boolean_t boost_active
)
3496 #if IMPORTANCE_DEBUG
3497 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_BOOST
, (boost_active
? IMP_BOOSTED
: IMP_UNBOOSTED
)) | DBG_FUNC_START
),
3498 proc_selfpid(), audit_token_pid_from_task(task
), trequested_0(task
, THREAD_NULL
), trequested_1(task
, THREAD_NULL
), 0);
3501 task
->requested_policy
.t_boosted
= boost_active
;
3503 #if IMPORTANCE_DEBUG
3504 if (boost_active
== TRUE
){
3505 DTRACE_BOOST2(boost
, task_t
, task
, int, audit_token_pid_from_task(task
));
3507 DTRACE_BOOST2(unboost
, task_t
, task
, int, audit_token_pid_from_task(task
));
3509 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_BOOST
, (boost_active
? IMP_BOOSTED
: IMP_UNBOOSTED
)) | DBG_FUNC_END
),
3510 proc_selfpid(), audit_token_pid_from_task(task
),
3511 trequested_0(task
, THREAD_NULL
), trequested_1(task
, THREAD_NULL
), 0);
3516 * Sets the task boost bit to the provided value and applies the update.
3518 * Task lock must be held. Must call update complete after unlocking the task.
3521 task_update_boost_locked(task_t task
, boolean_t boost_active
, task_pend_token_t pend_token
)
3523 task_set_boost_locked(task
, boost_active
);
3525 task_policy_update_locked(task
, THREAD_NULL
, pend_token
);
3529 * Check if this task should donate importance.
3531 * May be called without taking the task lock. In that case, donor status can change
3532 * so you must check only once for each donation event.
3535 task_is_importance_donor(task_t task
)
3537 if (task
->task_imp_base
== IIT_NULL
)
3539 return ipc_importance_task_is_donor(task
->task_imp_base
);
3543 * Query the status of the task's donor mark.
3546 task_is_marked_importance_donor(task_t task
)
3548 if (task
->task_imp_base
== IIT_NULL
)
3550 return ipc_importance_task_is_marked_donor(task
->task_imp_base
);
3554 * Query the status of the task's live donor and donor mark.
3557 task_is_marked_live_importance_donor(task_t task
)
3559 if (task
->task_imp_base
== IIT_NULL
)
3561 return ipc_importance_task_is_marked_live_donor(task
->task_imp_base
);
3566 * This routine may be called without holding task lock
3567 * since the value of imp_receiver can never be unset.
3570 task_is_importance_receiver(task_t task
)
3572 if (task
->task_imp_base
== IIT_NULL
)
3574 return ipc_importance_task_is_marked_receiver(task
->task_imp_base
);
3578 * Query the task's receiver mark.
3581 task_is_marked_importance_receiver(task_t task
)
3583 if (task
->task_imp_base
== IIT_NULL
)
3585 return ipc_importance_task_is_marked_receiver(task
->task_imp_base
);
3589 * This routine may be called without holding task lock
3590 * since the value of de-nap receiver can never be unset.
3593 task_is_importance_denap_receiver(task_t task
)
3595 if (task
->task_imp_base
== IIT_NULL
)
3597 return ipc_importance_task_is_denap_receiver(task
->task_imp_base
);
3601 * Query the task's de-nap receiver mark.
3604 task_is_marked_importance_denap_receiver(task_t task
)
3606 if (task
->task_imp_base
== IIT_NULL
)
3608 return ipc_importance_task_is_marked_denap_receiver(task
->task_imp_base
);
3612 * This routine may be called without holding task lock
3613 * since the value of imp_receiver can never be unset.
3616 task_is_importance_receiver_type(task_t task
)
3618 if (task
->task_imp_base
== IIT_NULL
)
3620 return (task_is_importance_receiver(task
) ||
3621 task_is_importance_denap_receiver(task
));
3625 * External importance assertions are managed by the process in userspace
3626 * Internal importance assertions are the responsibility of the kernel
3627 * Assertions are changed from internal to external via task_importance_externalize_assertion
3631 task_importance_hold_watchport_assertion(task_t target_task
, uint32_t count
)
3633 ipc_importance_task_t task_imp
;
3636 /* must already have set up an importance */
3637 task_imp
= target_task
->task_imp_base
;
3638 assert(IIT_NULL
!= task_imp
);
3640 ret
= ipc_importance_task_hold_internal_assertion(task_imp
, count
);
3641 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
3645 task_importance_hold_internal_assertion(task_t target_task
, uint32_t count
)
3647 ipc_importance_task_t task_imp
;
3650 /* may be first time, so allow for possible importance setup */
3651 task_imp
= ipc_importance_for_task(target_task
, FALSE
);
3652 if (IIT_NULL
== task_imp
) {
3655 ret
= ipc_importance_task_hold_internal_assertion(task_imp
, count
);
3656 ipc_importance_task_release(task_imp
);
3658 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
3662 task_importance_hold_file_lock_assertion(task_t target_task
, uint32_t count
)
3664 ipc_importance_task_t task_imp
;
3667 /* may be first time, so allow for possible importance setup */
3668 task_imp
= ipc_importance_for_task(target_task
, FALSE
);
3669 if (IIT_NULL
== task_imp
) {
3672 ret
= ipc_importance_task_hold_file_lock_assertion(task_imp
, count
);
3673 ipc_importance_task_release(task_imp
);
3675 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
3679 task_importance_hold_legacy_external_assertion(task_t target_task
, uint32_t count
)
3681 ipc_importance_task_t task_imp
;
3684 /* must already have set up an importance */
3685 task_imp
= target_task
->task_imp_base
;
3686 if (IIT_NULL
== task_imp
) {
3689 ret
= ipc_importance_task_hold_legacy_external_assertion(task_imp
, count
);
3690 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
3694 task_importance_drop_internal_assertion(task_t target_task
, uint32_t count
)
3696 ipc_importance_task_t task_imp
;
3699 /* must already have set up an importance */
3700 task_imp
= target_task
->task_imp_base
;
3701 if (IIT_NULL
== task_imp
) {
3704 ret
= ipc_importance_task_drop_internal_assertion(target_task
->task_imp_base
, count
);
3705 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
3709 task_importance_drop_file_lock_assertion(task_t target_task
, uint32_t count
)
3711 ipc_importance_task_t task_imp
;
3714 /* must already have set up an importance */
3715 task_imp
= target_task
->task_imp_base
;
3716 if (IIT_NULL
== task_imp
) {
3719 ret
= ipc_importance_task_drop_file_lock_assertion(target_task
->task_imp_base
, count
);
3720 return (KERN_SUCCESS
!= ret
) ? EOVERFLOW
: 0;
3724 task_importance_drop_legacy_external_assertion(task_t target_task
, uint32_t count
)
3726 ipc_importance_task_t task_imp
;
3729 /* must already have set up an importance */
3730 task_imp
= target_task
->task_imp_base
;
3731 if (IIT_NULL
== task_imp
) {
3734 ret
= ipc_importance_task_drop_legacy_external_assertion(task_imp
, count
);
3735 return (KERN_SUCCESS
!= ret
) ? EOVERFLOW
: 0;
3739 task_add_importance_watchport(task_t task
, mach_port_t port
, int *boostp
)
3743 __impdebug_only
int released_pid
= 0;
3744 __impdebug_only
int pid
= audit_token_pid_from_task(task
);
3746 ipc_importance_task_t release_imp_task
= IIT_NULL
;
3748 if (IP_VALID(port
) != 0) {
3749 ipc_importance_task_t new_imp_task
= ipc_importance_for_task(task
, FALSE
);
3754 * The port must have been marked tempowner already.
3755 * This also filters out ports whose receive rights
3756 * are already enqueued in a message, as you can't
3757 * change the right's destination once it's already
3760 if (port
->ip_tempowner
!= 0) {
3761 assert(port
->ip_impdonation
!= 0);
3763 boost
= port
->ip_impcount
;
3764 if (IIT_NULL
!= port
->ip_imp_task
) {
3766 * if this port is already bound to a task,
3767 * release the task reference and drop any
3768 * watchport-forwarded boosts
3770 release_imp_task
= port
->ip_imp_task
;
3771 port
->ip_imp_task
= IIT_NULL
;
3774 /* mark the port is watching another task (reference held in port->ip_imp_task) */
3775 if (ipc_importance_task_is_marked_receiver(new_imp_task
)) {
3776 port
->ip_imp_task
= new_imp_task
;
3777 new_imp_task
= IIT_NULL
;
3782 if (IIT_NULL
!= new_imp_task
) {
3783 ipc_importance_task_release(new_imp_task
);
3786 if (IIT_NULL
!= release_imp_task
) {
3788 ipc_importance_task_drop_internal_assertion(release_imp_task
, boost
);
3790 // released_pid = audit_token_pid_from_task(release_imp_task); /* TODO: Need ref-safe way to get pid */
3791 ipc_importance_task_release(release_imp_task
);
3793 #if IMPORTANCE_DEBUG
3794 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_WATCHPORT
, 0)) | DBG_FUNC_NONE
,
3795 proc_selfpid(), pid
, boost
, released_pid
, 0);
3796 #endif /* IMPORTANCE_DEBUG */
3803 #endif /* IMPORTANCE_INHERITANCE */
3806 * Routines for VM to query task importance
3811 * Order to be considered while estimating importance
3812 * for low memory notification and purging purgeable memory.
3814 #define TASK_IMPORTANCE_FOREGROUND 4
3815 #define TASK_IMPORTANCE_NOTDARWINBG 1
3819 * Checks if the task is already notified.
3821 * Condition: task lock should be held while calling this function.
3824 task_has_been_notified(task_t task
, int pressurelevel
)
3830 if (pressurelevel
== kVMPressureWarning
)
3831 return (task
->low_mem_notified_warn
? TRUE
: FALSE
);
3832 else if (pressurelevel
== kVMPressureCritical
)
3833 return (task
->low_mem_notified_critical
? TRUE
: FALSE
);
3840 * Checks if the task is used for purging.
3842 * Condition: task lock should be held while calling this function.
3845 task_used_for_purging(task_t task
, int pressurelevel
)
3851 if (pressurelevel
== kVMPressureWarning
)
3852 return (task
->purged_memory_warn
? TRUE
: FALSE
);
3853 else if (pressurelevel
== kVMPressureCritical
)
3854 return (task
->purged_memory_critical
? TRUE
: FALSE
);
3861 * Mark the task as notified with memory notification.
3863 * Condition: task lock should be held while calling this function.
3866 task_mark_has_been_notified(task_t task
, int pressurelevel
)
3872 if (pressurelevel
== kVMPressureWarning
)
3873 task
->low_mem_notified_warn
= 1;
3874 else if (pressurelevel
== kVMPressureCritical
)
3875 task
->low_mem_notified_critical
= 1;
3880 * Mark the task as purged.
3882 * Condition: task lock should be held while calling this function.
3885 task_mark_used_for_purging(task_t task
, int pressurelevel
)
3891 if (pressurelevel
== kVMPressureWarning
)
3892 task
->purged_memory_warn
= 1;
3893 else if (pressurelevel
== kVMPressureCritical
)
3894 task
->purged_memory_critical
= 1;
3899 * Mark the task eligible for low memory notification.
3901 * Condition: task lock should be held while calling this function.
3904 task_clear_has_been_notified(task_t task
, int pressurelevel
)
3910 if (pressurelevel
== kVMPressureWarning
)
3911 task
->low_mem_notified_warn
= 0;
3912 else if (pressurelevel
== kVMPressureCritical
)
3913 task
->low_mem_notified_critical
= 0;
3918 * Mark the task eligible for purging its purgeable memory.
3920 * Condition: task lock should be held while calling this function.
3923 task_clear_used_for_purging(task_t task
)
3929 task
->purged_memory_warn
= 0;
3930 task
->purged_memory_critical
= 0;
3935 * Estimate task importance for purging its purgeable memory
3936 * and low memory notification.
3938 * Importance is calculated in the following order of criteria:
3939 * -Task role : Background vs Foreground
3940 * -Boost status: Not boosted vs Boosted
3941 * -Darwin BG status.
3943 * Returns: Estimated task importance. Less important task will have lower
3944 * estimated importance.
3947 task_importance_estimate(task_t task
)
3949 int task_importance
= 0;
3955 if (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
) == TASK_FOREGROUND_APPLICATION
)
3956 task_importance
+= TASK_IMPORTANCE_FOREGROUND
;
3958 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
) == 0)
3959 task_importance
+= TASK_IMPORTANCE_NOTDARWINBG
;
3961 return task_importance
;