Home | History | Annotate | Line # | Download | only in rc.d
smtoff revision 1.3
      1 #!/bin/sh
      2 #
      3 # $NetBSD: smtoff,v 1.3 2019/05/12 11:55:47 kre Exp $
      4 #
      5 # Public Domain.
      6 #
      7 
      8 # PROVIDE: smtoff
      9 # REQUIRE: root bootconf mountcritlocal tty
     10 
     11 $_rc_subr_loaded . /etc/rc.subr
     12 
     13 name="smtoff"
     14 rcvar=$name
     15 
     16 start_cmd="smtoff_start"
     17 stop_cmd="smtoff_stop"
     18 
     19 # ------------------------------------------------------------------------------
     20 
     21 #
     22 # The format of the output is:
     23 #
     24 #     ...
     25 #     cpu0: SMT ID 1
     26 #     ...
     27 #
     28 # Return the value.
     29 #
     30 GetSmtId() {
     31 	cpuctl identify "$1" |
     32 	while read cpuN smt id N junk
     33 	do
     34 		test -n "$junk" && continue
     35 
     36 		case "${smt} ${id}" in
     37 		'SMT ID')
     38 			case "$N" in
     39 			[0-9]|[1-9][0-9]|[1-9][0-9]*[0-9])
     40 				printf %s "$N"
     41 				return
     42 				;;
     43 			esac
     44 			;;
     45 		esac
     46 	done
     47 }
     48 
     49 #
     50 # The format of the output (without -n) would be:
     51 #
     52 #     hw.ncpu = 80
     53 #
     54 # so use -n to make life easy
     55 #
     56 # Return the value.
     57 #
     58 CountCPUs() {
     59 	sysctl -n hw.ncpu
     60 }
     61 
     62 # ------------------------------------------------------------------------------
     63 
     64 #
     65 # Disable SMT. We skip cpu0.
     66 #
     67 smtoff_start()
     68 {
     69 	ncpus=$(CountCPUs)
     70 	i=1
     71 
     72 	while [ "$i" -lt "$ncpus" ]
     73 	do
     74 		smtid=$(GetSmtId "$i" 2>/dev/null)
     75 
     76 		case "$smtid" in
     77 		'')			# Didn't get the ID? Then maybe no SMT.
     78 			;;
     79 
     80 		0)			# The first thread is never disabled.
     81 			;;
     82 
     83 		*)
     84 			cpuctl offline "$i"
     85 			;;
     86 		esac
     87 
     88 		i=$(($i+1))
     89 	done
     90 }
     91 
     92 #
     93 # Enable SMT. We basically turn on each CPU.
     94 #
     95 smtoff_stop()
     96 {
     97 	ncpus=$(CountCPUs)
     98 	i=1
     99 
    100 	while [ "$i" -lt "$ncpus" ]
    101 	do
    102 		cpuctl online "$i"
    103 		i=$(($i+1))
    104 	done
    105 }
    106 
    107 load_rc_config $name
    108 run_rc_command "$1"
    109