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"
38 /***********************************************************/
41 #define QEMU_CLOCK_REALTIME 0
42 #define QEMU_CLOCK_VIRTUAL 1
43 #define QEMU_CLOCK_HOST 2
46 QEMUTimer
*active_timers
;
48 NotifierList reset_notifiers
;
56 int64_t expire_time
; /* in nanoseconds */
64 struct qemu_alarm_timer
{
66 int (*start
)(struct qemu_alarm_timer
*t
);
67 void (*stop
)(struct qemu_alarm_timer
*t
);
68 void (*rearm
)(struct qemu_alarm_timer
*t
, int64_t nearest_delta_ns
);
69 #if defined(__linux__)
79 static struct qemu_alarm_timer
*alarm_timer
;
81 static bool qemu_timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
83 return timer_head
&& (timer_head
->expire_time
<= current_time
);
86 static int64_t qemu_next_alarm_deadline(void)
88 int64_t delta
= INT64_MAX
;
91 if (!use_icount
&& vm_clock
->enabled
&& vm_clock
->active_timers
) {
92 delta
= vm_clock
->active_timers
->expire_time
-
93 qemu_get_clock_ns(vm_clock
);
95 if (host_clock
->enabled
&& host_clock
->active_timers
) {
96 int64_t hdelta
= host_clock
->active_timers
->expire_time
-
97 qemu_get_clock_ns(host_clock
);
102 if (rt_clock
->enabled
&& rt_clock
->active_timers
) {
103 rtdelta
= (rt_clock
->active_timers
->expire_time
-
104 qemu_get_clock_ns(rt_clock
));
105 if (rtdelta
< delta
) {
113 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
115 int64_t nearest_delta_ns
= qemu_next_alarm_deadline();
116 if (nearest_delta_ns
< INT64_MAX
) {
117 t
->rearm(t
, nearest_delta_ns
);
121 /* TODO: MIN_TIMER_REARM_NS should be optimized */
122 #define MIN_TIMER_REARM_NS 250000
126 static int mm_start_timer(struct qemu_alarm_timer
*t
);
127 static void mm_stop_timer(struct qemu_alarm_timer
*t
);
128 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
130 static int win32_start_timer(struct qemu_alarm_timer
*t
);
131 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
132 static void win32_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
136 static int unix_start_timer(struct qemu_alarm_timer
*t
);
137 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
138 static void unix_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
142 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
143 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
144 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
146 #endif /* __linux__ */
150 static struct qemu_alarm_timer alarm_timers
[] = {
153 {"dynticks", dynticks_start_timer
,
154 dynticks_stop_timer
, dynticks_rearm_timer
},
156 {"unix", unix_start_timer
, unix_stop_timer
, unix_rearm_timer
},
158 {"mmtimer", mm_start_timer
, mm_stop_timer
, mm_rearm_timer
},
159 {"dynticks", win32_start_timer
, win32_stop_timer
, win32_rearm_timer
},
164 static void show_available_alarms(void)
168 printf("Available alarm timers, in order of precedence:\n");
169 for (i
= 0; alarm_timers
[i
].name
; i
++)
170 printf("%s\n", alarm_timers
[i
].name
);
173 void configure_alarms(char const *opt
)
177 int count
= ARRAY_SIZE(alarm_timers
) - 1;
180 struct qemu_alarm_timer tmp
;
182 if (is_help_option(opt
)) {
183 show_available_alarms();
189 /* Reorder the array */
190 name
= strtok(arg
, ",");
192 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
193 if (!strcmp(alarm_timers
[i
].name
, name
))
198 fprintf(stderr
, "Unknown clock %s\n", name
);
207 tmp
= alarm_timers
[i
];
208 alarm_timers
[i
] = alarm_timers
[cur
];
209 alarm_timers
[cur
] = tmp
;
213 name
= strtok(NULL
, ",");
219 /* Disable remaining timers */
220 for (i
= cur
; i
< count
; i
++)
221 alarm_timers
[i
].name
= NULL
;
223 show_available_alarms();
230 QEMUClock
*host_clock
;
232 static QEMUClock
*qemu_new_clock(int type
)
236 clock
= g_malloc0(sizeof(QEMUClock
));
238 clock
->enabled
= true;
239 clock
->last
= INT64_MIN
;
240 notifier_list_init(&clock
->reset_notifiers
);
244 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
)
246 bool old
= clock
->enabled
;
247 clock
->enabled
= enabled
;
248 if (enabled
&& !old
) {
249 qemu_rearm_alarm_timer(alarm_timer
);
253 int64_t qemu_clock_has_timers(QEMUClock
*clock
)
255 return !!clock
->active_timers
;
258 int64_t qemu_clock_expired(QEMUClock
*clock
)
260 return (clock
->active_timers
&&
261 clock
->active_timers
->expire_time
< qemu_get_clock_ns(clock
));
264 int64_t qemu_clock_deadline(QEMUClock
*clock
)
266 /* To avoid problems with overflow limit this to 2^32. */
267 int64_t delta
= INT32_MAX
;
269 if (clock
->active_timers
) {
270 delta
= clock
->active_timers
->expire_time
- qemu_get_clock_ns(clock
);
278 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
279 QEMUTimerCB
*cb
, void *opaque
)
283 ts
= g_malloc0(sizeof(QEMUTimer
));
291 void qemu_free_timer(QEMUTimer
*ts
)
296 /* stop a timer, but do not dealloc it */
297 void qemu_del_timer(QEMUTimer
*ts
)
301 /* NOTE: this code must be signal safe because
302 qemu_timer_expired() can be called from a signal. */
303 pt
= &ts
->clock
->active_timers
;
316 /* modify the current timer so that it will be fired when current_time
317 >= expire_time. The corresponding callback will be called. */
318 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
324 /* add the timer in the sorted list */
325 /* NOTE: this code must be signal safe because
326 qemu_timer_expired() can be called from a signal. */
327 pt
= &ts
->clock
->active_timers
;
330 if (!qemu_timer_expired_ns(t
, expire_time
)) {
335 ts
->expire_time
= expire_time
;
339 /* Rearm if necessary */
340 if (pt
== &ts
->clock
->active_timers
) {
341 if (!alarm_timer
->pending
) {
342 qemu_rearm_alarm_timer(alarm_timer
);
344 /* Interrupt execution to force deadline recalculation. */
345 qemu_clock_warp(ts
->clock
);
352 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
354 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
357 bool qemu_timer_pending(QEMUTimer
*ts
)
360 for (t
= ts
->clock
->active_timers
; t
!= NULL
; t
= t
->next
) {
368 bool qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
370 return qemu_timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
373 void qemu_run_timers(QEMUClock
*clock
)
376 int64_t current_time
;
381 current_time
= qemu_get_clock_ns(clock
);
383 ts
= clock
->active_timers
;
384 if (!qemu_timer_expired_ns(ts
, current_time
)) {
387 /* remove timer from the list before calling the callback */
388 clock
->active_timers
= ts
->next
;
391 /* run the callback (the timer list can be modified) */
396 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
400 switch(clock
->type
) {
401 case QEMU_CLOCK_REALTIME
:
404 case QEMU_CLOCK_VIRTUAL
:
406 return cpu_get_icount();
408 return cpu_get_clock();
410 case QEMU_CLOCK_HOST
:
411 now
= get_clock_realtime();
415 notifier_list_notify(&clock
->reset_notifiers
, &now
);
421 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
423 notifier_list_add(&clock
->reset_notifiers
, notifier
);
426 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
428 notifier_remove(notifier
);
431 void init_clocks(void)
434 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
435 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
436 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
440 uint64_t qemu_timer_expire_time_ns(QEMUTimer
*ts
)
442 return qemu_timer_pending(ts
) ? ts
->expire_time
: -1;
445 void qemu_run_all_timers(void)
447 alarm_timer
->pending
= false;
450 qemu_run_timers(vm_clock
);
451 qemu_run_timers(rt_clock
);
452 qemu_run_timers(host_clock
);
454 /* rearm timer, if not periodic */
455 if (alarm_timer
->expired
) {
456 alarm_timer
->expired
= false;
457 qemu_rearm_alarm_timer(alarm_timer
);
462 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
464 static void host_alarm_handler(int host_signum
)
467 struct qemu_alarm_timer
*t
= alarm_timer
;
476 #if defined(__linux__)
478 #include "compatfd.h"
480 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
484 struct sigaction act
;
486 sigfillset(&act
.sa_mask
);
488 act
.sa_handler
= host_alarm_handler
;
490 sigaction(SIGALRM
, &act
, NULL
);
493 * Initialize ev struct to 0 to avoid valgrind complaining
494 * about uninitialized data in timer_create call
496 memset(&ev
, 0, sizeof(ev
));
497 ev
.sigev_value
.sival_int
= 0;
498 ev
.sigev_notify
= SIGEV_SIGNAL
;
499 #ifdef SIGEV_THREAD_ID
500 if (qemu_signalfd_available()) {
501 ev
.sigev_notify
= SIGEV_THREAD_ID
;
502 ev
._sigev_un
._tid
= qemu_get_thread_id();
504 #endif /* SIGEV_THREAD_ID */
505 ev
.sigev_signo
= SIGALRM
;
507 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
508 perror("timer_create");
512 t
->timer
= host_timer
;
517 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
519 timer_t host_timer
= t
->timer
;
521 timer_delete(host_timer
);
524 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
,
525 int64_t nearest_delta_ns
)
527 timer_t host_timer
= t
->timer
;
528 struct itimerspec timeout
;
531 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
532 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
534 /* check whether a timer is already running */
535 if (timer_gettime(host_timer
, &timeout
)) {
537 fprintf(stderr
, "Internal timer error: aborting\n");
540 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
541 if (current_ns
&& current_ns
<= nearest_delta_ns
)
544 timeout
.it_interval
.tv_sec
= 0;
545 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
546 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
547 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
548 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
550 fprintf(stderr
, "Internal timer error: aborting\n");
555 #endif /* defined(__linux__) */
559 static int unix_start_timer(struct qemu_alarm_timer
*t
)
561 struct sigaction act
;
564 sigfillset(&act
.sa_mask
);
566 act
.sa_handler
= host_alarm_handler
;
568 sigaction(SIGALRM
, &act
, NULL
);
572 static void unix_rearm_timer(struct qemu_alarm_timer
*t
,
573 int64_t nearest_delta_ns
)
575 struct itimerval itv
;
578 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
579 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
581 itv
.it_interval
.tv_sec
= 0;
582 itv
.it_interval
.tv_usec
= 0; /* 0 for one-shot timer */
583 itv
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
584 itv
.it_value
.tv_usec
= (nearest_delta_ns
% 1000000000) / 1000;
585 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
588 fprintf(stderr
, "Internal timer error: aborting\n");
593 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
595 struct itimerval itv
;
597 memset(&itv
, 0, sizeof(itv
));
598 setitimer(ITIMER_REAL
, &itv
, NULL
);
601 #endif /* !defined(_WIN32) */
606 static MMRESULT mm_timer
;
607 static TIMECAPS mm_tc
;
609 static void CALLBACK
mm_alarm_handler(UINT uTimerID
, UINT uMsg
,
610 DWORD_PTR dwUser
, DWORD_PTR dw1
,
613 struct qemu_alarm_timer
*t
= alarm_timer
;
622 static int mm_start_timer(struct qemu_alarm_timer
*t
)
624 timeGetDevCaps(&mm_tc
, sizeof(mm_tc
));
626 timeBeginPeriod(mm_tc
.wPeriodMin
);
628 mm_timer
= timeSetEvent(mm_tc
.wPeriodMin
, /* interval (ms) */
629 mm_tc
.wPeriodMin
, /* resolution */
630 mm_alarm_handler
, /* function */
631 (DWORD_PTR
)t
, /* parameter */
632 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
635 fprintf(stderr
, "Failed to initialize win32 alarm timer\n");
636 timeEndPeriod(mm_tc
.wPeriodMin
);
643 static void mm_stop_timer(struct qemu_alarm_timer
*t
)
645 timeKillEvent(mm_timer
);
646 timeEndPeriod(mm_tc
.wPeriodMin
);
649 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
)
651 int64_t nearest_delta_ms
= delta
/ 1000000;
652 if (nearest_delta_ms
< mm_tc
.wPeriodMin
) {
653 nearest_delta_ms
= mm_tc
.wPeriodMin
;
654 } else if (nearest_delta_ms
> mm_tc
.wPeriodMax
) {
655 nearest_delta_ms
= mm_tc
.wPeriodMax
;
658 timeKillEvent(mm_timer
);
659 mm_timer
= timeSetEvent((UINT
)nearest_delta_ms
,
663 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
666 fprintf(stderr
, "Failed to re-arm win32 alarm timer\n");
667 timeEndPeriod(mm_tc
.wPeriodMin
);
672 static int win32_start_timer(struct qemu_alarm_timer
*t
)
677 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
678 is zero) that has already expired, the timer is not updated. Since
679 creating a new timer is relatively expensive, set a bogus one-hour
680 interval in the dynticks case. */
681 success
= CreateTimerQueueTimer(&hTimer
,
687 WT_EXECUTEINTIMERTHREAD
);
690 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
699 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
701 HANDLE hTimer
= t
->timer
;
704 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
708 static void win32_rearm_timer(struct qemu_alarm_timer
*t
,
709 int64_t nearest_delta_ns
)
711 HANDLE hTimer
= t
->timer
;
712 int64_t nearest_delta_ms
;
715 nearest_delta_ms
= nearest_delta_ns
/ 1000000;
716 if (nearest_delta_ms
< 1) {
717 nearest_delta_ms
= 1;
719 /* ULONG_MAX can be 32 bit */
720 if (nearest_delta_ms
> ULONG_MAX
) {
721 nearest_delta_ms
= ULONG_MAX
;
723 success
= ChangeTimerQueueTimer(NULL
,
725 (unsigned long) nearest_delta_ms
,
729 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
738 static void quit_timers(void)
740 struct qemu_alarm_timer
*t
= alarm_timer
;
745 int init_timer_alarm(void)
747 struct qemu_alarm_timer
*t
= NULL
;
754 for (i
= 0; alarm_timers
[i
].name
; i
++) {
755 t
= &alarm_timers
[i
];