1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3# 4# Copyright (C) 2024 Yunsheng Lin <linyunsheng@huawei.com> 5# Copyright (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com> 6# 7# This is a test script for the kernel test driver to test the 8# correctness and performance of page_frag's implementation. 9# Therefore it is just a kernel module loader. You can specify 10# and pass different parameters in order to: 11# a) analyse performance of page fragment allocations; 12# b) stressing and stability check of page_frag subsystem. 13 14DRIVER="./page_frag/page_frag_test.ko" 15CPU_LIST=$(grep -m 2 processor /proc/cpuinfo | cut -d ' ' -f 2) 16TEST_CPU_0=$(echo $CPU_LIST | awk '{print $1}') 17 18if [ $(echo $CPU_LIST | wc -w) -gt 1 ]; then 19 TEST_CPU_1=$(echo $CPU_LIST | awk '{print $2}') 20 NR_TEST=100000000 21else 22 TEST_CPU_1=$TEST_CPU_0 23 NR_TEST=1000000 24fi 25 26# 1 if fails 27exitcode=1 28 29# Kselftest framework requirement - SKIP code is 4. 30ksft_skip=4 31 32check_test_failed_prefix() { 33 if dmesg | grep -q 'page_frag_test failed:';then 34 echo "page_frag_test failed, please check dmesg" 35 exit $exitcode 36 fi 37} 38 39# 40# Static templates for testing of page_frag APIs. 41# Also it is possible to pass any supported parameters manually. 42# 43SMOKE_PARAM="test_push_cpu=$TEST_CPU_0 test_pop_cpu=$TEST_CPU_1" 44NONALIGNED_PARAM="$SMOKE_PARAM test_alloc_len=75 nr_test=$NR_TEST" 45ALIGNED_PARAM="$NONALIGNED_PARAM test_align=1" 46 47check_test_requirements() 48{ 49 uid=$(id -u) 50 if [ $uid -ne 0 ]; then 51 echo "$0: Must be run as root" 52 exit $ksft_skip 53 fi 54 55 if ! which insmod > /dev/null 2>&1; then 56 echo "$0: You need insmod installed" 57 exit $ksft_skip 58 fi 59 60 if [ ! -f $DRIVER ]; then 61 echo "$0: You need to compile page_frag_test module" 62 exit $ksft_skip 63 fi 64} 65 66run_nonaligned_check() 67{ 68 echo "Run performance tests to evaluate how fast nonaligned alloc API is." 69 70 insmod $DRIVER $NONALIGNED_PARAM > /dev/null 2>&1 71} 72 73run_aligned_check() 74{ 75 echo "Run performance tests to evaluate how fast aligned alloc API is." 76 77 insmod $DRIVER $ALIGNED_PARAM > /dev/null 2>&1 78} 79 80run_smoke_check() 81{ 82 echo "Run smoke test." 83 84 insmod $DRIVER $SMOKE_PARAM > /dev/null 2>&1 85} 86 87usage() 88{ 89 echo -n "Usage: $0 [ aligned ] | [ nonaligned ] | | [ smoke ] | " 90 echo "manual parameters" 91 echo 92 echo "Valid tests and parameters:" 93 echo 94 modinfo $DRIVER 95 echo 96 echo "Example usage:" 97 echo 98 echo "# Shows help message" 99 echo "$0" 100 echo 101 echo "# Smoke testing" 102 echo "$0 smoke" 103 echo 104 echo "# Performance testing for nonaligned alloc API" 105 echo "$0 nonaligned" 106 echo 107 echo "# Performance testing for aligned alloc API" 108 echo "$0 aligned" 109 echo 110 exit 0 111} 112 113function validate_passed_args() 114{ 115 VALID_ARGS=`modinfo $DRIVER | awk '/parm:/ {print $2}' | sed 's/:.*//'` 116 117 # 118 # Something has been passed, check it. 119 # 120 for passed_arg in $@; do 121 key=${passed_arg//=*/} 122 valid=0 123 124 for valid_arg in $VALID_ARGS; do 125 if [[ $key = $valid_arg ]]; then 126 valid=1 127 break 128 fi 129 done 130 131 if [[ $valid -ne 1 ]]; then 132 echo "Error: key is not correct: ${key}" 133 exit $exitcode 134 fi 135 done 136} 137 138function run_manual_check() 139{ 140 # 141 # Validate passed parameters. If there is wrong one, 142 # the script exists and does not execute further. 143 # 144 validate_passed_args $@ 145 146 echo "Run the test with following parameters: $@" 147 insmod $DRIVER $@ > /dev/null 2>&1 148} 149 150function run_test() 151{ 152 if [ $# -eq 0 ]; then 153 usage 154 else 155 if [[ "$1" = "smoke" ]]; then 156 run_smoke_check 157 elif [[ "$1" = "nonaligned" ]]; then 158 run_nonaligned_check 159 elif [[ "$1" = "aligned" ]]; then 160 run_aligned_check 161 else 162 run_manual_check $@ 163 fi 164 fi 165 166 check_test_failed_prefix 167 168 echo "Done." 169 echo "Check the kernel ring buffer to see the summary." 170} 171 172check_test_requirements 173run_test $@ 174 175exit 0 176