113
113
static int64_t qemu_icount_delta(void)
116
return 5000 * (int64_t) 1000000;
117
} else if (use_icount == 1) {
115
if (use_icount == 1) {
118
116
/* When not using an adaptive execution frequency
119
117
we tend to get badly out of sync with real time,
120
118
so just delay for a reasonable amount of time. */
635
633
qemu_run_timers(host_clock);
636
static int64_t qemu_next_alarm_deadline(void);
639
639
static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
640
640
DWORD_PTR dwUser, DWORD_PTR dw1,
679
679
if (alarm_has_dynticks(t) ||
681
qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
682
qemu_get_clock(vm_clock))) ||
683
qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
684
qemu_get_clock(rt_clock)) ||
685
qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
686
qemu_get_clock(host_clock))) {
680
qemu_next_alarm_deadline () <= 0) {
688
681
t->expired = alarm_has_dynticks(t);
690
683
qemu_notify_event();
699
692
if (active_timers[QEMU_CLOCK_VIRTUAL]) {
700
693
delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
701
qemu_get_clock(vm_clock);
694
qemu_get_clock_ns(vm_clock);
703
696
if (active_timers[QEMU_CLOCK_HOST]) {
704
697
int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
705
qemu_get_clock(host_clock);
698
qemu_get_clock_ns(host_clock);
706
699
if (hdelta < delta)
718
#if defined(__linux__)
720
#define RTC_FREQ 1024
722
static uint64_t qemu_next_deadline_dyntick(void)
709
static int64_t qemu_next_alarm_deadline(void)
714
if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
715
delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
716
qemu_get_clock(vm_clock);
728
718
delta = INT32_MAX;
730
delta = (qemu_next_deadline() + 999) / 1000;
720
if (active_timers[QEMU_CLOCK_HOST]) {
721
int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
722
qemu_get_clock_ns(host_clock);
732
726
if (active_timers[QEMU_CLOCK_REALTIME]) {
733
rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
734
qemu_get_clock(rt_clock))*1000;
727
rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time * 1000000 -
728
qemu_get_clock_ns(rt_clock));
735
729
if (rtdelta < delta)
739
if (delta < MIN_TIMER_REARM_US)
740
delta = MIN_TIMER_REARM_US;
736
#if defined(__linux__)
738
#define RTC_FREQ 1024
745
740
static void enable_sigio_timer(int fd)
747
742
struct sigaction act;
888
883
timer_t host_timer = (timer_t)(long)t->priv;
889
884
struct itimerspec timeout;
890
int64_t nearest_delta_us = INT64_MAX;
885
int64_t nearest_delta_ns = INT64_MAX;
893
888
assert(alarm_has_dynticks(t));
894
889
if (!active_timers[QEMU_CLOCK_REALTIME] &&
896
891
!active_timers[QEMU_CLOCK_HOST])
899
nearest_delta_us = qemu_next_deadline_dyntick();
894
nearest_delta_ns = qemu_next_alarm_deadline();
895
if (nearest_delta_ns < MIN_TIMER_REARM_NS)
896
nearest_delta_ns = MIN_TIMER_REARM_NS;
901
898
/* check whether a timer is already running */
902
899
if (timer_gettime(host_timer, &timeout)) {
904
901
fprintf(stderr, "Internal timer error: aborting\n");
907
current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
908
if (current_us && current_us <= nearest_delta_us)
904
current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
905
if (current_ns && current_ns <= nearest_delta_ns)
911
908
timeout.it_interval.tv_sec = 0;
912
909
timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
913
timeout.it_value.tv_sec = nearest_delta_us / 1000000;
914
timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
910
timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
911
timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
915
912
if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
916
913
perror("settime");
917
914
fprintf(stderr, "Internal timer error: aborting\n");
1076
1075
int qemu_calculate_timeout(void)
1080
#ifdef CONFIG_IOTHREAD
1081
1081
/* When using icount, making forward progress with qemu_icount when the
1082
1082
guest CPU is idle is critical. We only use the static io-thread timeout
1083
1083
for non icount runs. */
1084
if (!use_icount || !vm_running) {
1092
/* XXX: use timeout computed from timers */
1095
/* Advance virtual time to the next event. */
1096
delta = qemu_icount_delta();
1098
/* If virtual time is ahead of real time then just
1100
timeout = (delta + 999999) / 1000000;
1102
/* Wait for either IO to occur or the next
1104
add = qemu_next_deadline();
1105
/* We advance the timer before checking for IO.
1106
Limit the amount we advance so that early IO
1107
activity won't get the guest too far ahead. */
1111
qemu_icount += qemu_icount_round (add);
1112
timeout = delta / 1000000;
1088
/* Advance virtual time to the next event. */
1089
delta = qemu_icount_delta();
1091
/* If virtual time is ahead of real time then just
1093
timeout = (delta + 999999) / 1000000;
1095
/* Wait for either IO to occur or the next
1097
add = qemu_next_deadline();
1098
/* We advance the timer before checking for IO.
1099
Limit the amount we advance so that early IO
1100
activity won't get the guest too far ahead. */
1104
qemu_icount += qemu_icount_round (add);
1105
timeout = delta / 1000000;
1118
1110
return timeout;