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