head	1.6;
access;
symbols
	perseant-exfatfs-base-20261102:1.6
	netbsd-11-1-RELEASE:1.6
	netbsd-10-2-RELEASE:1.6
	netbsd-9-5-RELEASE:1.4
	netbsd-11-0-RELEASE:1.6
	netbsd-11-0-RC7:1.6
	netbsd-11-0-RC6:1.6
	netbsd-11-0-RC5:1.6
	netbsd-11-0-RC4:1.6
	netbsd-11-0-RC3:1.6
	netbsd-11-0-RC2:1.6
	netbsd-11-0-RC1:1.6
	perseant-exfatfs-base-20250801:1.6
	netbsd-11:1.6.0.10
	netbsd-11-base:1.6
	netbsd-10-1-RELEASE:1.6
	perseant-exfatfs-base-20240630:1.6
	perseant-exfatfs:1.6.0.8
	perseant-exfatfs-base:1.6
	netbsd-8-3-RELEASE:1.4.2.2
	netbsd-9-4-RELEASE:1.4
	netbsd-10-0-RELEASE:1.6
	netbsd-10-0-RC6:1.6
	netbsd-10-0-RC5:1.6
	netbsd-10-0-RC4:1.6
	netbsd-10-0-RC3:1.6
	netbsd-10-0-RC2:1.6
	netbsd-10-0-RC1:1.6
	netbsd-10:1.6.0.6
	netbsd-10-base:1.6
	netbsd-9-3-RELEASE:1.4
	cjep_sun2x-base1:1.6
	cjep_sun2x:1.6.0.4
	cjep_sun2x-base:1.6
	cjep_staticlib_x-base1:1.6
	netbsd-9-2-RELEASE:1.4
	cjep_staticlib_x:1.6.0.2
	cjep_staticlib_x-base:1.6
	netbsd-9-1-RELEASE:1.4
	phil-wifi-20200421:1.4
	phil-wifi-20200411:1.4
	is-mlppp:1.4.0.8
	is-mlppp-base:1.4
	phil-wifi-20200406:1.4
	netbsd-8-2-RELEASE:1.4.2.2
	netbsd-9-0-RELEASE:1.4
	netbsd-9-0-RC2:1.4
	netbsd-9-0-RC1:1.4
	phil-wifi-20191119:1.4
	netbsd-9:1.4.0.6
	netbsd-9-base:1.4
	phil-wifi:1.4.0.4
	phil-wifi-20190609:1.4
	netbsd-8-1-RELEASE:1.4.2.2
	netbsd-8-1-RC1:1.4.2.2
	netbsd-8:1.4.0.2;
locks; strict;
comment	@# @;


1.6
date	2020.09.08.12.52.18;	author martin;	state Exp;
branches;
next	1.5;
commitid	IL2vGNw0FVFgcfnC;

1.5
date	2020.07.22.16.50.41;	author martin;	state Exp;
branches;
next	1.4;
commitid	ZlFyZCKhX3U036hC;

1.4
date	2019.05.13.18.53.10;	author maxv;	state Exp;
branches
	1.4.2.1
	1.4.4.1;
next	1.3;
commitid	Z3Hjcfl4jIBIK4nB;

1.3
date	2019.05.12.11.55.47;	author kre;	state Exp;
branches;
next	1.2;
commitid	DUOYJJQDZ6wBsUmB;

1.2
date	2019.05.12.00.24.03;	author kre;	state Exp;
branches;
next	1.1;
commitid	nXnB9SHF1cBkDQmB;

1.1
date	2019.05.11.19.31.03;	author maxv;	state Exp;
branches;
next	;
commitid	jcEOXkXQLMTD1PmB;

1.4.2.1
date	2019.05.13.18.53.10;	author martin;	state dead;
branches;
next	1.4.2.2;
commitid	0aZwwTN2YORZhanB;

1.4.2.2
date	2019.05.14.11.33.42;	author martin;	state Exp;
branches;
next	;
commitid	0aZwwTN2YORZhanB;

1.4.4.1
date	2019.05.13.18.53.10;	author christos;	state dead;
branches;
next	1.4.4.2;
commitid	jtc8rnCzWiEEHGqB;

1.4.4.2
date	2019.06.10.21.42.45;	author christos;	state Exp;
branches;
next	;
commitid	jtc8rnCzWiEEHGqB;


desc
@@


1.6
log
@Rename MOUNTCRITLOCAL to CRITLOCALMOUNTED to avoid a name collision
on case insensitive file systems
@
text
@#!/bin/sh
#
# $NetBSD: smtoff,v 1.5 2020/07/22 16:50:41 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"
@


1.5
log
@Split the local disk availability step into two phases to allow scripts
that pre-populate parts of the system (e.g. a tmpfs based /var) an
easy place to plug in like:

# REQUIRE: mountcritlocal
# BEFORE: MOUNTCRITLOCAL

This also cleans up the existing special handling a bit by separating it
into new scripts. All later scripts now depend on MOUNTCRITLOCAL.
Discussed on tech-userlevel some time ago.
@
text
@d3 1
a3 1
# $NetBSD: smtoff,v 1.4 2019/05/13 18:53:10 maxv Exp $
d9 1
a9 1
# REQUIRE: root bootconf MOUNTCRITLOCAL tty
@


1.4
log
@Remove comment, since there is no parsing anymore.
@
text
@d3 1
a3 1
# $NetBSD: smtoff,v 1.3 2019/05/12 11:55:47 kre Exp $
d9 1
a9 1
# REQUIRE: root bootconf mountcritlocal tty
@


1.4.4.1
log
@file smtoff was added on branch phil-wifi on 2019-06-10 21:42:45 +0000
@
text
@d1 99
@


1.4.4.2
log
@Sync with HEAD
@
text
@a0 99
#!/bin/sh
#
# $NetBSD: smtoff,v 1.4 2019/05/13 18:53:10 maxv 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"
@


1.4.2.1
log
@file smtoff was added on branch netbsd-8 on 2019-05-14 11:33:42 +0000
@
text
@d1 99
@


1.4.2.2
log
@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.
@
text
@a0 99
#!/bin/sh
#
# $NetBSD: smtoff,v 1.1 2019/05/11 19:31:03 maxv 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"
@


1.3
log
@
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.)
@
text
@d3 1
a3 1
# $NetBSD: smtoff,v 1.2 2019/05/12 00:24:03 kre Exp $
a48 9
#
# The format of the output (without -n) would be:
#
#     hw.ncpu = 80
#
# so use -n to make life easy
#
# Return the value.
#
@


1.2
log
@
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.
@
text
@d3 1
a3 1
# $NetBSD: smtoff,v 1.1 2019/05/11 19:31:03 maxv Exp $
a46 1
	printf "error"
d77 1
a77 1
		error)			# Didn't get the ID? Then maybe no SMT.
@


1.1
log
@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.
@
text
@d3 1
a3 1
# $NetBSD: smtoff,v 1.1 2019/05/10 18:36:25 maxv Exp $
d31 17
a47 7
	smtid=$(cpuctl identify $1 | grep "SMT ID" | cut -d " " -f 4)
	case $smtid in
		[0-9]*)
			echo "$smtid" ;;
		*)
			echo "error" ;;
	esac
d51 1
a51 1
# The format of the output is:
d55 2
d60 1
a60 2
	ncpus=$(sysctl hw.ncpu | cut -d " " -f 3)
	echo "$ncpus"
d73 1
a73 1
	while [ $i -lt $ncpus ]
d75 5
a79 1
		smtid=$(GetSmtId "$i")
d81 7
a87 11
		# Didn't get the ID? Then maybe no SMT.
		if [ "$smtid" = "error" ]; then
			i=$(($i+1))
			continue
		fi

		# The first thread is never disabled.
		if [ $smtid -eq 0 ]; then
			i=$(($i+1))
			continue
		fi
a88 2
		cmd="cpuctl offline $i"
		$cmd
d101 1
a101 1
	while [ $i -lt $ncpus ]
d103 1
a103 2
		cmd="cpuctl online $i"
		$cmd
@

