summaryrefslogtreecommitdiff
path: root/etc/rc.d/smtoff
blob: aa015ea5d86929e876d24e64091eb70327ae73eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
#
# $NetBSD: smtoff,v 1.6 2020/09/08 12:52:18 martin Exp $
#
# Public Domain.
#

# PROVIDE: smtoff
# REQUIRE: root bootconf CRITLOCALMOUNTED tty

$_rc_subr_loaded . /etc/rc.subr

name="smtoff"
rcvar=$name

start_cmd="smtoff_start"
stop_cmd="smtoff_stop"

# ------------------------------------------------------------------------------

#
# The format of the output is:
#
#     ...
#     cpu0: SMT ID 1
#     ...
#
# Return the value.
#
GetSmtId() {
	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
}

CountCPUs() {
	sysctl -n hw.ncpu
}

# ------------------------------------------------------------------------------

#
# Disable SMT. We skip cpu0.
#
smtoff_start()
{
	ncpus=$(CountCPUs)
	i=1

	while [ "$i" -lt "$ncpus" ]
	do
		smtid=$(GetSmtId "$i" 2>/dev/null)

		case "$smtid" in
		'')			# Didn't get the ID? Then maybe no SMT.
			;;

		0)			# The first thread is never disabled.
			;;

		*)
			cpuctl offline "$i"
			;;
		esac

		i=$(($i+1))
	done
}

#
# Enable SMT. We basically turn on each CPU.
#
smtoff_stop()
{
	ncpus=$(CountCPUs)
	i=1

	while [ "$i" -lt "$ncpus" ]
	do
		cpuctl online "$i"
		i=$(($i+1))
	done
}

load_rc_config $name
run_rc_command "$1"