From ee0595f5437dbfaa08b84f3e2686c1d5047a39ec Mon Sep 17 00:00:00 2001 From: toddouska Date: Thu, 28 Mar 2013 11:28:38 -0700 Subject: [PATCH] add --enable-stacksize to print out stack use info with pthreads for example client/server --- configure.ac | 15 +++++++++++ cyassl/test.h | 58 ++++++++++++++++++++++++++++++++++++++++ examples/client/client.c | 10 +++++-- examples/client/client.h | 2 +- examples/server/server.c | 4 +++ examples/server/server.h | 2 +- testsuite/testsuite.c | 2 +- 7 files changed, 88 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 3b1b872b3..f358684e5 100644 --- a/configure.ac +++ b/configure.ac @@ -570,6 +570,21 @@ else fi +# STACK SIZE info for examples +AC_ARG_ENABLE([stacksize], + [ --enable-stacksize Enable stack size info on examples (default: disabled)], + [ ENABLED_STACKSIZE=$enableval ], + [ ENABLED_STACKSIZE=no ] + ) + +if test "$ENABLED_STACKSIZE" = "yes" +then + AC_CHECK_FUNC([posix_memalign], [], [AC_MSG_ERROR(stacksize needs posix_memalign)]) + AC_CHECK_FUNC([pthread_attr_setstack], [], [AC_MSG_ERROR(stacksize needs pthread_attr_setstack)]) + AM_CFLAGS="$AM_CFLAGS -DHAVE_STACK_SIZE -DTFM_TIMING_RESISTANT" +fi + + # MEMORY AC_ARG_ENABLE([memory], [ --enable-memory Enable memory callbacks (default: enabled)], diff --git a/cyassl/test.h b/cyassl/test.h index 9e022c350..699f14049 100644 --- a/cyassl/test.h +++ b/cyassl/test.h @@ -1081,5 +1081,63 @@ static INLINE int CurrentDir(const char* str) #endif /* USE_CYASSL_MEMORY */ + +#ifdef HAVE_STACK_SIZE + +typedef THREAD_RETURN CYASSL_THREAD (*thread_func)(void* args); + + +static INLINE void StackSizeCheck(func_args* args, thread_func tf) +{ + int ret, i, used; + unsigned char* myStack; + int stackSize = 1024*128; + pthread_attr_t myAttr; + pthread_t threadId; + +#ifdef PTHREAD_STACK_MIN + if (stackSize < PTHREAD_STACK_MIN) + stackSize = PTHREAD_STACK_MIN; +#endif + + ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize); + if (ret != 0) + err_sys("posix_memalign failed\n"); + + memset(myStack, 0xee, stackSize); + + ret = pthread_attr_init(&myAttr); + if (ret != 0) + err_sys("attr_init failed"); + + ret = pthread_attr_setstack(&myAttr, myStack, stackSize); + if (ret != 0) + err_sys("attr_setstackaddr failed"); + + ret = pthread_create(&threadId, &myAttr, tf, args); + if (ret != 0) { + perror("pthread_create failed"); + exit(EXIT_FAILURE); + } + + ret = pthread_join(threadId, NULL); + if (ret != 0) + err_sys("pthread_join failed"); + + for (i = 0; i < stackSize; i++) { + if (myStack[i] != 0xee) { + break; + } + } + + used = stackSize - i; + printf("stack used = %d\n", used); +} + + +#endif /* HAVE_STACK_SIZE */ + + + #endif /* CyaSSL_TEST_H */ diff --git a/examples/client/client.c b/examples/client/client.c index 9234d3937..1ae41cf87 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -114,7 +114,7 @@ static void Usage(void) } -void client_test(void* args) +THREAD_RETURN CYASSL_THREAD client_test(void* args) { SOCKET_T sockfd = 0; @@ -592,6 +592,8 @@ void client_test(void* args) if (trackMemory) ShowMemoryTracker(); #endif /* USE_CYASSL_MEMORY */ + + return 0; } @@ -619,8 +621,12 @@ void client_test(void* args) #endif if (CurrentDir("client") || CurrentDir("build")) ChangeDirBack(2); - + +#ifdef HAVE_STACK_SIZE + StackSizeCheck(&args, client_test); +#else client_test(&args); +#endif CyaSSL_Cleanup(); #ifdef HAVE_CAVIUM diff --git a/examples/client/client.h b/examples/client/client.h index 75356b75f..2d051fb2d 100644 --- a/examples/client/client.h +++ b/examples/client/client.h @@ -21,5 +21,5 @@ #pragma once -void client_test(void* args); +THREAD_RETURN CYASSL_THREAD client_test(void* args); diff --git a/examples/server/server.c b/examples/server/server.c index 0c79aff80..0108909cf 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -458,7 +458,11 @@ THREAD_RETURN CYASSL_THREAD server_test(void* args) if (CurrentDir("server") || CurrentDir("build")) ChangeDirBack(2); +#ifdef HAVE_STACK_SIZE + StackSizeCheck(&args, server_test); +#else server_test(&args); +#endif CyaSSL_Cleanup(); #ifdef HAVE_CAVIUM diff --git a/examples/server/server.h b/examples/server/server.h index 3c03c4dba..d5efa435c 100644 --- a/examples/server/server.h +++ b/examples/server/server.h @@ -1,4 +1,4 @@ -/* server.c +/* server.h * * Copyright (C) 2006-2013 wolfSSL Inc. * diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index ddc4db77e..c6cbe5fb5 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -37,9 +37,9 @@ #include "examples/echoclient/echoclient.h" #include "examples/echoserver/echoserver.h" #include "examples/server/server.h" +#include "examples/client/client.h" #include "ctaocrypt/test/test.h" -void client_test(void*); void file_test(const char* file, byte* hash);