Skip to content

Commit

Permalink
Fix overhead with serializing variable
Browse files Browse the repository at this point in the history
  • Loading branch information
iliajie committed Aug 3, 2024
1 parent fd51ab9 commit c3a6564
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
51 changes: 0 additions & 51 deletions authentic-funcs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;
Expand Down
61 changes: 54 additions & 7 deletions stats-lib-funcs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -159,7 +155,6 @@ sub get_stats_now
$gadd->('net', [$in, $out]);
}
# Release memory
undef($network);
undef($nrs);
undef($in);
undef($out);
Expand Down Expand Up @@ -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;
1 change: 1 addition & 0 deletions stats.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ my $rs = system_logged(
"SESSION_ID=$main::session_id ".
"$statsserver_cmd @{[quotemeta($port)]} ".
">$logfile 2>&1 </dev/null &");
# Return the result
print_json({ success => !$rs, port => $port,
socket => $get_socket->($port),
new => 1, errlog => $logfile });
Expand Down

0 comments on commit c3a6564

Please sign in to comment.