blib/lib/Monitoring/Sneck/Checks/thermal_throttle.pm

filter: Statementstmt 100% 71 / 71 Branchbran 85.7% 24 / 28 Conditioncond 68.9% 20 / 29 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 86.2% 138 / 160 CC 30 SCAR 27.8
CC
30
Cov
90%
check
25.0
__ANON__
5.0
__ANON__
3.0
CRAP 16.1
« blib/lib/Monitoring/Sneck/Checks/log.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/containers.pm »
1 package Monitoring::Sneck::Checks::thermal_throttle;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::thermal_throttle - checks for CPUs being thermally throttled
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 # Where the CPUs are in sysfs. Set by tests.
22 3 our $CPU_DIR = '/sys/devices/system/cpu';
23  
24 =head1 SYNOPSIS
25  
26 thermal_throttle|perl:thermal_throttle
27 thermal_throttle_laptop|perl:thermal_throttle -w 100 -c 1000
28  
29 =head1 DESCRIPTION
30  
31 Counts the thermal throttle events since the last run, from the
32 counters Linux keeps under /sys/devices/system/cpu/cpu*/thermal_throttle
33 for Intel CPUs. By default any is WARNING.
34  
35 There are core and package counters. Hyperthreads of a core share its
36 counters, and every CPU in a package shows the package's, so they are
37 counted once each, going by the topology under each CPU. --warning and
38 --critical go by the worse of the two.
39  
40 The counters are kept in a state file, see
41 L<Monitoring::Sneck::Checks::base/read_state>, so the first run, and
42 the first after a reboot, have nothing to compare to and are OK. The
43 counts since boot are said as well.
44  
45 The milliseconds throttled are said when the kernel has the
46 *_throttle_total_time_ms counters.
47  
48 - CRITICAL :: New core or package events over --critical.
49  
50 - WARNING :: New core or package events over --warning.
51  
52 - UNKNOWN :: Not being Linux, or the state not being readable or
53 writable.
54  
55 - OK :: Everything else, including having no counters, as with AMD CPUs
56 and most VMs, so the same check can go everywhere.
57  
58 =head1 OPTIONS
59  
60 - -w, --warning :: Range for the new core or package events.
61 Default :: 0
62  
63 - -c, --critical :: Range for the new core or package events.
64  
65 =cut
66  
67 12 ▶sub option_spec { return ( 'warning|w=s', 'critical|c=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
68  
69 12 ▶sub defaults { return { warning => '0' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
70  
71 12 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
72  
73 =head1 METHODS
74  
75 =head2 check
76  
77 Does the check.
78  
79 =cut
80  
81 12 ▶sub check {
Subroutine check: called CC=24 SCAR=32.2
Pod documented
82 12 my ($self) = @_;
83  
84 12 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "linux" 1 11 12
85  
86 # a counter or topology file, as a number, or undef if it is not there
87 126 ▶ my $read = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
88 126 my $content = read_file( $_[0], err_mode => 'quiet' );
89 126 ▶ return undef if !defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 3 123 126
90 123 ▶ return $content =~ /\A\s*([0-9]+)\s*\z/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /\A\s*([0-9]+)\s*\z/u ? : 123 0 123
91 11 };
92  
93 # each core and package only once, the first CPU of it seen
94 11 my %cores;
95 my %packages;
96 11 ▶ opendir( my $dh, $CPU_DIR ) or return $self->finish( $self->OK, 'no thermal throttle counters' );
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR or return $self->finish($self->OK, "no thermal throttle counters") 10 1 11
97 10 foreach my $cpu ( grep { /\Acpu[0-9]+\z/ } readdir($dh) ) {
98 37 my $throttle = $CPU_DIR . '/' . $cpu . '/thermal_throttle';
99 37 ▶ next if !-d $throttle;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $throttle 1 36 37
100 36 ▶ my $package = $read->( $CPU_DIR . '/' . $cpu . '/topology/physical_package_id' ) // $cpu;
Condition33%1/3
&$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/physical_package_id') // $cpu
l!l&&r!l&&!r
3600
MC/DC0%0/2
&$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/physical_package_id') // $cpu
&$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/physical_package_id') $cpu
Truth table33%1/3
A // B
A= &$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/physical_package_id')B= $cpu
ABresult
1X1
011
000
101 36 ▶ my $core = $read->( $CPU_DIR . '/' . $cpu . '/topology/core_id' ) // $cpu;
Condition33%1/3
&$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/core_id') // $cpu
l!l&&r!l&&!r
3600
MC/DC0%0/2
&$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/core_id') // $cpu
&$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/core_id') $cpu
Truth table33%1/3
A // B
A= &$read($Monitoring::Sneck::Checks::thermal_throttle::CPU_DIR . '/' . $cpu . '/topology/core_id')B= $cpu
ABresult
1X1
011
000
102 36 ▶ $cores{ $package . ':' . $core } //= {
Condition100%2/2
$cores{$package . ':' . $core} //= {"count", &$read($throttle . '/core_throttle_count'), "ms", &$read($throttle . '/core_throttle_total_time_ms')}
l!l
1818
MC/DC100%1/1
$cores{$package . ':' . $core} //= {"count", &$read($throttle . '/core_throttle_count'), "ms", &$read($throttle . '/core_throttle_total_time_ms')}
$cores{$package . ':' . $core}
Truth table100%2/2
A
A= $cores{$package . ':' . $core}
Aresult
11
00
103 count => $read->( $throttle . '/core_throttle_count' ),
104 ms => $read->( $throttle . '/core_throttle_total_time_ms' ),
105 };
106 36 ▶ $packages{$package} //= {
Condition100%2/2
$packages{$package} //= {"count", &$read($throttle . '/package_throttle_count'), "ms", &$read($throttle . '/package_throttle_total_time_ms')}
l!l
279
MC/DC100%1/1
$packages{$package} //= {"count", &$read($throttle . '/package_throttle_count'), "ms", &$read($throttle . '/package_throttle_total_time_ms')}
$packages{$package}
Truth table100%2/2
A
A= $packages{$package}
Aresult
11
00
107 count => $read->( $throttle . '/package_throttle_count' ),
108 ms => $read->( $throttle . '/package_throttle_total_time_ms' ),
109 };
110 } ## end foreach my $cpu ( grep { /\Acpu[0-9]+\z/ } readdir...)
111 10 ▶ return $self->finish( $self->OK, 'no thermal throttle counters' ) if !%cores;
Branch: 1 branch
BranchTrueFalseTotal
unless %cores 1 9 10
112  
113 # the count and ms of all cores or packages, ms being undef if any lacks it, as on older kernels
114 18 ▶ my $add_up = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
115 18 my ($counters) = @_;
116 18 my $count = 0;
117 18 my $ms = 0;
118 18 foreach my $counter ( values( %{$counters} ) ) {
119 27 ▶ $count = $count + ( $counter->{count} // 0 );
Condition50%1/2
$counter->{"count"} // 0
l!l
270
MC/DC0%0/1
$counter->{"count"} // 0
$counter->{"count"}
Truth table50%1/2
A
A= $counter->{"count"}
Aresult
11
00
120 27 ▶ $ms = ( defined($ms) && defined( $counter->{ms} ) ) ? $ms + $counter->{ms} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ms && defined $counter->{'ms'} ? : 24 3 27
Condition100%3/3
defined $ms and defined $counter->{"ms"}
!ll&&!rl&&r
1224
MC/DC100%2/2
defined $ms and defined $counter->{"ms"}
defined $ms defined $counter->{"ms"}
Truth table100%3/3
A and B
A= defined $msB= defined $counter->{"ms"}
ABresult
0X0
100
111
121 }
122 18 return ( $count, $ms );
123 9 }; ## end $add_up = sub
124 9 my %now;
125 9 ( $now{core_count}, $now{core_ms} ) = $add_up->( \%cores );
126 9 ( $now{package_count}, $now{package_ms} ) = $add_up->( \%packages );
127  
128 9 my $file = $Monitoring::Sneck::Checks::base::STATE_DIR . '/thermal_throttle.json';
129 9 my $state = $self->read_state($file);
130 9 ▶ return $self->finish( $self->UNKNOWN, 'can not read the state in ' . $file ) if !defined($state);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 9 9
131 9 my $problem = $self->write_state( $file, \%now );
132 9 ▶ return $self->finish( $self->UNKNOWN, $problem ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 9 9
133  
134 9 my $since_boot = $now{core_count} . ' core and ' . $now{package_count} . ' package';
135  
136 # lower counts than last time is a reboot, so starts over
137 9 ▶ if ( !defined( $state->{core_count} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $state->{"core_count"} or $now{"core_count"} < $state->{"core_count"} or $now{"package_count"} < ($state->{"package_count"} // 0)) 4 5 9
Condition75%6/8
not defined $state->{"core_count"} or $now{"core_count"} < $state->{"core_count"} or $now{"package_count"} < ($state->{"package_count"} // 0)
l!l&&r!l&&!r
405
not defined $state->{"core_count"} or $now{"core_count"} < $state->{"core_count"}
l!l&&r!l&&!r
135
$state->{"package_count"} // 0
l!l
50
MC/DC66%2/3
not defined $state->{"core_count"} or $now{"core_count"} < $state->{"core_count"} or $now{"package_count"} < ($state->{"package_count"} // 0)
not defined $state->{"core_count"} $now{"core_count"} < $state->{"core_count"} $now{"package_count"} < ($state->{"package_count"} // 0)
MC/DC0%0/1
$state->{"package_count"} // 0
$state->{"package_count"}
Truth table75%3/4
not(A) or B or C
A= not defined $state->{"core_count"}B= $now{"core_count"} < $state->{"core_count"}C= $now{"package_count"} < ($state->{"package_count"} // 0)
ABCresult
1XX1
01X1
0011
0000
Truth table50%1/2
A
A= $state->{"package_count"}
Aresult
11
00
138 || $now{core_count} < $state->{core_count}
139 || $now{package_count} < ( $state->{package_count} // 0 ) )
140 {
141 4 return $self->finish( $self->OK, 'first check, ' . $since_boot . ' throttle events since boot' );
142 }
143  
144 5 my %new;
145 5 foreach my $name ( 'core_count', 'package_count', 'core_ms', 'package_ms' ) {
146 20 ▶ next if !defined( $now{$name} ) || !defined( $state->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $now{$name} and defined $state->{$name} 2 18 20
Condition66%2/3
defined $now{$name} and defined $state->{$name}
!ll&&!rl&&r
2018
MC/DC50%1/2
defined $now{$name} and defined $state->{$name}
defined $now{$name} defined $state->{$name}
Truth table66%2/3
A and B
A= defined $now{$name}B= defined $state->{$name}
ABresult
0X0
100
111
147 18 $new{$name} = $now{$name} - $state->{$name};
148 18 ▶ $new{$name} = $now{$name} if $new{$name} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $new{$name} < 0 0 18 18
149 }
150  
151 my $code = $self->worst(
152 $self->threshold_status( $new{core_count}, $self->opt('warning'), $self->opt('critical') ),
153 5 $self->threshold_status( $new{package_count}, $self->opt('warning'), $self->opt('critical') )
154 );
155 5 foreach my $name ( 'core', 'package' ) {
156 $self->perfdata(
157 label => $name . '_events',
158 10 value => $new{ $name . '_count' },
159 warning => $self->opt('warning'),
160 critical => $self->opt('critical'),
161 min => 0,
162 );
163 $self->perfdata( label => $name . '_time', value => $new{ $name . '_ms' }, uom => 'ms', min => 0 )
164 10 ▶ if defined( $new{ $name . '_ms' } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $new{$name . '_ms'} 8 2 10
165 } ## end foreach my $name ( 'core', 'package' )
166  
167 my @parts
168 5 = ( $new{core_count} . ' core and ' . $new{package_count} . ' package throttle events since the last check' );
169 5 ▶ if ( defined( $new{core_ms} ) && defined( $new{package_ms} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new{"core_ms"} and defined $new{"package_ms"}) 4 1 5
Condition66%2/3
defined $new{"core_ms"} and defined $new{"package_ms"}
!ll&&!rl&&r
104
MC/DC50%1/2
defined $new{"core_ms"} and defined $new{"package_ms"}
defined $new{"core_ms"} defined $new{"package_ms"}
Truth table66%2/3
A and B
A= defined $new{"core_ms"}B= defined $new{"package_ms"}
ABresult
0X0
100
111
170 4 push( @parts, $new{core_ms} . ' ms and ' . $new{package_ms} . ' ms throttled' );
171 }
172 5 push( @parts, $since_boot . ' since boot' );
173  
174 5 return $self->finish( $code, join( ', ', @parts ) );
175 } ## end sub check
176  
177 3 1;
« blib/lib/Monitoring/Sneck/Checks/log.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/containers.pm »