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
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	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#
58CountCPUs() {
59	sysctl -n hw.ncpu
60}
61
62# ------------------------------------------------------------------------------
63
64#
65# Disable SMT. We skip cpu0.
66#
67smtoff_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#
95smtoff_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
107load_rc_config $name
108run_rc_command "$1"
109