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 | |
4 | |
5 | use Unix::Getrusage; |
5 | # TODO, CONF framework? |
6 | |
|
|
7 | our $MAX_VMSIZE = $cf::CFG{checkrusage}{vmsize} || 1_000_000_000; |
6 | our $MAX_VMSIZE = $cf::CFG{checkrusage}{vmsize} || 1_000_000_000; |
8 | |
7 | |
9 | $^O eq "linux" or die "only linux supported right now.\n"; |
8 | $^O eq "linux" or die "only linux supported right now.\n"; |
10 | |
9 | |
11 | Event->timer ( |
10 | our $STAT_FH; |
12 | reentrant => 0, |
11 | our $TIMER; |
13 | data => cf::WF_AUTOCANCEL, |
12 | |
14 | interval => 10, |
13 | cf::post_init { |
15 | after => 10, |
14 | cf::async_ext { |
16 | cb => sub { |
15 | $STAT_FH = Coro::AIO::aio_open "/proc/self/stat", IO::AIO::O_RDONLY, 0 |
17 | open my $fh, "</proc/self/stat" |
|
|
18 | or return; |
16 | or return; |
19 | |
17 | |
|
|
18 | $TIMER = cf::periodic 10, sub { |
|
|
19 | sysseek $STAT_FH, 0, 0; |
20 | sysread $fh, my $stat, 4096; # only a single line |
20 | sysread $STAT_FH, my $stat, 4096; # only a single line |
21 | my $vmsize = (split / /, $stat)[22]; |
21 | my $vmsize = (split / /, $stat)[22]; |
22 | |
22 | |
23 | cf::cleanup "memory usage growing high, server would run out of memory soon", 0 |
23 | cf::cleanup "memory usage growing high ($vmsize > $MAX_VMSIZE), server would run out of memory soon", 0 |
24 | if $vmsize >= $MAX_VMSIZE; |
24 | if $vmsize >= $MAX_VMSIZE; |
|
|
25 | }; |
25 | }, |
26 | }; |
26 | ); |
27 | }; |