line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::SizeLimit; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
481
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
415
|
use Mojo::IOLoop; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
6
|
1
|
|
|
1
|
|
24
|
use Time::HiRes (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
633
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $LifeTime; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $PKG = __PACKAGE__; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
if ($^O eq 'solaris') { |
15
|
|
|
|
|
|
|
# do not consider version number, cos it prolly does more harm than help |
16
|
|
|
|
|
|
|
*check_size = \&_solaris_size_check; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
elsif ($^O eq 'linux') { |
19
|
|
|
|
|
|
|
*check_size = eval { require Linux::Smaps } && Linux::Smaps->new($$) ? |
20
|
|
|
|
|
|
|
\&_linux_smaps_size_check : \&_linux_size_check; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
elsif ($^O =~ /(?:bsd|aix)/i) { |
23
|
|
|
|
|
|
|
# on OSX, getrusage() is returning 0 for proc & shared size. |
24
|
|
|
|
|
|
|
_load('BSD::Resource'); |
25
|
|
|
|
|
|
|
*check_size = \&_bsd_size_check; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
elsif ($^O =~ /darwin/i) { |
28
|
|
|
|
|
|
|
_load('BSD::Resource'); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my ($ver) = (qx(sw_vers -productVersion) || 0) =~ /^10\.(\d+)\.\d+$/; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# OSX 10.9+ has no concept of rshrd in top |
33
|
|
|
|
|
|
|
*check_size = $ver >= 9 ? \&_bsd_size_check : \&_darwin_size_check; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
else { |
36
|
|
|
|
|
|
|
die "$PKG is not implemented on $^O.\n"; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub register { |
41
|
1
|
|
|
1
|
1
|
462
|
my ($self, $app, $conf) = @_; |
42
|
1
|
|
|
|
|
3
|
my ($total) = check_size($app); |
43
|
|
|
|
|
|
|
|
44
|
1
|
50
|
|
|
|
14
|
die "OS ($^O) not supported by $PKG: Can not determine memory usage.\n" |
45
|
|
|
|
|
|
|
unless $total; |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
8
|
my %conf = %$conf; |
48
|
|
|
|
|
|
|
|
49
|
1
|
50
|
|
|
|
4
|
$conf{report_level} = 'debug' unless exists $conf->{report_level}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# ... a sub that is true every $check_interval requests |
52
|
1
|
|
|
|
|
3
|
*_count_requests = _make_count_requests(\%$conf); |
53
|
|
|
|
|
|
|
# ... a sub that is true if memory consumption exceeds conf values |
54
|
1
|
|
|
|
|
5
|
*_limits_are_exceeded = _make_limits_are_exceeded(\%conf); |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
1
|
|
3511
|
Mojo::IOLoop->singleton->next_tick(sub { $LifeTime = Time::HiRes::time }) |
57
|
1
|
50
|
|
|
|
12
|
if $conf{report_level}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$app->hook(after_dispatch => sub { |
60
|
2
|
|
|
2
|
|
9785
|
my ($count, $check) = _count_requests(); |
61
|
2
|
100
|
66
|
|
|
26
|
$check and _limits_are_exceeded($app, $count) |
62
|
|
|
|
|
|
|
or return; |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
8
|
shift->res->headers->connection('close'); |
65
|
1
|
|
|
|
|
83
|
Mojo::IOLoop->singleton->stop_gracefully; |
66
|
1
|
|
|
|
|
104
|
}); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# rss is in KB but ixrss is in BYTES. |
70
|
|
|
|
|
|
|
# This is true on at least FreeBSD, OpenBSD, & NetBSD |
71
|
|
|
|
|
|
|
sub _bsd_size_check { |
72
|
0
|
|
|
0
|
|
0
|
my @results = BSD::Resource::getrusage(); |
73
|
0
|
|
|
|
|
0
|
my $max_rss = $results[2]; |
74
|
0
|
|
|
|
|
0
|
my $max_ixrss = int ( $results[3] / 1024 ); |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
return ($max_rss, $max_ixrss); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _darwin_size_check { |
80
|
0
|
|
|
0
|
|
0
|
my ($size) = _bsd_size_check(); |
81
|
0
|
|
|
|
|
0
|
my ($shared) = (`top -e -l 1 -stats rshrd -pid $$ -s 0`)[-1]; |
82
|
0
|
0
|
0
|
|
|
0
|
$shared =~ s/^(\d+)M.*/$1 * 1024 * 1024/e |
|
0
|
|
|
|
|
0
|
|
83
|
|
|
|
|
|
|
or |
84
|
0
|
|
|
|
|
0
|
$shared =~ s/^(\d+)K.*/$1 * 1024/e |
85
|
|
|
|
|
|
|
or |
86
|
|
|
|
|
|
|
$shared =~ s/^(\d+)B.*/$1/; |
87
|
1
|
|
|
1
|
|
5
|
no warnings 'numeric'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
509
|
|
88
|
0
|
|
|
|
|
0
|
return ($size, int($shared)); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _linux_smaps_size_check { |
92
|
3
|
|
|
3
|
|
47
|
my $s = Linux::Smaps->new($$)->all; |
93
|
3
|
|
|
|
|
29048
|
return ($s->size, $s->shared_clean + $s->shared_dirty); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _linux_size_check { |
97
|
0
|
|
|
0
|
|
0
|
my ($size, $share) = (0, 0); |
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
0
|
if (open my $fh, '<', '/proc/self/statm') { |
100
|
0
|
|
|
|
|
0
|
($size, $share) = (split /\s/, scalar <$fh>)[0,2]; |
101
|
0
|
|
|
|
|
0
|
close $fh; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
else { |
104
|
0
|
|
|
|
|
0
|
$_[0]->log->error("Couldn't access /proc/self/statm"); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# linux on intel x86 has 4KB page size... |
108
|
0
|
|
|
|
|
0
|
return ($size * 4, $share * 4); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _load { |
112
|
0
|
|
|
0
|
|
0
|
my $mod = shift; |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
0
|
eval "require $mod" |
115
|
|
|
|
|
|
|
or die "You must install $mod for $PKG to work on your platform."; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _make_count_requests { |
119
|
1
|
|
|
1
|
|
1
|
my $conf = shift; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
0
|
|
0
|
return sub { state $count = 0; return (++$count, 1) } |
|
0
|
|
|
|
|
0
|
|
122
|
1
|
50
|
50
|
|
|
4
|
if ($conf->{check_interval} // 1) == 1; |
123
|
|
|
|
|
|
|
|
124
|
1
|
|
|
2
|
|
140
|
return eval <<"_SUB_"; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
6
|
|
125
|
|
|
|
|
|
|
sub { |
126
|
|
|
|
|
|
|
state \$count = 0; |
127
|
|
|
|
|
|
|
return (\$count, ++\$count % $conf->{check_interval} == 0); |
128
|
|
|
|
|
|
|
}; |
129
|
|
|
|
|
|
|
_SUB_ |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub _make_limits_are_exceeded { |
133
|
1
|
|
|
1
|
|
2
|
my $conf = shift; |
134
|
1
|
|
|
|
|
1
|
my $report = $conf->{report_level}; |
135
|
1
|
|
|
|
|
2
|
my ($f, $s); |
136
|
|
|
|
|
|
|
|
137
|
1
|
50
|
|
|
|
3
|
if ($report) { |
138
|
1
|
|
|
|
|
1
|
$f = q{_report($app, $count, $size, $shared, '%s')}; |
139
|
1
|
50
|
|
1
|
|
115
|
eval <<"_SUB_"; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
20
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
59
|
|
140
|
|
|
|
|
|
|
sub _report { |
141
|
|
|
|
|
|
|
my (\$app, \$count, \$size, \$shared, \$s) = \@_; |
142
|
|
|
|
|
|
|
my \$m = "SizeLimit: Exceeding limit \$s KB. PID = \$\$, SIZE = \$size KB"; |
143
|
|
|
|
|
|
|
\$m .= ", SHARED = \$shared KB, UNSHARED = " . (\$size - \$shared) . " KB" |
144
|
|
|
|
|
|
|
if \$shared; |
145
|
|
|
|
|
|
|
\$m .= sprintf ", REQUESTS = %u, LIFETIME = %5.3f s", |
146
|
|
|
|
|
|
|
\$count, Time::HiRes::time - \$LifeTime; |
147
|
|
|
|
|
|
|
\$app->log->$report(\$m); |
148
|
|
|
|
|
|
|
return 1; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
_SUB_ |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
else { |
153
|
0
|
|
|
|
|
0
|
$f = '1'; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
1
|
|
|
|
|
4
|
my $sub = <<'_SUB_'; |
157
|
|
|
|
|
|
|
sub { |
158
|
|
|
|
|
|
|
my ($app, $count) = @_; |
159
|
|
|
|
|
|
|
my ($size, $shared) = check_size($app); |
160
|
|
|
|
|
|
|
_SUB_ |
161
|
|
|
|
|
|
|
|
162
|
1
|
50
|
|
|
|
4
|
if ($s = $conf->{max_process_size}) { |
163
|
0
|
|
|
|
|
0
|
$sub .= ' return ' . sprintf($f, "max_process_size = $s") . |
164
|
|
|
|
|
|
|
" if \$size > $s;\n"; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
2
|
$sub .= <<'_SUB_'; |
168
|
|
|
|
|
|
|
return 0 unless $shared; |
169
|
|
|
|
|
|
|
_SUB_ |
170
|
|
|
|
|
|
|
|
171
|
1
|
50
|
|
|
|
3
|
if ($s = $conf->{min_shared_size}) { |
172
|
0
|
|
|
|
|
0
|
$sub .= ' return ' . sprintf($f, "min_shared_size = $s") . |
173
|
|
|
|
|
|
|
" if \$shared < $s;\n"; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
1
|
50
|
|
|
|
3
|
if ($s = $conf->{max_unshared_size}) { |
177
|
1
|
|
|
|
|
7
|
$sub .= ' return ' . sprintf($f, "max_unshared_size = $s") . |
178
|
|
|
|
|
|
|
" if \$size - \$shared > $s;\n"; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
1
|
|
|
|
|
2
|
$sub .= <<'_SUB_'; |
182
|
|
|
|
|
|
|
return 0; |
183
|
|
|
|
|
|
|
}; |
184
|
|
|
|
|
|
|
_SUB_ |
185
|
|
|
|
|
|
|
|
186
|
1
|
50
|
|
1
|
|
84
|
return eval $sub; |
|
1
|
50
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
50
|
|
|
1
|
|
|
|
|
22
|
|
|
0
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub _solaris_size_check { |
190
|
0
|
0
|
|
0
|
|
|
my $size = -s '/proc/self/as' |
191
|
|
|
|
|
|
|
or $_[0]->log->error("/proc/self/as doesn't exist or is empty"); |
192
|
0
|
|
|
|
|
|
$size = int($size / 1024); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# return 0 for share, to avoid undef warnings |
195
|
0
|
|
|
|
|
|
return ($size, 0); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
__END__ |