From c3a6564f0b14ab83c0e0e8b22d2e11306e662d09 Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Sat, 3 Aug 2024 17:10:26 +0300 Subject: [PATCH] Fix overhead with serializing variable --- authentic-funcs.pl | 51 -------------------------------------- stats-lib-funcs.pl | 61 ++++++++++++++++++++++++++++++++++++++++------ stats.cgi | 1 + 3 files changed, 55 insertions(+), 58 deletions(-) diff --git a/authentic-funcs.pl b/authentic-funcs.pl index 9784ecc90..69524e701 100644 --- a/authentic-funcs.pl +++ b/authentic-funcs.pl @@ -696,57 +696,6 @@ sub current_to_pid write_file($tmp_file, \%pid); } -sub network_stats -{ - # Get network data from all interfaces - my ($type) = @_; - my $file = "/proc/net/dev"; - return () unless -r $file; - open(my $dev, $file); - my (@titles, %result); - while (my $line = <$dev>) { - chomp($line); - if ($line =~ /^.{6}\|([^\\]+)\|([^\\]+)$/) { - my ($rec, $trans) = ($1, $2); - @titles = ((map {"r$_"} split(/\s+/, $rec)), (map {"t$_"} split(/\s+/, $trans))); - } elsif ($line =~ /^\s*([^:]+):\s*(.*)$/) { - my ($id, @data) = ($1, split(/\s+/, $2)); - $result{$id} = { map {$titles[$_] => $data[$_];} (0 .. $#titles) }; - } - } - close($dev); - - # Return current network I/O - if ($type eq 'io') { - my ($rbytes, $tbytes, $rbytes2, $tbytes2) = (0, 0, 0, 0); - my @rs; - my $results = \%result; - - # Parse current data - foreach (%$results) { - $rbytes += $results->{$_}->{'rbytes'}; - $tbytes += $results->{$_}->{'tbytes'}; - } - - # Wait for one second and fetch data over again - sleep 1, $results = network_stats(); - - # Parse data after dalay - foreach (%$results) { - $rbytes2 += $results->{$_}->{'rbytes'}; - $tbytes2 += $results->{$_}->{'tbytes'}; - } - - # Return current network I/O - $rbytes = int($rbytes2 - $rbytes); - $tbytes = int($tbytes2 - $tbytes); - - @rs = ($rbytes, $tbytes); - return serialise_variable(\@rs); - } - return \%result; -} - sub acl_system_status { my ($show) = @_; diff --git a/stats-lib-funcs.pl b/stats-lib-funcs.pl index ac7e95508..ef87018ad 100644 --- a/stats-lib-funcs.pl +++ b/stats-lib-funcs.pl @@ -139,18 +139,14 @@ sub get_stats_now } # Number of running processes if ($acl_system_status->{'load'}) { - my @processes = proc::list_processes(); - my $proc = scalar(@processes); + my $proc = proc::count_processes(); $data{'proc'} = $proc; $gadd->('proc', $proc); - # Release memory - undef(@processes); } } # Network I/O if ($acl_system_status->{'load'}) { - my $network = network_stats('io'); - my $nrs = unserialise_variable($network); + my $nrs = stats_network('io'); my $in = @{$nrs}[0]; my $out = @{$nrs}[1]; @@ -159,7 +155,6 @@ sub get_stats_now $gadd->('net', [$in, $out]); } # Release memory - undef($network); undef($nrs); undef($in); undef($out); @@ -281,4 +276,56 @@ sub save_stats_history undef($graphs); } +sub stats_network +{ + # Get network data from all interfaces + my ($type) = @_; + my $file = "/proc/net/dev"; + return () unless -r $file; + open(my $dev, $file); + my (@titles, %result); + while (my $line = <$dev>) { + chomp($line); + if ($line =~ /^.{6}\|([^\\]+)\|([^\\]+)$/) { + my ($rec, $trans) = ($1, $2); + @titles = ((map {"r$_"} split(/\s+/, $rec)), (map {"t$_"} split(/\s+/, $trans))); + } elsif ($line =~ /^\s*([^:]+):\s*(.*)$/) { + my ($id, @data) = ($1, split(/\s+/, $2)); + $result{$id} = { map {$titles[$_] => $data[$_];} (0 .. $#titles) }; + } + } + close($dev); + + # Return current network I/O + if ($type eq 'io') { + my ($rbytes, $tbytes, $rbytes2, $tbytes2) = (0, 0, 0, 0); + my @rs; + my $results = \%result; + + # Parse current data + foreach (%$results) { + $rbytes += $results->{$_}->{'rbytes'}; + $tbytes += $results->{$_}->{'tbytes'}; + } + + # Wait for quater of a second and fetch data over again + select(undef, undef, undef, 0.25); + $results = stats_network(); + + # Parse data after dalay + foreach (%$results) { + $rbytes2 += $results->{$_}->{'rbytes'}; + $tbytes2 += $results->{$_}->{'tbytes'}; + } + + # Return current network I/O + $rbytes = int($rbytes2 - $rbytes); + $tbytes = int($tbytes2 - $tbytes); + + @rs = ($rbytes, $tbytes); + return \@rs; + } + return \%result; +} + 1; diff --git a/stats.cgi b/stats.cgi index f5cb6f275..2964f02f6 100755 --- a/stats.cgi +++ b/stats.cgi @@ -100,6 +100,7 @@ my $rs = system_logged( "SESSION_ID=$main::session_id ". "$statsserver_cmd @{[quotemeta($port)]} ". ">$logfile 2>&1 !$rs, port => $port, socket => $get_socket->($port), new => 1, errlog => $logfile });