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
39 #include <sys/param.h>
47 #include "qemu-timer.h"
49 /***********************************************************/
52 #define QEMU_CLOCK_REALTIME 0
53 #define QEMU_CLOCK_VIRTUAL 1
54 #define QEMU_CLOCK_HOST 2
60 QEMUTimer
*active_timers
;
62 NotifierList reset_notifiers
;
68 int64_t expire_time
; /* in nanoseconds */
72 struct QEMUTimer
*next
;
75 struct qemu_alarm_timer
{
77 int (*start
)(struct qemu_alarm_timer
*t
);
78 void (*stop
)(struct qemu_alarm_timer
*t
);
79 void (*rearm
)(struct qemu_alarm_timer
*t
, int64_t nearest_delta_ns
);
80 #if defined(__linux__)
90 static struct qemu_alarm_timer
*alarm_timer
;
92 static bool qemu_timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
94 return timer_head
&& (timer_head
->expire_time
<= current_time
);
97 int qemu_alarm_pending(void)
99 return alarm_timer
->pending
;
102 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
107 static int64_t qemu_next_alarm_deadline(void)
112 if (!use_icount
&& vm_clock
->active_timers
) {
113 delta
= vm_clock
->active_timers
->expire_time
-
114 qemu_get_clock_ns(vm_clock
);
118 if (host_clock
->active_timers
) {
119 int64_t hdelta
= host_clock
->active_timers
->expire_time
-
120 qemu_get_clock_ns(host_clock
);
121 if (hdelta
< delta
) {
125 if (rt_clock
->active_timers
) {
126 rtdelta
= (rt_clock
->active_timers
->expire_time
-
127 qemu_get_clock_ns(rt_clock
));
128 if (rtdelta
< delta
) {
136 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
138 int64_t nearest_delta_ns
;
139 assert(alarm_has_dynticks(t
));
140 if (!rt_clock
->active_timers
&&
141 !vm_clock
->active_timers
&&
142 !host_clock
->active_timers
) {
145 nearest_delta_ns
= qemu_next_alarm_deadline();
146 t
->rearm(t
, nearest_delta_ns
);
149 /* TODO: MIN_TIMER_REARM_NS should be optimized */
150 #define MIN_TIMER_REARM_NS 250000
154 static int mm_start_timer(struct qemu_alarm_timer
*t
);
155 static void mm_stop_timer(struct qemu_alarm_timer
*t
);
156 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
158 static int win32_start_timer(struct qemu_alarm_timer
*t
);
159 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
160 static void win32_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
164 static int unix_start_timer(struct qemu_alarm_timer
*t
);
165 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
166 static void unix_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
170 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
171 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
172 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
);
174 #endif /* __linux__ */
178 static struct qemu_alarm_timer alarm_timers
[] = {
181 {"dynticks", dynticks_start_timer
,
182 dynticks_stop_timer
, dynticks_rearm_timer
},
184 {"unix", unix_start_timer
, unix_stop_timer
, unix_rearm_timer
},
186 {"mmtimer", mm_start_timer
, mm_stop_timer
, mm_rearm_timer
},
187 {"dynticks", win32_start_timer
, win32_stop_timer
, win32_rearm_timer
},
192 static void show_available_alarms(void)
196 printf("Available alarm timers, in order of precedence:\n");
197 for (i
= 0; alarm_timers
[i
].name
; i
++)
198 printf("%s\n", alarm_timers
[i
].name
);
201 void configure_alarms(char const *opt
)
205 int count
= ARRAY_SIZE(alarm_timers
) - 1;
208 struct qemu_alarm_timer tmp
;
210 if (!strcmp(opt
, "?")) {
211 show_available_alarms();
217 /* Reorder the array */
218 name
= strtok(arg
, ",");
220 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
221 if (!strcmp(alarm_timers
[i
].name
, name
))
226 fprintf(stderr
, "Unknown clock %s\n", name
);
235 tmp
= alarm_timers
[i
];
236 alarm_timers
[i
] = alarm_timers
[cur
];
237 alarm_timers
[cur
] = tmp
;
241 name
= strtok(NULL
, ",");
247 /* Disable remaining timers */
248 for (i
= cur
; i
< count
; i
++)
249 alarm_timers
[i
].name
= NULL
;
251 show_available_alarms();
258 QEMUClock
*host_clock
;
260 static QEMUClock
*qemu_new_clock(int type
)
264 clock
= g_malloc0(sizeof(QEMUClock
));
267 clock
->last
= INT64_MIN
;
268 notifier_list_init(&clock
->reset_notifiers
);
272 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
274 bool old
= clock
->enabled
;
275 clock
->enabled
= enabled
;
276 if (enabled
&& !old
) {
277 qemu_rearm_alarm_timer(alarm_timer
);
281 int64_t qemu_clock_has_timers(QEMUClock
*clock
)
283 return !!clock
->active_timers
;
286 int64_t qemu_clock_expired(QEMUClock
*clock
)
288 return (clock
->active_timers
&&
289 clock
->active_timers
->expire_time
< qemu_get_clock_ns(clock
));
292 int64_t qemu_clock_deadline(QEMUClock
*clock
)
294 /* To avoid problems with overflow limit this to 2^32. */
295 int64_t delta
= INT32_MAX
;
297 if (clock
->active_timers
) {
298 delta
= clock
->active_timers
->expire_time
- qemu_get_clock_ns(clock
);
306 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
307 QEMUTimerCB
*cb
, void *opaque
)
311 ts
= g_malloc0(sizeof(QEMUTimer
));
319 void qemu_free_timer(QEMUTimer
*ts
)
324 /* stop a timer, but do not dealloc it */
325 void qemu_del_timer(QEMUTimer
*ts
)
329 /* NOTE: this code must be signal safe because
330 qemu_timer_expired() can be called from a signal. */
331 pt
= &ts
->clock
->active_timers
;
344 /* modify the current timer so that it will be fired when current_time
345 >= expire_time. The corresponding callback will be called. */
346 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
352 /* add the timer in the sorted list */
353 /* NOTE: this code must be signal safe because
354 qemu_timer_expired() can be called from a signal. */
355 pt
= &ts
->clock
->active_timers
;
358 if (!qemu_timer_expired_ns(t
, expire_time
)) {
363 ts
->expire_time
= expire_time
;
367 /* Rearm if necessary */
368 if (pt
== &ts
->clock
->active_timers
) {
369 if (!alarm_timer
->pending
) {
370 qemu_rearm_alarm_timer(alarm_timer
);
372 /* Interrupt execution to force deadline recalculation. */
373 qemu_clock_warp(ts
->clock
);
380 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
382 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
385 int qemu_timer_pending(QEMUTimer
*ts
)
388 for (t
= ts
->clock
->active_timers
; t
!= NULL
; t
= t
->next
) {
395 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
397 return qemu_timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
400 void qemu_run_timers(QEMUClock
*clock
)
402 QEMUTimer
**ptimer_head
, *ts
;
403 int64_t current_time
;
408 current_time
= qemu_get_clock_ns(clock
);
409 ptimer_head
= &clock
->active_timers
;
412 if (!qemu_timer_expired_ns(ts
, current_time
)) {
415 /* remove timer from the list before calling the callback */
416 *ptimer_head
= ts
->next
;
419 /* run the callback (the timer list can be modified) */
424 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
428 switch(clock
->type
) {
429 case QEMU_CLOCK_REALTIME
:
432 case QEMU_CLOCK_VIRTUAL
:
434 return cpu_get_icount();
436 return cpu_get_clock();
438 case QEMU_CLOCK_HOST
:
439 now
= get_clock_realtime();
443 notifier_list_notify(&clock
->reset_notifiers
, &now
);
449 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
451 notifier_list_add(&clock
->reset_notifiers
, notifier
);
454 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
456 notifier_remove(notifier
);
459 void init_clocks(void)
461 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
462 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
463 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
466 uint64_t qemu_timer_expire_time_ns(QEMUTimer
*ts
)
468 return qemu_timer_pending(ts
) ? ts
->expire_time
: -1;
471 void qemu_run_all_timers(void)
473 alarm_timer
->pending
= 0;
476 qemu_run_timers(vm_clock
);
477 qemu_run_timers(rt_clock
);
478 qemu_run_timers(host_clock
);
480 /* rearm timer, if not periodic */
481 if (alarm_timer
->expired
) {
482 alarm_timer
->expired
= 0;
483 qemu_rearm_alarm_timer(alarm_timer
);
488 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
490 static void host_alarm_handler(int host_signum
)
493 struct qemu_alarm_timer
*t
= alarm_timer
;
497 if (alarm_has_dynticks(t
) ||
498 qemu_next_alarm_deadline () <= 0) {
499 t
->expired
= alarm_has_dynticks(t
);
505 #if defined(__linux__)
507 #include "compatfd.h"
509 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
513 struct sigaction act
;
515 sigfillset(&act
.sa_mask
);
517 act
.sa_handler
= host_alarm_handler
;
519 sigaction(SIGALRM
, &act
, NULL
);
522 * Initialize ev struct to 0 to avoid valgrind complaining
523 * about uninitialized data in timer_create call
525 memset(&ev
, 0, sizeof(ev
));
526 ev
.sigev_value
.sival_int
= 0;
527 ev
.sigev_notify
= SIGEV_SIGNAL
;
528 #ifdef SIGEV_THREAD_ID
529 if (qemu_signalfd_available()) {
530 ev
.sigev_notify
= SIGEV_THREAD_ID
;
531 ev
._sigev_un
._tid
= qemu_get_thread_id();
533 #endif /* SIGEV_THREAD_ID */
534 ev
.sigev_signo
= SIGALRM
;
536 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
537 perror("timer_create");
539 /* disable dynticks */
540 fprintf(stderr
, "Dynamic Ticks disabled\n");
545 t
->timer
= host_timer
;
550 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
552 timer_t host_timer
= t
->timer
;
554 timer_delete(host_timer
);
557 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
,
558 int64_t nearest_delta_ns
)
560 timer_t host_timer
= t
->timer
;
561 struct itimerspec timeout
;
564 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
565 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
567 /* check whether a timer is already running */
568 if (timer_gettime(host_timer
, &timeout
)) {
570 fprintf(stderr
, "Internal timer error: aborting\n");
573 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
574 if (current_ns
&& current_ns
<= nearest_delta_ns
)
577 timeout
.it_interval
.tv_sec
= 0;
578 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
579 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
580 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
581 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
583 fprintf(stderr
, "Internal timer error: aborting\n");
588 #endif /* defined(__linux__) */
592 static int unix_start_timer(struct qemu_alarm_timer
*t
)
594 struct sigaction act
;
597 sigfillset(&act
.sa_mask
);
599 act
.sa_handler
= host_alarm_handler
;
601 sigaction(SIGALRM
, &act
, NULL
);
605 static void unix_rearm_timer(struct qemu_alarm_timer
*t
,
606 int64_t nearest_delta_ns
)
608 struct itimerval itv
;
611 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
612 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
614 itv
.it_interval
.tv_sec
= 0;
615 itv
.it_interval
.tv_usec
= 0; /* 0 for one-shot timer */
616 itv
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
617 itv
.it_value
.tv_usec
= (nearest_delta_ns
% 1000000000) / 1000;
618 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
621 fprintf(stderr
, "Internal timer error: aborting\n");
626 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
628 struct itimerval itv
;
630 memset(&itv
, 0, sizeof(itv
));
631 setitimer(ITIMER_REAL
, &itv
, NULL
);
634 #endif /* !defined(_WIN32) */
639 static MMRESULT mm_timer
;
640 static unsigned mm_period
;
642 static void CALLBACK
mm_alarm_handler(UINT uTimerID
, UINT uMsg
,
643 DWORD_PTR dwUser
, DWORD_PTR dw1
,
646 struct qemu_alarm_timer
*t
= alarm_timer
;
650 if (alarm_has_dynticks(t
) || qemu_next_alarm_deadline() <= 0) {
651 t
->expired
= alarm_has_dynticks(t
);
657 static int mm_start_timer(struct qemu_alarm_timer
*t
)
662 memset(&tc
, 0, sizeof(tc
));
663 timeGetDevCaps(&tc
, sizeof(tc
));
665 mm_period
= tc
.wPeriodMin
;
666 timeBeginPeriod(mm_period
);
668 flags
= TIME_CALLBACK_FUNCTION
;
669 if (alarm_has_dynticks(t
)) {
670 flags
|= TIME_ONESHOT
;
672 flags
|= TIME_PERIODIC
;
675 mm_timer
= timeSetEvent(1, /* interval (ms) */
676 mm_period
, /* resolution */
677 mm_alarm_handler
, /* function */
678 (DWORD_PTR
)t
, /* parameter */
682 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
684 timeEndPeriod(mm_period
);
691 static void mm_stop_timer(struct qemu_alarm_timer
*t
)
693 timeKillEvent(mm_timer
);
694 timeEndPeriod(mm_period
);
697 static void mm_rearm_timer(struct qemu_alarm_timer
*t
, int64_t delta
)
699 int nearest_delta_ms
= (delta
+ 999999) / 1000000;
700 if (nearest_delta_ms
< 1) {
701 nearest_delta_ms
= 1;
704 timeKillEvent(mm_timer
);
705 mm_timer
= timeSetEvent(nearest_delta_ms
,
709 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
712 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
715 timeEndPeriod(mm_period
);
720 static int win32_start_timer(struct qemu_alarm_timer
*t
)
725 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
726 is zero) that has already expired, the timer is not updated. Since
727 creating a new timer is relatively expensive, set a bogus one-hour
728 interval in the dynticks case. */
729 success
= CreateTimerQueueTimer(&hTimer
,
734 alarm_has_dynticks(t
) ? 3600000 : 1,
735 WT_EXECUTEINTIMERTHREAD
);
738 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
747 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
749 HANDLE hTimer
= t
->timer
;
752 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
756 static void win32_rearm_timer(struct qemu_alarm_timer
*t
,
757 int64_t nearest_delta_ns
)
759 HANDLE hTimer
= t
->timer
;
760 int nearest_delta_ms
;
763 nearest_delta_ms
= (nearest_delta_ns
+ 999999) / 1000000;
764 if (nearest_delta_ms
< 1) {
765 nearest_delta_ms
= 1;
767 success
= ChangeTimerQueueTimer(NULL
,
773 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
782 static void quit_timers(void)
784 struct qemu_alarm_timer
*t
= alarm_timer
;
789 int init_timer_alarm(void)
791 struct qemu_alarm_timer
*t
= NULL
;
794 for (i
= 0; alarm_timers
[i
].name
; i
++) {
795 t
= &alarm_timers
[i
];
807 /* first event is at time 0 */
818 int qemu_calculate_timeout(void)