Skip to content

Instantly share code, notes, and snippets.

@Artefact2
Last active September 3, 2021 13:24
Show Gist options
  • Save Artefact2/567141180965d01de8c2fca8cafb8c05 to your computer and use it in GitHub Desktop.
Save Artefact2/567141180965d01de8c2fca8cafb8c05 to your computer and use it in GitHub Desktop.
file sparseness (quick and dirty script)
#!/usr/bin/env php
<?php
$p = popen('btrfs-search-metadata file '.escapeshellarg($argv[1]), 'r');
$logsz = $physz = null;
$nbins = intval(shell_exec('tput cols')) - 3;
$bins = array_fill(0, $nbins, 0);
while(($line = fgets($p)) !== false) {
$fields = explode(' ', substr($line, 0, -1));
if($fields[0] === 'inode' && $fields[1] === 'objectid') {
$logsz = intval($fields[8]);
$physz = intval($fields[10]);
}
if($fields[0] !== 'extent' || $fields[15] === '0') continue;
$bins[floor(intval($fields[3]) / $logsz * $nbins)] += intval($fields[19]);
}
$binmax = $logsz / $nbins;
for($i = 0; $i < $nbins - 1; ++$i) {
if($bins[$i] <= $binmax) continue;
$bins[$i + 1] += $bins[$i] - $binmax;
$bins[$i] = $binmax;
}
echo 'Logical size: ', $logsz, ' bytes, physical size: ', $physz, ' bytes', PHP_EOL;
echo '[';
foreach($bins as $used) {
$pc = 100.0 * $used / $binmax;
echo match(true) {
$pc === 0.0 => ' ',
$pc < 10.0 => '.',
$pc < 50.0 => 'x',
default => 'X'
};
}
echo ']', PHP_EOL;
/*
Sample output:
% sudo sparseness ~/QEMU/Astero/Astero.img
Logical size: 17179869184 bytes, physical size: 2918068224 bytes
[XXXXXxxXXxXXXXXx...xx x . xx . .x . x .x.]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment