2 * Copyright (c) 2000-2016 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 <kern/policy_internal.h>
30 #include <mach/task_policy.h>
32 #include <mach/mach_types.h>
33 #include <mach/task_server.h>
35 #include <kern/host.h> /* host_priv_self() */
36 #include <mach/host_priv.h> /* host_get_special_port() */
37 #include <mach/host_special_ports.h> /* RESOURCE_NOTIFY_PORT */
38 #include <kern/sched.h>
39 #include <kern/task.h>
40 #include <mach/thread_policy.h>
41 #include <sys/errno.h>
42 #include <sys/resource.h>
43 #include <machine/limits.h>
44 #include <kern/ledger.h>
45 #include <kern/thread_call.h>
47 #include <kern/coalition.h>
49 #include <kern/telemetry.h>
52 #if IMPORTANCE_INHERITANCE
53 #include <ipc/ipc_importance.h>
55 #include <mach/machine/sdt.h>
56 #endif /* IMPORTANCE_DEBUG */
57 #endif /* IMPORTANCE_INHERITACE */
59 #include <sys/kdebug.h>
64 * This subsystem manages task and thread IO priority and backgrounding,
65 * as well as importance inheritance, process suppression, task QoS, and apptype.
66 * These properties have a suprising number of complex interactions, so they are
67 * centralized here in one state machine to simplify the implementation of those interactions.
70 * Threads and tasks have two policy fields: requested, effective.
71 * Requested represents the wishes of each interface that influences task policy.
72 * Effective represents the distillation of that policy into a set of behaviors.
74 * Each thread making a modification in the policy system passes a 'pending' struct,
75 * which tracks updates that will be applied after dropping the policy engine lock.
77 * Each interface that has an input into the task policy state machine controls a field in requested.
78 * If the interface has a getter, it returns what is in the field in requested, but that is
79 * not necessarily what is actually in effect.
81 * All kernel subsystems that behave differently based on task policy call into
82 * the proc_get_effective_(task|thread)_policy functions, which return the decision of the task policy state machine
83 * for that subsystem by querying only the 'effective' field.
85 * Policy change operations:
86 * Here are the steps to change a policy on a task or thread:
88 * 2) Change requested field for the relevant policy
89 * 3) Run a task policy update, which recalculates effective based on requested,
90 * then takes a diff between the old and new versions of requested and calls the relevant
91 * other subsystems to apply these changes, and updates the pending field.
93 * 5) Run task policy update complete, which looks at the pending field to update
94 * subsystems which cannot be touched while holding the task lock.
96 * To add a new requested policy, add the field in the requested struct, the flavor in task.h,
97 * the setter and getter in proc_(set|get)_task_policy*,
98 * then set up the effects of that behavior in task_policy_update*. If the policy manifests
99 * itself as a distinct effective policy, add it to the effective struct and add it to the
100 * proc_get_effective_task_policy accessor.
102 * Most policies are set via proc_set_task_policy, but policies that don't fit that interface
103 * roll their own lock/set/update/unlock/complete code inside this file.
108 * These are a set of behaviors that can be requested for a task. They currently have specific
109 * implied actions when they're enabled, but they may be made customizable in the future.
111 * When the affected task is boosted, we temporarily disable the suppression behaviors
112 * so that the affected process has a chance to run so it can call the API to permanently
113 * disable the suppression behaviors.
117 * Changing task policy on a task takes the task lock.
118 * Changing task policy on a thread takes the thread mutex.
119 * Task policy changes that affect threads will take each thread's mutex to update it if necessary.
121 * Querying the effective policy does not take a lock, because callers
122 * may run in interrupt context or other place where locks are not OK.
124 * This means that any notification of state change needs to be externally synchronized.
125 * We do this by idempotent callouts after the state has changed to ask
126 * other subsystems to update their view of the world.
128 * TODO: Move all cpu/wakes/io monitor code into a separate file
129 * TODO: Move all importance code over to importance subsystem
130 * TODO: Move all taskwatch code into a separate file
131 * TODO: Move all VM importance code into a separate file
134 /* Task policy related helper functions */
135 static void proc_set_task_policy_locked(task_t task
, int category
, int flavor
, int value
, int value2
);
137 static void task_policy_update_locked(task_t task
, task_pend_token_t pend_token
);
138 static void task_policy_update_internal_locked(task_t task
, boolean_t in_create
, task_pend_token_t pend_token
);
140 /* For attributes that have two scalars as input/output */
141 static void proc_set_task_policy2(task_t task
, int category
, int flavor
, int value1
, int value2
);
142 static void proc_get_task_policy2(task_t task
, int category
, int flavor
, int *value1
, int *value2
);
145 static boolean_t
task_policy_update_coalition_focal_tasks(task_t task
, int prev_role
, int next_role
);
148 static uint64_t task_requested_bitfield(task_t task
);
149 static uint64_t task_effective_bitfield(task_t task
);
151 /* Convenience functions for munging a policy bitfield into a tracepoint */
152 static uintptr_t trequested_0(task_t task
);
153 static uintptr_t trequested_1(task_t task
);
154 static uintptr_t teffective_0(task_t task
);
155 static uintptr_t teffective_1(task_t task
);
157 /* CPU limits helper functions */
158 static int task_set_cpuusage(task_t task
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
, int scope
, int entitled
);
159 static int task_get_cpuusage(task_t task
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
, int *scope
);
160 static int task_enable_cpumon_locked(task_t task
);
161 static int task_disable_cpumon(task_t task
);
162 static int task_clear_cpuusage_locked(task_t task
, int cpumon_entitled
);
163 static int task_apply_resource_actions(task_t task
, int type
);
164 static void task_action_cpuusage(thread_call_param_t param0
, thread_call_param_t param1
);
167 typedef struct proc
* proc_t
;
168 int proc_pid(void *proc
);
169 extern int proc_selfpid(void);
170 extern char * proc_name_address(void *p
);
171 extern char * proc_best_name(proc_t proc
);
173 extern int proc_pidpathinfo_internal(proc_t p
, uint64_t arg
,
174 char *buffer
, uint32_t buffersize
,
176 #endif /* MACH_BSD */
180 /* Importance Inheritance related helper functions */
182 #if IMPORTANCE_INHERITANCE
184 static void task_importance_mark_live_donor(task_t task
, boolean_t donating
);
185 static void task_importance_mark_receiver(task_t task
, boolean_t receiving
);
186 static void task_importance_mark_denap_receiver(task_t task
, boolean_t denap
);
188 static boolean_t
task_is_marked_live_importance_donor(task_t task
);
189 static boolean_t
task_is_importance_receiver(task_t task
);
190 static boolean_t
task_is_importance_denap_receiver(task_t task
);
192 static int task_importance_hold_internal_assertion(task_t target_task
, uint32_t count
);
194 static void task_add_importance_watchport(task_t task
, mach_port_t port
, int *boostp
);
195 static void task_importance_update_live_donor(task_t target_task
);
197 static void task_set_boost_locked(task_t task
, boolean_t boost_active
);
199 #endif /* IMPORTANCE_INHERITANCE */
202 #define __impdebug_only
204 #define __impdebug_only __unused
207 #if IMPORTANCE_INHERITANCE
210 #define __imp_only __unused
214 * Default parameters for certain policies
217 int proc_standard_daemon_tier
= THROTTLE_LEVEL_TIER1
;
218 int proc_suppressed_disk_tier
= THROTTLE_LEVEL_TIER1
;
219 int proc_tal_disk_tier
= THROTTLE_LEVEL_TIER1
;
221 int proc_graphics_timer_qos
= (LATENCY_QOS_TIER_0
& 0xFF);
223 const int proc_default_bg_iotier
= THROTTLE_LEVEL_TIER2
;
225 /* Latency/throughput QoS fields remain zeroed, i.e. TIER_UNSPECIFIED at creation */
226 const struct task_requested_policy default_task_requested_policy
= {
227 .trp_bg_iotier
= proc_default_bg_iotier
229 const struct task_effective_policy default_task_effective_policy
= {};
232 * Default parameters for CPU usage monitor.
234 * Default setting is 50% over 3 minutes.
236 #define DEFAULT_CPUMON_PERCENTAGE 50
237 #define DEFAULT_CPUMON_INTERVAL (3 * 60)
239 uint8_t proc_max_cpumon_percentage
;
240 uint64_t proc_max_cpumon_interval
;
244 qos_latency_policy_validate(task_latency_qos_t ltier
) {
245 if ((ltier
!= LATENCY_QOS_TIER_UNSPECIFIED
) &&
246 ((ltier
> LATENCY_QOS_TIER_5
) || (ltier
< LATENCY_QOS_TIER_0
)))
247 return KERN_INVALID_ARGUMENT
;
253 qos_throughput_policy_validate(task_throughput_qos_t ttier
) {
254 if ((ttier
!= THROUGHPUT_QOS_TIER_UNSPECIFIED
) &&
255 ((ttier
> THROUGHPUT_QOS_TIER_5
) || (ttier
< THROUGHPUT_QOS_TIER_0
)))
256 return KERN_INVALID_ARGUMENT
;
262 task_qos_policy_validate(task_qos_policy_t qosinfo
, mach_msg_type_number_t count
) {
263 if (count
< TASK_QOS_POLICY_COUNT
)
264 return KERN_INVALID_ARGUMENT
;
266 task_latency_qos_t ltier
= qosinfo
->task_latency_qos_tier
;
267 task_throughput_qos_t ttier
= qosinfo
->task_throughput_qos_tier
;
269 kern_return_t kr
= qos_latency_policy_validate(ltier
);
271 if (kr
!= KERN_SUCCESS
)
274 kr
= qos_throughput_policy_validate(ttier
);
280 qos_extract(uint32_t qv
) {
285 qos_latency_policy_package(uint32_t qv
) {
286 return (qv
== LATENCY_QOS_TIER_UNSPECIFIED
) ? LATENCY_QOS_TIER_UNSPECIFIED
: ((0xFF << 16) | qv
);
290 qos_throughput_policy_package(uint32_t qv
) {
291 return (qv
== THROUGHPUT_QOS_TIER_UNSPECIFIED
) ? THROUGHPUT_QOS_TIER_UNSPECIFIED
: ((0xFE << 16) | qv
);
294 /* TEMPORARY boot-arg controlling task_policy suppression (App Nap) */
295 static boolean_t task_policy_suppression_disable
= FALSE
;
300 task_policy_flavor_t flavor
,
301 task_policy_t policy_info
,
302 mach_msg_type_number_t count
)
304 kern_return_t result
= KERN_SUCCESS
;
306 if (task
== TASK_NULL
|| task
== kernel_task
)
307 return (KERN_INVALID_ARGUMENT
);
311 case TASK_CATEGORY_POLICY
: {
312 task_category_policy_t info
= (task_category_policy_t
)policy_info
;
314 if (count
< TASK_CATEGORY_POLICY_COUNT
)
315 return (KERN_INVALID_ARGUMENT
);
319 case TASK_FOREGROUND_APPLICATION
:
320 case TASK_BACKGROUND_APPLICATION
:
321 case TASK_DEFAULT_APPLICATION
:
322 proc_set_task_policy(task
,
323 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
327 case TASK_CONTROL_APPLICATION
:
328 if (task
!= current_task() || task
->sec_token
.val
[0] != 0)
329 result
= KERN_INVALID_ARGUMENT
;
331 proc_set_task_policy(task
,
332 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
336 case TASK_GRAPHICS_SERVER
:
337 /* TODO: Restrict this role to FCFS <rdar://problem/12552788> */
338 if (task
!= current_task() || task
->sec_token
.val
[0] != 0)
339 result
= KERN_INVALID_ARGUMENT
;
341 proc_set_task_policy(task
,
342 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
346 result
= KERN_INVALID_ARGUMENT
;
348 } /* switch (info->role) */
353 /* Desired energy-efficiency/performance "quality-of-service" */
354 case TASK_BASE_QOS_POLICY
:
355 case TASK_OVERRIDE_QOS_POLICY
:
357 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
358 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
360 if (kr
!= KERN_SUCCESS
)
364 uint32_t lqos
= qos_extract(qosinfo
->task_latency_qos_tier
);
365 uint32_t tqos
= qos_extract(qosinfo
->task_throughput_qos_tier
);
367 proc_set_task_policy2(task
, TASK_POLICY_ATTRIBUTE
,
368 flavor
== TASK_BASE_QOS_POLICY
? TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
: TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
,
373 case TASK_BASE_LATENCY_QOS_POLICY
:
375 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
376 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
378 if (kr
!= KERN_SUCCESS
)
381 uint32_t lqos
= qos_extract(qosinfo
->task_latency_qos_tier
);
383 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
, TASK_BASE_LATENCY_QOS_POLICY
, lqos
);
387 case TASK_BASE_THROUGHPUT_QOS_POLICY
:
389 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
390 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
392 if (kr
!= KERN_SUCCESS
)
395 uint32_t tqos
= qos_extract(qosinfo
->task_throughput_qos_tier
);
397 proc_set_task_policy(task
, TASK_POLICY_ATTRIBUTE
, TASK_BASE_THROUGHPUT_QOS_POLICY
, tqos
);
401 case TASK_SUPPRESSION_POLICY
:
404 task_suppression_policy_t info
= (task_suppression_policy_t
)policy_info
;
406 if (count
< TASK_SUPPRESSION_POLICY_COUNT
)
407 return (KERN_INVALID_ARGUMENT
);
409 struct task_qos_policy qosinfo
;
411 qosinfo
.task_latency_qos_tier
= info
->timer_throttle
;
412 qosinfo
.task_throughput_qos_tier
= info
->throughput_qos
;
414 kern_return_t kr
= task_qos_policy_validate(&qosinfo
, TASK_QOS_POLICY_COUNT
);
416 if (kr
!= KERN_SUCCESS
)
419 /* TEMPORARY disablement of task suppression */
420 if (task_policy_suppression_disable
&& info
->active
)
423 struct task_pend_token pend_token
= {};
427 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
428 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION
, info
->active
)) | DBG_FUNC_START
,
429 proc_selfpid(), task_pid(task
), trequested_0(task
),
430 trequested_1(task
), 0);
432 task
->requested_policy
.trp_sup_active
= (info
->active
) ? 1 : 0;
433 task
->requested_policy
.trp_sup_lowpri_cpu
= (info
->lowpri_cpu
) ? 1 : 0;
434 task
->requested_policy
.trp_sup_timer
= qos_extract(info
->timer_throttle
);
435 task
->requested_policy
.trp_sup_disk
= (info
->disk_throttle
) ? 1 : 0;
436 task
->requested_policy
.trp_sup_throughput
= qos_extract(info
->throughput_qos
);
437 task
->requested_policy
.trp_sup_cpu
= (info
->suppressed_cpu
) ? 1 : 0;
438 task
->requested_policy
.trp_sup_bg_sockets
= (info
->background_sockets
) ? 1 : 0;
440 task_policy_update_locked(task
, &pend_token
);
442 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
443 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION
, info
->active
)) | DBG_FUNC_END
,
444 proc_selfpid(), task_pid(task
), trequested_0(task
),
445 trequested_1(task
), 0);
449 task_policy_update_complete_unlocked(task
, &pend_token
);
456 result
= KERN_INVALID_ARGUMENT
;
463 /* Sets BSD 'nice' value on the task */
467 integer_t importance
)
469 if (task
== TASK_NULL
|| task
== kernel_task
)
470 return (KERN_INVALID_ARGUMENT
);
477 return (KERN_TERMINATED
);
480 if (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
) >= TASK_CONTROL_APPLICATION
) {
483 return (KERN_INVALID_ARGUMENT
);
486 task
->importance
= importance
;
488 struct task_pend_token pend_token
= {};
490 task_policy_update_locked(task
, &pend_token
);
494 task_policy_update_complete_unlocked(task
, &pend_token
);
496 return (KERN_SUCCESS
);
502 task_policy_flavor_t flavor
,
503 task_policy_t policy_info
,
504 mach_msg_type_number_t
*count
,
505 boolean_t
*get_default
)
507 if (task
== TASK_NULL
|| task
== kernel_task
)
508 return (KERN_INVALID_ARGUMENT
);
512 case TASK_CATEGORY_POLICY
:
514 task_category_policy_t info
= (task_category_policy_t
)policy_info
;
516 if (*count
< TASK_CATEGORY_POLICY_COUNT
)
517 return (KERN_INVALID_ARGUMENT
);
520 info
->role
= TASK_UNSPECIFIED
;
522 info
->role
= proc_get_task_policy(task
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
526 case TASK_BASE_QOS_POLICY
: /* FALLTHRU */
527 case TASK_OVERRIDE_QOS_POLICY
:
529 task_qos_policy_t info
= (task_qos_policy_t
)policy_info
;
531 if (*count
< TASK_QOS_POLICY_COUNT
)
532 return (KERN_INVALID_ARGUMENT
);
535 info
->task_latency_qos_tier
= LATENCY_QOS_TIER_UNSPECIFIED
;
536 info
->task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_UNSPECIFIED
;
537 } else if (flavor
== TASK_BASE_QOS_POLICY
) {
540 proc_get_task_policy2(task
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
, &value1
, &value2
);
542 info
->task_latency_qos_tier
= qos_latency_policy_package(value1
);
543 info
->task_throughput_qos_tier
= qos_throughput_policy_package(value2
);
545 } else if (flavor
== TASK_OVERRIDE_QOS_POLICY
) {
548 proc_get_task_policy2(task
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
, &value1
, &value2
);
550 info
->task_latency_qos_tier
= qos_latency_policy_package(value1
);
551 info
->task_throughput_qos_tier
= qos_throughput_policy_package(value2
);
557 case TASK_POLICY_STATE
:
559 task_policy_state_t info
= (task_policy_state_t
)policy_info
;
561 if (*count
< TASK_POLICY_STATE_COUNT
)
562 return (KERN_INVALID_ARGUMENT
);
564 /* Only root can get this info */
565 if (current_task()->sec_token
.val
[0] != 0)
566 return KERN_PROTECTION_FAILURE
;
572 info
->imp_assertcnt
= 0;
573 info
->imp_externcnt
= 0;
575 info
->imp_transitions
= 0;
579 info
->requested
= task_requested_bitfield(task
);
580 info
->effective
= task_effective_bitfield(task
);
583 info
->tps_requested_policy
= *(uint64_t*)(&task
->requested_policy
);
584 info
->tps_effective_policy
= *(uint64_t*)(&task
->effective_policy
);
587 if (task
->task_imp_base
!= NULL
) {
588 info
->imp_assertcnt
= task
->task_imp_base
->iit_assertcnt
;
589 info
->imp_externcnt
= IIT_EXTERN(task
->task_imp_base
);
590 info
->flags
|= (task_is_marked_importance_receiver(task
) ? TASK_IMP_RECEIVER
: 0);
591 info
->flags
|= (task_is_marked_importance_denap_receiver(task
) ? TASK_DENAP_RECEIVER
: 0);
592 info
->flags
|= (task_is_marked_importance_donor(task
) ? TASK_IMP_DONOR
: 0);
593 info
->flags
|= (task_is_marked_live_importance_donor(task
) ? TASK_IMP_LIVE_DONOR
: 0);
594 info
->imp_transitions
= task
->task_imp_base
->iit_transitions
;
596 info
->imp_assertcnt
= 0;
597 info
->imp_externcnt
= 0;
598 info
->imp_transitions
= 0;
606 case TASK_SUPPRESSION_POLICY
:
608 task_suppression_policy_t info
= (task_suppression_policy_t
)policy_info
;
610 if (*count
< TASK_SUPPRESSION_POLICY_COUNT
)
611 return (KERN_INVALID_ARGUMENT
);
617 info
->lowpri_cpu
= 0;
618 info
->timer_throttle
= LATENCY_QOS_TIER_UNSPECIFIED
;
619 info
->disk_throttle
= 0;
622 info
->throughput_qos
= 0;
623 info
->suppressed_cpu
= 0;
625 info
->active
= task
->requested_policy
.trp_sup_active
;
626 info
->lowpri_cpu
= task
->requested_policy
.trp_sup_lowpri_cpu
;
627 info
->timer_throttle
= qos_latency_policy_package(task
->requested_policy
.trp_sup_timer
);
628 info
->disk_throttle
= task
->requested_policy
.trp_sup_disk
;
631 info
->throughput_qos
= qos_throughput_policy_package(task
->requested_policy
.trp_sup_throughput
);
632 info
->suppressed_cpu
= task
->requested_policy
.trp_sup_cpu
;
633 info
->background_sockets
= task
->requested_policy
.trp_sup_bg_sockets
;
641 return (KERN_INVALID_ARGUMENT
);
644 return (KERN_SUCCESS
);
648 * Called at task creation
649 * We calculate the correct effective but don't apply it to anything yet.
650 * The threads, etc will inherit from the task as they get created.
653 task_policy_create(task_t task
, task_t parent_task
)
655 task
->requested_policy
.trp_apptype
= parent_task
->requested_policy
.trp_apptype
;
657 task
->requested_policy
.trp_int_darwinbg
= parent_task
->requested_policy
.trp_int_darwinbg
;
658 task
->requested_policy
.trp_ext_darwinbg
= parent_task
->requested_policy
.trp_ext_darwinbg
;
659 task
->requested_policy
.trp_int_iotier
= parent_task
->requested_policy
.trp_int_iotier
;
660 task
->requested_policy
.trp_ext_iotier
= parent_task
->requested_policy
.trp_ext_iotier
;
661 task
->requested_policy
.trp_int_iopassive
= parent_task
->requested_policy
.trp_int_iopassive
;
662 task
->requested_policy
.trp_ext_iopassive
= parent_task
->requested_policy
.trp_ext_iopassive
;
663 task
->requested_policy
.trp_bg_iotier
= parent_task
->requested_policy
.trp_bg_iotier
;
664 task
->requested_policy
.trp_terminated
= parent_task
->requested_policy
.trp_terminated
;
665 task
->requested_policy
.trp_qos_clamp
= parent_task
->requested_policy
.trp_qos_clamp
;
667 if (task
->requested_policy
.trp_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&& !task_is_exec_copy(task
)) {
668 /* Do not update the apptype for exec copy task */
669 if (parent_task
->requested_policy
.trp_boosted
) {
670 task
->requested_policy
.trp_apptype
= TASK_APPTYPE_DAEMON_INTERACTIVE
;
671 task_importance_mark_donor(task
, TRUE
);
673 task
->requested_policy
.trp_apptype
= TASK_APPTYPE_DAEMON_BACKGROUND
;
674 task_importance_mark_receiver(task
, FALSE
);
678 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
679 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
680 task_pid(task
), teffective_0(task
),
681 teffective_1(task
), task
->priority
, 0);
683 task_policy_update_internal_locked(task
, TRUE
, NULL
);
685 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
686 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
687 task_pid(task
), teffective_0(task
),
688 teffective_1(task
), task
->priority
, 0);
690 task_importance_update_live_donor(task
);
695 task_policy_update_locked(task_t task
, task_pend_token_t pend_token
)
697 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
698 (IMPORTANCE_CODE(IMP_UPDATE
, TASK_POLICY_TASK
) | DBG_FUNC_START
),
699 task_pid(task
), teffective_0(task
),
700 teffective_1(task
), task
->priority
, 0);
702 task_policy_update_internal_locked(task
, FALSE
, pend_token
);
704 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
705 (IMPORTANCE_CODE(IMP_UPDATE
, TASK_POLICY_TASK
)) | DBG_FUNC_END
,
706 task_pid(task
), teffective_0(task
),
707 teffective_1(task
), task
->priority
, 0);
711 * One state update function TO RULE THEM ALL
713 * This function updates the task or thread effective policy fields
714 * and pushes the results to the relevant subsystems.
716 * Must call update_complete after unlocking the task,
717 * as some subsystems cannot be updated while holding the task lock.
719 * Called with task locked, not thread
723 task_policy_update_internal_locked(task_t task
, boolean_t in_create
, task_pend_token_t pend_token
)
727 * Gather requested policy
730 struct task_requested_policy requested
= task
->requested_policy
;
734 * Calculate new effective policies from requested policy and task state
736 * Don't change requested, it won't take effect
739 struct task_effective_policy next
= {};
741 /* Update task role */
742 next
.tep_role
= requested
.trp_role
;
744 /* Set task qos clamp and ceiling */
745 next
.tep_qos_clamp
= requested
.trp_qos_clamp
;
747 if (requested
.trp_apptype
== TASK_APPTYPE_APP_DEFAULT
||
748 requested
.trp_apptype
== TASK_APPTYPE_APP_TAL
) {
750 switch (next
.tep_role
) {
751 case TASK_FOREGROUND_APPLICATION
:
752 /* Foreground apps get urgent scheduler priority */
753 next
.tep_qos_ui_is_urgent
= 1;
754 next
.tep_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
757 case TASK_BACKGROUND_APPLICATION
:
758 /* This is really 'non-focal but on-screen' */
759 next
.tep_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
762 case TASK_DEFAULT_APPLICATION
:
763 /* This is 'may render UI but we don't know if it's focal/nonfocal' */
764 next
.tep_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
767 case TASK_NONUI_APPLICATION
:
768 /* i.e. 'off-screen' */
769 next
.tep_qos_ceiling
= THREAD_QOS_LEGACY
;
772 case TASK_CONTROL_APPLICATION
:
773 case TASK_GRAPHICS_SERVER
:
774 next
.tep_qos_ui_is_urgent
= 1;
775 next
.tep_qos_ceiling
= THREAD_QOS_UNSPECIFIED
;
778 case TASK_THROTTLE_APPLICATION
:
779 /* i.e. 'TAL launch' */
780 next
.tep_qos_ceiling
= THREAD_QOS_UTILITY
;
783 case TASK_UNSPECIFIED
:
785 /* Apps that don't have an application role get
786 * USER_INTERACTIVE and USER_INITIATED squashed to LEGACY */
787 next
.tep_qos_ceiling
= THREAD_QOS_LEGACY
;
791 /* Daemons get USER_INTERACTIVE squashed to USER_INITIATED */
792 next
.tep_qos_ceiling
= THREAD_QOS_USER_INITIATED
;
795 /* Calculate DARWIN_BG */
796 boolean_t wants_darwinbg
= FALSE
;
797 boolean_t wants_all_sockets_bg
= FALSE
; /* Do I want my existing sockets to be bg */
798 boolean_t wants_watchersbg
= FALSE
; /* Do I want my pidbound threads to be bg */
801 * If DARWIN_BG has been requested at either level, it's engaged.
802 * Only true DARWIN_BG changes cause watchers to transition.
804 * Backgrounding due to apptype does.
806 if (requested
.trp_int_darwinbg
|| requested
.trp_ext_darwinbg
)
807 wants_watchersbg
= wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
809 /* Background TAL apps are throttled when TAL is enabled */
810 if (requested
.trp_apptype
== TASK_APPTYPE_APP_TAL
&&
811 requested
.trp_role
== TASK_BACKGROUND_APPLICATION
&&
812 requested
.trp_tal_enabled
== 1) {
813 next
.tep_tal_engaged
= 1;
816 if ((requested
.trp_apptype
== TASK_APPTYPE_APP_DEFAULT
||
817 requested
.trp_apptype
== TASK_APPTYPE_APP_TAL
) &&
818 requested
.trp_role
== TASK_THROTTLE_APPLICATION
) {
819 next
.tep_tal_engaged
= 1;
822 /* Adaptive daemons are DARWIN_BG unless boosted, and don't get network throttled. */
823 if (requested
.trp_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&&
824 requested
.trp_boosted
== 0)
825 wants_darwinbg
= TRUE
;
827 /* Background daemons are always DARWIN_BG, no exceptions, and don't get network throttled. */
828 if (requested
.trp_apptype
== TASK_APPTYPE_DAEMON_BACKGROUND
)
829 wants_darwinbg
= TRUE
;
831 if (next
.tep_qos_clamp
== THREAD_QOS_BACKGROUND
|| next
.tep_qos_clamp
== THREAD_QOS_MAINTENANCE
)
832 wants_darwinbg
= TRUE
;
834 /* Calculate side effects of DARWIN_BG */
836 if (wants_darwinbg
) {
837 next
.tep_darwinbg
= 1;
838 /* darwinbg tasks always create bg sockets, but we don't always loop over all sockets */
839 next
.tep_new_sockets_bg
= 1;
840 next
.tep_lowpri_cpu
= 1;
843 if (wants_all_sockets_bg
)
844 next
.tep_all_sockets_bg
= 1;
846 if (wants_watchersbg
)
847 next
.tep_watchers_bg
= 1;
849 /* Calculate low CPU priority */
851 boolean_t wants_lowpri_cpu
= FALSE
;
854 wants_lowpri_cpu
= TRUE
;
856 if (next
.tep_tal_engaged
)
857 wants_lowpri_cpu
= TRUE
;
859 if (requested
.trp_sup_lowpri_cpu
&& requested
.trp_boosted
== 0)
860 wants_lowpri_cpu
= TRUE
;
862 if (wants_lowpri_cpu
)
863 next
.tep_lowpri_cpu
= 1;
865 /* Calculate IO policy */
867 /* Update BG IO policy (so we can see if it has changed) */
868 next
.tep_bg_iotier
= requested
.trp_bg_iotier
;
870 int iopol
= THROTTLE_LEVEL_TIER0
;
873 iopol
= MAX(iopol
, requested
.trp_bg_iotier
);
875 if (requested
.trp_apptype
== TASK_APPTYPE_DAEMON_STANDARD
)
876 iopol
= MAX(iopol
, proc_standard_daemon_tier
);
878 if (requested
.trp_sup_disk
&& requested
.trp_boosted
== 0)
879 iopol
= MAX(iopol
, proc_suppressed_disk_tier
);
881 if (next
.tep_tal_engaged
)
882 iopol
= MAX(iopol
, proc_tal_disk_tier
);
884 if (next
.tep_qos_clamp
!= THREAD_QOS_UNSPECIFIED
)
885 iopol
= MAX(iopol
, thread_qos_policy_params
.qos_iotier
[next
.tep_qos_clamp
]);
887 iopol
= MAX(iopol
, requested
.trp_int_iotier
);
888 iopol
= MAX(iopol
, requested
.trp_ext_iotier
);
890 next
.tep_io_tier
= iopol
;
892 /* Calculate Passive IO policy */
894 if (requested
.trp_ext_iopassive
|| requested
.trp_int_iopassive
)
895 next
.tep_io_passive
= 1;
897 /* Calculate suppression-active flag */
898 if (requested
.trp_sup_active
&& requested
.trp_boosted
== 0)
899 next
.tep_sup_active
= 1;
901 /* Calculate timer QOS */
902 int latency_qos
= requested
.trp_base_latency_qos
;
904 if (requested
.trp_sup_timer
&& requested
.trp_boosted
== 0)
905 latency_qos
= requested
.trp_sup_timer
;
907 if (next
.tep_qos_clamp
!= THREAD_QOS_UNSPECIFIED
)
908 latency_qos
= MAX(latency_qos
, (int)thread_qos_policy_params
.qos_latency_qos
[next
.tep_qos_clamp
]);
910 if (requested
.trp_over_latency_qos
!= 0)
911 latency_qos
= requested
.trp_over_latency_qos
;
913 /* Treat the windowserver special */
914 if (requested
.trp_role
== TASK_GRAPHICS_SERVER
)
915 latency_qos
= proc_graphics_timer_qos
;
917 next
.tep_latency_qos
= latency_qos
;
919 /* Calculate throughput QOS */
920 int through_qos
= requested
.trp_base_through_qos
;
922 if (requested
.trp_sup_throughput
&& requested
.trp_boosted
== 0)
923 through_qos
= requested
.trp_sup_throughput
;
925 if (next
.tep_qos_clamp
!= THREAD_QOS_UNSPECIFIED
)
926 through_qos
= MAX(through_qos
, (int)thread_qos_policy_params
.qos_through_qos
[next
.tep_qos_clamp
]);
928 if (requested
.trp_over_through_qos
!= 0)
929 through_qos
= requested
.trp_over_through_qos
;
931 next
.tep_through_qos
= through_qos
;
933 /* Calculate suppressed CPU priority */
934 if (requested
.trp_sup_cpu
&& requested
.trp_boosted
== 0)
935 next
.tep_suppressed_cpu
= 1;
938 * Calculate background sockets
939 * Don't take into account boosting to limit transition frequency.
941 if (requested
.trp_sup_bg_sockets
){
942 next
.tep_all_sockets_bg
= 1;
943 next
.tep_new_sockets_bg
= 1;
946 /* Apply SFI Managed class bit */
947 next
.tep_sfi_managed
= requested
.trp_sfi_managed
;
949 /* Calculate 'live donor' status for live importance */
950 switch (requested
.trp_apptype
) {
951 case TASK_APPTYPE_APP_TAL
:
952 case TASK_APPTYPE_APP_DEFAULT
:
953 if (requested
.trp_ext_darwinbg
== 0)
954 next
.tep_live_donor
= 1;
956 next
.tep_live_donor
= 0;
959 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
960 case TASK_APPTYPE_DAEMON_STANDARD
:
961 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
962 case TASK_APPTYPE_DAEMON_BACKGROUND
:
964 next
.tep_live_donor
= 0;
968 if (requested
.trp_terminated
) {
970 * Shoot down the throttles that slow down exit or response to SIGTERM
971 * We don't need to shoot down:
972 * passive (don't want to cause others to throttle)
973 * all_sockets_bg (don't need to iterate FDs on every exit)
974 * new_sockets_bg (doesn't matter for exiting process)
975 * pidsuspend (jetsam-ed BG process shouldn't run again)
976 * watchers_bg (watcher threads don't need to be unthrottled)
977 * latency_qos (affects userspace timers only)
980 next
.tep_terminated
= 1;
981 next
.tep_darwinbg
= 0;
982 next
.tep_lowpri_cpu
= 0;
983 next
.tep_io_tier
= THROTTLE_LEVEL_TIER0
;
984 next
.tep_tal_engaged
= 0;
985 next
.tep_role
= TASK_UNSPECIFIED
;
986 next
.tep_suppressed_cpu
= 0;
991 * Swap out old policy for new policy
994 struct task_effective_policy prev
= task
->effective_policy
;
996 /* This is the point where the new values become visible to other threads */
997 task
->effective_policy
= next
;
999 /* Don't do anything further to a half-formed task */
1003 if (task
== kernel_task
)
1004 panic("Attempting to set task policy on kernel_task");
1008 * Pend updates that can't be done while holding the task lock
1011 if (prev
.tep_all_sockets_bg
!= next
.tep_all_sockets_bg
)
1012 pend_token
->tpt_update_sockets
= 1;
1014 /* Only re-scan the timer list if the qos level is getting less strong */
1015 if (prev
.tep_latency_qos
> next
.tep_latency_qos
)
1016 pend_token
->tpt_update_timers
= 1;
1019 if (prev
.tep_live_donor
!= next
.tep_live_donor
)
1020 pend_token
->tpt_update_live_donor
= 1;
1024 * Update other subsystems as necessary if something has changed
1027 boolean_t update_threads
= FALSE
, update_sfi
= FALSE
;
1030 * Check for the attributes that thread_policy_update_internal_locked() consults,
1031 * and trigger thread policy re-evaluation.
1033 if (prev
.tep_io_tier
!= next
.tep_io_tier
||
1034 prev
.tep_bg_iotier
!= next
.tep_bg_iotier
||
1035 prev
.tep_io_passive
!= next
.tep_io_passive
||
1036 prev
.tep_darwinbg
!= next
.tep_darwinbg
||
1037 prev
.tep_qos_clamp
!= next
.tep_qos_clamp
||
1038 prev
.tep_qos_ceiling
!= next
.tep_qos_ceiling
||
1039 prev
.tep_qos_ui_is_urgent
!= next
.tep_qos_ui_is_urgent
||
1040 prev
.tep_latency_qos
!= next
.tep_latency_qos
||
1041 prev
.tep_through_qos
!= next
.tep_through_qos
||
1042 prev
.tep_lowpri_cpu
!= next
.tep_lowpri_cpu
||
1043 prev
.tep_new_sockets_bg
!= next
.tep_new_sockets_bg
||
1044 prev
.tep_terminated
!= next
.tep_terminated
)
1045 update_threads
= TRUE
;
1048 * Check for the attributes that sfi_thread_classify() consults,
1049 * and trigger SFI re-evaluation.
1051 if (prev
.tep_latency_qos
!= next
.tep_latency_qos
||
1052 prev
.tep_role
!= next
.tep_role
||
1053 prev
.tep_sfi_managed
!= next
.tep_sfi_managed
)
1056 #if CONFIG_SCHED_SFI
1057 /* Reflect task role transitions into the coalition role counters */
1058 if (prev
.tep_role
!= next
.tep_role
) {
1059 if (task_policy_update_coalition_focal_tasks(task
, prev
.tep_role
, next
.tep_role
)) {
1061 pend_token
->tpt_update_coal_sfi
= 1;
1064 #endif /* !CONFIG_SCHED_SFI */
1066 boolean_t update_priority
= FALSE
;
1068 int priority
= BASEPRI_DEFAULT
;
1069 int max_priority
= MAXPRI_USER
;
1071 if (next
.tep_lowpri_cpu
) {
1072 priority
= MAXPRI_THROTTLE
;
1073 max_priority
= MAXPRI_THROTTLE
;
1074 } else if (next
.tep_suppressed_cpu
) {
1075 priority
= MAXPRI_SUPPRESSED
;
1076 max_priority
= MAXPRI_SUPPRESSED
;
1078 switch (next
.tep_role
) {
1079 case TASK_CONTROL_APPLICATION
:
1080 priority
= BASEPRI_CONTROL
;
1082 case TASK_GRAPHICS_SERVER
:
1083 priority
= BASEPRI_GRAPHICS
;
1084 max_priority
= MAXPRI_RESERVED
;
1090 /* factor in 'nice' value */
1091 priority
+= task
->importance
;
1093 if (task
->effective_policy
.tep_qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
1094 int qos_clamp_priority
= thread_qos_policy_params
.qos_pri
[task
->effective_policy
.tep_qos_clamp
];
1096 priority
= MIN(priority
, qos_clamp_priority
);
1097 max_priority
= MIN(max_priority
, qos_clamp_priority
);
1100 if (priority
> max_priority
)
1101 priority
= max_priority
;
1102 else if (priority
< MINPRI
)
1106 assert(priority
<= max_priority
);
1108 /* avoid extra work if priority isn't changing */
1109 if (priority
!= task
->priority
||
1110 max_priority
!= task
->max_priority
) {
1111 /* update the scheduling priority for the task */
1112 task
->max_priority
= max_priority
;
1113 task
->priority
= priority
;
1114 update_priority
= TRUE
;
1117 /* Loop over the threads in the task:
1120 * with one thread mutex hold per thread
1122 if (update_threads
|| update_priority
|| update_sfi
) {
1125 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1126 struct task_pend_token thread_pend_token
= {};
1129 thread_pend_token
.tpt_update_thread_sfi
= 1;
1131 if (update_priority
|| update_threads
)
1132 thread_policy_update_tasklocked(thread
,
1133 task
->priority
, task
->max_priority
,
1134 &thread_pend_token
);
1136 assert(!thread_pend_token
.tpt_update_sockets
);
1138 // Slightly risky, as we still hold the task lock...
1139 thread_policy_update_complete_unlocked(thread
, &thread_pend_token
);
1145 #if CONFIG_SCHED_SFI
1147 * Yet another layering violation. We reach out and bang on the coalition directly.
1150 task_policy_update_coalition_focal_tasks(task_t task
,
1154 boolean_t sfi_transition
= FALSE
;
1156 /* task moving into/out-of the foreground */
1157 if (prev_role
!= TASK_FOREGROUND_APPLICATION
&& next_role
== TASK_FOREGROUND_APPLICATION
) {
1158 if (task_coalition_adjust_focal_count(task
, 1) == 1)
1159 sfi_transition
= TRUE
;
1160 } else if (prev_role
== TASK_FOREGROUND_APPLICATION
&& next_role
!= TASK_FOREGROUND_APPLICATION
) {
1161 if (task_coalition_adjust_focal_count(task
, -1) == 0)
1162 sfi_transition
= TRUE
;
1165 /* task moving into/out-of background */
1166 if (prev_role
!= TASK_BACKGROUND_APPLICATION
&& next_role
== TASK_BACKGROUND_APPLICATION
) {
1167 if (task_coalition_adjust_nonfocal_count(task
, 1) == 1)
1168 sfi_transition
= TRUE
;
1169 } else if (prev_role
== TASK_BACKGROUND_APPLICATION
&& next_role
!= TASK_BACKGROUND_APPLICATION
) {
1170 if (task_coalition_adjust_nonfocal_count(task
, -1) == 0)
1171 sfi_transition
= TRUE
;
1174 return sfi_transition
;
1177 /* coalition object is locked */
1179 task_sfi_reevaluate_cb(coalition_t coal
, void *ctx
, task_t task
)
1183 /* unused for now */
1186 /* skip the task we're re-evaluating on behalf of: it's already updated */
1187 if (task
== (task_t
)ctx
)
1192 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1193 sfi_reevaluate(thread
);
1198 #endif /* CONFIG_SCHED_SFI */
1201 * Called with task unlocked to do things that can't be done while holding the task lock
1204 task_policy_update_complete_unlocked(task_t task
, task_pend_token_t pend_token
)
1207 if (pend_token
->tpt_update_sockets
)
1208 proc_apply_task_networkbg(task
->bsd_info
, THREAD_NULL
);
1209 #endif /* MACH_BSD */
1211 /* The timer throttle has been removed or reduced, we need to look for expired timers and fire them */
1212 if (pend_token
->tpt_update_timers
)
1213 ml_timer_evaluate();
1216 if (pend_token
->tpt_update_live_donor
)
1217 task_importance_update_live_donor(task
);
1219 #if CONFIG_SCHED_SFI
1220 /* use the resource coalition for SFI re-evaluation */
1221 if (pend_token
->tpt_update_coal_sfi
)
1222 coalition_for_each_task(task
->coalition
[COALITION_TYPE_RESOURCE
],
1223 (void *)task
, task_sfi_reevaluate_cb
);
1224 #endif /* CONFIG_SCHED_SFI */
1228 * Initiate a task policy state transition
1230 * Everything that modifies requested except functions that need to hold the task lock
1231 * should use this function
1233 * Argument validation should be performed before reaching this point.
1235 * TODO: Do we need to check task->active?
1238 proc_set_task_policy(task_t task
,
1243 struct task_pend_token pend_token
= {};
1247 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1248 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
1249 task_pid(task
), trequested_0(task
),
1250 trequested_1(task
), value
, 0);
1252 proc_set_task_policy_locked(task
, category
, flavor
, value
, 0);
1254 task_policy_update_locked(task
, &pend_token
);
1257 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1258 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
1259 task_pid(task
), trequested_0(task
),
1260 trequested_1(task
), tpending(&pend_token
), 0);
1264 task_policy_update_complete_unlocked(task
, &pend_token
);
1268 * Variant of proc_set_task_policy() that sets two scalars in the requested policy structure.
1269 * Same locking rules apply.
1272 proc_set_task_policy2(task_t task
,
1278 struct task_pend_token pend_token
= {};
1282 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1283 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
1284 task_pid(task
), trequested_0(task
),
1285 trequested_1(task
), value
, 0);
1287 proc_set_task_policy_locked(task
, category
, flavor
, value
, value2
);
1289 task_policy_update_locked(task
, &pend_token
);
1291 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1292 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
1293 task_pid(task
), trequested_0(task
),
1294 trequested_1(task
), tpending(&pend_token
), 0);
1298 task_policy_update_complete_unlocked(task
, &pend_token
);
1302 * Set the requested state for a specific flavor to a specific value.
1305 * Verify that arguments to non iopol things are 1 or 0
1308 proc_set_task_policy_locked(task_t task
,
1316 struct task_requested_policy requested
= task
->requested_policy
;
1320 /* Category: EXTERNAL and INTERNAL */
1322 case TASK_POLICY_DARWIN_BG
:
1323 if (category
== TASK_POLICY_EXTERNAL
)
1324 requested
.trp_ext_darwinbg
= value
;
1326 requested
.trp_int_darwinbg
= value
;
1329 case TASK_POLICY_IOPOL
:
1330 proc_iopol_to_tier(value
, &tier
, &passive
);
1331 if (category
== TASK_POLICY_EXTERNAL
) {
1332 requested
.trp_ext_iotier
= tier
;
1333 requested
.trp_ext_iopassive
= passive
;
1335 requested
.trp_int_iotier
= tier
;
1336 requested
.trp_int_iopassive
= passive
;
1340 case TASK_POLICY_IO
:
1341 if (category
== TASK_POLICY_EXTERNAL
)
1342 requested
.trp_ext_iotier
= value
;
1344 requested
.trp_int_iotier
= value
;
1347 case TASK_POLICY_PASSIVE_IO
:
1348 if (category
== TASK_POLICY_EXTERNAL
)
1349 requested
.trp_ext_iopassive
= value
;
1351 requested
.trp_int_iopassive
= value
;
1354 /* Category: INTERNAL */
1356 case TASK_POLICY_DARWIN_BG_IOPOL
:
1357 assert(category
== TASK_POLICY_INTERNAL
);
1358 proc_iopol_to_tier(value
, &tier
, &passive
);
1359 requested
.trp_bg_iotier
= tier
;
1362 /* Category: ATTRIBUTE */
1364 case TASK_POLICY_TAL
:
1365 assert(category
== TASK_POLICY_ATTRIBUTE
);
1366 requested
.trp_tal_enabled
= value
;
1369 case TASK_POLICY_BOOST
:
1370 assert(category
== TASK_POLICY_ATTRIBUTE
);
1371 requested
.trp_boosted
= value
;
1374 case TASK_POLICY_ROLE
:
1375 assert(category
== TASK_POLICY_ATTRIBUTE
);
1376 requested
.trp_role
= value
;
1379 case TASK_POLICY_TERMINATED
:
1380 assert(category
== TASK_POLICY_ATTRIBUTE
);
1381 requested
.trp_terminated
= value
;
1384 case TASK_BASE_LATENCY_QOS_POLICY
:
1385 assert(category
== TASK_POLICY_ATTRIBUTE
);
1386 requested
.trp_base_latency_qos
= value
;
1389 case TASK_BASE_THROUGHPUT_QOS_POLICY
:
1390 assert(category
== TASK_POLICY_ATTRIBUTE
);
1391 requested
.trp_base_through_qos
= value
;
1394 case TASK_POLICY_SFI_MANAGED
:
1395 assert(category
== TASK_POLICY_ATTRIBUTE
);
1396 requested
.trp_sfi_managed
= value
;
1399 case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
:
1400 assert(category
== TASK_POLICY_ATTRIBUTE
);
1401 requested
.trp_base_latency_qos
= value
;
1402 requested
.trp_base_through_qos
= value2
;
1405 case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
:
1406 assert(category
== TASK_POLICY_ATTRIBUTE
);
1407 requested
.trp_over_latency_qos
= value
;
1408 requested
.trp_over_through_qos
= value2
;
1412 panic("unknown task policy: %d %d %d %d", category
, flavor
, value
, value2
);
1416 task
->requested_policy
= requested
;
1420 * Gets what you set. Effective values may be different.
1423 proc_get_task_policy(task_t task
,
1431 struct task_requested_policy requested
= task
->requested_policy
;
1434 case TASK_POLICY_DARWIN_BG
:
1435 if (category
== TASK_POLICY_EXTERNAL
)
1436 value
= requested
.trp_ext_darwinbg
;
1438 value
= requested
.trp_int_darwinbg
;
1440 case TASK_POLICY_IOPOL
:
1441 if (category
== TASK_POLICY_EXTERNAL
)
1442 value
= proc_tier_to_iopol(requested
.trp_ext_iotier
,
1443 requested
.trp_ext_iopassive
);
1445 value
= proc_tier_to_iopol(requested
.trp_int_iotier
,
1446 requested
.trp_int_iopassive
);
1448 case TASK_POLICY_IO
:
1449 if (category
== TASK_POLICY_EXTERNAL
)
1450 value
= requested
.trp_ext_iotier
;
1452 value
= requested
.trp_int_iotier
;
1454 case TASK_POLICY_PASSIVE_IO
:
1455 if (category
== TASK_POLICY_EXTERNAL
)
1456 value
= requested
.trp_ext_iopassive
;
1458 value
= requested
.trp_int_iopassive
;
1460 case TASK_POLICY_DARWIN_BG_IOPOL
:
1461 assert(category
== TASK_POLICY_ATTRIBUTE
);
1462 value
= proc_tier_to_iopol(requested
.trp_bg_iotier
, 0);
1464 case TASK_POLICY_ROLE
:
1465 assert(category
== TASK_POLICY_ATTRIBUTE
);
1466 value
= requested
.trp_role
;
1468 case TASK_POLICY_SFI_MANAGED
:
1469 assert(category
== TASK_POLICY_ATTRIBUTE
);
1470 value
= requested
.trp_sfi_managed
;
1473 panic("unknown policy_flavor %d", flavor
);
1483 * Variant of proc_get_task_policy() that returns two scalar outputs.
1486 proc_get_task_policy2(task_t task
,
1487 __assert_only
int category
,
1494 struct task_requested_policy requested
= task
->requested_policy
;
1497 case TASK_POLICY_BASE_LATENCY_AND_THROUGHPUT_QOS
:
1498 assert(category
== TASK_POLICY_ATTRIBUTE
);
1499 *value1
= requested
.trp_base_latency_qos
;
1500 *value2
= requested
.trp_base_through_qos
;
1503 case TASK_POLICY_OVERRIDE_LATENCY_AND_THROUGHPUT_QOS
:
1504 assert(category
== TASK_POLICY_ATTRIBUTE
);
1505 *value1
= requested
.trp_over_latency_qos
;
1506 *value2
= requested
.trp_over_through_qos
;
1510 panic("unknown policy_flavor %d", flavor
);
1518 * Function for querying effective state for relevant subsystems
1519 * Gets what is actually in effect, for subsystems which pull policy instead of receive updates.
1521 * ONLY the relevant subsystem should query this.
1522 * NEVER take a value from the 'effective' function and stuff it into a setter.
1524 * NOTE: This accessor does not take the task lock.
1525 * Notifications of state updates need to be externally synchronized with state queries.
1526 * This routine *MUST* remain interrupt safe, as it is potentially invoked
1527 * within the context of a timer interrupt. It is also called in KDP context for stackshot.
1530 proc_get_effective_task_policy(task_t task
,
1536 case TASK_POLICY_DARWIN_BG
:
1538 * This backs the KPI call proc_pidbackgrounded to find
1539 * out if a pid is backgrounded.
1540 * It is used to communicate state to the VM system, as well as
1541 * prioritizing requests to the graphics system.
1542 * Returns 1 for background mode, 0 for normal mode
1544 value
= task
->effective_policy
.tep_darwinbg
;
1546 case TASK_POLICY_ALL_SOCKETS_BG
:
1548 * do_background_socket() calls this to determine what it should do to the proc's sockets
1549 * Returns 1 for background mode, 0 for normal mode
1551 * This consults both thread and task so un-DBGing a thread while the task is BG
1552 * doesn't get you out of the network throttle.
1554 value
= task
->effective_policy
.tep_all_sockets_bg
;
1556 case TASK_POLICY_LATENCY_QOS
:
1558 * timer arming calls into here to find out the timer coalescing level
1559 * Returns a QoS tier (0-6)
1561 value
= task
->effective_policy
.tep_latency_qos
;
1563 case TASK_POLICY_THROUGH_QOS
:
1565 * This value is passed into the urgency callout from the scheduler
1566 * to the performance management subsystem.
1567 * Returns a QoS tier (0-6)
1569 value
= task
->effective_policy
.tep_through_qos
;
1571 case TASK_POLICY_ROLE
:
1573 * This controls various things that ask whether a process is foreground,
1574 * like SFI, VM, access to GPU, etc
1576 value
= task
->effective_policy
.tep_role
;
1578 case TASK_POLICY_WATCHERS_BG
:
1580 * This controls whether or not a thread watching this process should be BG.
1582 value
= task
->effective_policy
.tep_watchers_bg
;
1584 case TASK_POLICY_SFI_MANAGED
:
1586 * This controls whether or not a process is targeted for specific control by thermald.
1588 value
= task
->effective_policy
.tep_sfi_managed
;
1591 panic("unknown policy_flavor %d", flavor
);
1599 * Convert from IOPOL_* values to throttle tiers.
1601 * TODO: Can this be made more compact, like an array lookup
1602 * Note that it is possible to support e.g. IOPOL_PASSIVE_STANDARD in the future
1606 proc_iopol_to_tier(int iopolicy
, int *tier
, int *passive
)
1611 case IOPOL_IMPORTANT
:
1612 *tier
= THROTTLE_LEVEL_TIER0
;
1615 *tier
= THROTTLE_LEVEL_TIER0
;
1618 case IOPOL_STANDARD
:
1619 *tier
= THROTTLE_LEVEL_TIER1
;
1622 *tier
= THROTTLE_LEVEL_TIER2
;
1624 case IOPOL_THROTTLE
:
1625 *tier
= THROTTLE_LEVEL_TIER3
;
1628 panic("unknown I/O policy %d", iopolicy
);
1634 proc_tier_to_iopol(int tier
, int passive
)
1638 case THROTTLE_LEVEL_TIER0
:
1639 return IOPOL_PASSIVE
;
1641 panic("unknown passive tier %d", tier
);
1642 return IOPOL_DEFAULT
;
1646 case THROTTLE_LEVEL_NONE
:
1647 case THROTTLE_LEVEL_TIER0
:
1648 return IOPOL_DEFAULT
;
1649 case THROTTLE_LEVEL_TIER1
:
1650 return IOPOL_STANDARD
;
1651 case THROTTLE_LEVEL_TIER2
:
1652 return IOPOL_UTILITY
;
1653 case THROTTLE_LEVEL_TIER3
:
1654 return IOPOL_THROTTLE
;
1656 panic("unknown tier %d", tier
);
1657 return IOPOL_DEFAULT
;
1663 proc_darwin_role_to_task_role(int darwin_role
, int* task_role
)
1665 integer_t role
= TASK_UNSPECIFIED
;
1667 switch (darwin_role
) {
1668 case PRIO_DARWIN_ROLE_DEFAULT
:
1669 role
= TASK_UNSPECIFIED
;
1671 case PRIO_DARWIN_ROLE_UI_FOCAL
:
1672 role
= TASK_FOREGROUND_APPLICATION
;
1674 case PRIO_DARWIN_ROLE_UI
:
1675 role
= TASK_DEFAULT_APPLICATION
;
1677 case PRIO_DARWIN_ROLE_NON_UI
:
1678 role
= TASK_NONUI_APPLICATION
;
1680 case PRIO_DARWIN_ROLE_UI_NON_FOCAL
:
1681 role
= TASK_BACKGROUND_APPLICATION
;
1683 case PRIO_DARWIN_ROLE_TAL_LAUNCH
:
1684 role
= TASK_THROTTLE_APPLICATION
;
1696 proc_task_role_to_darwin_role(int task_role
)
1698 switch (task_role
) {
1699 case TASK_FOREGROUND_APPLICATION
:
1700 return PRIO_DARWIN_ROLE_UI_FOCAL
;
1701 case TASK_BACKGROUND_APPLICATION
:
1702 return PRIO_DARWIN_ROLE_UI_NON_FOCAL
;
1703 case TASK_NONUI_APPLICATION
:
1704 return PRIO_DARWIN_ROLE_NON_UI
;
1705 case TASK_DEFAULT_APPLICATION
:
1706 return PRIO_DARWIN_ROLE_UI
;
1707 case TASK_THROTTLE_APPLICATION
:
1708 return PRIO_DARWIN_ROLE_TAL_LAUNCH
;
1709 case TASK_UNSPECIFIED
:
1711 return PRIO_DARWIN_ROLE_DEFAULT
;
1716 /* TODO: remove this variable when interactive daemon audit period is over */
1717 extern boolean_t ipc_importance_interactive_receiver
;
1720 * Called at process exec to initialize the apptype, qos clamp, and qos seed of a process
1722 * TODO: Make this function more table-driven instead of ad-hoc
1725 proc_set_task_spawnpolicy(task_t task
, int apptype
, int qos_clamp
, int role
,
1726 ipc_port_t
* portwatch_ports
, int portwatch_count
)
1728 struct task_pend_token pend_token
= {};
1730 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1731 (IMPORTANCE_CODE(IMP_TASK_APPTYPE
, apptype
)) | DBG_FUNC_START
,
1732 task_pid(task
), trequested_0(task
), trequested_1(task
),
1736 case TASK_APPTYPE_APP_TAL
:
1737 case TASK_APPTYPE_APP_DEFAULT
:
1738 /* Apps become donors via the 'live-donor' flag instead of the static donor flag */
1739 task_importance_mark_donor(task
, FALSE
);
1740 task_importance_mark_live_donor(task
, TRUE
);
1741 task_importance_mark_receiver(task
, FALSE
);
1742 /* Apps are de-nap recievers on desktop for suppression behaviors */
1743 task_importance_mark_denap_receiver(task
, TRUE
);
1746 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
1747 task_importance_mark_donor(task
, TRUE
);
1748 task_importance_mark_live_donor(task
, FALSE
);
1751 * A boot arg controls whether interactive daemons are importance receivers.
1752 * Normally, they are not. But for testing their behavior as an adaptive
1753 * daemon, the boot-arg can be set.
1755 * TODO: remove this when the interactive daemon audit period is over.
1757 task_importance_mark_receiver(task
, /* FALSE */ ipc_importance_interactive_receiver
);
1758 task_importance_mark_denap_receiver(task
, FALSE
);
1761 case TASK_APPTYPE_DAEMON_STANDARD
:
1762 task_importance_mark_donor(task
, TRUE
);
1763 task_importance_mark_live_donor(task
, FALSE
);
1764 task_importance_mark_receiver(task
, FALSE
);
1765 task_importance_mark_denap_receiver(task
, FALSE
);
1768 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
1769 task_importance_mark_donor(task
, FALSE
);
1770 task_importance_mark_live_donor(task
, FALSE
);
1771 task_importance_mark_receiver(task
, TRUE
);
1772 task_importance_mark_denap_receiver(task
, FALSE
);
1775 case TASK_APPTYPE_DAEMON_BACKGROUND
:
1776 task_importance_mark_donor(task
, FALSE
);
1777 task_importance_mark_live_donor(task
, FALSE
);
1778 task_importance_mark_receiver(task
, FALSE
);
1779 task_importance_mark_denap_receiver(task
, FALSE
);
1782 case TASK_APPTYPE_NONE
:
1786 if (portwatch_ports
!= NULL
&& apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
) {
1787 int portwatch_boosts
= 0;
1789 for (int i
= 0; i
< portwatch_count
; i
++) {
1790 ipc_port_t port
= NULL
;
1792 if ((port
= portwatch_ports
[i
]) != NULL
) {
1794 task_add_importance_watchport(task
, port
, &boost
);
1795 portwatch_boosts
+= boost
;
1799 if (portwatch_boosts
> 0) {
1800 task_importance_hold_internal_assertion(task
, portwatch_boosts
);
1806 if (apptype
== TASK_APPTYPE_APP_TAL
) {
1807 /* TAL starts off enabled by default */
1808 task
->requested_policy
.trp_tal_enabled
= 1;
1811 if (apptype
!= TASK_APPTYPE_NONE
) {
1812 task
->requested_policy
.trp_apptype
= apptype
;
1815 if (role
!= TASK_UNSPECIFIED
) {
1816 task
->requested_policy
.trp_role
= role
;
1819 if (qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
1820 task
->requested_policy
.trp_qos_clamp
= qos_clamp
;
1823 task_policy_update_locked(task
, &pend_token
);
1827 /* Ensure the donor bit is updated to be in sync with the new live donor status */
1828 pend_token
.tpt_update_live_donor
= 1;
1830 task_policy_update_complete_unlocked(task
, &pend_token
);
1832 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1833 (IMPORTANCE_CODE(IMP_TASK_APPTYPE
, apptype
)) | DBG_FUNC_END
,
1834 task_pid(task
), trequested_0(task
), trequested_1(task
),
1835 task_is_importance_receiver(task
), 0);
1839 * Inherit task role across exec
1842 proc_inherit_task_role(task_t new_task
,
1847 /* inherit the role from old task to new task */
1848 role
= proc_get_task_policy(old_task
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
1849 proc_set_task_policy(new_task
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
, role
);
1852 extern task_t bsd_init_task
;
1855 * Compute the default main thread qos for a task
1858 task_compute_main_thread_qos(task_t task
)
1860 int primordial_qos
= THREAD_QOS_UNSPECIFIED
;
1862 int qos_clamp
= task
->requested_policy
.trp_qos_clamp
;
1864 switch (task
->requested_policy
.trp_apptype
) {
1865 case TASK_APPTYPE_APP_TAL
:
1866 case TASK_APPTYPE_APP_DEFAULT
:
1867 primordial_qos
= THREAD_QOS_USER_INTERACTIVE
;
1870 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
1871 case TASK_APPTYPE_DAEMON_STANDARD
:
1872 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
1873 primordial_qos
= THREAD_QOS_LEGACY
;
1876 case TASK_APPTYPE_DAEMON_BACKGROUND
:
1877 primordial_qos
= THREAD_QOS_BACKGROUND
;
1881 if (task
== bsd_init_task
) {
1882 /* PID 1 gets a special case */
1883 primordial_qos
= MAX(primordial_qos
, THREAD_QOS_USER_INITIATED
);
1886 if (qos_clamp
!= THREAD_QOS_UNSPECIFIED
) {
1887 if (primordial_qos
!= THREAD_QOS_UNSPECIFIED
) {
1888 primordial_qos
= MIN(qos_clamp
, primordial_qos
);
1890 primordial_qos
= qos_clamp
;
1894 return primordial_qos
;
1898 /* for process_policy to check before attempting to set */
1900 proc_task_is_tal(task_t task
)
1902 return (task
->requested_policy
.trp_apptype
== TASK_APPTYPE_APP_TAL
) ? TRUE
: FALSE
;
1906 task_get_apptype(task_t task
)
1908 return task
->requested_policy
.trp_apptype
;
1912 task_is_daemon(task_t task
)
1914 switch (task
->requested_policy
.trp_apptype
) {
1915 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
1916 case TASK_APPTYPE_DAEMON_STANDARD
:
1917 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
1918 case TASK_APPTYPE_DAEMON_BACKGROUND
:
1926 task_is_app(task_t task
)
1928 switch (task
->requested_policy
.trp_apptype
) {
1929 case TASK_APPTYPE_APP_DEFAULT
:
1930 case TASK_APPTYPE_APP_TAL
:
1939 task_grab_latency_qos(task_t task
)
1941 return qos_latency_policy_package(proc_get_effective_task_policy(task
, TASK_POLICY_LATENCY_QOS
));
1944 /* update the darwin background action state in the flags field for libproc */
1946 proc_get_darwinbgstate(task_t task
, uint32_t * flagsp
)
1948 if (task
->requested_policy
.trp_ext_darwinbg
)
1949 *flagsp
|= PROC_FLAG_EXT_DARWINBG
;
1951 if (task
->requested_policy
.trp_int_darwinbg
)
1952 *flagsp
|= PROC_FLAG_DARWINBG
;
1955 if (task
->requested_policy
.trp_apptype
== TASK_APPTYPE_APP_DEFAULT
||
1956 task
->requested_policy
.trp_apptype
== TASK_APPTYPE_APP_TAL
)
1957 *flagsp
|= PROC_FLAG_APPLICATION
;
1959 if (task
->requested_policy
.trp_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
)
1960 *flagsp
|= PROC_FLAG_ADAPTIVE
;
1962 if (task
->requested_policy
.trp_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&&
1963 task
->requested_policy
.trp_boosted
== 1)
1964 *flagsp
|= PROC_FLAG_ADAPTIVE_IMPORTANT
;
1966 if (task_is_importance_donor(task
))
1967 *flagsp
|= PROC_FLAG_IMPORTANCE_DONOR
;
1969 if (task
->effective_policy
.tep_sup_active
)
1970 *flagsp
|= PROC_FLAG_SUPPRESSED
;
1976 * Tracepoint data... Reading the tracepoint data can be somewhat complicated.
1977 * The current scheme packs as much data into a single tracepoint as it can.
1979 * Each task/thread requested/effective structure is 64 bits in size. Any
1980 * given tracepoint will emit either requested or effective data, but not both.
1982 * A tracepoint may emit any of task, thread, or task & thread data.
1984 * The type of data emitted varies with pointer size. Where possible, both
1985 * task and thread data are emitted. In LP32 systems, the first and second
1986 * halves of either the task or thread data is emitted.
1988 * The code uses uintptr_t array indexes instead of high/low to avoid
1989 * confusion WRT big vs little endian.
1991 * The truth table for the tracepoint data functions is below, and has the
1992 * following invariants:
1994 * 1) task and thread are uintptr_t*
1995 * 2) task may never be NULL
1999 * trequested_0(task, NULL) task[0] task[0]
2000 * trequested_1(task, NULL) task[1] NULL
2001 * trequested_0(task, thread) thread[0] task[0]
2002 * trequested_1(task, thread) thread[1] thread[0]
2004 * Basically, you get a full task or thread on LP32, and both on LP64.
2006 * The uintptr_t munging here is squicky enough to deserve a comment.
2008 * The variables we are accessing are laid out in memory like this:
2010 * [ LP64 uintptr_t 0 ]
2011 * [ LP32 uintptr_t 0 ] [ LP32 uintptr_t 1 ]
2018 trequested_0(task_t task
)
2020 static_assert(sizeof(struct task_requested_policy
) == sizeof(uint64_t), "size invariant violated");
2022 uintptr_t* raw
= (uintptr_t*)&task
->requested_policy
;
2028 trequested_1(task_t task
)
2030 #if defined __LP64__
2034 uintptr_t* raw
= (uintptr_t*)(&task
->requested_policy
);
2040 teffective_0(task_t task
)
2042 uintptr_t* raw
= (uintptr_t*)&task
->effective_policy
;
2048 teffective_1(task_t task
)
2050 #if defined __LP64__
2054 uintptr_t* raw
= (uintptr_t*)(&task
->effective_policy
);
2059 /* dump pending for tracepoint */
2060 uint32_t tpending(task_pend_token_t pend_token
) { return *(uint32_t*)(void*)(pend_token
); }
2063 task_requested_bitfield(task_t task
)
2066 struct task_requested_policy requested
= task
->requested_policy
;
2068 bits
|= (requested
.trp_int_darwinbg
? POLICY_REQ_INT_DARWIN_BG
: 0);
2069 bits
|= (requested
.trp_ext_darwinbg
? POLICY_REQ_EXT_DARWIN_BG
: 0);
2070 bits
|= (requested
.trp_int_iotier
? (((uint64_t)requested
.trp_int_iotier
) << POLICY_REQ_INT_IO_TIER_SHIFT
) : 0);
2071 bits
|= (requested
.trp_ext_iotier
? (((uint64_t)requested
.trp_ext_iotier
) << POLICY_REQ_EXT_IO_TIER_SHIFT
) : 0);
2072 bits
|= (requested
.trp_int_iopassive
? POLICY_REQ_INT_PASSIVE_IO
: 0);
2073 bits
|= (requested
.trp_ext_iopassive
? POLICY_REQ_EXT_PASSIVE_IO
: 0);
2074 bits
|= (requested
.trp_bg_iotier
? (((uint64_t)requested
.trp_bg_iotier
) << POLICY_REQ_BG_IOTIER_SHIFT
) : 0);
2075 bits
|= (requested
.trp_terminated
? POLICY_REQ_TERMINATED
: 0);
2077 bits
|= (requested
.trp_boosted
? POLICY_REQ_BOOSTED
: 0);
2078 bits
|= (requested
.trp_tal_enabled
? POLICY_REQ_TAL_ENABLED
: 0);
2079 bits
|= (requested
.trp_apptype
? (((uint64_t)requested
.trp_apptype
) << POLICY_REQ_APPTYPE_SHIFT
) : 0);
2080 bits
|= (requested
.trp_role
? (((uint64_t)requested
.trp_role
) << POLICY_REQ_ROLE_SHIFT
) : 0);
2082 bits
|= (requested
.trp_sup_active
? POLICY_REQ_SUP_ACTIVE
: 0);
2083 bits
|= (requested
.trp_sup_lowpri_cpu
? POLICY_REQ_SUP_LOWPRI_CPU
: 0);
2084 bits
|= (requested
.trp_sup_cpu
? POLICY_REQ_SUP_CPU
: 0);
2085 bits
|= (requested
.trp_sup_timer
? (((uint64_t)requested
.trp_sup_timer
) << POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT
) : 0);
2086 bits
|= (requested
.trp_sup_throughput
? (((uint64_t)requested
.trp_sup_throughput
) << POLICY_REQ_SUP_THROUGHPUT_SHIFT
) : 0);
2087 bits
|= (requested
.trp_sup_disk
? POLICY_REQ_SUP_DISK_THROTTLE
: 0);
2088 bits
|= (requested
.trp_sup_bg_sockets
? POLICY_REQ_SUP_BG_SOCKETS
: 0);
2090 bits
|= (requested
.trp_base_latency_qos
? (((uint64_t)requested
.trp_base_latency_qos
) << POLICY_REQ_BASE_LATENCY_QOS_SHIFT
) : 0);
2091 bits
|= (requested
.trp_over_latency_qos
? (((uint64_t)requested
.trp_over_latency_qos
) << POLICY_REQ_OVER_LATENCY_QOS_SHIFT
) : 0);
2092 bits
|= (requested
.trp_base_through_qos
? (((uint64_t)requested
.trp_base_through_qos
) << POLICY_REQ_BASE_THROUGH_QOS_SHIFT
) : 0);
2093 bits
|= (requested
.trp_over_through_qos
? (((uint64_t)requested
.trp_over_through_qos
) << POLICY_REQ_OVER_THROUGH_QOS_SHIFT
) : 0);
2094 bits
|= (requested
.trp_sfi_managed
? POLICY_REQ_SFI_MANAGED
: 0);
2095 bits
|= (requested
.trp_qos_clamp
? (((uint64_t)requested
.trp_qos_clamp
) << POLICY_REQ_QOS_CLAMP_SHIFT
) : 0);
2101 task_effective_bitfield(task_t task
)
2104 struct task_effective_policy effective
= task
->effective_policy
;
2106 bits
|= (effective
.tep_io_tier
? (((uint64_t)effective
.tep_io_tier
) << POLICY_EFF_IO_TIER_SHIFT
) : 0);
2107 bits
|= (effective
.tep_io_passive
? POLICY_EFF_IO_PASSIVE
: 0);
2108 bits
|= (effective
.tep_darwinbg
? POLICY_EFF_DARWIN_BG
: 0);
2109 bits
|= (effective
.tep_lowpri_cpu
? POLICY_EFF_LOWPRI_CPU
: 0);
2110 bits
|= (effective
.tep_terminated
? POLICY_EFF_TERMINATED
: 0);
2111 bits
|= (effective
.tep_all_sockets_bg
? POLICY_EFF_ALL_SOCKETS_BG
: 0);
2112 bits
|= (effective
.tep_new_sockets_bg
? POLICY_EFF_NEW_SOCKETS_BG
: 0);
2113 bits
|= (effective
.tep_bg_iotier
? (((uint64_t)effective
.tep_bg_iotier
) << POLICY_EFF_BG_IOTIER_SHIFT
) : 0);
2114 bits
|= (effective
.tep_qos_ui_is_urgent
? POLICY_EFF_QOS_UI_IS_URGENT
: 0);
2116 bits
|= (effective
.tep_tal_engaged
? POLICY_EFF_TAL_ENGAGED
: 0);
2117 bits
|= (effective
.tep_watchers_bg
? POLICY_EFF_WATCHERS_BG
: 0);
2118 bits
|= (effective
.tep_sup_active
? POLICY_EFF_SUP_ACTIVE
: 0);
2119 bits
|= (effective
.tep_suppressed_cpu
? POLICY_EFF_SUP_CPU
: 0);
2120 bits
|= (effective
.tep_role
? (((uint64_t)effective
.tep_role
) << POLICY_EFF_ROLE_SHIFT
) : 0);
2121 bits
|= (effective
.tep_latency_qos
? (((uint64_t)effective
.tep_latency_qos
) << POLICY_EFF_LATENCY_QOS_SHIFT
) : 0);
2122 bits
|= (effective
.tep_through_qos
? (((uint64_t)effective
.tep_through_qos
) << POLICY_EFF_THROUGH_QOS_SHIFT
) : 0);
2123 bits
|= (effective
.tep_sfi_managed
? POLICY_EFF_SFI_MANAGED
: 0);
2124 bits
|= (effective
.tep_qos_ceiling
? (((uint64_t)effective
.tep_qos_ceiling
) << POLICY_EFF_QOS_CEILING_SHIFT
) : 0);
2131 * Resource usage and CPU related routines
2135 proc_get_task_ruse_cpu(task_t task
, uint32_t *policyp
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
)
2144 error
= task_get_cpuusage(task
, percentagep
, intervalp
, deadlinep
, &scope
);
2148 * Reverse-map from CPU resource limit scopes back to policies (see comment below).
2150 if (scope
== TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
2151 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
;
2152 } else if (scope
== TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
2153 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE
;
2154 } else if (scope
== TASK_RUSECPU_FLAGS_DEADLINE
) {
2155 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
2162 * Configure the default CPU usage monitor parameters.
2164 * For tasks which have this mechanism activated: if any thread in the
2165 * process consumes more CPU than this, an EXC_RESOURCE exception will be generated.
2168 proc_init_cpumon_params(void)
2171 * The max CPU percentage can be configured via the boot-args and
2172 * a key in the device tree. The boot-args are honored first, then the
2175 if (!PE_parse_boot_argn("max_cpumon_percentage", &proc_max_cpumon_percentage
,
2176 sizeof (proc_max_cpumon_percentage
)))
2178 uint64_t max_percentage
= 0ULL;
2180 if (!PE_get_default("kern.max_cpumon_percentage", &max_percentage
,
2181 sizeof(max_percentage
)))
2183 max_percentage
= DEFAULT_CPUMON_PERCENTAGE
;
2186 assert(max_percentage
<= UINT8_MAX
);
2187 proc_max_cpumon_percentage
= (uint8_t) max_percentage
;
2190 if (proc_max_cpumon_percentage
> 100) {
2191 proc_max_cpumon_percentage
= 100;
2195 * The interval should be specified in seconds.
2197 * Like the max CPU percentage, the max CPU interval can be configured
2198 * via boot-args and the device tree.
2200 if (!PE_parse_boot_argn("max_cpumon_interval", &proc_max_cpumon_interval
,
2201 sizeof (proc_max_cpumon_interval
)))
2203 if (!PE_get_default("kern.max_cpumon_interval", &proc_max_cpumon_interval
,
2204 sizeof(proc_max_cpumon_interval
)))
2206 proc_max_cpumon_interval
= DEFAULT_CPUMON_INTERVAL
;
2210 proc_max_cpumon_interval
*= NSEC_PER_SEC
;
2212 /* TEMPORARY boot arg to control App suppression */
2213 PE_parse_boot_argn("task_policy_suppression_disable",
2214 &task_policy_suppression_disable
,
2215 sizeof(task_policy_suppression_disable
));
2219 * Currently supported configurations for CPU limits.
2221 * Policy | Deadline-based CPU limit | Percentage-based CPU limit
2222 * -------------------------------------+--------------------------+------------------------------
2223 * PROC_POLICY_RSRCACT_THROTTLE | ENOTSUP | Task-wide scope only
2224 * PROC_POLICY_RSRCACT_SUSPEND | Task-wide scope only | ENOTSUP
2225 * PROC_POLICY_RSRCACT_TERMINATE | Task-wide scope only | ENOTSUP
2226 * PROC_POLICY_RSRCACT_NOTIFY_KQ | Task-wide scope only | ENOTSUP
2227 * PROC_POLICY_RSRCACT_NOTIFY_EXC | ENOTSUP | Per-thread scope only
2229 * A deadline-based CPU limit is actually a simple wallclock timer - the requested action is performed
2230 * after the specified amount of wallclock time has elapsed.
2232 * A percentage-based CPU limit performs the requested action after the specified amount of actual CPU time
2233 * has been consumed -- regardless of how much wallclock time has elapsed -- by either the task as an
2234 * aggregate entity (so-called "Task-wide" or "Proc-wide" scope, whereby the CPU time consumed by all threads
2235 * in the task are added together), or by any one thread in the task (so-called "per-thread" scope).
2237 * We support either deadline != 0 OR percentage != 0, but not both. The original intention in having them
2238 * share an API was to use actual CPU time as the basis of the deadline-based limit (as in: perform an action
2239 * after I have used some amount of CPU time; this is different than the recurring percentage/interval model)
2240 * but the potential consumer of the API at the time was insisting on wallclock time instead.
2242 * Currently, requesting notification via an exception is the only way to get per-thread scope for a
2243 * CPU limit. All other types of notifications force task-wide scope for the limit.
2246 proc_set_task_ruse_cpu(task_t task
, uint32_t policy
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
,
2247 int cpumon_entitled
)
2253 * Enforce the matrix of supported configurations for policy, percentage, and deadline.
2256 // If no policy is explicitly given, the default is to throttle.
2257 case TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
:
2258 case TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE
:
2261 scope
= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2263 case TASK_POLICY_RESOURCE_ATTRIBUTE_SUSPEND
:
2264 case TASK_POLICY_RESOURCE_ATTRIBUTE_TERMINATE
:
2265 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_KQ
:
2266 if (percentage
!= 0)
2268 scope
= TASK_RUSECPU_FLAGS_DEADLINE
;
2270 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
:
2273 scope
= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
2274 #ifdef CONFIG_NOMONITORS
2276 #endif /* CONFIG_NOMONITORS */
2283 if (task
!= current_task()) {
2284 task
->policy_ru_cpu_ext
= policy
;
2286 task
->policy_ru_cpu
= policy
;
2288 error
= task_set_cpuusage(task
, percentage
, interval
, deadline
, scope
, cpumon_entitled
);
2293 /* TODO: get rid of these */
2294 #define TASK_POLICY_CPU_RESOURCE_USAGE 0
2295 #define TASK_POLICY_WIREDMEM_RESOURCE_USAGE 1
2296 #define TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE 2
2297 #define TASK_POLICY_DISK_RESOURCE_USAGE 3
2298 #define TASK_POLICY_NETWORK_RESOURCE_USAGE 4
2299 #define TASK_POLICY_POWER_RESOURCE_USAGE 5
2301 #define TASK_POLICY_RESOURCE_USAGE_COUNT 6
2304 proc_clear_task_ruse_cpu(task_t task
, int cpumon_entitled
)
2308 void * bsdinfo
= NULL
;
2311 if (task
!= current_task()) {
2312 task
->policy_ru_cpu_ext
= TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT
;
2314 task
->policy_ru_cpu
= TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT
;
2317 error
= task_clear_cpuusage_locked(task
, cpumon_entitled
);
2321 action
= task
->applied_ru_cpu
;
2322 if (task
->applied_ru_cpu_ext
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2324 task
->applied_ru_cpu_ext
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
2326 if (action
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2327 bsdinfo
= task
->bsd_info
;
2329 proc_restore_resource_actions(bsdinfo
, TASK_POLICY_CPU_RESOURCE_USAGE
, action
);
2340 /* used to apply resource limit related actions */
2342 task_apply_resource_actions(task_t task
, int type
)
2344 int action
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
2345 void * bsdinfo
= NULL
;
2348 case TASK_POLICY_CPU_RESOURCE_USAGE
:
2350 case TASK_POLICY_WIREDMEM_RESOURCE_USAGE
:
2351 case TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE
:
2352 case TASK_POLICY_DISK_RESOURCE_USAGE
:
2353 case TASK_POLICY_NETWORK_RESOURCE_USAGE
:
2354 case TASK_POLICY_POWER_RESOURCE_USAGE
:
2361 /* only cpu actions for now */
2364 if (task
->applied_ru_cpu_ext
== TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2366 task
->applied_ru_cpu_ext
= task
->policy_ru_cpu_ext
;
2367 action
= task
->applied_ru_cpu_ext
;
2369 action
= task
->applied_ru_cpu_ext
;
2372 if (action
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2373 bsdinfo
= task
->bsd_info
;
2375 proc_apply_resource_actions(bsdinfo
, TASK_POLICY_CPU_RESOURCE_USAGE
, action
);
2383 * XXX This API is somewhat broken; we support multiple simultaneous CPU limits, but the get/set API
2384 * only allows for one at a time. This means that if there is a per-thread limit active, the other
2385 * "scopes" will not be accessible via this API. We could change it to pass in the scope of interest
2386 * to the caller, and prefer that, but there's no need for that at the moment.
2389 task_get_cpuusage(task_t task
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
, int *scope
)
2395 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) {
2396 *scope
= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
2397 *percentagep
= task
->rusage_cpu_perthr_percentage
;
2398 *intervalp
= task
->rusage_cpu_perthr_interval
;
2399 } else if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PROC_LIMIT
) != 0) {
2400 *scope
= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2401 *percentagep
= task
->rusage_cpu_percentage
;
2402 *intervalp
= task
->rusage_cpu_interval
;
2403 } else if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_DEADLINE
) != 0) {
2404 *scope
= TASK_RUSECPU_FLAGS_DEADLINE
;
2405 *deadlinep
= task
->rusage_cpu_deadline
;
2414 * Suspend the CPU usage monitor for the task. Return value indicates
2415 * if the mechanism was actually enabled.
2418 task_suspend_cpumon(task_t task
)
2422 task_lock_assert_owned(task
);
2424 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
2425 return KERN_INVALID_ARGUMENT
;
2428 #if CONFIG_TELEMETRY
2430 * Disable task-wide telemetry if it was ever enabled by the CPU usage
2431 * monitor's warning zone.
2433 telemetry_task_ctl_locked(task
, TF_CPUMON_WARNING
, 0);
2437 * Suspend monitoring for the task, and propagate that change to each thread.
2439 task
->rusage_cpu_flags
&= ~(TASK_RUSECPU_FLAGS_PERTHR_LIMIT
| TASK_RUSECPU_FLAGS_FATAL_CPUMON
);
2440 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2441 set_astledger(thread
);
2444 return KERN_SUCCESS
;
2448 * Remove all traces of the CPU monitor.
2451 task_disable_cpumon(task_t task
)
2455 task_lock_assert_owned(task
);
2457 kret
= task_suspend_cpumon(task
);
2458 if (kret
) return kret
;
2460 /* Once we clear these values, the monitor can't be resumed */
2461 task
->rusage_cpu_perthr_percentage
= 0;
2462 task
->rusage_cpu_perthr_interval
= 0;
2464 return (KERN_SUCCESS
);
2469 task_enable_cpumon_locked(task_t task
)
2472 task_lock_assert_owned(task
);
2474 if (task
->rusage_cpu_perthr_percentage
== 0 ||
2475 task
->rusage_cpu_perthr_interval
== 0) {
2476 return KERN_INVALID_ARGUMENT
;
2479 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
2480 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2481 set_astledger(thread
);
2484 return KERN_SUCCESS
;
2488 task_resume_cpumon(task_t task
)
2497 kret
= task_enable_cpumon_locked(task
);
2504 /* duplicate values from bsd/sys/process_policy.h */
2505 #define PROC_POLICY_CPUMON_DISABLE 0xFF
2506 #define PROC_POLICY_CPUMON_DEFAULTS 0xFE
2509 task_set_cpuusage(task_t task
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
, int scope
, int cpumon_entitled
)
2511 uint64_t abstime
= 0;
2512 uint64_t limittime
= 0;
2514 lck_mtx_assert(&task
->lock
, LCK_MTX_ASSERT_OWNED
);
2516 /* By default, refill once per second */
2518 interval
= NSEC_PER_SEC
;
2520 if (percentage
!= 0) {
2521 if (scope
== TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
2522 boolean_t warn
= FALSE
;
2525 * A per-thread CPU limit on a task generates an exception
2526 * (LEDGER_ACTION_EXCEPTION) if any one thread in the task
2527 * exceeds the limit.
2530 if (percentage
== PROC_POLICY_CPUMON_DISABLE
) {
2531 if (cpumon_entitled
) {
2532 /* 25095698 - task_disable_cpumon() should be reliable */
2533 task_disable_cpumon(task
);
2538 * This task wishes to disable the CPU usage monitor, but it's
2539 * missing the required entitlement:
2540 * com.apple.private.kernel.override-cpumon
2542 * Instead, treat this as a request to reset its params
2543 * back to the defaults.
2546 percentage
= PROC_POLICY_CPUMON_DEFAULTS
;
2549 if (percentage
== PROC_POLICY_CPUMON_DEFAULTS
) {
2550 percentage
= proc_max_cpumon_percentage
;
2551 interval
= proc_max_cpumon_interval
;
2554 if (percentage
> 100) {
2559 * Passing in an interval of -1 means either:
2560 * - Leave the interval as-is, if there's already a per-thread
2562 * - Use the system default.
2564 if (interval
== -1ULL) {
2565 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
2566 interval
= task
->rusage_cpu_perthr_interval
;
2568 interval
= proc_max_cpumon_interval
;
2573 * Enforce global caps on CPU usage monitor here if the process is not
2574 * entitled to escape the global caps.
2576 if ((percentage
> proc_max_cpumon_percentage
) && (cpumon_entitled
== 0)) {
2578 percentage
= proc_max_cpumon_percentage
;
2581 if ((interval
> proc_max_cpumon_interval
) && (cpumon_entitled
== 0)) {
2583 interval
= proc_max_cpumon_interval
;
2588 const char *procname
= "unknown";
2591 pid
= proc_selfpid();
2592 if (current_task()->bsd_info
!= NULL
) {
2593 procname
= proc_name_address(current_task()->bsd_info
);
2597 printf("process %s[%d] denied attempt to escape CPU monitor"
2598 " (missing required entitlement).\n", procname
, pid
);
2601 /* configure the limit values */
2602 task
->rusage_cpu_perthr_percentage
= percentage
;
2603 task
->rusage_cpu_perthr_interval
= interval
;
2605 /* and enable the CPU monitor */
2606 (void)task_enable_cpumon_locked(task
);
2607 } else if (scope
== TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
2609 * Currently, a proc-wide CPU limit always blocks if the limit is
2610 * exceeded (LEDGER_ACTION_BLOCK).
2612 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2613 task
->rusage_cpu_percentage
= percentage
;
2614 task
->rusage_cpu_interval
= interval
;
2616 limittime
= (interval
* percentage
) / 100;
2617 nanoseconds_to_absolutetime(limittime
, &abstime
);
2619 ledger_set_limit(task
->ledger
, task_ledgers
.cpu_time
, abstime
, 0);
2620 ledger_set_period(task
->ledger
, task_ledgers
.cpu_time
, interval
);
2621 ledger_set_action(task
->ledger
, task_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
2625 if (deadline
!= 0) {
2626 assert(scope
== TASK_RUSECPU_FLAGS_DEADLINE
);
2628 /* if already in use, cancel and wait for it to cleanout */
2629 if (task
->rusage_cpu_callt
!= NULL
) {
2631 thread_call_cancel_wait(task
->rusage_cpu_callt
);
2634 if (task
->rusage_cpu_callt
== NULL
) {
2635 task
->rusage_cpu_callt
= thread_call_allocate_with_priority(task_action_cpuusage
, (thread_call_param_t
)task
, THREAD_CALL_PRIORITY_KERNEL
);
2638 if (task
->rusage_cpu_callt
!= 0) {
2639 uint64_t save_abstime
= 0;
2641 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_DEADLINE
;
2642 task
->rusage_cpu_deadline
= deadline
;
2644 nanoseconds_to_absolutetime(deadline
, &abstime
);
2645 save_abstime
= abstime
;
2646 clock_absolutetime_interval_to_deadline(save_abstime
, &abstime
);
2647 thread_call_enter_delayed(task
->rusage_cpu_callt
, abstime
);
2655 task_clear_cpuusage(task_t task
, int cpumon_entitled
)
2660 retval
= task_clear_cpuusage_locked(task
, cpumon_entitled
);
2667 task_clear_cpuusage_locked(task_t task
, int cpumon_entitled
)
2669 thread_call_t savecallt
;
2671 /* cancel percentage handling if set */
2672 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
2673 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2674 ledger_set_limit(task
->ledger
, task_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
2675 task
->rusage_cpu_percentage
= 0;
2676 task
->rusage_cpu_interval
= 0;
2680 * Disable the CPU usage monitor.
2682 if (cpumon_entitled
) {
2683 task_disable_cpumon(task
);
2686 /* cancel deadline handling if set */
2687 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_DEADLINE
) {
2688 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_DEADLINE
;
2689 if (task
->rusage_cpu_callt
!= 0) {
2690 savecallt
= task
->rusage_cpu_callt
;
2691 task
->rusage_cpu_callt
= NULL
;
2692 task
->rusage_cpu_deadline
= 0;
2694 thread_call_cancel_wait(savecallt
);
2695 thread_call_free(savecallt
);
2702 /* called by ledger unit to enforce action due to resource usage criteria being met */
2704 task_action_cpuusage(thread_call_param_t param0
, __unused thread_call_param_t param1
)
2706 task_t task
= (task_t
)param0
;
2707 (void)task_apply_resource_actions(task
, TASK_POLICY_CPU_RESOURCE_USAGE
);
2713 * Routines for taskwatch and pidbind
2718 * Routines for importance donation/inheritance/boosting
2722 task_importance_update_live_donor(task_t target_task
)
2724 #if IMPORTANCE_INHERITANCE
2726 ipc_importance_task_t task_imp
;
2728 task_imp
= ipc_importance_for_task(target_task
, FALSE
);
2729 if (IIT_NULL
!= task_imp
) {
2730 ipc_importance_task_update_live_donor(task_imp
);
2731 ipc_importance_task_release(task_imp
);
2733 #endif /* IMPORTANCE_INHERITANCE */
2737 task_importance_mark_donor(task_t task
, boolean_t donating
)
2739 #if IMPORTANCE_INHERITANCE
2740 ipc_importance_task_t task_imp
;
2742 task_imp
= ipc_importance_for_task(task
, FALSE
);
2743 if (IIT_NULL
!= task_imp
) {
2744 ipc_importance_task_mark_donor(task_imp
, donating
);
2745 ipc_importance_task_release(task_imp
);
2747 #endif /* IMPORTANCE_INHERITANCE */
2751 task_importance_mark_live_donor(task_t task
, boolean_t live_donating
)
2753 #if IMPORTANCE_INHERITANCE
2754 ipc_importance_task_t task_imp
;
2756 task_imp
= ipc_importance_for_task(task
, FALSE
);
2757 if (IIT_NULL
!= task_imp
) {
2758 ipc_importance_task_mark_live_donor(task_imp
, live_donating
);
2759 ipc_importance_task_release(task_imp
);
2761 #endif /* IMPORTANCE_INHERITANCE */
2765 task_importance_mark_receiver(task_t task
, boolean_t receiving
)
2767 #if IMPORTANCE_INHERITANCE
2768 ipc_importance_task_t task_imp
;
2770 task_imp
= ipc_importance_for_task(task
, FALSE
);
2771 if (IIT_NULL
!= task_imp
) {
2772 ipc_importance_task_mark_receiver(task_imp
, receiving
);
2773 ipc_importance_task_release(task_imp
);
2775 #endif /* IMPORTANCE_INHERITANCE */
2779 task_importance_mark_denap_receiver(task_t task
, boolean_t denap
)
2781 #if IMPORTANCE_INHERITANCE
2782 ipc_importance_task_t task_imp
;
2784 task_imp
= ipc_importance_for_task(task
, FALSE
);
2785 if (IIT_NULL
!= task_imp
) {
2786 ipc_importance_task_mark_denap_receiver(task_imp
, denap
);
2787 ipc_importance_task_release(task_imp
);
2789 #endif /* IMPORTANCE_INHERITANCE */
2793 task_importance_reset(__imp_only task_t task
)
2795 #if IMPORTANCE_INHERITANCE
2796 ipc_importance_task_t task_imp
;
2798 /* TODO: Lower importance downstream before disconnect */
2799 task_imp
= task
->task_imp_base
;
2800 ipc_importance_reset(task_imp
, FALSE
);
2801 task_importance_update_live_donor(task
);
2802 #endif /* IMPORTANCE_INHERITANCE */
2805 #if IMPORTANCE_INHERITANCE
2808 * Sets the task boost bit to the provided value. Does NOT run the update function.
2810 * Task lock must be held.
2813 task_set_boost_locked(task_t task
, boolean_t boost_active
)
2815 #if IMPORTANCE_DEBUG
2816 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_BOOST
, (boost_active
? IMP_BOOSTED
: IMP_UNBOOSTED
)) | DBG_FUNC_START
),
2817 proc_selfpid(), task_pid(task
), trequested_0(task
), trequested_1(task
), 0);
2820 task
->requested_policy
.trp_boosted
= boost_active
;
2822 #if IMPORTANCE_DEBUG
2823 if (boost_active
== TRUE
){
2824 DTRACE_BOOST2(boost
, task_t
, task
, int, task_pid(task
));
2826 DTRACE_BOOST2(unboost
, task_t
, task
, int, task_pid(task
));
2828 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_BOOST
, (boost_active
? IMP_BOOSTED
: IMP_UNBOOSTED
)) | DBG_FUNC_END
),
2829 proc_selfpid(), task_pid(task
),
2830 trequested_0(task
), trequested_1(task
), 0);
2835 * Sets the task boost bit to the provided value and applies the update.
2837 * Task lock must be held. Must call update complete after unlocking the task.
2840 task_update_boost_locked(task_t task
, boolean_t boost_active
, task_pend_token_t pend_token
)
2842 task_set_boost_locked(task
, boost_active
);
2844 task_policy_update_locked(task
, pend_token
);
2848 * Check if this task should donate importance.
2850 * May be called without taking the task lock. In that case, donor status can change
2851 * so you must check only once for each donation event.
2854 task_is_importance_donor(task_t task
)
2856 if (task
->task_imp_base
== IIT_NULL
)
2858 return ipc_importance_task_is_donor(task
->task_imp_base
);
2862 * Query the status of the task's donor mark.
2865 task_is_marked_importance_donor(task_t task
)
2867 if (task
->task_imp_base
== IIT_NULL
)
2869 return ipc_importance_task_is_marked_donor(task
->task_imp_base
);
2873 * Query the status of the task's live donor and donor mark.
2876 task_is_marked_live_importance_donor(task_t task
)
2878 if (task
->task_imp_base
== IIT_NULL
)
2880 return ipc_importance_task_is_marked_live_donor(task
->task_imp_base
);
2885 * This routine may be called without holding task lock
2886 * since the value of imp_receiver can never be unset.
2889 task_is_importance_receiver(task_t task
)
2891 if (task
->task_imp_base
== IIT_NULL
)
2893 return ipc_importance_task_is_marked_receiver(task
->task_imp_base
);
2897 * Query the task's receiver mark.
2900 task_is_marked_importance_receiver(task_t task
)
2902 if (task
->task_imp_base
== IIT_NULL
)
2904 return ipc_importance_task_is_marked_receiver(task
->task_imp_base
);
2908 * This routine may be called without holding task lock
2909 * since the value of de-nap receiver can never be unset.
2912 task_is_importance_denap_receiver(task_t task
)
2914 if (task
->task_imp_base
== IIT_NULL
)
2916 return ipc_importance_task_is_denap_receiver(task
->task_imp_base
);
2920 * Query the task's de-nap receiver mark.
2923 task_is_marked_importance_denap_receiver(task_t task
)
2925 if (task
->task_imp_base
== IIT_NULL
)
2927 return ipc_importance_task_is_marked_denap_receiver(task
->task_imp_base
);
2931 * This routine may be called without holding task lock
2932 * since the value of imp_receiver can never be unset.
2935 task_is_importance_receiver_type(task_t task
)
2937 if (task
->task_imp_base
== IIT_NULL
)
2939 return (task_is_importance_receiver(task
) ||
2940 task_is_importance_denap_receiver(task
));
2944 * External importance assertions are managed by the process in userspace
2945 * Internal importance assertions are the responsibility of the kernel
2946 * Assertions are changed from internal to external via task_importance_externalize_assertion
2950 task_importance_hold_internal_assertion(task_t target_task
, uint32_t count
)
2952 ipc_importance_task_t task_imp
;
2955 /* may be first time, so allow for possible importance setup */
2956 task_imp
= ipc_importance_for_task(target_task
, FALSE
);
2957 if (IIT_NULL
== task_imp
) {
2960 ret
= ipc_importance_task_hold_internal_assertion(task_imp
, count
);
2961 ipc_importance_task_release(task_imp
);
2963 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
2967 task_importance_hold_file_lock_assertion(task_t target_task
, uint32_t count
)
2969 ipc_importance_task_t task_imp
;
2972 /* may be first time, so allow for possible importance setup */
2973 task_imp
= ipc_importance_for_task(target_task
, FALSE
);
2974 if (IIT_NULL
== task_imp
) {
2977 ret
= ipc_importance_task_hold_file_lock_assertion(task_imp
, count
);
2978 ipc_importance_task_release(task_imp
);
2980 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
2984 task_importance_hold_legacy_external_assertion(task_t target_task
, uint32_t count
)
2986 ipc_importance_task_t task_imp
;
2989 /* must already have set up an importance */
2990 task_imp
= target_task
->task_imp_base
;
2991 if (IIT_NULL
== task_imp
) {
2994 ret
= ipc_importance_task_hold_legacy_external_assertion(task_imp
, count
);
2995 return (KERN_SUCCESS
!= ret
) ? ENOTSUP
: 0;
2999 task_importance_drop_file_lock_assertion(task_t target_task
, uint32_t count
)
3001 ipc_importance_task_t task_imp
;
3004 /* must already have set up an importance */
3005 task_imp
= target_task
->task_imp_base
;
3006 if (IIT_NULL
== task_imp
) {
3009 ret
= ipc_importance_task_drop_file_lock_assertion(target_task
->task_imp_base
, count
);
3010 return (KERN_SUCCESS
!= ret
) ? EOVERFLOW
: 0;
3014 task_importance_drop_legacy_external_assertion(task_t target_task
, uint32_t count
)
3016 ipc_importance_task_t task_imp
;
3019 /* must already have set up an importance */
3020 task_imp
= target_task
->task_imp_base
;
3021 if (IIT_NULL
== task_imp
) {
3024 ret
= ipc_importance_task_drop_legacy_external_assertion(task_imp
, count
);
3025 return (KERN_SUCCESS
!= ret
) ? EOVERFLOW
: 0;
3029 task_add_importance_watchport(task_t task
, mach_port_t port
, int *boostp
)
3033 __impdebug_only
int released_pid
= 0;
3034 __impdebug_only
int pid
= task_pid(task
);
3036 ipc_importance_task_t release_imp_task
= IIT_NULL
;
3038 if (IP_VALID(port
) != 0) {
3039 ipc_importance_task_t new_imp_task
= ipc_importance_for_task(task
, FALSE
);
3044 * The port must have been marked tempowner already.
3045 * This also filters out ports whose receive rights
3046 * are already enqueued in a message, as you can't
3047 * change the right's destination once it's already
3050 if (port
->ip_tempowner
!= 0) {
3051 assert(port
->ip_impdonation
!= 0);
3053 boost
= port
->ip_impcount
;
3054 if (IIT_NULL
!= port
->ip_imp_task
) {
3056 * if this port is already bound to a task,
3057 * release the task reference and drop any
3058 * watchport-forwarded boosts
3060 release_imp_task
= port
->ip_imp_task
;
3061 port
->ip_imp_task
= IIT_NULL
;
3064 /* mark the port is watching another task (reference held in port->ip_imp_task) */
3065 if (ipc_importance_task_is_marked_receiver(new_imp_task
)) {
3066 port
->ip_imp_task
= new_imp_task
;
3067 new_imp_task
= IIT_NULL
;
3072 if (IIT_NULL
!= new_imp_task
) {
3073 ipc_importance_task_release(new_imp_task
);
3076 if (IIT_NULL
!= release_imp_task
) {
3078 ipc_importance_task_drop_internal_assertion(release_imp_task
, boost
);
3080 // released_pid = task_pid(release_imp_task); /* TODO: Need ref-safe way to get pid */
3081 ipc_importance_task_release(release_imp_task
);
3083 #if IMPORTANCE_DEBUG
3084 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_WATCHPORT
, 0)) | DBG_FUNC_NONE
,
3085 proc_selfpid(), pid
, boost
, released_pid
, 0);
3086 #endif /* IMPORTANCE_DEBUG */
3093 #endif /* IMPORTANCE_INHERITANCE */
3096 * Routines for VM to query task importance
3101 * Order to be considered while estimating importance
3102 * for low memory notification and purging purgeable memory.
3104 #define TASK_IMPORTANCE_FOREGROUND 4
3105 #define TASK_IMPORTANCE_NOTDARWINBG 1
3109 * (Un)Mark the task as a privileged listener for memory notifications.
3110 * if marked, this task will be among the first to be notified amongst
3111 * the bulk of all other tasks when the system enters a pressure level
3112 * of interest to this task.
3115 task_low_mem_privileged_listener(task_t task
, boolean_t new_value
, boolean_t
*old_value
)
3117 if (old_value
!= NULL
) {
3118 *old_value
= (boolean_t
)task
->low_mem_privileged_listener
;
3121 task
->low_mem_privileged_listener
= (uint32_t)new_value
;
3129 * Checks if the task is already notified.
3131 * Condition: task lock should be held while calling this function.
3134 task_has_been_notified(task_t task
, int pressurelevel
)
3140 if (pressurelevel
== kVMPressureWarning
)
3141 return (task
->low_mem_notified_warn
? TRUE
: FALSE
);
3142 else if (pressurelevel
== kVMPressureCritical
)
3143 return (task
->low_mem_notified_critical
? TRUE
: FALSE
);
3150 * Checks if the task is used for purging.
3152 * Condition: task lock should be held while calling this function.
3155 task_used_for_purging(task_t task
, int pressurelevel
)
3161 if (pressurelevel
== kVMPressureWarning
)
3162 return (task
->purged_memory_warn
? TRUE
: FALSE
);
3163 else if (pressurelevel
== kVMPressureCritical
)
3164 return (task
->purged_memory_critical
? TRUE
: FALSE
);
3171 * Mark the task as notified with memory notification.
3173 * Condition: task lock should be held while calling this function.
3176 task_mark_has_been_notified(task_t task
, int pressurelevel
)
3182 if (pressurelevel
== kVMPressureWarning
)
3183 task
->low_mem_notified_warn
= 1;
3184 else if (pressurelevel
== kVMPressureCritical
)
3185 task
->low_mem_notified_critical
= 1;
3190 * Mark the task as purged.
3192 * Condition: task lock should be held while calling this function.
3195 task_mark_used_for_purging(task_t task
, int pressurelevel
)
3201 if (pressurelevel
== kVMPressureWarning
)
3202 task
->purged_memory_warn
= 1;
3203 else if (pressurelevel
== kVMPressureCritical
)
3204 task
->purged_memory_critical
= 1;
3209 * Mark the task eligible for low memory notification.
3211 * Condition: task lock should be held while calling this function.
3214 task_clear_has_been_notified(task_t task
, int pressurelevel
)
3220 if (pressurelevel
== kVMPressureWarning
)
3221 task
->low_mem_notified_warn
= 0;
3222 else if (pressurelevel
== kVMPressureCritical
)
3223 task
->low_mem_notified_critical
= 0;
3228 * Mark the task eligible for purging its purgeable memory.
3230 * Condition: task lock should be held while calling this function.
3233 task_clear_used_for_purging(task_t task
)
3239 task
->purged_memory_warn
= 0;
3240 task
->purged_memory_critical
= 0;
3245 * Estimate task importance for purging its purgeable memory
3246 * and low memory notification.
3248 * Importance is calculated in the following order of criteria:
3249 * -Task role : Background vs Foreground
3250 * -Boost status: Not boosted vs Boosted
3251 * -Darwin BG status.
3253 * Returns: Estimated task importance. Less important task will have lower
3254 * estimated importance.
3257 task_importance_estimate(task_t task
)
3259 int task_importance
= 0;
3265 if (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
) == TASK_FOREGROUND_APPLICATION
)
3266 task_importance
+= TASK_IMPORTANCE_FOREGROUND
;
3268 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
) == 0)
3269 task_importance
+= TASK_IMPORTANCE_NOTDARWINBG
;
3271 return task_importance
;
3275 task_has_assertions(task_t task
)
3277 return (task
->task_imp_base
->iit_assertcnt
? TRUE
: FALSE
);
3282 send_resource_violation(typeof(send_cpu_usage_violation
) sendfunc
,
3284 struct ledger_entry_info
*linfo
,
3285 resource_notify_flags_t flags
)
3288 return KERN_NOT_SUPPORTED
;
3290 kern_return_t kr
= KERN_SUCCESS
;
3292 posix_path_t proc_path
= "";
3293 proc_name_t procname
= "<unknown>";
3297 mach_timespec_t timestamp
;
3298 thread_t curthread
= current_thread();
3299 ipc_port_t dstport
= MACH_PORT_NULL
;
3302 kr
= KERN_INVALID_ARGUMENT
; goto finish
;
3305 /* extract violator information */
3306 task_lock(violator
);
3307 if (!(proc
= get_bsdtask_info(violator
))) {
3308 task_unlock(violator
);
3309 kr
= KERN_INVALID_ARGUMENT
; goto finish
;
3311 (void)mig_strncpy(procname
, proc_best_name(proc
), sizeof(procname
));
3312 pid
= task_pid(violator
);
3313 if (flags
& kRNFatalLimitFlag
) {
3314 kr
= proc_pidpathinfo_internal(proc
, 0, proc_path
,
3315 sizeof(proc_path
), NULL
);
3317 task_unlock(violator
);
3318 if (kr
) goto finish
;
3320 /* violation time ~ now */
3321 clock_get_calendar_nanotime(&secs
, &nsecs
);
3322 timestamp
.tv_sec
= (int32_t)secs
;
3323 timestamp
.tv_nsec
= (int32_t)nsecs
;
3324 /* 25567702 tracks widening mach_timespec_t */
3327 kr
= host_get_special_port(host_priv_self(), HOST_LOCAL_NODE
,
3328 HOST_RESOURCE_NOTIFY_PORT
, &dstport
);
3329 if (kr
) goto finish
;
3331 /* TH_OPT_HONOR_QLIMIT causes ipc_kmsg_send() to respect the
3332 * queue limit. It also unsets this flag, but this code also
3333 * unsets it for clarity and in case that code changes. */
3334 curthread
->options
|= TH_OPT_HONOR_QLIMIT
;
3335 kr
= sendfunc(dstport
,
3336 procname
, pid
, proc_path
, timestamp
,
3337 linfo
->lei_balance
, linfo
->lei_last_refill
,
3338 linfo
->lei_limit
, linfo
->lei_refill_period
,
3340 curthread
->options
&= (~TH_OPT_HONOR_QLIMIT
);
3342 ipc_port_release_send(dstport
);
3346 #endif /* MACH_BSD */
3351 * Resource violations trace four 64-bit integers. For K32, two additional
3352 * codes are allocated, the first with the low nibble doubled. So if the K64
3353 * code is 0x042, the K32 codes would be 0x044 and 0x45.
3357 trace_resource_violation(uint16_t code
,
3358 struct ledger_entry_info
*linfo
)
3360 KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH
, DBG_MACH_RESOURCE
, code
),
3361 linfo
->lei_balance
, linfo
->lei_last_refill
,
3362 linfo
->lei_limit
, linfo
->lei_refill_period
);
3365 /* TODO: create/find a trace_two_LLs() for K32 systems */
3366 #define MASK32 0xffffffff
3368 trace_resource_violation(uint16_t code
,
3369 struct ledger_entry_info
*linfo
)
3371 int8_t lownibble
= (code
& 0x3) * 2;
3372 int16_t codeA
= (code
& 0xffc) | lownibble
;
3373 int16_t codeB
= codeA
+ 1;
3375 int32_t balance_high
= (linfo
->lei_balance
>> 32) & MASK32
;
3376 int32_t balance_low
= linfo
->lei_balance
& MASK32
;
3377 int32_t last_refill_high
= (linfo
->lei_last_refill
>> 32) & MASK32
;
3378 int32_t last_refill_low
= linfo
->lei_last_refill
& MASK32
;
3380 int32_t limit_high
= (linfo
->lei_limit
>> 32) & MASK32
;
3381 int32_t limit_low
= linfo
->lei_limit
& MASK32
;
3382 int32_t refill_period_high
= (linfo
->lei_refill_period
>> 32) & MASK32
;
3383 int32_t refill_period_low
= linfo
->lei_refill_period
& MASK32
;
3385 KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH
, DBG_MACH_RESOURCE
, codeA
),
3386 balance_high
, balance_low
,
3387 last_refill_high
, last_refill_low
);
3388 KERNEL_DBG_IST_SANE(KDBG_CODE(DBG_MACH
, DBG_MACH_RESOURCE
, codeB
),
3389 limit_high
, limit_low
,
3390 refill_period_high
, refill_period_low
);
3392 #endif /* K64/K32 */