memory.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. set -eu
  8. CONFIG_H='include/mbedtls/config.h'
  9. CLIENT='mini_client'
  10. CFLAGS_EXEC='-fno-asynchronous-unwind-tables -Wl,--gc-section -ffunction-sections -fdata-sections'
  11. CFLAGS_MEM=-g3
  12. if [ -r $CONFIG_H ]; then :; else
  13. echo "$CONFIG_H not found" >&2
  14. exit 1
  15. fi
  16. if grep -i cmake Makefile >/dev/null; then
  17. echo "Not compatible with CMake" >&2
  18. exit 1
  19. fi
  20. if [ $( uname ) != Linux ]; then
  21. echo "Only work on Linux" >&2
  22. exit 1
  23. fi
  24. if git status | grep -F $CONFIG_H >/dev/null 2>&1; then
  25. echo "config.h not clean" >&2
  26. exit 1
  27. fi
  28. # make measurements with one configuration
  29. # usage: do_config <name> <unset-list> <server-args>
  30. do_config()
  31. {
  32. NAME=$1
  33. UNSET_LIST=$2
  34. SERVER_ARGS=$3
  35. echo ""
  36. echo "config-$NAME:"
  37. cp configs/config-$NAME.h $CONFIG_H
  38. scripts/config.pl unset MBEDTLS_SSL_SRV_C
  39. for FLAG in $UNSET_LIST; do
  40. scripts/config.pl unset $FLAG
  41. done
  42. grep -F SSL_MAX_CONTENT_LEN $CONFIG_H || echo 'SSL_MAX_CONTENT_LEN=16384'
  43. printf " Executable size... "
  44. make clean
  45. CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os lib >/dev/null 2>&1
  46. cd programs
  47. CFLAGS=$CFLAGS_EXEC make OFLAGS=-Os ssl/$CLIENT >/dev/null
  48. strip ssl/$CLIENT
  49. stat -c '%s' ssl/$CLIENT
  50. cd ..
  51. printf " Peak ram usage... "
  52. make clean
  53. CFLAGS=$CFLAGS_MEM make OFLAGS=-Os lib >/dev/null 2>&1
  54. cd programs
  55. CFLAGS=$CFLAGS_MEM make OFLAGS=-Os ssl/$CLIENT >/dev/null
  56. cd ..
  57. ./ssl_server2 $SERVER_ARGS >/dev/null &
  58. SRV_PID=$!
  59. sleep 1;
  60. if valgrind --tool=massif --stacks=yes programs/ssl/$CLIENT >/dev/null 2>&1
  61. then
  62. FAILED=0
  63. else
  64. echo "client failed" >&2
  65. FAILED=1
  66. fi
  67. kill $SRV_PID
  68. wait $SRV_PID
  69. scripts/massif_max.pl massif.out.*
  70. mv massif.out.* massif-$NAME.$$
  71. }
  72. # preparation
  73. CONFIG_BAK=${CONFIG_H}.bak
  74. cp $CONFIG_H $CONFIG_BAK
  75. rm -f massif.out.*
  76. printf "building server... "
  77. make clean
  78. make lib >/dev/null 2>&1
  79. (cd programs && make ssl/ssl_server2) >/dev/null
  80. cp programs/ssl/ssl_server2 .
  81. echo "done"
  82. # actual measurements
  83. do_config "ccm-psk-tls1_2" \
  84. "" \
  85. "psk=000102030405060708090A0B0C0D0E0F"
  86. do_config "suite-b" \
  87. "MBEDTLS_BASE64_C MBEDTLS_PEM_PARSE_C MBEDTLS_CERTS_C" \
  88. ""
  89. # cleanup
  90. mv $CONFIG_BAK $CONFIG_H
  91. make clean
  92. rm ssl_server2
  93. exit $FAILED