Module Name:    src
Committed By:   martin
Date:           Tue May 14 11:33:43 UTC 2019

Modified Files:
        src/distrib/sets/lists/etc [netbsd-8]: mi
        src/etc/defaults [netbsd-8]: rc.conf
        src/etc/mtree [netbsd-8]: special
        src/etc/rc.d [netbsd-8]: Makefile
        src/share/man/man5 [netbsd-8]: rc.conf.5
        src/usr.sbin/postinstall [netbsd-8]: postinstall
Added Files:
        src/etc/rc.d [netbsd-8]: smtoff

Log Message:
Pull up following revision(s) (requested by maxv in ticket #1265):
        etc/rc.d/smtoff: revision 1.1
        etc/rc.d/smtoff: revision 1.2
        distrib/sets/lists/etc/mi: revision 1.258
        etc/rc.d/smtoff: revision 1.3
        etc/rc.d/smtoff: revision 1.4
        etc/defaults/rc.conf: revision 1.148
        etc/rc.d/Makefile: revision 1.103
        usr.sbin/postinstall/postinstall: revision 1.226
        etc/rc.d/Makefile: revision 1.104
        etc/mtree/special: revision 1.167
        share/man/man5/rc.conf.5: revision 1.180

Add smtoff, an rc.d script that disables Simultaneous Multi-Threading. It
parses the output of cpuctl, and executes "cpuctl offline" for each CPU
that has SmtID!=0.

The default is "smtoff=NO", which means that SMT remains enabled.
Restructure code a little.

Use quoting everywhere possibly useful (always the right way, except
in the few cases where it is wrong...)

Avoid using cut & grep (from /usr/bin) so script could run before /usr
is mounted (pity cpuctl is in /usr/sbin ...).

Use sysctl -n rather than attempting to parse its output.
install rc.d

No change...   Previous log message should have said:
  Install rc.d/smtoff

I should know better!   Don't rely upon the way the shell implements
pipes.   Skip the "error" printf from GetSmtId() as there is no easy
portable way to avoid it occurring (there are complicated ways) - but
we don't need it, there is no logical difference between "error" and ""
so just use the latter (if we get an ID, good, if there is nothing, then
there is none - saying 'error' does not mean anything.)

Remove comment, since there is no parsing anymore.


To generate a diff of this commit:
cvs rdiff -u -r1.247 -r1.247.2.1 src/distrib/sets/lists/etc/mi
cvs rdiff -u -r1.139 -r1.139.6.1 src/etc/defaults/rc.conf
cvs rdiff -u -r1.160 -r1.160.4.1 src/etc/mtree/special
cvs rdiff -u -r1.95 -r1.95.6.1 src/etc/rc.d/Makefile
cvs rdiff -u -r0 -r1.4.2.2 src/etc/rc.d/smtoff
cvs rdiff -u -r1.166 -r1.166.6.1 src/share/man/man5/rc.conf.5
cvs rdiff -u -r1.214.4.1 -r1.214.4.2 src/usr.sbin/postinstall/postinstall

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/etc/mi
diff -u src/distrib/sets/lists/etc/mi:1.247 src/distrib/sets/lists/etc/mi:1.247.2.1
--- src/distrib/sets/lists/etc/mi:1.247	Sun May 21 15:28:38 2017
+++ src/distrib/sets/lists/etc/mi	Tue May 14 11:33:42 2019
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.247 2017/05/21 15:28:38 riastradh Exp $
+# $NetBSD: mi,v 1.247.2.1 2019/05/14 11:33:42 martin Exp $
 #
 # Note: end-user configuration files that are moved to another location
 #	should not be marked "obsolete"; they should just be removed from
@@ -283,6 +283,7 @@
 ./etc/rc.d/screenblank				etc-sys-rc
 ./etc/rc.d/sdpd					etc-obsolete		obsolete
 ./etc/rc.d/securelevel				etc-sys-rc
+./etc/rc.d/smtoff				etc-sys-rc
 ./etc/rc.d/sshd					etc-secsh-rc
 ./etc/rc.d/staticroute				etc-sys-rc
 ./etc/rc.d/swap1				etc-sys-rc

Index: src/etc/defaults/rc.conf
diff -u src/etc/defaults/rc.conf:1.139 src/etc/defaults/rc.conf:1.139.6.1
--- src/etc/defaults/rc.conf:1.139	Sat Jan  7 20:00:33 2017
+++ src/etc/defaults/rc.conf	Tue May 14 11:33:42 2019
@@ -1,4 +1,4 @@
-#	$NetBSD: rc.conf,v 1.139 2017/01/07 20:00:33 christos Exp $
+#	$NetBSD: rc.conf,v 1.139.6.1 2019/05/14 11:33:42 martin Exp $
 #
 # /etc/defaults/rc.conf --
 #	default configuration of /etc/rc.conf
@@ -380,6 +380,9 @@ random_seed=YES
 # Creating / updating of man page index on boot
 makemandb=YES
 
+# Disable Simultaneous Multi-Threading
+smtoff=NO
+
 # blacklist daemon, needs npf
 blacklistd=NO
 

Index: src/etc/mtree/special
diff -u src/etc/mtree/special:1.160 src/etc/mtree/special:1.160.4.1
--- src/etc/mtree/special:1.160	Mon Feb 20 13:45:43 2017
+++ src/etc/mtree/special	Tue May 14 11:33:43 2019
@@ -1,4 +1,4 @@
-#	$NetBSD: special,v 1.160 2017/02/20 13:45:43 christos Exp $
+#	$NetBSD: special,v 1.160.4.1 2019/05/14 11:33:43 martin Exp $
 #	@(#)special	8.2 (Berkeley) 1/23/94
 #
 # This file may be overwritten on upgrades.
@@ -281,6 +281,7 @@
 ./etc/rc.d/savecore		type=file mode=0555
 ./etc/rc.d/screenblank		type=file mode=0555
 ./etc/rc.d/securelevel		type=file mode=0555
+./etc/rc.d/smtoff		type=file mode=0555
 ./etc/rc.d/sshd			type=file mode=0555
 ./etc/rc.d/staticroute		type=file mode=0555
 ./etc/rc.d/swap1		type=file mode=0555

Index: src/etc/rc.d/Makefile
diff -u src/etc/rc.d/Makefile:1.95 src/etc/rc.d/Makefile:1.95.6.1
--- src/etc/rc.d/Makefile:1.95	Thu Jan  5 14:39:09 2017
+++ src/etc/rc.d/Makefile	Tue May 14 11:33:42 2019
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.95 2017/01/05 14:39:09 christos Exp $
+# $NetBSD: Makefile,v 1.95.6.1 2019/05/14 11:33:42 martin Exp $
 
 .include <bsd.own.mk>
 
@@ -35,7 +35,7 @@ CONFIGFILES=\
 		racoon rpcbind raidframe raidframeparity random_seed rarpd \
 		rbootd resize_root rndctl root route6d routed rtadvd \
 		rtclocaltime rwho \
-		savecore screenblank securelevel sshd \
+		savecore screenblank securelevel smtoff sshd \
 		staticroute swap1 swap2 sysctl sysdb syslogd \
 		timed tpctl ttys \
 		veriexec virecover wdogctl wpa_supplicant wscons wsmoused \

Index: src/share/man/man5/rc.conf.5
diff -u src/share/man/man5/rc.conf.5:1.166 src/share/man/man5/rc.conf.5:1.166.6.1
--- src/share/man/man5/rc.conf.5:1.166	Sat Dec 17 07:44:59 2016
+++ src/share/man/man5/rc.conf.5	Tue May 14 11:33:43 2019
@@ -1,4 +1,4 @@
-.\"	$NetBSD: rc.conf.5,v 1.166 2016/12/17 07:44:59 maya Exp $
+.\"	$NetBSD: rc.conf.5,v 1.166.6.1 2019/05/14 11:33:43 martin Exp $
 .\"
 .\" Copyright (c) 1996 Matthew R. Green
 .\" All rights reserved.
@@ -55,7 +55,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 17, 2016
+.Dd May 14, 2019
 .Dt RC.CONF 5
 .Os
 .Sh NAME
@@ -621,6 +621,9 @@ A string.
 Flags to pass to the
 .Nm veriexecctl
 command.
+.It Sy smtoff
+Boolean value.
+Disables SMT (Simultaneous Multi-Threading).
 .El
 .Ss Networking startup
 .Bl -tag -width net_interfaces

Index: src/usr.sbin/postinstall/postinstall
diff -u src/usr.sbin/postinstall/postinstall:1.214.4.1 src/usr.sbin/postinstall/postinstall:1.214.4.2
--- src/usr.sbin/postinstall/postinstall:1.214.4.1	Mon Jul 10 13:06:45 2017
+++ src/usr.sbin/postinstall/postinstall	Tue May 14 11:33:43 2019
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $NetBSD: postinstall,v 1.214.4.1 2017/07/10 13:06:45 martin Exp $
+# $NetBSD: postinstall,v 1.214.4.2 2019/05/14 11:33:43 martin Exp $
 #
 # Copyright (c) 2002-2015 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -1457,6 +1457,7 @@ rwho
 savecore
 screenblank
 securelevel
+smtoff
 sshd
 staticroute
 swap1

Added files:

Index: src/etc/rc.d/smtoff
diff -u /dev/null src/etc/rc.d/smtoff:1.4.2.2
--- /dev/null	Tue May 14 11:33:43 2019
+++ src/etc/rc.d/smtoff	Tue May 14 11:33:42 2019
@@ -0,0 +1,99 @@
+#!/bin/sh
+#
+# $NetBSD: smtoff,v 1.4.2.2 2019/05/14 11:33:42 martin Exp $
+#
+# Public Domain.
+#
+
+# PROVIDE: smtoff
+# REQUIRE: root bootconf mountcritlocal 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"

Reply via email to