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
27 #include "ui/console.h"
31 #include "qemu-timer.h"
40 /***********************************************************/
43 #define QEMU_CLOCK_REALTIME 0
44 #define QEMU_CLOCK_VIRTUAL 1
45 #define QEMU_CLOCK_HOST 2
48 QEMUTimer
*active_timers
;
50 NotifierList reset_notifiers
;
58 int64_t expire_time
; /* in nanoseconds */
66 struct qemu_alarm_timer
{
68 int (*start
)(struct qemu_alarm_timer
*t
);
69 void (*stop
)(struct qemu_alarm_timer
*t
);
70 void (*rearm
)(struct qemu_alarm_timer
*t
, int64_t nearest_delta_ns
);
71 #if defined(__linux__)
81 static struct qemu_alarm_timer
*alarm_timer
;
83 static bool qemu_timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
85 return timer_head
&& (timer_head
->expire_time
<= current_time
);
88 static int64_t qemu_next_alarm_deadline(void)
90 int64_t delta
= INT64_MAX
;
93 if (!use_icount
&& vm_clock
->enabled
&& vm_clock
->active_timers
) {
94 delta
= vm_clock
->active_timers
->expire_time
-
95 qemu_get_clock_ns(vm_clock
);
97 if (host_clock
->enabled
&& host_clock
->active_timers
) {
98 int64_t hdelta
= host_clock
->active_timers
->expire_time
-
99 qemu_get_clock_ns(host_clock
);
100 if (hdelta
< delta
) {
104 if (rt_clock
->enabled
&& rt_clock
->active_timers
) {
105 rtdelta
= (rt_clock
->active_timers
->expire_time
-
106 qemu_get_clock_ns(rt_clock
));
107 if (rtdelta
< delta
) {
115 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
117 int64_t nearest_delta_ns
= qemu_next_alarm_deadline();
118 if (nearest_delta_ns
< INT64_MAX
) {
119 t
->rearm(t
, nearest_delta_ns
);
123 /* TODO: MIN_TIMER_REARM_NS should be optimized */
124 #define MIN_TIMER_REARM_NS 250000
128 static int mm_start_timer(struct qemu_alarm_timer
*t
);
129 static void mm_stop_timer(struct qemu_alarm_timer
*t
);
130 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
132 static int win32_start_timer(struct qemu_alarm_timer
*t
);
133 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
134 static void win32_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
138 static int unix_start_timer(struct qemu_alarm_timer
*t
);
139 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
140 static void unix_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
144 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
145 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
146 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
148 #endif /* __linux__ */
152 static struct qemu_alarm_timer alarm_timers
[] = {
155 {"dynticks", dynticks_start_timer
,
156 dynticks_stop_timer
, dynticks_rearm_timer
},
158 {"unix", unix_start_timer
, unix_stop_timer
, unix_rearm_timer
},
160 {"mmtimer", mm_start_timer
, mm_stop_timer
, mm_rearm_timer
},
161 {"dynticks", win32_start_timer
, win32_stop_timer
, win32_rearm_timer
},
166 static void show_available_alarms(void)
170 printf("Available alarm timers, in order of precedence:\n");
171 for (i
= 0; alarm_timers
[i
].name
; i
++)
172 printf("%s\n", alarm_timers
[i
].name
);
175 void configure_alarms(char const *opt
)
179 int count
= ARRAY_SIZE(alarm_timers
) - 1;
182 struct qemu_alarm_timer tmp
;
184 if (is_help_option(opt
)) {
185 show_available_alarms();
191 /* Reorder the array */
192 name
= strtok(arg
, ",");
194 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
195 if (!strcmp(alarm_timers
[i
].name
, name
))
200 fprintf(stderr
, "Unknown clock %s\n", name
);
209 tmp
= alarm_timers
[i
];
210 alarm_timers
[i
] = alarm_timers
[cur
];
211 alarm_timers
[cur
] = tmp
;
215 name
= strtok(NULL
, ",");
221 /* Disable remaining timers */
222 for (i
= cur
; i
< count
; i
++)
223 alarm_timers
[i
].name
= NULL
;
225 show_available_alarms();
232 QEMUClock
*host_clock
;
234 static QEMUClock
*qemu_new_clock(int type
)
238 clock
= g_malloc0(sizeof(QEMUClock
));
240 clock
->enabled
= true;
241 clock
->last
= INT64_MIN
;
242 notifier_list_init(&clock
->reset_notifiers
);
246 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
)
248 bool old
= clock
->enabled
;
249 clock
->enabled
= enabled
;
250 if (enabled
&& !old
) {
251 qemu_rearm_alarm_timer(alarm_timer
);
255 int64_t qemu_clock_has_timers(QEMUClock
*clock
)
257 return !!clock
->active_timers
;
260 int64_t qemu_clock_expired(QEMUClock
*clock
)
262 return (clock
->active_timers
&&
263 clock
->active_timers
->expire_time
< qemu_get_clock_ns(clock
));
266 int64_t qemu_clock_deadline(QEMUClock
*clock
)
268 /* To avoid problems with overflow limit this to 2^32. */
269 int64_t delta
= INT32_MAX
;
271 if (clock
->active_timers
) {
272 delta
= clock
->active_timers
->expire_time
- qemu_get_clock_ns(clock
);
280 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
281 QEMUTimerCB
*cb
, void *opaque
)
285 ts
= g_malloc0(sizeof(QEMUTimer
));
293 void qemu_free_timer(QEMUTimer
*ts
)
298 /* stop a timer, but do not dealloc it */
299 void qemu_del_timer(QEMUTimer
*ts
)
303 /* NOTE: this code must be signal safe because
304 qemu_timer_expired() can be called from a signal. */
305 pt
= &ts
->clock
->active_timers
;
318 /* modify the current timer so that it will be fired when current_time
319 >= expire_time. The corresponding callback will be called. */
320 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
326 /* add the timer in the sorted list */
327 /* NOTE: this code must be signal safe because
328 qemu_timer_expired() can be called from a signal. */
329 pt
= &ts
->clock
->active_timers
;
332 if (!qemu_timer_expired_ns(t
, expire_time
)) {
337 ts
->expire_time
= expire_time
;
341 /* Rearm if necessary */
342 if (pt
== &ts
->clock
->active_timers
) {
343 if (!alarm_timer
->pending
) {
344 qemu_rearm_alarm_timer(alarm_timer
);
346 /* Interrupt execution to force deadline recalculation. */
347 qemu_clock_warp(ts
->clock
);
354 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
356 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
359 bool qemu_timer_pending(QEMUTimer
*ts
)
362 for (t
= ts
->clock
->active_timers
; t
!= NULL
; t
= t
->next
) {
370 bool qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
372 return qemu_timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
375 void qemu_run_timers(QEMUClock
*clock
)
378 int64_t current_time
;
383 current_time
= qemu_get_clock_ns(clock
);
385 ts
= clock
->active_timers
;
386 if (!qemu_timer_expired_ns(ts
, current_time
)) {
389 /* remove timer from the list before calling the callback */
390 clock
->active_timers
= ts
->next
;
393 /* run the callback (the timer list can be modified) */
398 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
402 switch(clock
->type
) {
403 case QEMU_CLOCK_REALTIME
:
406 case QEMU_CLOCK_VIRTUAL
:
408 return cpu_get_icount();
410 return cpu_get_clock();
412 case QEMU_CLOCK_HOST
:
413 now
= get_clock_realtime();
417 notifier_list_notify(&clock
->reset_notifiers
, &now
);
423 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
425 notifier_list_add(&clock
->reset_notifiers
, notifier
);
428 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
430 notifier_remove(notifier
);
433 void init_clocks(void)
436 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
437 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
438 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
442 uint64_t qemu_timer_expire_time_ns(QEMUTimer
*ts
)
444 return qemu_timer_pending(ts
) ? ts
->expire_time
: -1;
447 void qemu_run_all_timers(void)
449 alarm_timer
->pending
= false;
452 qemu_run_timers(vm_clock
);
453 qemu_run_timers(rt_clock
);
454 qemu_run_timers(host_clock
);
456 /* rearm timer, if not periodic */
457 if (alarm_timer
->expired
) {
458 alarm_timer
->expired
= false;
459 qemu_rearm_alarm_timer(alarm_timer
);
464 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
466 static void host_alarm_handler(int host_signum
)
469 struct qemu_alarm_timer
*t
= alarm_timer
;
478 #if defined(__linux__)
480 #include "compatfd.h"
482 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
486 struct sigaction act
;
488 sigfillset(&act
.sa_mask
);
490 act
.sa_handler
= host_alarm_handler
;
492 sigaction(SIGALRM
, &act
, NULL
);
495 * Initialize ev struct to 0 to avoid valgrind complaining
496 * about uninitialized data in timer_create call
498 memset(&ev
, 0, sizeof(ev
));
499 ev
.sigev_value
.sival_int
= 0;
500 ev
.sigev_notify
= SIGEV_SIGNAL
;
501 #ifdef CONFIG_SIGEV_THREAD_ID
502 if (qemu_signalfd_available()) {
503 ev
.sigev_notify
= SIGEV_THREAD_ID
;
504 ev
._sigev_un
._tid
= qemu_get_thread_id();
506 #endif /* CONFIG_SIGEV_THREAD_ID */
507 ev
.sigev_signo
= SIGALRM
;
509 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
510 perror("timer_create");
514 t
->timer
= host_timer
;
519 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
521 timer_t host_timer
= t
->timer
;
523 timer_delete(host_timer
);
526 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
,
527 int64_t nearest_delta_ns
)
529 timer_t host_timer
= t
->timer
;
530 struct itimerspec timeout
;
533 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
534 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
536 /* check whether a timer is already running */
537 if (timer_gettime(host_timer
, &timeout
)) {
539 fprintf(stderr
, "Internal timer error: aborting\n");
542 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
543 if (current_ns
&& current_ns
<= nearest_delta_ns
)
546 timeout
.it_interval
.tv_sec
= 0;
547 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
548 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
549 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
550 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
552 fprintf(stderr
, "Internal timer error: aborting\n");
557 #endif /* defined(__linux__) */
561 static int unix_start_timer(struct qemu_alarm_timer
*t
)
563 struct sigaction act
;
566 sigfillset(&act
.sa_mask
);
568 act
.sa_handler
= host_alarm_handler
;
570 sigaction(SIGALRM
, &act
, NULL
);
574 static void unix_rearm_timer(struct qemu_alarm_timer
*t
,
575 int64_t nearest_delta_ns
)
577 struct itimerval itv
;
580 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
581 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
583 itv
.it_interval
.tv_sec
= 0;
584 itv
.it_interval
.tv_usec
= 0; /* 0 for one-shot timer */
585 itv
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
586 itv
.it_value
.tv_usec
= (nearest_delta_ns
% 1000000000) / 1000;
587 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
590 fprintf(stderr
, "Internal timer error: aborting\n");
595 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
597 struct itimerval itv
;
599 memset(&itv
, 0, sizeof(itv
));
600 setitimer(ITIMER_REAL
, &itv
, NULL
);
603 #endif /* !defined(_WIN32) */
608 static MMRESULT mm_timer
;
609 static TIMECAPS mm_tc
;
611 static void CALLBACK
mm_alarm_handler(UINT uTimerID
, UINT uMsg
,
612 DWORD_PTR dwUser
, DWORD_PTR dw1
,
615 struct qemu_alarm_timer
*t
= alarm_timer
;
624 static int mm_start_timer(struct qemu_alarm_timer
*t
)
626 timeGetDevCaps(&mm_tc
, sizeof(mm_tc
));
628 timeBeginPeriod(mm_tc
.wPeriodMin
);
630 mm_timer
= timeSetEvent(mm_tc
.wPeriodMin
, /* interval (ms) */
631 mm_tc
.wPeriodMin
, /* resolution */
632 mm_alarm_handler
, /* function */
633 (DWORD_PTR
)t
, /* parameter */
634 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
637 fprintf(stderr
, "Failed to initialize win32 alarm timer\n");
638 timeEndPeriod(mm_tc
.wPeriodMin
);
645 static void mm_stop_timer(struct qemu_alarm_timer
*t
)
647 timeKillEvent(mm_timer
);
648 timeEndPeriod(mm_tc
.wPeriodMin
);
651 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
)
653 int64_t nearest_delta_ms
= delta
/ 1000000;
654 if (nearest_delta_ms
< mm_tc
.wPeriodMin
) {
655 nearest_delta_ms
= mm_tc
.wPeriodMin
;
656 } else if (nearest_delta_ms
> mm_tc
.wPeriodMax
) {
657 nearest_delta_ms
= mm_tc
.wPeriodMax
;
660 timeKillEvent(mm_timer
);
661 mm_timer
= timeSetEvent((UINT
)nearest_delta_ms
,
665 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
668 fprintf(stderr
, "Failed to re-arm win32 alarm timer\n");
669 timeEndPeriod(mm_tc
.wPeriodMin
);
674 static int win32_start_timer(struct qemu_alarm_timer
*t
)
679 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
680 is zero) that has already expired, the timer is not updated. Since
681 creating a new timer is relatively expensive, set a bogus one-hour
682 interval in the dynticks case. */
683 success
= CreateTimerQueueTimer(&hTimer
,
689 WT_EXECUTEINTIMERTHREAD
);
692 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
701 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
703 HANDLE hTimer
= t
->timer
;
706 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
710 static void win32_rearm_timer(struct qemu_alarm_timer
*t
,
711 int64_t nearest_delta_ns
)
713 HANDLE hTimer
= t
->timer
;
714 int64_t nearest_delta_ms
;
717 nearest_delta_ms
= nearest_delta_ns
/ 1000000;
718 if (nearest_delta_ms
< 1) {
719 nearest_delta_ms
= 1;
721 /* ULONG_MAX can be 32 bit */
722 if (nearest_delta_ms
> ULONG_MAX
) {
723 nearest_delta_ms
= ULONG_MAX
;
725 success
= ChangeTimerQueueTimer(NULL
,
727 (unsigned long) nearest_delta_ms
,
731 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
740 static void quit_timers(void)
742 struct qemu_alarm_timer
*t
= alarm_timer
;
748 static void reinit_timers(void)
750 struct qemu_alarm_timer
*t
= alarm_timer
;
753 fprintf(stderr
, "Internal timer error: aborting\n");
756 qemu_rearm_alarm_timer(t
);
758 #endif /* CONFIG_POSIX */
760 int init_timer_alarm(void)
762 struct qemu_alarm_timer
*t
= NULL
;
769 for (i
= 0; alarm_timers
[i
].name
; i
++) {
770 t
= &alarm_timers
[i
];
784 pthread_atfork(NULL
, NULL
, reinit_timers
);