Skip to content

Commit 5cc433a

Browse files
Jose Ricardo Zivianityreld
Jose Ricardo Ziviani
authored andcommitted
Add smtstate script to persist SMT changes
This patch introduces smtstate script responsible to save and restore the current SMT state into /var/lib/powerpc-utils/smt.state. Signed-off-by: Jose Ricardo Ziviani <[email protected]> Signed-off-by: Tyrel Datwyler <[email protected]>
1 parent d055cce commit 5cc433a

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

man/smtstate.8

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.\"
2+
.\" Copyright (C) 2020 International Business Machines
3+
.\"
4+
.TH SMTSTATE 8 "January 2020" Linux "Linux on Power Service Tools"
5+
.SH NAME
6+
smtstate \- Save and restore the current SMT state
7+
.SH SYNOPSIS
8+
.B /usr/sbin/smtstate [ option ]
9+
.SH DESCRIPTION
10+
The
11+
.I smtstate
12+
script is used to persist the current SMT state and to restore it at a convenient time. The idea behind it is to use the script together with a systemd service to save SMT state at any shutdown event and to restore it at an early stage of the boot process. Such implementation creates a way to persist the SMT state across reboots, without requiring any other configuration from system administrators.
13+
14+
.SH OPTIONS
15+
.TP
16+
\fB\-\-save\fR
17+
Save the value read from "ppc64_cpu --smt -n" into the state file stored at /var/lib/powerpc-utils/.
18+
19+
.TP
20+
\fB\-\-load\fR
21+
Read the saved SMT value from the state file stored at /var/lib/powerpc-utils/ and set "ppc64_cpu --smt=value".
22+
23+
.TP
24+
\fB\-\-help\fR
25+
Display a concise command usage statement.
26+
27+
.SH AUTHOR
28+
Written by Jose Ricardo Ziviani

scripts/smtstate.in

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
# Copyright (c) 2020 International Business Machines
3+
#
4+
# This program is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU General Public License
6+
# as published by the Free Software Foundation; either version 2
7+
# of the License, or (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program; if not, write to the Free Software
16+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
18+
# Simple script designed to save and restore the SMT state.
19+
20+
readonly SMT_STATE=/var/lib/powerpc-utils/smt.state
21+
readonly SMT_STATE_BKP=/tmp/smt.state.backup
22+
readonly PPC64CPU=@sbindir@/ppc64_cpu
23+
24+
update_state() {
25+
# sanity check
26+
smt_state_exist
27+
28+
# users can online and offline individual CPUs, creating an inconsistent
29+
# SMT state. In this case ppc64_cpu --smt displays a more complex
30+
# configuration, such case is beyond the scope here so we simply ignore it
31+
if [ $($PPC64CPU --smt -n | cut -d= -f2 | grep ^[1-8]$ > /dev/null; echo $?) -ne 0 ]
32+
then
33+
return
34+
fi
35+
36+
# create a temporary backup
37+
cp -p $SMT_STATE $SMT_STATE_BKP
38+
if [ $? -ne 0 ]
39+
then
40+
logger -t ${0##*/}[$$] "Error saving $SMT_STATE_BKP"
41+
exit 1
42+
fi
43+
44+
# update SMT state file
45+
local current_smt=$($PPC64CPU --smt -n | cut -d= -f2)
46+
sed -i "s/SMT_VALUE=[0-9]/SMT_VALUE=$current_smt/" $SMT_STATE
47+
if [ $? -ne 0 ]
48+
then
49+
logger -t ${0##*/}[$$] "Error updating $SMT_STATE"
50+
cp -p $SMT_STATE_BKP $SMT_STATE
51+
rm $SMT_STATE_BKP
52+
exit 1
53+
fi
54+
55+
rm $SMT_STATE_BKP
56+
}
57+
58+
set_smt() {
59+
# sanity check
60+
smt_state_exist
61+
62+
# get saved SMT value and set it
63+
local smt=$(grep "^SMT_VALUE=[1-8]$" $SMT_STATE | cut -d= -f2)
64+
if [ "$smt" -lt 1 ] || [ "$smt" -gt 8 ]
65+
then
66+
logger -t ${0##*/}[$$] "$smt is an invalid SMT state"
67+
exit 1
68+
fi
69+
70+
$PPC64CPU --smt="$smt"
71+
if [ $? -ne 0 ]
72+
then
73+
logger -t ${0##*/}[$$] "Error updating SMT=$smt"
74+
exit 1
75+
fi
76+
}
77+
78+
smt_state_exist() {
79+
if [ ! -f $SMT_STATE ]
80+
then
81+
logger -t ${0##*/}[$$] "$SMT_STATE not found"
82+
exit 1
83+
fi
84+
85+
grep "^SMT_VALUE=[1-8]$" $SMT_STATE > /dev/null 2>&1
86+
if [ $? -ne 0 ]
87+
then
88+
logger -t ${0##*/}[$$] "$SMT_STATE does not have SMT_VALUE defined"
89+
exit 1
90+
fi
91+
}
92+
93+
check_smt_capable() {
94+
local msg=$($PPC64CPU --smt 2>&1 | grep -q "Machine is not SMT capable"; echo $?)
95+
if [ "$msg" -eq 0 ]
96+
then
97+
logger -t ${0##*/}[$$] "Machine is not SMT capable, exiting"
98+
exit 0
99+
fi
100+
}
101+
102+
usage() {
103+
printf "$0 is a script designed to save/restore the SMT state.\n"
104+
printf "Usage: %s [--save | --load | --help]\n" "$0"
105+
printf "\t--save (default): save current SMT state\n"
106+
printf "\t--load : set SMT to the value stored\n"
107+
printf "\t--help : print this message\n"
108+
printf "SMT state is saved in $SMT_STATE\n"
109+
}
110+
111+
main() {
112+
# if machine is not SMT capable don't even bother to continue
113+
check_smt_capable
114+
115+
local action="$1"
116+
117+
case "$action" in
118+
--load)
119+
set_smt
120+
;;
121+
122+
--save)
123+
update_state
124+
;;
125+
126+
--help)
127+
usage
128+
;;
129+
130+
*)
131+
logger -s -t ${0##*/}[$$] "Invalid action $1"
132+
usage
133+
exit 1
134+
;;
135+
esac
136+
}
137+
138+
action=${1:---save}
139+
main "$action"

var/lib/powerpc-utils/smt.state

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# smt.state configures the SMT default value for next
2+
# boots.
3+
4+
# SMT state. If smt_init.service is enabled, this value will be
5+
# used to set SMT in the next boot. If smtd.service is enabled,
6+
# this value will be updated whenever ppc64_cpu --smt=<value>
7+
# is issued.
8+
SMT_VALUE=1

0 commit comments

Comments
 (0)