line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Memory-Stats |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2014 by celogeek . |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package Memory::Stats; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# ABSTRACT: Memory Usage Consumption of your process |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
262703
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
14
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.04'; # VERSION |
16
|
1
|
|
|
1
|
|
9556
|
use Proc::ProcessTable; |
|
1
|
|
|
|
|
40231
|
|
|
1
|
|
|
|
|
76
|
|
17
|
1
|
|
|
1
|
|
10
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
18
|
1
|
|
|
1
|
|
902
|
use Moo; |
|
1
|
|
|
|
|
50316
|
|
|
1
|
|
|
|
|
8
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $pt = Proc::ProcessTable->new; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has '_memory_usage' => ( is => 'rw', default => sub { [] } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _get_current_memory_usage { |
25
|
10
|
|
|
10
|
|
42
|
my ($info) = grep { $_->pid eq $$ } @{ $pt->table }; |
|
80
|
|
|
|
|
1567
|
|
|
10
|
|
|
|
|
88691
|
|
26
|
10
|
|
|
|
|
141
|
my $memory_usage; |
27
|
10
|
50
|
|
|
|
25
|
return -1 if !defined eval { $memory_usage = $info->rss }; |
|
10
|
|
|
|
|
49
|
|
28
|
10
|
|
|
|
|
204
|
return $memory_usage; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub start { |
32
|
1
|
|
|
1
|
1
|
328
|
my $self = shift; |
33
|
1
|
|
|
|
|
4
|
$self->_memory_usage( [ [ "start", _get_current_memory_usage() ] ] ); |
34
|
1
|
|
|
|
|
3
|
return; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub checkpoint { |
38
|
4
|
|
|
4
|
1
|
1094
|
my $self = shift; |
39
|
4
|
|
100
|
|
|
27
|
my $title = shift // 'checkpoint'; |
40
|
4
|
|
|
|
|
315
|
croak "Please start first !" |
41
|
4
|
100
|
100
|
|
|
8
|
if !scalar @{ $self->_memory_usage } |
42
|
|
|
|
|
|
|
|| $self->_memory_usage->[-1][0] eq 'stop'; |
43
|
2
|
|
|
|
|
4
|
push @{ $self->_memory_usage }, [ $title, _get_current_memory_usage() ]; |
|
2
|
|
|
|
|
10
|
|
44
|
2
|
|
|
|
|
7
|
return; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub stop { |
48
|
3
|
|
|
3
|
1
|
516
|
my $self = shift; |
49
|
3
|
|
|
|
|
449
|
croak "Please start first !" |
50
|
3
|
100
|
100
|
|
|
8
|
if !scalar @{ $self->_memory_usage } |
51
|
|
|
|
|
|
|
|| $self->_memory_usage->[-1][0] eq 'stop'; |
52
|
1
|
|
|
|
|
5
|
push @{ $self->_memory_usage }, [ 'stop', _get_current_memory_usage() ]; |
|
1
|
|
|
|
|
6
|
|
53
|
1
|
|
|
|
|
4
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub delta_usage { |
57
|
5
|
|
|
5
|
1
|
2100433
|
my $self = shift; |
58
|
5
|
50
|
|
|
|
58
|
my $last_memory_usage = $self->_memory_usage->[-1][1] |
59
|
|
|
|
|
|
|
or return; |
60
|
5
|
|
|
|
|
30
|
return ( _get_current_memory_usage() - $last_memory_usage ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub usage { |
64
|
2
|
|
|
2
|
1
|
11
|
my $self = shift; |
65
|
2
|
|
|
|
|
268
|
croak "Please start and stop before !" |
66
|
2
|
100
|
66
|
|
|
5
|
if scalar @{ $self->_memory_usage } < 2 |
67
|
|
|
|
|
|
|
|| $self->_memory_usage->[-1][0] ne 'stop'; |
68
|
1
|
|
|
|
|
20
|
return $self->_memory_usage->[-1][1] - $self->_memory_usage->[0][1]; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub report { |
73
|
1
|
|
|
1
|
1
|
2297
|
my $self = shift; |
74
|
1
|
|
|
|
|
72
|
print "--- Memory Usage ---\n"; |
75
|
1
|
|
|
|
|
27
|
my $prev; |
76
|
1
|
|
|
|
|
2
|
for my $row ( @{ $self->_memory_usage } ) { |
|
1
|
|
|
|
|
6
|
|
77
|
4
|
100
|
|
|
|
9
|
if ($prev) { |
78
|
3
|
|
|
|
|
50
|
printf( |
79
|
|
|
|
|
|
|
"%s: %d - delta: %d - total: %d\n", |
80
|
|
|
|
|
|
|
@$row, |
81
|
|
|
|
|
|
|
$row->[1] - $prev, |
82
|
|
|
|
|
|
|
$row->[1] - $self->_memory_usage->[0][1] |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
1
|
|
|
|
|
21
|
printf( "%s: %d\n", @$row ); |
87
|
|
|
|
|
|
|
} |
88
|
4
|
|
|
|
|
12
|
$prev = $row->[1]; |
89
|
|
|
|
|
|
|
} |
90
|
1
|
|
|
|
|
12
|
print "--- Memory Usage ---\n"; |
91
|
1
|
|
|
|
|
5
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |