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"
41 /***********************************************************/
44 #define QEMU_CLOCK_REALTIME 0
45 #define QEMU_CLOCK_VIRTUAL 1
46 #define QEMU_CLOCK_HOST 2
49 QEMUTimer
*active_timers
;
51 NotifierList reset_notifiers
;
59 int64_t expire_time
; /* in nanoseconds */
67 struct qemu_alarm_timer
{
69 int (*start
)(struct qemu_alarm_timer
*t
);
70 void (*stop
)(struct qemu_alarm_timer
*t
);
71 void (*rearm
)(struct qemu_alarm_timer
*t
, int64_t nearest_delta_ns
);
72 #if defined(__linux__)
82 static struct qemu_alarm_timer
*alarm_timer
;
84 static bool qemu_timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
86 return timer_head
&& (timer_head
->expire_time
<= current_time
);
89 static int64_t qemu_next_alarm_deadline(void)
91 int64_t delta
= INT64_MAX
;
94 if (!use_icount
&& vm_clock
->enabled
&& vm_clock
->active_timers
) {
95 delta
= vm_clock
->active_timers
->expire_time
-
96 qemu_get_clock_ns(vm_clock
);
98 if (host_clock
->enabled
&& host_clock
->active_timers
) {
99 int64_t hdelta
= host_clock
->active_timers
->expire_time
-
100 qemu_get_clock_ns(host_clock
);
101 if (hdelta
< delta
) {
105 if (rt_clock
->enabled
&& rt_clock
->active_timers
) {
106 rtdelta
= (rt_clock
->active_timers
->expire_time
-
107 qemu_get_clock_ns(rt_clock
));
108 if (rtdelta
< delta
) {
116 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
118 int64_t nearest_delta_ns
= qemu_next_alarm_deadline();
119 if (nearest_delta_ns
< INT64_MAX
) {
120 t
->rearm(t
, nearest_delta_ns
);
124 /* TODO: MIN_TIMER_REARM_NS should be optimized */
125 #define MIN_TIMER_REARM_NS 250000
129 static int mm_start_timer(struct qemu_alarm_timer
*t
);
130 static void mm_stop_timer(struct qemu_alarm_timer
*t
);
131 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
133 static int win32_start_timer(struct qemu_alarm_timer
*t
);
134 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
135 static void win32_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
139 static int unix_start_timer(struct qemu_alarm_timer
*t
);
140 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
141 static void unix_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
145 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
146 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
147 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
149 #endif /* __linux__ */
153 static struct qemu_alarm_timer alarm_timers
[] = {
156 {"dynticks", dynticks_start_timer
,
157 dynticks_stop_timer
, dynticks_rearm_timer
},
159 {"unix", unix_start_timer
, unix_stop_timer
, unix_rearm_timer
},
161 {"mmtimer", mm_start_timer
, mm_stop_timer
, mm_rearm_timer
},
162 {"dynticks", win32_start_timer
, win32_stop_timer
, win32_rearm_timer
},
167 static void show_available_alarms(void)
171 printf("Available alarm timers, in order of precedence:\n");
172 for (i
= 0; alarm_timers
[i
].name
; i
++)
173 printf("%s\n", alarm_timers
[i
].name
);
176 void configure_alarms(char const *opt
)
180 int count
= ARRAY_SIZE(alarm_timers
) - 1;
183 struct qemu_alarm_timer tmp
;
185 if (is_help_option(opt
)) {
186 show_available_alarms();
192 /* Reorder the array */
193 name
= strtok(arg
, ",");
195 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
196 if (!strcmp(alarm_timers
[i
].name
, name
))
201 fprintf(stderr
, "Unknown clock %s\n", name
);
210 tmp
= alarm_timers
[i
];
211 alarm_timers
[i
] = alarm_timers
[cur
];
212 alarm_timers
[cur
] = tmp
;
216 name
= strtok(NULL
, ",");
222 /* Disable remaining timers */
223 for (i
= cur
; i
< count
; i
++)
224 alarm_timers
[i
].name
= NULL
;
226 show_available_alarms();
233 QEMUClock
*host_clock
;
235 static QEMUClock
*qemu_new_clock(int type
)
239 clock
= g_malloc0(sizeof(QEMUClock
));
241 clock
->enabled
= true;
242 clock
->last
= INT64_MIN
;
243 notifier_list_init(&clock
->reset_notifiers
);
247 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
)
249 bool old
= clock
->enabled
;
250 clock
->enabled
= enabled
;
251 if (enabled
&& !old
) {
252 qemu_rearm_alarm_timer(alarm_timer
);
256 int64_t qemu_clock_has_timers(QEMUClock
*clock
)
258 return !!clock
->active_timers
;
261 int64_t qemu_clock_expired(QEMUClock
*clock
)
263 return (clock
->active_timers
&&
264 clock
->active_timers
->expire_time
< qemu_get_clock_ns(clock
));
267 int64_t qemu_clock_deadline(QEMUClock
*clock
)
269 /* To avoid problems with overflow limit this to 2^32. */
270 int64_t delta
= INT32_MAX
;
272 if (clock
->active_timers
) {
273 delta
= clock
->active_timers
->expire_time
- qemu_get_clock_ns(clock
);
281 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
282 QEMUTimerCB
*cb
, void *opaque
)
286 ts
= g_malloc0(sizeof(QEMUTimer
));
294 void qemu_free_timer(QEMUTimer
*ts
)
299 /* stop a timer, but do not dealloc it */
300 void qemu_del_timer(QEMUTimer
*ts
)
304 /* NOTE: this code must be signal safe because
305 qemu_timer_expired() can be called from a signal. */
306 pt
= &ts
->clock
->active_timers
;
319 /* modify the current timer so that it will be fired when current_time
320 >= expire_time. The corresponding callback will be called. */
321 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
327 /* add the timer in the sorted list */
328 /* NOTE: this code must be signal safe because
329 qemu_timer_expired() can be called from a signal. */
330 pt
= &ts
->clock
->active_timers
;
333 if (!qemu_timer_expired_ns(t
, expire_time
)) {
338 ts
->expire_time
= expire_time
;
342 /* Rearm if necessary */
343 if (pt
== &ts
->clock
->active_timers
) {
344 if (!alarm_timer
->pending
) {
345 qemu_rearm_alarm_timer(alarm_timer
);
347 /* Interrupt execution to force deadline recalculation. */
348 qemu_clock_warp(ts
->clock
);
355 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
357 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
360 bool qemu_timer_pending(QEMUTimer
*ts
)
363 for (t
= ts
->clock
->active_timers
; t
!= NULL
; t
= t
->next
) {
371 bool qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
373 return qemu_timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
376 void qemu_run_timers(QEMUClock
*clock
)
379 int64_t current_time
;
384 current_time
= qemu_get_clock_ns(clock
);
386 ts
= clock
->active_timers
;
387 if (!qemu_timer_expired_ns(ts
, current_time
)) {
390 /* remove timer from the list before calling the callback */
391 clock
->active_timers
= ts
->next
;
394 /* run the callback (the timer list can be modified) */
399 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
403 switch(clock
->type
) {
404 case QEMU_CLOCK_REALTIME
:
407 case QEMU_CLOCK_VIRTUAL
:
409 return cpu_get_icount();
411 return cpu_get_clock();
413 case QEMU_CLOCK_HOST
:
414 now
= get_clock_realtime();
418 notifier_list_notify(&clock
->reset_notifiers
, &now
);
424 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
426 notifier_list_add(&clock
->reset_notifiers
, notifier
);
429 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
431 notifier_remove(notifier
);
434 void init_clocks(void)
437 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
438 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
439 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
443 uint64_t qemu_timer_expire_time_ns(QEMUTimer
*ts
)
445 return qemu_timer_pending(ts
) ? ts
->expire_time
: -1;
448 void qemu_run_all_timers(void)
450 alarm_timer
->pending
= false;
453 qemu_run_timers(vm_clock
);
454 qemu_run_timers(rt_clock
);
455 qemu_run_timers(host_clock
);
457 /* rearm timer, if not periodic */
458 if (alarm_timer
->expired
) {
459 alarm_timer
->expired
= false;
460 qemu_rearm_alarm_timer(alarm_timer
);
465 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
467 static void host_alarm_handler(int host_signum
)
470 struct qemu_alarm_timer
*t
= alarm_timer
;
479 #if defined(__linux__)
481 #include "compatfd.h"
483 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
487 struct sigaction act
;
489 sigfillset(&act
.sa_mask
);
491 act
.sa_handler
= host_alarm_handler
;
493 sigaction(SIGALRM
, &act
, NULL
);
496 * Initialize ev struct to 0 to avoid valgrind complaining
497 * about uninitialized data in timer_create call
499 memset(&ev
, 0, sizeof(ev
));
500 ev
.sigev_value
.sival_int
= 0;
501 ev
.sigev_notify
= SIGEV_SIGNAL
;
502 #ifdef CONFIG_SIGEV_THREAD_ID
503 if (qemu_signalfd_available()) {
504 ev
.sigev_notify
= SIGEV_THREAD_ID
;
505 ev
._sigev_un
._tid
= qemu_get_thread_id();
507 #endif /* CONFIG_SIGEV_THREAD_ID */
508 ev
.sigev_signo
= SIGALRM
;
510 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
511 perror("timer_create");
515 t
->timer
= host_timer
;
520 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
522 timer_t host_timer
= t
->timer
;
524 timer_delete(host_timer
);
527 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
,
528 int64_t nearest_delta_ns
)
530 timer_t host_timer
= t
->timer
;
531 struct itimerspec timeout
;
534 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
535 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
537 /* check whether a timer is already running */
538 if (timer_gettime(host_timer
, &timeout
)) {
540 fprintf(stderr
, "Internal timer error: aborting\n");
543 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
544 if (current_ns
&& current_ns
<= nearest_delta_ns
)
547 timeout
.it_interval
.tv_sec
= 0;
548 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
549 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
550 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
551 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
553 fprintf(stderr
, "Internal timer error: aborting\n");
558 #endif /* defined(__linux__) */
562 static int unix_start_timer(struct qemu_alarm_timer
*t
)
564 struct sigaction act
;
567 sigfillset(&act
.sa_mask
);
569 act
.sa_handler
= host_alarm_handler
;
571 sigaction(SIGALRM
, &act
, NULL
);
575 static void unix_rearm_timer(struct qemu_alarm_timer
*t
,
576 int64_t nearest_delta_ns
)
578 struct itimerval itv
;
581 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
582 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
584 itv
.it_interval
.tv_sec
= 0;
585 itv
.it_interval
.tv_usec
= 0; /* 0 for one-shot timer */
586 itv
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
587 itv
.it_value
.tv_usec
= (nearest_delta_ns
% 1000000000) / 1000;
588 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
591 fprintf(stderr
, "Internal timer error: aborting\n");
596 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
598 struct itimerval itv
;
600 memset(&itv
, 0, sizeof(itv
));
601 setitimer(ITIMER_REAL
, &itv
, NULL
);
604 #endif /* !defined(_WIN32) */
609 static MMRESULT mm_timer
;
610 static TIMECAPS mm_tc
;
612 static void CALLBACK
mm_alarm_handler(UINT uTimerID
, UINT uMsg
,
613 DWORD_PTR dwUser
, DWORD_PTR dw1
,
616 struct qemu_alarm_timer
*t
= alarm_timer
;
625 static int mm_start_timer(struct qemu_alarm_timer
*t
)
627 timeGetDevCaps(&mm_tc
, sizeof(mm_tc
));
629 timeBeginPeriod(mm_tc
.wPeriodMin
);
631 mm_timer
= timeSetEvent(mm_tc
.wPeriodMin
, /* interval (ms) */
632 mm_tc
.wPeriodMin
, /* resolution */
633 mm_alarm_handler
, /* function */
634 (DWORD_PTR
)t
, /* parameter */
635 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
638 fprintf(stderr
, "Failed to initialize win32 alarm timer\n");
639 timeEndPeriod(mm_tc
.wPeriodMin
);
646 static void mm_stop_timer(struct qemu_alarm_timer
*t
)
648 timeKillEvent(mm_timer
);
649 timeEndPeriod(mm_tc
.wPeriodMin
);
652 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
)
654 int64_t nearest_delta_ms
= delta
/ 1000000;
655 if (nearest_delta_ms
< mm_tc
.wPeriodMin
) {
656 nearest_delta_ms
= mm_tc
.wPeriodMin
;
657 } else if (nearest_delta_ms
> mm_tc
.wPeriodMax
) {
658 nearest_delta_ms
= mm_tc
.wPeriodMax
;
661 timeKillEvent(mm_timer
);
662 mm_timer
= timeSetEvent((UINT
)nearest_delta_ms
,
666 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
669 fprintf(stderr
, "Failed to re-arm win32 alarm timer\n");
670 timeEndPeriod(mm_tc
.wPeriodMin
);
675 static int win32_start_timer(struct qemu_alarm_timer
*t
)
680 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
681 is zero) that has already expired, the timer is not updated. Since
682 creating a new timer is relatively expensive, set a bogus one-hour
683 interval in the dynticks case. */
684 success
= CreateTimerQueueTimer(&hTimer
,
690 WT_EXECUTEINTIMERTHREAD
);
693 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
702 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
704 HANDLE hTimer
= t
->timer
;
707 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
711 static void win32_rearm_timer(struct qemu_alarm_timer
*t
,
712 int64_t nearest_delta_ns
)
714 HANDLE hTimer
= t
->timer
;
715 int64_t nearest_delta_ms
;
718 nearest_delta_ms
= nearest_delta_ns
/ 1000000;
719 if (nearest_delta_ms
< 1) {
720 nearest_delta_ms
= 1;
722 /* ULONG_MAX can be 32 bit */
723 if (nearest_delta_ms
> ULONG_MAX
) {
724 nearest_delta_ms
= ULONG_MAX
;
726 success
= ChangeTimerQueueTimer(NULL
,
728 (unsigned long) nearest_delta_ms
,
732 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
741 static void quit_timers(void)
743 struct qemu_alarm_timer
*t
= alarm_timer
;
749 static void reinit_timers(void)
751 struct qemu_alarm_timer
*t
= alarm_timer
;
754 fprintf(stderr
, "Internal timer error: aborting\n");
757 qemu_rearm_alarm_timer(t
);
759 #endif /* CONFIG_POSIX */
761 int init_timer_alarm(void)
763 struct qemu_alarm_timer
*t
= NULL
;
770 for (i
= 0; alarm_timers
[i
].name
; i
++) {
771 t
= &alarm_timers
[i
];
785 pthread_atfork(NULL
, NULL
, reinit_timers
);