1#!/bin/sh
2
3# Measure memory usage of a minimal client using a small configuration
4# Currently hardwired to ccm-psk and suite-b, may be expanded later
5#
6# Use different build options for measuring executable size and memory usage,
7# since for memory we want debug information.
8
9set -eu
10
11CONFIG_H='include/mbedtls/config.h'
12
13CLIENT='mini_client'
14
15CFLAGS_EXEC='-fno-asynchronous-unwind-tables -Wl,--gc-section -ffunction-sections -fdata-sections'
16CFLAGS_MEM=-g3
17
18if [ -r $CONFIG_H ]; then :; else
19    echo "$CONFIG_H not found" >&2
20    exit 1
21fi
22
23if grep -i cmake Makefile >/dev/null; then
24    echo "Not compatible with CMake" >&2
25    exit 1
26fi
27
28if [ $( uname ) != Linux ]; then
29    echo "Only work on Linux" >&2
30    exit 1
31fi
32
33if git status | grep -F $CONFIG_H >/dev/null 2>&1; then
34    echo "config.h not clean" >&2
35    exit 1
36fi
37
38# make measurements with one configuration
39# usage: do_config <name> <unset-list> <server-args>
40do_config()
41{
42    NAME=$1
43    UNSET_LIST=$2
44    SERVER_ARGS=$3
45
46    echo ""
47    echo "config-$NAME:"
48    cp configs/config-$NAME.h $CONFIG_H
49    scripts/config.pl unset MBEDTLS_SSL_SRV_C
50
51    for FLAG in $UNSET_LIST; do
52        scripts/config.pl unset $FLAG
53    done
54
55    grep -F SSL_MAX_CONTENT_LEN $CONFIG_H || echo 'SSL_MAX_CONTENT_LEN=16384'
56
57    printf "    Executable size... "
58
59    make clean
60    CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os lib >/dev/null 2>&1
61    cd programs
62    CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os ssl/$CLIENT >/dev/null
63    strip ssl/$CLIENT
64    stat -c '%s' ssl/$CLIENT
65    cd ..
66
67    printf "    Peak ram usage... "
68
69    make clean
70    CFLAGS=$CFLAGS_MEM make OFLAGS=-Os lib >/dev/null 2>&1
71    cd programs
72    CFLAGS=$CFLAGS_MEM make OFLAGS=-Os ssl/$CLIENT >/dev/null
73    cd ..
74
75    ./ssl_server2 $SERVER_ARGS >/dev/null &
76    SRV_PID=$!
77    sleep 1;
78
79    if valgrind --tool=massif --stacks=yes programs/ssl/$CLIENT >/dev/null 2>&1
80    then
81        FAILED=0
82    else
83        echo "client failed" >&2
84        FAILED=1
85    fi
86
87    kill $SRV_PID
88    wait $SRV_PID
89
90    scripts/massif_max.pl massif.out.*
91    mv massif.out.* massif-$NAME.$$
92}
93
94# preparation
95
96CONFIG_BAK=${CONFIG_H}.bak
97cp $CONFIG_H $CONFIG_BAK
98
99rm -f massif.out.*
100
101printf "building server... "
102
103make clean
104make lib >/dev/null 2>&1
105(cd programs && make ssl/ssl_server2) >/dev/null
106cp programs/ssl/ssl_server2 .
107
108echo "done"
109
110# actual measurements
111
112do_config   "ccm-psk-tls1_2" \
113            "" \
114            "psk=000102030405060708090A0B0C0D0E0F"
115
116do_config   "suite-b" \
117            "MBEDTLS_BASE64_C MBEDTLS_PEM_PARSE_C MBEDTLS_CERTS_C" \
118            ""
119
120# cleanup
121
122mv $CONFIG_BAK $CONFIG_H
123make clean
124rm ssl_server2
125
126exit $FAILED
127