config_cpu: use lscpu for identifying CPU information
authorMichael Prokop <mika@grml.org>
Fri, 8 Sep 2023 06:32:21 +0000 (08:32 +0200)
committerMichael Prokop <mika@grml.org>
Fri, 8 Sep 2023 06:35:25 +0000 (08:35 +0200)
On arm64 we don't have the CPU information in /proc/cpuinfo as expected
by our config_cpu, so its output is broken:

| # awk -F: '/^processor/{printf "        Processor"$2" is"};/^model name/{printf $2};/^vendor_id/{printf vendor};/^cpu MHz/{printf " %dMHz",int($2)};/^cache size/{printf ","$2" Cache"};/^$/{print ""}' /proc/cpuinfo
|         Processor 0 is
|         Processor 1 is
| [...]
|         Processor 13 is
|         Processor 14 is
|         Processor 15 is

FTR:

| # head /proc/cpuinfo
| processor       : 0
| BogoMIPS        : 50.00
| Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
| CPU implementer : 0x41
| CPU architecture: 8
| CPU variant     : 0x3
| CPU part        : 0xd0c
| CPU revision    : 1
|
| processor       : 1

While with lspcu we get the information we're interested in:

| # lscpu | grep 'Model name:'
| Model name:                      Neoverse-N1
| BIOS Model name:                 virt-5.2  CPU @ 2.0GHz

So instead of having some hackish /proc/cpuinfo parsing, let's rely on
util-linux's lscpu(1).

While at it, let's output only the number of present CPUs instead of
listing every single one of them, given that there exist systems with
>100 CPUs nowadays. :)

Thanks: Christopher Bock and AndrĂ¡s Korn for feedback

autoconfig.functions

index 4a8b167..0da044d 100755 (executable)
@@ -905,19 +905,23 @@ fi # -z $INSTALLED
 
 # {{{ CPU-detection
 config_cpu(){
-if checkbootparam 'nocpu'; then
-  ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
-  return 0
-fi
+  if checkbootparam 'nocpu'; then
+    ewarn "Skipping CPU detection as requested on boot commandline." ; eend 0
+    return 0
+  fi
 
-if [[ $(grep -c processor /proc/cpuinfo) -gt 1 ]] ; then
-   einfo "Found CPU:"
-   CPU=$(awk -F: '/^processor/{printf "        Processor"$2" is"};/^model name/{printf $2};/^vendor_id/{printf vendor};/^cpu MHz/{printf " %dMHz",int($2)};/^cache size/{printf ","$2" Cache"};/^$/{print ""}' /proc/cpuinfo 2>>$DEBUG)
-   echo $CPU | sed 's/ \{1,\}/ /g'
-   eend 0
-else
-   einfo "Found CPU: `awk -F: '/^processor/{printf " Processor"$2" is"};/^model name/{printf $2};/^vendor_id/{printf vendor};/^cpu MHz/{printf " %dMHz",int($2)};/^cache size/{printf ","$2" Cache"};/^$/{print ""}' /proc/cpuinfo 2>>$DEBUG` " ; eend 0
-fi
+  if ! [ -x "$(which lscpu)" ] ; then
+    ewarn "Skipping CPU detection due to lack of lscpu."; eend 0
+    return 0
+  fi
+
+  local cpu_info num_cpus
+
+  cpu_info="$(lscpu | sed -n '/^Model name:/s/[^:]*:\s*//p')"
+  num_cpus=$(grep -c processor /proc/cpuinfo)
+
+  einfo "Found ${num_cpus} CPU(s): ${cpu_info}"
+  eend 0
 }
 # }}}