4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 #include "qemu-timer.h"
35 #include <sys/param.h>
42 /***********************************************************/
45 #define QEMU_CLOCK_REALTIME 0
46 #define QEMU_CLOCK_VIRTUAL 1
47 #define QEMU_CLOCK_HOST 2
50 QEMUTimer
*active_timers
;
52 NotifierList reset_notifiers
;
60 int64_t expire_time
; /* in nanoseconds */
68 struct qemu_alarm_timer
{
70 int (*start
)(struct qemu_alarm_timer
*t
);
71 void (*stop
)(struct qemu_alarm_timer
*t
);
72 void (*rearm
)(struct qemu_alarm_timer
*t
, int64_t nearest_delta_ns
);
73 #if defined(__linux__)
83 static struct qemu_alarm_timer
*alarm_timer
;
85 static bool qemu_timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
87 return timer_head
&& (timer_head
->expire_time
<= current_time
);
90 static int64_t qemu_next_alarm_deadline(void)
92 int64_t delta
= INT64_MAX
;
95 if (!use_icount
&& vm_clock
->enabled
&& vm_clock
->active_timers
) {
96 delta
= vm_clock
->active_timers
->expire_time
-
97 qemu_get_clock_ns(vm_clock
);
99 if (host_clock
->enabled
&& host_clock
->active_timers
) {
100 int64_t hdelta
= host_clock
->active_timers
->expire_time
-
101 qemu_get_clock_ns(host_clock
);
102 if (hdelta
< delta
) {
106 if (rt_clock
->enabled
&& rt_clock
->active_timers
) {
107 rtdelta
= (rt_clock
->active_timers
->expire_time
-
108 qemu_get_clock_ns(rt_clock
));
109 if (rtdelta
< delta
) {
117 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
119 int64_t nearest_delta_ns
;
120 if (!rt_clock
->active_timers
&&
121 !vm_clock
->active_timers
&&
122 !host_clock
->active_timers
) {
125 nearest_delta_ns
= qemu_next_alarm_deadline();
126 t
->rearm(t
, nearest_delta_ns
);
129 /* TODO: MIN_TIMER_REARM_NS should be optimized */
130 #define MIN_TIMER_REARM_NS 250000
134 static int mm_start_timer(struct qemu_alarm_timer
*t
);
135 static void mm_stop_timer(struct qemu_alarm_timer
*t
);
136 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
138 static int win32_start_timer(struct qemu_alarm_timer
*t
);
139 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
140 static void win32_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
144 static int unix_start_timer(struct qemu_alarm_timer
*t
);
145 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
146 static void unix_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
150 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
151 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
152 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
154 #endif /* __linux__ */
158 static struct qemu_alarm_timer alarm_timers
[] = {
161 {"dynticks", dynticks_start_timer
,
162 dynticks_stop_timer
, dynticks_rearm_timer
},
164 {"unix", unix_start_timer
, unix_stop_timer
, unix_rearm_timer
},
166 {"mmtimer", mm_start_timer
, mm_stop_timer
, mm_rearm_timer
},
167 {"dynticks", win32_start_timer
, win32_stop_timer
, win32_rearm_timer
},
172 static void show_available_alarms(void)
176 printf("Available alarm timers, in order of precedence:\n");
177 for (i
= 0; alarm_timers
[i
].name
; i
++)
178 printf("%s\n", alarm_timers
[i
].name
);
181 void configure_alarms(char const *opt
)
185 int count
= ARRAY_SIZE(alarm_timers
) - 1;
188 struct qemu_alarm_timer tmp
;
190 if (!strcmp(opt
, "?")) {
191 show_available_alarms();
197 /* Reorder the array */
198 name
= strtok(arg
, ",");
200 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
201 if (!strcmp(alarm_timers
[i
].name
, name
))
206 fprintf(stderr
, "Unknown clock %s\n", name
);
215 tmp
= alarm_timers
[i
];
216 alarm_timers
[i
] = alarm_timers
[cur
];
217 alarm_timers
[cur
] = tmp
;
221 name
= strtok(NULL
, ",");
227 /* Disable remaining timers */
228 for (i
= cur
; i
< count
; i
++)
229 alarm_timers
[i
].name
= NULL
;
231 show_available_alarms();
238 QEMUClock
*host_clock
;
240 static QEMUClock
*qemu_new_clock(int type
)
244 clock
= g_malloc0(sizeof(QEMUClock
));
246 clock
->enabled
= true;
247 clock
->last
= INT64_MIN
;
248 notifier_list_init(&clock
->reset_notifiers
);
252 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
)
254 bool old
= clock
->enabled
;
255 clock
->enabled
= enabled
;
256 if (enabled
&& !old
) {
257 qemu_rearm_alarm_timer(alarm_timer
);
261 int64_t qemu_clock_has_timers(QEMUClock
*clock
)
263 return !!clock
->active_timers
;
266 int64_t qemu_clock_expired(QEMUClock
*clock
)
268 return (clock
->active_timers
&&
269 clock
->active_timers
->expire_time
< qemu_get_clock_ns(clock
));
272 int64_t qemu_clock_deadline(QEMUClock
*clock
)
274 /* To avoid problems with overflow limit this to 2^32. */
275 int64_t delta
= INT32_MAX
;
277 if (clock
->active_timers
) {
278 delta
= clock
->active_timers
->expire_time
- qemu_get_clock_ns(clock
);
286 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
287 QEMUTimerCB
*cb
, void *opaque
)
291 ts
= g_malloc0(sizeof(QEMUTimer
));
299 void qemu_free_timer(QEMUTimer
*ts
)
304 /* stop a timer, but do not dealloc it */
305 void qemu_del_timer(QEMUTimer
*ts
)
309 /* NOTE: this code must be signal safe because
310 qemu_timer_expired() can be called from a signal. */
311 pt
= &ts
->clock
->active_timers
;
324 /* modify the current timer so that it will be fired when current_time
325 >= expire_time. The corresponding callback will be called. */
326 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
332 /* add the timer in the sorted list */
333 /* NOTE: this code must be signal safe because
334 qemu_timer_expired() can be called from a signal. */
335 pt
= &ts
->clock
->active_timers
;
338 if (!qemu_timer_expired_ns(t
, expire_time
)) {
343 ts
->expire_time
= expire_time
;
347 /* Rearm if necessary */
348 if (pt
== &ts
->clock
->active_timers
) {
349 if (!alarm_timer
->pending
) {
350 qemu_rearm_alarm_timer(alarm_timer
);
352 /* Interrupt execution to force deadline recalculation. */
353 qemu_clock_warp(ts
->clock
);
360 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
362 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
365 bool qemu_timer_pending(QEMUTimer
*ts
)
368 for (t
= ts
->clock
->active_timers
; t
!= NULL
; t
= t
->next
) {
376 bool qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
378 return qemu_timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
381 void qemu_run_timers(QEMUClock
*clock
)
383 QEMUTimer
**ptimer_head
, *ts
;
384 int64_t current_time
;
389 current_time
= qemu_get_clock_ns(clock
);
390 ptimer_head
= &clock
->active_timers
;
393 if (!qemu_timer_expired_ns(ts
, current_time
)) {
396 /* remove timer from the list before calling the callback */
397 *ptimer_head
= ts
->next
;
400 /* run the callback (the timer list can be modified) */
405 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
409 switch(clock
->type
) {
410 case QEMU_CLOCK_REALTIME
:
413 case QEMU_CLOCK_VIRTUAL
:
415 return cpu_get_icount();
417 return cpu_get_clock();
419 case QEMU_CLOCK_HOST
:
420 now
= get_clock_realtime();
424 notifier_list_notify(&clock
->reset_notifiers
, &now
);
430 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
432 notifier_list_add(&clock
->reset_notifiers
, notifier
);
435 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
437 notifier_remove(notifier
);
440 void init_clocks(void)
442 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
443 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
444 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
447 uint64_t qemu_timer_expire_time_ns(QEMUTimer
*ts
)
449 return qemu_timer_pending(ts
) ? ts
->expire_time
: -1;
452 void qemu_run_all_timers(void)
454 alarm_timer
->pending
= false;
457 qemu_run_timers(vm_clock
);
458 qemu_run_timers(rt_clock
);
459 qemu_run_timers(host_clock
);
461 /* rearm timer, if not periodic */
462 if (alarm_timer
->expired
) {
463 alarm_timer
->expired
= false;
464 qemu_rearm_alarm_timer(alarm_timer
);
469 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
471 static void host_alarm_handler(int host_signum
)
474 struct qemu_alarm_timer
*t
= alarm_timer
;
483 #if defined(__linux__)
485 #include "compatfd.h"
487 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
491 struct sigaction act
;
493 sigfillset(&act
.sa_mask
);
495 act
.sa_handler
= host_alarm_handler
;
497 sigaction(SIGALRM
, &act
, NULL
);
500 * Initialize ev struct to 0 to avoid valgrind complaining
501 * about uninitialized data in timer_create call
503 memset(&ev
, 0, sizeof(ev
));
504 ev
.sigev_value
.sival_int
= 0;
505 ev
.sigev_notify
= SIGEV_SIGNAL
;
506 #ifdef SIGEV_THREAD_ID
507 if (qemu_signalfd_available()) {
508 ev
.sigev_notify
= SIGEV_THREAD_ID
;
509 ev
._sigev_un
._tid
= qemu_get_thread_id();
511 #endif /* SIGEV_THREAD_ID */
512 ev
.sigev_signo
= SIGALRM
;
514 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
515 perror("timer_create");
519 t
->timer
= host_timer
;
524 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
526 timer_t host_timer
= t
->timer
;
528 timer_delete(host_timer
);
531 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
,
532 int64_t nearest_delta_ns
)
534 timer_t host_timer
= t
->timer
;
535 struct itimerspec timeout
;
538 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
539 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
541 /* check whether a timer is already running */
542 if (timer_gettime(host_timer
, &timeout
)) {
544 fprintf(stderr
, "Internal timer error: aborting\n");
547 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
548 if (current_ns
&& current_ns
<= nearest_delta_ns
)
551 timeout
.it_interval
.tv_sec
= 0;
552 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
553 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
554 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
555 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
557 fprintf(stderr
, "Internal timer error: aborting\n");
562 #endif /* defined(__linux__) */
566 static int unix_start_timer(struct qemu_alarm_timer
*t
)
568 struct sigaction act
;
571 sigfillset(&act
.sa_mask
);
573 act
.sa_handler
= host_alarm_handler
;
575 sigaction(SIGALRM
, &act
, NULL
);
579 static void unix_rearm_timer(struct qemu_alarm_timer
*t
,
580 int64_t nearest_delta_ns
)
582 struct itimerval itv
;
585 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
586 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
588 itv
.it_interval
.tv_sec
= 0;
589 itv
.it_interval
.tv_usec
= 0; /* 0 for one-shot timer */
590 itv
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
591 itv
.it_value
.tv_usec
= (nearest_delta_ns
% 1000000000) / 1000;
592 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
595 fprintf(stderr
, "Internal timer error: aborting\n");
600 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
602 struct itimerval itv
;
604 memset(&itv
, 0, sizeof(itv
));
605 setitimer(ITIMER_REAL
, &itv
, NULL
);
608 #endif /* !defined(_WIN32) */
613 static MMRESULT mm_timer
;
614 static unsigned mm_period
;
616 static void CALLBACK
mm_alarm_handler(UINT uTimerID
, UINT uMsg
,
617 DWORD_PTR dwUser
, DWORD_PTR dw1
,
620 struct qemu_alarm_timer
*t
= alarm_timer
;
629 static int mm_start_timer(struct qemu_alarm_timer
*t
)
633 memset(&tc
, 0, sizeof(tc
));
634 timeGetDevCaps(&tc
, sizeof(tc
));
636 mm_period
= tc
.wPeriodMin
;
637 timeBeginPeriod(mm_period
);
639 mm_timer
= timeSetEvent(1, /* interval (ms) */
640 mm_period
, /* resolution */
641 mm_alarm_handler
, /* function */
642 (DWORD_PTR
)t
, /* parameter */
643 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
646 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
648 timeEndPeriod(mm_period
);
655 static void mm_stop_timer(struct qemu_alarm_timer
*t
)
657 timeKillEvent(mm_timer
);
658 timeEndPeriod(mm_period
);
661 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
)
663 int64_t nearest_delta_ms
= delta
/ 1000000;
664 if (nearest_delta_ms
< 1) {
665 nearest_delta_ms
= 1;
667 /* UINT_MAX can be 32 bit */
668 if (nearest_delta_ms
> UINT_MAX
) {
669 nearest_delta_ms
= UINT_MAX
;
672 timeKillEvent(mm_timer
);
673 mm_timer
= timeSetEvent((unsigned int) nearest_delta_ms
,
677 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
680 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
683 timeEndPeriod(mm_period
);
688 static int win32_start_timer(struct qemu_alarm_timer
*t
)
693 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
694 is zero) that has already expired, the timer is not updated. Since
695 creating a new timer is relatively expensive, set a bogus one-hour
696 interval in the dynticks case. */
697 success
= CreateTimerQueueTimer(&hTimer
,
703 WT_EXECUTEINTIMERTHREAD
);
706 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
715 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
717 HANDLE hTimer
= t
->timer
;
720 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
724 static void win32_rearm_timer(struct qemu_alarm_timer
*t
,
725 int64_t nearest_delta_ns
)
727 HANDLE hTimer
= t
->timer
;
728 int64_t nearest_delta_ms
;
731 nearest_delta_ms
= nearest_delta_ns
/ 1000000;
732 if (nearest_delta_ms
< 1) {
733 nearest_delta_ms
= 1;
735 /* ULONG_MAX can be 32 bit */
736 if (nearest_delta_ms
> ULONG_MAX
) {
737 nearest_delta_ms
= ULONG_MAX
;
739 success
= ChangeTimerQueueTimer(NULL
,
741 (unsigned long) nearest_delta_ms
,
745 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
754 static void quit_timers(void)
756 struct qemu_alarm_timer
*t
= alarm_timer
;
761 int init_timer_alarm(void)
763 struct qemu_alarm_timer
*t
= NULL
;
766 for (i
= 0; alarm_timers
[i
].name
; i
++) {
767 t
= &alarm_timers
[i
];
779 /* first event is at time 0 */