From 253d756269c58fc019073c2d8da597f9f9b65e48 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 4 Mar 2019 16:59:55 +0100 Subject: [PATCH] memory_usage_is_too_high von Dispatcher nach System::Process verschoben (cherry picked from commit c0e3364a21b2da1c61564ddb8d9afa5ab6489f9c) --- SL/Dispatcher.pm | 50 +------------------------------------ SL/System/Process.pm | 56 ++++++++++++++++++++++++++++++++++++++++++ scripts/task_server.pl | 4 +-- 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index abc5ec82f..fcb3aeaf1 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -231,7 +231,7 @@ sub handle_all_requests { while ($request->Accept() >= 0) { $self->handle_request($request); - $self->restart_after_request(1) if $self->_interface_is_fcgi && $self->_memory_usage_is_too_high; + $self->restart_after_request(1) if $self->_interface_is_fcgi && SL::System::Process::memory_usage_is_too_high(); $request->LastCall if $self->restart_after_request; } @@ -497,54 +497,6 @@ sub _check_for_old_config_files { end_request(); } -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); - - $::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; -} - sub end_request { die SL::Dispatcher->END_OF_REQUEST; } 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 diff --git a/scripts/task_server.pl b/scripts/task_server.pl index 2f3339ccb..7236deaf6 100755 --- a/scripts/task_server.pl +++ b/scripts/task_server.pl @@ -25,7 +25,7 @@ use SL::Auth; use SL::DBUpgrade2; use SL::DB::AuthClient; use SL::DB::BackgroundJob; -use SL::Dispatcher; +use SL::System::Process; use SL::BackgroundJob::ALL; use SL::Form; use SL::Helper::DateTime; @@ -332,7 +332,7 @@ sub gd_run { clean_before_sleeping(); - if (SL::Dispatcher::_memory_usage_is_too_high()) { + if (SL::System::Process::memory_usage_is_too_high()) { return; } -- 2.20.1