Skip to contentSkip to conversation

blk-mq queue stats

PATCH New Jes Sorensen 1 participant 6 messages May 29, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover blk-mq queue stats

From: Jes Sorensen <[email protected]>

Hi,

This patchset adds per-device histogram statistics of read, write, and discard request sizes, as well as number of requests for each bucket. It uses the blk-stat infrastructure and only collects stats for devices for which it has been explicitly enabled by writing 1 to /sys/block/<dev>/queue/histstat

In addition it builds on top of two patches I recklessly stole from Josef - thanks!

Thoughts or oppinions on this approach? I am not married to the naming, so happy to change that if wanted.

v2 updated to apply against 5.2-rc2

Thanks,
Jes

Jes Sorensen (3):
Use blk-stat infrastructure to collect per queue device stats Export block dev stats to sysfs
Expand block stats to export number of of requests per bucket

Josef Bacik (2):
blk-stat: rename batch to time
block: keep track of per-device io sizes in stats

 block/blk-iolatency.c     |  2 +-
 block/blk-mq.c            | 56 ++++++++++++++++++++++++++++--
 block/blk-stat.c          | 16 +++++----
 block/blk-stat.h          |  3 +-
 block/blk-sysfs.c         | 71 +++++++++++++++++++++++++++++++++++++++
 block/blk-throttle.c      |  3 +-
 include/linux/blk_types.h |  3 +-
 include/linux/blkdev.h    | 12 +++++--
 8 files changed, 149 insertions(+), 17 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/5 blk-stat: rename batch to time

From: Josef Bacik <[email protected]>

Batch isn't at all related to what it's actually used for, which is the running sum of time spent doing IO. Rename it to time so it makes logical sense.

---
 block/blk-stat.c          | 6 +++---
 include/linux/blk_types.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
block/blk-stat.c +3 -3
index 940f15d600f8..1529f22044bd 100644
@@ -22,7 +22,7 @@ void blk_rq_stat_init(struct blk_rq_stat *stat)
 {
 	stat->min = -1ULL;
 	stat->max = stat->nr_samples = stat->mean = 0;
-	stat->batch = 0;
+	stat->time = 0;
 }
 
 /* src is a per-cpu stat, mean isn't initialized */
@@ -34,7 +34,7 @@ void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
 	dst->min = min(dst->min, src->min);
 	dst->max = max(dst->max, src->max);
 
-	dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
+	dst->mean = div_u64(src->time + dst->mean * dst->nr_samples,
 				dst->nr_samples + src->nr_samples);
 
 	dst->nr_samples += src->nr_samples;
@@ -44,7 +44,7 @@ void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
 {
 	stat->min = min(stat->min, value);
 	stat->max = max(stat->max, value);
-	stat->batch += value;
+	stat->time += value;
 	stat->nr_samples++;
 }
 
include/linux/blk_types.h +1 -1
index 95202f80676c..6406430c517a 100644
@@ -442,7 +442,7 @@ struct blk_rq_stat {
 	u64 min;
 	u64 max;
 	u32 nr_samples;
-	u64 batch;
+	u64 time;
 };
 
 #endif /* __LINUX_BLK_TYPES_H */
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/5 block: keep track of per-device io sizes in stats

In reply to Jes Sorensen

From: Josef Bacik <[email protected]>

In order to track things like bw we need to know the number of bytes done per device. Add a io_bytes field to the struct request and populate this at start time. Add a field to the blk_rq_stat to hold this information so that consumers of the blk_rq_stat stuff can use it.

---
 block/blk-iolatency.c     |  2 +-
 block/blk-mq.c            |  4 +---
 block/blk-stat.c          | 14 ++++++++------
 block/blk-stat.h          |  2 +-
 block/blk-throttle.c      |  3 ++-
 include/linux/blk_types.h |  1 +
 include/linux/blkdev.h    |  5 ++---
 7 files changed, 16 insertions(+), 15 deletions(-)
block/blk-iolatency.c +1 -1
index d22e61bced86..51cb82c07912 100644
@@ -219,7 +219,7 @@ static inline void latency_stat_record_time(struct iolatency_grp *iolat,
 			stat->ps.missed++;
 		stat->ps.total++;
 	} else
-		blk_rq_stat_add(&stat->rqs, req_time);
+		blk_rq_stat_add(&stat->rqs, req_time, 0);
 	put_cpu_ptr(stat);
 }
 
block/blk-mq.c +1 -3
index ce0f5f4ede70..8a59bc0410a6 100644
@@ -679,9 +679,7 @@ void blk_mq_start_request(struct request *rq)
 
 	if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
 		rq->io_start_time_ns = ktime_get_ns();
-#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-		rq->throtl_size = blk_rq_sectors(rq);
-#endif
+		rq->io_bytes = blk_rq_bytes(rq);
 		rq->rq_flags |= RQF_STATS;
 		rq_qos_issue(q, rq);
 	}
block/blk-stat.c +8 -6
index 1529f22044bd..0c6942f8b141 100644
@@ -22,7 +22,7 @@ void blk_rq_stat_init(struct blk_rq_stat *stat)
 {
 	stat->min = -1ULL;
 	stat->max = stat->nr_samples = stat->mean = 0;
-	stat->time = 0;
+	stat->size = stat->time = 0;
 }
 
 /* src is a per-cpu stat, mean isn't initialized */
@@ -38,13 +38,15 @@ void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
 				dst->nr_samples + src->nr_samples);
 
 	dst->nr_samples += src->nr_samples;
+	dst->size += src->size;
 }
 
-void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
+void blk_rq_stat_add(struct blk_rq_stat *stat, u64 time, u64 size)
 {
-	stat->min = min(stat->min, value);
-	stat->max = max(stat->max, value);
-	stat->time += value;
+	stat->min = min(stat->min, time);
+	stat->max = max(stat->max, time);
+	stat->time += time;
+	stat->size += size;
 	stat->nr_samples++;
 }
 
@@ -70,7 +72,7 @@ void blk_stat_add(struct request *rq, u64 now)
 			continue;
 
 		stat = &get_cpu_ptr(cb->cpu_stat)[bucket];
-		blk_rq_stat_add(stat, value);
+		blk_rq_stat_add(stat, value, rq->io_bytes);
 		put_cpu_ptr(cb->cpu_stat);
 	}
 	rcu_read_unlock();
block/blk-stat.h +1 -1
index 17b47a86eefb..ea893c4a9af1 100644
@@ -164,7 +164,7 @@ static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb,
 	mod_timer(&cb->timer, jiffies + msecs_to_jiffies(msecs));
 }
 
-void blk_rq_stat_add(struct blk_rq_stat *, u64);
+void blk_rq_stat_add(struct blk_rq_stat *, u64, u64);
 void blk_rq_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
 void blk_rq_stat_init(struct blk_rq_stat *);
 
block/blk-throttle.c +2 -1
index 1b97a73d2fb1..b07e5feda553 100644
@@ -2248,8 +2248,9 @@ void blk_throtl_stat_add(struct request *rq, u64 time_ns)
 {
 	struct request_queue *q = rq->q;
 	struct throtl_data *td = q->td;
+	sector_t size = rq->io_bytes >> SECTOR_SHIFT;
 
-	throtl_track_latency(td, rq->throtl_size, req_op(rq), time_ns >> 10);
+	throtl_track_latency(td, size, req_op(rq), time_ns >> 10);
 }
 
 void blk_throtl_bio_endio(struct bio *bio)
include/linux/blk_types.h +1 -0
index 6406430c517a..b85b27eb52b4 100644
@@ -443,6 +443,7 @@ struct blk_rq_stat {
 	u64 max;
 	u32 nr_samples;
 	u64 time;
+	u64 size;
 };
 
 #endif /* __LINUX_BLK_TYPES_H */
include/linux/blkdev.h +2 -3
index 592669bcc536..2716f239b56d 100644
@@ -198,13 +198,12 @@ struct request {
 	u64 start_time_ns;
 	/* Time that I/O was submitted to the device. */
 	u64 io_start_time_ns;
+	/* Bytes submitted to the device. */
+	unsigned int io_bytes;
 
 #ifdef CONFIG_BLK_WBT
 	unsigned short wbt_flags;
 #endif
-#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-	unsigned short throtl_size;
-#endif
 
 	/*
 	 * Number of scatter-gather DMA addr+len pairs after
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 3/5 Use blk-stat infrastructure to collect per queue device stats

In reply to Jes Sorensen

From: Jes Sorensen <[email protected]>

Put request bytes into 8 buckets of requests, for read, write, and discard.

Enable stats by writing 1 to /sys/block/<dev>/queue/histstat, disable by writing 0.

---
 block/blk-mq.c         | 52 ++++++++++++++++++++++++++++++++++++++++++
 block/blk-stat.h       |  1 +
 block/blk-sysfs.c      | 40 ++++++++++++++++++++++++++++++++
 include/linux/blkdev.h |  7 ++++++
 4 files changed, 100 insertions(+)
block/blk-mq.c +52 -0
index 8a59bc0410a6..942ef6e1ed86 100644
@@ -59,6 +59,52 @@ static int blk_mq_poll_stats_bkt(const struct request *rq)
 	return bucket;
 }
 
+/*
+ * 8 buckets for each of read, write, and discard
+ */
+static int blk_dev_stats_bkt(const struct request *rq)
+{
+	int grp, bucket;
+
+	grp = op_stat_group(req_op(rq));
+
+	bucket = grp + 3*(ilog2(rq->io_bytes) - 9);
+
+	if (bucket < 0)
+		return -1;
+	else if (bucket >= BLK_DEV_STATS_BKTS)
+		return grp + BLK_DEV_STATS_BKTS - 3;
+
+	return bucket;
+}
+
+/*
+ * Copy out the stats to their official location
+ */
+static void blk_dev_stats_cb(struct blk_stat_callback *cb)
+{
+	struct request_queue *q = cb->data;
+	int bucket;
+
+	for (bucket = 0; bucket < BLK_DEV_STATS_BKTS; bucket++) {
+		if (cb->stat[bucket].nr_samples) {
+			q->dev_stat[bucket].size +=
+				cb->stat[bucket].size;
+			q->dev_stat[bucket].nr_samples +=
+				cb->stat[bucket].nr_samples;
+		}
+	}
+
+	if (!blk_stat_is_active(cb))
+		blk_stat_activate_msecs(cb, 100);
+}
+
+void blk_dev_stats_free(struct request_queue *q)
+{
+	blk_stat_remove_callback(q, q->dev_cb);
+	blk_stat_free_callback(q->dev_cb);
+}
+
 /*
  * Check if any of the ctx, dispatch list or elevator
  * have pending work in this hardware queue.
@@ -2881,6 +2927,12 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
 	if (!q->nr_hw_queues)
 		goto err_hctxs;
 
+	q->dev_cb = blk_stat_alloc_callback(blk_dev_stats_cb,
+					    blk_dev_stats_bkt,
+					    BLK_DEV_STATS_BKTS, q);
+	if (!q->dev_cb)
+		goto err_hctxs;
+
 	INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
 	blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
 
block/blk-stat.h +1 -0
index ea893c4a9af1..7f0c8b737a9d 100644
@@ -168,4 +168,5 @@ void blk_rq_stat_add(struct blk_rq_stat *, u64, u64);
 void blk_rq_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
 void blk_rq_stat_init(struct blk_rq_stat *);
 
+void blk_dev_stats_free(struct request_queue *q);
 #endif
block/blk-sysfs.c +40 -0
index a16a02c52a85..81566432828d 100644
@@ -530,6 +530,37 @@ static ssize_t queue_dax_show(struct request_queue *q, char *page)
 	return queue_var_show(blk_queue_dax(q), page);
 }
 
+static ssize_t queue_histstat_show(struct request_queue *q, char *page)
+{
+	return queue_var_show(test_bit(QUEUE_FLAG_HISTSTATS,
+				       &q->queue_flags), page);
+}
+
+static ssize_t queue_histstat_store(struct request_queue *q, const char *page,
+				    size_t size)
+{
+	unsigned long histstat_on;
+	ssize_t ret;
+
+	ret = queue_var_store(&histstat_on, page, size);
+	if (ret < 0)
+		return ret;
+
+	if (histstat_on) {
+		if (!blk_queue_flag_test_and_set(QUEUE_FLAG_HISTSTATS, q))
+			blk_stat_add_callback(q, q->dev_cb);
+		if (!blk_stat_is_active(q->dev_cb))
+			blk_stat_activate_msecs(q->dev_cb, 100);
+	} else {
+		if (test_bit(QUEUE_FLAG_HISTSTATS, &q->queue_flags)) {
+			blk_stat_remove_callback(q, q->dev_cb);
+			blk_queue_flag_clear(QUEUE_FLAG_HISTSTATS, q);
+		}
+	}
+
+	return ret;
+}
+
 static struct queue_sysfs_entry queue_requests_entry = {
 	.attr = {.name = "nr_requests", .mode = 0644 },
 	.show = queue_requests_show,
@@ -728,6 +759,12 @@ static struct queue_sysfs_entry throtl_sample_time_entry = {
 };
 #endif
 
+static struct queue_sysfs_entry queue_histstat_entry = {
+	.attr = {.name = "histstat", .mode = 0644 },
+	.show = queue_histstat_show,
+	.store = queue_histstat_store,
+};
+
 static struct attribute *queue_attrs[] = {
 	&queue_requests_entry.attr,
 	&queue_ra_entry.attr,
@@ -767,6 +804,7 @@ static struct attribute *queue_attrs[] = {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
 	&throtl_sample_time_entry.attr,
 #endif
+	&queue_histstat_entry.attr,
 	NULL,
 };
 
@@ -856,6 +894,8 @@ static void __blk_release_queue(struct work_struct *work)
 {
 	struct request_queue *q = container_of(work, typeof(*q), release_work);
 
+	blk_dev_stats_free(q);
+
 	if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
 		blk_stat_remove_callback(q, q->poll_cb);
 	blk_stat_free_callback(q->poll_cb);
include/linux/blkdev.h +7 -0
index 2716f239b56d..ed57518a15fb 100644
@@ -53,6 +53,9 @@ struct blk_stat_callback;
 /* Doing classic polling */
 #define BLK_MQ_POLL_CLASSIC -1
 
+/* Must be consistent with blk_part_stats_bkt() */
+#define BLK_DEV_STATS_BKTS (3 * 8)
+
 /*
  * Maximum number of blkcg policies allowed to be registered concurrently.
  * Defined here to simplify include dependency.
@@ -478,6 +481,9 @@ struct request_queue {
 	struct blk_stat_callback	*poll_cb;
 	struct blk_rq_stat	poll_stat[BLK_MQ_POLL_STATS_BKTS];
 
+	struct blk_stat_callback	*dev_cb;
+	struct blk_rq_stat	dev_stat[BLK_DEV_STATS_BKTS];
+
 	struct timer_list	timeout;
 	struct work_struct	timeout_work;
 
@@ -605,6 +611,7 @@ struct request_queue {
 #define QUEUE_FLAG_SCSI_PASSTHROUGH 23	/* queue supports SCSI commands */
 #define QUEUE_FLAG_QUIESCED	24	/* queue has been quiesced */
 #define QUEUE_FLAG_PCI_P2PDMA	25	/* device supports PCI p2p requests */
+#define QUEUE_FLAG_HISTSTATS	26	/* Hist stats enabled if set */
 
 #define QUEUE_FLAG_MQ_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
 				 (1 << QUEUE_FLAG_SAME_COMP))
Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 4/5 Export block dev stats to sysfs

In reply to Jes Sorensen

From: Jes Sorensen <[email protected]>

This exports bytes of read/write/discard per bucket to sysfs

---
 block/blk-sysfs.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
block/blk-sysfs.c +23 -0
index 81566432828d..63fdda5e6ccb 100644
@@ -530,6 +530,23 @@ static ssize_t queue_dax_show(struct request_queue *q, char *page)
 	return queue_var_show(blk_queue_dax(q), page);
 }
 
+static ssize_t queue_stat_show(struct request_queue *q, char *p)
+{
+	char name[3][8] = {"read", "write", "discard"};
+	int bkt, off, i;
+
+	off = 0;
+	for (i = 0; i < 3; i++) {
+		off += sprintf(p + off, "%s: ", name[i]);
+		for (bkt = 0; bkt < (BLK_DEV_STATS_BKTS / 3); bkt++) {
+			off += sprintf(p + off, "%llu ",
+				       q->dev_stat[i + 3 * bkt].size);
+		}
+		off += sprintf(p + off, "\n");
+	}
+	return off;
+}
+
 static ssize_t queue_histstat_show(struct request_queue *q, char *page)
 {
 	return queue_var_show(test_bit(QUEUE_FLAG_HISTSTATS,
@@ -759,6 +776,11 @@ static struct queue_sysfs_entry throtl_sample_time_entry = {
 };
 #endif
 
+static struct queue_sysfs_entry queue_stat_entry = {
+	.attr = {.name = "stat", .mode = 0444 },
+	.show = queue_stat_show,
+};
+
 static struct queue_sysfs_entry queue_histstat_entry = {
 	.attr = {.name = "histstat", .mode = 0644 },
 	.show = queue_histstat_show,
@@ -804,6 +826,7 @@ static struct attribute *queue_attrs[] = {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
 	&throtl_sample_time_entry.attr,
 #endif
+	&queue_stat_entry.attr,
 	&queue_histstat_entry.attr,
 	NULL,
 };
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 5/5 Expand block stats to export number of of requests per bucket

In reply to Jes Sorensen

From: Jes Sorensen <[email protected]>

---
 block/blk-sysfs.c | 8 ++++++++
 1 file changed, 8 insertions(+)
block/blk-sysfs.c +8 -0
index 63fdda5e6ccb..0dd4a3825a92 100644
@@ -543,6 +543,14 @@ static ssize_t queue_stat_show(struct request_queue *q, char *p)
 				       q->dev_stat[i + 3 * bkt].size);
 		}
 		off += sprintf(p + off, "\n");
+
+		off += sprintf(p + off, "%s reqs: ", name[i]);
+		for (bkt = 0; bkt < (BLK_DEV_STATS_BKTS / 3); bkt++) {
+			off += sprintf(p + off, "%u ",
+				       q->dev_stat[i + 3 * bkt].nr_samples);
+		}
+
+		off += sprintf(p + off, "\n");
 	}
 	return off;
 }
Signature · 1 line
2.17.1