mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-10-21 18:24:38 +00:00
Work in progress: Redesign pt-diskstats in Perl.
Mostly bits and pieces at the moments. ReadKeyMini is a portable-ish Term::ReadKey. Used here by DiskstatsMenu, which is currently only for show. Diskstats has most of the logic of the old pt-diskstats, with parts now implemented by the DiskstatsGroupBy* classes; The latter subclass the former. As mentioned at the start, this is a work in progress. In addition to to not having all the parts attached, also missing are the data-gathering mode, the command-line argument parsing, and a large amount of tests.
This commit is contained in:
188
lib/DiskstatsGroupBySample.pm
Normal file
188
lib/DiskstatsGroupBySample.pm
Normal file
@@ -0,0 +1,188 @@
|
||||
# This program is copyright 2011 Percona Inc.
|
||||
# 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 MKDEBUG => $ENV{MKDEBUG} || 0;
|
||||
|
||||
use base qw( Diskstats );
|
||||
|
||||
sub new {
|
||||
my ($class, %args) = @_;
|
||||
my $self = $class->SUPER::new(%args);
|
||||
$self->{_iterations} = 0;
|
||||
$self->{_interval} = 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_sample {
|
||||
my ($self, %args) = @_;
|
||||
my ($header_cb, $rest_cb) = $args{ qw( header_cb rest_cb ) };
|
||||
|
||||
$self->clear_state;
|
||||
|
||||
my $print_header = 1;
|
||||
my $printed_a_line = 0;
|
||||
$self->parse_from(
|
||||
ts_callback => sub {
|
||||
my ($self, $ts) = @_;
|
||||
my $printed_a_line = 0;
|
||||
|
||||
if ( $self->has_stats ) {
|
||||
$self->{_iterations}++;
|
||||
}
|
||||
my $elapsed = ($self->stats_for->{_ts} || 0) - ($self->previous_stats_for->{_ts} || 0);
|
||||
if ( $ts > 0 && $elapsed >= $self->{_interval} ) {
|
||||
|
||||
$self->print_deltas(
|
||||
dev_length => 6,
|
||||
header_cb => sub {
|
||||
my ($self, $header, @args) = @_;
|
||||
if ( $print_header ) {
|
||||
$print_header = 0;
|
||||
if ( my $cb = $args{header_cb} ) {
|
||||
$self->$cb($header, @args);
|
||||
}
|
||||
else {
|
||||
printf { $self->out_fh } $header."\n", @args;
|
||||
}
|
||||
}
|
||||
},
|
||||
rest_cb => sub {
|
||||
my ($self, $format, $cols, $stat) = @_;
|
||||
printf { $self->out_fh } $format."\n", @{$stat}{ qw( line_ts dev ), @$cols };
|
||||
$printed_a_line = 1;
|
||||
}
|
||||
);
|
||||
}
|
||||
if ( $self->{_iterations} == 1 || $printed_a_line == 1 ) {
|
||||
local $self->{_save_curr_as_prev} = 1;
|
||||
$self->_save_current_as_previous();
|
||||
}
|
||||
},
|
||||
map({ ($_ => $args{$_}) } qw(filehandle filename data)),
|
||||
);
|
||||
|
||||
$self->clear_state;
|
||||
}
|
||||
|
||||
sub delta_against {
|
||||
my ($self, $dev) = @_;
|
||||
return $self->previous_stats_for($dev);
|
||||
}
|
||||
|
||||
sub clear_state {
|
||||
my ($self, @args) = @_;
|
||||
$self->{_iterations} = 0;
|
||||
$self->{_save_curr_as_prev} = 0;
|
||||
$self->SUPER::clear_state(@args);
|
||||
}
|
||||
|
||||
sub compute_line_ts {
|
||||
my ($self, %args) = @_;
|
||||
return $args{first_ts} > 0
|
||||
? sprintf("%5.1f", $args{current_ts} - $args{first_ts})
|
||||
: sprintf("%5.1f", 0);;
|
||||
}
|
||||
|
||||
sub compute_devs_in_group {
|
||||
my ($self) = @_;
|
||||
return scalar grep 1, @{ $self->stats_for }{ @{$self->sorted_devs} };
|
||||
}
|
||||
|
||||
sub compute_in_progress {
|
||||
my ($self, $in_progress, $tot_in_progress) = @_;
|
||||
return $in_progress;
|
||||
}
|
||||
|
||||
sub compute_dev {
|
||||
my ($self, $dev) = @_;
|
||||
return $self->compute_devs_in_group() > 1
|
||||
? "{" . $self->compute_devs_in_group() . "}"
|
||||
: $self->sorted_devs->[0];
|
||||
}
|
||||
|
||||
# Terrible breach of encapsulation, but it'll have to do for the moment.
|
||||
sub _calc_deltas {
|
||||
my $self = shift;
|
||||
my ($callback) = @_;
|
||||
|
||||
my $elapsed = $self->stats_for->{_ts} - $self->delta_against->{_ts};
|
||||
die "Time elapsed is 0" unless $elapsed;
|
||||
my @end_stats;
|
||||
|
||||
my $delta_for;
|
||||
|
||||
for my $dev ( grep { $self->dev_ok($_) } @{$self->sorted_devs} ) {
|
||||
my $curr = $self->stats_for($dev);
|
||||
my $against = $self->delta_against($dev);
|
||||
|
||||
my $delta = $self->_calc_delta_for($curr, $against);
|
||||
$delta->{ios_in_progress} = $curr->{ios_in_progress};
|
||||
while ( my ($k, $v) = each %$delta ) {
|
||||
$delta_for->{$k} += $v;
|
||||
}
|
||||
}
|
||||
|
||||
my $in_progress = $delta_for->{ios_in_progress}; #$curr->{"ios_in_progress"};
|
||||
my $tot_in_progress = 0; #$against->{"sum_ios_in_progress"} || 0;
|
||||
|
||||
my $devs_in_group = $self->compute_devs_in_group;
|
||||
|
||||
# Compute the per-second stats for reads, writes, and overall.
|
||||
my %stats = (
|
||||
$self->_calc_read_stats($delta_for, $elapsed, $devs_in_group),
|
||||
$self->_calc_write_stats($delta_for, $elapsed, $devs_in_group),
|
||||
in_progress => $self->compute_in_progress($in_progress, $tot_in_progress),
|
||||
);
|
||||
|
||||
# Compute the numbers for reads and writes together, the things for
|
||||
# which we do not have separate statistics.
|
||||
# Busy is what iostat calls %util. This is the percent of
|
||||
# wall-clock time during which the device has I/O happening.
|
||||
$stats{busy} = 100 * $delta_for->{ms_spent_doing_io} / (1000 * $elapsed * $devs_in_group);
|
||||
$stats{line_ts} = $self->compute_line_ts(
|
||||
first_ts => $self->first_stats_for->{_ts},
|
||||
current_ts => $self->stats_for->{_ts},
|
||||
);
|
||||
|
||||
$stats{dev} = $self->compute_dev(\%stats);
|
||||
|
||||
if ($callback) {
|
||||
$self->$callback( \%stats );
|
||||
}
|
||||
|
||||
return \%stats;
|
||||
}
|
||||
|
||||
1;
|
||||
}
|
||||
# ###########################################################################
|
||||
# End DiskstatsGroupBySample package
|
||||
# ###########################################################################
|
Reference in New Issue
Block a user