2 * Copyright (c) 1993-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Timer interrupt callout module.
32 #include <mach/mach_types.h>
34 #include <kern/clock.h>
36 #include <kern/processor.h>
37 #include <kern/timer_call.h>
38 #include <kern/timer_queue.h>
39 #include <kern/call_entry.h>
40 #include <kern/thread.h>
41 #include <kern/policy_internal.h>
43 #include <sys/kdebug.h>
51 #define TIMER_ASSERT 1
54 //#define TIMER_ASSERT 1
58 #define DBG(x...) kprintf("DBG: " x);
64 #define TIMER_KDEBUG_TRACE KERNEL_DEBUG_CONSTANT_IST
66 #define TIMER_KDEBUG_TRACE(x...)
70 lck_grp_t timer_call_lck_grp
;
71 lck_attr_t timer_call_lck_attr
;
72 lck_grp_attr_t timer_call_lck_grp_attr
;
74 lck_grp_t timer_longterm_lck_grp
;
75 lck_attr_t timer_longterm_lck_attr
;
76 lck_grp_attr_t timer_longterm_lck_grp_attr
;
78 /* Timer queue lock must be acquired with interrupts disabled (under splclock()) */
80 #define timer_queue_lock_spin(queue) \
81 lck_mtx_lock_spin_always(&queue->lock_data)
83 #define timer_queue_unlock(queue) \
84 lck_mtx_unlock_always(&queue->lock_data)
86 #define timer_queue_lock_spin(queue) (void)1
87 #define timer_queue_unlock(queue) (void)1
90 #define QUEUE(x) ((queue_t)(x))
91 #define MPQUEUE(x) ((mpqueue_head_t *)(x))
92 #define TIMER_CALL(x) ((timer_call_t)(x))
93 #define TCE(x) (&(x->call_entry))
95 * The longterm timer object is a global structure holding all timers
96 * beyond the short-term, local timer queue threshold. The boot processor
97 * is responsible for moving each timer to its local timer queue
98 * if and when that timer becomes due within the threshold.
101 /* Sentinel for "no time set": */
102 #define TIMER_LONGTERM_NONE EndOfAllTime
103 /* The default threadhold is the delta above which a timer is "long-term" */
104 #if defined(__x86_64__)
105 #define TIMER_LONGTERM_THRESHOLD (1ULL * NSEC_PER_SEC) /* 1 sec */
107 #define TIMER_LONGTERM_THRESHOLD TIMER_LONGTERM_NONE /* disabled */
111 * The scan limit throttles processing of the longterm queue.
112 * If the scan time exceeds this limit, we terminate, unlock
113 * and repeat after this same interval. This prevents unbounded holding of
114 * timer queue locks with interrupts masked.
116 #define TIMER_LONGTERM_SCAN_LIMIT (1ULL * NSEC_PER_MSEC) /* 1 msec */
117 /* Sentinel for "scan limit exceeded": */
118 #define TIMER_LONGTERM_SCAN_AGAIN 0
121 uint64_t interval
; /* longterm timer interval */
122 uint64_t margin
; /* fudge factor (10% of interval */
123 uint64_t deadline
; /* first/soonest longterm deadline */
124 uint64_t preempted
; /* sooner timer has pre-empted */
125 timer_call_t call
; /* first/soonest longterm timer call */
126 uint64_t deadline_set
; /* next timer set */
127 timer_call_data_t timer
; /* timer used by threshold management */
129 uint64_t scans
; /* num threshold timer scans */
130 uint64_t preempts
; /* num threshold reductions */
131 uint64_t latency
; /* average threshold latency */
132 uint64_t latency_min
; /* minimum threshold latency */
133 uint64_t latency_max
; /* maximum threshold latency */
137 mpqueue_head_t queue
; /* longterm timer list */
138 uint64_t enqueues
; /* num timers queued */
139 uint64_t dequeues
; /* num timers dequeued */
140 uint64_t escalates
; /* num timers becoming shortterm */
141 uint64_t scan_time
; /* last time the list was scanned */
142 threshold_t threshold
; /* longterm timer threshold */
143 uint64_t scan_limit
; /* maximum scan time */
144 uint64_t scan_pauses
; /* num scans exceeding time limit */
147 timer_longterm_t timer_longterm
= {
148 .scan_limit
= TIMER_LONGTERM_SCAN_LIMIT
,
151 static mpqueue_head_t
*timer_longterm_queue
= NULL
;
153 static void timer_longterm_init(void);
154 static void timer_longterm_callout(
155 timer_call_param_t p0
,
156 timer_call_param_t p1
);
157 extern void timer_longterm_scan(
158 timer_longterm_t
*tlp
,
160 static void timer_longterm_update(
161 timer_longterm_t
*tlp
);
162 static void timer_longterm_update_locked(
163 timer_longterm_t
*tlp
);
164 static mpqueue_head_t
* timer_longterm_enqueue_unlocked(
168 mpqueue_head_t
** old_queue
,
169 uint64_t soft_deadline
,
171 timer_call_param_t param1
,
172 uint32_t callout_flags
);
173 static void timer_longterm_dequeued_locked(
176 uint64_t past_deadline_timers
;
177 uint64_t past_deadline_deltas
;
178 uint64_t past_deadline_longest
;
179 uint64_t past_deadline_shortest
= ~0ULL;
180 enum {PAST_DEADLINE_TIMER_ADJUSTMENT_NS
= 10 * 1000};
182 uint64_t past_deadline_timer_adjustment
;
184 static boolean_t
timer_call_enter_internal(timer_call_t call
, timer_call_param_t param1
, uint64_t deadline
, uint64_t leeway
, uint32_t flags
, boolean_t ratelimited
);
185 boolean_t mach_timer_coalescing_enabled
= TRUE
;
187 mpqueue_head_t
*timer_call_enqueue_deadline_unlocked(
189 mpqueue_head_t
*queue
,
191 uint64_t soft_deadline
,
193 timer_call_param_t param1
,
196 mpqueue_head_t
*timer_call_dequeue_unlocked(
199 timer_coalescing_priority_params_t tcoal_prio_params
;
202 int32_t nc_tcl
, rt_tcl
, bg_tcl
, kt_tcl
, fp_tcl
, ts_tcl
, qos_tcl
;
203 #define TCOAL_PRIO_STAT(x) (x++)
205 #define TCOAL_PRIO_STAT(x)
209 timer_call_init_abstime(void)
213 timer_coalescing_priority_params_ns_t
* tcoal_prio_params_init
= timer_call_get_priority_params();
214 nanoseconds_to_absolutetime(PAST_DEADLINE_TIMER_ADJUSTMENT_NS
, &past_deadline_timer_adjustment
);
215 nanoseconds_to_absolutetime(tcoal_prio_params_init
->idle_entry_timer_processing_hdeadline_threshold_ns
, &result
);
216 tcoal_prio_params
.idle_entry_timer_processing_hdeadline_threshold_abstime
= (uint32_t)result
;
217 nanoseconds_to_absolutetime(tcoal_prio_params_init
->interrupt_timer_coalescing_ilat_threshold_ns
, &result
);
218 tcoal_prio_params
.interrupt_timer_coalescing_ilat_threshold_abstime
= (uint32_t)result
;
219 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_resort_threshold_ns
, &result
);
220 tcoal_prio_params
.timer_resort_threshold_abstime
= (uint32_t)result
;
221 tcoal_prio_params
.timer_coalesce_rt_shift
= tcoal_prio_params_init
->timer_coalesce_rt_shift
;
222 tcoal_prio_params
.timer_coalesce_bg_shift
= tcoal_prio_params_init
->timer_coalesce_bg_shift
;
223 tcoal_prio_params
.timer_coalesce_kt_shift
= tcoal_prio_params_init
->timer_coalesce_kt_shift
;
224 tcoal_prio_params
.timer_coalesce_fp_shift
= tcoal_prio_params_init
->timer_coalesce_fp_shift
;
225 tcoal_prio_params
.timer_coalesce_ts_shift
= tcoal_prio_params_init
->timer_coalesce_ts_shift
;
227 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_rt_ns_max
,
228 &tcoal_prio_params
.timer_coalesce_rt_abstime_max
);
229 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_bg_ns_max
,
230 &tcoal_prio_params
.timer_coalesce_bg_abstime_max
);
231 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_kt_ns_max
,
232 &tcoal_prio_params
.timer_coalesce_kt_abstime_max
);
233 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_fp_ns_max
,
234 &tcoal_prio_params
.timer_coalesce_fp_abstime_max
);
235 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_ts_ns_max
,
236 &tcoal_prio_params
.timer_coalesce_ts_abstime_max
);
238 for (i
= 0; i
< NUM_LATENCY_QOS_TIERS
; i
++) {
239 tcoal_prio_params
.latency_qos_scale
[i
] = tcoal_prio_params_init
->latency_qos_scale
[i
];
240 nanoseconds_to_absolutetime(tcoal_prio_params_init
->latency_qos_ns_max
[i
],
241 &tcoal_prio_params
.latency_qos_abstime_max
[i
]);
242 tcoal_prio_params
.latency_tier_rate_limited
[i
] = tcoal_prio_params_init
->latency_tier_rate_limited
[i
];
248 timer_call_init(void)
250 lck_attr_setdefault(&timer_call_lck_attr
);
251 lck_grp_attr_setdefault(&timer_call_lck_grp_attr
);
252 lck_grp_init(&timer_call_lck_grp
, "timer_call", &timer_call_lck_grp_attr
);
254 timer_longterm_init();
255 timer_call_init_abstime();
260 timer_call_queue_init(mpqueue_head_t
*queue
)
262 DBG("timer_call_queue_init(%p)\n", queue
);
263 mpqueue_init(queue
, &timer_call_lck_grp
, &timer_call_lck_attr
);
270 timer_call_func_t func
,
271 timer_call_param_t param0
)
273 DBG("timer_call_setup(%p,%p,%p)\n", call
, func
, param0
);
274 call_entry_setup(TCE(call
), func
, param0
);
275 simple_lock_init(&(call
)->lock
, 0);
276 call
->async_dequeue
= FALSE
;
279 static __inline__ mpqueue_head_t
*
280 timer_call_entry_dequeue(
283 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
285 if (!hw_lock_held((hw_lock_t
)&entry
->lock
))
286 panic("_call_entry_dequeue() "
287 "entry %p is not locked\n", entry
);
289 * XXX The queue lock is actually a mutex in spin mode
290 * but there's no way to test for it being held
291 * so we pretend it's a spinlock!
293 if (!hw_lock_held((hw_lock_t
)&old_queue
->lock_data
))
294 panic("_call_entry_dequeue() "
295 "queue %p is not locked\n", old_queue
);
297 call_entry_dequeue(TCE(entry
));
303 static __inline__ mpqueue_head_t
*
304 timer_call_entry_enqueue_deadline(
306 mpqueue_head_t
*queue
,
309 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
311 if (!hw_lock_held((hw_lock_t
)&entry
->lock
))
312 panic("_call_entry_enqueue_deadline() "
313 "entry %p is not locked\n", entry
);
314 /* XXX More lock pretense: */
315 if (!hw_lock_held((hw_lock_t
)&queue
->lock_data
))
316 panic("_call_entry_enqueue_deadline() "
317 "queue %p is not locked\n", queue
);
318 if (old_queue
!= NULL
&& old_queue
!= queue
)
319 panic("_call_entry_enqueue_deadline() "
320 "old_queue %p != queue", old_queue
);
322 call_entry_enqueue_deadline(TCE(entry
), QUEUE(queue
), deadline
);
324 /* For efficiency, track the earliest soft deadline on the queue, so that
325 * fuzzy decisions can be made without lock acquisitions.
327 timer_call_t thead
= (timer_call_t
)queue_first(&queue
->head
);
329 queue
->earliest_soft_deadline
= thead
->flags
& TIMER_CALL_RATELIMITED
? TCE(thead
)->deadline
: thead
->soft_deadline
;
340 static __inline__ mpqueue_head_t
*
341 timer_call_entry_dequeue(
344 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
346 call_entry_dequeue(TCE(entry
));
352 static __inline__ mpqueue_head_t
*
353 timer_call_entry_enqueue_deadline(
355 mpqueue_head_t
*queue
,
358 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
360 call_entry_enqueue_deadline(TCE(entry
), QUEUE(queue
), deadline
);
362 /* For efficiency, track the earliest soft deadline on the queue,
363 * so that fuzzy decisions can be made without lock acquisitions.
366 timer_call_t thead
= (timer_call_t
)queue_first(&queue
->head
);
367 queue
->earliest_soft_deadline
= thead
->flags
& TIMER_CALL_RATELIMITED
? TCE(thead
)->deadline
: thead
->soft_deadline
;
378 static __inline__
void
379 timer_call_entry_enqueue_tail(
381 mpqueue_head_t
*queue
)
383 call_entry_enqueue_tail(TCE(entry
), QUEUE(queue
));
389 * Remove timer entry from its queue but don't change the queue pointer
390 * and set the async_dequeue flag. This is locking case 2b.
392 static __inline__
void
393 timer_call_entry_dequeue_async(
396 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
399 (void) remque(qe(entry
));
400 entry
->async_dequeue
= TRUE
;
406 unsigned timer_call_enqueue_deadline_unlocked_async1
;
407 unsigned timer_call_enqueue_deadline_unlocked_async2
;
410 * Assumes call_entry and queues unlocked, interrupts disabled.
412 __inline__ mpqueue_head_t
*
413 timer_call_enqueue_deadline_unlocked(
415 mpqueue_head_t
*queue
,
417 uint64_t soft_deadline
,
419 timer_call_param_t param1
,
420 uint32_t callout_flags
)
422 call_entry_t entry
= TCE(call
);
423 mpqueue_head_t
*old_queue
;
425 DBG("timer_call_enqueue_deadline_unlocked(%p,%p,)\n", call
, queue
);
427 simple_lock(&call
->lock
);
429 old_queue
= MPQUEUE(entry
->queue
);
431 if (old_queue
!= NULL
) {
432 timer_queue_lock_spin(old_queue
);
433 if (call
->async_dequeue
) {
434 /* collision (1c): timer already dequeued, clear flag */
436 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
437 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
438 VM_KERNEL_UNSLIDE_OR_PERM(call
),
440 VM_KERNEL_UNSLIDE_OR_PERM(TCE(call
)->queue
),
442 timer_call_enqueue_deadline_unlocked_async1
++;
444 call
->async_dequeue
= FALSE
;
446 } else if (old_queue
!= queue
) {
447 timer_call_entry_dequeue(call
);
449 timer_call_enqueue_deadline_unlocked_async2
++;
452 if (old_queue
== timer_longterm_queue
)
453 timer_longterm_dequeued_locked(call
);
454 if (old_queue
!= queue
) {
455 timer_queue_unlock(old_queue
);
456 timer_queue_lock_spin(queue
);
459 timer_queue_lock_spin(queue
);
462 call
->soft_deadline
= soft_deadline
;
463 call
->flags
= callout_flags
;
464 TCE(call
)->param1
= param1
;
467 timer_call_entry_enqueue_deadline(call
, queue
, deadline
);
468 timer_queue_unlock(queue
);
469 simple_unlock(&call
->lock
);
475 unsigned timer_call_dequeue_unlocked_async1
;
476 unsigned timer_call_dequeue_unlocked_async2
;
479 timer_call_dequeue_unlocked(
482 call_entry_t entry
= TCE(call
);
483 mpqueue_head_t
*old_queue
;
485 DBG("timer_call_dequeue_unlocked(%p)\n", call
);
487 simple_lock(&call
->lock
);
488 old_queue
= MPQUEUE(entry
->queue
);
490 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
491 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
492 VM_KERNEL_UNSLIDE_OR_PERM(call
),
494 VM_KERNEL_UNSLIDE_OR_PERM(TCE(call
)->queue
),
497 if (old_queue
!= NULL
) {
498 timer_queue_lock_spin(old_queue
);
499 if (call
->async_dequeue
) {
500 /* collision (1c): timer already dequeued, clear flag */
502 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
503 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
504 VM_KERNEL_UNSLIDE_OR_PERM(call
),
506 VM_KERNEL_UNSLIDE_OR_PERM(TCE(call
)->queue
),
508 timer_call_dequeue_unlocked_async1
++;
510 call
->async_dequeue
= FALSE
;
513 timer_call_entry_dequeue(call
);
515 if (old_queue
== timer_longterm_queue
)
516 timer_longterm_dequeued_locked(call
);
517 timer_queue_unlock(old_queue
);
519 simple_unlock(&call
->lock
);
524 past_deadline_timer_handle(uint64_t deadline
, uint64_t ctime
)
526 uint64_t delta
= (ctime
- deadline
);
528 past_deadline_timers
++;
529 past_deadline_deltas
+= delta
;
530 if (delta
> past_deadline_longest
)
531 past_deadline_longest
= deadline
;
532 if (delta
< past_deadline_shortest
)
533 past_deadline_shortest
= delta
;
535 return (ctime
+ past_deadline_timer_adjustment
);
539 * Timer call entry locking model
540 * ==============================
542 * Timer call entries are linked on per-cpu timer queues which are protected
543 * by the queue lock and the call entry lock. The locking protocol is:
545 * 0) The canonical locking order is timer call entry followed by queue.
547 * 1) With only the entry lock held, entry.queue is valid:
548 * 1a) NULL: the entry is not queued, or
549 * 1b) non-NULL: this queue must be locked before the entry is modified.
550 * After locking the queue, the call.async_dequeue flag must be checked:
551 * 1c) TRUE: the entry was removed from the queue by another thread
552 * and we must NULL the entry.queue and reset this flag, or
553 * 1d) FALSE: (ie. queued), the entry can be manipulated.
555 * 2) If a queue lock is obtained first, the queue is stable:
556 * 2a) If a try-lock of a queued entry succeeds, the call can be operated on
558 * 2b) If a try-lock fails, it indicates that another thread is attempting
559 * to change the entry and move it to a different position in this queue
560 * or to different queue. The entry can be dequeued but it should not be
561 * operated upon since it is being changed. Furthermore, we don't null
562 * the entry.queue pointer (protected by the entry lock we don't own).
563 * Instead, we set the async_dequeue flag -- see (1c).
564 * 2c) Same as 2b but occurring when a longterm timer is matured.
565 * 3) A callout's parameters (deadline, flags, parameters, soft deadline &c.)
566 * should be manipulated with the appropriate timer queue lock held,
567 * to prevent queue traversal observations from observing inconsistent
568 * updates to an in-flight callout.
572 * Inlines timer_call_entry_dequeue() and timer_call_entry_enqueue_deadline()
573 * cast between pointer types (mpqueue_head_t *) and (queue_t) so that
574 * we can use the call_entry_dequeue() and call_entry_enqueue_deadline()
575 * methods to operate on timer_call structs as if they are call_entry structs.
576 * These structures are identical except for their queue head pointer fields.
578 * In the debug case, we assert that the timer call locking protocol
583 timer_call_enter_internal(
585 timer_call_param_t param1
,
589 boolean_t ratelimited
)
591 mpqueue_head_t
*queue
= NULL
;
592 mpqueue_head_t
*old_queue
;
596 uint64_t sdeadline
, ttd
;
598 assert(call
->call_entry
.func
!= NULL
);
601 sdeadline
= deadline
;
602 uint64_t ctime
= mach_absolute_time();
604 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
605 DECR_TIMER_ENTER
| DBG_FUNC_START
,
606 VM_KERNEL_UNSLIDE_OR_PERM(call
),
607 VM_KERNEL_ADDRHIDE(param1
), deadline
, flags
, 0);
609 urgency
= (flags
& TIMER_CALL_URGENCY_MASK
);
611 boolean_t slop_ratelimited
= FALSE
;
612 slop
= timer_call_slop(deadline
, ctime
, urgency
, current_thread(), &slop_ratelimited
);
614 if ((flags
& TIMER_CALL_LEEWAY
) != 0 && leeway
> slop
)
617 if (UINT64_MAX
- deadline
<= slop
) {
618 deadline
= UINT64_MAX
;
623 if (__improbable(deadline
< ctime
)) {
624 deadline
= past_deadline_timer_handle(deadline
, ctime
);
625 sdeadline
= deadline
;
628 if (ratelimited
|| slop_ratelimited
) {
629 flags
|= TIMER_CALL_RATELIMITED
;
631 flags
&= ~TIMER_CALL_RATELIMITED
;
634 ttd
= sdeadline
- ctime
;
636 DTRACE_TMR7(callout__create
, timer_call_func_t
, TCE(call
)->func
,
637 timer_call_param_t
, TCE(call
)->param0
, uint32_t, flags
,
638 (deadline
- sdeadline
),
639 (ttd
>> 32), (unsigned) (ttd
& 0xFFFFFFFF), call
);
642 /* Program timer callout parameters under the appropriate per-CPU or
643 * longterm queue lock. The callout may have been previously enqueued
644 * and in-flight on this or another timer queue.
646 if (!ratelimited
&& !slop_ratelimited
) {
647 queue
= timer_longterm_enqueue_unlocked(call
, ctime
, deadline
, &old_queue
, sdeadline
, ttd
, param1
, flags
);
651 queue
= timer_queue_assign(deadline
);
652 old_queue
= timer_call_enqueue_deadline_unlocked(call
, queue
, deadline
, sdeadline
, ttd
, param1
, flags
);
656 TCE(call
)->entry_time
= ctime
;
659 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
660 DECR_TIMER_ENTER
| DBG_FUNC_END
,
661 VM_KERNEL_UNSLIDE_OR_PERM(call
),
662 (old_queue
!= NULL
), deadline
, queue
->count
, 0);
666 return (old_queue
!= NULL
);
671 * return boolean indicating whether the call was previously queued.
679 return timer_call_enter_internal(call
, NULL
, deadline
, 0, flags
, FALSE
);
685 timer_call_param_t param1
,
689 return timer_call_enter_internal(call
, param1
, deadline
, 0, flags
, FALSE
);
693 timer_call_enter_with_leeway(
695 timer_call_param_t param1
,
699 boolean_t ratelimited
)
701 return timer_call_enter_internal(call
, param1
, deadline
, leeway
, flags
, ratelimited
);
705 timer_call_quantum_timer_enter(
707 timer_call_param_t param1
,
711 assert(call
->call_entry
.func
!= NULL
);
712 assert(ml_get_interrupts_enabled() == FALSE
);
714 uint32_t flags
= TIMER_CALL_SYS_CRITICAL
| TIMER_CALL_LOCAL
;
716 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
, DECR_TIMER_ENTER
| DBG_FUNC_START
,
717 VM_KERNEL_UNSLIDE_OR_PERM(call
),
718 VM_KERNEL_ADDRHIDE(param1
), deadline
,
721 if (__improbable(deadline
< ctime
)) {
722 deadline
= past_deadline_timer_handle(deadline
, ctime
);
725 uint64_t ttd
= deadline
- ctime
;
727 DTRACE_TMR7(callout__create
, timer_call_func_t
, TCE(call
)->func
,
728 timer_call_param_t
, TCE(call
)->param0
, uint32_t, flags
, 0,
729 (ttd
>> 32), (unsigned) (ttd
& 0xFFFFFFFF), call
);
732 quantum_timer_set_deadline(deadline
);
733 TCE(call
)->deadline
= deadline
;
734 TCE(call
)->param1
= param1
;
739 TCE(call
)->entry_time
= ctime
;
742 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
, DECR_TIMER_ENTER
| DBG_FUNC_END
,
743 VM_KERNEL_UNSLIDE_OR_PERM(call
),
751 timer_call_quantum_timer_cancel(
754 assert(ml_get_interrupts_enabled() == FALSE
);
756 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
757 DECR_TIMER_CANCEL
| DBG_FUNC_START
,
758 VM_KERNEL_UNSLIDE_OR_PERM(call
), TCE(call
)->deadline
,
761 TCE(call
)->deadline
= 0;
762 quantum_timer_set_deadline(0);
764 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
765 DECR_TIMER_CANCEL
| DBG_FUNC_END
,
766 VM_KERNEL_UNSLIDE_OR_PERM(call
), 0,
767 TCE(call
)->deadline
- mach_absolute_time(),
768 TCE(call
)->deadline
- TCE(call
)->entry_time
, 0);
771 DTRACE_TMR6(callout__cancel
, timer_call_func_t
, TCE(call
)->func
,
772 timer_call_param_t
, TCE(call
)->param0
, uint32_t, call
->flags
, 0,
773 (call
->ttd
>> 32), (unsigned) (call
->ttd
& 0xFFFFFFFF));
783 mpqueue_head_t
*old_queue
;
788 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
789 DECR_TIMER_CANCEL
| DBG_FUNC_START
,
790 VM_KERNEL_UNSLIDE_OR_PERM(call
),
791 TCE(call
)->deadline
, call
->soft_deadline
, call
->flags
, 0);
793 old_queue
= timer_call_dequeue_unlocked(call
);
795 if (old_queue
!= NULL
) {
796 timer_queue_lock_spin(old_queue
);
797 if (!queue_empty(&old_queue
->head
)) {
798 timer_queue_cancel(old_queue
, TCE(call
)->deadline
, CE(queue_first(&old_queue
->head
))->deadline
);
799 timer_call_t thead
= (timer_call_t
)queue_first(&old_queue
->head
);
800 old_queue
->earliest_soft_deadline
= thead
->flags
& TIMER_CALL_RATELIMITED
? TCE(thead
)->deadline
: thead
->soft_deadline
;
803 timer_queue_cancel(old_queue
, TCE(call
)->deadline
, UINT64_MAX
);
804 old_queue
->earliest_soft_deadline
= UINT64_MAX
;
806 timer_queue_unlock(old_queue
);
808 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
809 DECR_TIMER_CANCEL
| DBG_FUNC_END
,
810 VM_KERNEL_UNSLIDE_OR_PERM(call
),
811 VM_KERNEL_UNSLIDE_OR_PERM(old_queue
),
812 TCE(call
)->deadline
- mach_absolute_time(),
813 TCE(call
)->deadline
- TCE(call
)->entry_time
, 0);
817 DTRACE_TMR6(callout__cancel
, timer_call_func_t
, TCE(call
)->func
,
818 timer_call_param_t
, TCE(call
)->param0
, uint32_t, call
->flags
, 0,
819 (call
->ttd
>> 32), (unsigned) (call
->ttd
& 0xFFFFFFFF));
822 return (old_queue
!= NULL
);
825 static uint32_t timer_queue_shutdown_lock_skips
;
826 static uint32_t timer_queue_shutdown_discarded
;
829 timer_queue_shutdown(
830 mpqueue_head_t
*queue
)
833 mpqueue_head_t
*new_queue
;
837 DBG("timer_queue_shutdown(%p)\n", queue
);
841 /* Note comma operator in while expression re-locking each iteration */
842 while ((void)timer_queue_lock_spin(queue
), !queue_empty(&queue
->head
)) {
843 call
= TIMER_CALL(queue_first(&queue
->head
));
845 if (!simple_lock_try(&call
->lock
)) {
847 * case (2b) lock order inversion, dequeue and skip
848 * Don't change the call_entry queue back-pointer
849 * but set the async_dequeue field.
851 timer_queue_shutdown_lock_skips
++;
852 timer_call_entry_dequeue_async(call
);
854 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
855 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
856 VM_KERNEL_UNSLIDE_OR_PERM(call
),
858 VM_KERNEL_UNSLIDE_OR_PERM(TCE(call
)->queue
),
861 timer_queue_unlock(queue
);
865 boolean_t call_local
= ((call
->flags
& TIMER_CALL_LOCAL
) != 0);
867 /* remove entry from old queue */
868 timer_call_entry_dequeue(call
);
869 timer_queue_unlock(queue
);
871 if (call_local
== FALSE
) {
872 /* and queue it on new, discarding LOCAL timers */
873 new_queue
= timer_queue_assign(TCE(call
)->deadline
);
874 timer_queue_lock_spin(new_queue
);
875 timer_call_entry_enqueue_deadline(
876 call
, new_queue
, TCE(call
)->deadline
);
877 timer_queue_unlock(new_queue
);
879 timer_queue_shutdown_discarded
++;
882 assert(call_local
== FALSE
);
883 simple_unlock(&call
->lock
);
886 timer_queue_unlock(queue
);
892 quantum_timer_expire(
895 processor_t processor
= current_processor();
896 timer_call_t call
= TIMER_CALL(&(processor
->quantum_timer
));
898 if (__improbable(TCE(call
)->deadline
> deadline
))
899 panic("CPU quantum timer deadlin out of sync with timer call deadline");
901 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
902 DECR_TIMER_EXPIRE
| DBG_FUNC_NONE
,
903 VM_KERNEL_UNSLIDE_OR_PERM(call
),
906 TCE(call
)->entry_time
, 0);
908 timer_call_func_t func
= TCE(call
)->func
;
909 timer_call_param_t param0
= TCE(call
)->param0
;
910 timer_call_param_t param1
= TCE(call
)->param1
;
912 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
913 DECR_TIMER_CALLOUT
| DBG_FUNC_START
,
914 VM_KERNEL_UNSLIDE_OR_PERM(call
), VM_KERNEL_UNSLIDE(func
),
915 VM_KERNEL_ADDRHIDE(param0
),
916 VM_KERNEL_ADDRHIDE(param1
),
920 DTRACE_TMR7(callout__start
, timer_call_func_t
, func
,
921 timer_call_param_t
, param0
, unsigned, call
->flags
,
922 0, (call
->ttd
>> 32),
923 (unsigned) (call
->ttd
& 0xFFFFFFFF), call
);
925 (*func
)(param0
, param1
);
927 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
928 DECR_TIMER_CALLOUT
| DBG_FUNC_END
,
929 VM_KERNEL_UNSLIDE_OR_PERM(call
), VM_KERNEL_UNSLIDE(func
),
930 VM_KERNEL_ADDRHIDE(param0
),
931 VM_KERNEL_ADDRHIDE(param1
),
935 static uint32_t timer_queue_expire_lock_skips
;
937 timer_queue_expire_with_options(
938 mpqueue_head_t
*queue
,
942 timer_call_t call
= NULL
;
943 uint32_t tc_iterations
= 0;
944 DBG("timer_queue_expire(%p,)\n", queue
);
946 uint64_t cur_deadline
= deadline
;
947 timer_queue_lock_spin(queue
);
949 while (!queue_empty(&queue
->head
)) {
950 /* Upon processing one or more timer calls, refresh the
951 * deadline to account for time elapsed in the callout
953 if (++tc_iterations
> 1)
954 cur_deadline
= mach_absolute_time();
957 call
= TIMER_CALL(queue_first(&queue
->head
));
959 if (call
->soft_deadline
<= cur_deadline
) {
960 timer_call_func_t func
;
961 timer_call_param_t param0
, param1
;
963 TCOAL_DEBUG(0xDDDD0000, queue
->earliest_soft_deadline
, call
->soft_deadline
, 0, 0, 0);
964 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
965 DECR_TIMER_EXPIRE
| DBG_FUNC_NONE
,
966 VM_KERNEL_UNSLIDE_OR_PERM(call
),
969 TCE(call
)->entry_time
, 0);
971 if ((call
->flags
& TIMER_CALL_RATELIMITED
) &&
972 (TCE(call
)->deadline
> cur_deadline
)) {
977 if (!simple_lock_try(&call
->lock
)) {
978 /* case (2b) lock inversion, dequeue and skip */
979 timer_queue_expire_lock_skips
++;
980 timer_call_entry_dequeue_async(call
);
985 timer_call_entry_dequeue(call
);
987 func
= TCE(call
)->func
;
988 param0
= TCE(call
)->param0
;
989 param1
= TCE(call
)->param1
;
991 simple_unlock(&call
->lock
);
992 timer_queue_unlock(queue
);
994 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
995 DECR_TIMER_CALLOUT
| DBG_FUNC_START
,
996 VM_KERNEL_UNSLIDE_OR_PERM(call
), VM_KERNEL_UNSLIDE(func
),
997 VM_KERNEL_ADDRHIDE(param0
),
998 VM_KERNEL_ADDRHIDE(param1
),
1002 DTRACE_TMR7(callout__start
, timer_call_func_t
, func
,
1003 timer_call_param_t
, param0
, unsigned, call
->flags
,
1004 0, (call
->ttd
>> 32),
1005 (unsigned) (call
->ttd
& 0xFFFFFFFF), call
);
1007 /* Maintain time-to-deadline in per-processor data
1008 * structure for thread wakeup deadline statistics.
1010 uint64_t *ttdp
= &(PROCESSOR_DATA(current_processor(), timer_call_ttd
));
1012 (*func
)(param0
, param1
);
1015 DTRACE_TMR4(callout__end
, timer_call_func_t
, func
,
1016 param0
, param1
, call
);
1019 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1020 DECR_TIMER_CALLOUT
| DBG_FUNC_END
,
1021 VM_KERNEL_UNSLIDE_OR_PERM(call
), VM_KERNEL_UNSLIDE(func
),
1022 VM_KERNEL_ADDRHIDE(param0
),
1023 VM_KERNEL_ADDRHIDE(param1
),
1026 timer_queue_lock_spin(queue
);
1028 if (__probable(rescan
== FALSE
)) {
1031 int64_t skew
= TCE(call
)->deadline
- call
->soft_deadline
;
1032 assert(TCE(call
)->deadline
>= call
->soft_deadline
);
1034 /* DRK: On a latency quality-of-service level change,
1035 * re-sort potentially rate-limited timers. The platform
1036 * layer determines which timers require
1037 * this. In the absence of the per-callout
1038 * synchronization requirement, a global resort could
1039 * be more efficient. The re-sort effectively
1040 * annuls all timer adjustments, i.e. the "soft
1041 * deadline" is the sort key.
1044 if (timer_resort_threshold(skew
)) {
1045 if (__probable(simple_lock_try(&call
->lock
))) {
1046 timer_call_entry_dequeue(call
);
1047 timer_call_entry_enqueue_deadline(call
, queue
, call
->soft_deadline
);
1048 simple_unlock(&call
->lock
);
1053 call
= TIMER_CALL(queue_next(qe(call
)));
1054 if (queue_end(&queue
->head
, qe(call
)))
1061 if (!queue_empty(&queue
->head
)) {
1062 call
= TIMER_CALL(queue_first(&queue
->head
));
1063 cur_deadline
= TCE(call
)->deadline
;
1064 queue
->earliest_soft_deadline
= (call
->flags
& TIMER_CALL_RATELIMITED
) ? TCE(call
)->deadline
: call
->soft_deadline
;
1066 queue
->earliest_soft_deadline
= cur_deadline
= UINT64_MAX
;
1069 timer_queue_unlock(queue
);
1071 return (cur_deadline
);
1076 mpqueue_head_t
*queue
,
1079 return timer_queue_expire_with_options(queue
, deadline
, FALSE
);
1082 extern int serverperfmode
;
1083 static uint32_t timer_queue_migrate_lock_skips
;
1085 * timer_queue_migrate() is called by timer_queue_migrate_cpu()
1086 * to move timer requests from the local processor (queue_from)
1087 * to a target processor's (queue_to).
1090 timer_queue_migrate(mpqueue_head_t
*queue_from
, mpqueue_head_t
*queue_to
)
1093 timer_call_t head_to
;
1094 int timers_migrated
= 0;
1096 DBG("timer_queue_migrate(%p,%p)\n", queue_from
, queue_to
);
1098 assert(!ml_get_interrupts_enabled());
1099 assert(queue_from
!= queue_to
);
1101 if (serverperfmode
) {
1103 * if we're running a high end server
1104 * avoid migrations... they add latency
1105 * and don't save us power under typical
1112 * Take both local (from) and target (to) timer queue locks while
1113 * moving the timers from the local queue to the target processor.
1114 * We assume that the target is always the boot processor.
1115 * But only move if all of the following is true:
1116 * - the target queue is non-empty
1117 * - the local queue is non-empty
1118 * - the local queue's first deadline is later than the target's
1119 * - the local queue contains no non-migrateable "local" call
1120 * so that we need not have the target resync.
1123 timer_queue_lock_spin(queue_to
);
1125 head_to
= TIMER_CALL(queue_first(&queue_to
->head
));
1126 if (queue_empty(&queue_to
->head
)) {
1127 timers_migrated
= -1;
1131 timer_queue_lock_spin(queue_from
);
1133 if (queue_empty(&queue_from
->head
)) {
1134 timers_migrated
= -2;
1138 call
= TIMER_CALL(queue_first(&queue_from
->head
));
1139 if (TCE(call
)->deadline
< TCE(head_to
)->deadline
) {
1140 timers_migrated
= 0;
1144 /* perform scan for non-migratable timers */
1146 if (call
->flags
& TIMER_CALL_LOCAL
) {
1147 timers_migrated
= -3;
1150 call
= TIMER_CALL(queue_next(qe(call
)));
1151 } while (!queue_end(&queue_from
->head
, qe(call
)));
1153 /* migration loop itself -- both queues are locked */
1154 while (!queue_empty(&queue_from
->head
)) {
1155 call
= TIMER_CALL(queue_first(&queue_from
->head
));
1156 if (!simple_lock_try(&call
->lock
)) {
1157 /* case (2b) lock order inversion, dequeue only */
1159 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1160 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
1161 VM_KERNEL_UNSLIDE_OR_PERM(call
),
1162 VM_KERNEL_UNSLIDE_OR_PERM(TCE(call
)->queue
),
1163 VM_KERNEL_UNSLIDE_OR_PERM(call
->lock
.interlock
.lock_data
),
1166 timer_queue_migrate_lock_skips
++;
1167 timer_call_entry_dequeue_async(call
);
1170 timer_call_entry_dequeue(call
);
1171 timer_call_entry_enqueue_deadline(
1172 call
, queue_to
, TCE(call
)->deadline
);
1174 simple_unlock(&call
->lock
);
1176 queue_from
->earliest_soft_deadline
= UINT64_MAX
;
1178 timer_queue_unlock(queue_from
);
1180 timer_queue_unlock(queue_to
);
1182 return timers_migrated
;
1186 timer_queue_trace_cpu(int ncpu
)
1188 timer_call_nosync_cpu(
1190 (void(*)(void *))timer_queue_trace
,
1191 (void*) timer_queue_cpu(ncpu
));
1196 mpqueue_head_t
*queue
)
1205 timer_queue_lock_spin(queue
);
1207 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1208 DECR_TIMER_QUEUE
| DBG_FUNC_START
,
1209 queue
->count
, mach_absolute_time(), 0, 0, 0);
1211 if (!queue_empty(&queue
->head
)) {
1212 call
= TIMER_CALL(queue_first(&queue
->head
));
1214 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1215 DECR_TIMER_QUEUE
| DBG_FUNC_NONE
,
1216 call
->soft_deadline
,
1217 TCE(call
)->deadline
,
1218 TCE(call
)->entry_time
,
1219 VM_KERNEL_UNSLIDE(TCE(call
)->func
),
1221 call
= TIMER_CALL(queue_next(qe(call
)));
1222 } while (!queue_end(&queue
->head
, qe(call
)));
1225 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1226 DECR_TIMER_QUEUE
| DBG_FUNC_END
,
1227 queue
->count
, mach_absolute_time(), 0, 0, 0);
1229 timer_queue_unlock(queue
);
1234 timer_longterm_dequeued_locked(timer_call_t call
)
1236 timer_longterm_t
*tlp
= &timer_longterm
;
1239 if (call
== tlp
->threshold
.call
)
1240 tlp
->threshold
.call
= NULL
;
1244 * Place a timer call in the longterm list
1245 * and adjust the next timer callout deadline if the new timer is first.
1248 timer_longterm_enqueue_unlocked(timer_call_t call
,
1251 mpqueue_head_t
**old_queue
,
1252 uint64_t soft_deadline
,
1254 timer_call_param_t param1
,
1255 uint32_t callout_flags
)
1257 timer_longterm_t
*tlp
= &timer_longterm
;
1258 boolean_t update_required
= FALSE
;
1259 uint64_t longterm_threshold
;
1261 longterm_threshold
= now
+ tlp
->threshold
.interval
;
1264 * Return NULL without doing anything if:
1265 * - this timer is local, or
1266 * - the longterm mechanism is disabled, or
1267 * - this deadline is too short.
1269 if ((callout_flags
& TIMER_CALL_LOCAL
) != 0 ||
1270 (tlp
->threshold
.interval
== TIMER_LONGTERM_NONE
) ||
1271 (deadline
<= longterm_threshold
))
1275 * Remove timer from its current queue, if any.
1277 *old_queue
= timer_call_dequeue_unlocked(call
);
1280 * Lock the longterm queue, queue timer and determine
1281 * whether an update is necessary.
1283 assert(!ml_get_interrupts_enabled());
1284 simple_lock(&call
->lock
);
1285 timer_queue_lock_spin(timer_longterm_queue
);
1286 TCE(call
)->deadline
= deadline
;
1287 TCE(call
)->param1
= param1
;
1289 call
->soft_deadline
= soft_deadline
;
1290 call
->flags
= callout_flags
;
1291 timer_call_entry_enqueue_tail(call
, timer_longterm_queue
);
1296 * We'll need to update the currently set threshold timer
1297 * if the new deadline is sooner and no sooner update is in flight.
1299 if (deadline
< tlp
->threshold
.deadline
&&
1300 deadline
< tlp
->threshold
.preempted
) {
1301 tlp
->threshold
.preempted
= deadline
;
1302 tlp
->threshold
.call
= call
;
1303 update_required
= TRUE
;
1305 timer_queue_unlock(timer_longterm_queue
);
1306 simple_unlock(&call
->lock
);
1308 if (update_required
) {
1310 * Note: this call expects that calling the master cpu
1311 * alone does not involve locking the topo lock.
1313 timer_call_nosync_cpu(
1315 (void (*)(void *)) timer_longterm_update
,
1319 return timer_longterm_queue
;
1323 * Scan for timers below the longterm threshold.
1324 * Move these to the local timer queue (of the boot processor on which the
1325 * calling thread is running).
1326 * Both the local (boot) queue and the longterm queue are locked.
1327 * The scan is similar to the timer migrate sequence but is performed by
1328 * successively examining each timer on the longterm queue:
1329 * - if within the short-term threshold
1330 * - enter on the local queue (unless being deleted),
1332 * - if sooner, deadline becomes the next threshold deadline.
1333 * The total scan time is limited to TIMER_LONGTERM_SCAN_LIMIT. Should this be
1334 * exceeded, we abort and reschedule again so that we don't shut others from
1335 * the timer queues. Longterm timers firing late is not critical.
1338 timer_longterm_scan(timer_longterm_t
*tlp
,
1339 uint64_t time_start
)
1345 uint64_t time_limit
= time_start
+ tlp
->scan_limit
;
1346 mpqueue_head_t
*timer_master_queue
;
1348 assert(!ml_get_interrupts_enabled());
1349 assert(cpu_number() == master_cpu
);
1351 if (tlp
->threshold
.interval
!= TIMER_LONGTERM_NONE
)
1352 threshold
= time_start
+ tlp
->threshold
.interval
;
1354 tlp
->threshold
.deadline
= TIMER_LONGTERM_NONE
;
1355 tlp
->threshold
.call
= NULL
;
1357 if (queue_empty(&timer_longterm_queue
->head
))
1360 timer_master_queue
= timer_queue_cpu(master_cpu
);
1361 timer_queue_lock_spin(timer_master_queue
);
1363 qe
= queue_first(&timer_longterm_queue
->head
);
1364 while (!queue_end(&timer_longterm_queue
->head
, qe
)) {
1365 call
= TIMER_CALL(qe
);
1366 deadline
= call
->soft_deadline
;
1367 qe
= queue_next(qe
);
1368 if (!simple_lock_try(&call
->lock
)) {
1369 /* case (2c) lock order inversion, dequeue only */
1371 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1372 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
1373 VM_KERNEL_UNSLIDE_OR_PERM(call
),
1374 VM_KERNEL_UNSLIDE_OR_PERM(TCE(call
)->queue
),
1375 VM_KERNEL_UNSLIDE_OR_PERM(call
->lock
.interlock
.lock_data
),
1378 timer_call_entry_dequeue_async(call
);
1381 if (deadline
< threshold
) {
1383 * This timer needs moving (escalating)
1384 * to the local (boot) processor's queue.
1387 if (deadline
< time_start
)
1388 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1389 DECR_TIMER_OVERDUE
| DBG_FUNC_NONE
,
1390 VM_KERNEL_UNSLIDE_OR_PERM(call
),
1396 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1397 DECR_TIMER_ESCALATE
| DBG_FUNC_NONE
,
1398 VM_KERNEL_UNSLIDE_OR_PERM(call
),
1399 TCE(call
)->deadline
,
1400 TCE(call
)->entry_time
,
1401 VM_KERNEL_UNSLIDE(TCE(call
)->func
),
1404 timer_call_entry_dequeue(call
);
1405 timer_call_entry_enqueue_deadline(
1406 call
, timer_master_queue
, TCE(call
)->deadline
);
1408 * A side-effect of the following call is to update
1409 * the actual hardware deadline if required.
1411 (void) timer_queue_assign(deadline
);
1413 if (deadline
< tlp
->threshold
.deadline
) {
1414 tlp
->threshold
.deadline
= deadline
;
1415 tlp
->threshold
.call
= call
;
1418 simple_unlock(&call
->lock
);
1420 /* Abort scan if we're taking too long. */
1421 if (mach_absolute_time() > time_limit
) {
1422 tlp
->threshold
.deadline
= TIMER_LONGTERM_SCAN_AGAIN
;
1424 DBG("timer_longterm_scan() paused %llu, qlen: %llu\n",
1425 time_limit
, tlp
->queue
.count
);
1430 timer_queue_unlock(timer_master_queue
);
1434 timer_longterm_callout(timer_call_param_t p0
, __unused timer_call_param_t p1
)
1436 timer_longterm_t
*tlp
= (timer_longterm_t
*) p0
;
1438 timer_longterm_update(tlp
);
1442 timer_longterm_update_locked(timer_longterm_t
*tlp
)
1446 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1447 DECR_TIMER_UPDATE
| DBG_FUNC_START
,
1448 VM_KERNEL_UNSLIDE_OR_PERM(&tlp
->queue
),
1449 tlp
->threshold
.deadline
,
1450 tlp
->threshold
.preempted
,
1451 tlp
->queue
.count
, 0);
1453 tlp
->scan_time
= mach_absolute_time();
1454 if (tlp
->threshold
.preempted
!= TIMER_LONGTERM_NONE
) {
1455 tlp
->threshold
.preempts
++;
1456 tlp
->threshold
.deadline
= tlp
->threshold
.preempted
;
1457 tlp
->threshold
.preempted
= TIMER_LONGTERM_NONE
;
1459 * Note: in the unlikely event that a pre-empted timer has
1460 * itself been cancelled, we'll simply re-scan later at the
1461 * time of the preempted/cancelled timer.
1464 tlp
->threshold
.scans
++;
1467 * Maintain a moving average of our wakeup latency.
1468 * Clamp latency to 0 and ignore above threshold interval.
1470 if (tlp
->scan_time
> tlp
->threshold
.deadline_set
)
1471 latency
= tlp
->scan_time
- tlp
->threshold
.deadline_set
;
1474 if (latency
< tlp
->threshold
.interval
) {
1475 tlp
->threshold
.latency_min
=
1476 MIN(tlp
->threshold
.latency_min
, latency
);
1477 tlp
->threshold
.latency_max
=
1478 MAX(tlp
->threshold
.latency_max
, latency
);
1479 tlp
->threshold
.latency
=
1480 (tlp
->threshold
.latency
*99 + latency
) / 100;
1483 timer_longterm_scan(tlp
, tlp
->scan_time
);
1486 tlp
->threshold
.deadline_set
= tlp
->threshold
.deadline
;
1487 /* The next deadline timer to be set is adjusted */
1488 if (tlp
->threshold
.deadline
!= TIMER_LONGTERM_NONE
&&
1489 tlp
->threshold
.deadline
!= TIMER_LONGTERM_SCAN_AGAIN
) {
1490 tlp
->threshold
.deadline_set
-= tlp
->threshold
.margin
;
1491 tlp
->threshold
.deadline_set
-= tlp
->threshold
.latency
;
1494 /* Throttle next scan time */
1495 uint64_t scan_clamp
= mach_absolute_time() + tlp
->scan_limit
;
1496 if (tlp
->threshold
.deadline_set
< scan_clamp
)
1497 tlp
->threshold
.deadline_set
= scan_clamp
;
1499 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1500 DECR_TIMER_UPDATE
| DBG_FUNC_END
,
1501 VM_KERNEL_UNSLIDE_OR_PERM(&tlp
->queue
),
1502 tlp
->threshold
.deadline
,
1503 tlp
->threshold
.scans
,
1504 tlp
->queue
.count
, 0);
1508 timer_longterm_update(timer_longterm_t
*tlp
)
1510 spl_t s
= splclock();
1512 timer_queue_lock_spin(timer_longterm_queue
);
1514 if (cpu_number() != master_cpu
)
1515 panic("timer_longterm_update_master() on non-boot cpu");
1517 timer_longterm_update_locked(tlp
);
1519 if (tlp
->threshold
.deadline
!= TIMER_LONGTERM_NONE
)
1521 &tlp
->threshold
.timer
,
1522 tlp
->threshold
.deadline_set
,
1523 TIMER_CALL_LOCAL
| TIMER_CALL_SYS_CRITICAL
);
1525 timer_queue_unlock(timer_longterm_queue
);
1530 timer_longterm_init(void)
1533 timer_longterm_t
*tlp
= &timer_longterm
;
1535 DBG("timer_longterm_init() tlp: %p, queue: %p\n", tlp
, &tlp
->queue
);
1538 * Set the longterm timer threshold. Defaults to TIMER_LONGTERM_THRESHOLD
1539 * or TIMER_LONGTERM_NONE (disabled) for server;
1540 * overridden longterm boot-arg
1542 tlp
->threshold
.interval
= serverperfmode
? TIMER_LONGTERM_NONE
1543 : TIMER_LONGTERM_THRESHOLD
;
1544 if (PE_parse_boot_argn("longterm", &longterm
, sizeof (longterm
))) {
1545 tlp
->threshold
.interval
= (longterm
== 0) ?
1546 TIMER_LONGTERM_NONE
:
1547 longterm
* NSEC_PER_MSEC
;
1549 if (tlp
->threshold
.interval
!= TIMER_LONGTERM_NONE
) {
1550 printf("Longterm timer threshold: %llu ms\n",
1551 tlp
->threshold
.interval
/ NSEC_PER_MSEC
);
1552 kprintf("Longterm timer threshold: %llu ms\n",
1553 tlp
->threshold
.interval
/ NSEC_PER_MSEC
);
1554 nanoseconds_to_absolutetime(tlp
->threshold
.interval
,
1555 &tlp
->threshold
.interval
);
1556 tlp
->threshold
.margin
= tlp
->threshold
.interval
/ 10;
1557 tlp
->threshold
.latency_min
= EndOfAllTime
;
1558 tlp
->threshold
.latency_max
= 0;
1561 tlp
->threshold
.preempted
= TIMER_LONGTERM_NONE
;
1562 tlp
->threshold
.deadline
= TIMER_LONGTERM_NONE
;
1564 lck_attr_setdefault(&timer_longterm_lck_attr
);
1565 lck_grp_attr_setdefault(&timer_longterm_lck_grp_attr
);
1566 lck_grp_init(&timer_longterm_lck_grp
,
1567 "timer_longterm", &timer_longterm_lck_grp_attr
);
1568 mpqueue_init(&tlp
->queue
,
1569 &timer_longterm_lck_grp
, &timer_longterm_lck_attr
);
1571 timer_call_setup(&tlp
->threshold
.timer
,
1572 timer_longterm_callout
, (timer_call_param_t
) tlp
);
1574 timer_longterm_queue
= &tlp
->queue
;
1579 ENQUEUES
, DEQUEUES
, ESCALATES
, SCANS
, PREEMPTS
,
1580 LATENCY
, LATENCY_MIN
, LATENCY_MAX
, SCAN_LIMIT
, PAUSES
1583 timer_sysctl_get(int oid
)
1585 timer_longterm_t
*tlp
= &timer_longterm
;
1589 return (tlp
->threshold
.interval
== TIMER_LONGTERM_NONE
) ?
1590 0 : tlp
->threshold
.interval
/ NSEC_PER_MSEC
;
1592 return tlp
->queue
.count
;
1594 return tlp
->enqueues
;
1596 return tlp
->dequeues
;
1598 return tlp
->escalates
;
1600 return tlp
->threshold
.scans
;
1602 return tlp
->threshold
.preempts
;
1604 return tlp
->threshold
.latency
;
1606 return tlp
->threshold
.latency_min
;
1608 return tlp
->threshold
.latency_max
;
1610 return tlp
->scan_limit
;
1612 return tlp
->scan_pauses
;
1619 * timer_master_scan() is the inverse of timer_longterm_scan()
1620 * since it un-escalates timers to the longterm queue.
1623 timer_master_scan(timer_longterm_t
*tlp
,
1630 mpqueue_head_t
*timer_master_queue
;
1632 if (tlp
->threshold
.interval
!= TIMER_LONGTERM_NONE
)
1633 threshold
= now
+ tlp
->threshold
.interval
;
1635 threshold
= TIMER_LONGTERM_NONE
;
1637 timer_master_queue
= timer_queue_cpu(master_cpu
);
1638 timer_queue_lock_spin(timer_master_queue
);
1640 qe
= queue_first(&timer_master_queue
->head
);
1641 while (!queue_end(&timer_master_queue
->head
, qe
)) {
1642 call
= TIMER_CALL(qe
);
1643 deadline
= TCE(call
)->deadline
;
1644 qe
= queue_next(qe
);
1645 if ((call
->flags
& TIMER_CALL_LOCAL
) != 0)
1647 if (!simple_lock_try(&call
->lock
)) {
1648 /* case (2c) lock order inversion, dequeue only */
1649 timer_call_entry_dequeue_async(call
);
1652 if (deadline
> threshold
) {
1653 /* move from master to longterm */
1654 timer_call_entry_dequeue(call
);
1655 timer_call_entry_enqueue_tail(call
, timer_longterm_queue
);
1656 if (deadline
< tlp
->threshold
.deadline
) {
1657 tlp
->threshold
.deadline
= deadline
;
1658 tlp
->threshold
.call
= call
;
1661 simple_unlock(&call
->lock
);
1663 timer_queue_unlock(timer_master_queue
);
1667 timer_sysctl_set_threshold(uint64_t value
)
1669 timer_longterm_t
*tlp
= &timer_longterm
;
1670 spl_t s
= splclock();
1671 boolean_t threshold_increase
;
1673 timer_queue_lock_spin(timer_longterm_queue
);
1675 timer_call_cancel(&tlp
->threshold
.timer
);
1678 * Set the new threshold and note whther it's increasing.
1681 tlp
->threshold
.interval
= TIMER_LONGTERM_NONE
;
1682 threshold_increase
= TRUE
;
1683 timer_call_cancel(&tlp
->threshold
.timer
);
1685 uint64_t old_interval
= tlp
->threshold
.interval
;
1686 tlp
->threshold
.interval
= value
* NSEC_PER_MSEC
;
1687 nanoseconds_to_absolutetime(tlp
->threshold
.interval
,
1688 &tlp
->threshold
.interval
);
1689 tlp
->threshold
.margin
= tlp
->threshold
.interval
/ 10;
1690 if (old_interval
== TIMER_LONGTERM_NONE
)
1691 threshold_increase
= FALSE
;
1693 threshold_increase
= (tlp
->threshold
.interval
> old_interval
);
1696 if (threshold_increase
/* or removal */) {
1697 /* Escalate timers from the longterm queue */
1698 timer_longterm_scan(tlp
, mach_absolute_time());
1699 } else /* decrease or addition */ {
1701 * We scan the local/master queue for timers now longterm.
1702 * To be strictly correct, we should scan all processor queues
1703 * but timer migration results in most timers gravitating to the
1704 * master processor in any case.
1706 timer_master_scan(tlp
, mach_absolute_time());
1709 /* Set new timer accordingly */
1710 tlp
->threshold
.deadline_set
= tlp
->threshold
.deadline
;
1711 if (tlp
->threshold
.deadline
!= TIMER_LONGTERM_NONE
) {
1712 tlp
->threshold
.deadline_set
-= tlp
->threshold
.margin
;
1713 tlp
->threshold
.deadline_set
-= tlp
->threshold
.latency
;
1715 &tlp
->threshold
.timer
,
1716 tlp
->threshold
.deadline_set
,
1717 TIMER_CALL_LOCAL
| TIMER_CALL_SYS_CRITICAL
);
1724 tlp
->scan_pauses
= 0;
1725 tlp
->threshold
.scans
= 0;
1726 tlp
->threshold
.preempts
= 0;
1727 tlp
->threshold
.latency
= 0;
1728 tlp
->threshold
.latency_min
= EndOfAllTime
;
1729 tlp
->threshold
.latency_max
= 0;
1731 timer_queue_unlock(timer_longterm_queue
);
1736 timer_sysctl_set(int oid
, uint64_t value
)
1742 (void (*)(void *)) timer_sysctl_set_threshold
,
1744 return KERN_SUCCESS
;
1746 timer_longterm
.scan_limit
= value
;
1747 return KERN_SUCCESS
;
1749 return KERN_INVALID_ARGUMENT
;
1754 /* Select timer coalescing window based on per-task quality-of-service hints */
1755 static boolean_t
tcoal_qos_adjust(thread_t t
, int32_t *tshift
, uint64_t *tmax_abstime
, boolean_t
*pratelimited
) {
1756 uint32_t latency_qos
;
1757 boolean_t adjusted
= FALSE
;
1758 task_t ctask
= t
->task
;
1761 latency_qos
= proc_get_effective_thread_policy(t
, TASK_POLICY_LATENCY_QOS
);
1763 assert(latency_qos
<= NUM_LATENCY_QOS_TIERS
);
1766 *tshift
= tcoal_prio_params
.latency_qos_scale
[latency_qos
- 1];
1767 *tmax_abstime
= tcoal_prio_params
.latency_qos_abstime_max
[latency_qos
- 1];
1768 *pratelimited
= tcoal_prio_params
.latency_tier_rate_limited
[latency_qos
- 1];
1776 /* Adjust timer deadlines based on priority of the thread and the
1777 * urgency value provided at timeout establishment. With this mechanism,
1778 * timers are no longer necessarily sorted in order of soft deadline
1779 * on a given timer queue, i.e. they may be differentially skewed.
1780 * In the current scheme, this could lead to fewer pending timers
1781 * processed than is technically possible when the HW deadline arrives.
1784 timer_compute_leeway(thread_t cthread
, int32_t urgency
, int32_t *tshift
, uint64_t *tmax_abstime
, boolean_t
*pratelimited
) {
1785 int16_t tpri
= cthread
->sched_pri
;
1786 if ((urgency
& TIMER_CALL_USER_MASK
) != 0) {
1787 if (tpri
>= BASEPRI_RTQUEUES
||
1788 urgency
== TIMER_CALL_USER_CRITICAL
) {
1789 *tshift
= tcoal_prio_params
.timer_coalesce_rt_shift
;
1790 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_rt_abstime_max
;
1791 TCOAL_PRIO_STAT(rt_tcl
);
1792 } else if (proc_get_effective_thread_policy(cthread
, TASK_POLICY_DARWIN_BG
) ||
1793 (urgency
== TIMER_CALL_USER_BACKGROUND
)) {
1794 /* Determine if timer should be subjected to a lower QoS */
1795 if (tcoal_qos_adjust(cthread
, tshift
, tmax_abstime
, pratelimited
)) {
1796 if (*tmax_abstime
> tcoal_prio_params
.timer_coalesce_bg_abstime_max
) {
1799 *pratelimited
= FALSE
;
1802 *tshift
= tcoal_prio_params
.timer_coalesce_bg_shift
;
1803 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_bg_abstime_max
;
1804 TCOAL_PRIO_STAT(bg_tcl
);
1805 } else if (tpri
>= MINPRI_KERNEL
) {
1806 *tshift
= tcoal_prio_params
.timer_coalesce_kt_shift
;
1807 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_kt_abstime_max
;
1808 TCOAL_PRIO_STAT(kt_tcl
);
1809 } else if (cthread
->sched_mode
== TH_MODE_FIXED
) {
1810 *tshift
= tcoal_prio_params
.timer_coalesce_fp_shift
;
1811 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_fp_abstime_max
;
1812 TCOAL_PRIO_STAT(fp_tcl
);
1813 } else if (tcoal_qos_adjust(cthread
, tshift
, tmax_abstime
, pratelimited
)) {
1814 TCOAL_PRIO_STAT(qos_tcl
);
1815 } else if (cthread
->sched_mode
== TH_MODE_TIMESHARE
) {
1816 *tshift
= tcoal_prio_params
.timer_coalesce_ts_shift
;
1817 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_ts_abstime_max
;
1818 TCOAL_PRIO_STAT(ts_tcl
);
1820 TCOAL_PRIO_STAT(nc_tcl
);
1822 } else if (urgency
== TIMER_CALL_SYS_BACKGROUND
) {
1823 *tshift
= tcoal_prio_params
.timer_coalesce_bg_shift
;
1824 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_bg_abstime_max
;
1825 TCOAL_PRIO_STAT(bg_tcl
);
1827 *tshift
= tcoal_prio_params
.timer_coalesce_kt_shift
;
1828 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_kt_abstime_max
;
1829 TCOAL_PRIO_STAT(kt_tcl
);
1834 int timer_user_idle_level
;
1837 timer_call_slop(uint64_t deadline
, uint64_t now
, uint32_t flags
, thread_t cthread
, boolean_t
*pratelimited
)
1839 int32_t tcs_shift
= 0;
1840 uint64_t tcs_max_abstime
= 0;
1842 uint32_t urgency
= (flags
& TIMER_CALL_URGENCY_MASK
);
1844 if (mach_timer_coalescing_enabled
&&
1845 (deadline
> now
) && (urgency
!= TIMER_CALL_SYS_CRITICAL
)) {
1846 timer_compute_leeway(cthread
, urgency
, &tcs_shift
, &tcs_max_abstime
, pratelimited
);
1849 adjval
= MIN((deadline
- now
) >> tcs_shift
, tcs_max_abstime
);
1851 adjval
= MIN((deadline
- now
) << (-tcs_shift
), tcs_max_abstime
);
1852 /* Apply adjustments derived from "user idle level" heuristic */
1853 adjval
+= (adjval
* timer_user_idle_level
) >> 7;
1861 timer_get_user_idle_level(void) {
1862 return timer_user_idle_level
;
1865 kern_return_t
timer_set_user_idle_level(int ilevel
) {
1866 boolean_t do_reeval
= FALSE
;
1868 if ((ilevel
< 0) || (ilevel
> 128))
1869 return KERN_INVALID_ARGUMENT
;
1871 if (ilevel
< timer_user_idle_level
) {
1875 timer_user_idle_level
= ilevel
;
1878 ml_timer_evaluate();
1880 return KERN_SUCCESS
;