line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################### |
2
|
|
|
|
|
|
|
package Process::MaxSize; |
3
|
|
|
|
|
|
|
########################################### |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
82660
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
6
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
7
|
1
|
|
|
1
|
|
6
|
use Log::Log4perl qw(:easy); |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
9
|
|
8
|
1
|
|
|
1
|
|
622
|
use Cwd; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
750
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "0.04"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
########################################### |
13
|
|
|
|
|
|
|
sub new { |
14
|
|
|
|
|
|
|
########################################### |
15
|
0
|
|
|
0
|
0
|
0
|
my($class, %options) = @_; |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
0
|
my @argv = @ARGV; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $self = { |
20
|
|
|
|
|
|
|
max_size => "10m", |
21
|
|
|
|
|
|
|
restart => sub { |
22
|
0
|
|
|
0
|
|
0
|
INFO "Restarting $0 @ARGV"; |
23
|
0
|
0
|
|
|
|
0
|
exec $0, @argv or |
24
|
|
|
|
|
|
|
LOGDIE "exec failed: $!"; |
25
|
|
|
|
|
|
|
}, |
26
|
0
|
|
|
|
|
0
|
sleep => 2, |
27
|
|
|
|
|
|
|
cwd => cwd(), |
28
|
|
|
|
|
|
|
%options, |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
0
|
if($self->{max_size} =~ /m$/) { |
32
|
0
|
|
|
|
|
0
|
$self->{max_size} =~ s/\D//g; |
33
|
0
|
|
|
|
|
0
|
$self->{max_size} *= 1024; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
bless $self, $class; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
########################################### |
40
|
|
|
|
|
|
|
sub check { |
41
|
|
|
|
|
|
|
########################################### |
42
|
0
|
|
|
0
|
0
|
0
|
my($self) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
my $process_size = $self->process_size(); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
DEBUG "Checking size: ${process_size}kB (max $self->{max_size}kB)"; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
0
|
if($process_size > $self->{max_size}) { |
49
|
0
|
|
|
|
|
0
|
WARN "Max size reached ", |
50
|
|
|
|
|
|
|
"(${process_size}kB/$self->{max_size}kB, restarting"; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Sleep the specified number of seconds |
53
|
0
|
0
|
|
|
|
0
|
if($self->{sleep}) { |
54
|
0
|
|
|
|
|
0
|
INFO "Sleeping $self->{sleep} seconds."; |
55
|
0
|
|
|
|
|
0
|
sleep($self->{sleep}); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Try to go back to the start directory |
59
|
0
|
|
|
|
|
0
|
INFO "Changing directory to $self->{cwd}"; |
60
|
0
|
|
|
|
|
0
|
chdir $self->{cwd}; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
$self->{restart}->(); |
63
|
|
|
|
|
|
|
# Doesn't return |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
return $process_size; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
########################################### |
70
|
|
|
|
|
|
|
sub process_size { |
71
|
|
|
|
|
|
|
########################################### |
72
|
1
|
|
|
1
|
0
|
7
|
my($self) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Doesn't work on freebsd |
75
|
|
|
|
|
|
|
#use Proc::ProcessTable; |
76
|
|
|
|
|
|
|
#my $t = Proc::ProcessTable->new(); |
77
|
|
|
|
|
|
|
#my($p) = grep { $_->pid == $$ } @{$t->table}; |
78
|
|
|
|
|
|
|
#return $p->size(); |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
1
|
my $size; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
3
|
my $ps = "ps"; |
83
|
1
|
|
|
|
|
2
|
my $solaris_ps = "/usr/ucb/ps"; |
84
|
|
|
|
|
|
|
|
85
|
1
|
50
|
|
|
|
24
|
if( -f $solaris_ps ) { |
86
|
0
|
|
|
|
|
0
|
$ps = $solaris_ps; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
5682
|
open PIPE, "$ps wwaxo 'pid,rss' |"; |
90
|
1
|
|
|
|
|
5448
|
while() { |
91
|
13
|
100
|
|
|
|
5667
|
next unless /^\s*$$\s/; |
92
|
1
|
|
|
|
|
15
|
s/^\s+//g; |
93
|
1
|
|
|
|
|
6
|
chomp; |
94
|
1
|
|
|
|
|
14
|
$size = (split(' ', $_))[1]; |
95
|
|
|
|
|
|
|
} |
96
|
1
|
|
|
|
|
49
|
close PIPE; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
33
|
return $size; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
__END__ |