All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hanna Czenczek <hreitz@redhat.com>
To: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org,
	John Snow <jsnow@redhat.com>, "Denis V . Lunev" <den@openvz.org>,
	Eric Blake <eblake@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [PATCH 6/9] block/accounting: Emit BLOCK_IO_DELAY event
Date: Wed, 23 Sep 2026 13:11:24 +0200	[thread overview]
Message-ID: <47928b1a-7bc4-4194-a623-b8113e419dda@redhat.com> (raw)
In-Reply-To: <arJ4JzSmzTO1GMin@redhat.com>

On 22.09.26 14:44, Kevin Wolf wrote:
> Am 31.08.2026 um 15:52 hat Hanna Czenczek geschrieben:
>> When a request finishes with a higher latency than a predefined
>> threshold, emit the BLOCK_IO_DELAY event.
>>
>> Note there would be an alternative, more precise solution: We could keep
>> all active cookies per BlockBackend in a list and repeatedly iterate
>> over it in a background coroutine (woken on a timer so it would wake
>> always exactly when the next request would time out, so it generally
>> stays asleep until there is actually a timeout). This way, we could emit
>> the event exactly when a request crosses the delay threshold, while it
>> is still running; and we could hypothetically even take actions like
>> pausing the VM until the request is done so the guest operating system
>> is shielded from extreme latency spikes.
>>
>> In practice, this is very complicated because latency cookies are
>> created and finalized all over the place, so it is very hard to
>> guarantee that every `block_acct_start()` is matched by the right
>> finalization to ensure that cookies are properly removed from the list
>> when they are done. Even if we fix all non-matching places now, there is
>> hardly a guarantee this will be kept in order in the future.
>>
>> So, for now, just implement the simpler solution of only notifying the
>> management layer when the request does complete, so VMs with high
>> latency spikes can at least be identified when they happen without
>> having to regularly check the latency histogram.
>>
>> Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
> Checking only at request completion for now is fine with me; however,
> the documentation the previous patches added (And possibly their commit
> messages? Not sure any more.) suggests that the event is emitted while
> the request is still in flight. So these description need to change to
> be consistent with the actual behaviour.

I tried being a little bit ambiguous so that behavior could change in 
the future.

But now that you make me think about it, that was a terrible idea. I 
should fix it to reflect the current behavior and explicitly state that 
it might do something else, too, in the future. (*If* we think changing 
behavior is OK, then it should at least be explicit now.)

>>   include/block/accounting.h | 10 ++++++++-
>>   block/accounting.c         | 42 +++++++++++++++++++++++++++++++++++++-
>>   blockdev.c                 |  2 +-
>>   hw/block/block.c           |  2 +-
>>   4 files changed, 52 insertions(+), 4 deletions(-)
>>
>> diff --git a/include/block/accounting.h b/include/block/accounting.h
>> index 025536239e6..ba3a6859cf4 100644
>> --- a/include/block/accounting.h
>> +++ b/include/block/accounting.h
>> @@ -92,6 +92,7 @@ struct BlockAcctStats {
>>       QSLIST_HEAD(, BlockAcctTimedStats) intervals;
>>       bool account_invalid;
>>       bool account_failed;
>> +    int64_t delay_threshold_ns;
> Why signed?

Because block/accounting.c uses int64_t for all latency values (except 
for block_acct_queue_depth()).

>>       BlockLatencyHistogram latency_histogram[BLOCK_MAX_IOTYPE];
>>   };
>>   
>> @@ -103,9 +104,16 @@ typedef struct BlockAcctCookie {
>>   } BlockAcctCookie;
>>   
>>   void block_acct_init(BlockBackend *blk, BlockAcctStats *stats);
>> +/**
>> + * Set up accounting for a block device in @stats.
>> + * @alert_ns specifies a latency so that if any request takes longer than that
>> + * threshold, a BLOCK_IO_DELAY event will be generated (when that request
>> + * completes). Pass 0 to disable.
>> + */
>>   bool block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid,
>>                         enum OnOffAuto account_failed, uint32_t *stats_intervals,
>> -                      uint32_t num_stats_intervals, Error **errp);
>> +                      uint32_t num_stats_intervals, int64_t alert_ns,
>> +                      Error **errp);
>>   void block_acct_cleanup(BlockAcctStats *stats);
>>   void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length);
>>   BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
>> diff --git a/block/accounting.c b/block/accounting.c
>> index a74551d41f2..debf1924455 100644
>> --- a/block/accounting.c
>> +++ b/block/accounting.c
>> @@ -27,8 +27,10 @@
>>   #include "block/accounting.h"
>>   #include "block/block_int.h"
>>   #include "qemu/timer.h"
>> +#include "system/block-backend.h"
>>   #include "system/qtest.h"
>>   #include "qapi/error.h"
>> +#include "qapi/qapi-events-block.h"
>>   
>>   static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
>>   static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
>> @@ -62,9 +64,35 @@ static bool bool_from_onoffauto(OnOffAuto val, bool def)
>>       }
>>   }
>>   
>> +/**
>> + * Convert a BlockAcctType into its QAPI equivalent IoAccountingOperation.
>> + *
>> + * Must only be called for valid BlockAcctType values, i.e. specifically not for
>> + * `BLOCK_ACCT_NONE`.
>> + */
>> +static IoAccountingOperation block_acct_qapi_type(enum BlockAcctType type)
>> +{
>> +    switch (type) {
>> +    case BLOCK_ACCT_READ:
>> +        return IO_ACCOUNTING_OPERATION_READ;
>> +    case BLOCK_ACCT_WRITE:
>> +        return IO_ACCOUNTING_OPERATION_WRITE;
>> +    case BLOCK_ACCT_FLUSH:
>> +        return IO_ACCOUNTING_OPERATION_FLUSH;
>> +    case BLOCK_ACCT_ZONE_APPEND:
>> +        return IO_ACCOUNTING_OPERATION_ZONE_APPEND;
>> +    case BLOCK_ACCT_UNMAP:
>> +        return IO_ACCOUNTING_OPERATION_UNMAP;
>> +    case BLOCK_ACCT_NONE:
>> +    default:
>> +        g_assert_not_reached();
>> +    }
>> +}
> Wouldn't it be nice if QAPI could generate the shorter BlockAcctType
> with BLOCK_ACCT_* on the C side while still keeping the nicer
> IoAccountingOperation name externally? :-)
>
> (Not a request to change it now, but a wishlist item for Markus.)
>
> Though actually BLOCK_ACCT_* seems to already be possible with 'prefix'.
> Maybe we could live with the longer IoAccountingOperation type name
> everywhere and avoid having two separate enums?

Probably.

(If not, then IoAccountingOperation were not doing what it’s supposed to 
be doing.)

I’ll take a look.

>>   bool block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid,
>>                         enum OnOffAuto account_failed, uint32_t *stats_intervals,
>> -                      uint32_t num_stats_intervals, Error **errp)
>> +                      uint32_t num_stats_intervals, int64_t alert_ns,
>> +                      Error **errp)
>>   {
>>       stats->account_invalid = bool_from_onoffauto(account_invalid,
>>                                                    stats->account_invalid);
>> @@ -79,6 +107,7 @@ bool block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid,
>>               block_acct_add_interval(stats, stats_intervals[i]);
>>           }
>>       }
>> +    stats->delay_threshold_ns = alert_ns;
>>       return true;
>>   }
>>   
>> @@ -252,6 +281,17 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
>>           return;
>>       }
>>   
>> +    if (stats->delay_threshold_ns && latency_ns > stats->delay_threshold_ns) {
>> +        g_autofree char *dev_path = blk_get_attached_dev_path(stats->blk);
>> +        double latency = latency_ns / (double)NANOSECONDS_PER_SECOND;
> Wouldn't it be both easier and more reliable to just expose an integer
> latency-ns in QAPI? I think all other time related values in the block
> layer interfaces work this way, too.

Yup, noted to use nanoseconds exclusively.

>> +        qapi_event_send_block_io_delay(dev_path,
>> +                                       block_acct_qapi_type(cookie->type),
>> +                                       latency,
>> +                                       cookie->offset >= 0, cookie->offset,
>> +                                       cookie->bytes);
>> +    }
>> +
>>       WITH_QEMU_LOCK_GUARD(&stats->lock) {
>>           if (failed) {
>>               stats->failed_ops[cookie->type]++;
> Kevin
>

Thanks for reviewing!

Hanna



  reply	other threads:[~2026-09-23 11:12 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 13:51 [PATCH 0/9] block: BLOCK_IO_DELAY event Hanna Czenczek
2026-08-31 13:51 ` [PATCH 1/9] block/accounting: Add offset to BlockAcctCookie Hanna Czenczek
2026-09-07 10:05   ` Jesper Wendel Devantier
2026-08-31 13:51 ` [PATCH 2/9] qapi/block: Add IoAccountingOperation enum Hanna Czenczek
2026-09-03 14:32   ` Markus Armbruster
2026-09-16  8:09     ` Hanna Czenczek
2026-08-31 13:51 ` [PATCH 3/9] qapi/block: Add BLOCK_IO_DELAY event Hanna Czenczek
2026-09-03 14:36   ` Markus Armbruster
2026-09-16  8:08     ` Hanna Czenczek
2026-09-16  9:57       ` Markus Armbruster
2026-08-31 13:52 ` [PATCH 4/9] block-backend: Public blk_get_attached_dev_path() Hanna Czenczek
2026-08-31 13:52 ` [PATCH 5/9] block/accounting: Add BB field to latency checker Hanna Czenczek
2026-09-22 12:14   ` Kevin Wolf
2026-09-23 11:05     ` Hanna Czenczek
2026-08-31 13:52 ` [PATCH 6/9] block/accounting: Emit BLOCK_IO_DELAY event Hanna Czenczek
2026-09-03 14:23   ` Stefan Hajnoczi
2026-09-16  8:14     ` Hanna Czenczek
2026-09-16 12:04       ` Hanna Czenczek
2026-09-21 20:48         ` Stefan Hajnoczi
2026-09-23 10:45           ` Hanna Czenczek
2026-09-22 12:44   ` Kevin Wolf
2026-09-23 11:11     ` Hanna Czenczek [this message]
2026-08-31 13:52 ` [PATCH 7/9] block: Add delay-alert-ms property Hanna Czenczek
2026-09-03 14:30   ` Stefan Hajnoczi
2026-09-16  8:17     ` Hanna Czenczek
2026-09-22 12:51       ` Kevin Wolf
2026-08-31 13:52 ` [PATCH 8/9] block/accounting: Move latency_ns override down Hanna Czenczek
2026-09-03 15:08   ` Stefan Hajnoczi
2026-09-16  8:21     ` Hanna Czenczek
2026-09-21 20:42       ` Stefan Hajnoczi
2026-09-22 13:03         ` Kevin Wolf
2026-09-22 13:44           ` Alberto Garcia
2026-09-22 13:52             ` Kevin Wolf
2026-09-23 10:50               ` Hanna Czenczek
2026-08-31 13:52 ` [PATCH 9/9] iotests: Add delay-alert test Hanna Czenczek
2026-09-03 15:16   ` Stefan Hajnoczi
2026-09-03 14:08 ` [PATCH 0/9] block: BLOCK_IO_DELAY event Stefan Hajnoczi
2026-09-16  8:04   ` Hanna Czenczek
2026-09-21 20:41     ` Stefan Hajnoczi
2026-09-22 13:31       ` Kevin Wolf
2026-09-22 17:18         ` Stefan Hajnoczi
2026-09-23 10:52           ` Hanna Czenczek
2026-09-24 14:51             ` Stefan Hajnoczi
2026-09-23 10:48       ` Hanna Czenczek
2026-09-23 14:50         ` Kevin Wolf
2026-09-24 15:17         ` Stefan Hajnoczi
2026-09-24 18:31           ` Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47928b1a-7bc4-4194-a623-b8113e419dda@redhat.com \
    --to=hreitz@redhat.com \
    --cc=armbru@redhat.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.