Repository navigation
Expand file tree
/
Copy pathcpu.bash
More file actions
221 lines (192 loc) · 5.2 KB
/
Copy pathcpu.bash
File metadata and controls
221 lines (192 loc) · 5.2 KB
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
# Copyright (c) 2014-2016, 2021, Cody Opel <cwopel@chlorm.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# lscpu
# FIXME: drop usage of lscpu, parse cpuinfo flags instead
# Return CPU architecture without endianness or address space size
# shellcheck disable=SC2120
stl_cpu_arch() {
#stl_func_reqargs '0' "$#"
# Do NOT use `uname -m' to achieve this functionality.
local arch
local -a cpuinfo=()
local i
local -a cpuFlags=()
mapfile -t cpuinfo < <(command lscpu)
for i in "${cpuinfo[@]}"; do
if [[ $i =~ ^'Architecture:' ]]; then
arch="$i"
break
fi
done
shopt -s nocasematch
case "$arch" in
*i686* | *x86_64*)
arch='x86'
;;
*power*)
arch='power'
;;
*arm*)
arch='arm'
;;
*)
# Null variable to return error
arch=
;;
esac
shopt -u nocasematch
# FIXME: implement better error message here
stl_type_str "$arch"
echo "$arch"
}
# Find CPU address space size (ie. 32bit/64bit)
stl_cpu_address_space() {
stl_func_reqargs '0' "$#"
local addressSpace=32
local cpuFlags
local -a cpuinfo=()
local -r prefix='flags'
local i
mapfile -t cpuinfo </proc/cpuinfo
for i in "${cpuinfo[@]}"; do
if [[ $i =~ ^'flags' ]]; then
cpuFlags="$i"
break
fi
done
if [[ "$cpuFlags" == *' lm '* ]]; then
addressSpace=64
fi
stl_type_int "$addressSpace"
echo "$addressSpace"
}
# shellcheck disable=SC2120
stl_cpu_sockets() {
stl_func_reqargs '0' "$#"
local i
local -a cpuinfo=()
local sockets
mapfile -t cpuinfo </proc/cpuinfo
for i in "${cpuinfo[@]}"; do
if [[ $i =~ ^'Socket(s):' ]]; then
sockets="$i"
break
fi
done
sockets=${sockets//[^0-9]/}
if [ ! $sockets -ge 1 ]; then
# Assume a socket exists even if it fails to find any
sockets=1
fi
echo "$sockets"
}
# FIXME: needs testing for big/little, currently assumes all sockets identical
# shellcheck disable=SC2120
stl_cpu_cores() {
stl_func_reqargs '0' "$#"
local i
local cores
local -a cpuinfo=()
mapfile -t cpuinfo </proc/cpuinfo
for i in "${cpuinfo[@]}"; do
if [[ $i =~ ^'Core(s) per socket:' ]]; then
cores="$i"
break
fi
done
cores=${cores//[^0-9]/}
if [ -z "$cores" ]; then
cores=1
else
cores=$(($cores * $(stl_cpu_sockets)))
fi
echo "$cores"
}
stl_cpu_logical() {
stl_func_reqargs '0' "$#"
# Assumes all sockets are identical, only some arm platforms won't
# work with this logic
local i
local CpuThreads
local -a cpuinfo=()
# Finds number of logical threads per physical core
mapfile -t cpuinfo </proc/cpuinfo
for i in "${cpuinfo[@]}"; do
if [[ $i =~ ^'Thread(s) per core:' ]]; then
CpuThreads="$i"
break
fi
done
CpuThreads=${CpuThreads//[^0-9]/}
if stl_type_int $CpuThreads 2>&-; then
# Convert to number of threads per cpu
CpuThreads=$(($CpuThreads * $(stl_cpu_physical)))
fi
CpuThreads=${CpuThreads//[^0-9]/}
if stl_type_null $CpuThreads 2>&-; then
CpuThreads=$(stl_cpu_physical)
else
CpuThreads=$(($CpuThreads * $(stl_cpu_sockets)))
fi
echo "$CpuThreads"
}
stl_cpu_flags() {
stl_func_reqargs '0' "$#"
local -a cpuinfo=()
local i
local cpuFlags=
local prefix='flags'
if [ "$(stl_cpu_arch)" == 'arm' ]; then
prefix='Features'
fi
mapfile -t cpuinfo </proc/cpuinfo
for i in "${cpuinfo[@]}"; do
if [[ $i =~ ^$prefix ]]; then
cpuFlags="$i"
break
fi
done
# Split out the flags: prefix
mapfile -t -d: cpuinfo < <(printf '%s\n' "$cpuFlags")
# Split the idividual flags
mapfile -t -d' ' cpuinfo < <(echo "${cpuinfo[1]}")
for i in "${cpuinfo[@]}"; do
if [ stl_type_str "$i" ] 2>&-; then
printf '%s\n' "${i//[$'\t\r\n']/}"
fi
done
}
stl_cpu_x86_level() {
stl_func_reqargs '0' "$#"
local -A levels=(
[1]='cmov,cx8,fpu,fxsr,mmx,lm,sse2,syscall'
[2]='cx16,lahf_lm,popcnt,sse4_1,sse4_2,ssse3'
[3]='avx512bw,avx512cd,avx512dq,avx512f,avx512vl'
)
local -a cpuFlags=()
local l
local r
local -i level=0
mapfile -t cpuFlags < <(stl_cpu_flags)
for l in 1 2 3; do
mapfile -t -d, reqs < <(printf '%s' "${levels[$l]}")
for r in "${reqs[@]}"; do
if ! stl_list_contains "$r" "${cpuFlags[@]}"; then
echo $level
return 0
fi
done
level=$(($level + 1))
done
}