monitor: Clean up and simplify event rate limiting
cover monitor: Clean up and simplify event rate limiting
Markus Armbruster (3):
monitor: Fix event rate limit tracing
monitor: Factor out monitor_event_rate_limited()
monitor: Dumb down to a single event rate limit
monitor/monitor.c | 81 ++++++++++++++++++++++---------------------- monitor/trace-events | 2 +- 2 files changed, 42 insertions(+), 41 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 monitor: Fix event rate limit tracing
Trace event monitor_protocol_event_queue argument @rate is the rate event's limit. It's 1000000000 for BLOCK_IO_ERROR with BLOCK_ERROR_ACTION_STOP, but the actual rate limit is 0, i.e. unlimited. Fix that.
--- monitor/monitor.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
monitor/monitor.c +10 -9
@@ -330,12 +330,11 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) {MonitorQAPIEventConf *evconf;MonitorQAPIEventState *evstate; -bool throttled; +int64_t rate_limit;assert (event <QAPI_EVENT__MAX ); evconf = &monitor_qapi_event_conf[event]; -trace_monitor_protocol_event_queue (event, qdict, evconf->rate); - throttled = evconf->rate; + rate_limit = evconf->rate;/* * Rate limit BLOCK_IO_ERROR only for action != "stop".
@@ -350,13 +349,15 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)QDict *data =qobject_to (QDict,qdict_get (qdict,"data" ));const char *action =qdict_get_str (data,"action" );if (!strcmp (action,"stop" )) { - throttled = false; + rate_limit =0 ; } } +trace_monitor_protocol_event_queue (event, qdict, rate_limit); +QEMU_LOCK_GUARD (&monitor_lock); -if (!throttled) { +if (!rate_limit) {/* Unthrottled event */ monitor_qapi_event_emit (event, qdict); }else {
@@ -368,7 +369,7 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)if (evstate) {/* -* Timer is pending for (at least) evconf->rate ns after +* Timer is pending for (at least) @rate_limit ns after * last send. Store event for sending when timer fires, * replacing a prior stored event if any. */
@@ -376,9 +377,9 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) evstate->qdict =qobject_ref (qdict); }else {/* -* Last send was (at least) evconf->rate ns ago. +* Last send was (at least) @rate_limit ns ago. * Send immediately, and arm the timer to call -* monitor_qapi_event_handler() in evconf->rate ns. Any +* monitor_qapi_event_handler() in @rate_limit ns. Any * events arriving before then will be delayed until then. */ int64_t now =qemu_clock_get_ns (monitor_get_event_clock ());
@@ -393,7 +394,7 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) monitor_qapi_event_handler, evstate);g_hash_table_add (monitor_qapi_event_state, evstate); -timer_mod_ns (evstate->timer, now + evconf->rate); +timer_mod_ns (evstate->timer, now + rate_limit); } } }
Signature · 1 line
2.53.0
PATCH 2/3 monitor: Factor out monitor_event_rate_limited()
In reply to Markus Armbruster--- monitor/monitor.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
monitor/monitor.c +17 -11
@@ -319,17 +319,9 @@ static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) } } -static void monitor_qapi_event_handler (void *opaque); - -/* -* Queue a new event for emission to Monitor instances, -* applying any rate limiting if required. -*/ -static void -monitor_qapi_event_queue_no_reenter (QAPIEvent event,QDict *qdict) +static int64_t monitor_event_rate_limit (QAPIEvent event,QDict *data) {MonitorQAPIEventConf *evconf; -MonitorQAPIEventState *evstate;int64_t rate_limit;assert (event <QAPI_EVENT__MAX );
@@ -346,13 +338,28 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)* limited, so we can do without rate limiting. */ if (event ==QAPI_EVENT_BLOCK_IO_ERROR ) { -QDict *data =qobject_to (QDict,qdict_get (qdict,"data" ));const char *action =qdict_get_str (data,"action" );if (!strcmp (action,"stop" )) { rate_limit =0 ; } } +return rate_limit; +} + +static void monitor_qapi_event_handler (void *opaque); + +/* +* Queue a new event for emission to Monitor instances, +* applying any rate limiting if required. +*/ +static void +monitor_qapi_event_queue_no_reenter (QAPIEvent event,QDict *qdict) +{ +QDict *data =qobject_to (QDict,qdict_get (qdict,"data" )); +int64_t rate_limit =monitor_event_rate_limit (event, data); +MonitorQAPIEventState *evstate; +trace_monitor_protocol_event_queue (event, qdict, rate_limit);QEMU_LOCK_GUARD (&monitor_lock);
@@ -361,7 +368,6 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)/* Unthrottled event */ monitor_qapi_event_emit (event, qdict); }else { -QDict *data =qobject_to (QDict,qdict_get (qdict,"data" ));MonitorQAPIEventState key = { .event = event, .data = data }; evstate =g_hash_table_lookup (monitor_qapi_event_state, &key);
Signature · 1 line
2.53.0
PATCH 3/3 monitor: Dumb down to a single event rate limit
In reply to Markus ArmbrusterThe code lets us define event-specific rate limits. In more than a decade, we've only ever used the same 1s limit for all rate-limited events. Drop the ability and simplify.
--- monitor/monitor.c | 56 ++++++++++++++++++++------------------------ monitor/trace-events | 2 +- 2 files changed, 26 insertions(+), 32 deletions(-)
monitor/monitor.c +25 -31
@@ -46,9 +46,8 @@ typedef struct MonitorQAPIEventState {QDict *qdict;/* Delayed event (if any) */ } MonitorQAPIEventState; -typedef struct { -int64_t rate;/* Minimum time (in ns) between two events */ -}MonitorQAPIEventConf ; +/* Minimum time (in ns) between events if the event is rate-limited */ +#define EVENT_RATE (1000 * SCALE_MS)/* Shared monitor I/O thread */ IOThread *mon_iothread;
@@ -268,19 +267,6 @@ void monitor_printc(Monitor *mon, int c)monitor_printf (mon,"'" ); } -static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX ] = { -/* Limit guest-triggerable events to 1 per second */ - [QAPI_EVENT_RTC_CHANGE ] = {1000 *SCALE_MS }, - [QAPI_EVENT_BLOCK_IO_ERROR ] = {1000 *SCALE_MS }, - [QAPI_EVENT_WATCHDOG ] = {1000 *SCALE_MS }, - [QAPI_EVENT_BALLOON_CHANGE ] = {1000 *SCALE_MS }, - [QAPI_EVENT_QUORUM_REPORT_BAD ] = {1000 *SCALE_MS }, - [QAPI_EVENT_QUORUM_FAILURE ] = {1000 *SCALE_MS }, - [QAPI_EVENT_VSERPORT_CHANGE ] = {1000 *SCALE_MS }, - [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE ] = {1000 *SCALE_MS }, - [QAPI_EVENT_HV_BALLOON_STATUS_REPORT ] = {1000 *SCALE_MS }, -}; -/* * Return the clock to use for recording an event's time. * It's QEMU_CLOCK_REALTIME, except for qtests it's
@@ -319,14 +305,23 @@ static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) } } -static int64_t monitor_event_rate_limit (QAPIEvent event,QDict *data) +static bool monitor_event_is_rate_limited (QAPIEvent event,QDict *data) { -MonitorQAPIEventConf *evconf; -int64_t rate_limit; +static bool event_is_ratelimited[QAPI_EVENT__MAX ] = { + [QAPI_EVENT_RTC_CHANGE ] = true, + [QAPI_EVENT_BLOCK_IO_ERROR ] = true, + [QAPI_EVENT_WATCHDOG ] = true, + [QAPI_EVENT_BALLOON_CHANGE ] = true, + [QAPI_EVENT_QUORUM_REPORT_BAD ] = true, + [QAPI_EVENT_QUORUM_FAILURE ] = true, + [QAPI_EVENT_VSERPORT_CHANGE ] = true, + [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE ] = true, + [QAPI_EVENT_HV_BALLOON_STATUS_REPORT ] = true, + }; +bool rate_limited;assert (event <QAPI_EVENT__MAX ); - evconf = &monitor_qapi_event_conf[event]; - rate_limit = evconf->rate; + rate_limited = event_is_ratelimited[event];/* * Rate limit BLOCK_IO_ERROR only for action != "stop".
@@ -340,11 +335,11 @@ static int64_t monitor_event_rate_limit(QAPIEvent event, QDict *data)if (event ==QAPI_EVENT_BLOCK_IO_ERROR ) {const char *action =qdict_get_str (data,"action" );if (!strcmp (action,"stop" )) { - rate_limit =0 ; + rate_limited = false; } } -return rate_limit; +return rate_limited; }static void monitor_qapi_event_handler (void *opaque);
@@ -357,14 +352,14 @@ static void monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) { QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); -int64_t rate_limit =monitor_event_rate_limit (event, data); +bool rate_limited =monitor_event_is_rate_limited (event, data);MonitorQAPIEventState *evstate; -trace_monitor_protocol_event_queue (event, qdict, rate_limit); +trace_monitor_protocol_event_queue (event, qdict, rate_limited);QEMU_LOCK_GUARD (&monitor_lock); -if (!rate_limit) { +if (!rate_limited) {/* Unthrottled event */ monitor_qapi_event_emit (event, qdict); }else {
@@ -375,7 +370,7 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)if (evstate) {/* -* Timer is pending for (at least) @rate_limit ns after +* Timer is pending for (at least) @EVENT_RATE ns after * last send. Store event for sending when timer fires, * replacing a prior stored event if any. */
@@ -385,7 +380,7 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)/* * Last send was (at least) @rate_limit ns ago. * Send immediately, and arm the timer to call -* monitor_qapi_event_handler() in @rate_limit ns. Any +* monitor_qapi_event_handler() in @EVENT_RATE ns. Any * events arriving before then will be delayed until then. */ int64_t now =qemu_clock_get_ns (monitor_get_event_clock ());
@@ -400,7 +395,7 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) monitor_qapi_event_handler, evstate);g_hash_table_add (monitor_qapi_event_state, evstate); -timer_mod_ns (evstate->timer, now + rate_limit); +timer_mod_ns (evstate->timer, now +EVENT_RATE ); } } }
@@ -454,7 +449,6 @@ void qapi_event_emit(QAPIEvent event, QDict *qdict)static void monitor_qapi_event_handler (void *opaque) {MonitorQAPIEventState *evstate = opaque; -MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];trace_monitor_protocol_event_handler (evstate->event, evstate->qdict);QEMU_LOCK_GUARD (&monitor_lock);
@@ -465,7 +459,7 @@ static void monitor_qapi_event_handler(void *opaque)monitor_qapi_event_emit (evstate->event, evstate->qdict);qobject_unref (evstate->qdict); evstate->qdict =NULL ; -timer_mod_ns (evstate->timer, now + evconf->rate); +timer_mod_ns (evstate->timer, now +EVENT_RATE ); }else {g_hash_table_remove (monitor_qapi_event_state, evstate);qobject_unref (evstate->data);
monitor/trace-events +1 -1
@@ -6,7 +6,7 @@ handle_hmp_command(void *mon, const char *cmdline) "mon %p cmdline: %s" # monitor.c monitor_protocol_event_handler(uint32_t event, void *qdict) "event=%d data=%p" monitor_protocol_event_emit(uint32_t event, void *data) "event=%d data=%p" -monitor_protocol_event_queue(uint32_t event, void *qdict, uint64_t rate) "event=%d data=%p rate=%" PRId64 +monitor_protocol_event_queue(uint32_t event, void *qdict, bool rate_limited) "event=%d data=%p rate_limited=%d" monitor_suspend(void *ptr, int cnt) "mon %p: %d" # qmp.c
Signature · 1 line
2.53.0