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>
42 #include <sys/kdebug.h>
50 #define TIMER_ASSERT 1
53 //#define TIMER_ASSERT 1
57 #define DBG(x...) kprintf("DBG: " x);
63 #define TIMER_KDEBUG_TRACE KERNEL_DEBUG_CONSTANT_IST
65 #define TIMER_KDEBUG_TRACE(x...)
69 lck_grp_t timer_call_lck_grp
;
70 lck_attr_t timer_call_lck_attr
;
71 lck_grp_attr_t timer_call_lck_grp_attr
;
73 lck_grp_t timer_longterm_lck_grp
;
74 lck_attr_t timer_longterm_lck_attr
;
75 lck_grp_attr_t timer_longterm_lck_grp_attr
;
77 /* Timer queue lock must be acquired with interrupts disabled (under splclock()) */
79 #define timer_queue_lock_spin(queue) \
80 lck_mtx_lock_spin_always(&queue->lock_data)
82 #define timer_queue_unlock(queue) \
83 lck_mtx_unlock_always(&queue->lock_data)
85 #define timer_queue_lock_spin(queue) (void)1
86 #define timer_queue_unlock(queue) (void)1
89 #define QUEUE(x) ((queue_t)(x))
90 #define MPQUEUE(x) ((mpqueue_head_t *)(x))
91 #define TIMER_CALL(x) ((timer_call_t)(x))
92 #define TCE(x) (&(x->call_entry))
94 * The longterm timer object is a global structure holding all timers
95 * beyond the short-term, local timer queue threshold. The boot processor
96 * is responsible for moving each timer to its local timer queue
97 * if and when that timer becomes due within the threshold.
99 #define TIMER_LONGTERM_NONE EndOfAllTime
100 #if defined(__x86_64__)
101 #define TIMER_LONGTERM_THRESHOLD (1ULL * NSEC_PER_SEC)
103 #define TIMER_LONGTERM_THRESHOLD TIMER_LONGTERM_NONE
107 uint64_t interval
; /* longterm timer interval */
108 uint64_t margin
; /* fudge factor (10% of interval */
109 uint64_t deadline
; /* first/soonest longterm deadline */
110 uint64_t preempted
; /* sooner timer has pre-empted */
111 timer_call_t call
; /* first/soonest longterm timer call */
112 uint64_t deadline_set
; /* next timer set */
113 timer_call_data_t timer
; /* timer used by threshold management */
115 uint64_t scans
; /* num threshold timer scans */
116 uint64_t preempts
; /* num threshold reductions */
117 uint64_t latency
; /* average threshold latency */
118 uint64_t latency_min
; /* minimum threshold latency */
119 uint64_t latency_max
; /* maximum threshold latency */
123 mpqueue_head_t queue
; /* longterm timer list */
124 uint64_t enqueues
; /* num timers queued */
125 uint64_t dequeues
; /* num timers dequeued */
126 uint64_t escalates
; /* num timers becoming shortterm */
127 uint64_t scan_time
; /* last time the list was scanned */
128 threshold_t threshold
; /* longterm timer threshold */
131 timer_longterm_t timer_longterm
;
133 static mpqueue_head_t
*timer_longterm_queue
= NULL
;
135 static void timer_longterm_init(void);
136 static void timer_longterm_callout(
137 timer_call_param_t p0
,
138 timer_call_param_t p1
);
139 extern void timer_longterm_scan(
140 timer_longterm_t
*tlp
,
142 static void timer_longterm_update(
143 timer_longterm_t
*tlp
);
144 static void timer_longterm_update_locked(
145 timer_longterm_t
*tlp
);
146 static mpqueue_head_t
* timer_longterm_enqueue_unlocked(
150 mpqueue_head_t
** old_queue
,
151 uint64_t soft_deadline
,
153 timer_call_param_t param1
,
154 uint32_t callout_flags
);
155 static void timer_longterm_dequeued_locked(
158 uint64_t past_deadline_timers
;
159 uint64_t past_deadline_deltas
;
160 uint64_t past_deadline_longest
;
161 uint64_t past_deadline_shortest
= ~0ULL;
162 enum {PAST_DEADLINE_TIMER_ADJUSTMENT_NS
= 10 * 1000};
164 uint64_t past_deadline_timer_adjustment
;
166 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
);
167 boolean_t mach_timer_coalescing_enabled
= TRUE
;
169 mpqueue_head_t
*timer_call_enqueue_deadline_unlocked(
171 mpqueue_head_t
*queue
,
173 uint64_t soft_deadline
,
175 timer_call_param_t param1
,
178 mpqueue_head_t
*timer_call_dequeue_unlocked(
181 timer_coalescing_priority_params_t tcoal_prio_params
;
184 int32_t nc_tcl
, rt_tcl
, bg_tcl
, kt_tcl
, fp_tcl
, ts_tcl
, qos_tcl
;
185 #define TCOAL_PRIO_STAT(x) (x++)
187 #define TCOAL_PRIO_STAT(x)
191 timer_call_init_abstime(void)
195 timer_coalescing_priority_params_ns_t
* tcoal_prio_params_init
= timer_call_get_priority_params();
196 nanoseconds_to_absolutetime(PAST_DEADLINE_TIMER_ADJUSTMENT_NS
, &past_deadline_timer_adjustment
);
197 nanoseconds_to_absolutetime(tcoal_prio_params_init
->idle_entry_timer_processing_hdeadline_threshold_ns
, &result
);
198 tcoal_prio_params
.idle_entry_timer_processing_hdeadline_threshold_abstime
= (uint32_t)result
;
199 nanoseconds_to_absolutetime(tcoal_prio_params_init
->interrupt_timer_coalescing_ilat_threshold_ns
, &result
);
200 tcoal_prio_params
.interrupt_timer_coalescing_ilat_threshold_abstime
= (uint32_t)result
;
201 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_resort_threshold_ns
, &result
);
202 tcoal_prio_params
.timer_resort_threshold_abstime
= (uint32_t)result
;
203 tcoal_prio_params
.timer_coalesce_rt_shift
= tcoal_prio_params_init
->timer_coalesce_rt_shift
;
204 tcoal_prio_params
.timer_coalesce_bg_shift
= tcoal_prio_params_init
->timer_coalesce_bg_shift
;
205 tcoal_prio_params
.timer_coalesce_kt_shift
= tcoal_prio_params_init
->timer_coalesce_kt_shift
;
206 tcoal_prio_params
.timer_coalesce_fp_shift
= tcoal_prio_params_init
->timer_coalesce_fp_shift
;
207 tcoal_prio_params
.timer_coalesce_ts_shift
= tcoal_prio_params_init
->timer_coalesce_ts_shift
;
209 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_rt_ns_max
,
210 &tcoal_prio_params
.timer_coalesce_rt_abstime_max
);
211 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_bg_ns_max
,
212 &tcoal_prio_params
.timer_coalesce_bg_abstime_max
);
213 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_kt_ns_max
,
214 &tcoal_prio_params
.timer_coalesce_kt_abstime_max
);
215 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_fp_ns_max
,
216 &tcoal_prio_params
.timer_coalesce_fp_abstime_max
);
217 nanoseconds_to_absolutetime(tcoal_prio_params_init
->timer_coalesce_ts_ns_max
,
218 &tcoal_prio_params
.timer_coalesce_ts_abstime_max
);
220 for (i
= 0; i
< NUM_LATENCY_QOS_TIERS
; i
++) {
221 tcoal_prio_params
.latency_qos_scale
[i
] = tcoal_prio_params_init
->latency_qos_scale
[i
];
222 nanoseconds_to_absolutetime(tcoal_prio_params_init
->latency_qos_ns_max
[i
],
223 &tcoal_prio_params
.latency_qos_abstime_max
[i
]);
224 tcoal_prio_params
.latency_tier_rate_limited
[i
] = tcoal_prio_params_init
->latency_tier_rate_limited
[i
];
230 timer_call_init(void)
232 lck_attr_setdefault(&timer_call_lck_attr
);
233 lck_grp_attr_setdefault(&timer_call_lck_grp_attr
);
234 lck_grp_init(&timer_call_lck_grp
, "timer_call", &timer_call_lck_grp_attr
);
236 timer_longterm_init();
237 timer_call_init_abstime();
242 timer_call_queue_init(mpqueue_head_t
*queue
)
244 DBG("timer_call_queue_init(%p)\n", queue
);
245 mpqueue_init(queue
, &timer_call_lck_grp
, &timer_call_lck_attr
);
252 timer_call_func_t func
,
253 timer_call_param_t param0
)
255 DBG("timer_call_setup(%p,%p,%p)\n", call
, func
, param0
);
256 call_entry_setup(TCE(call
), func
, param0
);
257 simple_lock_init(&(call
)->lock
, 0);
258 call
->async_dequeue
= FALSE
;
261 static __inline__ mpqueue_head_t
*
262 timer_call_entry_dequeue(
265 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
267 if (!hw_lock_held((hw_lock_t
)&entry
->lock
))
268 panic("_call_entry_dequeue() "
269 "entry %p is not locked\n", entry
);
271 * XXX The queue lock is actually a mutex in spin mode
272 * but there's no way to test for it being held
273 * so we pretend it's a spinlock!
275 if (!hw_lock_held((hw_lock_t
)&old_queue
->lock_data
))
276 panic("_call_entry_dequeue() "
277 "queue %p is not locked\n", old_queue
);
279 call_entry_dequeue(TCE(entry
));
285 static __inline__ mpqueue_head_t
*
286 timer_call_entry_enqueue_deadline(
288 mpqueue_head_t
*queue
,
291 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
293 if (!hw_lock_held((hw_lock_t
)&entry
->lock
))
294 panic("_call_entry_enqueue_deadline() "
295 "entry %p is not locked\n", entry
);
296 /* XXX More lock pretense: */
297 if (!hw_lock_held((hw_lock_t
)&queue
->lock_data
))
298 panic("_call_entry_enqueue_deadline() "
299 "queue %p is not locked\n", queue
);
300 if (old_queue
!= NULL
&& old_queue
!= queue
)
301 panic("_call_entry_enqueue_deadline() "
302 "old_queue %p != queue", old_queue
);
304 call_entry_enqueue_deadline(TCE(entry
), QUEUE(queue
), deadline
);
306 /* For efficiency, track the earliest soft deadline on the queue, so that
307 * fuzzy decisions can be made without lock acquisitions.
309 timer_call_t thead
= (timer_call_t
)queue_first(&queue
->head
);
311 queue
->earliest_soft_deadline
= thead
->flags
& TIMER_CALL_RATELIMITED
? TCE(thead
)->deadline
: thead
->soft_deadline
;
322 static __inline__ mpqueue_head_t
*
323 timer_call_entry_dequeue(
326 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
328 call_entry_dequeue(TCE(entry
));
334 static __inline__ mpqueue_head_t
*
335 timer_call_entry_enqueue_deadline(
337 mpqueue_head_t
*queue
,
340 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
342 call_entry_enqueue_deadline(TCE(entry
), QUEUE(queue
), deadline
);
344 /* For efficiency, track the earliest soft deadline on the queue,
345 * so that fuzzy decisions can be made without lock acquisitions.
348 timer_call_t thead
= (timer_call_t
)queue_first(&queue
->head
);
349 queue
->earliest_soft_deadline
= thead
->flags
& TIMER_CALL_RATELIMITED
? TCE(thead
)->deadline
: thead
->soft_deadline
;
360 static __inline__
void
361 timer_call_entry_enqueue_tail(
363 mpqueue_head_t
*queue
)
365 call_entry_enqueue_tail(TCE(entry
), QUEUE(queue
));
371 * Remove timer entry from its queue but don't change the queue pointer
372 * and set the async_dequeue flag. This is locking case 2b.
374 static __inline__
void
375 timer_call_entry_dequeue_async(
378 mpqueue_head_t
*old_queue
= MPQUEUE(TCE(entry
)->queue
);
381 (void) remque(qe(entry
));
382 entry
->async_dequeue
= TRUE
;
388 unsigned timer_call_enqueue_deadline_unlocked_async1
;
389 unsigned timer_call_enqueue_deadline_unlocked_async2
;
392 * Assumes call_entry and queues unlocked, interrupts disabled.
394 __inline__ mpqueue_head_t
*
395 timer_call_enqueue_deadline_unlocked(
397 mpqueue_head_t
*queue
,
399 uint64_t soft_deadline
,
401 timer_call_param_t param1
,
402 uint32_t callout_flags
)
404 call_entry_t entry
= TCE(call
);
405 mpqueue_head_t
*old_queue
;
407 DBG("timer_call_enqueue_deadline_unlocked(%p,%p,)\n", call
, queue
);
409 simple_lock(&call
->lock
);
411 old_queue
= MPQUEUE(entry
->queue
);
413 if (old_queue
!= NULL
) {
414 timer_queue_lock_spin(old_queue
);
415 if (call
->async_dequeue
) {
416 /* collision (1c): timer already dequeued, clear flag */
418 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
419 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
424 timer_call_enqueue_deadline_unlocked_async1
++;
426 call
->async_dequeue
= FALSE
;
428 } else if (old_queue
!= queue
) {
429 timer_call_entry_dequeue(call
);
431 timer_call_enqueue_deadline_unlocked_async2
++;
434 if (old_queue
== timer_longterm_queue
)
435 timer_longterm_dequeued_locked(call
);
436 if (old_queue
!= queue
) {
437 timer_queue_unlock(old_queue
);
438 timer_queue_lock_spin(queue
);
441 timer_queue_lock_spin(queue
);
444 call
->soft_deadline
= soft_deadline
;
445 call
->flags
= callout_flags
;
446 TCE(call
)->param1
= param1
;
449 timer_call_entry_enqueue_deadline(call
, queue
, deadline
);
450 timer_queue_unlock(queue
);
451 simple_unlock(&call
->lock
);
457 unsigned timer_call_dequeue_unlocked_async1
;
458 unsigned timer_call_dequeue_unlocked_async2
;
461 timer_call_dequeue_unlocked(
464 call_entry_t entry
= TCE(call
);
465 mpqueue_head_t
*old_queue
;
467 DBG("timer_call_dequeue_unlocked(%p)\n", call
);
469 simple_lock(&call
->lock
);
470 old_queue
= MPQUEUE(entry
->queue
);
472 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
473 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
479 if (old_queue
!= NULL
) {
480 timer_queue_lock_spin(old_queue
);
481 if (call
->async_dequeue
) {
482 /* collision (1c): timer already dequeued, clear flag */
484 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
485 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
490 timer_call_dequeue_unlocked_async1
++;
492 call
->async_dequeue
= FALSE
;
495 timer_call_entry_dequeue(call
);
497 if (old_queue
== timer_longterm_queue
)
498 timer_longterm_dequeued_locked(call
);
499 timer_queue_unlock(old_queue
);
501 simple_unlock(&call
->lock
);
507 * Timer call entry locking model
508 * ==============================
510 * Timer call entries are linked on per-cpu timer queues which are protected
511 * by the queue lock and the call entry lock. The locking protocol is:
513 * 0) The canonical locking order is timer call entry followed by queue.
515 * 1) With only the entry lock held, entry.queue is valid:
516 * 1a) NULL: the entry is not queued, or
517 * 1b) non-NULL: this queue must be locked before the entry is modified.
518 * After locking the queue, the call.async_dequeue flag must be checked:
519 * 1c) TRUE: the entry was removed from the queue by another thread
520 * and we must NULL the entry.queue and reset this flag, or
521 * 1d) FALSE: (ie. queued), the entry can be manipulated.
523 * 2) If a queue lock is obtained first, the queue is stable:
524 * 2a) If a try-lock of a queued entry succeeds, the call can be operated on
526 * 2b) If a try-lock fails, it indicates that another thread is attempting
527 * to change the entry and move it to a different position in this queue
528 * or to different queue. The entry can be dequeued but it should not be
529 * operated upon since it is being changed. Furthermore, we don't null
530 * the entry.queue pointer (protected by the entry lock we don't own).
531 * Instead, we set the async_dequeue flag -- see (1c).
532 * 2c) Same as 2b but occurring when a longterm timer is matured.
533 * 3) A callout's parameters (deadline, flags, parameters, soft deadline &c.)
534 * should be manipulated with the appropriate timer queue lock held,
535 * to prevent queue traversal observations from observing inconsistent
536 * updates to an in-flight callout.
540 * Inlines timer_call_entry_dequeue() and timer_call_entry_enqueue_deadline()
541 * cast between pointer types (mpqueue_head_t *) and (queue_t) so that
542 * we can use the call_entry_dequeue() and call_entry_enqueue_deadline()
543 * methods to operate on timer_call structs as if they are call_entry structs.
544 * These structures are identical except for their queue head pointer fields.
546 * In the debug case, we assert that the timer call locking protocol
551 timer_call_enter_internal(
553 timer_call_param_t param1
,
557 boolean_t ratelimited
)
559 mpqueue_head_t
*queue
= NULL
;
560 mpqueue_head_t
*old_queue
;
564 uint64_t sdeadline
, ttd
;
568 sdeadline
= deadline
;
569 uint64_t ctime
= mach_absolute_time();
571 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
572 DECR_TIMER_ENTER
| DBG_FUNC_START
,
574 param1
, deadline
, flags
, 0);
576 urgency
= (flags
& TIMER_CALL_URGENCY_MASK
);
578 boolean_t slop_ratelimited
= FALSE
;
579 slop
= timer_call_slop(deadline
, ctime
, urgency
, current_thread(), &slop_ratelimited
);
581 if ((flags
& TIMER_CALL_LEEWAY
) != 0 && leeway
> slop
)
584 if (UINT64_MAX
- deadline
<= slop
) {
585 deadline
= UINT64_MAX
;
590 if (__improbable(deadline
< ctime
)) {
591 uint64_t delta
= (ctime
- deadline
);
593 past_deadline_timers
++;
594 past_deadline_deltas
+= delta
;
595 if (delta
> past_deadline_longest
)
596 past_deadline_longest
= deadline
;
597 if (delta
< past_deadline_shortest
)
598 past_deadline_shortest
= delta
;
600 deadline
= ctime
+ past_deadline_timer_adjustment
;
601 sdeadline
= deadline
;
604 if (ratelimited
|| slop_ratelimited
) {
605 flags
|= TIMER_CALL_RATELIMITED
;
607 flags
&= ~TIMER_CALL_RATELIMITED
;
610 ttd
= sdeadline
- ctime
;
612 DTRACE_TMR7(callout__create
, timer_call_func_t
, TCE(call
)->func
,
613 timer_call_param_t
, TCE(call
)->param0
, uint32_t, flags
,
614 (deadline
- sdeadline
),
615 (ttd
>> 32), (unsigned) (ttd
& 0xFFFFFFFF), call
);
618 /* Program timer callout parameters under the appropriate per-CPU or
619 * longterm queue lock. The callout may have been previously enqueued
620 * and in-flight on this or another timer queue.
622 if (!ratelimited
&& !slop_ratelimited
) {
623 queue
= timer_longterm_enqueue_unlocked(call
, ctime
, deadline
, &old_queue
, sdeadline
, ttd
, param1
, flags
);
627 queue
= timer_queue_assign(deadline
);
628 old_queue
= timer_call_enqueue_deadline_unlocked(call
, queue
, deadline
, sdeadline
, ttd
, param1
, flags
);
632 TCE(call
)->entry_time
= ctime
;
635 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
636 DECR_TIMER_ENTER
| DBG_FUNC_END
,
638 (old_queue
!= NULL
), deadline
, queue
->count
, 0);
642 return (old_queue
!= NULL
);
647 * return boolean indicating whether the call was previously queued.
655 return timer_call_enter_internal(call
, NULL
, deadline
, 0, flags
, FALSE
);
661 timer_call_param_t param1
,
665 return timer_call_enter_internal(call
, param1
, deadline
, 0, flags
, FALSE
);
669 timer_call_enter_with_leeway(
671 timer_call_param_t param1
,
675 boolean_t ratelimited
)
677 return timer_call_enter_internal(call
, param1
, deadline
, leeway
, flags
, ratelimited
);
684 mpqueue_head_t
*old_queue
;
689 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
690 DECR_TIMER_CANCEL
| DBG_FUNC_START
,
692 TCE(call
)->deadline
, call
->soft_deadline
, call
->flags
, 0);
694 old_queue
= timer_call_dequeue_unlocked(call
);
696 if (old_queue
!= NULL
) {
697 timer_queue_lock_spin(old_queue
);
698 if (!queue_empty(&old_queue
->head
)) {
699 timer_queue_cancel(old_queue
, TCE(call
)->deadline
, CE(queue_first(&old_queue
->head
))->deadline
);
700 timer_call_t thead
= (timer_call_t
)queue_first(&old_queue
->head
);
701 old_queue
->earliest_soft_deadline
= thead
->flags
& TIMER_CALL_RATELIMITED
? TCE(thead
)->deadline
: thead
->soft_deadline
;
704 timer_queue_cancel(old_queue
, TCE(call
)->deadline
, UINT64_MAX
);
705 old_queue
->earliest_soft_deadline
= UINT64_MAX
;
707 timer_queue_unlock(old_queue
);
709 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
710 DECR_TIMER_CANCEL
| DBG_FUNC_END
,
713 TCE(call
)->deadline
- mach_absolute_time(),
714 TCE(call
)->deadline
- TCE(call
)->entry_time
, 0);
718 DTRACE_TMR6(callout__cancel
, timer_call_func_t
, TCE(call
)->func
,
719 timer_call_param_t
, TCE(call
)->param0
, uint32_t, call
->flags
, 0,
720 (call
->ttd
>> 32), (unsigned) (call
->ttd
& 0xFFFFFFFF));
723 return (old_queue
!= NULL
);
726 static uint32_t timer_queue_shutdown_lock_skips
;
727 static uint32_t timer_queue_shutdown_discarded
;
730 timer_queue_shutdown(
731 mpqueue_head_t
*queue
)
734 mpqueue_head_t
*new_queue
;
738 DBG("timer_queue_shutdown(%p)\n", queue
);
742 /* Note comma operator in while expression re-locking each iteration */
743 while (timer_queue_lock_spin(queue
), !queue_empty(&queue
->head
)) {
744 call
= TIMER_CALL(queue_first(&queue
->head
));
746 if (!simple_lock_try(&call
->lock
)) {
748 * case (2b) lock order inversion, dequeue and skip
749 * Don't change the call_entry queue back-pointer
750 * but set the async_dequeue field.
752 timer_queue_shutdown_lock_skips
++;
753 timer_call_entry_dequeue_async(call
);
755 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
756 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
762 timer_queue_unlock(queue
);
766 boolean_t call_local
= ((call
->flags
& TIMER_CALL_LOCAL
) != 0);
768 /* remove entry from old queue */
769 timer_call_entry_dequeue(call
);
770 timer_queue_unlock(queue
);
772 if (call_local
== FALSE
) {
773 /* and queue it on new, discarding LOCAL timers */
774 new_queue
= timer_queue_assign(TCE(call
)->deadline
);
775 timer_queue_lock_spin(new_queue
);
776 timer_call_entry_enqueue_deadline(
777 call
, new_queue
, TCE(call
)->deadline
);
778 timer_queue_unlock(new_queue
);
780 timer_queue_shutdown_discarded
++;
783 /* The only lingering LOCAL timer should be this thread's
784 * quantum expiration timer.
786 assert((call_local
== FALSE
) ||
787 (TCE(call
)->func
== thread_quantum_expire
));
789 simple_unlock(&call
->lock
);
792 timer_queue_unlock(queue
);
796 static uint32_t timer_queue_expire_lock_skips
;
798 timer_queue_expire_with_options(
799 mpqueue_head_t
*queue
,
803 timer_call_t call
= NULL
;
804 uint32_t tc_iterations
= 0;
805 DBG("timer_queue_expire(%p,)\n", queue
);
807 uint64_t cur_deadline
= deadline
;
808 timer_queue_lock_spin(queue
);
810 while (!queue_empty(&queue
->head
)) {
811 /* Upon processing one or more timer calls, refresh the
812 * deadline to account for time elapsed in the callout
814 if (++tc_iterations
> 1)
815 cur_deadline
= mach_absolute_time();
818 call
= TIMER_CALL(queue_first(&queue
->head
));
820 if (call
->soft_deadline
<= cur_deadline
) {
821 timer_call_func_t func
;
822 timer_call_param_t param0
, param1
;
824 TCOAL_DEBUG(0xDDDD0000, queue
->earliest_soft_deadline
, call
->soft_deadline
, 0, 0, 0);
825 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
826 DECR_TIMER_EXPIRE
| DBG_FUNC_NONE
,
830 TCE(call
)->entry_time
, 0);
832 if ((call
->flags
& TIMER_CALL_RATELIMITED
) &&
833 (TCE(call
)->deadline
> cur_deadline
)) {
838 if (!simple_lock_try(&call
->lock
)) {
839 /* case (2b) lock inversion, dequeue and skip */
840 timer_queue_expire_lock_skips
++;
841 timer_call_entry_dequeue_async(call
);
846 timer_call_entry_dequeue(call
);
848 func
= TCE(call
)->func
;
849 param0
= TCE(call
)->param0
;
850 param1
= TCE(call
)->param1
;
852 simple_unlock(&call
->lock
);
853 timer_queue_unlock(queue
);
855 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
856 DECR_TIMER_CALLOUT
| DBG_FUNC_START
,
857 call
, VM_KERNEL_UNSLIDE(func
), param0
, param1
, 0);
860 DTRACE_TMR7(callout__start
, timer_call_func_t
, func
,
861 timer_call_param_t
, param0
, unsigned, call
->flags
,
862 0, (call
->ttd
>> 32),
863 (unsigned) (call
->ttd
& 0xFFFFFFFF), call
);
865 /* Maintain time-to-deadline in per-processor data
866 * structure for thread wakeup deadline statistics.
868 uint64_t *ttdp
= &(PROCESSOR_DATA(current_processor(), timer_call_ttd
));
870 (*func
)(param0
, param1
);
873 DTRACE_TMR4(callout__end
, timer_call_func_t
, func
,
874 param0
, param1
, call
);
877 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
878 DECR_TIMER_CALLOUT
| DBG_FUNC_END
,
879 call
, VM_KERNEL_UNSLIDE(func
), param0
, param1
, 0);
881 timer_queue_lock_spin(queue
);
883 if (__probable(rescan
== FALSE
)) {
886 int64_t skew
= TCE(call
)->deadline
- call
->soft_deadline
;
887 assert(TCE(call
)->deadline
>= call
->soft_deadline
);
889 /* DRK: On a latency quality-of-service level change,
890 * re-sort potentially rate-limited timers. The platform
891 * layer determines which timers require
892 * this. In the absence of the per-callout
893 * synchronization requirement, a global resort could
894 * be more efficient. The re-sort effectively
895 * annuls all timer adjustments, i.e. the "soft
896 * deadline" is the sort key.
899 if (timer_resort_threshold(skew
)) {
900 if (__probable(simple_lock_try(&call
->lock
))) {
901 timer_call_entry_dequeue(call
);
902 timer_call_entry_enqueue_deadline(call
, queue
, call
->soft_deadline
);
903 simple_unlock(&call
->lock
);
908 call
= TIMER_CALL(queue_next(qe(call
)));
909 if (queue_end(&queue
->head
, qe(call
)))
916 if (!queue_empty(&queue
->head
)) {
917 call
= TIMER_CALL(queue_first(&queue
->head
));
918 cur_deadline
= TCE(call
)->deadline
;
919 queue
->earliest_soft_deadline
= (call
->flags
& TIMER_CALL_RATELIMITED
) ? TCE(call
)->deadline
: call
->soft_deadline
;
921 queue
->earliest_soft_deadline
= cur_deadline
= UINT64_MAX
;
924 timer_queue_unlock(queue
);
926 return (cur_deadline
);
931 mpqueue_head_t
*queue
,
934 return timer_queue_expire_with_options(queue
, deadline
, FALSE
);
937 extern int serverperfmode
;
938 static uint32_t timer_queue_migrate_lock_skips
;
940 * timer_queue_migrate() is called by timer_queue_migrate_cpu()
941 * to move timer requests from the local processor (queue_from)
942 * to a target processor's (queue_to).
945 timer_queue_migrate(mpqueue_head_t
*queue_from
, mpqueue_head_t
*queue_to
)
948 timer_call_t head_to
;
949 int timers_migrated
= 0;
951 DBG("timer_queue_migrate(%p,%p)\n", queue_from
, queue_to
);
953 assert(!ml_get_interrupts_enabled());
954 assert(queue_from
!= queue_to
);
956 if (serverperfmode
) {
958 * if we're running a high end server
959 * avoid migrations... they add latency
960 * and don't save us power under typical
967 * Take both local (from) and target (to) timer queue locks while
968 * moving the timers from the local queue to the target processor.
969 * We assume that the target is always the boot processor.
970 * But only move if all of the following is true:
971 * - the target queue is non-empty
972 * - the local queue is non-empty
973 * - the local queue's first deadline is later than the target's
974 * - the local queue contains no non-migrateable "local" call
975 * so that we need not have the target resync.
978 timer_queue_lock_spin(queue_to
);
980 head_to
= TIMER_CALL(queue_first(&queue_to
->head
));
981 if (queue_empty(&queue_to
->head
)) {
982 timers_migrated
= -1;
986 timer_queue_lock_spin(queue_from
);
988 if (queue_empty(&queue_from
->head
)) {
989 timers_migrated
= -2;
993 call
= TIMER_CALL(queue_first(&queue_from
->head
));
994 if (TCE(call
)->deadline
< TCE(head_to
)->deadline
) {
999 /* perform scan for non-migratable timers */
1001 if (call
->flags
& TIMER_CALL_LOCAL
) {
1002 timers_migrated
= -3;
1005 call
= TIMER_CALL(queue_next(qe(call
)));
1006 } while (!queue_end(&queue_from
->head
, qe(call
)));
1008 /* migration loop itself -- both queues are locked */
1009 while (!queue_empty(&queue_from
->head
)) {
1010 call
= TIMER_CALL(queue_first(&queue_from
->head
));
1011 if (!simple_lock_try(&call
->lock
)) {
1012 /* case (2b) lock order inversion, dequeue only */
1014 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1015 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
1018 call
->lock
.interlock
.lock_data
,
1021 timer_queue_migrate_lock_skips
++;
1022 timer_call_entry_dequeue_async(call
);
1025 timer_call_entry_dequeue(call
);
1026 timer_call_entry_enqueue_deadline(
1027 call
, queue_to
, TCE(call
)->deadline
);
1029 simple_unlock(&call
->lock
);
1031 queue_from
->earliest_soft_deadline
= UINT64_MAX
;
1033 timer_queue_unlock(queue_from
);
1035 timer_queue_unlock(queue_to
);
1037 return timers_migrated
;
1041 timer_queue_trace_cpu(int ncpu
)
1043 timer_call_nosync_cpu(
1045 (void(*)())timer_queue_trace
,
1046 (void*) timer_queue_cpu(ncpu
));
1051 mpqueue_head_t
*queue
)
1060 timer_queue_lock_spin(queue
);
1062 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1063 DECR_TIMER_QUEUE
| DBG_FUNC_START
,
1064 queue
->count
, mach_absolute_time(), 0, 0, 0);
1066 if (!queue_empty(&queue
->head
)) {
1067 call
= TIMER_CALL(queue_first(&queue
->head
));
1069 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1070 DECR_TIMER_QUEUE
| DBG_FUNC_NONE
,
1071 call
->soft_deadline
,
1072 TCE(call
)->deadline
,
1073 TCE(call
)->entry_time
,
1076 call
= TIMER_CALL(queue_next(qe(call
)));
1077 } while (!queue_end(&queue
->head
, qe(call
)));
1080 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1081 DECR_TIMER_QUEUE
| DBG_FUNC_END
,
1082 queue
->count
, mach_absolute_time(), 0, 0, 0);
1084 timer_queue_unlock(queue
);
1089 timer_longterm_dequeued_locked(timer_call_t call
)
1091 timer_longterm_t
*tlp
= &timer_longterm
;
1094 if (call
== tlp
->threshold
.call
)
1095 tlp
->threshold
.call
= NULL
;
1099 * Place a timer call in the longterm list
1100 * and adjust the next timer callout deadline if the new timer is first.
1103 timer_longterm_enqueue_unlocked(timer_call_t call
,
1106 mpqueue_head_t
**old_queue
,
1107 uint64_t soft_deadline
,
1109 timer_call_param_t param1
,
1110 uint32_t callout_flags
)
1112 timer_longterm_t
*tlp
= &timer_longterm
;
1113 boolean_t update_required
= FALSE
;
1114 uint64_t longterm_threshold
;
1116 longterm_threshold
= now
+ tlp
->threshold
.interval
;
1119 * Return NULL without doing anything if:
1120 * - this timer is local, or
1121 * - the longterm mechanism is disabled, or
1122 * - this deadline is too short.
1124 if ((callout_flags
& TIMER_CALL_LOCAL
) != 0 ||
1125 (tlp
->threshold
.interval
== TIMER_LONGTERM_NONE
) ||
1126 (deadline
<= longterm_threshold
))
1130 * Remove timer from its current queue, if any.
1132 *old_queue
= timer_call_dequeue_unlocked(call
);
1135 * Lock the longterm queue, queue timer and determine
1136 * whether an update is necessary.
1138 assert(!ml_get_interrupts_enabled());
1139 simple_lock(&call
->lock
);
1140 timer_queue_lock_spin(timer_longterm_queue
);
1141 TCE(call
)->deadline
= deadline
;
1142 TCE(call
)->param1
= param1
;
1144 call
->soft_deadline
= soft_deadline
;
1145 call
->flags
= callout_flags
;
1146 timer_call_entry_enqueue_tail(call
, timer_longterm_queue
);
1151 * We'll need to update the currently set threshold timer
1152 * if the new deadline is sooner and no sooner update is in flight.
1154 if (deadline
< tlp
->threshold
.deadline
&&
1155 deadline
< tlp
->threshold
.preempted
) {
1156 tlp
->threshold
.preempted
= deadline
;
1157 tlp
->threshold
.call
= call
;
1158 update_required
= TRUE
;
1160 timer_queue_unlock(timer_longterm_queue
);
1161 simple_unlock(&call
->lock
);
1163 if (update_required
) {
1165 * Note: this call expects that calling the master cpu
1166 * alone does not involve locking the topo lock.
1168 timer_call_nosync_cpu(
1170 (void (*)(void *)) timer_longterm_update
,
1174 return timer_longterm_queue
;
1178 * Scan for timers below the longterm threshold.
1179 * Move these to the local timer queue (of the boot processor on which the
1180 * calling thread is running).
1181 * Both the local (boot) queue and the longterm queue are locked.
1182 * The scan is similar to the timer migrate sequence but is performed by
1183 * successively examining each timer on the longterm queue:
1184 * - if within the short-term threshold
1185 * - enter on the local queue (unless being deleted),
1187 * - if sooner, deadline becomes the next threshold deadline.
1190 timer_longterm_scan(timer_longterm_t
*tlp
,
1197 mpqueue_head_t
*timer_master_queue
;
1199 assert(!ml_get_interrupts_enabled());
1200 assert(cpu_number() == master_cpu
);
1202 if (tlp
->threshold
.interval
!= TIMER_LONGTERM_NONE
)
1203 threshold
= now
+ tlp
->threshold
.interval
;
1205 threshold
= TIMER_LONGTERM_NONE
;
1207 tlp
->threshold
.deadline
= TIMER_LONGTERM_NONE
;
1208 tlp
->threshold
.call
= NULL
;
1210 if (queue_empty(&timer_longterm_queue
->head
))
1213 timer_master_queue
= timer_queue_cpu(master_cpu
);
1214 timer_queue_lock_spin(timer_master_queue
);
1216 qe
= queue_first(&timer_longterm_queue
->head
);
1217 while (!queue_end(&timer_longterm_queue
->head
, qe
)) {
1218 call
= TIMER_CALL(qe
);
1219 deadline
= call
->soft_deadline
;
1220 qe
= queue_next(qe
);
1221 if (!simple_lock_try(&call
->lock
)) {
1222 /* case (2c) lock order inversion, dequeue only */
1224 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1225 DECR_TIMER_ASYNC_DEQ
| DBG_FUNC_NONE
,
1228 call
->lock
.interlock
.lock_data
,
1231 timer_call_entry_dequeue_async(call
);
1234 if (deadline
< threshold
) {
1236 * This timer needs moving (escalating)
1237 * to the local (boot) processor's queue.
1241 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1242 DECR_TIMER_OVERDUE
| DBG_FUNC_NONE
,
1249 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1250 DECR_TIMER_ESCALATE
| DBG_FUNC_NONE
,
1252 TCE(call
)->deadline
,
1253 TCE(call
)->entry_time
,
1257 timer_call_entry_dequeue(call
);
1258 timer_call_entry_enqueue_deadline(
1259 call
, timer_master_queue
, TCE(call
)->deadline
);
1261 * A side-effect of the following call is to update
1262 * the actual hardware deadline if required.
1264 (void) timer_queue_assign(deadline
);
1266 if (deadline
< tlp
->threshold
.deadline
) {
1267 tlp
->threshold
.deadline
= deadline
;
1268 tlp
->threshold
.call
= call
;
1271 simple_unlock(&call
->lock
);
1274 timer_queue_unlock(timer_master_queue
);
1278 timer_longterm_callout(timer_call_param_t p0
, __unused timer_call_param_t p1
)
1280 timer_longterm_t
*tlp
= (timer_longterm_t
*) p0
;
1282 timer_longterm_update(tlp
);
1286 timer_longterm_update_locked(timer_longterm_t
*tlp
)
1290 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1291 DECR_TIMER_UPDATE
| DBG_FUNC_START
,
1293 tlp
->threshold
.deadline
,
1294 tlp
->threshold
.preempted
,
1295 tlp
->queue
.count
, 0);
1297 tlp
->scan_time
= mach_absolute_time();
1298 if (tlp
->threshold
.preempted
!= TIMER_LONGTERM_NONE
) {
1299 tlp
->threshold
.preempts
++;
1300 tlp
->threshold
.deadline
= tlp
->threshold
.preempted
;
1301 tlp
->threshold
.preempted
= TIMER_LONGTERM_NONE
;
1303 * Note: in the unlikely event that a pre-empted timer has
1304 * itself been cancelled, we'll simply re-scan later at the
1305 * time of the preempted/cancelled timer.
1308 tlp
->threshold
.scans
++;
1311 * Maintain a moving average of our wakeup latency.
1312 * Clamp latency to 0 and ignore above threshold interval.
1314 if (tlp
->scan_time
> tlp
->threshold
.deadline_set
)
1315 latency
= tlp
->scan_time
- tlp
->threshold
.deadline_set
;
1318 if (latency
< tlp
->threshold
.interval
) {
1319 tlp
->threshold
.latency_min
=
1320 MIN(tlp
->threshold
.latency_min
, latency
);
1321 tlp
->threshold
.latency_max
=
1322 MAX(tlp
->threshold
.latency_max
, latency
);
1323 tlp
->threshold
.latency
=
1324 (tlp
->threshold
.latency
*99 + latency
) / 100;
1327 timer_longterm_scan(tlp
, tlp
->scan_time
);
1330 tlp
->threshold
.deadline_set
= tlp
->threshold
.deadline
;
1331 /* The next deadline timer to be set is adjusted */
1332 if (tlp
->threshold
.deadline
!= TIMER_LONGTERM_NONE
) {
1333 tlp
->threshold
.deadline_set
-= tlp
->threshold
.margin
;
1334 tlp
->threshold
.deadline_set
-= tlp
->threshold
.latency
;
1337 TIMER_KDEBUG_TRACE(KDEBUG_TRACE
,
1338 DECR_TIMER_UPDATE
| DBG_FUNC_END
,
1340 tlp
->threshold
.deadline
,
1341 tlp
->threshold
.scans
,
1342 tlp
->queue
.count
, 0);
1346 timer_longterm_update(timer_longterm_t
*tlp
)
1348 spl_t s
= splclock();
1350 timer_queue_lock_spin(timer_longterm_queue
);
1352 if (cpu_number() != master_cpu
)
1353 panic("timer_longterm_update_master() on non-boot cpu");
1355 timer_longterm_update_locked(tlp
);
1357 if (tlp
->threshold
.deadline
!= TIMER_LONGTERM_NONE
)
1359 &tlp
->threshold
.timer
,
1360 tlp
->threshold
.deadline_set
,
1361 TIMER_CALL_LOCAL
| TIMER_CALL_SYS_CRITICAL
);
1363 timer_queue_unlock(timer_longterm_queue
);
1368 timer_longterm_init(void)
1371 timer_longterm_t
*tlp
= &timer_longterm
;
1373 DBG("timer_longterm_init() tlp: %p, queue: %p\n", tlp
, &tlp
->queue
);
1376 * Set the longterm timer threshold. Defaults to TIMER_LONGTERM_THRESHOLD
1377 * or TIMER_LONGTERM_NONE (disabled) for server;
1378 * overridden longterm boot-arg
1380 tlp
->threshold
.interval
= serverperfmode
? TIMER_LONGTERM_NONE
1381 : TIMER_LONGTERM_THRESHOLD
;
1382 if (PE_parse_boot_argn("longterm", &longterm
, sizeof (longterm
))) {
1383 tlp
->threshold
.interval
= (longterm
== 0) ?
1384 TIMER_LONGTERM_NONE
:
1385 longterm
* NSEC_PER_MSEC
;
1387 if (tlp
->threshold
.interval
!= TIMER_LONGTERM_NONE
) {
1388 printf("Longterm timer threshold: %llu ms\n",
1389 tlp
->threshold
.interval
/ NSEC_PER_MSEC
);
1390 kprintf("Longterm timer threshold: %llu ms\n",
1391 tlp
->threshold
.interval
/ NSEC_PER_MSEC
);
1392 nanoseconds_to_absolutetime(tlp
->threshold
.interval
,
1393 &tlp
->threshold
.interval
);
1394 tlp
->threshold
.margin
= tlp
->threshold
.interval
/ 10;
1395 tlp
->threshold
.latency_min
= EndOfAllTime
;
1396 tlp
->threshold
.latency_max
= 0;
1399 tlp
->threshold
.preempted
= TIMER_LONGTERM_NONE
;
1400 tlp
->threshold
.deadline
= TIMER_LONGTERM_NONE
;
1402 lck_attr_setdefault(&timer_longterm_lck_attr
);
1403 lck_grp_attr_setdefault(&timer_longterm_lck_grp_attr
);
1404 lck_grp_init(&timer_longterm_lck_grp
,
1405 "timer_longterm", &timer_longterm_lck_grp_attr
);
1406 mpqueue_init(&tlp
->queue
,
1407 &timer_longterm_lck_grp
, &timer_longterm_lck_attr
);
1409 timer_call_setup(&tlp
->threshold
.timer
,
1410 timer_longterm_callout
, (timer_call_param_t
) tlp
);
1412 timer_longterm_queue
= &tlp
->queue
;
1417 ENQUEUES
, DEQUEUES
, ESCALATES
, SCANS
, PREEMPTS
,
1418 LATENCY
, LATENCY_MIN
, LATENCY_MAX
1421 timer_sysctl_get(int oid
)
1423 timer_longterm_t
*tlp
= &timer_longterm
;
1427 return (tlp
->threshold
.interval
== TIMER_LONGTERM_NONE
) ?
1428 0 : tlp
->threshold
.interval
/ NSEC_PER_MSEC
;
1430 return tlp
->queue
.count
;
1432 return tlp
->enqueues
;
1434 return tlp
->dequeues
;
1436 return tlp
->escalates
;
1438 return tlp
->threshold
.scans
;
1440 return tlp
->threshold
.preempts
;
1442 return tlp
->threshold
.latency
;
1444 return tlp
->threshold
.latency_min
;
1446 return tlp
->threshold
.latency_max
;
1453 * timer_master_scan() is the inverse of timer_longterm_scan()
1454 * since it un-escalates timers to the longterm queue.
1457 timer_master_scan(timer_longterm_t
*tlp
,
1464 mpqueue_head_t
*timer_master_queue
;
1466 if (tlp
->threshold
.interval
!= TIMER_LONGTERM_NONE
)
1467 threshold
= now
+ tlp
->threshold
.interval
;
1469 threshold
= TIMER_LONGTERM_NONE
;
1471 timer_master_queue
= timer_queue_cpu(master_cpu
);
1472 timer_queue_lock_spin(timer_master_queue
);
1474 qe
= queue_first(&timer_master_queue
->head
);
1475 while (!queue_end(&timer_master_queue
->head
, qe
)) {
1476 call
= TIMER_CALL(qe
);
1477 deadline
= TCE(call
)->deadline
;
1478 qe
= queue_next(qe
);
1479 if ((call
->flags
& TIMER_CALL_LOCAL
) != 0)
1481 if (!simple_lock_try(&call
->lock
)) {
1482 /* case (2c) lock order inversion, dequeue only */
1483 timer_call_entry_dequeue_async(call
);
1486 if (deadline
> threshold
) {
1487 /* move from master to longterm */
1488 timer_call_entry_dequeue(call
);
1489 timer_call_entry_enqueue_tail(call
, timer_longterm_queue
);
1490 if (deadline
< tlp
->threshold
.deadline
) {
1491 tlp
->threshold
.deadline
= deadline
;
1492 tlp
->threshold
.call
= call
;
1495 simple_unlock(&call
->lock
);
1497 timer_queue_unlock(timer_master_queue
);
1501 timer_sysctl_set_threshold(uint64_t value
)
1503 timer_longterm_t
*tlp
= &timer_longterm
;
1504 spl_t s
= splclock();
1505 boolean_t threshold_increase
;
1507 timer_queue_lock_spin(timer_longterm_queue
);
1509 timer_call_cancel(&tlp
->threshold
.timer
);
1512 * Set the new threshold and note whther it's increasing.
1515 tlp
->threshold
.interval
= TIMER_LONGTERM_NONE
;
1516 threshold_increase
= TRUE
;
1517 timer_call_cancel(&tlp
->threshold
.timer
);
1519 uint64_t old_interval
= tlp
->threshold
.interval
;
1520 tlp
->threshold
.interval
= value
* NSEC_PER_MSEC
;
1521 nanoseconds_to_absolutetime(tlp
->threshold
.interval
,
1522 &tlp
->threshold
.interval
);
1523 tlp
->threshold
.margin
= tlp
->threshold
.interval
/ 10;
1524 if (old_interval
== TIMER_LONGTERM_NONE
)
1525 threshold_increase
= FALSE
;
1527 threshold_increase
= (tlp
->threshold
.interval
> old_interval
);
1530 if (threshold_increase
/* or removal */) {
1531 /* Escalate timers from the longterm queue */
1532 timer_longterm_scan(tlp
, mach_absolute_time());
1533 } else /* decrease or addition */ {
1535 * We scan the local/master queue for timers now longterm.
1536 * To be strictly correct, we should scan all processor queues
1537 * but timer migration results in most timers gravitating to the
1538 * master processor in any case.
1540 timer_master_scan(tlp
, mach_absolute_time());
1543 /* Set new timer accordingly */
1544 tlp
->threshold
.deadline_set
= tlp
->threshold
.deadline
;
1545 if (tlp
->threshold
.deadline
!= TIMER_LONGTERM_NONE
) {
1546 tlp
->threshold
.deadline_set
-= tlp
->threshold
.margin
;
1547 tlp
->threshold
.deadline_set
-= tlp
->threshold
.latency
;
1549 &tlp
->threshold
.timer
,
1550 tlp
->threshold
.deadline_set
,
1551 TIMER_CALL_LOCAL
| TIMER_CALL_SYS_CRITICAL
);
1558 tlp
->threshold
.scans
= 0;
1559 tlp
->threshold
.preempts
= 0;
1560 tlp
->threshold
.latency
= 0;
1561 tlp
->threshold
.latency_min
= EndOfAllTime
;
1562 tlp
->threshold
.latency_max
= 0;
1564 timer_queue_unlock(timer_longterm_queue
);
1569 timer_sysctl_set(int oid
, uint64_t value
)
1575 (void (*)(void *)) timer_sysctl_set_threshold
,
1577 return KERN_SUCCESS
;
1579 return KERN_INVALID_ARGUMENT
;
1584 /* Select timer coalescing window based on per-task quality-of-service hints */
1585 static boolean_t
tcoal_qos_adjust(thread_t t
, int32_t *tshift
, uint64_t *tmax_abstime
, boolean_t
*pratelimited
) {
1586 uint32_t latency_qos
;
1587 boolean_t adjusted
= FALSE
;
1588 task_t ctask
= t
->task
;
1591 latency_qos
= proc_get_effective_thread_policy(t
, TASK_POLICY_LATENCY_QOS
);
1593 assert(latency_qos
<= NUM_LATENCY_QOS_TIERS
);
1596 *tshift
= tcoal_prio_params
.latency_qos_scale
[latency_qos
- 1];
1597 *tmax_abstime
= tcoal_prio_params
.latency_qos_abstime_max
[latency_qos
- 1];
1598 *pratelimited
= tcoal_prio_params
.latency_tier_rate_limited
[latency_qos
- 1];
1606 /* Adjust timer deadlines based on priority of the thread and the
1607 * urgency value provided at timeout establishment. With this mechanism,
1608 * timers are no longer necessarily sorted in order of soft deadline
1609 * on a given timer queue, i.e. they may be differentially skewed.
1610 * In the current scheme, this could lead to fewer pending timers
1611 * processed than is technically possible when the HW deadline arrives.
1614 timer_compute_leeway(thread_t cthread
, int32_t urgency
, int32_t *tshift
, uint64_t *tmax_abstime
, boolean_t
*pratelimited
) {
1615 int16_t tpri
= cthread
->sched_pri
;
1616 if ((urgency
& TIMER_CALL_USER_MASK
) != 0) {
1617 if (tpri
>= BASEPRI_RTQUEUES
||
1618 urgency
== TIMER_CALL_USER_CRITICAL
) {
1619 *tshift
= tcoal_prio_params
.timer_coalesce_rt_shift
;
1620 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_rt_abstime_max
;
1621 TCOAL_PRIO_STAT(rt_tcl
);
1622 } else if (proc_get_effective_thread_policy(cthread
, TASK_POLICY_DARWIN_BG
) ||
1623 (urgency
== TIMER_CALL_USER_BACKGROUND
)) {
1624 /* Determine if timer should be subjected to a lower QoS */
1625 if (tcoal_qos_adjust(cthread
, tshift
, tmax_abstime
, pratelimited
)) {
1626 if (*tmax_abstime
> tcoal_prio_params
.timer_coalesce_bg_abstime_max
) {
1629 *pratelimited
= FALSE
;
1632 *tshift
= tcoal_prio_params
.timer_coalesce_bg_shift
;
1633 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_bg_abstime_max
;
1634 TCOAL_PRIO_STAT(bg_tcl
);
1635 } else if (tpri
>= MINPRI_KERNEL
) {
1636 *tshift
= tcoal_prio_params
.timer_coalesce_kt_shift
;
1637 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_kt_abstime_max
;
1638 TCOAL_PRIO_STAT(kt_tcl
);
1639 } else if (cthread
->sched_mode
== TH_MODE_FIXED
) {
1640 *tshift
= tcoal_prio_params
.timer_coalesce_fp_shift
;
1641 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_fp_abstime_max
;
1642 TCOAL_PRIO_STAT(fp_tcl
);
1643 } else if (tcoal_qos_adjust(cthread
, tshift
, tmax_abstime
, pratelimited
)) {
1644 TCOAL_PRIO_STAT(qos_tcl
);
1645 } else if (cthread
->sched_mode
== TH_MODE_TIMESHARE
) {
1646 *tshift
= tcoal_prio_params
.timer_coalesce_ts_shift
;
1647 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_ts_abstime_max
;
1648 TCOAL_PRIO_STAT(ts_tcl
);
1650 TCOAL_PRIO_STAT(nc_tcl
);
1652 } else if (urgency
== TIMER_CALL_SYS_BACKGROUND
) {
1653 *tshift
= tcoal_prio_params
.timer_coalesce_bg_shift
;
1654 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_bg_abstime_max
;
1655 TCOAL_PRIO_STAT(bg_tcl
);
1657 *tshift
= tcoal_prio_params
.timer_coalesce_kt_shift
;
1658 *tmax_abstime
= tcoal_prio_params
.timer_coalesce_kt_abstime_max
;
1659 TCOAL_PRIO_STAT(kt_tcl
);
1664 int timer_user_idle_level
;
1667 timer_call_slop(uint64_t deadline
, uint64_t now
, uint32_t flags
, thread_t cthread
, boolean_t
*pratelimited
)
1669 int32_t tcs_shift
= 0;
1670 uint64_t tcs_max_abstime
= 0;
1672 uint32_t urgency
= (flags
& TIMER_CALL_URGENCY_MASK
);
1674 if (mach_timer_coalescing_enabled
&&
1675 (deadline
> now
) && (urgency
!= TIMER_CALL_SYS_CRITICAL
)) {
1676 timer_compute_leeway(cthread
, urgency
, &tcs_shift
, &tcs_max_abstime
, pratelimited
);
1679 adjval
= MIN((deadline
- now
) >> tcs_shift
, tcs_max_abstime
);
1681 adjval
= MIN((deadline
- now
) << (-tcs_shift
), tcs_max_abstime
);
1682 /* Apply adjustments derived from "user idle level" heuristic */
1683 adjval
+= (adjval
* timer_user_idle_level
) >> 7;
1691 timer_get_user_idle_level(void) {
1692 return timer_user_idle_level
;
1695 kern_return_t
timer_set_user_idle_level(int ilevel
) {
1696 boolean_t do_reeval
= FALSE
;
1698 if ((ilevel
< 0) || (ilevel
> 128))
1699 return KERN_INVALID_ARGUMENT
;
1701 if (ilevel
< timer_user_idle_level
) {
1705 timer_user_idle_level
= ilevel
;
1708 ml_timer_evaluate();
1710 return KERN_SUCCESS
;