| 1 | package Brackup::BackupStats; | ||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||
| 5 | 5 | my $class = shift; | |||||||||
| 6 | 5 | my %opts = @_; | |||||||||
| 7 | 5 | ▶ | croak("Unknown options: " . join(', ', keys %opts)) if %opts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | my $self = { | |||||||||
| 10 | start_time => time, | ||||||||||
| 11 | ts => Brackup::BackupStats::Data->new, | ||||||||||
| 12 | data => Brackup::BackupStats::Data->new, | ||||||||||
| 13 | }; | ||||||||||
| 14 | |||||||||||
| 15 | 5 | ▶ | if (eval { require GTop }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | $self->{gtop} = GTop->new; | |||||||||
| 17 | 0 | $self->{gtop_max} = 0; | |||||||||
| 18 | 0 | $self->{gtop_data} = Brackup::BackupStats::Data->new; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 5 | return bless $self, $class; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | sub print { | ||||||||
|
Subroutine
print: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||
| 25 | 0 | my $self = shift; | |||||||||
| 26 | 0 | my $stats_file = shift; | |||||||||
| 27 | |||||||||||
| 28 | # Reset iterators | ||||||||||
| 29 | 0 | $self->reset; | |||||||||
| 30 | |||||||||||
| 31 | 0 | my $fh; | |||||||||
| 32 | 0 | ▶ | if ($stats_file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | ▶ | open $fh, ">$stats_file" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | or die "Failed to open stats file '$stats_file': $!"; | ||||||||||
| 35 | } | ||||||||||
| 36 | else { | ||||||||||
| 37 | 0 | $fh = *STDOUT; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | my $hash = $stats_file ? '' : '# '; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | print $fh "${hash}BACKUPS STATS:\n"; | |||||||||
| 42 | 0 | print $fh "${hash}\n"; | |||||||||
| 43 | |||||||||||
| 44 | 0 | my $start_time = $self->{start_time}; | |||||||||
| 45 | 0 | my $end_time = time; | |||||||||
| 46 | 0 | my $fmt = "${hash}%-37s %s\n"; | |||||||||
| 47 | 0 | printf $fh $fmt, 'Start Time:', scalar localtime $start_time; | |||||||||
| 48 | 0 | printf $fh $fmt, 'End Time:', scalar localtime $end_time; | |||||||||
| 49 | |||||||||||
| 50 | 0 | my $ts = $start_time; | |||||||||
| 51 | 0 | ▶ | while (my ($label, $next_ts) = $self->{ts}->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | printf $fh $fmt, "$label Time:", ($next_ts - $ts) . 's'; | |||||||||
| 53 | 0 | $ts = $next_ts; | |||||||||
| 54 | } | ||||||||||
| 55 | 0 | printf $fh $fmt, 'Total Run Time:', ($end_time - $start_time) . 's'; | |||||||||
| 56 | 0 | print $fh "${hash}\n"; | |||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | if (my $gtop_data = $self->{gtop_data}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | ▶ | while (my ($label, $size) = $gtop_data->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | printf $fh $fmt, | |||||||||
| 61 | "Post $label Memory Usage:", sprintf('%0.1f MB', $size / (1024 * 1024)); | ||||||||||
| 62 | } | ||||||||||
| 63 | printf $fh $fmt, | ||||||||||
| 64 | 0 | 'Peak Memory Usage:', sprintf('%0.1f MB', $self->{gtop_max} / (1024 * 1024)); | |||||||||
| 65 | 0 | print $fh "${hash}\n"; | |||||||||
| 66 | } else { | ||||||||||
| 67 | 0 | print $fh "${hash}GTop not installed, memory usage stats disabled\n"; | |||||||||
| 68 | 0 | print $fh "${hash}\n"; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | my $data = $self->{data}; | |||||||||
| 72 | 0 | ▶ | while (my ($key, $value) = $data->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | printf $fh $fmt, $key, $value; | |||||||||
| 74 | } | ||||||||||
| 75 | 0 | ▶ | print $fh "\n" if $stats_file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | # Check/record max memory usage | ||||||||||
| 79 | 48 | ▶ | sub check_maxmem { | ||||||||
|
Subroutine
check_maxmem: called CC=3 SCAR=16.5
Pod
undocumented
| |||||||||||
| 80 | 48 | my $self = shift; | |||||||||
| 81 | 48 | ▶ | return unless $self->{gtop}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | my $mem = $self->{gtop}->proc_mem($$)->size; | |||||||||
| 83 | 0 | ▶ | $self->{gtop_max} = $mem if $mem > $self->{gtop_max}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | # Record current time (and memory, if applicable) against $label | ||||||||||
| 87 | 20 | ▶ | sub timestamp { | ||||||||
|
Subroutine
timestamp: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||
| 88 | 20 | my ($self, $label) = @_; | |||||||||
| 89 | 20 | $self->{ts}->set($label => time); | |||||||||
| 90 | 20 | ▶ | return unless $self->{gtop}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | $self->{gtop_data}->set($label => $self->{gtop}->proc_mem($$)->size); | |||||||||
| 92 | 0 | $self->check_maxmem; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 20 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 96 | 20 | my $self = shift; | |||||||||
| 97 | 20 | ▶ | $self->{data}->set(shift, shift) while @_ >= 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | sub reset { | ||||||||
|
Subroutine
reset: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 101 | 0 | my $self = shift; | |||||||||
| 102 | 0 | $self->{ts}->reset; | |||||||||
| 103 | 0 | $self->{data}->reset; | |||||||||
| 104 | 0 | ▶ | $self->{gtop_data}->reset if $self->{gtop_data}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub note_stored_chunk { | ||||||||
|
Subroutine
note_stored_chunk: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 108 | 0 | my ($self, $chunk) = @_; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | package Brackup::BackupStats::Data; | ||||||||||
| 112 | |||||||||||
| 113 | 10 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 114 | 10 | my $class = shift; | |||||||||
| 115 | 10 | return bless { | |||||||||
| 116 | index => 0, | ||||||||||
| 117 | list => [], # ordered list of data keys | ||||||||||
| 118 | data => {}, | ||||||||||
| 119 | }, $class; | ||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 40 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=2 SCAR=7.0
| |||||||||||
| 123 | 40 | my ($self, $key, $value) = @_; | |||||||||
| 124 | 40 | ▶ | die "data key '$key' exists" if exists $self->{data}->{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 40 | push @{$self->{list}}, $key; | |||||||||
| 126 | 40 | $self->{data}->{$key} = $value; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | # Iterator interface, returning ($key, $value) | ||||||||||
| 130 | 0 | ▶ | sub next { | ||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
| |||||||||||
| 131 | 0 | my $self = shift; | |||||||||
| 132 | 0 | ▶ | return () unless $self->{index} <= $#{$self->{list}}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | my $key = $self->{list}->[$self->{index}++]; | |||||||||
| 134 | 0 | return ($key, $self->{data}->{$key}); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | # Reset/rewind iterator | ||||||||||
| 138 | 0 | ▶ | sub reset { | ||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
| |||||||||||
| 139 | 0 | my $self = shift; | |||||||||
| 140 | 0 | $self->{index} = 0; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 13 | 1; | |||||||||
| 144 | |||||||||||
| 145 | # vim:sw=4 | ||||||||||