62df83118367013c6fd70e00f1db81f8b3df5b3a
[kivitendo-erp.git] / SL / System / Process.pm
1 package SL::System::Process;
2
3 use strict;
4
5 use parent qw(Rose::Object);
6
7 use English qw(-no_match_vars);
8 use FindBin;
9 use File::Spec;
10 use File::Basename;
11 use List::Util qw(first);
12
13 my $cached_exe_dir;
14
15 sub exe_dir {
16   return $cached_exe_dir if defined $cached_exe_dir;
17
18   my $bin_dir       = File::Spec->rel2abs($FindBin::Bin);
19   my @dirs          = File::Spec->splitdir($bin_dir);
20
21   $cached_exe_dir   = first { -f File::Spec->catdir(@dirs[0..$_], 'SL', 'System', 'TaskServer.pm') }
22                       reverse(0..scalar(@dirs) - 1);
23   $cached_exe_dir   = defined($cached_exe_dir) ? File::Spec->catdir(@dirs[0..$cached_exe_dir]) : File::Spec->curdir;
24
25   return $cached_exe_dir;
26 }
27
28 sub _parse_number_with_unit {
29   my ($number) = @_;
30
31   return undef   unless defined $number;
32   return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi;
33
34   my %factors = (K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024);
35
36   return $1 * $factors{uc $2};
37 }
38
39 sub memory_usage_is_too_high {
40   return undef unless $::lx_office_conf{system};
41
42   my %limits = (
43     rss  => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_rss}),
44     size => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_vsz}),
45   );
46
47   # $::lxdebug->dump(0, "limits", \%limits);
48
49   return undef unless $limits{rss} || $limits{vsz};
50
51   my %usage;
52
53   my $in = IO::File->new("/proc/$$/status", "r") or return undef;
54
55   while (<$in>) {
56     chomp;
57     $usage{lc $1} = _parse_number_with_unit($2) if m{^ vm(rss|size): \s* (\d+ \s* [kmg]b) \s* $}ix;
58   }
59
60   $in->close;
61
62   # $::lxdebug->dump(0, "usage", \%usage);
63
64   foreach my $type (keys %limits) {
65     next if !$limits{$type};
66     next if $limits{$type} >= ($usage{$type} // 0);
67
68     {
69       no warnings 'once';
70       $::lxdebug->message(LXDebug::WARN(), "Exiting due to memory size limit reached for type '${type}': limit " . $limits{$type} . " bytes, usage " . $usage{$type} . " bytes");
71     }
72
73     return 1;
74   }
75
76   return 0;
77 }
78
79 1;
80 __END__
81
82 =pod
83
84 =encoding utf8
85
86 =head1 NAME
87
88 SL::System::Process - assorted system-relevant functions
89
90 =head1 SYNOPSIS
91
92   # Get base path to kivitendo scripts
93   my $path = SL::System::Process->exe_dir;
94
95 =head1 FUNCTIONS
96
97 =over 4
98
99 =item C<exe_dir>
100
101 Returns the absolute path to the directory the kivitendo executables
102 (C<login.pl> etc.) and modules (sub-directory C<SL/> etc.) are located
103 in.
104
105 =item C<memory_usage_is_too_high>
106
107 Returns true if the current process uses more memory than the configured
108 limits.
109
110 =back
111
112 =head1 BUGS
113
114 Nothing here yet.
115
116 =head1 AUTHOR
117
118 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
119
120 =cut