memory.sh 3.2 KB

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