-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcputopology
executable file
·277 lines (247 loc) · 7.36 KB
/
cputopology
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
DEBUG=
CMDLINEFILE=/proc/cmdline
TOPOLOGY=
MAXCPUS=0
MAXSOCKETS=0
MAXCORES=0
MAXTHREADS=0
RESTORE=
usage () {
echo "usage: cputopology --topology [ sparse | dense ] "
echo " --maxcpus [ maximum number of cpus ] "
echo " --maxsockets [ maximum number of sockets ] "
echo " --maxcores [ maximum number of cores/socket ] "
echo " --maxthreads [ maximum number of threads/core ] "
echo " --kernel (get values from kernel command line)"
echo " cputopology.topology=[ sparse | dense ]"
echo " cputopology.maxcpus=[ maximum number of cpus ]"
echo " cputopology.maxsockets=[ maximum number of sockets ]"
echo " cputopology.maxcores=[ maximum number of cores/socket ]"
echo " cputopology.maxthreads=[ maximum number of threads/core ]"
echo " --restore (enable all cpus) "
echo " --help (this message) "
echo " "
echo "The cputopology utility enables/disables cpus according to the input parameters"
echo "specified by a user. The --topology option must be used to define a sparse or"
echo "dense topology. A sparse topology is one where cpus are spread out evenly on"
echo "each socket, and a dense topology enumerates all the cpus on socket 0,"
echo "socket 1, and so on."
echo " "
echo "The cpu enumeration on many systems takes into account IRQ enumeration,"
echo "I/O & memory ordering, and thermal issues. This utility overrides those"
echo "settings"
echo " "
echo "ie.) USE AT YOUR OWN RISK."
}
cpustatus () {
# arg 1 is cpu number (0,1,2,...)
# arg 2 is 0 (offline) or 1 (online)
if [ -e /sys/devices/system/cpu/cpu$1/online ]; then
if [ ! "$DEBUG" ]; then
( echo $2 > /sys/devices/system/cpu/cpu$1/online ) >& /dev/null
else
echo $2 > /sys/devices/system/cpu/cpu$1/online
fi
fi
}
# optargs is nice but it only allows character use and not strings ...
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--topology)
[ "$2" == "sparse" ] && TOPOLOGY="SPARSE"
[ "$2" == "dense" ] && TOPOLOGY="DENSE"
shift;;
--maxcpus)
MAXCPUS="$2"
shift;;
--maxsockets)
MAXSOCKETS="$2"
shift;;
--maxcores)
MAXCORES="$2"
shift;;
--maxthreads)
MAXTHREADS="$2"
shift;;
--help)
usage
exit 0
;;
--restore)
RESTORE=1;
;;
--kernel)
KERNEL=1;
;;
--debug) # undocumented on purpose :)
DEBUG=1;
;;
--debugcmdline) # undocumented on purpose :)
CMDLINEFILE="$2"
shift
;;
*)
echo "Error: Unknown option $key"
usage
exit 1
;;
esac
shift
done
if [ $KERNEL ]; then
for parameter in $(cat $CMDLINEFILE)
do
echo $parameter | grep cputopology >& /dev/null
if [ $? -eq 0 ]; then
_par=${parameter##cputopology.}
key=$(echo $_par | awk -F "=" ' { print $1 } ')
arg=$(echo $_par | awk -F "=" ' { print $2 } ')
[ $DEBUG ] && echo "kernel parameter: $_par evaluated as $key $arg"
case $key in
topology) # read cputopology.topology for configuration
arg=$(echo $arg | tr [a-z] [A-Z])
TOPOLOGY=$arg
;;
maxcpus) # read cputopology.maxcpus for MAXCPUS
MAXCPUS=$arg
;;
maxsockets) # read cputopology.maxsockets for MAXSOCKETS
MAXSOCKETS=$arg
;;
maxcores) # read cputopology.maxcores for MAXCORES
MAXCORES=$arg
;;
maxthreads) # read cputopology.maxthreads for MAXTHREADS
MAXTHREADS=$arg
;;
*)
echo "Error: unknown option $key"
usage
exit 1
;;
esac
fi
done
fi
if [ ! "$RESTORE" ] && [ ! "$TOPOLOGY" ] && [ ! "$DEBUG" ]; then
echo "Error: Either --restore or --topology must be set."
usage
exit 1;
fi
[ $RESTORE ] && echo "Restoring all cpus."
MAXVALCPUS=$(lscpu | grep "^CPU(s)" | awk -F ":" ' { print $2 } ' | tr -d " ")
# online everything
for cpu in `seq 0 $MAXVALCPUS`
do
cpustatus $cpu 1
done
[ $RESTORE ] && exit
# find the real max cpus, sockets, cores, and threads (HT may be disabled!)
MAXVALSOCKETS=$(lscpu | grep "Socket(s)" | awk -F ":" ' { print $2 } ' | tr -d " ")
MAXVALTHREADS=$(lscpu | grep "Thread(s) per core" | awk -F ":" ' { print $2 } ' | tr -d " ")
MAXVALCPS=$(lscpu | grep "Core(s) per socket" | awk -F ":" ' { print $2 } ' | tr -d " ")
# These checks also mean that the MAX* values CANNOT be zero
[ $MAXCPUS -eq 0 ] && MAXCPUS=$MAXVALCPUS
[ $MAXSOCKETS -eq 0 ] && MAXSOCKETS=$MAXVALSOCKETS
[ $MAXCORES -eq 0 ] && MAXCORES=$MAXVALCPS
[ $MAXTHREADS -eq 0 ] && MAXTHREADS=$MAXVALTHREADS
echo "The hardware configuration is:"
echo "Max CPUS - $MAXVALCPUS"
echo "Max SOCKETS - $MAXVALSOCKETS"
echo "Max CORES - $MAXVALCPS"
echo "Max THREADS - $MAXVALTHREADS"
echo " "
echo "Configuring system with TOPOLOGY $TOPOLOGY"
echo "Maximum CPUS - $MAXCPUS"
echo "Maximum SOCKETS - $MAXSOCKETS"
echo "Maximum CORES - $MAXCORES"
echo "Maximum THREADS - $MAXTHREADS"
echo " "
echo "Calculated topology:"
calculated_max_cpus=$(expr $MAXSOCKETS \* $MAXCORES \* $MAXTHREADS)
[ $MAXCPUS -lt $calculated_max_cpus ] && calculated_max_cpus=$MAXCPUS
echo "Maximum CPUS - $calculated_max_cpus"
# how many cpus are there per socket?
cpuspersocket=$(expr $MAXCORES \* $MAXTHREADS)
enable_threads_on_node () {
[ $DEBUG ] && echo "enabled $2 cpus on node $1"
maxcount=$2
NODEPATH=/sys/devices/system/node/node$1
[ ! -e $NODEPATH ] && return
count=0
core=0
for cpu in `seq 0 $MAXVALCPUS`
do
# if this cpu is in enable_cpus then continue to next cpu
[[ " ${enable_cpus[@]} " =~ " ${cpu} " ]] && continue
CPUPATH=$NODEPATH/cpu${cpu}
[ ! -e $CPUPATH ] && continue
# if this cpu's sibling is online and smt disabled
# then continue to next cpu
if [ -e $CPUPATH/topology/thread_siblings_list ]; then
sib=$(cat $CPUPATH/topology/thread_siblings_list | tr , "\n" | grep -vw $cpu)
[ $MAXTHREADS -eq 1 ] && [[ " ${enable_cpus[@]} " =~ " ${sib} " ]] && continue
fi
[ $count -ge $maxcount ] && continue
[ $core -ge $MAXCORES ] && continue
enable_cpus+=( "$cpu" )
((count++))
((core++))
[ $DEBUG ] && echo "node = $1 cpu = $cpu sib = $sib count = $count core = $core"
if [ -e $CPUPATH/topology/thread_siblings_list ]; then
[ $count -ge $maxcount ] && continue
[ $MAXTHREADS -eq 1 ] && continue
enable_cpus+=( "$sib" )
((count++))
fi
done
}
dense () {
for node in `seq 0 $MAXSOCKETS`
do
if [ $remainingcpus -ge $cpuspersocket ]; then
enable_threads_on_node $node $cpuspersocket
else
enable_threads_on_node $node $remainingcpus
fi
remainingcpus=$(expr $remainingcpus - $cpuspersocket)
done
}
sparse () {
while [ $remainingcpus -ge 0 ];
do
for node in `seq 0 $MAXSOCKETS`
do
[ $remainingcpus -gt 1 ] && enable_threads_on_node $node 2
[ $remainingcpus -eq 1 ] && enable_threads_on_node $node 1
remainingcpus=$(expr $remainingcpus - 2)
done
done
}
((MAXSOCKETS--))
remainingcpus=$calculated_max_cpus
[ $DEBUG ] && [ ! "$TOPOLOGY" ] && echo "Error: Must specify --topology with --debug." && exit 1
topology_valid=
[ $TOPOLOGY == "SPARSE" ] && topology_valid=1 && sparse
[ $TOPOLOGY == "DENSE" ] && topology_valid=1 && dense
[ ! $topology_valid ] && echo "Error: Topology value $TOPOLOGY invalid." && exit 1
if [ $DEBUG ]; then
count=0
for cpu in ${enable_cpus[@]}; do
((count++))
echo $count $cpu
done
fi
[ ${#enable_cpus[@]} -eq 0 ] && echo "Error: Resulting topology results in no online cpus." && exit 1
[ $DEBUG ] && exit
for cpu in `seq 0 $MAXVALCPUS`
do
if [[ " ${enable_cpus[@]} " =~ " ${cpu} " ]]; then
cpustatus $cpu 1
else
cpustatus $cpu 0
fi
done