2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
30 #include <mach/task_server.h>
32 #include <kern/sched.h>
33 #include <kern/task.h>
34 #include <mach/thread_policy.h>
35 #include <sys/errno.h>
36 #include <sys/resource.h>
37 #include <machine/limits.h>
38 #include <kern/ledger.h>
39 #include <kern/thread_call.h>
41 #include <kern/telemetry.h>
45 #include <mach/machine/sdt.h>
46 #endif /* IMPORTANCE_DEBUG */
48 #include <sys/kdebug.h>
53 * This subsystem manages task and thread IO priority and backgrounding,
54 * as well as importance inheritance, process suppression, task QoS, and apptype.
55 * These properties have a suprising number of complex interactions, so they are
56 * centralized here in one state machine to simplify the implementation of those interactions.
59 * Threads and tasks have three policy fields: requested, effective, and pending.
60 * Requested represents the wishes of each interface that influences task policy.
61 * Effective represents the distillation of that policy into a set of behaviors.
62 * Pending represents updates that haven't been applied yet.
64 * Each interface that has an input into the task policy state machine controls a field in requested.
65 * If the interface has a getter, it returns what is in the field in requested, but that is
66 * not necessarily what is actually in effect.
68 * All kernel subsystems that behave differently based on task policy call into
69 * the get_effective_policy function, which returns the decision of the task policy state machine
70 * for that subsystem by querying only the 'effective' field.
72 * Policy change operations:
73 * Here are the steps to change a policy on a task or thread:
75 * 2) Change requested field for the relevant policy
76 * 3) Run a task policy update, which recalculates effective based on requested,
77 * then takes a diff between the old and new versions of requested and calls the relevant
78 * other subsystems to apply these changes, and updates the pending field.
80 * 5) Run task policy update complete, which looks at the pending field to update
81 * subsystems which cannot be touched while holding the task lock.
83 * To add a new requested policy, add the field in the requested struct, the flavor in task.h,
84 * the setter and getter in proc_(set|get)_task_policy*, and dump the state in task_requested_bitfield,
85 * then set up the effects of that behavior in task_policy_update*.
87 * Most policies are set via proc_set_task_policy, but policies that don't fit that interface
88 * roll their own lock/set/update/unlock/complete code inside this file.
93 * These are a set of behaviors that can be requested for a task. They currently have specific
94 * implied actions when they're enabled, but they may be made customizable in the future.
96 * When the affected task is boosted, we temporarily disable the suppression behaviors
97 * so that the affected process has a chance to run so it can call the API to permanently
98 * disable the suppression behaviors.
102 * Changing task policy on a task or thread takes the task lock, and not the thread lock.
103 * TODO: Should changing policy on a thread take the thread lock instead?
105 * Querying the effective policy does not take the task lock, to prevent deadlocks or slowdown in sensitive code.
106 * This means that any notification of state change needs to be externally synchronized.
110 /* for task holds without dropping the lock */
111 extern void task_hold_locked(task_t task
);
112 extern void task_release_locked(task_t task
);
113 extern void task_wait_locked(task_t task
, boolean_t until_not_runnable
);
115 /* Task policy related helper functions */
116 static void proc_set_task_policy_locked(task_t task
, thread_t thread
, int category
, int flavor
, int value
);
118 static void task_policy_update_locked(task_t task
, thread_t thread
);
119 static void task_policy_update_internal_locked(task_t task
, thread_t thread
, boolean_t in_create
);
120 static void task_policy_update_task_locked(task_t task
, boolean_t update_throttle
, boolean_t update_bg_throttle
);
121 static void task_policy_update_thread_locked(thread_t thread
, int update_cpu
, boolean_t update_throttle
);
123 static void task_policy_update_complete_unlocked(task_t task
, thread_t thread
);
125 static int proc_get_effective_policy(task_t task
, thread_t thread
, int policy
);
127 static void proc_iopol_to_tier(int iopolicy
, int *tier
, int *passive
);
128 static int proc_tier_to_iopol(int tier
, int passive
);
130 static uintptr_t trequested(task_t task
, thread_t thread
);
131 static uintptr_t teffective(task_t task
, thread_t thread
);
132 static uintptr_t tpending(task_t task
, thread_t thread
);
133 static uint64_t task_requested_bitfield(task_t task
, thread_t thread
);
134 static uint64_t task_effective_bitfield(task_t task
, thread_t thread
);
135 static uint64_t task_pending_bitfield(task_t task
, thread_t thread
);
137 void proc_get_thread_policy(thread_t thread
, thread_policy_state_t info
);
139 /* CPU Limits related helper functions */
140 static int task_get_cpuusage(task_t task
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
, int *scope
);
141 int task_set_cpuusage(task_t task
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
, int scope
, int entitled
);
142 static int task_clear_cpuusage_locked(task_t task
, int cpumon_entitled
);
143 int task_disable_cpumon(task_t task
);
144 static int task_apply_resource_actions(task_t task
, int type
);
145 void task_action_cpuusage(thread_call_param_t param0
, thread_call_param_t param1
);
146 void proc_init_cpumon_params(void);
149 int proc_pid(void *proc
);
150 extern int proc_selfpid(void);
151 extern char * proc_name_address(void *p
);
152 extern void rethrottle_thread(void * uthread
);
153 extern void proc_apply_task_networkbg(void * bsd_info
, thread_t thread
, int bg
);
154 #endif /* MACH_BSD */
157 /* Importance Inheritance related helper functions */
159 void task_importance_mark_receiver(task_t task
, boolean_t receiving
);
161 #if IMPORTANCE_INHERITANCE
162 static void task_update_boost_locked(task_t task
, boolean_t boost_active
);
164 static int task_importance_hold_assertion_locked(task_t target_task
, int external
, uint32_t count
);
165 static int task_importance_drop_assertion_locked(task_t target_task
, int external
, uint32_t count
);
166 #endif /* IMPORTANCE_INHERITANCE */
169 #define __impdebug_only
171 #define __impdebug_only __unused
174 #if IMPORTANCE_INHERITANCE
177 #define __imp_only __unused
180 #define TASK_LOCKED 1
181 #define TASK_UNLOCKED 0
183 #define DO_LOWPRI_CPU 1
184 #define UNDO_LOWPRI_CPU 2
186 /* Macros for making tracing simpler */
188 #define tpriority(task, thread) ((uintptr_t)(thread == THREAD_NULL ? (task->priority) : (thread->priority)))
189 #define tisthread(thread) (thread == THREAD_NULL ? TASK_POLICY_TASK : TASK_POLICY_THREAD)
190 #define targetid(task, thread) ((uintptr_t)(thread == THREAD_NULL ? (audit_token_pid_from_task(task)) : (thread->thread_id)))
193 * Default parameters for certain policies
196 int proc_standard_daemon_tier
= THROTTLE_LEVEL_TIER1
;
197 int proc_suppressed_disk_tier
= THROTTLE_LEVEL_TIER1
;
198 int proc_tal_disk_tier
= THROTTLE_LEVEL_TIER1
;
200 int proc_graphics_timer_qos
= (LATENCY_QOS_TIER_0
& 0xFF);
202 const int proc_default_bg_iotier
= THROTTLE_LEVEL_TIER2
;
205 const struct task_requested_policy default_task_requested_policy
= {
206 .bg_iotier
= proc_default_bg_iotier
208 const struct task_effective_policy default_task_effective_policy
= {};
209 const struct task_pended_policy default_task_pended_policy
= {};
212 * Default parameters for CPU usage monitor.
214 * Default setting is 50% over 3 minutes.
216 #define DEFAULT_CPUMON_PERCENTAGE 50
217 #define DEFAULT_CPUMON_INTERVAL (3 * 60)
219 uint8_t proc_max_cpumon_percentage
;
220 uint64_t proc_max_cpumon_interval
;
223 task_qos_policy_validate(task_qos_policy_t qosinfo
, mach_msg_type_number_t count
) {
224 if (count
< TASK_QOS_POLICY_COUNT
)
225 return KERN_INVALID_ARGUMENT
;
227 task_latency_qos_t ltier
= qosinfo
->task_latency_qos_tier
;
228 task_throughput_qos_t ttier
= qosinfo
->task_throughput_qos_tier
;
230 if ((ltier
!= LATENCY_QOS_TIER_UNSPECIFIED
) &&
231 ((ltier
> LATENCY_QOS_TIER_5
) || (ltier
< LATENCY_QOS_TIER_0
)))
232 return KERN_INVALID_ARGUMENT
;
234 if ((ttier
!= THROUGHPUT_QOS_TIER_UNSPECIFIED
) &&
235 ((ttier
> THROUGHPUT_QOS_TIER_5
) || (ttier
< THROUGHPUT_QOS_TIER_0
)))
236 return KERN_INVALID_ARGUMENT
;
242 task_qos_extract(uint32_t qv
) {
247 task_qos_latency_package(uint32_t qv
) {
248 return (qv
== LATENCY_QOS_TIER_UNSPECIFIED
) ? LATENCY_QOS_TIER_UNSPECIFIED
: ((0xFF << 16) | qv
);
252 task_qos_throughput_package(uint32_t qv
) {
253 return (qv
== THROUGHPUT_QOS_TIER_UNSPECIFIED
) ? THROUGHPUT_QOS_TIER_UNSPECIFIED
: ((0xFE << 16) | qv
);
259 task_policy_flavor_t flavor
,
260 task_policy_t policy_info
,
261 mach_msg_type_number_t count
)
263 kern_return_t result
= KERN_SUCCESS
;
265 if (task
== TASK_NULL
|| task
== kernel_task
)
266 return (KERN_INVALID_ARGUMENT
);
270 case TASK_CATEGORY_POLICY
: {
271 task_category_policy_t info
= (task_category_policy_t
)policy_info
;
273 if (count
< TASK_CATEGORY_POLICY_COUNT
)
274 return (KERN_INVALID_ARGUMENT
);
278 case TASK_FOREGROUND_APPLICATION
:
279 case TASK_BACKGROUND_APPLICATION
:
280 case TASK_DEFAULT_APPLICATION
:
281 proc_set_task_policy(task
, THREAD_NULL
,
282 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
286 case TASK_CONTROL_APPLICATION
:
287 if (task
!= current_task() || task
->sec_token
.val
[0] != 0)
288 result
= KERN_INVALID_ARGUMENT
;
290 proc_set_task_policy(task
, THREAD_NULL
,
291 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
295 case TASK_GRAPHICS_SERVER
:
296 /* TODO: Restrict this role to FCFS <rdar://problem/12552788> */
297 if (task
!= current_task() || task
->sec_token
.val
[0] != 0)
298 result
= KERN_INVALID_ARGUMENT
;
300 proc_set_task_policy(task
, THREAD_NULL
,
301 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
,
305 result
= KERN_INVALID_ARGUMENT
;
307 } /* switch (info->role) */
312 /* Desired energy-efficiency/performance "quality-of-service" */
313 case TASK_BASE_QOS_POLICY
:
315 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
316 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
318 if (kr
!= KERN_SUCCESS
)
323 /* This uses the latency QoS tracepoint, even though we might be changing both */
324 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
325 (IMPORTANCE_CODE(TASK_POLICY_LATENCY_QOS
, (TASK_POLICY_ATTRIBUTE
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
326 proc_selfpid(), targetid(task
, THREAD_NULL
), trequested(task
, THREAD_NULL
), 0, 0);
328 task
->requested_policy
.t_base_latency_qos
= task_qos_extract(qosinfo
->task_latency_qos_tier
);
329 task
->requested_policy
.t_base_through_qos
= task_qos_extract(qosinfo
->task_throughput_qos_tier
);
331 task_policy_update_locked(task
, THREAD_NULL
);
335 task_policy_update_complete_unlocked(task
, THREAD_NULL
);
337 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
338 (IMPORTANCE_CODE(TASK_POLICY_LATENCY_QOS
, (TASK_POLICY_ATTRIBUTE
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
339 proc_selfpid(), targetid(task
, THREAD_NULL
), trequested(task
, THREAD_NULL
), 0, 0);
343 case TASK_OVERRIDE_QOS_POLICY
:
345 task_qos_policy_t qosinfo
= (task_qos_policy_t
)policy_info
;
346 kern_return_t kr
= task_qos_policy_validate(qosinfo
, count
);
348 if (kr
!= KERN_SUCCESS
)
353 /* This uses the latency QoS tracepoint, even though we might be changing both */
354 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
355 (IMPORTANCE_CODE(TASK_POLICY_LATENCY_QOS
, (TASK_POLICY_ATTRIBUTE
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
356 proc_selfpid(), targetid(task
, THREAD_NULL
), trequested(task
, THREAD_NULL
), 0, 0);
358 task
->requested_policy
.t_over_latency_qos
= task_qos_extract(qosinfo
->task_latency_qos_tier
);
359 task
->requested_policy
.t_over_through_qos
= task_qos_extract(qosinfo
->task_throughput_qos_tier
);
361 task_policy_update_locked(task
, THREAD_NULL
);
365 task_policy_update_complete_unlocked(task
, THREAD_NULL
);
367 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
368 (IMPORTANCE_CODE(TASK_POLICY_LATENCY_QOS
, (TASK_POLICY_ATTRIBUTE
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
369 proc_selfpid(), targetid(task
, THREAD_NULL
), trequested(task
, THREAD_NULL
), 0, 0);
373 case TASK_SUPPRESSION_POLICY
:
376 task_suppression_policy_t info
= (task_suppression_policy_t
)policy_info
;
378 if (count
< TASK_SUPPRESSION_POLICY_COUNT
)
379 return (KERN_INVALID_ARGUMENT
);
381 struct task_qos_policy qosinfo
;
383 qosinfo
.task_latency_qos_tier
= info
->timer_throttle
;
384 qosinfo
.task_throughput_qos_tier
= info
->throughput_qos
;
386 kern_return_t kr
= task_qos_policy_validate(&qosinfo
, TASK_QOS_POLICY_COUNT
);
388 if (kr
!= KERN_SUCCESS
)
393 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
394 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION
, info
->active
)) | DBG_FUNC_START
,
395 proc_selfpid(), audit_token_pid_from_task(task
), trequested(task
, THREAD_NULL
),
398 task
->requested_policy
.t_sup_active
= (info
->active
) ? 1 : 0;
399 task
->requested_policy
.t_sup_lowpri_cpu
= (info
->lowpri_cpu
) ? 1 : 0;
400 task
->requested_policy
.t_sup_timer
= task_qos_extract(info
->timer_throttle
);
401 task
->requested_policy
.t_sup_disk
= (info
->disk_throttle
) ? 1 : 0;
402 task
->requested_policy
.t_sup_cpu_limit
= (info
->cpu_limit
) ? 1 : 0;
403 task
->requested_policy
.t_sup_suspend
= (info
->suspend
) ? 1 : 0;
404 task
->requested_policy
.t_sup_throughput
= task_qos_extract(info
->throughput_qos
);
405 task
->requested_policy
.t_sup_cpu
= (info
->suppressed_cpu
) ? 1 : 0;
407 task_policy_update_locked(task
, THREAD_NULL
);
411 task_policy_update_complete_unlocked(task
, THREAD_NULL
);
413 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
414 (IMPORTANCE_CODE(IMP_TASK_SUPPRESSION
, info
->active
)) | DBG_FUNC_END
,
415 proc_selfpid(), audit_token_pid_from_task(task
), trequested(task
, THREAD_NULL
),
423 result
= KERN_INVALID_ARGUMENT
;
430 /* Sets BSD 'nice' value on the task */
434 integer_t importance
)
436 if (task
== TASK_NULL
|| task
== kernel_task
)
437 return (KERN_INVALID_ARGUMENT
);
444 return (KERN_TERMINATED
);
447 if (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
) >= TASK_CONTROL_APPLICATION
) {
450 return (KERN_INVALID_ARGUMENT
);
453 task
->importance
= importance
;
455 /* TODO: tracepoint? */
457 /* Redrive only the task priority calculation */
458 task_policy_update_task_locked(task
, FALSE
, FALSE
);
462 return (KERN_SUCCESS
);
468 task_policy_flavor_t flavor
,
469 task_policy_t policy_info
,
470 mach_msg_type_number_t
*count
,
471 boolean_t
*get_default
)
473 if (task
== TASK_NULL
|| task
== kernel_task
)
474 return (KERN_INVALID_ARGUMENT
);
478 case TASK_CATEGORY_POLICY
:
480 task_category_policy_t info
= (task_category_policy_t
)policy_info
;
482 if (*count
< TASK_CATEGORY_POLICY_COUNT
)
483 return (KERN_INVALID_ARGUMENT
);
486 info
->role
= TASK_UNSPECIFIED
;
488 info
->role
= proc_get_task_policy(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
492 case TASK_BASE_QOS_POLICY
: /* FALLTHRU */
493 case TASK_OVERRIDE_QOS_POLICY
:
495 task_qos_policy_t info
= (task_qos_policy_t
)policy_info
;
497 if (*count
< TASK_QOS_POLICY_COUNT
)
498 return (KERN_INVALID_ARGUMENT
);
501 info
->task_latency_qos_tier
= LATENCY_QOS_TIER_UNSPECIFIED
;
502 info
->task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_UNSPECIFIED
;
503 } else if (flavor
== TASK_BASE_QOS_POLICY
) {
506 info
->task_latency_qos_tier
= task_qos_latency_package(task
->requested_policy
.t_base_latency_qos
);
507 info
->task_throughput_qos_tier
= task_qos_throughput_package(task
->requested_policy
.t_base_through_qos
);
510 } else if (flavor
== TASK_OVERRIDE_QOS_POLICY
) {
513 info
->task_latency_qos_tier
= task_qos_latency_package(task
->requested_policy
.t_over_latency_qos
);
514 info
->task_throughput_qos_tier
= task_qos_throughput_package(task
->requested_policy
.t_over_through_qos
);
522 case TASK_POLICY_STATE
:
524 task_policy_state_t info
= (task_policy_state_t
)policy_info
;
526 if (*count
< TASK_POLICY_STATE_COUNT
)
527 return (KERN_INVALID_ARGUMENT
);
529 /* Only root can get this info */
530 if (current_task()->sec_token
.val
[0] != 0)
531 return KERN_PROTECTION_FAILURE
;
539 info
->imp_assertcnt
= 0;
540 info
->imp_externcnt
= 0;
543 info
->requested
= task_requested_bitfield(task
, THREAD_NULL
);
544 info
->effective
= task_effective_bitfield(task
, THREAD_NULL
);
545 info
->pending
= task_pending_bitfield(task
, THREAD_NULL
);
546 info
->imp_assertcnt
= task
->task_imp_assertcnt
;
547 info
->imp_externcnt
= task
->task_imp_externcnt
;
550 info
->flags
|= (task
->imp_receiver
? TASK_IMP_RECEIVER
: 0);
551 info
->flags
|= (task
->imp_donor
? TASK_IMP_DONOR
: 0);
559 case TASK_SUPPRESSION_POLICY
:
561 task_suppression_policy_t info
= (task_suppression_policy_t
)policy_info
;
563 if (*count
< TASK_SUPPRESSION_POLICY_COUNT
)
564 return (KERN_INVALID_ARGUMENT
);
570 info
->lowpri_cpu
= 0;
571 info
->timer_throttle
= LATENCY_QOS_TIER_UNSPECIFIED
;
572 info
->disk_throttle
= 0;
575 info
->throughput_qos
= 0;
576 info
->suppressed_cpu
= 0;
578 info
->active
= task
->requested_policy
.t_sup_active
;
579 info
->lowpri_cpu
= task
->requested_policy
.t_sup_lowpri_cpu
;
580 info
->timer_throttle
= task_qos_latency_package(task
->requested_policy
.t_sup_timer
);
581 info
->disk_throttle
= task
->requested_policy
.t_sup_disk
;
582 info
->cpu_limit
= task
->requested_policy
.t_sup_cpu_limit
;
583 info
->suspend
= task
->requested_policy
.t_sup_suspend
;
584 info
->throughput_qos
= task_qos_throughput_package(task
->requested_policy
.t_sup_throughput
);
585 info
->suppressed_cpu
= task
->requested_policy
.t_sup_cpu
;
593 return (KERN_INVALID_ARGUMENT
);
596 return (KERN_SUCCESS
);
600 * Called at task creation
601 * We calculate the correct effective but don't apply it to anything yet.
602 * The threads, etc will inherit from the task as they get created.
605 task_policy_create(task_t task
, int parent_boosted
)
607 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
) {
608 if (parent_boosted
) {
609 task
->requested_policy
.t_apptype
= TASK_APPTYPE_DAEMON_INTERACTIVE
;
610 task_importance_mark_donor(task
, TRUE
);
612 task
->requested_policy
.t_apptype
= TASK_APPTYPE_DAEMON_BACKGROUND
;
613 task_importance_mark_receiver(task
, FALSE
);
617 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
618 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_TASK
))) | DBG_FUNC_START
,
619 proc_selfpid(), audit_token_pid_from_task(task
),
620 teffective(task
, THREAD_NULL
), tpriority(task
, THREAD_NULL
), 0);
622 task_policy_update_internal_locked(task
, THREAD_NULL
, TRUE
);
624 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
625 (IMPORTANCE_CODE(IMP_UPDATE
, (IMP_UPDATE_TASK_CREATE
| TASK_POLICY_TASK
))) | DBG_FUNC_END
,
626 proc_selfpid(), audit_token_pid_from_task(task
),
627 teffective(task
, THREAD_NULL
), tpriority(task
, THREAD_NULL
), 0);
631 task_policy_update_locked(task_t task
, thread_t thread
)
633 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
634 (IMPORTANCE_CODE(IMP_UPDATE
, tisthread(thread
)) | DBG_FUNC_START
),
635 proc_selfpid(), targetid(task
, thread
),
636 teffective(task
, thread
), tpriority(task
, thread
), 0);
638 task_policy_update_internal_locked(task
, thread
, FALSE
);
640 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
641 (IMPORTANCE_CODE(IMP_UPDATE
, tisthread(thread
))) | DBG_FUNC_END
,
642 proc_selfpid(), targetid(task
, thread
),
643 teffective(task
, thread
), tpriority(task
, thread
), 0);
647 * One state update function TO RULE THEM ALL
649 * This function updates the task or thread effective policy fields
650 * and pushes the results to the relevant subsystems.
652 * Must call update_complete after unlocking the task,
653 * as some subsystems cannot be updated while holding the task lock.
655 * Called with task locked, not thread
658 task_policy_update_internal_locked(task_t task
, thread_t thread
, boolean_t in_create
)
660 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
664 * Gather requested policy
667 struct task_requested_policy requested
=
668 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
672 * Calculate new effective policies from requested policy and task state
674 * If in an 'on_task' block, must only look at and set fields starting with t_
675 * If operating on a task, don't touch anything starting with th_
676 * If operating on a thread, don't touch anything starting with t_
677 * Don't change requested, it won't take effect
680 struct task_effective_policy next
= {};
682 /* Calculate DARWIN_BG */
683 boolean_t wants_darwinbg
= FALSE
;
684 boolean_t wants_all_sockets_bg
= FALSE
; /* Do I want my existing sockets to be bg */
685 boolean_t wants_watchersbg
= FALSE
; /* Do I want my pidbound threads to be bg */
686 boolean_t wants_tal
= FALSE
; /* Do I want the effects of TAL mode */
688 * If DARWIN_BG has been requested at either level, it's engaged.
689 * Only true DARWIN_BG changes cause watchers to transition.
691 if (requested
.int_darwinbg
|| requested
.ext_darwinbg
)
692 wants_watchersbg
= wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
695 /* Background TAL apps are throttled when TAL is enabled */
696 if (requested
.t_apptype
== TASK_APPTYPE_APP_TAL
&&
697 requested
.t_role
== TASK_BACKGROUND_APPLICATION
&&
698 requested
.t_tal_enabled
== 1) {
700 next
.t_tal_engaged
= 1;
703 /* Adaptive daemons are DARWIN_BG unless boosted, and don't get network throttled. */
704 if (requested
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&&
705 requested
.t_boosted
== 0)
706 wants_darwinbg
= TRUE
;
708 /* Background daemons are always DARWIN_BG, no exceptions, and don't get network throttled. */
709 if (requested
.t_apptype
== TASK_APPTYPE_DAEMON_BACKGROUND
)
710 wants_darwinbg
= TRUE
;
712 if (requested
.th_pidbind_bg
)
713 wants_all_sockets_bg
= wants_darwinbg
= TRUE
;
715 if (requested
.th_workq_bg
)
716 wants_darwinbg
= TRUE
;
719 /* Calculate side effects of DARWIN_BG */
721 if (wants_darwinbg
) {
723 /* darwinbg threads/tasks always create bg sockets, but we don't always loop over all sockets */
724 next
.new_sockets_bg
= 1;
728 if (wants_all_sockets_bg
)
729 next
.all_sockets_bg
= 1;
731 if (on_task
&& wants_watchersbg
)
732 next
.t_watchers_bg
= 1;
734 /* Calculate low CPU priority */
736 boolean_t wants_lowpri_cpu
= FALSE
;
738 if (wants_darwinbg
|| wants_tal
)
739 wants_lowpri_cpu
= TRUE
;
741 if (on_task
&& requested
.t_sup_lowpri_cpu
&& requested
.t_boosted
== 0)
742 wants_lowpri_cpu
= TRUE
;
744 if (wants_lowpri_cpu
)
747 /* Calculate IO policy */
749 /* Update BG IO policy (so we can see if it has changed) */
750 next
.bg_iotier
= requested
.bg_iotier
;
752 int iopol
= THROTTLE_LEVEL_TIER0
;
755 iopol
= MAX(iopol
, requested
.bg_iotier
);
758 if (requested
.t_apptype
== TASK_APPTYPE_DAEMON_STANDARD
)
759 iopol
= MAX(iopol
, proc_standard_daemon_tier
);
761 if (requested
.t_sup_disk
&& requested
.t_boosted
== 0)
762 iopol
= MAX(iopol
, proc_suppressed_disk_tier
);
765 iopol
= MAX(iopol
, proc_tal_disk_tier
);
768 iopol
= MAX(iopol
, requested
.int_iotier
);
769 iopol
= MAX(iopol
, requested
.ext_iotier
);
771 next
.io_tier
= iopol
;
773 /* Calculate Passive IO policy */
775 if (requested
.ext_iopassive
|| requested
.int_iopassive
)
778 /* Calculate miscellaneous policy */
782 next
.t_role
= requested
.t_role
;
784 /* Calculate suppression-active flag */
785 if (requested
.t_sup_active
&& requested
.t_boosted
== 0)
786 next
.t_sup_active
= 1;
788 /* Calculate suspend policy */
789 if (requested
.t_sup_suspend
&& requested
.t_boosted
== 0)
790 next
.t_suspended
= 1;
792 /* Calculate GPU Access policy */
793 if (requested
.t_int_gpu_deny
|| requested
.t_ext_gpu_deny
)
797 /* Calculate timer QOS */
798 int latency_qos
= requested
.t_base_latency_qos
;
800 if (requested
.t_sup_timer
&& requested
.t_boosted
== 0)
801 latency_qos
= requested
.t_sup_timer
;
803 if (requested
.t_over_latency_qos
!= 0)
804 latency_qos
= requested
.t_over_latency_qos
;
806 /* Treat the windowserver special */
807 if (requested
.t_role
== TASK_GRAPHICS_SERVER
)
808 latency_qos
= proc_graphics_timer_qos
;
810 next
.t_latency_qos
= latency_qos
;
812 /* Calculate throughput QOS */
813 int through_qos
= requested
.t_base_through_qos
;
815 if (requested
.t_sup_throughput
&& requested
.t_boosted
== 0)
816 through_qos
= requested
.t_sup_throughput
;
818 if (requested
.t_over_through_qos
!= 0)
819 through_qos
= requested
.t_over_through_qos
;
821 next
.t_through_qos
= through_qos
;
823 /* Calculate suppressed CPU priority */
824 if (requested
.t_sup_cpu
&& requested
.t_boosted
== 0)
825 next
.t_suppressed_cpu
= 1;
828 if (requested
.terminated
) {
830 * Shoot down the throttles that slow down exit or response to SIGTERM
831 * We don't need to shoot down:
832 * passive (don't want to cause others to throttle)
833 * all_sockets_bg (don't need to iterate FDs on every exit)
834 * new_sockets_bg (doesn't matter for exiting process)
835 * gpu deny (doesn't matter for exiting process)
836 * pidsuspend (jetsam-ed BG process shouldn't run again)
837 * watchers_bg (watcher threads don't need to be unthrottled)
838 * t_latency_qos (affects userspace timers only)
844 next
.io_tier
= THROTTLE_LEVEL_TIER0
;
846 next
.t_tal_engaged
= 0;
847 next
.t_role
= TASK_UNSPECIFIED
;
848 next
.t_suppressed_cpu
= 0;
850 /* TODO: This should only be shot down on SIGTERM, not exit */
851 next
.t_suspended
= 0;
857 * Swap out old policy for new policy
860 struct task_effective_policy prev
=
861 (on_task
) ? task
->effective_policy
: thread
->effective_policy
;
864 * Check for invalid transitions here for easier debugging
865 * TODO: dump the structs as hex in the panic string
867 if (task
== kernel_task
&& prev
.all_sockets_bg
!= next
.all_sockets_bg
)
868 panic("unexpected network change for kernel task");
870 /* This is the point where the new values become visible to other threads */
872 task
->effective_policy
= next
;
874 thread
->effective_policy
= next
;
876 /* Don't do anything further to a half-formed task or thread */
882 * Pend updates that can't be done while holding the task lock
883 * Preserve pending updates that may still be waiting to be applied
886 struct task_pended_policy pended
=
887 (on_task
) ? task
->pended_policy
: thread
->pended_policy
;
889 if (prev
.all_sockets_bg
!= next
.all_sockets_bg
)
890 pended
.update_sockets
= 1;
893 /* Only re-scan the timer list if the qos level is getting less strong */
894 if (prev
.t_latency_qos
> next
.t_latency_qos
)
895 pended
.t_update_timers
= 1;
900 task
->pended_policy
= pended
;
902 thread
->pended_policy
= pended
;
906 * Update other subsystems as necessary if something has changed
909 boolean_t update_throttle
= (prev
.io_tier
!= next
.io_tier
) ? TRUE
: FALSE
;
912 if (prev
.t_suspended
== 0 && next
.t_suspended
== 1 && task
->active
) {
913 task_hold_locked(task
);
914 task_wait_locked(task
, FALSE
);
916 if (prev
.t_suspended
== 1 && next
.t_suspended
== 0 && task
->active
) {
917 task_release_locked(task
);
920 boolean_t update_threads
= FALSE
;
922 if (prev
.bg_iotier
!= next
.bg_iotier
)
923 update_threads
= TRUE
;
925 if (prev
.terminated
!= next
.terminated
)
926 update_threads
= TRUE
;
928 task_policy_update_task_locked(task
, update_throttle
, update_threads
);
932 if (prev
.lowpri_cpu
!= next
.lowpri_cpu
)
933 update_cpu
= (next
.lowpri_cpu
? DO_LOWPRI_CPU
: UNDO_LOWPRI_CPU
);
935 task_policy_update_thread_locked(thread
, update_cpu
, update_throttle
);
939 /* Despite the name, the thread's task is locked, the thread is not */
941 task_policy_update_thread_locked(thread_t thread
,
943 boolean_t update_throttle
)
945 thread_precedence_policy_data_t policy
;
947 if (update_throttle
) {
948 rethrottle_thread(thread
->uthread
);
952 * TODO: pidbind needs to stuff remembered importance into saved_importance
953 * properly deal with bg'ed threads being pidbound and unbging while pidbound
955 * TODO: A BG thread's priority is 0 on desktop and 4 on embedded. Need to reconcile this.
957 if (update_cpu
== DO_LOWPRI_CPU
) {
958 thread
->saved_importance
= thread
->importance
;
959 policy
.importance
= INT_MIN
;
960 } else if (update_cpu
== UNDO_LOWPRI_CPU
) {
961 policy
.importance
= thread
->saved_importance
;
962 thread
->saved_importance
= 0;
965 /* Takes thread lock and thread mtx lock */
967 thread_policy_set_internal(thread
, THREAD_PRECEDENCE_POLICY
,
968 (thread_policy_t
)&policy
,
969 THREAD_PRECEDENCE_POLICY_COUNT
);
973 * Calculate priority on a task, loop through its threads, and tell them about
974 * priority changes and throttle changes.
977 task_policy_update_task_locked(task_t task
,
978 boolean_t update_throttle
,
979 boolean_t update_threads
)
981 boolean_t update_priority
= FALSE
;
983 if (task
== kernel_task
)
984 panic("Attempting to set task policy on kernel_task");
986 int priority
= BASEPRI_DEFAULT
;
987 int max_priority
= MAXPRI_USER
;
989 if (proc_get_effective_task_policy(task
, TASK_POLICY_LOWPRI_CPU
)) {
990 priority
= MAXPRI_THROTTLE
;
991 max_priority
= MAXPRI_THROTTLE
;
992 } else if (proc_get_effective_task_policy(task
, TASK_POLICY_SUPPRESSED_CPU
)) {
993 priority
= MAXPRI_SUPPRESSED
;
994 max_priority
= MAXPRI_SUPPRESSED
;
996 switch (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
)) {
997 case TASK_FOREGROUND_APPLICATION
:
998 priority
= BASEPRI_FOREGROUND
;
1000 case TASK_BACKGROUND_APPLICATION
:
1001 priority
= BASEPRI_BACKGROUND
;
1003 case TASK_CONTROL_APPLICATION
:
1004 priority
= BASEPRI_CONTROL
;
1006 case TASK_GRAPHICS_SERVER
:
1007 priority
= BASEPRI_GRAPHICS
;
1008 max_priority
= MAXPRI_RESERVED
;
1014 /* factor in 'nice' value */
1015 priority
+= task
->importance
;
1018 /* avoid extra work if priority isn't changing */
1019 if (task
->priority
!= priority
|| task
->max_priority
!= max_priority
) {
1020 update_priority
= TRUE
;
1022 /* update the scheduling priority for the task */
1023 task
->max_priority
= max_priority
;
1025 if (priority
> task
->max_priority
)
1026 priority
= task
->max_priority
;
1027 else if (priority
< MINPRI
)
1030 task
->priority
= priority
;
1033 /* Loop over the threads in the task only once, and only if necessary */
1034 if (update_threads
|| update_throttle
|| update_priority
) {
1037 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1038 if (update_priority
) {
1039 thread_mtx_lock(thread
);
1042 thread_task_priority(thread
, priority
, max_priority
);
1044 thread_mtx_unlock(thread
);
1047 if (update_throttle
) {
1048 rethrottle_thread(thread
->uthread
);
1051 if (update_threads
) {
1052 thread
->requested_policy
.bg_iotier
= task
->effective_policy
.bg_iotier
;
1053 thread
->requested_policy
.terminated
= task
->effective_policy
.terminated
;
1055 task_policy_update_internal_locked(task
, thread
, FALSE
);
1056 /* The thread policy must not emit any completion actions due to this change. */
1063 * Called with task unlocked to do things that can't be done while holding the task lock
1064 * To keep things consistent, only one thread can make progress through here at a time for any one task.
1069 task_policy_update_complete_unlocked(task_t task
, thread_t thread
)
1071 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1075 while (task
->pended_policy
.t_updating_policy
!= 0) {
1076 assert_wait((event_t
)&task
->pended_policy
, THREAD_UNINT
);
1078 thread_block(THREAD_CONTINUE_NULL
);
1082 /* Take a snapshot of the current state */
1084 struct task_pended_policy pended
=
1085 (on_task
) ? task
->pended_policy
: thread
->pended_policy
;
1087 struct task_effective_policy effective
=
1088 (on_task
) ? task
->effective_policy
: thread
->effective_policy
;
1090 /* Mark the pended operations as being handled */
1092 task
->pended_policy
= default_task_pended_policy
;
1094 thread
->pended_policy
= default_task_pended_policy
;
1096 task
->pended_policy
.t_updating_policy
= 1;
1100 /* Update the other subsystems with the new state */
1103 if (pended
.update_sockets
)
1104 proc_apply_task_networkbg(task
->bsd_info
, thread
, effective
.all_sockets_bg
);
1105 #endif /* MACH_BSD */
1108 /* The timer throttle has been removed, we need to look for expired timers and fire them */
1109 if (pended
.t_update_timers
)
1110 ml_timer_evaluate();
1114 /* Wake up anyone waiting to make another update */
1116 task
->pended_policy
.t_updating_policy
= 0;
1117 thread_wakeup(&task
->pended_policy
);
1122 * Initiate a task policy state transition
1124 * Everything that modifies requested except functions that need to hold the task lock
1125 * should use this function
1127 * Argument validation should be performed before reaching this point.
1129 * TODO: Do we need to check task->active or thread->active?
1132 proc_set_task_policy(task_t task
,
1140 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1141 (IMPORTANCE_CODE(flavor
, (category
| tisthread(thread
)))) | DBG_FUNC_START
,
1142 proc_selfpid(), targetid(task
, thread
), trequested(task
, thread
), value
, 0);
1144 proc_set_task_policy_locked(task
, thread
, category
, flavor
, value
);
1146 task_policy_update_locked(task
, thread
);
1150 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1151 (IMPORTANCE_CODE(flavor
, (category
| tisthread(thread
)))) | DBG_FUNC_END
,
1152 proc_selfpid(), targetid(task
, thread
), trequested(task
, thread
), tpending(task
, thread
), 0);
1154 task_policy_update_complete_unlocked(task
, thread
);
1158 * Initiate a task policy state transition on a thread with its TID
1159 * Useful if you cannot guarantee the thread won't get terminated
1162 proc_set_task_policy_thread(task_t task
,
1169 thread_t self
= current_thread();
1173 if (tid
== TID_NULL
|| tid
== self
->thread_id
)
1176 thread
= task_findtid(task
, tid
);
1178 if (thread
== THREAD_NULL
) {
1183 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1184 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_THREAD
))) | DBG_FUNC_START
,
1185 proc_selfpid(), targetid(task
, thread
), trequested(task
, thread
), value
, 0);
1187 proc_set_task_policy_locked(task
, thread
, category
, flavor
, value
);
1189 task_policy_update_locked(task
, thread
);
1193 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1194 (IMPORTANCE_CODE(flavor
, (category
| TASK_POLICY_THREAD
))) | DBG_FUNC_END
,
1195 proc_selfpid(), targetid(task
, thread
), trequested(task
, thread
), tpending(task
, thread
), 0);
1197 task_policy_update_complete_unlocked(task
, thread
);
1202 * Set the requested state for a specific flavor to a specific value.
1205 * Verify that arguments to non iopol things are 1 or 0
1208 proc_set_task_policy_locked(task_t task
,
1214 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1218 struct task_requested_policy requested
=
1219 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
1223 /* Category: EXTERNAL and INTERNAL, thread and task */
1225 case TASK_POLICY_DARWIN_BG
:
1226 if (category
== TASK_POLICY_EXTERNAL
)
1227 requested
.ext_darwinbg
= value
;
1229 requested
.int_darwinbg
= value
;
1232 case TASK_POLICY_IOPOL
:
1233 proc_iopol_to_tier(value
, &tier
, &passive
);
1234 if (category
== TASK_POLICY_EXTERNAL
) {
1235 requested
.ext_iotier
= tier
;
1236 requested
.ext_iopassive
= passive
;
1238 requested
.int_iotier
= tier
;
1239 requested
.int_iopassive
= passive
;
1243 case TASK_POLICY_IO
:
1244 if (category
== TASK_POLICY_EXTERNAL
)
1245 requested
.ext_iotier
= value
;
1247 requested
.int_iotier
= value
;
1250 case TASK_POLICY_PASSIVE_IO
:
1251 if (category
== TASK_POLICY_EXTERNAL
)
1252 requested
.ext_iopassive
= value
;
1254 requested
.int_iopassive
= value
;
1257 /* Category: EXTERNAL and INTERNAL, task only */
1259 case TASK_POLICY_GPU_DENY
:
1261 if (category
== TASK_POLICY_EXTERNAL
)
1262 requested
.t_ext_gpu_deny
= value
;
1264 requested
.t_int_gpu_deny
= value
;
1267 case TASK_POLICY_DARWIN_BG_AND_GPU
:
1269 if (category
== TASK_POLICY_EXTERNAL
) {
1270 requested
.ext_darwinbg
= value
;
1271 requested
.t_ext_gpu_deny
= value
;
1273 requested
.int_darwinbg
= value
;
1274 requested
.t_int_gpu_deny
= value
;
1278 /* Category: INTERNAL, task only */
1280 case TASK_POLICY_DARWIN_BG_IOPOL
:
1281 assert(on_task
&& category
== TASK_POLICY_INTERNAL
);
1282 proc_iopol_to_tier(value
, &tier
, &passive
);
1283 requested
.bg_iotier
= tier
;
1286 /* Category: ATTRIBUTE, task only */
1288 case TASK_POLICY_TAL
:
1289 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1290 requested
.t_tal_enabled
= value
;
1293 case TASK_POLICY_BOOST
:
1294 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1295 requested
.t_boosted
= value
;
1298 case TASK_POLICY_ROLE
:
1299 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1300 requested
.t_role
= value
;
1303 case TASK_POLICY_TERMINATED
:
1304 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1305 requested
.terminated
= value
;
1308 /* Category: ATTRIBUTE, thread only */
1310 case TASK_POLICY_PIDBIND_BG
:
1311 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1312 requested
.th_pidbind_bg
= value
;
1315 case TASK_POLICY_WORKQ_BG
:
1316 assert(!on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1317 requested
.th_workq_bg
= value
;
1321 panic("unknown task policy: %d %d %d", category
, flavor
, value
);
1326 task
->requested_policy
= requested
;
1328 thread
->requested_policy
= requested
;
1333 * Gets what you set. Effective values may be different.
1336 proc_get_task_policy(task_t task
,
1341 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1347 struct task_requested_policy requested
=
1348 (on_task
) ? task
->requested_policy
: thread
->requested_policy
;
1351 case TASK_POLICY_DARWIN_BG
:
1352 if (category
== TASK_POLICY_EXTERNAL
)
1353 value
= requested
.ext_darwinbg
;
1355 value
= requested
.int_darwinbg
;
1357 case TASK_POLICY_IOPOL
:
1358 if (category
== TASK_POLICY_EXTERNAL
)
1359 value
= proc_tier_to_iopol(requested
.ext_iotier
,
1360 requested
.ext_iopassive
);
1362 value
= proc_tier_to_iopol(requested
.int_iotier
,
1363 requested
.int_iopassive
);
1365 case TASK_POLICY_IO
:
1366 if (category
== TASK_POLICY_EXTERNAL
)
1367 value
= requested
.ext_iotier
;
1369 value
= requested
.int_iotier
;
1371 case TASK_POLICY_PASSIVE_IO
:
1372 if (category
== TASK_POLICY_EXTERNAL
)
1373 value
= requested
.ext_iopassive
;
1375 value
= requested
.int_iopassive
;
1377 case TASK_POLICY_GPU_DENY
:
1379 if (category
== TASK_POLICY_EXTERNAL
)
1380 value
= requested
.t_ext_gpu_deny
;
1382 value
= requested
.t_int_gpu_deny
;
1384 case TASK_POLICY_DARWIN_BG_IOPOL
:
1385 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1386 value
= proc_tier_to_iopol(requested
.bg_iotier
, 0);
1388 case TASK_POLICY_ROLE
:
1389 assert(on_task
&& category
== TASK_POLICY_ATTRIBUTE
);
1390 value
= requested
.t_role
;
1393 panic("unknown policy_flavor %d", flavor
);
1404 * Functions for querying effective state for relevant subsystems
1405 * ONLY the relevant subsystem should query these.
1406 * NEVER take a value from one of the 'effective' functions and stuff it into a setter.
1410 proc_get_effective_task_policy(task_t task
, int flavor
)
1412 return proc_get_effective_policy(task
, THREAD_NULL
, flavor
);
1416 proc_get_effective_thread_policy(thread_t thread
, int flavor
)
1418 return proc_get_effective_policy(thread
->task
, thread
, flavor
);
1422 * Gets what is actually in effect, for subsystems which pull policy instead of receive updates.
1424 * NOTE: This accessor does not take the task lock.
1425 * Notifications of state updates need to be externally synchronized with state queries.
1426 * This routine *MUST* remain interrupt safe, as it is potentially invoked
1427 * within the context of a timer interrupt.
1430 proc_get_effective_policy(task_t task
,
1434 boolean_t on_task
= (thread
== THREAD_NULL
) ? TRUE
: FALSE
;
1438 case TASK_POLICY_DARWIN_BG
:
1440 * This backs the KPI call proc_pidbackgrounded to find
1441 * out if a pid is backgrounded,
1442 * as well as proc_get_effective_thread_policy.
1443 * Its main use is within the timer layer, as well as
1444 * prioritizing requests to the graphics system.
1445 * Returns 1 for background mode, 0 for normal mode
1448 value
= task
->effective_policy
.darwinbg
;
1450 value
= (task
->effective_policy
.darwinbg
||
1451 thread
->effective_policy
.darwinbg
) ? 1 : 0;
1453 case TASK_POLICY_IO
:
1455 * The I/O system calls here to find out what throttling tier to apply to an operation.
1456 * Returns THROTTLE_LEVEL_* values
1459 value
= task
->effective_policy
.io_tier
;
1461 value
= MAX(task
->effective_policy
.io_tier
,
1462 thread
->effective_policy
.io_tier
);
1463 if (thread
->iotier_override
!= THROTTLE_LEVEL_NONE
)
1464 value
= MIN(value
, thread
->iotier_override
);
1467 case TASK_POLICY_PASSIVE_IO
:
1469 * The I/O system calls here to find out whether an operation should be passive.
1470 * (i.e. not cause operations with lower throttle tiers to be throttled)
1471 * Returns 1 for passive mode, 0 for normal mode
1474 value
= task
->effective_policy
.io_passive
;
1476 value
= (task
->effective_policy
.io_passive
||
1477 thread
->effective_policy
.io_passive
) ? 1 : 0;
1479 case TASK_POLICY_NEW_SOCKETS_BG
:
1481 * socreate() calls this to determine if it should mark a new socket as background
1482 * Returns 1 for background mode, 0 for normal mode
1485 value
= task
->effective_policy
.new_sockets_bg
;
1487 value
= (task
->effective_policy
.new_sockets_bg
||
1488 thread
->effective_policy
.new_sockets_bg
) ? 1 : 0;
1490 case TASK_POLICY_LOWPRI_CPU
:
1492 * Returns 1 for low priority cpu mode, 0 for normal mode
1495 value
= task
->effective_policy
.lowpri_cpu
;
1497 value
= (task
->effective_policy
.lowpri_cpu
||
1498 thread
->effective_policy
.lowpri_cpu
) ? 1 : 0;
1500 case TASK_POLICY_SUPPRESSED_CPU
:
1502 * Returns 1 for suppressed cpu mode, 0 for normal mode
1505 value
= task
->effective_policy
.t_suppressed_cpu
;
1507 case TASK_POLICY_LATENCY_QOS
:
1509 * timer arming calls into here to find out the timer coalescing level
1510 * Returns a QoS tier (0-6)
1513 value
= task
->effective_policy
.t_latency_qos
;
1515 case TASK_POLICY_THROUGH_QOS
:
1517 * Returns a QoS tier (0-6)
1520 value
= task
->effective_policy
.t_through_qos
;
1522 case TASK_POLICY_GPU_DENY
:
1524 * This is where IOKit calls into task_policy to find out whether
1525 * it should allow access to the GPU.
1526 * Returns 1 for NOT allowed, returns 0 for allowed
1529 value
= task
->effective_policy
.t_gpu_deny
;
1531 case TASK_POLICY_ROLE
:
1533 value
= task
->effective_policy
.t_role
;
1535 case TASK_POLICY_WATCHERS_BG
:
1537 value
= task
->effective_policy
.t_watchers_bg
;
1540 panic("unknown policy_flavor %d", flavor
);
1548 * Convert from IOPOL_* values to throttle tiers.
1550 * TODO: Can this be made more compact, like an array lookup
1551 * Note that it is possible to support e.g. IOPOL_PASSIVE_STANDARD in the future
1555 proc_iopol_to_tier(int iopolicy
, int *tier
, int *passive
)
1560 case IOPOL_IMPORTANT
:
1561 *tier
= THROTTLE_LEVEL_TIER0
;
1564 *tier
= THROTTLE_LEVEL_TIER0
;
1567 case IOPOL_STANDARD
:
1568 *tier
= THROTTLE_LEVEL_TIER1
;
1571 *tier
= THROTTLE_LEVEL_TIER2
;
1573 case IOPOL_THROTTLE
:
1574 *tier
= THROTTLE_LEVEL_TIER3
;
1577 panic("unknown I/O policy %d", iopolicy
);
1583 proc_tier_to_iopol(int tier
, int passive
)
1587 case THROTTLE_LEVEL_TIER0
:
1588 return IOPOL_PASSIVE
;
1591 panic("unknown passive tier %d", tier
);
1592 return IOPOL_DEFAULT
;
1597 case THROTTLE_LEVEL_NONE
:
1598 return IOPOL_DEFAULT
;
1600 case THROTTLE_LEVEL_TIER0
:
1601 return IOPOL_IMPORTANT
;
1603 case THROTTLE_LEVEL_TIER1
:
1604 return IOPOL_STANDARD
;
1606 case THROTTLE_LEVEL_TIER2
:
1607 return IOPOL_UTILITY
;
1609 case THROTTLE_LEVEL_TIER3
:
1610 return IOPOL_THROTTLE
;
1613 panic("unknown tier %d", tier
);
1614 return IOPOL_DEFAULT
;
1620 /* apply internal backgrounding for workqueue threads */
1622 proc_apply_workq_bgthreadpolicy(thread_t thread
)
1624 if (thread
== THREAD_NULL
)
1627 proc_set_task_policy(thread
->task
, thread
, TASK_POLICY_ATTRIBUTE
,
1628 TASK_POLICY_WORKQ_BG
, TASK_POLICY_ENABLE
);
1634 * remove internal backgrounding for workqueue threads
1635 * does NOT go find sockets created while BG and unbackground them
1638 proc_restore_workq_bgthreadpolicy(thread_t thread
)
1640 if (thread
== THREAD_NULL
)
1643 proc_set_task_policy(thread
->task
, thread
, TASK_POLICY_ATTRIBUTE
,
1644 TASK_POLICY_WORKQ_BG
, TASK_POLICY_DISABLE
);
1649 /* here for temporary compatibility */
1651 proc_setthread_saved_importance(__unused thread_t thread
, __unused
int importance
)
1657 * Set an override on the thread which is consulted with a
1658 * higher priority than the task/thread policy. This should
1659 * only be set for temporary grants until the thread
1660 * returns to the userspace boundary
1662 * We use atomic operations to swap in the override, with
1663 * the assumption that the thread itself can
1664 * read the override and clear it on return to userspace.
1666 * No locking is performed, since it is acceptable to see
1667 * a stale override for one loop through throttle_lowpri_io().
1668 * However a thread reference must be held on the thread.
1671 void set_thread_iotier_override(thread_t thread
, int policy
)
1673 int current_override
;
1675 /* Let most aggressive I/O policy win until user boundary */
1677 current_override
= thread
->iotier_override
;
1679 if (current_override
!= THROTTLE_LEVEL_NONE
)
1680 policy
= MIN(current_override
, policy
);
1682 if (current_override
== policy
) {
1683 /* no effective change */
1686 } while (!OSCompareAndSwap(current_override
, policy
, &thread
->iotier_override
));
1689 * Since the thread may be currently throttled,
1690 * re-evaluate tiers and potentially break out
1693 rethrottle_thread(thread
->uthread
);
1697 * Called at process exec to initialize the apptype of a process
1700 proc_set_task_apptype(task_t task
, int apptype
)
1704 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1705 (IMPORTANCE_CODE(IMP_TASK_APPTYPE
, apptype
)) | DBG_FUNC_START
,
1706 proc_selfpid(), audit_token_pid_from_task(task
), trequested(task
, THREAD_NULL
),
1710 case TASK_APPTYPE_APP_TAL
:
1711 /* TAL starts off enabled by default */
1712 task
->requested_policy
.t_tal_enabled
= 1;
1715 case TASK_APPTYPE_APP_DEFAULT
:
1716 case TASK_APPTYPE_DAEMON_INTERACTIVE
:
1717 task
->requested_policy
.t_apptype
= apptype
;
1719 task_importance_mark_donor(task
, TRUE
);
1720 /* Apps (and interactive daemons) are boost recievers on desktop for suppression behaviors */
1721 task_importance_mark_receiver(task
, TRUE
);
1724 case TASK_APPTYPE_DAEMON_STANDARD
:
1725 task
->requested_policy
.t_apptype
= apptype
;
1727 task_importance_mark_donor(task
, TRUE
);
1728 task_importance_mark_receiver(task
, FALSE
);
1731 case TASK_APPTYPE_DAEMON_ADAPTIVE
:
1732 task
->requested_policy
.t_apptype
= apptype
;
1734 task_importance_mark_donor(task
, FALSE
);
1735 task_importance_mark_receiver(task
, TRUE
);
1738 case TASK_APPTYPE_DAEMON_BACKGROUND
:
1739 task
->requested_policy
.t_apptype
= apptype
;
1741 task_importance_mark_donor(task
, FALSE
);
1742 task_importance_mark_receiver(task
, FALSE
);
1746 panic("invalid apptype %d", apptype
);
1750 task_policy_update_locked(task
, THREAD_NULL
);
1754 task_policy_update_complete_unlocked(task
, THREAD_NULL
);
1756 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1757 (IMPORTANCE_CODE(IMP_TASK_APPTYPE
, apptype
)) | DBG_FUNC_END
,
1758 proc_selfpid(), audit_token_pid_from_task(task
), trequested(task
, THREAD_NULL
),
1759 task
->imp_receiver
, 0);
1762 /* for process_policy to check before attempting to set */
1764 proc_task_is_tal(task_t task
)
1766 return (task
->requested_policy
.t_apptype
== TASK_APPTYPE_APP_TAL
) ? TRUE
: FALSE
;
1771 task_grab_latency_qos(task_t task
)
1773 return task_qos_latency_package(proc_get_effective_task_policy(task
, TASK_POLICY_LATENCY_QOS
));
1776 /* update the darwin background action state in the flags field for libproc */
1778 proc_get_darwinbgstate(task_t task
, uint32_t * flagsp
)
1780 if (task
->requested_policy
.ext_darwinbg
)
1781 *flagsp
|= PROC_FLAG_EXT_DARWINBG
;
1783 if (task
->requested_policy
.int_darwinbg
)
1784 *flagsp
|= PROC_FLAG_DARWINBG
;
1787 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
)
1788 *flagsp
|= PROC_FLAG_ADAPTIVE
;
1790 if (task
->requested_policy
.t_apptype
== TASK_APPTYPE_DAEMON_ADAPTIVE
&& task
->requested_policy
.t_boosted
== 1)
1791 *flagsp
|= PROC_FLAG_ADAPTIVE_IMPORTANT
;
1793 if (task
->imp_donor
)
1794 *flagsp
|= PROC_FLAG_IMPORTANCE_DONOR
;
1796 if (task
->effective_policy
.t_sup_active
)
1797 *flagsp
|= PROC_FLAG_SUPPRESSED
;
1802 /* All per-thread state is in the first 32-bits of the bitfield */
1804 proc_get_thread_policy(thread_t thread
, thread_policy_state_t info
)
1806 task_t task
= thread
->task
;
1808 info
->requested
= (integer_t
)task_requested_bitfield(task
, thread
);
1809 info
->effective
= (integer_t
)task_effective_bitfield(task
, thread
);
1810 info
->pending
= (integer_t
)task_pending_bitfield(task
, thread
);
1815 /* dump requested for tracepoint */
1817 trequested(task_t task
, thread_t thread
)
1819 return (uintptr_t) task_requested_bitfield(task
, thread
);
1822 /* dump effective for tracepoint */
1824 teffective(task_t task
, thread_t thread
)
1826 return (uintptr_t) task_effective_bitfield(task
, thread
);
1829 /* dump pending for tracepoint */
1831 tpending(task_t task
, thread_t thread
)
1833 return (uintptr_t) task_pending_bitfield(task
, thread
);
1837 task_requested_bitfield(task_t task
, thread_t thread
)
1840 struct task_requested_policy requested
=
1841 (thread
== THREAD_NULL
) ? task
->requested_policy
: thread
->requested_policy
;
1843 bits
|= (requested
.int_darwinbg
? POLICY_REQ_INT_DARWIN_BG
: 0);
1844 bits
|= (requested
.ext_darwinbg
? POLICY_REQ_EXT_DARWIN_BG
: 0);
1845 bits
|= (requested
.int_iotier
? (((uint64_t)requested
.int_iotier
) << POLICY_REQ_INT_IO_TIER_SHIFT
) : 0);
1846 bits
|= (requested
.ext_iotier
? (((uint64_t)requested
.ext_iotier
) << POLICY_REQ_EXT_IO_TIER_SHIFT
) : 0);
1847 bits
|= (requested
.int_iopassive
? POLICY_REQ_INT_PASSIVE_IO
: 0);
1848 bits
|= (requested
.ext_iopassive
? POLICY_REQ_EXT_PASSIVE_IO
: 0);
1849 bits
|= (requested
.bg_iotier
? (((uint64_t)requested
.bg_iotier
) << POLICY_REQ_BG_IOTIER_SHIFT
) : 0);
1850 bits
|= (requested
.terminated
? POLICY_REQ_TERMINATED
: 0);
1852 bits
|= (requested
.th_pidbind_bg
? POLICY_REQ_PIDBIND_BG
: 0);
1853 bits
|= (requested
.th_workq_bg
? POLICY_REQ_WORKQ_BG
: 0);
1855 bits
|= (requested
.t_boosted
? POLICY_REQ_BOOSTED
: 0);
1856 bits
|= (requested
.t_tal_enabled
? POLICY_REQ_TAL_ENABLED
: 0);
1857 bits
|= (requested
.t_int_gpu_deny
? POLICY_REQ_INT_GPU_DENY
: 0);
1858 bits
|= (requested
.t_ext_gpu_deny
? POLICY_REQ_EXT_GPU_DENY
: 0);
1859 bits
|= (requested
.t_apptype
? (((uint64_t)requested
.t_apptype
) << POLICY_REQ_APPTYPE_SHIFT
) : 0);
1860 bits
|= (requested
.t_role
? (((uint64_t)requested
.t_role
) << POLICY_REQ_ROLE_SHIFT
) : 0);
1862 bits
|= (requested
.t_sup_active
? POLICY_REQ_SUP_ACTIVE
: 0);
1863 bits
|= (requested
.t_sup_lowpri_cpu
? POLICY_REQ_SUP_LOWPRI_CPU
: 0);
1864 bits
|= (requested
.t_sup_cpu
? POLICY_REQ_SUP_CPU
: 0);
1865 bits
|= (requested
.t_sup_timer
? (((uint64_t)requested
.t_sup_timer
) << POLICY_REQ_SUP_TIMER_THROTTLE_SHIFT
) : 0);
1866 bits
|= (requested
.t_sup_throughput
? (((uint64_t)requested
.t_sup_throughput
) << POLICY_REQ_SUP_THROUGHPUT_SHIFT
) : 0);
1867 bits
|= (requested
.t_sup_disk
? POLICY_REQ_SUP_DISK_THROTTLE
: 0);
1868 bits
|= (requested
.t_sup_cpu_limit
? POLICY_REQ_SUP_CPU_LIMIT
: 0);
1869 bits
|= (requested
.t_sup_suspend
? POLICY_REQ_SUP_SUSPEND
: 0);
1870 bits
|= (requested
.t_base_latency_qos
? (((uint64_t)requested
.t_base_latency_qos
) << POLICY_REQ_BASE_LATENCY_QOS_SHIFT
) : 0);
1871 bits
|= (requested
.t_over_latency_qos
? (((uint64_t)requested
.t_over_latency_qos
) << POLICY_REQ_OVER_LATENCY_QOS_SHIFT
) : 0);
1872 bits
|= (requested
.t_base_through_qos
? (((uint64_t)requested
.t_base_through_qos
) << POLICY_REQ_BASE_THROUGH_QOS_SHIFT
) : 0);
1873 bits
|= (requested
.t_over_through_qos
? (((uint64_t)requested
.t_over_through_qos
) << POLICY_REQ_OVER_THROUGH_QOS_SHIFT
) : 0);
1879 task_effective_bitfield(task_t task
, thread_t thread
)
1882 struct task_effective_policy effective
=
1883 (thread
== THREAD_NULL
) ? task
->effective_policy
: thread
->effective_policy
;
1885 bits
|= (effective
.io_tier
? (((uint64_t)effective
.io_tier
) << POLICY_EFF_IO_TIER_SHIFT
) : 0);
1886 bits
|= (effective
.io_passive
? POLICY_EFF_IO_PASSIVE
: 0);
1887 bits
|= (effective
.darwinbg
? POLICY_EFF_DARWIN_BG
: 0);
1888 bits
|= (effective
.lowpri_cpu
? POLICY_EFF_LOWPRI_CPU
: 0);
1889 bits
|= (effective
.terminated
? POLICY_EFF_TERMINATED
: 0);
1890 bits
|= (effective
.all_sockets_bg
? POLICY_EFF_ALL_SOCKETS_BG
: 0);
1891 bits
|= (effective
.new_sockets_bg
? POLICY_EFF_NEW_SOCKETS_BG
: 0);
1892 bits
|= (effective
.bg_iotier
? (((uint64_t)effective
.bg_iotier
) << POLICY_EFF_BG_IOTIER_SHIFT
) : 0);
1894 bits
|= (effective
.t_gpu_deny
? POLICY_EFF_GPU_DENY
: 0);
1895 bits
|= (effective
.t_tal_engaged
? POLICY_EFF_TAL_ENGAGED
: 0);
1896 bits
|= (effective
.t_suspended
? POLICY_EFF_SUSPENDED
: 0);
1897 bits
|= (effective
.t_watchers_bg
? POLICY_EFF_WATCHERS_BG
: 0);
1898 bits
|= (effective
.t_sup_active
? POLICY_EFF_SUP_ACTIVE
: 0);
1899 bits
|= (effective
.t_suppressed_cpu
? POLICY_EFF_SUP_CPU
: 0);
1900 bits
|= (effective
.t_role
? (((uint64_t)effective
.t_role
) << POLICY_EFF_ROLE_SHIFT
) : 0);
1901 bits
|= (effective
.t_latency_qos
? (((uint64_t)effective
.t_latency_qos
) << POLICY_EFF_LATENCY_QOS_SHIFT
) : 0);
1902 bits
|= (effective
.t_through_qos
? (((uint64_t)effective
.t_through_qos
) << POLICY_EFF_THROUGH_QOS_SHIFT
) : 0);
1908 task_pending_bitfield(task_t task
, thread_t thread
)
1911 struct task_pended_policy pended
=
1912 (thread
== THREAD_NULL
) ? task
->pended_policy
: thread
->pended_policy
;
1914 bits
|= (pended
.t_updating_policy
? POLICY_PEND_UPDATING
: 0);
1915 bits
|= (pended
.update_sockets
? POLICY_PEND_SOCKETS
: 0);
1917 bits
|= (pended
.t_update_timers
? POLICY_PEND_TIMERS
: 0);
1918 bits
|= (pended
.t_update_watchers
? POLICY_PEND_WATCHERS
: 0);
1925 * Resource usage and CPU related routines
1929 proc_get_task_ruse_cpu(task_t task
, uint32_t *policyp
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
)
1938 error
= task_get_cpuusage(task
, percentagep
, intervalp
, deadlinep
, &scope
);
1942 * Reverse-map from CPU resource limit scopes back to policies (see comment below).
1944 if (scope
== TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
1945 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
;
1946 } else if (scope
== TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
1947 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE
;
1948 } else if (scope
== TASK_RUSECPU_FLAGS_DEADLINE
) {
1949 *policyp
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
1956 * Configure the default CPU usage monitor parameters.
1958 * For tasks which have this mechanism activated: if any thread in the
1959 * process consumes more CPU than this, an EXC_RESOURCE exception will be generated.
1962 proc_init_cpumon_params(void)
1964 if (!PE_parse_boot_argn("max_cpumon_percentage", &proc_max_cpumon_percentage
,
1965 sizeof (proc_max_cpumon_percentage
))) {
1966 proc_max_cpumon_percentage
= DEFAULT_CPUMON_PERCENTAGE
;
1969 if (proc_max_cpumon_percentage
> 100) {
1970 proc_max_cpumon_percentage
= 100;
1973 /* The interval should be specified in seconds. */
1974 if (!PE_parse_boot_argn("max_cpumon_interval", &proc_max_cpumon_interval
,
1975 sizeof (proc_max_cpumon_interval
))) {
1976 proc_max_cpumon_interval
= DEFAULT_CPUMON_INTERVAL
;
1979 proc_max_cpumon_interval
*= NSEC_PER_SEC
;
1983 * Currently supported configurations for CPU limits.
1985 * Policy | Deadline-based CPU limit | Percentage-based CPU limit
1986 * -------------------------------------+--------------------------+------------------------------
1987 * PROC_POLICY_RSRCACT_THROTTLE | ENOTSUP | Task-wide scope only
1988 * PROC_POLICY_RSRCACT_SUSPEND | Task-wide scope only | ENOTSUP
1989 * PROC_POLICY_RSRCACT_TERMINATE | Task-wide scope only | ENOTSUP
1990 * PROC_POLICY_RSRCACT_NOTIFY_KQ | Task-wide scope only | ENOTSUP
1991 * PROC_POLICY_RSRCACT_NOTIFY_EXC | ENOTSUP | Per-thread scope only
1993 * A deadline-based CPU limit is actually a simple wallclock timer - the requested action is performed
1994 * after the specified amount of wallclock time has elapsed.
1996 * A percentage-based CPU limit performs the requested action after the specified amount of actual CPU time
1997 * has been consumed -- regardless of how much wallclock time has elapsed -- by either the task as an
1998 * aggregate entity (so-called "Task-wide" or "Proc-wide" scope, whereby the CPU time consumed by all threads
1999 * in the task are added together), or by any one thread in the task (so-called "per-thread" scope).
2001 * We support either deadline != 0 OR percentage != 0, but not both. The original intention in having them
2002 * share an API was to use actual CPU time as the basis of the deadline-based limit (as in: perform an action
2003 * after I have used some amount of CPU time; this is different than the recurring percentage/interval model)
2004 * but the potential consumer of the API at the time was insisting on wallclock time instead.
2006 * Currently, requesting notification via an exception is the only way to get per-thread scope for a
2007 * CPU limit. All other types of notifications force task-wide scope for the limit.
2010 proc_set_task_ruse_cpu(task_t task
, uint32_t policy
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
,
2011 int cpumon_entitled
)
2017 * Enforce the matrix of supported configurations for policy, percentage, and deadline.
2020 // If no policy is explicitly given, the default is to throttle.
2021 case TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
:
2022 case TASK_POLICY_RESOURCE_ATTRIBUTE_THROTTLE
:
2025 scope
= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2027 case TASK_POLICY_RESOURCE_ATTRIBUTE_SUSPEND
:
2028 case TASK_POLICY_RESOURCE_ATTRIBUTE_TERMINATE
:
2029 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_KQ
:
2030 if (percentage
!= 0)
2032 scope
= TASK_RUSECPU_FLAGS_DEADLINE
;
2034 case TASK_POLICY_RESOURCE_ATTRIBUTE_NOTIFY_EXC
:
2037 scope
= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
2038 #ifdef CONFIG_NOMONITORS
2040 #endif /* CONFIG_NOMONITORS */
2047 if (task
!= current_task()) {
2048 task
->policy_ru_cpu_ext
= policy
;
2050 task
->policy_ru_cpu
= policy
;
2052 error
= task_set_cpuusage(task
, percentage
, interval
, deadline
, scope
, cpumon_entitled
);
2058 proc_clear_task_ruse_cpu(task_t task
, int cpumon_entitled
)
2062 void * bsdinfo
= NULL
;
2065 if (task
!= current_task()) {
2066 task
->policy_ru_cpu_ext
= TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT
;
2068 task
->policy_ru_cpu
= TASK_POLICY_RESOURCE_ATTRIBUTE_DEFAULT
;
2071 error
= task_clear_cpuusage_locked(task
, cpumon_entitled
);
2075 action
= task
->applied_ru_cpu
;
2076 if (task
->applied_ru_cpu_ext
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2078 task
->applied_ru_cpu_ext
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
2080 if (action
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2081 bsdinfo
= task
->bsd_info
;
2083 proc_restore_resource_actions(bsdinfo
, TASK_POLICY_CPU_RESOURCE_USAGE
, action
);
2094 /* used to apply resource limit related actions */
2096 task_apply_resource_actions(task_t task
, int type
)
2098 int action
= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
;
2099 void * bsdinfo
= NULL
;
2102 case TASK_POLICY_CPU_RESOURCE_USAGE
:
2104 case TASK_POLICY_WIREDMEM_RESOURCE_USAGE
:
2105 case TASK_POLICY_VIRTUALMEM_RESOURCE_USAGE
:
2106 case TASK_POLICY_DISK_RESOURCE_USAGE
:
2107 case TASK_POLICY_NETWORK_RESOURCE_USAGE
:
2108 case TASK_POLICY_POWER_RESOURCE_USAGE
:
2115 /* only cpu actions for now */
2118 if (task
->applied_ru_cpu_ext
== TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2120 task
->applied_ru_cpu_ext
= task
->policy_ru_cpu_ext
;
2121 action
= task
->applied_ru_cpu_ext
;
2123 action
= task
->applied_ru_cpu_ext
;
2126 if (action
!= TASK_POLICY_RESOURCE_ATTRIBUTE_NONE
) {
2127 bsdinfo
= task
->bsd_info
;
2129 proc_apply_resource_actions(bsdinfo
, TASK_POLICY_CPU_RESOURCE_USAGE
, action
);
2137 * XXX This API is somewhat broken; we support multiple simultaneous CPU limits, but the get/set API
2138 * only allows for one at a time. This means that if there is a per-thread limit active, the other
2139 * "scopes" will not be accessible via this API. We could change it to pass in the scope of interest
2140 * to the caller, and prefer that, but there's no need for that at the moment.
2143 task_get_cpuusage(task_t task
, uint8_t *percentagep
, uint64_t *intervalp
, uint64_t *deadlinep
, int *scope
)
2149 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) != 0) {
2150 *scope
= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
2151 *percentagep
= task
->rusage_cpu_perthr_percentage
;
2152 *intervalp
= task
->rusage_cpu_perthr_interval
;
2153 } else if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PROC_LIMIT
) != 0) {
2154 *scope
= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2155 *percentagep
= task
->rusage_cpu_percentage
;
2156 *intervalp
= task
->rusage_cpu_interval
;
2157 } else if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_DEADLINE
) != 0) {
2158 *scope
= TASK_RUSECPU_FLAGS_DEADLINE
;
2159 *deadlinep
= task
->rusage_cpu_deadline
;
2168 * Disable the CPU usage monitor for the task. Return value indicates
2169 * if the mechanism was actually enabled.
2172 task_disable_cpumon(task_t task
) {
2175 task_lock_assert_owned(task
);
2177 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
2178 return (KERN_INVALID_ARGUMENT
);
2181 #if CONFIG_TELEMETRY
2183 * Disable task-wide telemetry if it was ever enabled by the CPU usage
2184 * monitor's warning zone.
2186 telemetry_task_ctl_locked(current_task(), TF_CPUMON_WARNING
, 0);
2190 * Disable the monitor for the task, and propagate that change to each thread.
2192 task
->rusage_cpu_flags
&= ~(TASK_RUSECPU_FLAGS_PERTHR_LIMIT
| TASK_RUSECPU_FLAGS_FATAL_CPUMON
);
2193 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2194 set_astledger(thread
);
2196 task
->rusage_cpu_perthr_percentage
= 0;
2197 task
->rusage_cpu_perthr_interval
= 0;
2199 return (KERN_SUCCESS
);
2203 task_set_cpuusage(task_t task
, uint8_t percentage
, uint64_t interval
, uint64_t deadline
, int scope
, int cpumon_entitled
)
2206 uint64_t abstime
= 0;
2207 uint64_t limittime
= 0;
2209 lck_mtx_assert(&task
->lock
, LCK_MTX_ASSERT_OWNED
);
2211 /* By default, refill once per second */
2213 interval
= NSEC_PER_SEC
;
2215 if (percentage
!= 0) {
2216 if (scope
== TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
2217 boolean_t warn
= FALSE
;
2220 * A per-thread CPU limit on a task generates an exception
2221 * (LEDGER_ACTION_EXCEPTION) if any one thread in the task
2222 * exceeds the limit.
2225 if (percentage
== TASK_POLICY_CPUMON_DISABLE
) {
2226 if (cpumon_entitled
) {
2227 task_disable_cpumon(task
);
2232 * This task wishes to disable the CPU usage monitor, but it's
2233 * missing the required entitlement:
2234 * com.apple.private.kernel.override-cpumon
2236 * Instead, treat this as a request to reset its params
2237 * back to the defaults.
2240 percentage
= TASK_POLICY_CPUMON_DEFAULTS
;
2243 if (percentage
== TASK_POLICY_CPUMON_DEFAULTS
) {
2244 percentage
= proc_max_cpumon_percentage
;
2245 interval
= proc_max_cpumon_interval
;
2248 if (percentage
> 100) {
2253 * Passing in an interval of -1 means either:
2254 * - Leave the interval as-is, if there's already a per-thread
2256 * - Use the system default.
2258 if (interval
== -1ULL) {
2259 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
2260 interval
= task
->rusage_cpu_perthr_interval
;
2262 interval
= proc_max_cpumon_interval
;
2267 * Enforce global caps on CPU usage monitor here if the process is not
2268 * entitled to escape the global caps.
2270 if ((percentage
> proc_max_cpumon_percentage
) && (cpumon_entitled
== 0)) {
2272 percentage
= proc_max_cpumon_percentage
;
2275 if ((interval
> proc_max_cpumon_interval
) && (cpumon_entitled
== 0)) {
2277 interval
= proc_max_cpumon_interval
;
2282 char *procname
= (char *)"unknown";
2285 pid
= proc_selfpid();
2286 if (current_task()->bsd_info
!= NULL
) {
2287 procname
= proc_name_address(current_task()->bsd_info
);
2291 printf("process %s[%d] denied attempt to escape CPU monitor"
2292 " (missing required entitlement).\n", procname
, pid
);
2295 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PERTHR_LIMIT
;
2296 task
->rusage_cpu_perthr_percentage
= percentage
;
2297 task
->rusage_cpu_perthr_interval
= interval
;
2298 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
2299 set_astledger(thread
);
2301 } else if (scope
== TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
2303 * Currently, a proc-wide CPU limit always blocks if the limit is
2304 * exceeded (LEDGER_ACTION_BLOCK).
2306 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2307 task
->rusage_cpu_percentage
= percentage
;
2308 task
->rusage_cpu_interval
= interval
;
2310 limittime
= (interval
* percentage
) / 100;
2311 nanoseconds_to_absolutetime(limittime
, &abstime
);
2313 ledger_set_limit(task
->ledger
, task_ledgers
.cpu_time
, abstime
, 0);
2314 ledger_set_period(task
->ledger
, task_ledgers
.cpu_time
, interval
);
2315 ledger_set_action(task
->ledger
, task_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
2319 if (deadline
!= 0) {
2320 assert(scope
== TASK_RUSECPU_FLAGS_DEADLINE
);
2322 /* if already in use, cancel and wait for it to cleanout */
2323 if (task
->rusage_cpu_callt
!= NULL
) {
2325 thread_call_cancel_wait(task
->rusage_cpu_callt
);
2328 if (task
->rusage_cpu_callt
== NULL
) {
2329 task
->rusage_cpu_callt
= thread_call_allocate_with_priority(task_action_cpuusage
, (thread_call_param_t
)task
, THREAD_CALL_PRIORITY_KERNEL
);
2332 if (task
->rusage_cpu_callt
!= 0) {
2333 uint64_t save_abstime
= 0;
2335 task
->rusage_cpu_flags
|= TASK_RUSECPU_FLAGS_DEADLINE
;
2336 task
->rusage_cpu_deadline
= deadline
;
2338 nanoseconds_to_absolutetime(deadline
, &abstime
);
2339 save_abstime
= abstime
;
2340 clock_absolutetime_interval_to_deadline(save_abstime
, &abstime
);
2341 thread_call_enter_delayed(task
->rusage_cpu_callt
, abstime
);
2349 task_clear_cpuusage(task_t task
, int cpumon_entitled
)
2354 retval
= task_clear_cpuusage_locked(task
, cpumon_entitled
);
2361 task_clear_cpuusage_locked(task_t task
, int cpumon_entitled
)
2363 thread_call_t savecallt
;
2365 /* cancel percentage handling if set */
2366 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PROC_LIMIT
) {
2367 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_PROC_LIMIT
;
2368 ledger_set_limit(task
->ledger
, task_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
2369 task
->rusage_cpu_percentage
= 0;
2370 task
->rusage_cpu_interval
= 0;
2374 * Disable the CPU usage monitor.
2376 if (cpumon_entitled
) {
2377 task_disable_cpumon(task
);
2380 /* cancel deadline handling if set */
2381 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_DEADLINE
) {
2382 task
->rusage_cpu_flags
&= ~TASK_RUSECPU_FLAGS_DEADLINE
;
2383 if (task
->rusage_cpu_callt
!= 0) {
2384 savecallt
= task
->rusage_cpu_callt
;
2385 task
->rusage_cpu_callt
= NULL
;
2386 task
->rusage_cpu_deadline
= 0;
2388 thread_call_cancel_wait(savecallt
);
2389 thread_call_free(savecallt
);
2396 /* called by ledger unit to enforce action due to resource usage criteria being met */
2398 task_action_cpuusage(thread_call_param_t param0
, __unused thread_call_param_t param1
)
2400 task_t task
= (task_t
)param0
;
2401 (void)task_apply_resource_actions(task
, TASK_POLICY_CPU_RESOURCE_USAGE
);
2407 * Routines for taskwatch and pidbind
2412 * Routines for importance donation/inheritance/boosting
2416 task_importance_mark_donor(task_t task
, boolean_t donating
)
2418 #if IMPORTANCE_INHERITANCE
2419 task
->imp_donor
= (donating
? 1 : 0);
2420 #endif /* IMPORTANCE_INHERITANCE */
2424 task_importance_mark_receiver(task_t task
, boolean_t receiving
)
2426 #if IMPORTANCE_INHERITANCE
2428 assert(task
->task_imp_assertcnt
== 0);
2429 task
->imp_receiver
= 1; /* task can receive importance boost */
2430 task
->task_imp_assertcnt
= 0;
2431 task
->task_imp_externcnt
= 0;
2433 if (task
->task_imp_assertcnt
!= 0 || task
->task_imp_externcnt
!= 0)
2434 panic("disabling imp_receiver on task with pending boosts!");
2436 task
->imp_receiver
= 0;
2437 task
->task_imp_assertcnt
= 0;
2438 task
->task_imp_externcnt
= 0;
2440 #endif /* IMPORTANCE_INHERITANCE */
2444 #if IMPORTANCE_INHERITANCE
2447 task_update_boost_locked(task_t task
, boolean_t boost_active
)
2449 #if IMPORTANCE_DEBUG
2450 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_BOOST
, (boost_active
? IMP_BOOSTED
: IMP_UNBOOSTED
)) | DBG_FUNC_START
),
2451 proc_selfpid(), audit_token_pid_from_task(task
), trequested(task
, THREAD_NULL
), 0, 0);
2454 /* assert(boost_active ? task->requested_policy.t_boosted == 0 : task->requested_policy.t_boosted == 1); */
2456 proc_set_task_policy_locked(task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
, TASK_POLICY_BOOST
, boost_active
);
2458 task_policy_update_locked(task
, THREAD_NULL
);
2460 #if IMPORTANCE_DEBUG
2461 if (boost_active
== TRUE
){
2462 DTRACE_BOOST2(boost
, task_t
, task
, int, audit_token_pid_from_task(task
));
2464 DTRACE_BOOST2(unboost
, task_t
, task
, int, audit_token_pid_from_task(task
));
2466 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_BOOST
, (boost_active
? IMP_BOOSTED
: IMP_UNBOOSTED
)) | DBG_FUNC_END
),
2467 proc_selfpid(), audit_token_pid_from_task(task
),
2468 trequested(task
, THREAD_NULL
), tpending(task
, THREAD_NULL
), 0);
2473 * Check if this task should donate importance.
2475 * May be called without taking the task lock. In that case, donor status can change
2476 * so you must check only once for each donation event.
2479 task_is_importance_donor(task_t task
)
2481 return (task
->imp_donor
== 1 || task
->task_imp_assertcnt
> 0) ? TRUE
: FALSE
;
2485 * This routine may be called without holding task lock
2486 * since the value of imp_receiver can never be unset.
2489 task_is_importance_receiver(task_t task
)
2491 return (task
->imp_receiver
) ? TRUE
: FALSE
;
2495 * External importance assertions are managed by the process in userspace
2496 * Internal importance assertions are the responsibility of the kernel
2497 * Assertions are changed from internal to external via task_importance_externalize_assertion
2501 task_importance_hold_internal_assertion(task_t target_task
, uint32_t count
)
2505 task_lock(target_task
);
2506 rval
= task_importance_hold_assertion_locked(target_task
, TASK_POLICY_INTERNAL
, count
);
2507 task_unlock(target_task
);
2509 task_policy_update_complete_unlocked(target_task
, THREAD_NULL
);
2515 task_importance_hold_external_assertion(task_t target_task
, uint32_t count
)
2519 task_lock(target_task
);
2520 rval
= task_importance_hold_assertion_locked(target_task
, TASK_POLICY_EXTERNAL
, count
);
2521 task_unlock(target_task
);
2523 task_policy_update_complete_unlocked(target_task
, THREAD_NULL
);
2529 task_importance_drop_internal_assertion(task_t target_task
, uint32_t count
)
2533 task_lock(target_task
);
2534 rval
= task_importance_drop_assertion_locked(target_task
, TASK_POLICY_INTERNAL
, count
);
2535 task_unlock(target_task
);
2537 task_policy_update_complete_unlocked(target_task
, THREAD_NULL
);
2543 task_importance_drop_external_assertion(task_t target_task
, uint32_t count
)
2547 task_lock(target_task
);
2548 rval
= task_importance_drop_assertion_locked(target_task
, TASK_POLICY_EXTERNAL
, count
);
2549 task_unlock(target_task
);
2551 task_policy_update_complete_unlocked(target_task
, THREAD_NULL
);
2557 * Returns EOVERFLOW if an external assertion is taken when not holding an external boost.
2560 task_importance_hold_assertion_locked(task_t target_task
, int external
, uint32_t count
)
2562 boolean_t apply_boost
= FALSE
;
2565 assert(target_task
->imp_receiver
!= 0);
2567 #if IMPORTANCE_DEBUG
2568 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_ASSERTION
, (IMP_HOLD
| external
))) | DBG_FUNC_START
,
2569 proc_selfpid(), audit_token_pid_from_task(target_task
), target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
, 0);
2572 /* assert(target_task->task_imp_assertcnt >= target_task->task_imp_externcnt); */
2574 if (external
== TASK_POLICY_EXTERNAL
) {
2575 if (target_task
->task_imp_externcnt
== 0) {
2576 /* Only allowed to take a new boost assertion when holding an external boost */
2577 printf("BUG in process %s[%d]: it attempted to acquire a new boost assertion without holding an existing external assertion. "
2578 "(%d total, %d external)\n",
2579 proc_name_address(target_task
->bsd_info
), audit_token_pid_from_task(target_task
),
2580 target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
);
2584 target_task
->task_imp_assertcnt
+= count
;
2585 target_task
->task_imp_externcnt
+= count
;
2588 if (target_task
->task_imp_assertcnt
== 0)
2590 target_task
->task_imp_assertcnt
+= count
;
2593 if (apply_boost
== TRUE
)
2594 task_update_boost_locked(target_task
, TRUE
);
2596 #if IMPORTANCE_DEBUG
2597 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_ASSERTION
, (IMP_HOLD
| external
))) | DBG_FUNC_END
,
2598 proc_selfpid(), audit_token_pid_from_task(target_task
), target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
, 0);
2599 DTRACE_BOOST6(receive_internal_boost
, task_t
, target_task
, int, audit_token_pid_from_task(target_task
), task_t
, current_task(), int, proc_selfpid(), int, count
, int, target_task
->task_imp_assertcnt
);
2600 if (external
== TASK_POLICY_EXTERNAL
){
2601 DTRACE_BOOST5(receive_boost
, task_t
, target_task
, int, audit_token_pid_from_task(target_task
), int, proc_selfpid(), int, count
, int, target_task
->task_imp_externcnt
);
2609 * Returns EOVERFLOW if an external assertion is over-released.
2610 * Panics if an internal assertion is over-released.
2613 task_importance_drop_assertion_locked(task_t target_task
, int external
, uint32_t count
)
2617 assert(target_task
->imp_receiver
!= 0);
2619 #if IMPORTANCE_DEBUG
2620 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_ASSERTION
, (IMP_DROP
| external
))) | DBG_FUNC_START
,
2621 proc_selfpid(), audit_token_pid_from_task(target_task
), target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
, 0);
2624 /* assert(target_task->task_imp_assertcnt >= target_task->task_imp_externcnt); */
2626 if (external
== TASK_POLICY_EXTERNAL
) {
2628 if (count
<= target_task
->task_imp_externcnt
) {
2629 target_task
->task_imp_externcnt
-= count
;
2630 if (count
<= target_task
->task_imp_assertcnt
)
2631 target_task
->task_imp_assertcnt
-= count
;
2633 /* Process over-released its boost count */
2634 printf("BUG in process %s[%d]: over-released external boost assertions (%d total, %d external)\n",
2635 proc_name_address(target_task
->bsd_info
), audit_token_pid_from_task(target_task
),
2636 target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
);
2638 /* TODO: If count > 1, we should clear out as many external assertions as there are left. */
2643 if (count
<= target_task
->task_imp_assertcnt
) {
2644 target_task
->task_imp_assertcnt
-= count
;
2646 /* TODO: Turn this back into a panic <rdar://problem/12592649> */
2647 printf("Over-release of kernel-internal importance assertions for task %p (%s), dropping %d assertion(s) but task only has %d remaining (%d external).\n",
2649 (target_task
->bsd_info
== NULL
) ? "" : proc_name_address(target_task
->bsd_info
),
2651 target_task
->task_imp_assertcnt
,
2652 target_task
->task_imp_externcnt
);
2657 /* assert(target_task->task_imp_assertcnt >= target_task->task_imp_externcnt); */
2659 if (target_task
->task_imp_assertcnt
== 0 && ret
== 0)
2660 task_update_boost_locked(target_task
, FALSE
);
2662 #if IMPORTANCE_DEBUG
2663 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_ASSERTION
, (IMP_DROP
| external
))) | DBG_FUNC_END
,
2664 proc_selfpid(), audit_token_pid_from_task(target_task
), target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
, 0);
2665 if (external
== TASK_POLICY_EXTERNAL
) {
2666 DTRACE_BOOST4(drop_boost
, task_t
, target_task
, int, audit_token_pid_from_task(target_task
), int, count
, int, target_task
->task_imp_externcnt
);
2668 DTRACE_BOOST4(drop_internal_boost
, task_t
, target_task
, int, audit_token_pid_from_task(target_task
), int, count
, int, target_task
->task_imp_assertcnt
);
2674 /* Transfer an assertion to userspace responsibility */
2676 task_importance_externalize_assertion(task_t target_task
, uint32_t count
, __unused
int sender_pid
)
2678 assert(target_task
!= TASK_NULL
);
2679 assert(target_task
->imp_receiver
!= 0);
2681 task_lock(target_task
);
2683 #if IMPORTANCE_DEBUG
2684 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_ASSERTION
, IMP_EXTERN
)) | DBG_FUNC_START
,
2685 proc_selfpid(), audit_token_pid_from_task(target_task
), target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
, 0);
2688 /* assert(target_task->task_imp_assertcnt >= target_task->task_imp_externcnt + count); */
2690 target_task
->task_imp_externcnt
+= count
;
2692 #if IMPORTANCE_DEBUG
2693 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_ASSERTION
, IMP_EXTERN
)) | DBG_FUNC_END
,
2694 proc_selfpid(), audit_token_pid_from_task(target_task
), target_task
->task_imp_assertcnt
, target_task
->task_imp_externcnt
, 0);
2695 DTRACE_BOOST5(receive_boost
, task_t
, target_task
, int, audit_token_pid_from_task(target_task
),
2696 int, sender_pid
, int, count
, int, target_task
->task_imp_externcnt
);
2697 #endif /* IMPORTANCE_DEBUG */
2699 task_unlock(target_task
);
2705 #endif /* IMPORTANCE_INHERITANCE */
2708 task_hold_multiple_assertion(__imp_only task_t task
, __imp_only
uint32_t count
)
2710 #if IMPORTANCE_INHERITANCE
2711 assert(task
->imp_receiver
!= 0);
2713 task_importance_hold_internal_assertion(task
, count
);
2714 #endif /* IMPORTANCE_INHERITANCE */
2718 task_add_importance_watchport(__imp_only task_t task
, __imp_only __impdebug_only
int pid
, __imp_only mach_port_t port
, int *boostp
)
2722 __impdebug_only
int released_pid
= 0;
2724 #if IMPORTANCE_INHERITANCE
2725 task_t release_imp_task
= TASK_NULL
;
2727 if (task
->imp_receiver
== 0) {
2732 if (IP_VALID(port
) != 0) {
2736 * The port must have been marked tempowner already.
2737 * This also filters out ports whose receive rights
2738 * are already enqueued in a message, as you can't
2739 * change the right's destination once it's already
2742 if (port
->ip_tempowner
!= 0) {
2743 assert(port
->ip_impdonation
!= 0);
2745 boost
= port
->ip_impcount
;
2746 if (port
->ip_taskptr
!= 0) {
2748 * if this port is already bound to a task,
2749 * release the task reference and drop any
2750 * watchport-forwarded boosts
2752 release_imp_task
= port
->ip_imp_task
;
2755 /* mark the port is watching another task */
2756 port
->ip_taskptr
= 1;
2757 port
->ip_imp_task
= task
;
2758 task_reference(task
);
2762 if (release_imp_task
!= TASK_NULL
) {
2764 task_importance_drop_internal_assertion(release_imp_task
, boost
);
2765 released_pid
= audit_token_pid_from_task(release_imp_task
);
2766 task_deallocate(release_imp_task
);
2768 #if IMPORTANCE_DEBUG
2769 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
, (IMPORTANCE_CODE(IMP_WATCHPORT
, 0)) | DBG_FUNC_NONE
,
2770 proc_selfpid(), pid
, boost
, released_pid
, 0);
2771 #endif /* IMPORTANCE_DEBUG */
2773 #endif /* IMPORTANCE_INHERITANCE */
2781 * Routines for VM to query task importance
2786 * Order to be considered while estimating importance
2787 * for low memory notification and purging purgeable memory.
2789 #define TASK_IMPORTANCE_FOREGROUND 4
2790 #define TASK_IMPORTANCE_NOTDARWINBG 1
2794 * Checks if the task is already notified.
2796 * Condition: task lock should be held while calling this function.
2799 task_has_been_notified(task_t task
, int pressurelevel
)
2805 if (pressurelevel
== kVMPressureWarning
)
2806 return (task
->low_mem_notified_warn
? TRUE
: FALSE
);
2807 else if (pressurelevel
== kVMPressureCritical
)
2808 return (task
->low_mem_notified_critical
? TRUE
: FALSE
);
2815 * Checks if the task is used for purging.
2817 * Condition: task lock should be held while calling this function.
2820 task_used_for_purging(task_t task
, int pressurelevel
)
2826 if (pressurelevel
== kVMPressureWarning
)
2827 return (task
->purged_memory_warn
? TRUE
: FALSE
);
2828 else if (pressurelevel
== kVMPressureCritical
)
2829 return (task
->purged_memory_critical
? TRUE
: FALSE
);
2836 * Mark the task as notified with memory notification.
2838 * Condition: task lock should be held while calling this function.
2841 task_mark_has_been_notified(task_t task
, int pressurelevel
)
2847 if (pressurelevel
== kVMPressureWarning
)
2848 task
->low_mem_notified_warn
= 1;
2849 else if (pressurelevel
== kVMPressureCritical
)
2850 task
->low_mem_notified_critical
= 1;
2855 * Mark the task as purged.
2857 * Condition: task lock should be held while calling this function.
2860 task_mark_used_for_purging(task_t task
, int pressurelevel
)
2866 if (pressurelevel
== kVMPressureWarning
)
2867 task
->purged_memory_warn
= 1;
2868 else if (pressurelevel
== kVMPressureCritical
)
2869 task
->purged_memory_critical
= 1;
2874 * Mark the task eligible for low memory notification.
2876 * Condition: task lock should be held while calling this function.
2879 task_clear_has_been_notified(task_t task
, int pressurelevel
)
2885 if (pressurelevel
== kVMPressureWarning
)
2886 task
->low_mem_notified_warn
= 0;
2887 else if (pressurelevel
== kVMPressureCritical
)
2888 task
->low_mem_notified_critical
= 0;
2893 * Mark the task eligible for purging its purgeable memory.
2895 * Condition: task lock should be held while calling this function.
2898 task_clear_used_for_purging(task_t task
)
2904 task
->purged_memory_warn
= 0;
2905 task
->purged_memory_critical
= 0;
2910 * Estimate task importance for purging its purgeable memory
2911 * and low memory notification.
2913 * Importance is calculated in the following order of criteria:
2914 * -Task role : Background vs Foreground
2915 * -Boost status: Not boosted vs Boosted
2916 * -Darwin BG status.
2918 * Returns: Estimated task importance. Less important task will have lower
2919 * estimated importance.
2922 task_importance_estimate(task_t task
)
2924 int task_importance
= 0;
2930 if (proc_get_effective_task_policy(task
, TASK_POLICY_ROLE
) == TASK_FOREGROUND_APPLICATION
)
2931 task_importance
+= TASK_IMPORTANCE_FOREGROUND
;
2933 if (proc_get_effective_task_policy(task
, TASK_POLICY_DARWIN_BG
) == 0)
2934 task_importance
+= TASK_IMPORTANCE_NOTDARWINBG
;
2936 return task_importance
;