1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # restart server when certain resource limits are exceeded |
3 | # restart server when certain resource limits are exceeded |
4 | |
|
|
5 | use Unix::Getrusage; |
|
|
6 | |
4 | |
7 | our $MAX_VMSIZE = $cf::CFG{checkrusage}{vmsize} || 1_000_000_000; |
5 | our $MAX_VMSIZE = $cf::CFG{checkrusage}{vmsize} || 1_000_000_000; |
8 | |
6 | |
9 | $^O eq "linux" or die "only linux supported right now.\n"; |
7 | $^O eq "linux" or die "only linux supported right now.\n"; |
10 | |
8 | |
11 | Event->timer ( |
9 | our $TIMER = cf::periodic 10, sub { |
12 | reentrant => 0, |
|
|
13 | data => cf::WF_AUTOCANCEL, |
|
|
14 | interval => 10, |
|
|
15 | after => 10, |
|
|
16 | cb => sub { |
|
|
17 | open my $fh, "</proc/self/stat" |
10 | open my $fh, "</proc/self/stat" |
18 | or return; |
11 | or return; |
19 | |
12 | |
20 | sysread $fh, my $stat, 4096; # only a single line |
13 | sysread $fh, my $stat, 4096; # only a single line |
21 | my $vmsize = (split / /, $stat)[22]; |
14 | my $vmsize = (split / /, $stat)[22]; |
22 | |
15 | |
23 | cf::cleanup "memory usage growing high, server would run out of memory soon", 0 if $vmsize >= $MAX_VMSIZE; |
16 | cf::cleanup "memory usage growing high, server would run out of memory soon", 0 |
24 | }, |
17 | if $vmsize >= $MAX_VMSIZE; |
25 | ); |
18 | }; |