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
11#
12# Licensed under the Apache License, Version 2.0 (the "License"); you may
13# not use this file except in compliance with the License.
14# You may obtain a copy of the License at
15#
16# http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21# See the License for the specific language governing permissions and
22# limitations under the License.
23
24set -eu
25
26CONFIG_H='include/mbedtls/config.h'
27
28CLIENT='mini_client'
29
30CFLAGS_EXEC='-fno-asynchronous-unwind-tables -Wl,--gc-section -ffunction-sections -fdata-sections'
31CFLAGS_MEM=-g3
32
33if [ -r $CONFIG_H ]; then :; else
34    echo "$CONFIG_H not found" >&2
35    exit 1
36fi
37
38if grep -i cmake Makefile >/dev/null; then
39    echo "Not compatible with CMake" >&2
40    exit 1
41fi
42
43if [ $( uname ) != Linux ]; then
44    echo "Only work on Linux" >&2
45    exit 1
46fi
47
48if git status | grep -F $CONFIG_H >/dev/null 2>&1; then
49    echo "config.h not clean" >&2
50    exit 1
51fi
52
53# make measurements with one configuration
54# usage: do_config <name> <unset-list> <server-args>
55do_config()
56{
57    NAME=$1
58    UNSET_LIST=$2
59    SERVER_ARGS=$3
60
61    echo ""
62    echo "config-$NAME:"
63    cp configs/config-$NAME.h $CONFIG_H
64    scripts/config.py unset MBEDTLS_SSL_SRV_C
65
66    for FLAG in $UNSET_LIST; do
67        scripts/config.py unset $FLAG
68    done
69
70    grep -F SSL_MAX_CONTENT_LEN $CONFIG_H || echo 'SSL_MAX_CONTENT_LEN=16384'
71
72    printf "    Executable size... "
73
74    make clean
75    CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os lib >/dev/null 2>&1
76    cd programs
77    CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os ssl/$CLIENT >/dev/null
78    strip ssl/$CLIENT
79    stat -c '%s' ssl/$CLIENT
80    cd ..
81
82    printf "    Peak ram usage... "
83
84    make clean
85    CFLAGS=$CFLAGS_MEM make OFLAGS=-Os lib >/dev/null 2>&1
86    cd programs
87    CFLAGS=$CFLAGS_MEM make OFLAGS=-Os ssl/$CLIENT >/dev/null
88    cd ..
89
90    ./ssl_server2 $SERVER_ARGS >/dev/null &
91    SRV_PID=$!
92    sleep 1;
93
94    if valgrind --tool=massif --stacks=yes programs/ssl/$CLIENT >/dev/null 2>&1
95    then
96        FAILED=0
97    else
98        echo "client failed" >&2
99        FAILED=1
100    fi
101
102    kill $SRV_PID
103    wait $SRV_PID
104
105    scripts/massif_max.pl massif.out.*
106    mv massif.out.* massif-$NAME.$$
107}
108
109# preparation
110
111CONFIG_BAK=${CONFIG_H}.bak
112cp $CONFIG_H $CONFIG_BAK
113
114rm -f massif.out.*
115
116printf "building server... "
117
118make clean
119make lib >/dev/null 2>&1
120(cd programs && make ssl/ssl_server2) >/dev/null
121cp programs/ssl/ssl_server2 .
122
123echo "done"
124
125# actual measurements
126
127do_config   "ccm-psk-tls1_2" \
128            "" \
129            "psk=000102030405060708090A0B0C0D0E0F"
130
131do_config   "suite-b" \
132            "MBEDTLS_BASE64_C MBEDTLS_PEM_PARSE_C MBEDTLS_CERTS_C" \
133            ""
134
135# cleanup
136
137mv $CONFIG_BAK $CONFIG_H
138make clean
139rm ssl_server2
140
141exit $FAILED
142