files.pl (1744B)
1 #!/usr/bin/perl -w 2 # 3 # files.pl Print file sizes in folded format, for a flame graph. 4 # 5 # This helps you understand storage consumed by a file system, by creating 6 # a flame graph visualization of space consumed. This is basically a Perl 7 # version of the "find" command, which emits in folded format for piping 8 # into flamegraph.pl. 9 # 10 # Copyright (c) 2017 Brendan Gregg. 11 # Licensed under the Apache License, Version 2.0 (the "License") 12 # 13 # 03-Feb-2017 Brendan Gregg Created this. 14 15 use strict; 16 use File::Find; 17 18 sub usage { 19 print STDERR "USAGE: $0 [--xdev] [DIRECTORY]...\n"; 20 print STDERR " eg, $0 /Users\n"; 21 print STDERR " To not descend directories on other filesystems:"; 22 print STDERR " eg, $0 --xdev /\n"; 23 print STDERR "Intended to be piped to flamegraph.pl. Full example:\n"; 24 print STDERR " $0 /Users | flamegraph.pl " . 25 "--hash --countname=bytes > files.svg\n"; 26 print STDERR " $0 /usr /home /root /etc | flamegraph.pl " . 27 "--hash --countname=bytes > files.svg\n"; 28 print STDERR " $0 --xdev / | flamegraph.pl " . 29 "--hash --countname=bytes > files.svg\n"; 30 exit 1; 31 } 32 33 usage() if @ARGV == 0 or $ARGV[0] eq "--help" or $ARGV[0] eq "-h"; 34 35 my $filter_xdev = 0; 36 my $xdev_id; 37 38 foreach my $dir (@ARGV) { 39 if ($dir eq "--xdev") { 40 $filter_xdev = 1; 41 } else { 42 find(\&wanted, $dir); 43 } 44 } 45 46 sub wanted { 47 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = lstat($_); 48 return unless defined $size; 49 if ($filter_xdev) { 50 if (!$xdev_id) { 51 $xdev_id = $dev; 52 } elsif ($xdev_id ne $dev) { 53 $File::Find::prune = 1; 54 return; 55 } 56 } 57 my $path = $File::Find::name; 58 $path =~ tr/\//;/; # delimiter 59 $path =~ tr/;.a-zA-Z0-9-/_/c; # ditch whitespace and other chars 60 $path =~ s/^;//; 61 print "$path $size\n"; 62 }