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