Skip to contentSkip to conversation

Add block size histogram qapi interface

PATCH Superseded zhenwei pi 1 participant 4 messages Jun 24, 2019 Block layer

lore
Share & tools
lore

zhenwei pi

author
#0 lore raw

v2 cover Add block size histogram qapi interface

Modify command 'block-latency-histogram-set' to make block histogram interface common to use. And support block size histogram. Thanks to Eric Blake&Vladimir Sementsov-Ogievskiy for the suggestions.

This command has been tested for half year on QEMU-2.12, and we found that 3K+ virtual machines write 25GB/s totally, the block size histogram like following:

        0 ~ 8k: 58% ~ 62%
        8k ~ 32k: 10% ~ 12%
        32k ~ 128k: 2% ~ 3%
        128K ~ 512K: 24% ~ 26%
        512K ~ : ...

And the histogram data help us to optimise backend distributed storage.

zhenwei pi (3):
block/accounting: rename struct BlockLatencyHistogram block/accounting: introduce block size histogram
qapi: make block histogram interface common

 block/accounting.c         |  62 ++++++++++++++++++++--------
 block/qapi.c               |  32 ++++++++------
 blockdev.c                 |  33 +++++++++++----
 include/block/accounting.h |  13 +++---
 qapi/block-core.json       | 101 +++++++++++++++++++++++++++------------------
 5 files changed, 158 insertions(+), 83 deletions(-)
Signature · 1 line
2.11.0
author
#1 lore raw

PATCH v2 1/3 block/accounting: rename struct BlockLatencyHistogram

Rename struct BlockLatencyHistogram to BlockHistogram, and rename related functions.
Make this struct and functions be common, they can be used widely.

---
 block/accounting.c         | 44 +++++++++++++++++++++++++++-----------------
 block/qapi.c               | 23 +++++++++++------------
 include/block/accounting.h |  8 ++++----
 3 files changed, 42 insertions(+), 33 deletions(-)
block/accounting.c +27 -17
index 70a3d9a426..d210a73fe9 100644
@@ -94,13 +94,14 @@ void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
     cookie->type = type;
 }
 
-/* block_latency_histogram_compare_func:
+/**
+ * block_histogram_compare_func:
  * Compare @key with interval [@it[0], @it[1]).
  * Return: -1 if @key < @it[0]
  *          0 if @key in [@it[0], @it[1])
  *         +1 if @key >= @it[1]
  */
-static int block_latency_histogram_compare_func(const void *key, const void *it)
+static int block_histogram_compare_func(const void *key, const void *it)
 {
     uint64_t k = *(uint64_t *)key;
     uint64_t a = ((uint64_t *)it)[0];
@@ -109,8 +110,7 @@ static int block_latency_histogram_compare_func(const void *key, const void *it)
     return k < a ? -1 : (k < b ? 0 : 1);
 }
 
-static void block_latency_histogram_account(BlockLatencyHistogram *hist,
-                                            int64_t latency_ns)
+static void block_histogram_account(BlockHistogram *hist, int64_t val)
 {
     uint64_t *pos;
 
@@ -120,28 +120,26 @@ static void block_latency_histogram_account(BlockLatencyHistogram *hist,
     }
 
 
-    if (latency_ns < hist->boundaries[0]) {
+    if (val < hist->boundaries[0]) {
         hist->bins[0]++;
         return;
     }
 
-    if (latency_ns >= hist->boundaries[hist->nbins - 2]) {
+    if (val >= hist->boundaries[hist->nbins - 2]) {
         hist->bins[hist->nbins - 1]++;
         return;
     }
 
-    pos = bsearch(&latency_ns, hist->boundaries, hist->nbins - 2,
+    pos = bsearch(&val, hist->boundaries, hist->nbins - 2,
                   sizeof(hist->boundaries[0]),
-                  block_latency_histogram_compare_func);
+                  block_histogram_compare_func);
     assert(pos != NULL);
 
     hist->bins[pos - hist->boundaries + 1]++;
 }
 
-int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
-                                uint64List *boundaries)
+static int block_histogram_set(BlockHistogram *hist, uint64List *boundaries)
 {
-    BlockLatencyHistogram *hist = &stats->latency_histogram[type];
     uint64List *entry;
     uint64_t *ptr;
     uint64_t prev = 0;
@@ -170,15 +168,27 @@ int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
     return 0;
 }
 
+static void block_histogram_clear(BlockHistogram *hist)
+{
+    g_free(hist->bins);
+    g_free(hist->boundaries);
+    memset(hist, 0, sizeof(*hist));
+}
+
+int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
+                                uint64List *boundaries)
+{
+    BlockHistogram *hist = &stats->latency_histogram[type];
+
+    return block_histogram_set(hist, boundaries);
+}
+
 void block_latency_histograms_clear(BlockAcctStats *stats)
 {
     int i;
 
     for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
-        BlockLatencyHistogram *hist = &stats->latency_histogram[i];
-        g_free(hist->bins);
-        g_free(hist->boundaries);
-        memset(hist, 0, sizeof(*hist));
+        block_histogram_clear(&stats->latency_histogram[i]);
     }
 }
 
@@ -204,8 +214,8 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
         stats->nr_ops[cookie->type]++;
     }
 
-    block_latency_histogram_account(&stats->latency_histogram[cookie->type],
-                                    latency_ns);
+    block_histogram_account(&stats->latency_histogram[cookie->type],
+                            latency_ns);
 
     if (!failed || stats->account_failed) {
         stats->total_time_ns[cookie->type] += latency_ns;
block/qapi.c +11 -12
index 917435f022..9d9b2f1927 100644
@@ -415,9 +415,8 @@ static uint64List *uint64_list(uint64_t *list, int size)
     return out_list;
 }
 
-static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
-                                         bool *not_null,
-                                         BlockLatencyHistogramInfo **info)
+static void bdrv_histogram_stats(BlockHistogram *hist, bool *not_null,
+                                 BlockLatencyHistogramInfo **info)
 {
     *not_null = hist->bins != NULL;
     if (*not_null) {
@@ -494,15 +493,15 @@ static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
     }
 
-    bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
-                                 &ds->has_rd_latency_histogram,
-                                 &ds->rd_latency_histogram);
-    bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
-                                 &ds->has_wr_latency_histogram,
-                                 &ds->wr_latency_histogram);
-    bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
-                                 &ds->has_flush_latency_histogram,
-                                 &ds->flush_latency_histogram);
+    bdrv_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
+                         &ds->has_rd_latency_histogram,
+                         &ds->rd_latency_histogram);
+    bdrv_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
+                         &ds->has_wr_latency_histogram,
+                         &ds->wr_latency_histogram);
+    bdrv_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
+                         &ds->has_flush_latency_histogram,
+                         &ds->flush_latency_histogram);
 }
 
 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
include/block/accounting.h +4 -4
index d1f67b10dd..270fddb69c 100644
@@ -46,7 +46,7 @@ struct BlockAcctTimedStats {
     QSLIST_ENTRY(BlockAcctTimedStats) entries;
 };
 
-typedef struct BlockLatencyHistogram {
+typedef struct BlockHistogram {
     /* The following histogram is represented like this:
      *
      * 5|           *
@@ -57,7 +57,7 @@ typedef struct BlockLatencyHistogram {
      *  +------------------
      *      10   50   100
      *
-     * BlockLatencyHistogram histogram = {
+     * BlockHistogram histogram = {
      *     .nbins = 4,
      *     .boundaries = {10, 50, 100},
      *     .bins = {3, 1, 5, 2},
@@ -74,7 +74,7 @@ typedef struct BlockLatencyHistogram {
     uint64_t *boundaries; /* @nbins-1 numbers here
                              (all boundaries, except 0 and +inf) */
     uint64_t *bins;
-} BlockLatencyHistogram;
+} BlockHistogram;
 
 struct BlockAcctStats {
     QemuMutex lock;
@@ -88,7 +88,7 @@ struct BlockAcctStats {
     QSLIST_HEAD(, BlockAcctTimedStats) intervals;
     bool account_invalid;
     bool account_failed;
-    BlockLatencyHistogram latency_histogram[BLOCK_MAX_IOTYPE];
+    BlockHistogram latency_histogram[BLOCK_MAX_IOTYPE];
 };
 
 typedef struct BlockAcctCookie {
Signature · 1 line
2.11.0
author
#2 lore raw

PATCH v2 2/3 block/accounting: introduce block size histogram

In reply to zhenwei pi

Introduce block size histogram statics for block devices.

For read/write/flush operation type, the block size region [0, +inf) is divided into subregions by several points. It works like block latency histogram.

---
 block/accounting.c         | 18 ++++++++++++++++++
 include/block/accounting.h |  5 ++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
block/accounting.c +18 -0
index d210a73fe9..3a1e41a971 100644
@@ -192,6 +192,22 @@ void block_latency_histograms_clear(BlockAcctStats *stats)
     }
 }
 
+int block_size_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
+                             uint64List *boundaries)
+{
+    BlockHistogram *hist = &stats->size_histogram[type];
+
+    return block_histogram_set(hist, boundaries);
+}
+
+void block_size_histograms_clear(BlockAcctStats *stats)
+{
+    int i;
+
+    for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
+        block_histogram_clear(&stats->size_histogram[i]);
+    }
+}
 static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
                                  bool failed)
 {
@@ -216,6 +232,8 @@ static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
 
     block_histogram_account(&stats->latency_histogram[cookie->type],
                             latency_ns);
+    block_histogram_account(&stats->size_histogram[cookie->type],
+                            cookie->bytes);
 
     if (!failed || stats->account_failed) {
         stats->total_time_ns[cookie->type] += latency_ns;
include/block/accounting.h +4 -1
index 270fddb69c..0fbba64408 100644
@@ -89,6 +89,7 @@ struct BlockAcctStats {
     bool account_invalid;
     bool account_failed;
     BlockHistogram latency_histogram[BLOCK_MAX_IOTYPE];
+    BlockHistogram size_histogram[BLOCK_MAX_IOTYPE];
 };
 
 typedef struct BlockAcctCookie {
@@ -117,5 +118,7 @@ double block_acct_queue_depth(BlockAcctTimedStats *stats,
 int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
                                 uint64List *boundaries);
 void block_latency_histograms_clear(BlockAcctStats *stats);
-
+int block_size_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
+                             uint64List *boundaries);
+void block_size_histograms_clear(BlockAcctStats *stats);
 #endif
Signature · 1 line
2.11.0
author
#3 lore raw

PATCH v2 3/3 qapi: make block histogram interface common

In reply to zhenwei pi

Modify command 'block-latency-histogram-set' to 'block-histogram-set' and modify struct 'BlockLatencyHistogramInfo' to struct 'BlockHistogramInfo', this makes block histogram interface common to use.

Currently 'BlockHistogramType' supports 'latency', it works as same as the old command 'block-latency-histogram-set'. New enum 'size' allows QEMU to fill histogram by block I/O size in byte.

---
 block/qapi.c         |  11 +++++-
 blockdev.c           |  33 +++++++++++++----
 qapi/block-core.json | 101 +++++++++++++++++++++++++++++++--------------------
 3 files changed, 95 insertions(+), 50 deletions(-)
block/qapi.c +9 -2
index 9d9b2f1927..c778b2c376 100644
@@ -416,11 +416,11 @@ static uint64List *uint64_list(uint64_t *list, int size)
 }
 
 static void bdrv_histogram_stats(BlockHistogram *hist, bool *not_null,
-                                 BlockLatencyHistogramInfo **info)
+                                 BlockHistogramInfo **info)
 {
     *not_null = hist->bins != NULL;
     if (*not_null) {
-        *info = g_new0(BlockLatencyHistogramInfo, 1);
+        *info = g_new0(BlockHistogramInfo, 1);
 
         (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
         (*info)->bins = uint64_list(hist->bins, hist->nbins);
@@ -502,6 +502,13 @@ static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
     bdrv_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
                          &ds->has_flush_latency_histogram,
                          &ds->flush_latency_histogram);
+    bdrv_histogram_stats(&stats->size_histogram[BLOCK_ACCT_READ],
+                         &ds->has_rd_size_histogram, &ds->rd_size_histogram);
+    bdrv_histogram_stats(&stats->size_histogram[BLOCK_ACCT_WRITE],
+                         &ds->has_wr_size_histogram, &ds->wr_size_histogram);
+    bdrv_histogram_stats(&stats->size_histogram[BLOCK_ACCT_FLUSH],
+                         &ds->has_flush_size_histogram,
+                         &ds->flush_size_histogram);
 }
 
 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
blockdev.c +25 -8
index 5d6a13dea9..82240ae8c5 100644
@@ -4507,8 +4507,9 @@ void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
     aio_context_release(old_context);
 }
 
-void qmp_block_latency_histogram_set(
+void qmp_block_histogram_set(
     const char *id,
+    BlockHistogramType type,
     bool has_boundaries, uint64List *boundaries,
     bool has_boundaries_read, uint64List *boundaries_read,
     bool has_boundaries_write, uint64List *boundaries_write,
@@ -4517,24 +4518,42 @@ void qmp_block_latency_histogram_set(
 {
     BlockBackend *blk = qmp_get_blk(NULL, id, errp);
     BlockAcctStats *stats;
+    void (*clear_func)(BlockAcctStats *stats);
+    int (*set_func)(BlockAcctStats *stats, enum BlockAcctType type,
+                    uint64List *boundaries);
     int ret;
 
     if (!blk) {
         return;
     }
 
+    switch (type) {
+    case BLOCK_HISTOGRAM_TYPE_LATENCY: {
+        clear_func = block_latency_histograms_clear;
+        set_func = block_latency_histogram_set;
+        break;
+    }
+    case BLOCK_HISTOGRAM_TYPE_SIZE: {
+        clear_func = block_size_histograms_clear;
+        set_func = block_size_histogram_set;
+        break;
+    }
+    default:
+        error_setg(errp, "Unsupported block histogram type");
+        return;
+    }
+
     stats = blk_get_stats(blk);
 
     if (!has_boundaries && !has_boundaries_read && !has_boundaries_write &&
         !has_boundaries_flush)
     {
-        block_latency_histograms_clear(stats);
+        clear_func(stats);
         return;
     }
 
     if (has_boundaries || has_boundaries_read) {
-        ret = block_latency_histogram_set(
-            stats, BLOCK_ACCT_READ,
+        ret = set_func(stats, BLOCK_ACCT_READ,
             has_boundaries_read ? boundaries_read : boundaries);
         if (ret) {
             error_setg(errp, "Device '%s' set read boundaries fail", id);
@@ -4543,8 +4562,7 @@ void qmp_block_latency_histogram_set(
     }
 
     if (has_boundaries || has_boundaries_write) {
-        ret = block_latency_histogram_set(
-            stats, BLOCK_ACCT_WRITE,
+        ret = set_func(stats, BLOCK_ACCT_WRITE,
             has_boundaries_write ? boundaries_write : boundaries);
         if (ret) {
             error_setg(errp, "Device '%s' set write boundaries fail", id);
@@ -4553,8 +4571,7 @@ void qmp_block_latency_histogram_set(
     }
 
     if (has_boundaries || has_boundaries_flush) {
-        ret = block_latency_histogram_set(
-            stats, BLOCK_ACCT_FLUSH,
+        ret = set_func(stats, BLOCK_ACCT_FLUSH,
             has_boundaries_flush ? boundaries_flush : boundaries);
         if (ret) {
             error_setg(errp, "Device '%s' set flush boundaries fail", id);
qapi/block-core.json +61 -40

170 lines not on this page · raw message

Signature · 1 line
2.11.0