1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Copyright (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com>
5#
6# This is a test script for the kernel test driver to analyse vmalloc
7# allocator. Therefore it is just a kernel module loader. You can specify
8# and pass different parameters in order to:
9#     a) analyse performance of vmalloc allocations;
10#     b) stressing and stability check of vmalloc subsystem.
11
12TEST_NAME="vmalloc"
13DRIVER="test_${TEST_NAME}"
14NUM_CPUS=`grep -c ^processor /proc/cpuinfo`
15
16# 1 if fails
17exitcode=1
18
19# Kselftest framework requirement - SKIP code is 4.
20ksft_skip=4
21
22#
23# Static templates for performance, stressing and smoke tests.
24# Also it is possible to pass any supported parameters manualy.
25#
26PERF_PARAM="sequential_test_order=1 test_repeat_count=3"
27SMOKE_PARAM="test_loop_count=10000 test_repeat_count=10"
28STRESS_PARAM="nr_threads=$NUM_CPUS test_repeat_count=20"
29
30check_test_requirements()
31{
32	uid=$(id -u)
33	if [ $uid -ne 0 ]; then
34		echo "$0: Must be run as root"
35		exit $ksft_skip
36	fi
37
38	if ! which modprobe > /dev/null 2>&1; then
39		echo "$0: You need modprobe installed"
40		exit $ksft_skip
41	fi
42
43	if ! modinfo $DRIVER > /dev/null 2>&1; then
44		echo "$0: You must have the following enabled in your kernel:"
45		echo "CONFIG_TEST_VMALLOC=m"
46		exit $ksft_skip
47	fi
48}
49
50run_perfformance_check()
51{
52	echo "Run performance tests to evaluate how fast vmalloc allocation is."
53	echo "It runs all test cases on one single CPU with sequential order."
54
55	modprobe $DRIVER $PERF_PARAM > /dev/null 2>&1
56	echo "Done."
57	echo "Ccheck the kernel message buffer to see the summary."
58}
59
60run_stability_check()
61{
62	echo "Run stability tests. In order to stress vmalloc subsystem all"
63	echo "available test cases are run by NUM_CPUS workers simultaneously."
64	echo "It will take time, so be patient."
65
66	modprobe $DRIVER $STRESS_PARAM > /dev/null 2>&1
67	echo "Done."
68	echo "Check the kernel ring buffer to see the summary."
69}
70
71run_smoke_check()
72{
73	echo "Run smoke test. Note, this test provides basic coverage."
74	echo "Please check $0 output how it can be used"
75	echo "for deep performance analysis as well as stress testing."
76
77	modprobe $DRIVER $SMOKE_PARAM > /dev/null 2>&1
78	echo "Done."
79	echo "Check the kernel ring buffer to see the summary."
80}
81
82usage()
83{
84	echo -n "Usage: $0 [ performance ] | [ stress ] | | [ smoke ] | "
85	echo "manual parameters"
86	echo
87	echo "Valid tests and parameters:"
88	echo
89	modinfo $DRIVER
90	echo
91	echo "Example usage:"
92	echo
93	echo "# Shows help message"
94	echo "./${DRIVER}.sh"
95	echo
96	echo "# Runs 1 test(id_1), repeats it 5 times by NUM_CPUS workers"
97	echo "./${DRIVER}.sh nr_threads=$NUM_CPUS run_test_mask=1 test_repeat_count=5"
98	echo
99	echo -n "# Runs 4 tests(id_1|id_2|id_4|id_16) on one CPU with "
100	echo "sequential order"
101	echo -n "./${DRIVER}.sh sequential_test_order=1 "
102	echo "run_test_mask=23"
103	echo
104	echo -n "# Runs all tests by NUM_CPUS workers, shuffled order, repeats "
105	echo "20 times"
106	echo "./${DRIVER}.sh nr_threads=$NUM_CPUS test_repeat_count=20"
107	echo
108	echo "# Performance analysis"
109	echo "./${DRIVER}.sh performance"
110	echo
111	echo "# Stress testing"
112	echo "./${DRIVER}.sh stress"
113	echo
114	exit 0
115}
116
117function validate_passed_args()
118{
119	VALID_ARGS=`modinfo $DRIVER | awk '/parm:/ {print $2}' | sed 's/:.*//'`
120
121	#
122	# Something has been passed, check it.
123	#
124	for passed_arg in $@; do
125		key=${passed_arg//=*/}
126		val="${passed_arg:$((${#key}+1))}"
127		valid=0
128
129		for valid_arg in $VALID_ARGS; do
130			if [[ $key = $valid_arg ]] && [[ $val -gt 0 ]]; then
131				valid=1
132				break
133			fi
134		done
135
136		if [[ $valid -ne 1 ]]; then
137			echo "Error: key or value is not correct: ${key} $val"
138			exit $exitcode
139		fi
140	done
141}
142
143function run_manual_check()
144{
145	#
146	# Validate passed parameters. If there is wrong one,
147	# the script exists and does not execute further.
148	#
149	validate_passed_args $@
150
151	echo "Run the test with following parameters: $@"
152	modprobe $DRIVER $@ > /dev/null 2>&1
153	echo "Done."
154	echo "Check the kernel ring buffer to see the summary."
155}
156
157function run_test()
158{
159	if [ $# -eq 0 ]; then
160		usage
161	else
162		if [[ "$1" = "performance" ]]; then
163			run_perfformance_check
164		elif [[ "$1" = "stress" ]]; then
165			run_stability_check
166		elif [[ "$1" = "smoke" ]]; then
167			run_smoke_check
168		else
169			run_manual_check $@
170		fi
171	fi
172}
173
174check_test_requirements
175run_test $@
176
177exit 0
178