smtoff revision 1.1
1#!/bin/sh
2#
3# $NetBSD: smtoff,v 1.1 2019/05/11 19:31:03 maxv 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
13name="smtoff"
14rcvar=$name
15
16start_cmd="smtoff_start"
17stop_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#
30GetSmtId() {
31	smtid=$(cpuctl identify $1 | grep "SMT ID" | cut -d " " -f 4)
32	case $smtid in
33		[0-9]*)
34			echo "$smtid" ;;
35		*)
36			echo "error" ;;
37	esac
38}
39
40#
41# The format of the output is:
42#
43#     hw.ncpu = 80
44#
45# Return the value.
46#
47CountCPUs() {
48	ncpus=$(sysctl hw.ncpu | cut -d " " -f 3)
49	echo "$ncpus"
50}
51
52# ------------------------------------------------------------------------------
53
54#
55# Disable SMT. We skip cpu0.
56#
57smtoff_start()
58{
59	ncpus=$(CountCPUs)
60	i=1
61
62	while [ $i -lt $ncpus ]
63	do
64		smtid=$(GetSmtId "$i")
65
66		# Didn't get the ID? Then maybe no SMT.
67		if [ "$smtid" = "error" ]; then
68			i=$(($i+1))
69			continue
70		fi
71
72		# The first thread is never disabled.
73		if [ $smtid -eq 0 ]; then
74			i=$(($i+1))
75			continue
76		fi
77
78		cmd="cpuctl offline $i"
79		$cmd
80		i=$(($i+1))
81	done
82}
83
84#
85# Enable SMT. We basically turn on each CPU.
86#
87smtoff_stop()
88{
89	ncpus=$(CountCPUs)
90	i=1
91
92	while [ $i -lt $ncpus ]
93	do
94		cmd="cpuctl online $i"
95		$cmd
96		i=$(($i+1))
97	done
98}
99
100load_rc_config $name
101run_rc_command "$1"
102