X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FSystem%2FProcess.pm;h=62df83118367013c6fd70e00f1db81f8b3df5b3a;hb=08e48f66590f580cbe2c8e3df76883d88b4c0fef;hp=36f1f657f39d207ff8e91252ed4b46eb9cd0bd36;hpb=cff913a1c984f82558a7d59dec0b8b1a06c5530d;p=kivitendo-erp.git diff --git a/SL/System/Process.pm b/SL/System/Process.pm index 36f1f657f..62df83118 100644 --- a/SL/System/Process.pm +++ b/SL/System/Process.pm @@ -25,6 +25,57 @@ sub exe_dir { return $cached_exe_dir; } +sub _parse_number_with_unit { + my ($number) = @_; + + return undef unless defined $number; + return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi; + + my %factors = (K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024); + + return $1 * $factors{uc $2}; +} + +sub memory_usage_is_too_high { + return undef unless $::lx_office_conf{system}; + + my %limits = ( + rss => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_rss}), + size => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_vsz}), + ); + + # $::lxdebug->dump(0, "limits", \%limits); + + return undef unless $limits{rss} || $limits{vsz}; + + my %usage; + + my $in = IO::File->new("/proc/$$/status", "r") or return undef; + + while (<$in>) { + chomp; + $usage{lc $1} = _parse_number_with_unit($2) if m{^ vm(rss|size): \s* (\d+ \s* [kmg]b) \s* $}ix; + } + + $in->close; + + # $::lxdebug->dump(0, "usage", \%usage); + + foreach my $type (keys %limits) { + next if !$limits{$type}; + next if $limits{$type} >= ($usage{$type} // 0); + + { + no warnings 'once'; + $::lxdebug->message(LXDebug::WARN(), "Exiting due to memory size limit reached for type '${type}': limit " . $limits{$type} . " bytes, usage " . $usage{$type} . " bytes"); + } + + return 1; + } + + return 0; +} + 1; __END__ @@ -51,6 +102,11 @@ Returns the absolute path to the directory the kivitendo executables (C etc.) and modules (sub-directory C etc.) are located in. +=item C + +Returns true if the current process uses more memory than the configured +limits. + =back =head1 BUGS