blob: c05be74c861031094046deef4ac66ed127cea0e7 [file] [log] [blame]
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +01001#!/bin/sh
2
3# Measure memory usage of a minimal client using a small configuration
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +01004# 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.
Bence Szépkúti700ee442020-05-26 00:33:31 +02008#
Bence Szépkúti1e148272020-08-07 13:07:28 +02009# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +020010# 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.
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +010023
24set -eu
25
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026CONFIG_H='include/mbedtls/config.h'
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010027
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +010028CLIENT='mini_client'
29
Manuel Pégourié-Gonnard47e02142015-03-18 16:52:20 +000030CFLAGS_EXEC='-fno-asynchronous-unwind-tables -Wl,--gc-section -ffunction-sections -fdata-sections'
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010031CFLAGS_MEM=-g3
32
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +010033if [ -r $CONFIG_H ]; then :; else
34 echo "$CONFIG_H not found" >&2
35 exit 1
36fi
37
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +010038if grep -i cmake Makefile >/dev/null; then
39 echo "Not compatible with CMake" >&2
40 exit 1
41fi
42
Manuel Pégourié-Gonnard4a7ed712015-03-11 10:26:50 +000043if [ $( uname ) != Linux ]; then
44 echo "Only work on Linux" >&2
45 exit 1
46fi
47
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +010048if git status | grep -F $CONFIG_H >/dev/null 2>&1; then
49 echo "config.h not clean" >&2
50 exit 1
51fi
52
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010053# 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
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020064 scripts/config.py unset MBEDTLS_SSL_SRV_C
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010065
66 for FLAG in $UNSET_LIST; do
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020067 scripts/config.py unset $FLAG
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010068 done
69
Manuel Pégourié-Gonnarda6b95f02015-09-09 13:47:28 +020070 grep -F SSL_MAX_CONTENT_LEN $CONFIG_H || echo 'SSL_MAX_CONTENT_LEN=16384'
71
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010072 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
Manuel Pégourié-Gonnard4a7ed712015-03-11 10:26:50 +000079 stat -c '%s' ssl/$CLIENT
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +010080 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
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +0100109# preparation
110
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +0100111CONFIG_BAK=${CONFIG_H}.bak
112cp $CONFIG_H $CONFIG_BAK
113
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +0100114rm -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
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +0100125# actual measurements
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +0100126
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +0100127do_config "ccm-psk-tls1_2" \
128 "" \
129 "psk=000102030405060708090A0B0C0D0E0F"
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +0100130
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +0100131do_config "suite-b" \
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100132 "MBEDTLS_BASE64_C MBEDTLS_PEM_PARSE_C" \
Manuel Pégourié-Gonnardc5b849b2014-12-01 12:15:47 +0100133 ""
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +0100134
135# cleanup
136
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +0100137mv $CONFIG_BAK $CONFIG_H
Manuel Pégourié-Gonnardf166c542014-12-01 11:30:56 +0100138make clean
139rm ssl_server2
Manuel Pégourié-Gonnard4d5cc112014-11-25 12:21:48 +0100140
141exit $FAILED