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 /* Conversion factor from emulated instructions to virtual clock ticks. */
50 int icount_time_shift
;
51 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
52 #define MAX_ICOUNT_SHIFT 10
53 /* Compensate for varying guest execution speed. */
54 int64_t qemu_icount_bias
;
55 static QEMUTimer
*icount_rt_timer
;
56 static QEMUTimer
*icount_vm_timer
;
58 /***********************************************************/
59 /* guest cycle counter */
61 typedef struct TimersState
{
62 int64_t cpu_ticks_prev
;
63 int64_t cpu_ticks_offset
;
64 int64_t cpu_clock_offset
;
65 int32_t cpu_ticks_enabled
;
69 TimersState timers_state
;
71 /* return the host CPU cycle counter and handle stop/restart */
72 int64_t cpu_get_ticks(void)
75 return cpu_get_icount();
77 if (!timers_state
.cpu_ticks_enabled
) {
78 return timers_state
.cpu_ticks_offset
;
81 ticks
= cpu_get_real_ticks();
82 if (timers_state
.cpu_ticks_prev
> ticks
) {
83 /* Note: non increasing ticks may happen if the host uses
85 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
87 timers_state
.cpu_ticks_prev
= ticks
;
88 return ticks
+ timers_state
.cpu_ticks_offset
;
92 /* return the host CPU monotonic timer and handle stop/restart */
93 static int64_t cpu_get_clock(void)
96 if (!timers_state
.cpu_ticks_enabled
) {
97 return timers_state
.cpu_clock_offset
;
100 return ti
+ timers_state
.cpu_clock_offset
;
104 #ifndef CONFIG_IOTHREAD
105 static int64_t qemu_icount_delta(void)
108 return 5000 * (int64_t) 1000000;
109 } else if (use_icount
== 1) {
110 /* When not using an adaptive execution frequency
111 we tend to get badly out of sync with real time,
112 so just delay for a reasonable amount of time. */
115 return cpu_get_icount() - cpu_get_clock();
120 /* enable cpu_get_ticks() */
121 void cpu_enable_ticks(void)
123 if (!timers_state
.cpu_ticks_enabled
) {
124 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
125 timers_state
.cpu_clock_offset
-= get_clock();
126 timers_state
.cpu_ticks_enabled
= 1;
130 /* disable cpu_get_ticks() : the clock is stopped. You must not call
131 cpu_get_ticks() after that. */
132 void cpu_disable_ticks(void)
134 if (timers_state
.cpu_ticks_enabled
) {
135 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
136 timers_state
.cpu_clock_offset
= cpu_get_clock();
137 timers_state
.cpu_ticks_enabled
= 0;
141 /***********************************************************/
144 #define QEMU_CLOCK_REALTIME 0
145 #define QEMU_CLOCK_VIRTUAL 1
146 #define QEMU_CLOCK_HOST 2
152 QEMUTimer
*warp_timer
;
154 NotifierList reset_notifiers
;
160 int64_t expire_time
; /* in nanoseconds */
164 struct QEMUTimer
*next
;
167 struct qemu_alarm_timer
{
169 int (*start
)(struct qemu_alarm_timer
*t
);
170 void (*stop
)(struct qemu_alarm_timer
*t
);
171 void (*rearm
)(struct qemu_alarm_timer
*t
);
172 #if defined(__linux__)
175 #elif defined(_WIN32)
182 static struct qemu_alarm_timer
*alarm_timer
;
184 static bool qemu_timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
186 return timer_head
&& (timer_head
->expire_time
<= current_time
);
189 int qemu_alarm_pending(void)
191 return alarm_timer
->pending
;
194 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
199 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
201 if (!alarm_has_dynticks(t
))
207 /* TODO: MIN_TIMER_REARM_NS should be optimized */
208 #define MIN_TIMER_REARM_NS 250000
212 static int mm_start_timer(struct qemu_alarm_timer
*t
);
213 static void mm_stop_timer(struct qemu_alarm_timer
*t
);
214 static void mm_rearm_timer(struct qemu_alarm_timer
*t
);
216 static int win32_start_timer(struct qemu_alarm_timer
*t
);
217 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
218 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
222 static int unix_start_timer(struct qemu_alarm_timer
*t
);
223 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
224 static void unix_rearm_timer(struct qemu_alarm_timer
*t
);
228 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
229 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
230 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
232 #endif /* __linux__ */
236 /* Correlation between real and virtual time is always going to be
237 fairly approximate, so ignore small variation.
238 When the guest is idle real and virtual time will be aligned in
240 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
242 static void icount_adjust(void)
247 static int64_t last_delta
;
248 /* If the VM is not running, then do nothing. */
252 cur_time
= cpu_get_clock();
253 cur_icount
= qemu_get_clock_ns(vm_clock
);
254 delta
= cur_icount
- cur_time
;
255 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
257 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
258 && icount_time_shift
> 0) {
259 /* The guest is getting too far ahead. Slow time down. */
263 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
264 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
265 /* The guest is getting too far behind. Speed time up. */
269 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
272 static void icount_adjust_rt(void * opaque
)
274 qemu_mod_timer(icount_rt_timer
,
275 qemu_get_clock_ms(rt_clock
) + 1000);
279 static void icount_adjust_vm(void * opaque
)
281 qemu_mod_timer(icount_vm_timer
,
282 qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 10);
286 int64_t qemu_icount_round(int64_t count
)
288 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
291 static struct qemu_alarm_timer alarm_timers
[] = {
294 {"dynticks", dynticks_start_timer
,
295 dynticks_stop_timer
, dynticks_rearm_timer
},
297 {"unix", unix_start_timer
, unix_stop_timer
, unix_rearm_timer
},
299 {"mmtimer", mm_start_timer
, mm_stop_timer
, NULL
},
300 {"mmtimer2", mm_start_timer
, mm_stop_timer
, mm_rearm_timer
},
301 {"dynticks", win32_start_timer
, win32_stop_timer
, win32_rearm_timer
},
302 {"win32", win32_start_timer
, win32_stop_timer
, NULL
},
307 static void show_available_alarms(void)
311 printf("Available alarm timers, in order of precedence:\n");
312 for (i
= 0; alarm_timers
[i
].name
; i
++)
313 printf("%s\n", alarm_timers
[i
].name
);
316 void configure_alarms(char const *opt
)
320 int count
= ARRAY_SIZE(alarm_timers
) - 1;
323 struct qemu_alarm_timer tmp
;
325 if (!strcmp(opt
, "?")) {
326 show_available_alarms();
332 /* Reorder the array */
333 name
= strtok(arg
, ",");
335 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
336 if (!strcmp(alarm_timers
[i
].name
, name
))
341 fprintf(stderr
, "Unknown clock %s\n", name
);
350 tmp
= alarm_timers
[i
];
351 alarm_timers
[i
] = alarm_timers
[cur
];
352 alarm_timers
[cur
] = tmp
;
356 name
= strtok(NULL
, ",");
362 /* Disable remaining timers */
363 for (i
= cur
; i
< count
; i
++)
364 alarm_timers
[i
].name
= NULL
;
366 show_available_alarms();
371 #define QEMU_NUM_CLOCKS 3
375 QEMUClock
*host_clock
;
377 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
379 static QEMUClock
*qemu_new_clock(int type
)
383 clock
= g_malloc0(sizeof(QEMUClock
));
386 notifier_list_init(&clock
->reset_notifiers
);
387 /* required to detect & report backward jumps */
388 if (type
== QEMU_CLOCK_HOST
) {
389 clock
->last
= get_clock_realtime();
394 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
396 clock
->enabled
= enabled
;
399 static int64_t vm_clock_warp_start
;
401 static void icount_warp_rt(void *opaque
)
403 if (vm_clock_warp_start
== -1) {
408 int64_t clock
= qemu_get_clock_ns(rt_clock
);
409 int64_t warp_delta
= clock
- vm_clock_warp_start
;
410 if (use_icount
== 1) {
411 qemu_icount_bias
+= warp_delta
;
414 * In adaptive mode, do not let the vm_clock run too
415 * far ahead of real time.
417 int64_t cur_time
= cpu_get_clock();
418 int64_t cur_icount
= qemu_get_clock_ns(vm_clock
);
419 int64_t delta
= cur_time
- cur_icount
;
420 qemu_icount_bias
+= MIN(warp_delta
, delta
);
422 if (qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
423 qemu_get_clock_ns(vm_clock
))) {
427 vm_clock_warp_start
= -1;
430 void qemu_clock_warp(QEMUClock
*clock
)
434 if (!clock
->warp_timer
) {
439 * There are too many global variables to make the "warp" behavior
440 * applicable to other clocks. But a clock argument removes the
441 * need for if statements all over the place.
443 assert(clock
== vm_clock
);
446 * If the CPUs have been sleeping, advance the vm_clock timer now. This
447 * ensures that the deadline for the timer is computed correctly below.
448 * This also makes sure that the insn counter is synchronized before the
449 * CPU starts running, in case the CPU is woken by an event other than
450 * the earliest vm_clock timer.
452 icount_warp_rt(NULL
);
453 if (!all_cpu_threads_idle() || !active_timers
[clock
->type
]) {
454 qemu_del_timer(clock
->warp_timer
);
458 vm_clock_warp_start
= qemu_get_clock_ns(rt_clock
);
459 deadline
= qemu_next_icount_deadline();
462 * Ensure the vm_clock proceeds even when the virtual CPU goes to
463 * sleep. Otherwise, the CPU might be waiting for a future timer
464 * interrupt to wake it up, but the interrupt never comes because
465 * the vCPU isn't running any insns and thus doesn't advance the
468 * An extreme solution for this problem would be to never let VCPUs
469 * sleep in icount mode if there is a pending vm_clock timer; rather
470 * time could just advance to the next vm_clock event. Instead, we
471 * do stop VCPUs and only advance vm_clock after some "real" time,
472 * (related to the time left until the next event) has passed. This
473 * rt_clock timer will do this. This avoids that the warps are too
474 * visible externally---for example, you will not be sending network
475 * packets continously instead of every 100ms.
477 qemu_mod_timer(clock
->warp_timer
, vm_clock_warp_start
+ deadline
);
483 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
484 QEMUTimerCB
*cb
, void *opaque
)
488 ts
= g_malloc0(sizeof(QEMUTimer
));
496 void qemu_free_timer(QEMUTimer
*ts
)
501 /* stop a timer, but do not dealloc it */
502 void qemu_del_timer(QEMUTimer
*ts
)
506 /* NOTE: this code must be signal safe because
507 qemu_timer_expired() can be called from a signal. */
508 pt
= &active_timers
[ts
->clock
->type
];
521 /* modify the current timer so that it will be fired when current_time
522 >= expire_time. The corresponding callback will be called. */
523 static void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
529 /* add the timer in the sorted list */
530 /* NOTE: this code must be signal safe because
531 qemu_timer_expired() can be called from a signal. */
532 pt
= &active_timers
[ts
->clock
->type
];
535 if (!qemu_timer_expired_ns(t
, expire_time
)) {
540 ts
->expire_time
= expire_time
;
544 /* Rearm if necessary */
545 if (pt
== &active_timers
[ts
->clock
->type
]) {
546 if (!alarm_timer
->pending
) {
547 qemu_rearm_alarm_timer(alarm_timer
);
549 /* Interrupt execution to force deadline recalculation. */
550 qemu_clock_warp(ts
->clock
);
557 /* modify the current timer so that it will be fired when current_time
558 >= expire_time. The corresponding callback will be called. */
559 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
561 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
564 int qemu_timer_pending(QEMUTimer
*ts
)
567 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
574 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
576 return qemu_timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
579 static void qemu_run_timers(QEMUClock
*clock
)
581 QEMUTimer
**ptimer_head
, *ts
;
582 int64_t current_time
;
587 current_time
= qemu_get_clock_ns(clock
);
588 ptimer_head
= &active_timers
[clock
->type
];
591 if (!qemu_timer_expired_ns(ts
, current_time
)) {
594 /* remove timer from the list before calling the callback */
595 *ptimer_head
= ts
->next
;
598 /* run the callback (the timer list can be modified) */
603 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
607 switch(clock
->type
) {
608 case QEMU_CLOCK_REALTIME
:
611 case QEMU_CLOCK_VIRTUAL
:
613 return cpu_get_icount();
615 return cpu_get_clock();
617 case QEMU_CLOCK_HOST
:
618 now
= get_clock_realtime();
622 notifier_list_notify(&clock
->reset_notifiers
, &now
);
628 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
630 notifier_list_add(&clock
->reset_notifiers
, notifier
);
633 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
)
635 notifier_list_remove(&clock
->reset_notifiers
, notifier
);
638 void init_clocks(void)
640 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
641 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
642 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
644 rtc_clock
= host_clock
;
648 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
650 uint64_t expire_time
;
652 if (qemu_timer_pending(ts
)) {
653 expire_time
= ts
->expire_time
;
657 qemu_put_be64(f
, expire_time
);
660 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
662 uint64_t expire_time
;
664 expire_time
= qemu_get_be64(f
);
665 if (expire_time
!= -1) {
666 qemu_mod_timer_ns(ts
, expire_time
);
672 static const VMStateDescription vmstate_timers
= {
675 .minimum_version_id
= 1,
676 .minimum_version_id_old
= 1,
677 .fields
= (VMStateField
[]) {
678 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
679 VMSTATE_INT64(dummy
, TimersState
),
680 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
681 VMSTATE_END_OF_LIST()
685 void configure_icount(const char *option
)
687 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
691 #ifdef CONFIG_IOTHREAD
692 vm_clock
->warp_timer
= qemu_new_timer_ns(rt_clock
, icount_warp_rt
, NULL
);
695 if (strcmp(option
, "auto") != 0) {
696 icount_time_shift
= strtol(option
, NULL
, 0);
703 /* 125MIPS seems a reasonable initial guess at the guest speed.
704 It will be corrected fairly quickly anyway. */
705 icount_time_shift
= 3;
707 /* Have both realtime and virtual time triggers for speed adjustment.
708 The realtime trigger catches emulated time passing too slowly,
709 the virtual time trigger catches emulated time passing too fast.
710 Realtime triggers occur even when idle, so use them less frequently
712 icount_rt_timer
= qemu_new_timer_ms(rt_clock
, icount_adjust_rt
, NULL
);
713 qemu_mod_timer(icount_rt_timer
,
714 qemu_get_clock_ms(rt_clock
) + 1000);
715 icount_vm_timer
= qemu_new_timer_ns(vm_clock
, icount_adjust_vm
, NULL
);
716 qemu_mod_timer(icount_vm_timer
,
717 qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 10);
720 void qemu_run_all_timers(void)
722 alarm_timer
->pending
= 0;
724 /* rearm timer, if not periodic */
725 if (alarm_timer
->expired
) {
726 alarm_timer
->expired
= 0;
727 qemu_rearm_alarm_timer(alarm_timer
);
732 qemu_run_timers(vm_clock
);
735 qemu_run_timers(rt_clock
);
736 qemu_run_timers(host_clock
);
739 static int64_t qemu_next_alarm_deadline(void);
742 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
744 static void host_alarm_handler(int host_signum
)
747 struct qemu_alarm_timer
*t
= alarm_timer
;
752 #define DISP_FREQ 1000
754 static int64_t delta_min
= INT64_MAX
;
755 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
757 ti
= qemu_get_clock_ns(vm_clock
);
758 if (last_clock
!= 0) {
759 delta
= ti
- last_clock
;
760 if (delta
< delta_min
)
762 if (delta
> delta_max
)
765 if (++count
== DISP_FREQ
) {
766 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
767 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
768 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
769 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
770 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
772 delta_min
= INT64_MAX
;
780 if (alarm_has_dynticks(t
) ||
781 qemu_next_alarm_deadline () <= 0) {
782 t
->expired
= alarm_has_dynticks(t
);
788 int64_t qemu_next_icount_deadline(void)
790 /* To avoid problems with overflow limit this to 2^32. */
791 int64_t delta
= INT32_MAX
;
794 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
795 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
796 qemu_get_clock_ns(vm_clock
);
805 static int64_t qemu_next_alarm_deadline(void)
810 if (!use_icount
&& active_timers
[QEMU_CLOCK_VIRTUAL
]) {
811 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
812 qemu_get_clock_ns(vm_clock
);
816 if (active_timers
[QEMU_CLOCK_HOST
]) {
817 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
818 qemu_get_clock_ns(host_clock
);
822 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
823 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
824 qemu_get_clock_ns(rt_clock
));
832 #if defined(__linux__)
834 #include "compatfd.h"
836 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
840 struct sigaction act
;
842 sigfillset(&act
.sa_mask
);
844 act
.sa_handler
= host_alarm_handler
;
846 sigaction(SIGALRM
, &act
, NULL
);
849 * Initialize ev struct to 0 to avoid valgrind complaining
850 * about uninitialized data in timer_create call
852 memset(&ev
, 0, sizeof(ev
));
853 ev
.sigev_value
.sival_int
= 0;
854 ev
.sigev_notify
= SIGEV_SIGNAL
;
855 #ifdef SIGEV_THREAD_ID
856 if (qemu_signalfd_available()) {
857 ev
.sigev_notify
= SIGEV_THREAD_ID
;
858 ev
._sigev_un
._tid
= qemu_get_thread_id();
860 #endif /* SIGEV_THREAD_ID */
861 ev
.sigev_signo
= SIGALRM
;
863 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
864 perror("timer_create");
866 /* disable dynticks */
867 fprintf(stderr
, "Dynamic Ticks disabled\n");
872 t
->timer
= host_timer
;
877 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
879 timer_t host_timer
= t
->timer
;
881 timer_delete(host_timer
);
884 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
886 timer_t host_timer
= t
->timer
;
887 struct itimerspec timeout
;
888 int64_t nearest_delta_ns
= INT64_MAX
;
891 assert(alarm_has_dynticks(t
));
892 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
893 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
894 !active_timers
[QEMU_CLOCK_HOST
])
897 nearest_delta_ns
= qemu_next_alarm_deadline();
898 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
899 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
901 /* check whether a timer is already running */
902 if (timer_gettime(host_timer
, &timeout
)) {
904 fprintf(stderr
, "Internal timer error: aborting\n");
907 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
908 if (current_ns
&& current_ns
<= nearest_delta_ns
)
911 timeout
.it_interval
.tv_sec
= 0;
912 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
913 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
914 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
915 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
917 fprintf(stderr
, "Internal timer error: aborting\n");
922 #endif /* defined(__linux__) */
926 static int unix_start_timer(struct qemu_alarm_timer
*t
)
928 struct sigaction act
;
931 sigfillset(&act
.sa_mask
);
933 act
.sa_handler
= host_alarm_handler
;
935 sigaction(SIGALRM
, &act
, NULL
);
939 static void unix_rearm_timer(struct qemu_alarm_timer
*t
)
941 struct itimerval itv
;
942 int64_t nearest_delta_ns
= INT64_MAX
;
945 assert(alarm_has_dynticks(t
));
946 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
947 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
948 !active_timers
[QEMU_CLOCK_HOST
])
951 nearest_delta_ns
= qemu_next_alarm_deadline();
952 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
953 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
955 itv
.it_interval
.tv_sec
= 0;
956 itv
.it_interval
.tv_usec
= 0; /* 0 for one-shot timer */
957 itv
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
958 itv
.it_value
.tv_usec
= (nearest_delta_ns
% 1000000000) / 1000;
959 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
962 fprintf(stderr
, "Internal timer error: aborting\n");
967 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
969 struct itimerval itv
;
971 memset(&itv
, 0, sizeof(itv
));
972 setitimer(ITIMER_REAL
, &itv
, NULL
);
975 #endif /* !defined(_WIN32) */
980 static MMRESULT mm_timer
;
981 static unsigned mm_period
;
983 static void CALLBACK
mm_alarm_handler(UINT uTimerID
, UINT uMsg
,
984 DWORD_PTR dwUser
, DWORD_PTR dw1
,
987 struct qemu_alarm_timer
*t
= alarm_timer
;
991 if (alarm_has_dynticks(t
) || qemu_next_alarm_deadline() <= 0) {
992 t
->expired
= alarm_has_dynticks(t
);
998 static int mm_start_timer(struct qemu_alarm_timer
*t
)
1003 memset(&tc
, 0, sizeof(tc
));
1004 timeGetDevCaps(&tc
, sizeof(tc
));
1006 mm_period
= tc
.wPeriodMin
;
1007 timeBeginPeriod(mm_period
);
1009 flags
= TIME_CALLBACK_FUNCTION
;
1010 if (alarm_has_dynticks(t
)) {
1011 flags
|= TIME_ONESHOT
;
1013 flags
|= TIME_PERIODIC
;
1016 mm_timer
= timeSetEvent(1, /* interval (ms) */
1017 mm_period
, /* resolution */
1018 mm_alarm_handler
, /* function */
1019 (DWORD_PTR
)t
, /* parameter */
1023 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1025 timeEndPeriod(mm_period
);
1032 static void mm_stop_timer(struct qemu_alarm_timer
*t
)
1034 timeKillEvent(mm_timer
);
1035 timeEndPeriod(mm_period
);
1038 static void mm_rearm_timer(struct qemu_alarm_timer
*t
)
1040 int nearest_delta_ms
;
1042 assert(alarm_has_dynticks(t
));
1043 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1044 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1045 !active_timers
[QEMU_CLOCK_HOST
]) {
1049 timeKillEvent(mm_timer
);
1051 nearest_delta_ms
= (qemu_next_alarm_deadline() + 999999) / 1000000;
1052 if (nearest_delta_ms
< 1) {
1053 nearest_delta_ms
= 1;
1055 mm_timer
= timeSetEvent(nearest_delta_ms
,
1059 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1062 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1065 timeEndPeriod(mm_period
);
1070 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1075 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1076 is zero) that has already expired, the timer is not updated. Since
1077 creating a new timer is relatively expensive, set a bogus one-hour
1078 interval in the dynticks case. */
1079 success
= CreateTimerQueueTimer(&hTimer
,
1084 alarm_has_dynticks(t
) ? 3600000 : 1,
1085 WT_EXECUTEINTIMERTHREAD
);
1088 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1097 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1099 HANDLE hTimer
= t
->timer
;
1102 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
1106 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1108 HANDLE hTimer
= t
->timer
;
1109 int nearest_delta_ms
;
1112 assert(alarm_has_dynticks(t
));
1113 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1114 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1115 !active_timers
[QEMU_CLOCK_HOST
])
1118 nearest_delta_ms
= (qemu_next_alarm_deadline() + 999999) / 1000000;
1119 if (nearest_delta_ms
< 1) {
1120 nearest_delta_ms
= 1;
1122 success
= ChangeTimerQueueTimer(NULL
,
1128 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
1137 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1140 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1143 int init_timer_alarm(void)
1145 struct qemu_alarm_timer
*t
= NULL
;
1148 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1149 t
= &alarm_timers
[i
];
1161 /* first event is at time 0 */
1164 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1172 void quit_timers(void)
1174 struct qemu_alarm_timer
*t
= alarm_timer
;
1179 int qemu_calculate_timeout(void)
1181 #ifndef CONFIG_IOTHREAD
1187 /* XXX: use timeout computed from timers */
1190 /* Advance virtual time to the next event. */
1191 delta
= qemu_icount_delta();
1193 /* If virtual time is ahead of real time then just
1195 timeout
= (delta
+ 999999) / 1000000;
1197 /* Wait for either IO to occur or the next
1199 add
= qemu_next_icount_deadline();
1200 /* We advance the timer before checking for IO.
1201 Limit the amount we advance so that early IO
1202 activity won't get the guest too far ahead. */
1206 qemu_icount
+= qemu_icount_round (add
);
1207 timeout
= delta
/ 1000000;
1214 #else /* CONFIG_IOTHREAD */