linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yu Kuai <yukuai3@huawei.com>
To: <axboe@kernel.dk>, <mpatocka@redhat.com>, <snitzer@redhat.com>
Cc: <linux-block@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<yukuai3@huawei.com>, <yi.zhang@huawei.com>
Subject: [PATCH 2/3] block: factor out common code for part_stat_show() and diskstats_show()
Date: Thu, 17 Mar 2022 19:26:52 +0800	[thread overview]
Message-ID: <20220317112653.1019490-3-yukuai3@huawei.com> (raw)
In-Reply-To: <20220317112653.1019490-1-yukuai3@huawei.com>

part_stat_show() and diskstats_show() are very similar, just factor out
common code.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/genhd.c | 130 +++++++++++++++++++-------------------------------
 1 file changed, 49 insertions(+), 81 deletions(-)

diff --git a/block/genhd.c b/block/genhd.c
index e5307f512185..f2c7de2e7ca9 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -53,6 +53,30 @@ static atomic64_t diskseq;
 #define NR_EXT_DEVT		(1 << MINORBITS)
 static DEFINE_IDA(ext_devt_ida);
 
+#define part_stat_info(inflight, stat) \
+	"%lu %lu %llu %u " \
+	"%lu %lu %llu %u " \
+	"%u %u %u " \
+	"%lu %lu %llu %u " \
+	"%lu %u" \
+	"\n", \
+	(stat).ios[STAT_READ], (stat).merges[STAT_READ], \
+	(unsigned long long)(stat).sectors[STAT_READ], \
+	(unsigned int)div_u64((stat).nsecs[STAT_READ], NSEC_PER_MSEC), \
+	(stat).ios[STAT_WRITE], (stat).merges[STAT_WRITE], \
+	(unsigned long long)(stat).sectors[STAT_WRITE], \
+	(unsigned int)div_u64((stat).nsecs[STAT_WRITE], NSEC_PER_MSEC), \
+	(inflight), jiffies_to_msecs((stat).io_ticks), \
+	(unsigned int)div_u64((stat).nsecs[STAT_READ] + \
+			      (stat).nsecs[STAT_WRITE] + \
+			      (stat).nsecs[STAT_DISCARD] + \
+			      (stat).nsecs[STAT_FLUSH], NSEC_PER_MSEC), \
+	(stat).ios[STAT_DISCARD], (stat).merges[STAT_DISCARD], \
+	(unsigned long long)(stat).sectors[STAT_DISCARD], \
+	(unsigned int)div_u64((stat).nsecs[STAT_DISCARD], NSEC_PER_MSEC), \
+	(stat).ios[STAT_FLUSH], \
+	(unsigned int)div_u64((stat).nsecs[STAT_FLUSH], NSEC_PER_MSEC)
+
 void set_capacity(struct gendisk *disk, sector_t sectors)
 {
 	struct block_device *bdev = disk->part0;
@@ -929,17 +953,13 @@ ssize_t part_size_show(struct device *dev,
 	return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
 }
 
-ssize_t part_stat_show(struct device *dev,
-		       struct device_attribute *attr, char *buf)
+static unsigned int part_get_stat(struct block_device *bdev,
+				  struct disk_stats *stat)
+
 {
-	struct block_device *bdev = dev_to_bdev(dev);
 	struct request_queue *q = bdev_get_queue(bdev);
-	struct disk_stats stat;
 	unsigned int inflight;
 
-	if (!blk_queue_io_stat(q))
-		return sprintf(buf, "io accounting is disabled\n");
-
 	if (queue_is_mq(q))
 		inflight = blk_mq_in_flight(q, bdev);
 	else
@@ -950,35 +970,23 @@ ssize_t part_stat_show(struct device *dev,
 		update_io_ticks(bdev, jiffies, true);
 		part_stat_unlock();
 	}
-	part_stat_read_all(bdev, &stat);
-	return sprintf(buf,
-		"%8lu %8lu %8llu %8u "
-		"%8lu %8lu %8llu %8u "
-		"%8u %8u %8u "
-		"%8lu %8lu %8llu %8u "
-		"%8lu %8u"
-		"\n",
-		stat.ios[STAT_READ],
-		stat.merges[STAT_READ],
-		(unsigned long long)stat.sectors[STAT_READ],
-		(unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
-		stat.ios[STAT_WRITE],
-		stat.merges[STAT_WRITE],
-		(unsigned long long)stat.sectors[STAT_WRITE],
-		(unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
-		inflight,
-		jiffies_to_msecs(stat.io_ticks),
-		(unsigned int)div_u64(stat.nsecs[STAT_READ] +
-				      stat.nsecs[STAT_WRITE] +
-				      stat.nsecs[STAT_DISCARD] +
-				      stat.nsecs[STAT_FLUSH],
-						NSEC_PER_MSEC),
-		stat.ios[STAT_DISCARD],
-		stat.merges[STAT_DISCARD],
-		(unsigned long long)stat.sectors[STAT_DISCARD],
-		(unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
-		stat.ios[STAT_FLUSH],
-		(unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
+	part_stat_read_all(bdev, stat);
+
+	return inflight;
+}
+
+ssize_t part_stat_show(struct device *dev,
+		       struct device_attribute *attr, char *buf)
+{
+	struct block_device *bdev = dev_to_bdev(dev);
+	struct disk_stats stat;
+	unsigned int inflight;
+
+	if (!blk_queue_io_stat(bdev_get_queue(bdev)))
+		return sprintf(buf, "io accounting is disabled\n");
+
+	inflight = part_get_stat(bdev, &stat);
+	return sprintf(buf, part_stat_info(inflight, stat));
 }
 
 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
@@ -1212,51 +1220,11 @@ static int diskstats_show(struct seq_file *seqf, void *v)
 			continue;
 		if (!blk_queue_io_stat(gp->queue))
 			continue;
-		if (queue_is_mq(gp->queue))
-			inflight = blk_mq_in_flight(gp->queue, hd);
-		else
-			inflight = part_in_flight(hd);
-
-		if (inflight) {
-			part_stat_lock();
-			update_io_ticks(hd, jiffies, true);
-			part_stat_unlock();
-		}
-		part_stat_read_all(hd, &stat);
-		seq_printf(seqf, "%4d %7d %pg "
-			   "%lu %lu %lu %u "
-			   "%lu %lu %lu %u "
-			   "%u %u %u "
-			   "%lu %lu %lu %u "
-			   "%lu %u"
-			   "\n",
-			   MAJOR(hd->bd_dev), MINOR(hd->bd_dev), hd,
-			   stat.ios[STAT_READ],
-			   stat.merges[STAT_READ],
-			   stat.sectors[STAT_READ],
-			   (unsigned int)div_u64(stat.nsecs[STAT_READ],
-							NSEC_PER_MSEC),
-			   stat.ios[STAT_WRITE],
-			   stat.merges[STAT_WRITE],
-			   stat.sectors[STAT_WRITE],
-			   (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
-							NSEC_PER_MSEC),
-			   inflight,
-			   jiffies_to_msecs(stat.io_ticks),
-			   (unsigned int)div_u64(stat.nsecs[STAT_READ] +
-						 stat.nsecs[STAT_WRITE] +
-						 stat.nsecs[STAT_DISCARD] +
-						 stat.nsecs[STAT_FLUSH],
-							NSEC_PER_MSEC),
-			   stat.ios[STAT_DISCARD],
-			   stat.merges[STAT_DISCARD],
-			   stat.sectors[STAT_DISCARD],
-			   (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
-						 NSEC_PER_MSEC),
-			   stat.ios[STAT_FLUSH],
-			   (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
-						 NSEC_PER_MSEC)
-			);
+
+		inflight = part_get_stat(hd, &stat);
+		seq_printf(seqf, "%4d %7d %pg ",
+			   MAJOR(hd->bd_dev), MINOR(hd->bd_dev), hd);
+		seq_printf(seqf, part_stat_info(inflight, stat));
 	}
 	rcu_read_unlock();
 
-- 
2.31.1


  parent reply	other threads:[~2022-03-17 11:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 11:26 [PATCH 0/3] optimizations for io accounting Yu Kuai
2022-03-17 11:26 ` [PATCH 1/3] block: don't show disk stats if io accounting is disabled Yu Kuai
2022-03-17 14:06   ` Bart Van Assche
2022-03-18  1:36     ` yukuai (C)
2022-03-18  3:10       ` Bart Van Assche
2022-03-18  6:12         ` yukuai (C)
2022-03-25  8:46   ` Christoph Hellwig
2022-03-17 11:26 ` Yu Kuai [this message]
2022-03-25  8:46   ` [PATCH 2/3] block: factor out common code for part_stat_show() and diskstats_show() Christoph Hellwig
2022-03-26  1:11     ` yukuai (C)
2022-03-17 11:26 ` [PATCH -next 3/3] block: update nsecs[] in part_get_stat() Yu Kuai
2022-04-01  3:42   ` yukuai (C)
2022-04-08  6:52     ` yukuai (C)
2022-03-25  7:29 ` [PATCH 0/3] optimizations for io accounting yukuai (C)

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=20220317112653.1019490-3-yukuai3@huawei.com \
    --to=yukuai3@huawei.com \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=snitzer@redhat.com \
    --cc=yi.zhang@huawei.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).