From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54891) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPbyZ-00085z-Lo for qemu-devel@nongnu.org; Wed, 12 Aug 2015 15:46:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZPbyY-0002VG-OM for qemu-devel@nongnu.org; Wed, 12 Aug 2015 15:46:15 -0400 Received: from mail-qg0-x235.google.com ([2607:f8b0:400d:c04::235]:36581) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZPbyY-0002VB-K9 for qemu-devel@nongnu.org; Wed, 12 Aug 2015 15:46:14 -0400 Received: by qgdd90 with SMTP id d90so17856463qgd.3 for ; Wed, 12 Aug 2015 12:46:14 -0700 (PDT) Sender: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= From: marcandre.lureau@redhat.com Date: Wed, 12 Aug 2015 21:46:03 +0200 Message-Id: <1439408763-12785-4-git-send-email-marcandre.lureau@redhat.com> In-Reply-To: <1439408763-12785-1-git-send-email-marcandre.lureau@redhat.com> References: <1439408763-12785-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [RFC 3/3] monitor: remove old entries from event hash table List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: lersek@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= From: Marc-André Lureau Do not let the hash table grow without limit, schedule a cleanup for outdated event. Signed-off-by: Marc-André Lureau --- monitor.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/monitor.c b/monitor.c index 7334ffb..6980806 100644 --- a/monitor.c +++ b/monitor.c @@ -170,6 +170,8 @@ typedef struct { bool in_command_mode; /* are we in command mode? */ } MonitorQMP; +typedef struct MonitorQAPIEventState MonitorQAPIEventState; + typedef struct MonitorQAPIEventPending { QAPIEvent event; /* Event being tracked */ int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */ @@ -177,8 +179,6 @@ typedef struct MonitorQAPIEventPending { QObject *data; /* Event pending delayed dispatch */ } MonitorQAPIEventPending; -typedef struct MonitorQAPIEventState MonitorQAPIEventState; - typedef bool (*MonitorQAPIEventDelay) (MonitorQAPIEventState *evstate, QDict *data); /* @@ -540,6 +540,45 @@ monitor_qapi_event_pending_new(QAPIEvent event) return p; } +static void monitor_qapi_event_id_remove(void *opaque) +{ + MonitorQAPIEventPending *p = opaque; + MonitorQAPIEventState *s = &monitor_qapi_event_state[p->event]; + GHashTable *ht = s->data; + GHashTableIter iter; + gpointer value; + + g_hash_table_iter_init(&iter, ht); + while (g_hash_table_iter_next(&iter, NULL, &value)) { + if (value == p) { + g_hash_table_iter_remove(&iter); + return; + } + } +} + +/* + * do not let the hash table grow, if no later pending event + * scheduled, remove the old entry after rate timeout. + */ +static void monitor_qapi_event_id_schedule_remove(MonitorQAPIEventPending *p) +{ + MonitorQAPIEventState *s = &monitor_qapi_event_state[p->event]; + int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + int64_t then = now + s->rate; + + p->timer->cb = monitor_qapi_event_id_remove; + timer_mod_ns(p->timer, then); +} + +static void monitor_qapi_event_id_handler(void *opaque) +{ + MonitorQAPIEventPending *p = opaque; + + monitor_qapi_event_handler(p); + monitor_qapi_event_id_schedule_remove(p); +} + static bool monitor_qapi_event_id_delay(MonitorQAPIEventState *evstate, QDict *data) { @@ -554,7 +593,13 @@ monitor_qapi_event_id_delay(MonitorQAPIEventState *evstate, QDict *data) g_hash_table_insert(ht, g_strdup(id), p); } - return monitor_qapi_event_pending_update(evstate, p, data); + if (monitor_qapi_event_pending_update(evstate, p, data)) { + p->timer->cb = monitor_qapi_event_id_handler; + return TRUE; + } else { + monitor_qapi_event_id_schedule_remove(p); + return FALSE; + } } /* -- 2.4.3