summaryrefslogtreecommitdiffstats
path: root/src/spdk/test/dd/common.sh
blob: d2f7defa3f13e9fdd69a57f656dea26f8cc145e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
source "$rootdir/test/common/autotest_common.sh"
source "$rootdir/scripts/common.sh"

clear_nvme() {
	local bdev=$1
	local nvme_ref=$2
	local size=${3:-0xffff}

	local bs=$((1024 << 10)) # 1M
	local count=$(((size / bs) + (size % bs ? 1 : 0)))

	"${DD_APP[@]}" \
		--if="/dev/zero" \
		--bs="$bs" \
		--ob="$bdev" \
		--count="$count" \
		--json <(gen_conf $nvme_ref)
}

trunc_files() {
	local f
	for f; do : > "$f"; done
}

gen_conf() {
	xtrace_disable

	local ref_name
	local method methods
	local param params
	local config

	# Pick references to all assoc arrays and build subsystem's config
	# around them. The assoc array should be the name of the rpc method
	# suffixed with unique _ID (ID may be any string). Default arrays
	# should be prefixed with _method string. The keys of the array
	# should store names of the method's parameters - proper quoting
	# of the values is done here. extra_subsystems[] can store extra
	# json configuration for different subsystems, other than bdev.

	methods=("${@:-${!method_@}}")
	local IFS=","

	for ref_name in "${methods[@]}"; do
		method=${ref_name#*method_} method=${method%_*} params=()

		# FIXME: centos7's Bash got trapped in 2011:
		# local -n ref=$ref_name -> local: -n: invalid option
		# HACK: it with eval and partial refs instead.
		eval "local refs=(\${!${ref_name}[@]})"
		local param_ref

		for param in "${refs[@]}"; do
			param_ref="${ref_name}[$param]"
			if [[ ${!param_ref} =~ ^([0-9]+|true|false|\{.*\})$ ]]; then
				params+=("\"$param\": ${!param_ref}")
			else
				params+=("\"$param\": \"${!param_ref}\"")
			fi
		done

		config+=("$(
			cat <<- JSON
				{
				  "params": {
				    ${params[*]}
				  },
				  "method": "$method"
				}
			JSON
		)")
	done

	jq . <<- JSON | tee /dev/stderr
		{
		  "subsystems": [
		    {
		      "subsystem": "bdev",
		      "config": [
		        ${config[*]}
		      ]
		    }
		    ${extra_subsystems[*]:+,${extra_subsystems[*]}}
		  ]
		}
	JSON

	xtrace_restore
}

gen_bytes() {
	xtrace_disable

	local max=$1
	local bytes
	local byte
	local string
	shift

	bytes=({a..z} {0..9})
	if (($#)); then
		bytes=("$@")
	fi

	for ((byte = 0; byte < max; byte++)); do
		string+=${bytes[RANDOM % ${#bytes[@]}]}
	done
	printf '%b' "$string"

	xtrace_restore
}

get_native_nvme_bs() {
	# This is now needed since spdk_dd will reject all bs smaller than the
	# native bs of given nvme. We need to make sure all tests are using
	# bs >= native_bs. Use identify here so we don't have to switch nvmes
	# between user space and the kernel back and forth.
	local pci=$1 lbaf id

	mapfile -t id < <("$rootdir/build/examples/identify" -r trtype:pcie "traddr:$pci")

	# Get size of the current LBAF
	[[ ${id[*]} =~ "Current LBA Format:"\ *"LBA Format #"([0-9]+) ]]
	lbaf=${BASH_REMATCH[1]}
	[[ ${id[*]} =~ "LBA Format #$lbaf: Data Size:"\ *([0-9]+) ]]
	lbaf=${BASH_REMATCH[1]}

	echo "$lbaf"
}

check_liburing() {
	# Simply check if spdk_dd links to liburing. If yes, log that information.
	local lib so
	local -g liburing_in_use=0

	while read -r lib _ so _; do
		if [[ $lib == liburing.so.* ]]; then
			printf '* spdk_dd linked to liburing\n'
			# For sanity, check build config to see if liburing was requested.
			if [[ -e $rootdir/test/common/build_config.sh ]]; then
				source "$rootdir/test/common/build_config.sh"
			fi
			if [[ $CONFIG_URING != y ]]; then
				printf '* spdk_dd built with liburing, but no liburing support requested?\n'
			fi
			if [[ ! -e $so ]]; then
				printf '* %s is missing, aborting\n' "$lib"
				return 1
			fi
			export liburing_in_use=1
			return 0
		fi
	done < <(LD_TRACE_LOADED_OBJECTS=1 "${DD_APP[@]}") >&2
}