memory.sh 4.1 KB

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