qemu-timer: check active_timers outside lock/event
This avoids taking the active_timers_lock or resetting/setting the
timers_done_ev if there are no active timers. This removes a small
(2-3%) source of overhead for dataplane. The list is then checked
again inside the lock, or a NULL pointer could be dereferenced.
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
diff --git a/qemu-timer.c b/qemu-timer.c
index 9299cdc..ff620ec 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -174,7 +174,7 @@
bool timerlist_has_timers(QEMUTimerList *timer_list)
{
- return !!timer_list->active_timers;
+ return !!atomic_read(&timer_list->active_timers);
}
bool qemu_clock_has_timers(QEMUClockType type)
@@ -187,6 +187,10 @@
{
int64_t expire_time;
+ if (!atomic_read(&timer_list->active_timers)) {
+ return false;
+ }
+
qemu_mutex_lock(&timer_list->active_timers_lock);
if (!timer_list->active_timers) {
qemu_mutex_unlock(&timer_list->active_timers_lock);
@@ -214,6 +218,10 @@
int64_t delta;
int64_t expire_time;
+ if (!atomic_read(&timer_list->active_timers)) {
+ return -1;
+ }
+
if (!timer_list->clock->enabled) {
return -1;
}
@@ -363,7 +371,7 @@
if (!t)
break;
if (t == ts) {
- *pt = t->next;
+ atomic_set(pt, t->next);
break;
}
pt = &t->next;
@@ -386,7 +394,7 @@
}
ts->expire_time = MAX(expire_time, 0);
ts->next = *pt;
- *pt = ts;
+ atomic_set(pt, ts);
return pt == &timer_list->active_timers;
}
@@ -481,8 +489,12 @@
QEMUTimerCB *cb;
void *opaque;
+ if (!atomic_read(&timer_list->active_timers)) {
+ return false;
+ }
+
qemu_event_reset(&timer_list->timers_done_ev);
- if (!timer_list->clock->enabled || !timer_list->active_timers) {
+ if (!timer_list->clock->enabled) {
goto out;
}