Small change to remove short 250us timeouts every other time through
the wait loop.

Basically, the main wait loop calls qemu_run_all_timers() unconditionally. The
first thing this routine used to do is to see if a timer had been serviced,
and then reset the loop timeout to the next deadline.

However, the new deadlines had not been calculated at that point, as
qemu_run_timers() had not been called yet for each of the clocks. So
qemu_rearm_alarm_timer() would end up with a negative or zero deadline, and
default to setting a 250us timeout for the loop.

As qemu_run_timers() is called for each clock, the real deadlines would be put
in place, but because a loop timeout was already set, the loop timeout would
not be changed.

Once that 250us timeout fired, the real deadline would be used for the
subsequent timeout.

For idle VMs, this effectively doubles the number of times through the loop,
doubling the number of select() system calls, timer calls, etc. putting added
scheduling pressure on the kernel. And under cgroups, this really causes a big
problem because the cgroup code does not scale well.

By simply running the timers before trying to rearm the timer, we always rearm
with a non-zero deadline, effectively halving the number of system calls.

Signed-off-by: Peter Portante <pportant@redhat.com>
Use CLOCK_MONOTONIC (where available) as the foundational timer to avoid clock adjustments affecting timeouts.

Use CLOCK_REALTIME instead of gettimeofday() (where available) to avoid microsecond scaling.

Signed-off-by: Peter Portante <pportant@redhat.com>
Revert commit 49cb088b3de680e834773f56c13f32bd7067eb1e:
  * "Use CLOCK_MONOTONIC (where available) as the foundational timer to avoid clock adjustments affecting timeouts."
  * "Use CLOCK_REALTIME instead of gettimeofday() (where available) to avoid microsecond scaling."

They should be independently committed.

Signed-off-by: Peter Portante <peter.portante@redhat.com>
Use CLOCK_MONOTONIC (where available) as the foundational timer to avoid clock adjustments affecting timeouts.

Signed-off-by: Peter Portante <peter.portante@redhat.com>
Use clock_gettime(CLOCK_REALTIME) instead of gettimeofday() to avoid
microsecond scaling (it is also about 2.7% faster).

Signed-off-by: Peter Portante <peter.portante@redhat.com>
Add a FIXME comment noting possible threading interaction in quit_timers().

Signed-off-by: Peter Portante <peter.portante@redhat.com>
qemu-timer.c: refactor init_timer_alarm() to enhance maintainability

Add some comments describing what init_timer_alarm() is doing. Remove the goto
and early return to have one return point. Move the for loop exit condition
into the for loop statement itself to keep the conditions in one place.

Signed-off-by: Peter Portante <peter.portante@redhat.com>
qemu-timer.c: add comments to note meaning of "REALTIME" and "HOST" clocks

Add some simple comments to note that QEMU_CLOCK_REALTIME really refers to
a monotonically increasing clock, and that QEMU_CLOCK_HOST refers to the
host's realtime clock (either gettimeofday() or clock_gettime(CLOCK_REALTIME).
And for those not in the know, QEMU_CLOCK_VIRTUAL refers to the VM's clock.

Signed-off-by: Peter Portante <peter.portante@redhat.com>
Merge branch 'fixes'
block, hw, qemu-timer, slirp: refactor use of nanosecond literal

Define one constant for nanoseconds per second and use it properly in all
locations for consistency.

Signed-off-by: Peter Portante <peter.portante@redhat.com>