[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 4/5] monitor: throttle QAPI_EVENT_VSERPORT_CHANGE
From: |
marcandre . lureau |
Subject: |
[Qemu-devel] [PATCH v2 4/5] monitor: throttle QAPI_EVENT_VSERPORT_CHANGE by "id" |
Date: |
Thu, 17 Sep 2015 18:08:49 +0200 |
From: Marc-André Lureau <address@hidden>
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 <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
monitor.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 81 insertions(+), 23 deletions(-)
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);
}
--
2.4.3
- [Qemu-devel] [PATCH v2 0/5] monitor: throttle VSERPORT_CHANGED by "id", marcandre . lureau, 2015/09/17
- [Qemu-devel] [PATCH v2 1/5] monitor: split MonitorQAPIEventState, marcandre . lureau, 2015/09/17
- [Qemu-devel] [PATCH v2 2/5] monitor: introduce MonitorQAPIEventDelay callback, marcandre . lureau, 2015/09/17
- [Qemu-devel] [PATCH v2 4/5] monitor: throttle QAPI_EVENT_VSERPORT_CHANGE by "id",
marcandre . lureau <=
- [Qemu-devel] [PATCH v2 3/5] monitor: rename QDict *data->qdict, marcandre . lureau, 2015/09/17
- [Qemu-devel] [PATCH v2 5/5] monitor: remove old entries from event hash table, marcandre . lureau, 2015/09/17
- Re: [Qemu-devel] [PATCH v2 0/5] monitor: throttle VSERPORT_CHANGED by "id", Marc-André Lureau, 2015/09/23