summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/rcutorture/bin/kvm-assign-cpus.sh
blob: 46b08cd16ba5c2e8fccb6464721d7c810d98c6f6 (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
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0+
#
# Produce awk statements roughly depicting the system's CPU and cache
# layout.  If the required information is not available, produce
# error messages as awk comments.  Successful exit regardless.
#
# Usage: kvm-assign-cpus.sh /path/to/sysfs

T="`mktemp -d ${TMPDIR-/tmp}/kvm-assign-cpus.sh.XXXXXX`"
trap 'rm -rf $T' 0 2

sysfsdir=${1-/sys/devices/system/node}
if ! cd "$sysfsdir" > $T/msg 2>&1
then
	sed -e 's/^/# /' < $T/msg
	exit 0
fi
nodelist="`ls -d node*`"
for i in node*
do
	if ! test -d $i/
	then
		echo "# Not a directory: $sysfsdir/node*"
		exit 0
	fi
	for j in $i/cpu*/cache/index*
	do
		if ! test -d $j/
		then
			echo "# Not a directory: $sysfsdir/$j"
			exit 0
		else
			break
		fi
	done
	indexlist="`ls -d $i/cpu* | grep 'cpu[0-9][0-9]*' | head -1 | sed -e 's,^.*$,ls -d &/cache/index*,' | sh | sed -e 's,^.*/,,'`"
	break
done
for i in node*/cpu*/cache/index*/shared_cpu_list
do
	if ! test -f $i
	then
		echo "# Not a file: $sysfsdir/$i"
		exit 0
	else
		break
	fi
done
firstshared=
for i in $indexlist
do
	rm -f $T/cpulist
	for n in node*
	do
		f="$n/cpu*/cache/$i/shared_cpu_list"
		if ! cat $f > $T/msg 2>&1
		then
			sed -e 's/^/# /' < $T/msg
			exit 0
		fi
		cat $f >> $T/cpulist
	done
	if grep -q '[-,]' $T/cpulist
	then
		if test -z "$firstshared"
		then
			firstshared="$i"
		fi
	fi
done
if test -z "$firstshared"
then
	splitindex="`echo $indexlist | sed -e 's/ .*$//'`"
else
	splitindex="$firstshared"
fi
nodenum=0
for n in node*
do
	cat $n/cpu*/cache/$splitindex/shared_cpu_list | sort -u -k1n |
	awk -v nodenum="$nodenum" '
	BEGIN {
		idx = 0;
	}

	{
		nlists = split($0, cpulists, ",");
		for (i = 1; i <= nlists; i++) {
			listsize = split(cpulists[i], cpus, "-");
			if (listsize == 1)
				cpus[2] = cpus[1];
			for (j = cpus[1]; j <= cpus[2]; j++) {
				print "cpu[" nodenum "][" idx "] = " j ";";
				idx++;
			}
		}
	}

	END {
		print "nodecpus[" nodenum "] = " idx ";";
	}'
	nodenum=`expr $nodenum + 1`
done
echo "numnodes = $nodenum;"