|
| 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" |
0 commit comments