Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/etc/rc.d Restructure code a little.



details:   https://anonhg.NetBSD.org/src/rev/fd5b3a626e48
branches:  trunk
changeset: 841397:fd5b3a626e48
user:      kre <kre%NetBSD.org@localhost>
date:      Sun May 12 00:24:03 2019 +0000

description:
Restructure code a little.

Use quoting everywhere possibly useful (always the right way, except
in the few cases where it is wrong...)

Avoid using cut & grep (from /usr/bin) so script could run before /usr
is mounted (pity cpuctl is in /usr/sbin ...).

Use sysctl -n rather than attempting to parse its output.

diffstat:

 etc/rc.d/smtoff |  64 ++++++++++++++++++++++++++++++++------------------------
 1 files changed, 36 insertions(+), 28 deletions(-)

diffs (108 lines):

diff -r 987faeb749ec -r fd5b3a626e48 etc/rc.d/smtoff
--- a/etc/rc.d/smtoff   Sat May 11 19:31:03 2019 +0000
+++ b/etc/rc.d/smtoff   Sun May 12 00:24:03 2019 +0000
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $NetBSD: smtoff,v 1.1 2019/05/11 19:31:03 maxv Exp $
+# $NetBSD: smtoff,v 1.2 2019/05/12 00:24:03 kre Exp $
 #
 # Public Domain.
 #
@@ -28,25 +28,36 @@
 # Return the value.
 #
 GetSmtId() {
-       smtid=$(cpuctl identify $1 | grep "SMT ID" | cut -d " " -f 4)
-       case $smtid in
-               [0-9]*)
-                       echo "$smtid" ;;
-               *)
-                       echo "error" ;;
-       esac
+       cpuctl identify "$1" |
+       while read cpuN smt id N junk
+       do
+               test -n "$junk" && continue
+
+               case "${smt} ${id}" in
+               'SMT ID')
+                       case "$N" in
+                       [0-9]|[1-9][0-9]|[1-9][0-9]*[0-9])
+                               printf %s "$N"
+                               return
+                               ;;
+                       esac
+                       ;;
+               esac
+       done
+       printf "error"
 }
 
 #
-# The format of the output is:
+# The format of the output (without -n) would be:
 #
 #     hw.ncpu = 80
 #
+# so use -n to make life easy
+#
 # Return the value.
 #
 CountCPUs() {
-       ncpus=$(sysctl hw.ncpu | cut -d " " -f 3)
-       echo "$ncpus"
+       sysctl -n hw.ncpu
 }
 
 # ------------------------------------------------------------------------------
@@ -59,24 +70,22 @@
        ncpus=$(CountCPUs)
        i=1
 
-       while [ $i -lt $ncpus ]
+       while [ "$i" -lt "$ncpus" ]
        do
-               smtid=$(GetSmtId "$i")
+               smtid=$(GetSmtId "$i" 2>/dev/null)
+
+               case "$smtid" in
+               error)                  # Didn't get the ID? Then maybe no SMT.
+                       ;;
 
-               # Didn't get the ID? Then maybe no SMT.
-               if [ "$smtid" = "error" ]; then
-                       i=$(($i+1))
-                       continue
-               fi
+               0)                      # The first thread is never disabled.
+                       ;;
 
-               # The first thread is never disabled.
-               if [ $smtid -eq 0 ]; then
-                       i=$(($i+1))
-                       continue
-               fi
+               *)
+                       cpuctl offline "$i"
+                       ;;
+               esac
 
-               cmd="cpuctl offline $i"
-               $cmd
                i=$(($i+1))
        done
 }
@@ -89,10 +98,9 @@
        ncpus=$(CountCPUs)
        i=1
 
-       while [ $i -lt $ncpus ]
+       while [ "$i" -lt "$ncpus" ]
        do
-               cmd="cpuctl online $i"
-               $cmd
+               cpuctl online "$i"
                i=$(($i+1))
        done
 }



Home | Main Index | Thread Index | Old Index