• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#
12# This file is provided under the Apache License 2.0, or the
13# GNU General Public License v2.0 or later.
14#
15# **********
16# Apache License 2.0:
17#
18# Licensed under the Apache License, Version 2.0 (the "License"); you may
19# not use this file except in compliance with the License.
20# You may obtain a copy of the License at
21#
22# http://www.apache.org/licenses/LICENSE-2.0
23#
24# Unless required by applicable law or agreed to in writing, software
25# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27# See the License for the specific language governing permissions and
28# limitations under the License.
29#
30# **********
31#
32# **********
33# GNU General Public License v2.0 or later:
34#
35# This program is free software; you can redistribute it and/or modify
36# it under the terms of the GNU General Public License as published by
37# the Free Software Foundation; either version 2 of the License, or
38# (at your option) any later version.
39#
40# This program is distributed in the hope that it will be useful,
41# but WITHOUT ANY WARRANTY; without even the implied warranty of
42# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43# GNU General Public License for more details.
44#
45# You should have received a copy of the GNU General Public License along
46# with this program; if not, write to the Free Software Foundation, Inc.,
47# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48#
49# **********
50
51set -eu
52
53CONFIG_H='include/mbedtls/config.h'
54
55CLIENT='mini_client'
56
57CFLAGS_EXEC='-fno-asynchronous-unwind-tables -Wl,--gc-section -ffunction-sections -fdata-sections'
58CFLAGS_MEM=-g3
59
60if [ -r $CONFIG_H ]; then :; else
61    echo "$CONFIG_H not found" >&2
62    exit 1
63fi
64
65if grep -i cmake Makefile >/dev/null; then
66    echo "Not compatible with CMake" >&2
67    exit 1
68fi
69
70if [ $( uname ) != Linux ]; then
71    echo "Only work on Linux" >&2
72    exit 1
73fi
74
75if git status | grep -F $CONFIG_H >/dev/null 2>&1; then
76    echo "config.h not clean" >&2
77    exit 1
78fi
79
80# make measurements with one configuration
81# usage: do_config <name> <unset-list> <server-args>
82do_config()
83{
84    NAME=$1
85    UNSET_LIST=$2
86    SERVER_ARGS=$3
87
88    echo ""
89    echo "config-$NAME:"
90    cp configs/config-$NAME.h $CONFIG_H
91    scripts/config.pl unset MBEDTLS_SSL_SRV_C
92
93    for FLAG in $UNSET_LIST; do
94        scripts/config.pl unset $FLAG
95    done
96
97    grep -F SSL_MAX_CONTENT_LEN $CONFIG_H || echo 'SSL_MAX_CONTENT_LEN=16384'
98
99    printf "    Executable size... "
100
101    make clean
102    CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os lib >/dev/null 2>&1
103    cd programs
104    CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os ssl/$CLIENT >/dev/null
105    strip ssl/$CLIENT
106    stat -c '%s' ssl/$CLIENT
107    cd ..
108
109    printf "    Peak ram usage... "
110
111    make clean
112    CFLAGS=$CFLAGS_MEM make OFLAGS=-Os lib >/dev/null 2>&1
113    cd programs
114    CFLAGS=$CFLAGS_MEM make OFLAGS=-Os ssl/$CLIENT >/dev/null
115    cd ..
116
117    ./ssl_server2 $SERVER_ARGS >/dev/null &
118    SRV_PID=$!
119    sleep 1;
120
121    if valgrind --tool=massif --stacks=yes programs/ssl/$CLIENT >/dev/null 2>&1
122    then
123        FAILED=0
124    else
125        echo "client failed" >&2
126        FAILED=1
127    fi
128
129    kill $SRV_PID
130    wait $SRV_PID
131
132    scripts/massif_max.pl massif.out.*
133    mv massif.out.* massif-$NAME.$$
134}
135
136# preparation
137
138CONFIG_BAK=${CONFIG_H}.bak
139cp $CONFIG_H $CONFIG_BAK
140
141rm -f massif.out.*
142
143printf "building server... "
144
145make clean
146make lib >/dev/null 2>&1
147(cd programs && make ssl/ssl_server2) >/dev/null
148cp programs/ssl/ssl_server2 .
149
150echo "done"
151
152# actual measurements
153
154do_config   "ccm-psk-tls1_2" \
155            "" \
156            "psk=000102030405060708090A0B0C0D0E0F"
157
158do_config   "suite-b" \
159            "MBEDTLS_BASE64_C MBEDTLS_PEM_PARSE_C MBEDTLS_CERTS_C" \
160            ""
161
162# cleanup
163
164mv $CONFIG_BAK $CONFIG_H
165make clean
166rm ssl_server2
167
168exit $FAILED
169