Skip to content

Commit 41d2d84

Browse files
apaszkieliu-song-6
authored andcommitted
md: improve io stats accounting
Use generic io accounting functions to manage io stats. There was an attempt to do this earlier in commit 18c0b22 ("md: use generic io stats accounting functions to simplify io stat accounting"), but it did not include a call to generic_end_io_acct() and caused issues with tracking in-flight IOs, so it was later removed in commit 74672d0 ("md: fix md io stats accounting broken"). This patch attempts to fix this by using both disk_start_io_acct() and disk_end_io_acct(). To make it possible, a struct md_io is allocated for every new md bio, which includes the io start_time. A new mempool is introduced for this purpose. We override bio->bi_end_io with our own callback and call disk_start_io_acct() before passing the bio to md_handle_request(). When it completes, we call disk_end_io_acct() and the original bi_end_io callback. This adds correct statistics about in-flight IOs and IO processing time, interpreted e.g. in iostat as await, svctm, aqu-sz and %util. It also fixes a situation where too many IOs where reported if a bio was re-submitted to the mddev, because io accounting is now performed only on newly arriving bios. Acked-by: Guoqing Jiang <[email protected]> Signed-off-by: Artur Paszkiewicz <[email protected]> Signed-off-by: Song Liu <[email protected]>
1 parent 9a5a859 commit 41d2d84

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

drivers/md/md.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,33 @@ void md_handle_request(struct mddev *mddev, struct bio *bio)
463463
}
464464
EXPORT_SYMBOL(md_handle_request);
465465

466+
struct md_io {
467+
struct mddev *mddev;
468+
bio_end_io_t *orig_bi_end_io;
469+
void *orig_bi_private;
470+
unsigned long start_time;
471+
};
472+
473+
static void md_end_io(struct bio *bio)
474+
{
475+
struct md_io *md_io = bio->bi_private;
476+
struct mddev *mddev = md_io->mddev;
477+
478+
disk_end_io_acct(mddev->gendisk, bio_op(bio), md_io->start_time);
479+
480+
bio->bi_end_io = md_io->orig_bi_end_io;
481+
bio->bi_private = md_io->orig_bi_private;
482+
483+
mempool_free(md_io, &mddev->md_io_pool);
484+
485+
if (bio->bi_end_io)
486+
bio->bi_end_io(bio);
487+
}
488+
466489
static blk_qc_t md_submit_bio(struct bio *bio)
467490
{
468491
const int rw = bio_data_dir(bio);
469-
const int sgrp = op_stat_group(bio_op(bio));
470492
struct mddev *mddev = bio->bi_disk->private_data;
471-
unsigned int sectors;
472493

473494
if (mddev == NULL || mddev->pers == NULL) {
474495
bio_io_error(bio);
@@ -489,21 +510,27 @@ static blk_qc_t md_submit_bio(struct bio *bio)
489510
return BLK_QC_T_NONE;
490511
}
491512

492-
/*
493-
* save the sectors now since our bio can
494-
* go away inside make_request
495-
*/
496-
sectors = bio_sectors(bio);
513+
if (bio->bi_end_io != md_end_io) {
514+
struct md_io *md_io;
515+
516+
md_io = mempool_alloc(&mddev->md_io_pool, GFP_NOIO);
517+
md_io->mddev = mddev;
518+
md_io->orig_bi_end_io = bio->bi_end_io;
519+
md_io->orig_bi_private = bio->bi_private;
520+
521+
bio->bi_end_io = md_end_io;
522+
bio->bi_private = md_io;
523+
524+
md_io->start_time = disk_start_io_acct(mddev->gendisk,
525+
bio_sectors(bio),
526+
bio_op(bio));
527+
}
528+
497529
/* bio could be mergeable after passing to underlayer */
498530
bio->bi_opf &= ~REQ_NOMERGE;
499531

500532
md_handle_request(mddev, bio);
501533

502-
part_stat_lock();
503-
part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
504-
part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
505-
part_stat_unlock();
506-
507534
return BLK_QC_T_NONE;
508535
}
509536

@@ -5546,6 +5573,7 @@ static void md_free(struct kobject *ko)
55465573

55475574
bioset_exit(&mddev->bio_set);
55485575
bioset_exit(&mddev->sync_set);
5576+
mempool_exit(&mddev->md_io_pool);
55495577
kfree(mddev);
55505578
}
55515579

@@ -5641,6 +5669,11 @@ static int md_alloc(dev_t dev, char *name)
56415669
*/
56425670
mddev->hold_active = UNTIL_STOP;
56435671

5672+
error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE,
5673+
sizeof(struct md_io));
5674+
if (error)
5675+
goto abort;
5676+
56445677
error = -ENOMEM;
56455678
mddev->queue = blk_alloc_queue(NUMA_NO_NODE);
56465679
if (!mddev->queue)

drivers/md/md.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ struct mddev {
481481
struct bio_set sync_set; /* for sync operations like
482482
* metadata and bitmap writes
483483
*/
484+
mempool_t md_io_pool;
484485

485486
/* Generic flush handling.
486487
* The last to finish preflush schedules a worker to submit

0 commit comments

Comments
 (0)