xref: /spdk/test/dd/common.sh (revision 06b537bfdb4393dea857e204b85d8df46a351d8a)
1source "$rootdir/test/common/autotest_common.sh"
2source "$rootdir/scripts/common.sh"
3
4clear_nvme() {
5	local bdev=$1
6	local nvme_ref=$2
7	local size=${3:-0xffff}
8
9	local bs=$((1024 << 10)) # 1M
10	local count=$(((size / bs) + (size % bs ? 1 : 0)))
11
12	"${DD_APP[@]}" \
13		--if="/dev/zero" \
14		--bs="$bs" \
15		--ob="$bdev" \
16		--count="$count" \
17		--json <(gen_conf $nvme_ref)
18}
19
20trunc_files() {
21	local f
22	for f; do : > "$f"; done
23}
24
25gen_conf() {
26	xtrace_disable
27
28	local ref_name
29	local method methods
30	local param params
31	local config
32
33	# Pick references to all assoc arrays and build subsystem's config
34	# around them. The assoc array should be the name of the rpc method
35	# suffixed with unique _ID (ID may be any string). Default arrays
36	# should be prefixed with _method string. The keys of the array
37	# should store names of the method's parameters - proper quoting
38	# of the values is done here. extra_subsystems[] can store extra
39	# json configuration for different subsystems, other than bdev.
40
41	methods=("${@:-${!method_@}}")
42	local IFS=","
43
44	for ref_name in "${methods[@]}"; do
45		method=${ref_name#*method_} method=${method%_*} params=()
46
47		# FIXME: centos7's Bash got trapped in 2011:
48		# local -n ref=$ref_name -> local: -n: invalid option
49		# HACK: it with eval and partial refs instead.
50		eval "local refs=(\${!${ref_name}[@]})"
51		local param_ref
52
53		for param in "${refs[@]}"; do
54			param_ref="${ref_name}[$param]"
55			if [[ ${!param_ref} =~ ^([0-9]+|true|false|\{.*\})$ ]]; then
56				params+=("\"$param\": ${!param_ref}")
57			else
58				params+=("\"$param\": \"${!param_ref}\"")
59			fi
60		done
61
62		config+=("$(
63			cat <<- JSON
64				{
65				  "params": {
66				    ${params[*]}
67				  },
68				  "method": "$method"
69				}
70			JSON
71		)")
72	done
73
74	jq . <<- JSON | tee /dev/stderr
75		{
76		  "subsystems": [
77		    {
78		      "subsystem": "bdev",
79		      "config": [
80		        ${config[*]}
81		      ]
82		    }
83		    ${extra_subsystems[*]:+,${extra_subsystems[*]}}
84		  ]
85		}
86	JSON
87
88	xtrace_restore
89}
90
91gen_bytes() {
92	xtrace_disable
93
94	local max=$1
95	local bytes
96	local byte
97	local string
98	shift
99
100	bytes=({a..z} {0..9})
101	if (($#)); then
102		bytes=("$@")
103	fi
104
105	for ((byte = 0; byte < max; byte++)); do
106		string+=${bytes[RANDOM % ${#bytes[@]}]}
107	done
108	printf '%b' "$string"
109
110	xtrace_restore
111}
112
113get_native_nvme_bs() {
114	# This is now needed since spdk_dd will reject all bs smaller than the
115	# native bs of given nvme. We need to make sure all tests are using
116	# bs >= native_bs. Use identify here so we don't have to switch nvmes
117	# between user space and the kernel back and forth.
118	local pci=$1 lbaf id
119
120	mapfile -t id < <("$rootdir/build/examples/identify" -r trtype:pcie "traddr:$pci")
121
122	# Get size of the current LBAF
123	[[ ${id[*]} =~ "Current LBA Format:"\ *"LBA Format #"([0-9]+) ]]
124	lbaf=${BASH_REMATCH[1]}
125	[[ ${id[*]} =~ "LBA Format #$lbaf: Data Size:"\ *([0-9]+) ]]
126	lbaf=${BASH_REMATCH[1]}
127
128	echo "$lbaf"
129}
130
131check_liburing() {
132	# Simply check if spdk_dd links to liburing. If yes, log that information.
133	local lib so
134	local -g liburing_in_use=0
135
136	while read -r lib _ so _; do
137		if [[ $lib == liburing.so.* ]]; then
138			printf '* spdk_dd linked to liburing\n'
139			# For sanity, check build config to see if liburing was requested.
140			if [[ -e $rootdir/test/common/build_config.sh ]]; then
141				source "$rootdir/test/common/build_config.sh"
142			fi
143			if [[ $CONFIG_URING != y ]]; then
144				printf '* spdk_dd built with liburing, but no liburing support requested?\n'
145			fi
146			if [[ ! -e $so ]]; then
147				printf '* %s is missing, aborting\n' "$lib"
148				return 1
149			fi
150			export liburing_in_use=1
151			return 0
152		fi
153	done < <(LD_TRACE_LOADED_OBJECTS=1 "${DD_APP[@]}") >&2
154}
155