diff mbox

[v2,4/5] monitor: throttle QAPI_EVENT_VSERPORT_CHANGE by "id"

Message ID 1442506130-15219-5-git-send-email-marcandre.lureau@redhat.com
State New
Headers show

Commit Message

Marc-André Lureau Sept. 17, 2015, 4:08 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Use a hash table to lookup the pending event corresponding to the "id"
field. The hash table may grow without limit here, the following patch
will add some cleaning.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 monitor.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 81 insertions(+), 23 deletions(-)

Comments

Daniel P. Berrangé Sept. 23, 2015, 10:07 a.m. UTC | #1
On Thu, Sep 17, 2015 at 06:08:49PM +0200, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Use a hash table to lookup the pending event corresponding to the "id"
> field. The hash table may grow without limit here, the following patch
> will add some cleaning.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> ---
>  monitor.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 81 insertions(+), 23 deletions(-)

Reviewed-by: Daniel P. Berrange <berrange@redhat.com>


Regards,
Daniel
diff mbox

Patch

diff --git a/monitor.c b/monitor.c
index 2f8af5b..90f06ce 100644
--- a/monitor.c
+++ b/monitor.c
@@ -472,10 +472,10 @@  static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
  * Return 'false' if the event is not delayed and can be emitted now.
  */
 static bool
-monitor_qapi_event_delay(MonitorQAPIEventState *evstate, QDict *qdict)
+monitor_qapi_event_pending_update(MonitorQAPIEventState *evstate,
+                                  MonitorQAPIEventPending *p, QDict *qdict)
 {
     int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
-    MonitorQAPIEventPending *p = evstate->delay_data;
     int64_t delta = now - p->last;
 
     trace_monitor_protocol_event_delay(p->event,
@@ -510,27 +510,11 @@  monitor_qapi_event_delay(MonitorQAPIEventState *evstate, QDict *qdict)
     return false;
 }
 
-/*
- * Queue a new event for emission to Monitor instances,
- * applying any rate limiting if required.
- */
-static void
-monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
+static bool
+monitor_qapi_event_delay(MonitorQAPIEventState *evstate, QDict *qdict)
 {
-    MonitorQAPIEventState *evstate;
-    assert(event < QAPI_EVENT_MAX);
-
-    evstate = &monitor_qapi_event_state[event];
-    trace_monitor_protocol_event_queue(event, qdict);
-
-    qemu_mutex_lock(&monitor_lock);
-
-    if (!evstate->delay ||
-        !evstate->delay(evstate, qdict)) {
-        monitor_qapi_event_emit(event, QOBJECT(qdict));
-    }
-
-    qemu_mutex_unlock(&monitor_lock);
+    return monitor_qapi_event_pending_update(evstate,
+                                             evstate->delay_data, qdict);
 }
 
 /*
@@ -571,6 +555,62 @@  monitor_qapi_event_pending_new(QAPIEvent event)
 }
 
 /*
+ * A delay handler that will filter events by the "id" event field.
+ * evstate must be an element of the monitor_qapi_event_state array.
+ *
+ * Return 'false' if the event is not delayed and can be emitted now.
+ */
+static bool
+monitor_qapi_event_id_delay(MonitorQAPIEventState *evstate, QDict *qdict)
+{
+    GHashTable *ht = evstate->delay_data;
+    QDict *data = qdict_get_qdict(qdict, "data");
+    const char *id = qdict_get_str(data, "id");
+    MonitorQAPIEventPending *p = g_hash_table_lookup(ht, id);
+    QAPIEvent event = evstate - monitor_qapi_event_state;
+    assert(event >= 0 || event < QAPI_EVENT_MAX);
+
+    if (!p) {
+        p = monitor_qapi_event_pending_new(event);
+        g_hash_table_insert(ht, g_strdup(id), p);
+    }
+
+    return monitor_qapi_event_pending_update(evstate, p, qdict);
+}
+
+/*
+ * Queue a new event for emission to Monitor instances,
+ * applying any rate limiting if required.
+ */
+static void
+monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
+{
+    MonitorQAPIEventState *evstate;
+    assert(event < QAPI_EVENT_MAX);
+
+    evstate = &(monitor_qapi_event_state[event]);
+    trace_monitor_protocol_event_queue(event, qdict);
+
+    qemu_mutex_lock(&monitor_lock);
+
+    if (!evstate->delay ||
+        !evstate->delay(evstate, qdict)) {
+        monitor_qapi_event_emit(event, QOBJECT(qdict));
+    }
+
+    qemu_mutex_unlock(&monitor_lock);
+}
+
+static void
+monitor_qapi_event_pending_free(MonitorQAPIEventPending *p)
+{
+    qobject_decref(p->qdict);
+    timer_del(p->timer);
+    timer_free(p->timer);
+    g_free(p);
+}
+
+/*
  * @event: the event ID to be limited
  * @rate: the rate limit in milliseconds
  *
@@ -594,6 +634,24 @@  monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
     evstate->delay_data = monitor_qapi_event_pending_new(event);
 }
 
+static void
+monitor_qapi_event_id_throttle(QAPIEvent event, int64_t rate)
+{
+    MonitorQAPIEventState *evstate;
+    assert(event < QAPI_EVENT_MAX);
+
+    evstate = &(monitor_qapi_event_state[event]);
+
+    trace_monitor_protocol_event_throttle(event, rate);
+    assert(rate * SCALE_MS <= INT64_MAX);
+    evstate->rate = rate * SCALE_MS;
+
+    evstate->delay = monitor_qapi_event_id_delay;
+    evstate->delay_data =
+        g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+            (GDestroyNotify)monitor_qapi_event_pending_free);
+}
+
 static void monitor_qapi_event_init(void)
 {
     /* Limit guest-triggerable events to 1 per second */
@@ -602,7 +660,7 @@  static void monitor_qapi_event_init(void)
     monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
     monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
     monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
-    monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
+    monitor_qapi_event_id_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
 
     qmp_event_set_func_emit(monitor_qapi_event_queue);
 }