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