diff mbox

[RFC,1/4] scripts: add stackusage script

Message ID 1434849046-19920-2-git-send-email-linux@rasmusvillemoes.dk (mailing list archive)
State New, archived
Headers show

Commit Message

Rasmus Villemoes June 21, 2015, 1:10 a.m. UTC
The current checkstack.pl script has a few problems, stemming from the
overly simplistic attempt at parsing objdump output with regular
expresions: For example, on x86_64 it doesn't take the push
instruction into account, making it consistently underestimate the
real stack use, and it also doesn't capture stack pointer adjustments
of exactly 128 bytes [1].

Since newer gcc (>= 4.6) knows about -fstack-usage, we might as well
take the information straight from the horse's mouth. This patch
introduces scripts/stackusage, which is a simple wrapper for running
make with EXTRA_CFLAGS set to -fstack-usage. Example use is

scripts/stackusage -o out.su -- -j8 fs/ext4/

Arguments after -- are passed to make. Afterwards, we find all newly
created .su files, massage them a little, sort by stack use and
concatenate the result to a single output file.

[1] Since gcc encodes that by

48 83 c4 80             add    $0xffffffffffffff80,%rsp

and not

48 81 ec 80 00 00 00    sub    $0x80,%rsp

since -128 fits in an imm8.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 scripts/stackusage | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100755 scripts/stackusage

Comments

Michal Marek June 23, 2015, 1:53 p.m. UTC | #1
On 2015-06-21 03:10, Rasmus Villemoes wrote:
> The current checkstack.pl script has a few problems, stemming from the
> overly simplistic attempt at parsing objdump output with regular
> expresions: For example, on x86_64 it doesn't take the push
> instruction into account, making it consistently underestimate the
> real stack use, and it also doesn't capture stack pointer adjustments
> of exactly 128 bytes [1].
> 
> Since newer gcc (>= 4.6) knows about -fstack-usage, we might as well
> take the information straight from the horse's mouth. This patch
> introduces scripts/stackusage, which is a simple wrapper for running
> make with EXTRA_CFLAGS set to -fstack-usage. Example use is
> 
> scripts/stackusage -o out.su -- -j8 fs/ext4/
> 
> Arguments after -- are passed to make. Afterwards, we find all newly
> created .su files, massage them a little, sort by stack use and
> concatenate the result to a single output file.
> 
> [1] Since gcc encodes that by
> 
> 48 83 c4 80             add    $0xffffffffffffff80,%rsp
> 
> and not
> 
> 48 81 ec 80 00 00 00    sub    $0x80,%rsp
> 
> since -128 fits in an imm8.
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  scripts/stackusage | 40 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 40 insertions(+)
>  create mode 100755 scripts/stackusage
> 
> diff --git a/scripts/stackusage b/scripts/stackusage
> new file mode 100755
> index 000000000000..d631af648ae7
> --- /dev/null
> +++ b/scripts/stackusage
> @@ -0,0 +1,40 @@
> +#!/bin/sh
> +
> +outfile=""
> +now=`date +%s`
> +
> +while [ $# -gt 0 ]
> +do
> +    case "$1" in
> +        -o)
> +	    outfile="$2"
> +	    echo "$outfile"
> +	    shift 2;;
> +	-h)
> +	    echo "usage: $0 [-o outfile] -- <make options/args>"
> +	    exit 0;;
> +	--)
> +	    shift
> +	    break;;
> +	-*)
> +            echo >&2 "usage: $0 [-o outfile] -- <make options/args>"
> +	    exit 1;;
> +	*)  break;;
> +    esac
> +done
> +
> +if [ -z "$outfile" ]
> +then
> +    outfile=`mktemp --tmpdir stackusage.$$.XXXX`
> +fi
> +
> +make EXTRA_CFLAGS="-fstack-usage" "$@"

EXTRA_CFLAGS is reserved for use by Makefiles, please use KCFLAGS
instead. I wonder whether it worked at all, because EXTRA_CFLAGS is
reset by scripts/Makefile.build.

Michal
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rasmus Villemoes June 25, 2015, 8:04 a.m. UTC | #2
On Tue, Jun 23 2015, Michal Marek <mmarek@suse.cz> wrote:

>
> EXTRA_CFLAGS is reserved for use by Makefiles, please use KCFLAGS
> instead.

Will do, thanks. Maybe Documentation/SubmitChecklist and
Documentation/development-process/4.Coding wants to be updated.

> I wonder whether it worked at all, because EXTRA_CFLAGS is
> reset by scripts/Makefile.build.

It did indeed work, but only because I passed the setting on the make
command line and not via the environment. I think one needs an override
directive to actually override the settings from the command line:

$ cat Makefile 
VAR1 := a1
VAR2 := a2
override VAR3 := a3
override VAR4 := a4

all:
        @echo $(VAR1) $(VAR2) $(VAR3) $(VAR4)
$ VAR1=b1 VAR3=b3 make VAR2=b2 VAR4=b4 all
a1 b2 a3 a4
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/scripts/stackusage b/scripts/stackusage
new file mode 100755
index 000000000000..d631af648ae7
--- /dev/null
+++ b/scripts/stackusage
@@ -0,0 +1,40 @@ 
+#!/bin/sh
+
+outfile=""
+now=`date +%s`
+
+while [ $# -gt 0 ]
+do
+    case "$1" in
+        -o)
+	    outfile="$2"
+	    echo "$outfile"
+	    shift 2;;
+	-h)
+	    echo "usage: $0 [-o outfile] -- <make options/args>"
+	    exit 0;;
+	--)
+	    shift
+	    break;;
+	-*)
+            echo >&2 "usage: $0 [-o outfile] -- <make options/args>"
+	    exit 1;;
+	*)  break;;
+    esac
+done
+
+if [ -z "$outfile" ]
+then
+    outfile=`mktemp --tmpdir stackusage.$$.XXXX`
+fi
+
+make EXTRA_CFLAGS="-fstack-usage" "$@"
+
+# Prepend directory name to file names, remove line/column
+# information, make file/function/size/type properly tab-separated.
+find . -name '*.su' -newermt "@${now}" -print |                 \
+    xargs perl -MFile::Basename -pe                             \
+      '$d = dirname($ARGV); s#([^:]+):([0-9]+:){2}#$d/$1\t#;' | \
+    sort -k3,3nr > "${outfile}"
+
+echo "$0: output written to ${outfile}"