#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2018 Ming Lei <ming.lei@redhat.com>
#
# Test blk-mq request allocation when hardware tags are limited. Regression
# test for commit e6fc46498784 ("blk-mq: avoid starving tag allocation after
# allocating process migrates").

. tests/block/rc
. common/null_blk

DESCRIPTION="run null-blk on different schedulers with only one hardware tag"
QUICK=1
CAN_BE_ZONED=1

requires() {
	_have_null_blk && _have_fio
}

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

	if ! _configure_null_blk nullb1 irqmode=2 completion_nsec=2000000 \
	     submit_queues=4 hw_queue_depth=1 power=1; then
		return 1
	fi

	local max_iodepth=$(($(cat /proc/sys/fs/aio-max-nr) / $(nproc)))
	local iodepth=1024
	if (( iodepth > max_iodepth )); then
		iodepth=$max_iodepth
	fi
	for sched in $(_io_schedulers nullb1); do
		echo "Testing $sched" >> "$FULL"
		echo "$sched" > /sys/block/nullb1/queue/scheduler
		_fio_perf --bs=4k --ioengine=libaio --iodepth=$iodepth \
			--numjobs="$(nproc)" --rw=randread --name=async \
			--filename=/dev/nullb1 --size=1g --direct=1 \
			--runtime=10
	done

	_exit_null_blk

	echo "Test complete"
}
