1 #!/bin/sh 2 # 3 # $NetBSD: smtoff,v 1.2 2019/05/12 00:24:03 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 printf "error" 48 } 49 50 # 51 # The format of the output (without -n) would be: 52 # 53 # hw.ncpu = 80 54 # 55 # so use -n to make life easy 56 # 57 # Return the value. 58 # 59 CountCPUs() { 60 sysctl -n hw.ncpu 61 } 62 63 # ------------------------------------------------------------------------------ 64 65 # 66 # Disable SMT. We skip cpu0. 67 # 68 smtoff_start() 69 { 70 ncpus=$(CountCPUs) 71 i=1 72 73 while [ "$i" -lt "$ncpus" ] 74 do 75 smtid=$(GetSmtId "$i" 2>/dev/null) 76 77 case "$smtid" in 78 error) # Didn't get the ID? Then maybe no SMT. 79 ;; 80 81 0) # The first thread is never disabled. 82 ;; 83 84 *) 85 cpuctl offline "$i" 86 ;; 87 esac 88 89 i=$(($i+1)) 90 done 91 } 92 93 # 94 # Enable SMT. We basically turn on each CPU. 95 # 96 smtoff_stop() 97 { 98 ncpus=$(CountCPUs) 99 i=1 100 101 while [ "$i" -lt "$ncpus" ] 102 do 103 cpuctl online "$i" 104 i=$(($i+1)) 105 done 106 } 107 108 load_rc_config $name 109 run_rc_command "$1" 110