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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# This program is copyright 2011 Percona Ireland Ltd.
# Feedback and improvements are welcome.
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# 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, version 2; OR the Perl Artistic License. On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# 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.
# ###########################################################################
# DiskstatsGroupBySample package
# ###########################################################################
{
# Package: DiskstatsGroupBySample
#
package DiskstatsGroupBySample;
use warnings;
use strict;
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
use base qw( Diskstats );
use POSIX qw( ceil );
sub new {
my ( $class, %args ) = @_;
my $self = $class->SUPER::new(%args);
$self->{_iterations} = 0;
$self->{_save_curr_as_prev} = 0;
return $self;
}
# Prints out one line for each disk, summing over the interval from first to
# last sample.
sub group_by {
my ( $self, %args ) = @_;
my @optional_args = qw( header_callback rows_callback );
my ( $header_callback, $rows_callback ) = $args{ @optional_args };
$self->clear_state() unless $self->interactive();
$self->parse_from(
# ->can comes from UNIVERSAL. Returns a coderef to the method, if found.
# undef otherwise.
# Basically \&func, but always in runtime, and allows overriding
# the method in child classes.
sample_callback => $self->can("_sample_callback"),
filehandle => $args{filehandle},
filename => $args{filename},
data => $args{data},
);
return;
}
sub _sample_callback {
my ( $self, $ts, %args ) = @_;
my $printed_a_line = 0;
if ( $self->has_stats() ) {
$self->{_iterations}++;
}
my $elapsed = ($self->curr_ts() || 0)
- ($self->prev_ts() || 0);
if ( $ts > 0 && ceil($elapsed) >= $self->sample_time() ) {
$self->print_deltas(
# When grouping by samples, we don't usually show the device names,
# only a count of how many devices each sample has, which causes the
# columns' width change depending on simple invisible. That's uncalled
# for, so we hardcode the width here
# (6 is what the shell version used).
max_device_length => 6,
header_callback => sub {
my ( $self, $header, @args ) = @_;
if ( $self->force_header() ) {
my $method = $args{header_callback} || "print_header";
$self->$method( $header, @args );
$self->set_force_header(undef);
}
},
rows_callback => sub {
my ( $self, $format, $cols, $stat ) = @_;
my $method = $args{rows_callback} || "print_rows";
$self->$method( $format, $cols, $stat );
$printed_a_line = 1;
}
);
}
if ( $self->{_iterations} == 1 || $printed_a_line == 1 ) {
$self->{_save_curr_as_prev} = 1;
$self->_save_curr_as_prev( $self->stats_for() );
$self->set_prev_ts_line( $self->curr_ts_line() );
$self->{_save_curr_as_prev} = 0;
}
return;
}
sub delta_against {
my ( $self, $dev ) = @_;
return $self->prev_stats_for($dev);
}
sub ts_line_for_timestamp {
my ($self) = @_;
return $self->prev_ts_line();
}
sub delta_against_ts {
my ( $self ) = @_;
return $self->prev_ts();
}
sub clear_state {
my ( $self, @args ) = @_;
$self->{_iterations} = 0;
$self->{_save_curr_as_prev} = 0;
$self->SUPER::clear_state(@args);
}
sub compute_devs_in_group {
my ($self) = @_;
my $stats = $self->stats_for();
return scalar grep {
# Got stats for that device, and it matches the devices re
$stats->{$_} && $self->_print_device_if($_)
} $self->ordered_devs;
}
sub compute_dev {
my ( $self, $devs ) = @_;
$devs ||= $self->compute_devs_in_group();
return "{" . $devs . "}" if $devs > 1;
return (grep { $self->_print_device_if($_) } $self->ordered_devs())[0];
}
# Terrible breach of encapsulation, but it'll have to do for the moment.
sub _calc_stats_for_deltas {
my ( $self, $elapsed ) = @_;
my $delta_for;
foreach my $dev ( grep { $self->_print_device_if($_) } $self->ordered_devs() ) {
my $curr = $self->stats_for($dev);
my $against = $self->delta_against($dev);
next unless $curr && $against;
my $delta = $self->_calc_delta_for( $curr, $against );
$delta->{ios_in_progress} = $curr->[Diskstats::IOS_IN_PROGRESS];
while ( my ( $k, $v ) = each %$delta ) {
$delta_for->{$k} += $v;
}
}
return unless $delta_for && %{$delta_for};
my $in_progress = $delta_for->{ios_in_progress};
my $tot_in_progress = 0;
my $devs_in_group = $self->compute_devs_in_group() || 1;
my %stats = (
$self->_calc_read_stats(
delta_for => $delta_for,
elapsed => $elapsed,
devs_in_group => $devs_in_group,
),
$self->_calc_write_stats(
delta_for => $delta_for,
elapsed => $elapsed,
devs_in_group => $devs_in_group,
),
in_progress =>
$self->compute_in_progress( $in_progress, $tot_in_progress ),
);
my %extras = $self->_calc_misc_stats(
delta_for => $delta_for,
elapsed => $elapsed,
devs_in_group => $devs_in_group,
stats => \%stats,
);
@stats{ keys %extras } = values %extras;
$stats{dev} = $self->compute_dev( $devs_in_group );
$self->{_first_time_magic} = undef;
if ( @{$self->{_nochange_skips}} ) {
my $devs = join ", ", @{$self->{_nochange_skips}};
PTDEBUG && _d("Skipping [$devs], haven't changed from the first sample");
$self->{_nochange_skips} = [];
}
return \%stats;
}
sub compute_line_ts {
my ($self, %args) = @_;
if ( $self->show_timestamps() ) {
@args{ qw( first_ts curr_ts ) } = @args{ qw( curr_ts first_ts ) }
}
return $self->SUPER::compute_line_ts(%args);
}
1;
}
# ###########################################################################
# End DiskstatsGroupBySample package
# ###########################################################################
|