Skip to content

Instantly share code, notes, and snippets.

@aspiers
Created January 8, 2012 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspiers/1578065 to your computer and use it in GitHub Desktop.
Save aspiers/1578065 to your computer and use it in GitHub Desktop.
quick and dirty tool to analyse `strace -efile` output from Rails boot-up
#!/usr/bin/perl
# Try to figure out why booting up Rails causes so many
# open(2) syscalls which fail with ENOENT.
use strict;
use warnings;
my $TOP_N = 50;
my @syscalls_to_profile = qw(open);
die "Usage: strace -efile COMMAND ARGS 2>&1 | $0\n" if -t 0;
my (%gems, %paths, %syscalls);
chomp(my $gemdir = `rvm gemdir`);
die "rvm gemdir failed" unless -d $gemdir;
my $syscalls_regex = join '|', @syscalls_to_profile;
while (<>) {
if (m!($syscalls_regex)\("\Q$gemdir\E/gems/(.+?)/(.+)", (.+) = -1 ENOENT!) {
my ($syscall, $gem, $subpath, $syscall_params) = ($1, $2, $3, $4);
$syscalls{$syscall}++; # strace -c is more useful than this
$gems{$gem}++;
$paths{$subpath}++;
}
}
print <<EOF;
Top 50 gems
===========
EOF
my $max = 0;
for (sort { $gems{$b} <=> $gems{$a} or $a cmp $b} keys %gems) {
printf "%6d %s\n", $gems{$_}, $_;
last if ++$max > $TOP_N;
}
$max = 0;
print <<EOF;
Top 50 subpaths
===============
EOF
for (sort { $paths{$b} <=> $paths{$a} or $a cmp $b} keys %paths) {
printf "%6d %s\n", $paths{$_}, $_;
last if ++$max > $TOP_N;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment