-
Notifications
You must be signed in to change notification settings - Fork 32
/
cputemp
executable file
·155 lines (142 loc) · 3.89 KB
/
cputemp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
#
# cputemp Show CPU temperatures. MSR specific (currently Intel).
#
# This was written for use in a Xen guest (AWS EC2).
#
# USAGE: cputemp [-hu] [interval [count]]
#
# Temperatures are printed in degrees Celsius, per-CPU.
#
# This uses the CPU Model Specific Register to read per-cpu temperature
# information. The way the MSR is read is processor specific. If you want to run
# this on AMD or other CPU types, the MSR definitions section will need editing.
#
# Thanks to Ryan Cox, for the basic example of doing this:
# http://lists.us.dell.com/pipermail/linux-poweredge/2010-December/043786.html
#
# REQUIREMENTS: awk, rdmsr (from msr-tools)
#
# COPYRIGHT: Copyright (c) 2014 Brendan Gregg.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# (http://www.gnu.org/copyleft/gpl.html)
#
# 12-Sep-2014 Brendan Gregg Created this.
# 13-Feb-2018 " " Added -a for average temp.
### MSR definitions
MSR_TEMPERATURE_TARGET=0x1a2
MSR_TEMPERATURE_TARGET_MASK=23:16
IA32_THERM_STATUS=0x19c
IA32_THERM_STATUS_MASK=22:16
### sanity check
family=$(awk '/cpu family/ { print $NF; exit }' /proc/cpuinfo)
if (( family != 6 )); then
echo >&2 "WARNING: CPU family $family not recognized (not Intel?):"
head >&2 /proc/cpuinfo
echo >&2 "WARNING: continuining, but data is probably wrong."
echo >&2 "WARNING: edit this script to use the correct MSRs."
fi
### options
opt_avg=0
opt_util=0
ncpus=$(nproc --all)
interval=1
count=1
function usage {
cat <<-END >&2
USAGE: cputemp [-ahu] [interval [count]]
-h # USAGE message
-a # average temperature only
-u # include average CPU utilization %
Temperatures are shown in degrees Celsius
END
exit
}
while getopts ahu opt
do
case $opt in
a) opt_avg=1 ;;
u) opt_util=1 ;;
h|?) usage ;;
esac
done
shift $(( $OPTIND - 1 ))
if (( $# )); then
interval=$1
shift
(( count = 7 * 24 * 3600 ))
(( $# )) && count=$1
fi
### check access
if [[ "$USER" != "root" ]]; then
echo >&2 "ERROR: needs root access. Exiting."
exit 1
fi
if ! /sbin/modprobe msr; then
echo >&2 "ERROR: modprobe msr. Missing msr-tools package? Exiting."
exit 1
fi
cd /dev/cpu
### heading
head=
(( opt_util )) && head="AvgUtil(%) "
i=0
if (( opt_avg )); then
head="${head}AvgTemp(C)"
else
while (( i++ < ncpus )); do
head="${head}CPU$i"
(( i != ncpus )) && head="$head "
done
fi
echo $head
### main
n=0
while (( n++ < count )); do
out=
# measure and print CPU utilization
if (( opt_util )); then
set -- $(awk '$1 == "cpu" { print $0; exit }' /proc/stat)
(( used0 = $2 + $3 + $4 + $6 + $7 ))
sleep $interval
set -- $(awk '$1 == "cpu" { print $0; exit }' /proc/stat)
(( used1 = $2 + $3 + $4 + $6 + $7 ))
(( used = (used1 - used0) / ncpus ))
else
(( n != count )) && sleep $interval
fi
# fetch and print CPU temperatures
cpu=0
sum=0
out=""
while (( cpu < ncpus )); do
tt=$(rdmsr -p$cpu -f $MSR_TEMPERATURE_TARGET_MASK \
-d $MSR_TEMPERATURE_TARGET)
do=$(rdmsr -p$cpu -f $IA32_THERM_STATUS_MASK \
-u $IA32_THERM_STATUS)
(( temp = tt - do ))
(( sum += temp ))
out="${out}$temp "
(( cpu++ ))
done
(( opt_util )) && printf "$used "
if (( opt_avg )); then
awk -v sum=$sum -v ncpus=$ncpus 'BEGIN { printf("%.1f\n", sum / ncpus); }'
else
echo $out
fi
done