Last active
March 12, 2019 13:52
-
-
Save dedeibel/e8b2d699ff61b9799dc0184949bf74bf to your computer and use it in GitHub Desktop.
show usage of zram usages and compression ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use Getopt::Long; | |
# | |
# zram-savings.pl -a - show for all zram blocks | |
# -v - show some verbose debug output | |
# | |
sub read_file { | |
my ($path) = @_; | |
my $content = ''; | |
my $fh = IO::File->new(); | |
if ($fh->open("< $path")) { | |
$content .= <$fh>; | |
$fh->close; | |
} | |
return $content; | |
} | |
sub get_name { | |
my ($path) = @_; | |
if ($path =~ m#[^/]+$#) { | |
return $&; | |
} | |
else { | |
die "illegal device path $path"; | |
} | |
} | |
sub get_data { | |
my ($path, $attribute) = @_; | |
my $bytes = read_file($path .'/'. $attribute); | |
chomp $bytes; | |
return $bytes; | |
} | |
sub calc_data { | |
my ($entry) = @_; | |
$entry->{savings} = $entry->{orig_data_size} - $entry->{compr_data_size}; | |
$entry->{gross_savings} = $entry->{orig_data_size} - $entry->{mem_used_total}; | |
$entry->{alloc_overhead} = $entry->{mem_used_total} - $entry->{compr_data_size}; | |
$entry->{savings_percent} = $entry->{savings} / $entry->{orig_data_size} * 100.; | |
$entry->{gross_savings_percent} = $entry->{gross_savings} / $entry->{orig_data_size} * 100.; | |
$entry->{alloc_overhead_percent} = $entry->{alloc_overhead} / $entry->{compr_data_size} * 100.; | |
$entry->{usage} = $entry->{mem_used_total} / $entry->{disksize} * 100.; | |
return $entry; | |
} | |
sub format_bytes { | |
my ($num) = @_; | |
my @suffixes = qw/B KiB MiB GiB/; | |
for my $s (@suffixes) { | |
if ($num / 1024. < 1.) { | |
return sprintf('%.1f %s', $num, $s); | |
} | |
$num /= 1024.; | |
} | |
return sprintf('%.1f %s', $num, 'TiB'); | |
} | |
my $all = 0; | |
my $verbose = 0; | |
my $args = GetOptions("all" => \$all, # flag | |
"verbose" => \$verbose); # flag | |
my @devices = map { | |
{ path => $_, | |
name => get_name($_), | |
orig_data_size => get_data($_, 'orig_data_size'), | |
mem_used_total => get_data($_, 'mem_used_total'), | |
compr_data_size => get_data($_, 'compr_data_size'), | |
disksize => get_data($_, 'disksize') | |
} } glob '/sys/block/zram*'; | |
my $sum = { | |
name => 'sum', | |
orig_data_size => 0, | |
compr_data_size => 0, | |
mem_used_total => 0, | |
disksize => 0, | |
savings => 0, | |
savings_percent => 0, | |
usage => 0 | |
}; | |
foreach (@devices) { | |
my $entry = $_; | |
calc_data($entry); | |
for (qw(orig_data_size compr_data_size mem_used_total disksize)) { | |
$sum->{$_} += $entry->{$_}; | |
} | |
} | |
calc_data($sum); | |
if ($verbose) { | |
require Data::Dumper; | |
print Data::Dumper::Dumper(\@devices), "\n"; | |
} | |
my @toprint = ($sum); | |
unshift(@toprint, @devices) if $all; | |
foreach (@toprint) { | |
my $d = $_; | |
printf "%6s capacity %*s used %3.0f%% %*s savings %3.0f%% %*s (space wasted %3.0f%% %*s)\n", | |
$d->{name}, 10, format_bytes($d->{disksize}), | |
$d->{usage}, 10, format_bytes($d->{mem_used_total}), | |
$d->{savings_percent}, 10, format_bytes($d->{savings}), | |
$d->{alloc_overhead_percent}, 10, format_bytes($d->{alloc_overhead}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment