X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FSystem%2FProcess.pm;h=62df83118367013c6fd70e00f1db81f8b3df5b3a;hb=a6e0a7f493d24aec0eebede85eeaa5e724bd2e11;hp=0bddfd6b3c9de89ed7dbad1262f9ec1be9522d0f;hpb=4ab897fdb1a54cd753ec66ebc8fbd5ffdd883cd6;p=kivitendo-erp.git diff --git a/SL/System/Process.pm b/SL/System/Process.pm index 0bddfd6b3..62df83118 100644 --- a/SL/System/Process.pm +++ b/SL/System/Process.pm @@ -5,21 +5,75 @@ use strict; use parent qw(Rose::Object); use English qw(-no_match_vars); +use FindBin; use File::Spec; use File::Basename; +use List::Util qw(first); + +my $cached_exe_dir; sub exe_dir { - my $dir = dirname(File::Spec->rel2abs($PROGRAM_NAME)); - my $system_dir = File::Spec->catdir($dir, 'SL', 'System'); - return $dir if -d $system_dir && -f File::Spec->catfile($system_dir, 'TaskServer.pm'); + return $cached_exe_dir if defined $cached_exe_dir; + + my $bin_dir = File::Spec->rel2abs($FindBin::Bin); + my @dirs = File::Spec->splitdir($bin_dir); + + $cached_exe_dir = first { -f File::Spec->catdir(@dirs[0..$_], 'SL', 'System', 'TaskServer.pm') } + reverse(0..scalar(@dirs) - 1); + $cached_exe_dir = defined($cached_exe_dir) ? File::Spec->catdir(@dirs[0..$cached_exe_dir]) : File::Spec->curdir; + + return $cached_exe_dir; +} + +sub _parse_number_with_unit { + my ($number) = @_; - my @dirs = reverse File::Spec->splitdir($dir); - shift @dirs; - $dir = File::Spec->catdir(reverse @dirs); - $system_dir = File::Spec->catdir($dir, 'SL', 'System'); - return File::Spec->curdir unless -d $system_dir && -f File::Spec->catfile($system_dir, 'TaskServer.pm'); + return undef unless defined $number; + return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi; - return $dir; + 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; @@ -35,7 +89,7 @@ SL::System::Process - assorted system-relevant functions =head1 SYNOPSIS - # Get base path to Kivitendo scripts + # Get base path to kivitendo scripts my $path = SL::System::Process->exe_dir; =head1 FUNCTIONS @@ -44,10 +98,15 @@ SL::System::Process - assorted system-relevant functions =item C -Returns the absolute path to the directory the Kivitendo executables +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