#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2018 Omar Sandoval
#
# Test the inflight counter in /sys/block/$dev/inflight, /sys/block/$dev/stat,
# and /proc/diskstats. Regression test for commit bf0ddaba65dd "blk-mq: fix
# sysfs inflight counter".

. tests/block/rc
. common/null_blk

DESCRIPTION="do I/O and check the inflight counter"
QUICK=1
CAN_BE_ZONED=1

requires() {
	_have_null_blk
}

show_inflight() {
	awk '{ printf "sysfs inflight reads %d\nsysfs inflight writes %d\n", $1, $2 }' \
		/sys/block/nullb0/inflight
	awk '{ print "sysfs stat " $9 }' /sys/block/nullb0/stat
	awk '$3 == "nullb0" { print "diskstats " $12 }' /proc/diskstats
}

test() {
	echo "Running ${TEST_NAME}"

	if ! _init_null_blk queue_mode=2 irqmode=2 \
	     completion_nsec=500000000; then
		return 1
	fi

	dd if=/dev/nullb0 of=/dev/null bs=4096 iflag=direct count=1 status=none &
	sleep 0.1
	show_inflight

	dd if=/dev/zero of=/dev/nullb0 bs=4096 oflag=direct count=1 status=none &
	sleep 0.1
	show_inflight

	wait
	show_inflight

	_exit_null_blk

	echo "Test complete"
}
