files.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. use strict;
  15. use File::Find;
  16. sub usage {
  17. print STDERR "USAGE: $0 [--xdev] [DIRECTORY]...\n";
  18. print STDERR " eg, $0 /Users\n";
  19. print STDERR " To not descend directories on other filesystems:";
  20. print STDERR " eg, $0 --xdev /\n";
  21. print STDERR "Intended to be piped to flamegraph.pl. Full example:\n";
  22. print STDERR " $0 /Users | flamegraph.pl " .
  23. "--hash --countname=bytes > files.svg\n";
  24. print STDERR " $0 /usr /home /root /etc | flamegraph.pl " .
  25. "--hash --countname=bytes > files.svg\n";
  26. print STDERR " $0 --xdev / | flamegraph.pl " .
  27. "--hash --countname=bytes > files.svg\n";
  28. exit 1;
  29. }
  30. usage() if @ARGV == 0 or $ARGV[0] eq "--help" or $ARGV[0] eq "-h";
  31. my $filter_xdev = 0;
  32. my $xdev_id;
  33. foreach my $dir (@ARGV) {
  34. if ($dir eq "--xdev") {
  35. $filter_xdev = 1;
  36. } else {
  37. find(\&wanted, $dir);
  38. }
  39. }
  40. sub wanted {
  41. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = lstat($_);
  42. return unless defined $size;
  43. if ($filter_xdev) {
  44. if (!$xdev_id) {
  45. $xdev_id = $dev;
  46. } elsif ($xdev_id ne $dev) {
  47. $File::Find::prune = 1;
  48. return;
  49. }
  50. }
  51. my $path = $File::Find::name;
  52. $path =~ tr/\//;/; # delimiter
  53. $path =~ tr/;.a-zA-Z0-9-/_/c; # ditch whitespace and other chars
  54. $path =~ s/^;//;
  55. print "$path $size\n";
  56. }