Skip to contentSkip to conversation

discard blockstats

PATCH Superseded Anton Nefedov 3 participants 20 messages Oct 31, 2018 – Nov 26, 2018 Block layer

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover discard blockstats

new in v5:
- new patch 3. Protects from double-accounting IDE trim requests in ide_handle_rw_error().
- patch 4: move block_acct_invalid() for unmap to ide_issue_trim_cb() This way it doesn't affect read and write requests.
- patch 7: added missing hunks as suggested by Kevin

v4: http://lists.nongnu.org/archive/html/qemu-devel/2018-08/msg04308.html
v3: http://lists.nongnu.org/archive/html/qemu-devel/2018-06/msg03688.html

----

qmp query-blockstats provides stats info for write/read/flush ops.

Patches 1-7 implement the similar for discard (unmap) command for scsi and ide disks.
Discard stat "unmap_ops / unmap_bytes" is supposed to account the ops that have completed without an error.

However, discard operation is advisory. Specifically,
- common block layer ignores ENOTSUP error code.
That might be returned if the block driver does not support discard, or discard has been configured to be ignored.
- format drivers such as qcow2 may ignore discard if they were configured to ignore that, or if the corresponding area is already marked unused (unallocated / zero clusters).

And what is actually useful is the number of bytes actually discarded down on the host filesystem.
To achieve that, driver-specific statistics has been added to blockstats (patch 9).
With patch 8, file-posix driver accounts discard operations on its level too.

query-blockstat result:

(note the difference between blockdevice unmap and file discard stats. qcow2 sends fewer ops down to the file as the clusters are actually unallocated on qcow2 level)

    {
      "device": "drive-scsi0-0-0-0",
      "node-name": "#block159",
      "stats": {
Quoted 2 lines

"invalid_unmap_operations": 0,
"failed_unmap_operations": 0,

        "wr_highest_offset": 13411688448,
        "rd_total_time_ns": 2859566315,
        "rd_bytes": 103182336,
        "rd_merged": 0,
        "flush_operations": 19,
        "invalid_wr_operations": 0,
        "flush_total_time_ns": 23111608,
        "failed_rd_operations": 0,
        "failed_flush_operations": 0,
        "invalid_flush_operations": 0,
        "timed_stats": [
          
        ],
        "wr_merged": 0,
        "wr_bytes": 1702912,
Quoted 2 lines
      "unmap_bytes": 11954954240,
      "unmap_operations": 865,
        "idle_time_ns": 2669508623,
        "account_invalid": true,
Quoted 1 line
      "unmap_total_time_ns": 19698002,
        "wr_operations": 143,
        "failed_wr_operations": 0,
        "rd_operations": 4816,
        "account_failed": true,
Quoted 1 line
      "unmap_merged": 0,
        "wr_total_time_ns": 1262686124,
        "invalid_rd_operations": 0
      },
      "parent": {
Quoted 6 lines
      "driver-specific": {
        "discard-nb-failed": 0,
        "discard-bytes-ok": 720896,
        "driver": "file",
        "discard-nb-ok": 8
      },
        "node-name": "#block009",
        "stats": {
        [..]
        }
      }
    },
    {
      "device": "floppy0",

Anton Nefedov (9):
qapi: group BlockDeviceStats fields
qapi: add unmap to BlockDeviceStats
block: add empty account cookie type
ide: account UNMAP (TRIM) operations
scsi: store unmap offset and nb_sectors in request struct
scsi: move unmap error checking to the complete callback
scsi: account unmap operations
file-posix: account discard operations
qapi: query-blockstat: add driver specific file-posix stats

 qapi/block-core.json       | 82 +++++++++++++++++++++++++++++++++-----
 include/block/accounting.h |  2 +
 include/block/block.h      |  1 +
 include/block/block_int.h  |  1 +
 block.c                    |  9 +++++
 block/accounting.c         |  6 +++
 block/file-posix.c         | 45 +++++++++++++++++++--
 block/qapi.c               | 11 +++++
 hw/ide/core.c              | 12 ++++++
 hw/scsi/scsi-disk.c        | 32 ++++++++++-----
 tests/qemu-iotests/227.out | 18 +++++++++
 11 files changed, 194 insertions(+), 25 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v5 1/9 qapi: group BlockDeviceStats fields

Make the stat fields definition slightly more readable. Also reorder total_time_ns stats read-write-flush as done elsewhere. Cosmetic change only.

---
 qapi/block-core.json | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
qapi/block-core.json +15 -11
index 0fc1590c1b..2903b8dfc9 100644
@@ -784,12 +784,12 @@
 # @flush_operations: The number of cache flush operations performed by the
 #                    device (since 0.15.0)
 #
-# @flush_total_time_ns: Total time spend on cache flushes in nano-seconds
-#                       (since 0.15.0).
+# @rd_total_time_ns: Total time spent on reads in nanoseconds (since 0.15.0).
 #
-# @wr_total_time_ns: Total time spend on writes in nano-seconds (since 0.15.0).
+# @wr_total_time_ns: Total time spent on writes in nanoseconds (since 0.15.0).
 #
-# @rd_total_time_ns: Total_time_spend on reads in nano-seconds (since 0.15.0).
+# @flush_total_time_ns: Total time spent on cache flushes in nanoseconds
+#                       (since 0.15.0).
 #
 # @wr_highest_offset: The offset after the greatest byte written to the
 #                     device.  The intended use of this information is for
@@ -842,14 +842,18 @@
 # Since: 0.14.0
 ##
 { 'struct': 'BlockDeviceStats',
-  'data': {'rd_bytes': 'int', 'wr_bytes': 'int', 'rd_operations': 'int',
-           'wr_operations': 'int', 'flush_operations': 'int',
-           'flush_total_time_ns': 'int', 'wr_total_time_ns': 'int',
-           'rd_total_time_ns': 'int', 'wr_highest_offset': 'int',
-           'rd_merged': 'int', 'wr_merged': 'int', '*idle_time_ns': 'int',
+  'data': {'rd_bytes': 'int', 'wr_bytes': 'int',
+           'rd_operations': 'int', 'wr_operations': 'int',
+           'flush_operations': 'int',
+           'rd_total_time_ns': 'int', 'wr_total_time_ns': 'int',
+           'flush_total_time_ns': 'int',
+           'wr_highest_offset': 'int',
+           'rd_merged': 'int', 'wr_merged': 'int',
+           '*idle_time_ns': 'int',
            'failed_rd_operations': 'int', 'failed_wr_operations': 'int',
-           'failed_flush_operations': 'int', 'invalid_rd_operations': 'int',
-           'invalid_wr_operations': 'int', 'invalid_flush_operations': 'int',
+           'failed_flush_operations': 'int',
+           'invalid_rd_operations': 'int', 'invalid_wr_operations': 'int',
+           'invalid_flush_operations': 'int',
            'account_invalid': 'bool', 'account_failed': 'bool',
            'timed_stats': ['BlockDeviceTimedStats'],
            '*x_rd_latency_histogram': 'BlockLatencyHistogramInfo',
Signature · 1 line
2.17.1
Reviewed-by on v5 1/9
#3 lore raw

Re: [Qemu-devel] [PATCH v5 1/9] qapi: group BlockDeviceStats fields

In reply to Anton Nefedov
Quoted 5 lines · Anton Nefedov

Make the stat fields definition slightly more readable. Also reorder total_time_ns stats read-write-flush as done elsewhere. Cosmetic change only.

Quoted 52 lines
---
  qapi/block-core.json | 26 +++++++++++++++-----------
  1 file changed, 15 insertions(+), 11 deletions(-)
qapi/block-core.json +15 -11
index 0fc1590c1b..2903b8dfc9 100644
@@ -784,12 +784,12 @@
  # @flush_operations: The number of cache flush operations performed by the
  #                    device (since 0.15.0)
  #
-# @flush_total_time_ns: Total time spend on cache flushes in nano-seconds
-#                       (since 0.15.0).
+# @rd_total_time_ns: Total time spent on reads in nanoseconds (since 0.15.0).
  #
-# @wr_total_time_ns: Total time spend on writes in nano-seconds (since 0.15.0).
+# @wr_total_time_ns: Total time spent on writes in nanoseconds (since 0.15.0).
  #
-# @rd_total_time_ns: Total_time_spend on reads in nano-seconds (since 0.15.0).
+# @flush_total_time_ns: Total time spent on cache flushes in nanoseconds
+#                       (since 0.15.0).
  #
  # @wr_highest_offset: The offset after the greatest byte written to the
  #                     device.  The intended use of this information is for
@@ -842,14 +842,18 @@
  # Since: 0.14.0
  ##
  { 'struct': 'BlockDeviceStats',
-  'data': {'rd_bytes': 'int', 'wr_bytes': 'int', 'rd_operations': 'int',
-           'wr_operations': 'int', 'flush_operations': 'int',
-           'flush_total_time_ns': 'int', 'wr_total_time_ns': 'int',
-           'rd_total_time_ns': 'int', 'wr_highest_offset': 'int',
-           'rd_merged': 'int', 'wr_merged': 'int', '*idle_time_ns': 'int',
+  'data': {'rd_bytes': 'int', 'wr_bytes': 'int',
+           'rd_operations': 'int', 'wr_operations': 'int',
+           'flush_operations': 'int',
+           'rd_total_time_ns': 'int', 'wr_total_time_ns': 'int',
+           'flush_total_time_ns': 'int',
+           'wr_highest_offset': 'int',
+           'rd_merged': 'int', 'wr_merged': 'int',
+           '*idle_time_ns': 'int',
             'failed_rd_operations': 'int', 'failed_wr_operations': 'int',
-           'failed_flush_operations': 'int', 'invalid_rd_operations': 'int',
-           'invalid_wr_operations': 'int', 'invalid_flush_operations': 'int',
+           'failed_flush_operations': 'int',
+           'invalid_rd_operations': 'int', 'invalid_wr_operations': 'int',
+           'invalid_flush_operations': 'int',
             'account_invalid': 'bool', 'account_failed': 'bool',
             'timed_stats': ['BlockDeviceTimedStats'],
             '*x_rd_latency_histogram': 'BlockLatencyHistogramInfo',
Signature · 2 lines
Best regards,
Vladimir
#4 lore raw

PATCH v5 2/9 qapi: add unmap to BlockDeviceStats

In reply to Anton Nefedov
---
 qapi/block-core.json       | 29 +++++++++++++++++++++++------
 include/block/accounting.h |  1 +
 block/qapi.c               |  6 ++++++
 tests/qemu-iotests/227.out | 18 ++++++++++++++++++
 4 files changed, 48 insertions(+), 6 deletions(-)
qapi/block-core.json +23 -6
index 2903b8dfc9..01da84cb61 100644
@@ -777,6 +777,8 @@
 #
 # @wr_bytes:      The number of bytes written by the device.
 #
+# @unmap_bytes: The number of bytes unmapped by the device (Since 3.1)
+#
 # @rd_operations: The number of read operations performed by the device.
 #
 # @wr_operations: The number of write operations performed by the device.
@@ -784,6 +786,9 @@
 # @flush_operations: The number of cache flush operations performed by the
 #                    device (since 0.15.0)
 #
+# @unmap_operations: The number of unmap operations performed by the device
+#                    (Since 3.1)
+#
 # @rd_total_time_ns: Total time spent on reads in nanoseconds (since 0.15.0).
 #
 # @wr_total_time_ns: Total time spent on writes in nanoseconds (since 0.15.0).
@@ -791,6 +796,9 @@
 # @flush_total_time_ns: Total time spent on cache flushes in nanoseconds
 #                       (since 0.15.0).
 #
+# @unmap_total_time_ns: Total time spent on unmap operations in nanoseconds
+#                       (Since 3.1)
+#
 # @wr_highest_offset: The offset after the greatest byte written to the
 #                     device.  The intended use of this information is for
 #                     growable sparse files (like qcow2) that are used on top
@@ -802,6 +810,9 @@
 # @wr_merged: Number of write requests that have been merged into another
 #             request (Since 2.3).
 #
+# @unmap_merged: Number of unmap requests that have been merged into another
+#                request (Since 3.1)
+#
 # @idle_time_ns: Time since the last I/O operation, in
 #                nanoseconds. If the field is absent it means that
 #                there haven't been any operations yet (Since 2.5).
@@ -815,6 +826,9 @@
 # @failed_flush_operations: The number of failed flush operations
 #                           performed by the device (Since 2.5)
 #
+# @failed_unmap_operations: The number of failed unmap operations performed
+#                           by the device (Since 3.1)
+#
 # @invalid_rd_operations: The number of invalid read operations
 #                          performed by the device (Since 2.5)
 #
@@ -824,6 +838,9 @@
 # @invalid_flush_operations: The number of invalid flush operations
 #                            performed by the device (Since 2.5)
 #
+# @invalid_unmap_operations: The number of invalid unmap operations performed
+#                            by the device (Since 3.1)
+#
 # @account_invalid: Whether invalid operations are included in the
 #                   last access statistics (Since 2.5)
 #
@@ -842,18 +859,18 @@
 # Since: 0.14.0
 ##
 { 'struct': 'BlockDeviceStats',
-  'data': {'rd_bytes': 'int', 'wr_bytes': 'int',
+  'data': {'rd_bytes': 'int', 'wr_bytes': 'int', 'unmap_bytes' : 'int',
            'rd_operations': 'int', 'wr_operations': 'int',
-           'flush_operations': 'int',
+           'flush_operations': 'int', 'unmap_operations': 'int',
            'rd_total_time_ns': 'int', 'wr_total_time_ns': 'int',
-           'flush_total_time_ns': 'int',
+           'flush_total_time_ns': 'int', 'unmap_total_time_ns': 'int',
            'wr_highest_offset': 'int',
-           'rd_merged': 'int', 'wr_merged': 'int',
+           'rd_merged': 'int', 'wr_merged': 'int', 'unmap_merged': 'int',
            '*idle_time_ns': 'int',
            'failed_rd_operations': 'int', 'failed_wr_operations': 'int',
-           'failed_flush_operations': 'int',
+           'failed_flush_operations': 'int', 'failed_unmap_operations': 'int',
            'invalid_rd_operations': 'int', 'invalid_wr_operations': 'int',
-           'invalid_flush_operations': 'int',
+           'invalid_flush_operations': 'int', 'invalid_unmap_operations': 'int',
            'account_invalid': 'bool', 'account_failed': 'bool',
            'timed_stats': ['BlockDeviceTimedStats'],
            '*x_rd_latency_histogram': 'BlockLatencyHistogramInfo',
include/block/accounting.h +1 -0
index d1f67b10dd..ba8b04d572 100644
@@ -36,6 +36,7 @@ enum BlockAcctType {
     BLOCK_ACCT_READ,
     BLOCK_ACCT_WRITE,
     BLOCK_ACCT_FLUSH,
+    BLOCK_ACCT_UNMAP,
     BLOCK_MAX_IOTYPE,
 };
 
block/qapi.c +6 -0
index c66f949db8..df31f351d2 100644
@@ -432,24 +432,30 @@ static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
 
     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
+    ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
+    ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
 
     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
+    ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
 
     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
     ds->invalid_flush_operations =
         stats->invalid_ops[BLOCK_ACCT_FLUSH];
+    ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
 
     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
+    ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
+    ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
 
     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
     if (ds->has_idle_time_ns) {
tests/qemu-iotests/227.out +18 -0
index 736f2e3b11..62a9dbaffa 100644
@@ -15,6 +15,8 @@ Testing: -drive driver=null-co,if=virtio
         {
             "device": "virtio0",
             "stats": {
+                "unmap_operations": 0,
+                "unmap_merged": 0,
                 "flush_total_time_ns": 0,
                 "wr_highest_offset": 0,
                 "wr_total_time_ns": 0,
@@ -24,13 +26,17 @@ Testing: -drive driver=null-co,if=virtio
                 "wr_bytes": 0,
                 "timed_stats": [
                 ],
+                "failed_unmap_operations": 0,
                 "failed_flush_operations": 0,
                 "account_invalid": true,
                 "rd_total_time_ns": 0,
+                "invalid_unmap_operations": 0,
                 "flush_operations": 0,
                 "wr_operations": 0,
+                "unmap_bytes": 0,
                 "rd_merged": 0,
                 "rd_bytes": 0,
+                "unmap_total_time_ns": 0,
                 "invalid_flush_operations": 0,
                 "account_failed": true,
                 "rd_operations": 0,
@@ -73,6 +79,8 @@ Testing: -drive driver=null-co,if=none
         {
             "device": "none0",
             "stats": {
+                "unmap_operations": 0,
+                "unmap_merged": 0,
                 "flush_total_time_ns": 0,
                 "wr_highest_offset": 0,
                 "wr_total_time_ns": 0,
@@ -82,13 +90,17 @@ Testing: -drive driver=null-co,if=none
                 "wr_bytes": 0,
                 "timed_stats": [
                 ],
+                "failed_unmap_operations": 0,
                 "failed_flush_operations": 0,
                 "account_invalid": true,
                 "rd_total_time_ns": 0,
+                "invalid_unmap_operations": 0,
                 "flush_operations": 0,
                 "wr_operations": 0,
+                "unmap_bytes": 0,
                 "rd_merged": 0,
                 "rd_bytes": 0,
+                "unmap_total_time_ns": 0,
                 "invalid_flush_operations": 0,
                 "account_failed": true,
                 "rd_operations": 0,
@@ -160,6 +172,8 @@ Testing: -blockdev driver=null-co,node-name=null -device virtio-blk,drive=null,i
         {
             "device": "",
             "stats": {
+                "unmap_operations": 0,
+                "unmap_merged": 0,
                 "flush_total_time_ns": 0,
                 "wr_highest_offset": 0,
                 "wr_total_time_ns": 0,
@@ -169,13 +183,17 @@ Testing: -blockdev driver=null-co,node-name=null -device virtio-blk,drive=null,i
                 "wr_bytes": 0,
                 "timed_stats": [
                 ],
+                "failed_unmap_operations": 0,
                 "failed_flush_operations": 0,
                 "account_invalid": false,
                 "rd_total_time_ns": 0,
+                "invalid_unmap_operations": 0,
                 "flush_operations": 0,
                 "wr_operations": 0,
+                "unmap_bytes": 0,
                 "rd_merged": 0,
                 "rd_bytes": 0,
+                "unmap_total_time_ns": 0,
                 "invalid_flush_operations": 0,
                 "account_failed": false,
                 "rd_operations": 0,
Signature · 1 line
2.17.1
#5 lore raw

PATCH v5 3/9 block: add empty account cookie type

In reply to Anton Nefedov

This adds some protection from accounting unitialized cookie. That is, block_acct_failed/done without previous block_acct_start; in that case, cookie probably holds values from previous operation.

(Note: it might also be unitialized holding garbage value and there is
still "< BLOCK_MAX_IOTYPE" assertion for that.
So block_acct_failed/done without previous block_acct_start should be used with caution.)

Currently this is particularly useful in ide code where it's hard to keep track whether the request started accounting or not. For example, trim requests do the accounting separately.

---
 include/block/accounting.h | 1 +
 block/accounting.c         | 6 ++++++
 2 files changed, 7 insertions(+)
include/block/accounting.h +1 -0
index ba8b04d572..878b4c3581 100644
@@ -33,6 +33,7 @@ typedef struct BlockAcctTimedStats BlockAcctTimedStats;
 typedef struct BlockAcctStats BlockAcctStats;
 
 enum BlockAcctType {
+    BLOCK_ACCT_NONE = 0,
     BLOCK_ACCT_READ,
     BLOCK_ACCT_WRITE,
     BLOCK_ACCT_FLUSH,
block/accounting.c +6 -0
index 70a3d9a426..8d41c8a83a 100644
@@ -195,6 +195,10 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
 
     assert(cookie->type < BLOCK_MAX_IOTYPE);
 
+    if (cookie->type == BLOCK_ACCT_NONE) {
+        return;
+    }
+
     qemu_mutex_lock(&stats->lock);
 
     if (failed) {
@@ -217,6 +221,8 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
     }
 
     qemu_mutex_unlock(&stats->lock);
+
+    cookie->type = BLOCK_ACCT_NONE;
 }
 
 void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
Signature · 1 line
2.17.1
#6 lore raw

Re: [Qemu-devel] [PATCH v5 3/9] block: add empty account cookie type

Quoted 1 line · Anton Nefedov

This adds some protection from accounting unitialized cookie.

uninitialized

Quoted 4 lines

That is, block_acct_failed/done without previous block_acct_start; in that case, cookie probably holds values from previous operation.

(Note: it might also be unitialized holding garbage value and there is

and here.

Quoted 7 lines

still "< BLOCK_MAX_IOTYPE" assertion for that.
So block_acct_failed/done without previous block_acct_start should be used with caution.)

Currently this is particularly useful in ide code where it's hard to keep track whether the request started accounting or not. For example, trim requests do the accounting separately.

so, is it an existing bug? Could you please give an example in the code of such wrong (before the patch) accounting?

Quoted 44 lines
---
  include/block/accounting.h | 1 +
  block/accounting.c         | 6 ++++++
  2 files changed, 7 insertions(+)
include/block/accounting.h +1 -0
index ba8b04d572..878b4c3581 100644
@@ -33,6 +33,7 @@ typedef struct BlockAcctTimedStats BlockAcctTimedStats;
  typedef struct BlockAcctStats BlockAcctStats;
 
  enum BlockAcctType {
+    BLOCK_ACCT_NONE = 0,
      BLOCK_ACCT_READ,
      BLOCK_ACCT_WRITE,
      BLOCK_ACCT_FLUSH,
block/accounting.c +6 -0
index 70a3d9a426..8d41c8a83a 100644
@@ -195,6 +195,10 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
 
      assert(cookie->type < BLOCK_MAX_IOTYPE);
 
+    if (cookie->type == BLOCK_ACCT_NONE) {
+        return;
+    }
+
      qemu_mutex_lock(&stats->lock);
 
      if (failed) {
@@ -217,6 +221,8 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
      }
 
      qemu_mutex_unlock(&stats->lock);
+
+    cookie->type = BLOCK_ACCT_NONE;
  }
 
  void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
Signature · 2 lines
Best regards,
Vladimir
#7 lore raw

Re: [Qemu-devel] [PATCH v5 3/9] block: add empty account cookie type

Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Quoted 1 line · Anton Nefedov

This adds some protection from accounting unitialized cookie.

uninitialized

fixed

Quoted 7 lines
Quoted 4 lines

That is, block_acct_failed/done without previous block_acct_start; in that case, cookie probably holds values from previous operation.

(Note: it might also be unitialized holding garbage value and there is

and here.

fixed

Quoted 10 lines
Quoted 7 lines

still "< BLOCK_MAX_IOTYPE" assertion for that.
So block_acct_failed/done without previous block_acct_start should be used with caution.)

Currently this is particularly useful in ide code where it's hard to keep track whether the request started accounting or not. For example, trim requests do the accounting separately.

so, is it an existing bug? Could you please give an example in the code of such wrong (before the patch) accounting?

e.g. look at ide_dma_cb()->ide_handle_rw_error() call. In IDE_DMA_TRIM case, the cookie is never initialized, but it is block_acct_failed()-ed anyway.

Quoted 46 lines
Quoted 44 lines
---
   include/block/accounting.h | 1 +
   block/accounting.c         | 6 ++++++
   2 files changed, 7 insertions(+)
include/block/accounting.h +1 -0
index ba8b04d572..878b4c3581 100644
@@ -33,6 +33,7 @@ typedef struct BlockAcctTimedStats BlockAcctTimedStats;
   typedef struct BlockAcctStats BlockAcctStats;
 
   enum BlockAcctType {
+    BLOCK_ACCT_NONE = 0,
       BLOCK_ACCT_READ,
       BLOCK_ACCT_WRITE,
       BLOCK_ACCT_FLUSH,
block/accounting.c +6 -0
index 70a3d9a426..8d41c8a83a 100644
@@ -195,6 +195,10 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
 
       assert(cookie->type < BLOCK_MAX_IOTYPE);
 
+    if (cookie->type == BLOCK_ACCT_NONE) {
+        return;
+    }
+
       qemu_mutex_lock(&stats->lock);
 
       if (failed) {
@@ -217,6 +221,8 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
       }
 
       qemu_mutex_unlock(&stats->lock);
+
+    cookie->type = BLOCK_ACCT_NONE;
   }
 
   void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
#8 lore raw

PATCH v5 4/9 ide: account UNMAP (TRIM) operations

In reply to Anton Nefedov
---
 hw/ide/core.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/ide/core.c +12 -0
index 04e22e751d..8da77ff3e3 100644
@@ -441,6 +441,14 @@ static void ide_issue_trim_cb(void *opaque, int ret)
     TrimAIOCB *iocb = opaque;
     IDEState *s = iocb->s;
 
+    if (iocb->i >= 0) {
+        if (ret >= 0) {
+            block_acct_done(blk_get_stats(s->blk), &s->acct);
+        } else {
+            block_acct_failed(blk_get_stats(s->blk), &s->acct);
+        }
+    }
+
     if (ret >= 0) {
         while (iocb->j < iocb->qiov->niov) {
             int j = iocb->j;
@@ -458,10 +466,14 @@ static void ide_issue_trim_cb(void *opaque, int ret)
                 }
 
                 if (!ide_sect_range_ok(s, sector, count)) {
+                    block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_UNMAP);
                     iocb->ret = -EINVAL;
                     goto done;
                 }
 
+                block_acct_start(blk_get_stats(s->blk), &s->acct,
+                                 count << BDRV_SECTOR_BITS, BLOCK_ACCT_UNMAP);
+
                 /* Got an entry! Submit and exit.  */
                 iocb->aiocb = blk_aio_pdiscard(s->blk,
                                                sector << BDRV_SECTOR_BITS,
Signature · 1 line
2.17.1
#10 lore raw

PATCH v5 5/9 scsi: store unmap offset and nb_sectors in request struct

In reply to Anton Nefedov

it allows to report it in the error handler

---
 hw/scsi/scsi-disk.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
hw/scsi/scsi-disk.c +5 -7
index e2c5408aa2..661228b569 100644
@@ -1656,8 +1656,6 @@ static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
 {
     SCSIDiskReq *r = data->r;
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
-    uint64_t sector_num;
-    uint32_t nb_sectors;
 
     assert(r->req.aiocb == NULL);
     if (scsi_disk_req_check_error(r, ret, false)) {
@@ -1665,16 +1663,16 @@ static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
     }
 
     if (data->count > 0) {
-        sector_num = ldq_be_p(&data->inbuf[0]);
-        nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
-        if (!check_lba_range(s, sector_num, nb_sectors)) {
+        r->sector = ldq_be_p(&data->inbuf[0]);
+        r->sector_count = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
+        if (!check_lba_range(s, r->sector, r->sector_count)) {
             scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
             goto done;
         }
 
         r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk,
-                                        sector_num * s->qdev.blocksize,
-                                        nb_sectors * s->qdev.blocksize,
+                                        r->sector * s->qdev.blocksize,
+                                        r->sector_count * s->qdev.blocksize,
                                         scsi_unmap_complete, data);
         data->count--;
         data->inbuf += 16;
Signature · 1 line
2.17.1
#11 lore raw

PATCH v5 6/9 scsi: move unmap error checking to the complete callback

In reply to Anton Nefedov

This will help to account the operation in the following commit.

The difference is that we don't call scsi_disk_req_check_error() before the 1st discard iteration anymore. That function also checks if the request is cancelled, however it shouldn't get canceled until it yields in blk_aio() functions anyway.
Same approach is already used for emulate_write_same.

---
 hw/scsi/scsi-disk.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
hw/scsi/scsi-disk.c +6 -4
index 661228b569..e132504913 100644
@@ -1658,9 +1658,6 @@ static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
 
     assert(r->req.aiocb == NULL);
-    if (scsi_disk_req_check_error(r, ret, false)) {
-        goto done;
-    }
 
     if (data->count > 0) {
         r->sector = ldq_be_p(&data->inbuf[0]);
@@ -1696,7 +1693,12 @@ static void scsi_unmap_complete(void *opaque, int ret)
     r->req.aiocb = NULL;
 
     aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
-    scsi_unmap_complete_noio(data, ret);
+    if (scsi_disk_req_check_error(r, ret, false)) {
+        scsi_req_unref(&r->req);
+        g_free(data);
+    } else {
+        scsi_unmap_complete_noio(data, ret);
+    }
     aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
 }
 
Signature · 1 line
2.17.1
#12 lore raw

PATCH v5 7/9 scsi: account unmap operations

In reply to Anton Nefedov
---
 hw/scsi/scsi-disk.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/scsi/scsi-disk.c +11 -1
index e132504913..dee71f9dde 100644
@@ -1663,10 +1663,16 @@ static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
         r->sector = ldq_be_p(&data->inbuf[0]);
         r->sector_count = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
         if (!check_lba_range(s, r->sector, r->sector_count)) {
+            block_acct_invalid(blk_get_stats(s->qdev.conf.blk),
+                               BLOCK_ACCT_UNMAP);
             scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
             goto done;
         }
 
+        block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
+                         r->sector_count * s->qdev.blocksize,
+                         BLOCK_ACCT_UNMAP);
+
         r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk,
                                         r->sector * s->qdev.blocksize,
                                         r->sector_count * s->qdev.blocksize,
@@ -1693,10 +1699,11 @@ static void scsi_unmap_complete(void *opaque, int ret)
     r->req.aiocb = NULL;
 
     aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
-    if (scsi_disk_req_check_error(r, ret, false)) {
+    if (scsi_disk_req_check_error(r, ret, true)) {
         scsi_req_unref(&r->req);
         g_free(data);
     } else {
+        block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
         scsi_unmap_complete_noio(data, ret);
     }
     aio_context_release(blk_get_aio_context(s->qdev.conf.blk));
@@ -1728,6 +1735,7 @@ static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
     }
 
     if (blk_is_read_only(s->qdev.conf.blk)) {
+        block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP);
         scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
         return;
     }
@@ -1743,10 +1751,12 @@ static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
     return;
 
 invalid_param_len:
+    block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP);
     scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
     return;
 
 invalid_field:
+    block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP);
     scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
 }
 
Signature · 1 line
2.17.1
Reviewed-by on v5 7/9
#13 lore raw

Re: [Qemu-devel] [PATCH v5 7/9] scsi: account unmap operations

Quoted 1 line · Anton Nefedov

but be careful: on git am, the chunk about read-only case goes into scsi_disk_emulate_write_same instead of scsi_disk_emulate_unmap (at least for me, with latest master branch and git 2.18

Quoted 39 lines
---
  hw/scsi/scsi-disk.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
hw/scsi/scsi-disk.c +8 -1
index e132504913..dee71f9dde 100644
@@ -1663,10 +1663,16 @@ static void scsi_unmap_complete_noio(UnmapCBData *data, int ret)
          r->sector = ldq_be_p(&data->inbuf[0]);
          r->sector_count = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL;
          if (!check_lba_range(s, r->sector, r->sector_count)) {
+            block_acct_invalid(blk_get_stats(s->qdev.conf.blk),
+                               BLOCK_ACCT_UNMAP);
              scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE));
              goto done;
          }
 
+        block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct,
+                         r->sector_count * s->qdev.blocksize,
+                         BLOCK_ACCT_UNMAP);
+
          r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk,
                                          r->sector * s->qdev.blocksize,
                                          r->sector_count * s->qdev.blocksize,
@@ -1693,10 +1699,11 @@ static void scsi_unmap_complete(void *opaque, int ret)
      r->req.aiocb = NULL;
 
      aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk));
-    if (scsi_disk_req_check_error(r, ret, false)) {
+    if (scsi_disk_req_check_error(r, ret, true)) {
          scsi_req_unref(&r->req);
          g_free(data);
      } else {
+        block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct);
          scsi_unmap_complete_noio(data, ret);
      }
      aio_context_release(blk_get_aio_context(s->qdev.conf.blk));

hw/scsi/scsi-disk.c static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf) · line 1738

Quoted 4 lines
hw/scsi/scsi-disk.c +1 -0
@@ -1728,6 +1735,7 @@ static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
      }
 
      if (blk_is_read_only(s->qdev.conf.blk)) {
+        block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP);

this one goes to scsi_disk_emulate_write_same on automatic patch apply.

Quoted 17 lines
hw/scsi/scsi-disk.c +2 -0
          scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED));
          return;
      }
@@ -1743,10 +1751,12 @@ static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf)
      return;
 
  invalid_param_len:
+    block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP);
      scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN));
      return;
 
  invalid_field:
+    block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP);
      scsi_check_condition(r, SENSE_CODE(INVALID_FIELD));
  }
 
Signature · 2 lines
Best regards,
Vladimir
#14 lore raw

Re: [Qemu-devel] [PATCH v5 7/9] scsi: account unmap operations

Quoted 8 lines · Vladimir Sementsov-Ogievskiy
Quoted 1 line · Anton Nefedov

but be careful: on git am, the chunk about read-only case goes into scsi_disk_emulate_write_same instead of scsi_disk_emulate_unmap (at least for me, with latest master branch and git 2.18

It does. That's weird :/
Thanks!

#15 lore raw

PATCH v5 8/9 file-posix: account discard operations

In reply to Anton Nefedov

This will help to identify how many of the user-issued discard operations (accounted on a device level) have actually suceeded down on the host file (even though the numbers will not be exactly the same if non-raw format driver is used (e.g. qcow2 sending metadata discards)).

Note that these numbers will not include discards triggered by write-zeroes + MAY_UNMAP calls.

---
 block/file-posix.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
block/file-posix.c +25 -3
index 2da3a76355..1a7126046c 100644
@@ -163,6 +163,11 @@ typedef struct BDRVRawState {
     bool has_fallocate;
     bool needs_alignment;
     bool check_cache_dropped;
+    struct {
+        int64_t discard_nb_ok;
+        int64_t discard_nb_failed;
+        int64_t discard_bytes_ok;
+    } stats;
 
     PRManager *pr_mgr;
 } BDRVRawState;
@@ -2582,12 +2587,25 @@ static void coroutine_fn raw_co_invalidate_cache(BlockDriverState *bs,
 #endif /* !__linux__ */
 }
 
+static void raw_account_discard(BDRVRawState *s, uint64_t nbytes, int ret)
+{
+    if (ret) {
+        s->stats.discard_nb_failed++;
+    } else {
+        s->stats.discard_nb_ok++;
+        s->stats.discard_bytes_ok += nbytes;
+    }
+}
+
 static coroutine_fn int
 raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
 {
     BDRVRawState *s = bs->opaque;
+    int ret;
 
-    return paio_submit_co(bs, s->fd, offset, NULL, bytes, QEMU_AIO_DISCARD);
+    ret = paio_submit_co(bs, s->fd, offset, NULL, bytes, QEMU_AIO_DISCARD);
+    raw_account_discard(s, bytes, ret);
+    return ret;
 }
 
 static int coroutine_fn raw_co_pwrite_zeroes(
@@ -3084,10 +3102,14 @@ hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
 
     ret = fd_open(bs);
     if (ret < 0) {
+        raw_account_discard(s, bytes, ret);
         return ret;
     }
-    return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                          QEMU_AIO_DISCARD | QEMU_AIO_BLKDEV);
+
+    ret = paio_submit_co(bs, s->fd, offset, NULL, bytes,
+                         QEMU_AIO_DISCARD | QEMU_AIO_BLKDEV);
+    raw_account_discard(s, bytes, ret);
+    return ret;
 }
 
 static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs,
Signature · 1 line
2.17.1
Reviewed-by on v5 8/9
#16 lore raw

Re: [Qemu-devel] [PATCH v5 8/9] file-posix: account discard operations

Quoted 9 lines · Anton Nefedov

This will help to identify how many of the user-issued discard operations (accounted on a device level) have actually suceeded down on the host file (even though the numbers will not be exactly the same if non-raw format driver is used (e.g. qcow2 sending metadata discards)).

Note that these numbers will not include discards triggered by write-zeroes + MAY_UNMAP calls.

Signature · 2 lines
Best regards,
Vladimir
#17 lore raw

PATCH v5 9/9 qapi: query-blockstat: add driver specific file-posix stats

In reply to Anton Nefedov

A block driver can provide a callback to report driver-specific statistics.

file-posix driver now reports discard statistics

---
 qapi/block-core.json      | 39 +++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |  1 +
 include/block/block_int.h |  1 +
 block.c                   |  9 +++++++++
 block/file-posix.c        | 17 +++++++++++++++++
 block/qapi.c              |  5 +++++
 6 files changed, 72 insertions(+)
qapi/block-core.json +39 -0
index 01da84cb61..cd0344435e 100644
@@ -877,6 +877,42 @@
            '*x_wr_latency_histogram': 'BlockLatencyHistogramInfo',
            '*x_flush_latency_histogram': 'BlockLatencyHistogramInfo' } }
 
+##
+# @BlockStatsSpecificFile:
+#
+# File driver statistics
+#
+# @discard-nb-ok: The number of succeeded discard operations performed by
+#                 the driver.
+#
+# @discard-nb-failed: The number of failed discard operations performed by
+#                     the driver.
+#
+# @discard-bytes-ok: The number of bytes discarded by the driver.
+#
+# Since 3.1
+##
+{ 'struct': 'BlockStatsSpecificFile',
+  'data': {
+      'discard-nb-ok': 'int',
+      'discard-nb-failed': 'int',
+      'discard-bytes-ok': 'int'
+  } }
+
+##
+# @BlockStatsSpecific:
+#
+# Block driver specific statistics
+#
+# Since: 3.1
+##
+{ 'union': 'BlockStatsSpecific',
+  'base': { 'driver': 'BlockdevDriver' },
+  'discriminator': 'driver',
+  'data': {
+      'file': 'BlockStatsSpecificFile'
+  } }
+
 ##
 # @BlockStats:
 #
@@ -892,6 +928,8 @@
 #
 # @stats:  A @BlockDeviceStats for the device.
 #
+# @driver-specific: Optional driver-specific stats. (Since 3.1)
+#
 # @parent: This describes the file block device if it has one.
 #          Contains recursively the statistics of the underlying
 #          protocol (e.g. the host file for a qcow2 image). If there is
@@ -905,6 +943,7 @@
 { 'struct': 'BlockStats',
   'data': {'*device': 'str', '*qdev': 'str', '*node-name': 'str',
            'stats': 'BlockDeviceStats',
+           '*driver-specific': 'BlockStatsSpecific',
            '*parent': 'BlockStats',
            '*backing': 'BlockStats'} }
 
include/block/block.h +1 -0
index b189cf422e..07a3b31386 100644
@@ -475,6 +475,7 @@ const char *bdrv_get_device_or_node_name(const BlockDriverState *bs);
 int bdrv_get_flags(BlockDriverState *bs);
 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs);
+BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs);
 void bdrv_round_to_clusters(BlockDriverState *bs,
                             int64_t offset, int64_t bytes,
                             int64_t *cluster_offset,
include/block/block_int.h +1 -0
index f605622216..236d4aceef 100644
@@ -320,6 +320,7 @@ struct BlockDriver {
                                   Error **errp);
     int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
     ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
+    BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
 
     int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs,
                                           QEMUIOVector *qiov,
block.c +9 -0
index 95d8635aa1..1e5bba4ac6 100644
@@ -4244,6 +4244,15 @@ ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
     return NULL;
 }
 
+BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (!drv || !drv->bdrv_get_specific_stats) {
+        return NULL;
+    }
+    return drv->bdrv_get_specific_stats(bs);
+}
+
 void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
 {
     if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
block/file-posix.c +17 -0
index 1a7126046c..a0a06585ab 100644
@@ -2630,6 +2630,21 @@ static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
     return 0;
 }
 
+static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)
+{
+    BDRVRawState *s = bs->opaque;
+    BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
+
+    stats->driver = BLOCKDEV_DRIVER_FILE;
+    stats->u.file = (BlockStatsSpecificFile){
+        .discard_nb_ok = s->stats.discard_nb_ok,
+        .discard_nb_failed = s->stats.discard_nb_failed,
+        .discard_bytes_ok = s->stats.discard_bytes_ok,
+    };
+
+    return stats;
+}
+
 static QemuOptsList raw_create_opts = {
     .name = "raw-create-opts",
     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
@@ -2741,6 +2756,7 @@ BlockDriver bdrv_file = {
     .bdrv_get_info = raw_get_info,
     .bdrv_get_allocated_file_size
                         = raw_get_allocated_file_size,
+    .bdrv_get_specific_stats = raw_get_specific_stats,
     .bdrv_check_perm = raw_check_perm,
     .bdrv_set_perm   = raw_set_perm,
     .bdrv_abort_perm_update = raw_abort_perm_update,
@@ -3226,6 +3242,7 @@ static BlockDriver bdrv_host_device = {
     .bdrv_get_info = raw_get_info,
     .bdrv_get_allocated_file_size
                         = raw_get_allocated_file_size,
+    .bdrv_get_specific_stats = raw_get_specific_stats,
     .bdrv_check_perm = raw_check_perm,
     .bdrv_set_perm   = raw_set_perm,
     .bdrv_abort_perm_update = raw_abort_perm_update,
block/qapi.c +5 -0
index df31f351d2..74f762ea6c 100644
@@ -535,6 +535,11 @@ static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
 
     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
 
+    s->driver_specific = bdrv_get_specific_stats(bs);
+    if (s->driver_specific) {
+        s->has_driver_specific = true;
+    }
+
     if (bs->file) {
         s->has_parent = true;
         s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
Signature · 1 line
2.17.1
#18 lore raw

Re: [Qemu-devel] [PATCH v5 9/9] qapi: query-blockstat: add driver specific file-posix stats

Quoted 27 lines · Anton Nefedov

A block driver can provide a callback to report driver-specific statistics.

file-posix driver now reports discard statistics

---
  qapi/block-core.json      | 39 +++++++++++++++++++++++++++++++++++++++
  include/block/block.h     |  1 +
  include/block/block_int.h |  1 +
  block.c                   |  9 +++++++++
  block/file-posix.c        | 17 +++++++++++++++++
  block/qapi.c              |  5 +++++
  6 files changed, 72 insertions(+)
qapi/block-core.json +4 -0
index 01da84cb61..cd0344435e 100644
@@ -877,6 +877,42 @@
             '*x_wr_latency_histogram': 'BlockLatencyHistogramInfo',
             '*x_flush_latency_histogram': 'BlockLatencyHistogramInfo' } }
 
+##
+# @BlockStatsSpecificFile:
+#
+# File driver statistics

qapi/block-core.json line 893

Quoted 10 lines
qapi/block-core.json +10 -0
+#
+# @discard-nb-ok: The number of succeeded discard operations performed by
+#                 the driver.
+#
+# @discard-nb-failed: The number of failed discard operations performed by
+#                     the driver.
+#
+# @discard-bytes-ok: The number of bytes discarded by the driver.
+#
+# Since 3.1

colon after Since:
Since: 3.1

qapi/block-core.json line 900

Quoted 7 lines
qapi/block-core.json +7 -0
+##
+{ 'struct': 'BlockStatsSpecificFile',
+  'data': {
+      'discard-nb-ok': 'int',
+      'discard-nb-failed': 'int',
+      'discard-bytes-ok': 'int'
+  } }

the common stile here is no extra \n around braces, like:

{ 'struct': 'BlockStatsSpecificFile',
'data': { 'discard-nb-ok': 'int',

             'discard-nb-failed': 'int',
             'discard-bytes-ok': 'int' } }
Quoted 4 lines
qapi/block-core.json +4 -0
+
+##
+# @BlockStatsSpecific:
+#

qapi/block-core.json line 914

Quoted 10 lines
qapi/block-core.json +10 -0
+# Block driver specific statistics
+#
+# Since: 3.1
+##
+{ 'union': 'BlockStatsSpecific',
+  'base': { 'driver': 'BlockdevDriver' },
+  'discriminator': 'driver',
+  'data': {
+      'file': 'BlockStatsSpecificFile'
+  } }

and here.

Quoted 14 lines
qapi/block-core.json +3 -0
+
  ##
  # @BlockStats:
  #
@@ -892,6 +928,8 @@
  #
  # @stats:  A @BlockDeviceStats for the device.
  #
+# @driver-specific: Optional driver-specific stats. (Since 3.1)
+#
  # @parent: This describes the file block device if it has one.
  #          Contains recursively the statistics of the underlying
  #          protocol (e.g. the host file for a qcow2 image). If there is

qapi/block-core.json line 946

Quoted 4 lines
qapi/block-core.json +1 -0
@@ -905,6 +943,7 @@
  { 'struct': 'BlockStats',
    'data': {'*device': 'str', '*qdev': 'str', '*node-name': 'str',
             'stats': 'BlockDeviceStats',
+           '*driver-specific': 'BlockStatsSpecific',

[offtopic]
hmm, do anyone know, why thunderbird when quoting patches adds "> " between all lines except ones starting with a space, and for lines, starting with space, it adds "> " (two extra spaces, not one), which leads to wrong indenting in quotes? And how to fix that? [/offtopic]

Quoted 52 lines
qapi/block-core.json +0 -0
             '*parent': 'BlockStats',
             '*backing': 'BlockStats'} }
 
include/block/block.h +1 -0
index b189cf422e..07a3b31386 100644
@@ -475,6 +475,7 @@ const char *bdrv_get_device_or_node_name(const BlockDriverState *bs);
  int bdrv_get_flags(BlockDriverState *bs);
  int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
  ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs);
+BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs);
  void bdrv_round_to_clusters(BlockDriverState *bs,
                              int64_t offset, int64_t bytes,
                              int64_t *cluster_offset,
include/block/block_int.h +1 -0
index f605622216..236d4aceef 100644
@@ -320,6 +320,7 @@ struct BlockDriver {
                                    Error **errp);
      int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
      ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
+    BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
 
      int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs,
                                            QEMUIOVector *qiov,
block.c +9 -0
index 95d8635aa1..1e5bba4ac6 100644
@@ -4244,6 +4244,15 @@ ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
      return NULL;
  }
 
+BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (!drv || !drv->bdrv_get_specific_stats) {
+        return NULL;
+    }
+    return drv->bdrv_get_specific_stats(bs);
+}
+
  void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
  {
      if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
block/file-posix.c +0 -0
index 1a7126046c..a0a06585ab 100644

block/file-posix.c static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) · line 2638

Quoted 9 lines
block/file-posix.c +6 -0
@@ -2630,6 +2630,21 @@ static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
      return 0;
  }
 
+static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)
+{
+    BDRVRawState *s = bs->opaque;
+    BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
+
+    stats->driver = BLOCKDEV_DRIVER_FILE;

it should be BLOCKDEV_DRIVER_HOST_DEVICE for bdrv_host_device driver I think.

block/file-posix.c static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) · line 2639

Quoted 1 line
block/file-posix.c +1 -0
+    stats->u.file = (BlockStatsSpecificFile){

and than, here we'll have "different" union field - host_device. (and for code-style, whitespace before brace)

Quoted 44 lines
block/file-posix.c +10 -0
+        .discard_nb_ok = s->stats.discard_nb_ok,
+        .discard_nb_failed = s->stats.discard_nb_failed,
+        .discard_bytes_ok = s->stats.discard_bytes_ok,
+    };
+
+    return stats;
+}
+
  static QemuOptsList raw_create_opts = {
      .name = "raw-create-opts",
      .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
@@ -2741,6 +2756,7 @@ BlockDriver bdrv_file = {
      .bdrv_get_info = raw_get_info,
      .bdrv_get_allocated_file_size
                          = raw_get_allocated_file_size,
+    .bdrv_get_specific_stats = raw_get_specific_stats,
      .bdrv_check_perm = raw_check_perm,
      .bdrv_set_perm   = raw_set_perm,
      .bdrv_abort_perm_update = raw_abort_perm_update,
@@ -3226,6 +3242,7 @@ static BlockDriver bdrv_host_device = {
      .bdrv_get_info = raw_get_info,
      .bdrv_get_allocated_file_size
                          = raw_get_allocated_file_size,
+    .bdrv_get_specific_stats = raw_get_specific_stats,
      .bdrv_check_perm = raw_check_perm,
      .bdrv_set_perm   = raw_set_perm,
      .bdrv_abort_perm_update = raw_abort_perm_update,
block/qapi.c +5 -0
index df31f351d2..74f762ea6c 100644
@@ -535,6 +535,11 @@ static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
 
      s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
 
+    s->driver_specific = bdrv_get_specific_stats(bs);
+    if (s->driver_specific) {
+        s->has_driver_specific = true;
+    }
+
      if (bs->file) {
          s->has_parent = true;
          s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
Signature · 2 lines
Best regards,
Vladimir
#19 lore raw

Re: [Qemu-devel] [PATCH v5 9/9] qapi: query-blockstat: add driver specific file-posix stats

Quoted 33 lines · Vladimir Sementsov-Ogievskiy
Quoted 32 lines · Anton Nefedov

A block driver can provide a callback to report driver-specific statistics.

file-posix driver now reports discard statistics

---
   qapi/block-core.json      | 39 +++++++++++++++++++++++++++++++++++++++
   include/block/block.h     |  1 +
   include/block/block_int.h |  1 +
   block.c                   |  9 +++++++++
   block/file-posix.c        | 17 +++++++++++++++++
   block/qapi.c              |  5 +++++
   6 files changed, 72 insertions(+)
qapi/block-core.json +9 -0
index 01da84cb61..cd0344435e 100644
@@ -877,6 +877,42 @@
              '*x_wr_latency_histogram': 'BlockLatencyHistogramInfo',
              '*x_flush_latency_histogram': 'BlockLatencyHistogramInfo' } }
 
+##
+# @BlockStatsSpecificFile:
+#
+# File driver statistics
+#
+# @discard-nb-ok: The number of succeeded discard operations performed by
+#                 the driver.
+#
+# @discard-nb-failed: The number of failed discard operations performed by
Quoted 9 lines

qapi/block-core.json line 893

Quoted 5 lines
qapi/block-core.json +5 -0
+#                     the driver.
+#
+# @discard-bytes-ok: The number of bytes discarded by the driver.
+#
+# Since 3.1

colon after Since:
Since: 3.1

done

Quoted 10 lines

qapi/block-core.json line 900

Quoted 7 lines
qapi/block-core.json +7 -0
+##
+{ 'struct': 'BlockStatsSpecificFile',
+  'data': {
+      'discard-nb-ok': 'int',
+      'discard-nb-failed': 'int',
+      'discard-bytes-ok': 'int'
+  } }

the common stile here is no extra \n around braces, like:

Quoted 6 lines

{ 'struct': 'BlockStatsSpecificFile',

    'data': { 'discard-nb-ok': 'int',
              'discard-nb-failed': 'int',
              'discard-bytes-ok': 'int' } }

Ok this one is more frequent. Fixed.

Quoted 8 lines
Quoted 8 lines
qapi/block-core.json +8 -0
+
+##
+# @BlockStatsSpecific:
+#
+# Block driver specific statistics
+#
+# Since: 3.1
+##
Quoted 9 lines

qapi/block-core.json line 914

Quoted 6 lines
qapi/block-core.json +6 -0
+{ 'union': 'BlockStatsSpecific',
+  'base': { 'driver': 'BlockdevDriver' },
+  'discriminator': 'driver',
+  'data': {
+      'file': 'BlockStatsSpecificFile'
+  } }

and here.

done

Quoted 78 lines
Quoted 14 lines
qapi/block-core.json +3 -0
+
   ##
   # @BlockStats:
   #
@@ -892,6 +928,8 @@
   #
   # @stats:  A @BlockDeviceStats for the device.
   #
+# @driver-specific: Optional driver-specific stats. (Since 3.1)
+#
   # @parent: This describes the file block device if it has one.
   #          Contains recursively the statistics of the underlying
   #          protocol (e.g. the host file for a qcow2 image). If there is

qapi/block-core.json line 946

Quoted 4 lines
qapi/block-core.json +1 -0
@@ -905,6 +943,7 @@
   { 'struct': 'BlockStats',
     'data': {'*device': 'str', '*qdev': 'str', '*node-name': 'str',
              'stats': 'BlockDeviceStats',
+           '*driver-specific': 'BlockStatsSpecific',

[offtopic]
hmm, do anyone know, why thunderbird when quoting patches adds "> " between all lines except ones starting with a space, and for lines, starting with space, it adds "> " (two extra spaces, not one), which leads to wrong indenting in quotes? And how to fix that? [/offtopic]

Quoted 55 lines
qapi/block-core.json +0 -0
              '*parent': 'BlockStats',
              '*backing': 'BlockStats'} }
 
include/block/block.h +1 -0
index b189cf422e..07a3b31386 100644
@@ -475,6 +475,7 @@ const char *bdrv_get_device_or_node_name(const BlockDriverState *bs);
   int bdrv_get_flags(BlockDriverState *bs);
   int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
   ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs);
+BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs);
   void bdrv_round_to_clusters(BlockDriverState *bs,
                               int64_t offset, int64_t bytes,
                               int64_t *cluster_offset,
include/block/block_int.h +1 -0
index f605622216..236d4aceef 100644
@@ -320,6 +320,7 @@ struct BlockDriver {
                                     Error **errp);
       int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
       ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
+    BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
 
       int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs,
                                             QEMUIOVector *qiov,
block.c +9 -0
index 95d8635aa1..1e5bba4ac6 100644
@@ -4244,6 +4244,15 @@ ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
       return NULL;
   }
 
+BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (!drv || !drv->bdrv_get_specific_stats) {
+        return NULL;
+    }
+    return drv->bdrv_get_specific_stats(bs);
+}
+
   void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
   {
       if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
block/file-posix.c +0 -0
index 1a7126046c..a0a06585ab 100644
@@ -2630,6 +2630,21 @@ static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
       return 0;
   }
 
Quoted 9 lines

block/file-posix.c static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) · line 2638

Quoted 6 lines
block/file-posix.c +6 -0
+static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)
+{
+    BDRVRawState *s = bs->opaque;
+    BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
+
+    stats->driver = BLOCKDEV_DRIVER_FILE;

it should be BLOCKDEV_DRIVER_HOST_DEVICE for bdrv_host_device driver I think.

I guess so. Fixed

Quoted 4 lines

block/file-posix.c static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) · line 2639

Quoted 1 line
block/file-posix.c +1 -0
+    stats->u.file = (BlockStatsSpecificFile){

and than, here we'll have "different" union field - host_device. (and for code-style, whitespace before brace)

done

Quoted 46 lines
Quoted 44 lines
block/file-posix.c +10 -0
+        .discard_nb_ok = s->stats.discard_nb_ok,
+        .discard_nb_failed = s->stats.discard_nb_failed,
+        .discard_bytes_ok = s->stats.discard_bytes_ok,
+    };
+
+    return stats;
+}
+
   static QemuOptsList raw_create_opts = {
       .name = "raw-create-opts",
       .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
@@ -2741,6 +2756,7 @@ BlockDriver bdrv_file = {
       .bdrv_get_info = raw_get_info,
       .bdrv_get_allocated_file_size
                           = raw_get_allocated_file_size,
+    .bdrv_get_specific_stats = raw_get_specific_stats,
       .bdrv_check_perm = raw_check_perm,
       .bdrv_set_perm   = raw_set_perm,
       .bdrv_abort_perm_update = raw_abort_perm_update,
@@ -3226,6 +3242,7 @@ static BlockDriver bdrv_host_device = {
       .bdrv_get_info = raw_get_info,
       .bdrv_get_allocated_file_size
                           = raw_get_allocated_file_size,
+    .bdrv_get_specific_stats = raw_get_specific_stats,
       .bdrv_check_perm = raw_check_perm,
       .bdrv_set_perm   = raw_set_perm,
       .bdrv_abort_perm_update = raw_abort_perm_update,
block/qapi.c +5 -0
index df31f351d2..74f762ea6c 100644
@@ -535,6 +535,11 @@ static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
 
       s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
 
+    s->driver_specific = bdrv_get_specific_stats(bs);
+    if (s->driver_specific) {
+        s->has_driver_specific = true;
+    }
+
       if (bs->file) {
           s->has_parent = true;
           s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);