| Hanno Becker | 43fa8d6 | 2019-03-18 17:12:05 +0000 | [diff] [blame] | 1 | #!/bin/sh | 
|  | 2 | # generate_codesize_stats.sh | 
|  | 3 | # | 
|  | 4 | # This file is part of mbed TLS (https://tls.mbed.org) | 
|  | 5 | # | 
|  | 6 | # Copyright (c) 2019, ARM Limited, All Rights Reserved | 
|  | 7 | # | 
|  | 8 | # Purpose | 
|  | 9 | # | 
|  | 10 | # Generate static memory usage statistics for an Mbed TLS build. | 
|  | 11 | # | 
|  | 12 |  | 
|  | 13 | set -eu | 
|  | 14 |  | 
|  | 15 | if [ -d include/mbedtls ]; then :; else | 
|  | 16 | echo "$0: must be run from root" >&2 | 
|  | 17 | exit 1 | 
|  | 18 | fi | 
|  | 19 |  | 
|  | 20 | if grep -i cmake Makefile >/dev/null; then | 
|  | 21 | echo "$0: not compatible with cmake" >&2 | 
|  | 22 | exit 1 | 
|  | 23 | fi | 
|  | 24 |  | 
|  | 25 | BUILD_DIR="./library" | 
|  | 26 |  | 
|  | 27 | LIBFILES=$( ls $BUILD_DIR/*.a ) | 
|  | 28 | OBJFILES=$( ls $BUILD_DIR/*.o ) | 
|  | 29 |  | 
|  | 30 | SUMMARY_ONLY=0 | 
|  | 31 | LIMIT=9999 | 
|  | 32 |  | 
|  | 33 | print_usage() { | 
|  | 34 | echo "\nExtract static memory usage statistics for an Mbed TLS build.\n" | 
|  | 35 | echo "Usage: $0 [options]" | 
|  | 36 | echo "  --files\tGenerate per-file code-size statistics." | 
|  | 37 | echo "  --syms\tGenerate per-symbol code-size statistics." | 
|  | 38 | echo "  -l|--limit num\tPrint only the largest 'num' symbols of the given type. (Default: $LIMIT) " | 
|  | 39 | echo "  -h|--help\tPrint this help." | 
|  | 40 | echo "  -d|--dir=BUILD_DIR\tThe build directory containing the 'library' folder (default: ${BUILD_DIR})" | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | get_options() { | 
|  | 44 | while [ $# -gt 0 ]; do | 
|  | 45 | case "$1" in | 
|  | 46 | -d|--dir) | 
|  | 47 | shift; BUILD_DIR=$1 | 
|  | 48 | ;; | 
|  | 49 | -h|--help) | 
|  | 50 | print_usage | 
|  | 51 | exit 0 | 
|  | 52 | ;; | 
|  | 53 | --files) | 
|  | 54 | FILE_STATS=1 | 
|  | 55 | ;; | 
|  | 56 | --syms) | 
|  | 57 | SYM_STATS=1 | 
|  | 58 | ;; | 
|  | 59 | -l|--limit) | 
|  | 60 | shift; LIMIT=$1 | 
|  | 61 | ;; | 
|  | 62 | -n|--name) | 
|  | 63 | shift; name=$1 | 
|  | 64 | ;; | 
|  | 65 | -i|--info) | 
|  | 66 | shift; info=$1 | 
|  | 67 | ;; | 
|  | 68 | *) | 
|  | 69 | echo "Unknown argument: $1" | 
|  | 70 | print_usage | 
|  | 71 | exit 1 | 
|  | 72 | ;; | 
|  | 73 | esac | 
|  | 74 | shift | 
|  | 75 | done | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | FILE_STATS=0 | 
|  | 79 | SYM_STATS=0 | 
|  | 80 | name="unnamed" | 
|  | 81 | info="noinfo" | 
|  | 82 | get_options "$@" | 
|  | 83 |  | 
|  | 84 | date=$( date +%Y-%m-%d-%H-%M-%S ) | 
|  | 85 |  | 
|  | 86 | report_syms() { | 
|  | 87 | file=$(basename $1) | 
|  | 88 | type=$2 | 
|  | 89 | stat=$(nm --line-numbers --radix=d --size-sort --reverse $1 | | 
|  | 90 | grep " [$3] "  | | 
|  | 91 | sort --reverse | | 
|  | 92 | head -n $LIMIT | | 
|  | 93 | awk -v type="$type" -v info="$info" -v name="$name" -v date="$date" -v file="$file" \ | 
|  | 94 | '{ printf( "%10s %42s %12s %20s %8s %6d %s\n", date, name, info, file, type, $1, $3 ); }') | 
|  | 95 | if [ -n "$stat" ]; then | 
|  | 96 | echo "$stat" | 
|  | 97 | fi | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | # Report static memory usage (RAM and ROM) | 
|  | 101 | if [ $FILE_STATS -eq 1 ]; then | 
|  | 102 | for file_full in $LIBFILES; do | 
|  | 103 | file=$(basename $file_full) | 
|  | 104 | size --radix=10 $file_full   | | 
|  | 105 | sort -s -n -k 1,1        | | 
|  | 106 | tail -n +2               | | 
|  | 107 | sed -n '/^[ ]*0/!p'      | | 
|  | 108 | awk -v info="$info" -v name="$name" -v date="$date" '{ printf( "%10s %42s %12s %20s %6d %6d %6d\n", date, name, info, $6, $1, $2, $3 ); }' | | 
|  | 109 | awk -v info="$info" -v name="$name" -v date="$date" -v file="$file" '{print $0; sum_text += $5; sum_data += $6; sum_bss += $7} | 
|  | 110 | END { printf( "%10s %42s %12s %20s %6d %6d %6d\n\n", date, name, info, file, sum_text, sum_data, sum_bss ); }' | 
|  | 111 | done | 
|  | 112 | fi | 
|  | 113 |  | 
|  | 114 | if [ $SYM_STATS -eq 1 ]; then | 
|  | 115 | SYMTYPES="CODE-tT DATA-dD RODATA-rR BSS-bB" | 
|  | 116 | for symtype in $SYMTYPES; do | 
|  | 117 | type=${symtype%*-*} | 
|  | 118 | specifier=${symtype#*-*} | 
|  | 119 | for file_full in $OBJFILES; do | 
|  | 120 | report_syms "$file_full" $type $specifier | 
|  | 121 | done | 
|  | 122 | done | 
|  | 123 | fi |