xref: /openbmc/linux/scripts/stackdelta (revision b2441318)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
35bbb9f75SRasmus Villemoes
45bbb9f75SRasmus Villemoes# Read two files produced by the stackusage script, and show the
55bbb9f75SRasmus Villemoes# delta between them.
65bbb9f75SRasmus Villemoes#
75bbb9f75SRasmus Villemoes# Currently, only shows changes for functions listed in both files. We
85bbb9f75SRasmus Villemoes# could add an option to show also functions which have vanished or
95bbb9f75SRasmus Villemoes# appeared (which would often be due to gcc making other inlining
105bbb9f75SRasmus Villemoes# decisions).
115bbb9f75SRasmus Villemoes#
125bbb9f75SRasmus Villemoes# Another possible option would be a minimum absolute value for the
135bbb9f75SRasmus Villemoes# delta.
145bbb9f75SRasmus Villemoes#
155bbb9f75SRasmus Villemoes# A third possibility is for sorting by delta, but that can be
165bbb9f75SRasmus Villemoes# achieved by piping to sort -k5,5g.
175bbb9f75SRasmus Villemoes
185bbb9f75SRasmus Villemoessub read_stack_usage_file {
195bbb9f75SRasmus Villemoes    my %su;
205bbb9f75SRasmus Villemoes    my $f = shift;
215bbb9f75SRasmus Villemoes    open(my $fh, '<', $f)
225bbb9f75SRasmus Villemoes	or die "cannot open $f: $!";
235bbb9f75SRasmus Villemoes    while (<$fh>) {
245bbb9f75SRasmus Villemoes	chomp;
255bbb9f75SRasmus Villemoes	my ($file, $func, $size, $type) = split;
265bbb9f75SRasmus Villemoes	# Old versions of gcc (at least 4.7) have an annoying quirk in
275bbb9f75SRasmus Villemoes	# that a (static) function whose name has been changed into
285bbb9f75SRasmus Villemoes	# for example ext4_find_unwritten_pgoff.isra.11 will show up
295bbb9f75SRasmus Villemoes	# in the .su file with a name of just "11". Since such a
305bbb9f75SRasmus Villemoes	# numeric suffix is likely to change across different
315bbb9f75SRasmus Villemoes	# commits/compilers/.configs or whatever else we're trying to
325bbb9f75SRasmus Villemoes	# tweak, we can't really track those functions, so we just
335bbb9f75SRasmus Villemoes	# silently skip them.
345bbb9f75SRasmus Villemoes	#
355bbb9f75SRasmus Villemoes	# Newer gcc (at least 5.0) report the full name, so again,
365bbb9f75SRasmus Villemoes	# since the suffix is likely to change, we strip it.
375bbb9f75SRasmus Villemoes	next if $func =~ m/^[0-9]+$/;
385bbb9f75SRasmus Villemoes	$func =~ s/\..*$//;
395bbb9f75SRasmus Villemoes	# Line numbers are likely to change; strip those.
405bbb9f75SRasmus Villemoes	$file =~ s/:[0-9]+$//;
415bbb9f75SRasmus Villemoes	$su{"${file}\t${func}"} = {size => $size, type => $type};
425bbb9f75SRasmus Villemoes    }
435bbb9f75SRasmus Villemoes    close($fh);
445bbb9f75SRasmus Villemoes    return \%su;
455bbb9f75SRasmus Villemoes}
465bbb9f75SRasmus Villemoes
475bbb9f75SRasmus Villemoes@ARGV == 2
485bbb9f75SRasmus Villemoes    or die "usage: $0 <old> <new>";
495bbb9f75SRasmus Villemoes
505bbb9f75SRasmus Villemoesmy $old = read_stack_usage_file($ARGV[0]);
515bbb9f75SRasmus Villemoesmy $new = read_stack_usage_file($ARGV[1]);
525bbb9f75SRasmus Villemoesmy @common = sort grep {exists $new->{$_}} keys %$old;
535bbb9f75SRasmus Villemoesfor (@common) {
545bbb9f75SRasmus Villemoes    my $x = $old->{$_}{size};
555bbb9f75SRasmus Villemoes    my $y = $new->{$_}{size};
565bbb9f75SRasmus Villemoes    my $delta = $y - $x;
575bbb9f75SRasmus Villemoes    if ($delta) {
585bbb9f75SRasmus Villemoes	printf "%s\t%d\t%d\t%+d\n", $_, $x, $y, $delta;
595bbb9f75SRasmus Villemoes    }
605bbb9f75SRasmus Villemoes}
61