xref: /spdk/test/dd/common.sh (revision cc8a75c40bd3aeff9dc4c269de0daacfe94e61ea)
1#  SPDX-License-Identifier: BSD-3-Clause
2#  Copyright (C) 2020 Intel Corporation
3#  All rights reserved.
4#
5
6source "$rootdir/test/common/autotest_common.sh"
7source "$rootdir/scripts/common.sh"
8
9clear_nvme() {
10	local bdev=$1
11	local nvme_ref=$2
12	local size=${3:-0xffff}
13
14	local bs=$((1024 << 10)) # 1M
15	local count=$(((size / bs) + (size % bs ? 1 : 0)))
16
17	"${DD_APP[@]}" \
18		--if="/dev/zero" \
19		--bs="$bs" \
20		--ob="$bdev" \
21		--count="$count" \
22		--json <(gen_conf $nvme_ref)
23}
24
25trunc_files() {
26	local f
27	for f; do : > "$f"; done
28}
29
30gen_conf() {
31	xtrace_disable
32
33	local ref_name
34	local method methods
35	local param params
36	local config
37
38	# Pick references to all assoc arrays and build subsystem's config
39	# around them. The assoc array should be the name of the rpc method
40	# suffixed with unique _ID (ID may be any string). Default arrays
41	# should be prefixed with _method string. The keys of the array
42	# should store names of the method's parameters - proper quoting
43	# of the values is done here. extra_subsystems[] can store extra
44	# json configuration for different subsystems, other than bdev.
45
46	if (($# == 0)) && [[ -z ${!method_@} ]]; then
47		return 1
48	fi
49
50	methods=("${@:-${!method_@}}")
51	local IFS=","
52
53	for ref_name in "${methods[@]}"; do
54		method=${ref_name#*method_} method=${method%_*} params=()
55
56		local -n refs=$ref_name
57		for param in "${!refs[@]}"; do
58			if [[ ${refs["$param"]} =~ ^([0-9]+|true|false|\{.*\})$ ]]; then
59				params+=("\"$param\": ${refs["$param"]}")
60			else
61				params+=("\"$param\": \"${refs["$param"]}\"")
62			fi
63		done
64
65		config+=("$(
66			cat <<- JSON
67				{
68				  "params": {
69				    ${params[*]}
70				  },
71				  "method": "$method"
72				}
73			JSON
74		)")
75	done
76
77	jq . <<- JSON | tee /dev/stderr
78		{
79		  "subsystems": [
80		    {
81		      "subsystem": "bdev",
82		      "config": [
83		        ${config[*]},
84			{
85			  "method": "bdev_wait_for_examine"
86			}
87		      ]
88		    }
89		    ${extra_subsystems[*]:+,${extra_subsystems[*]}}
90		  ]
91		}
92	JSON
93
94	xtrace_restore
95}
96
97gen_bytes() {
98	xtrace_disable
99
100	local max=$1
101	local bytes
102	local byte
103	local string
104	shift
105
106	bytes=({a..z} {0..9})
107	if (($#)); then
108		bytes=("$@")
109	fi
110
111	for ((byte = 0; byte < max; byte++)); do
112		string+=${bytes[RANDOM % ${#bytes[@]}]}
113	done
114	printf '%b' "$string"
115
116	xtrace_restore
117}
118
119get_native_nvme_bs() {
120	# This is now needed since spdk_dd will reject all bs smaller than the
121	# native bs of given nvme. We need to make sure all tests are using
122	# bs >= native_bs. Use identify here so we don't have to switch nvmes
123	# between user space and the kernel back and forth.
124	local pci=$1 lbaf id
125
126	mapfile -t id < <("$rootdir/build/bin/spdk_nvme_identify" -r "trtype:pcie traddr:$pci")
127
128	# Get size of the current LBAF
129	[[ ${id[*]} =~ "Current LBA Format:"\ *"LBA Format #"([0-9]+) ]]
130	lbaf=${BASH_REMATCH[1]}
131	[[ ${id[*]} =~ "LBA Format #$lbaf: Data Size:"\ *([0-9]+) ]]
132	lbaf=${BASH_REMATCH[1]}
133
134	echo "$lbaf"
135}
136
137check_liburing() {
138	# Simply check if spdk_dd links to liburing. If yes, log that information.
139	local lib
140	local -g liburing_in_use=0
141
142	while read -r _ lib _; do
143		if [[ $lib == liburing.so.* ]]; then
144			printf '* spdk_dd linked to liburing\n'
145			# For sanity, check build config to see if liburing was requested.
146			if [[ -e $rootdir/test/common/build_config.sh ]]; then
147				source "$rootdir/test/common/build_config.sh"
148			fi
149			if [[ $CONFIG_URING != y ]]; then
150				printf '* spdk_dd built with liburing, but no liburing support requested?\n'
151			fi
152			export liburing_in_use=1
153			return 0
154		fi
155	done < <(objdump -p "${DD_APP[@]}" | grep "NEEDED") >&2
156}
157
158init_zram() {
159	[[ -e /sys/class/zram-control ]] || modprobe zram num_devices=0
160	return
161}
162
163create_zram_dev() {
164	cat /sys/class/zram-control/hot_add
165}
166
167remove_zram_dev() {
168	local id=$1
169
170	[[ -e /sys/block/zram$id ]]
171
172	echo 1 > "/sys/block/zram$id/reset"
173	echo "$id" > "/sys/class/zram-control/hot_remove"
174}
175
176set_zram_dev() {
177	local id=$1
178	local size=${2:-64M}
179
180	[[ -e /sys/block/zram$id ]]
181
182	echo "$size" > "/sys/block/zram$id/disksize"
183}
184
185init_null_blk() {
186	[[ -e /sys/module/null_blk ]] || modprobe null_blk "$@"
187	return
188}
189
190remove_null_blk() {
191	modprobe -r null_blk
192}
193