Skip to content

Commit 7e66f98

Browse files
committed
Add stack tracking to zephyr benchmark
1 parent 1ab33b8 commit 7e66f98

1 file changed

Lines changed: 108 additions & 3 deletions

File tree

wolfssl/wolfcrypt/mem_track.h

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ static WC_INLINE int CleanupMemoryTracker(void)
484484
#include <unistd.h>
485485
#endif
486486

487-
typedef void* (*thread_func)(void* args);
487+
typedef THREAD_RETURN (*thread_func)(void* args);
488488
#define STACK_CHECK_VAL 0x01
489489

490490
struct stack_size_debug_context {
@@ -620,7 +620,7 @@ static WC_INLINE int StackSizeCheck_Rebaseline(void)
620620
* ./configure --enable-stacksize=verbose [...]
621621
*/
622622

623-
static void* debug_stack_size_verbose_shim(
623+
static THREAD_RETURN debug_stack_size_verbose_shim(
624624
struct stack_size_debug_context *shim_args)
625625
{
626626
StackSizeCheck_myStack = shim_args->myStack;
@@ -733,7 +733,112 @@ int StackSizeHWMReset(void)
733733

734734
#endif /* HAVE_STACK_SIZE_VERBOSE */
735735

736-
#ifdef HAVE_PTHREAD
736+
#if defined(__ZEPHYR__)
737+
738+
static WC_INLINE int StackSizeCheck(struct func_args* args, thread_func tf)
739+
{
740+
size_t i;
741+
int ret = 0;
742+
int err;
743+
struct k_thread* tid = NULL;
744+
k_thread_stack_t* threadStack = NULL;
745+
unsigned char* myStack = NULL;
746+
747+
#ifdef HAVE_STACK_SIZE_VERBOSE
748+
struct stack_size_debug_context shim_args;
749+
#endif
750+
751+
tid = (struct k_thread*)XMALLOC(
752+
Z_KERNEL_STACK_SIZE_ADJUST(sizeof(struct k_thread)),
753+
wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER);
754+
if (tid == NULL) {
755+
printf("error: XMALLOC tid failed");
756+
ret = MEMORY_E;
757+
goto out;
758+
}
759+
760+
#ifndef WOLFSSL_ZEPHYR_STACK_SZ
761+
#define WOLFSSL_ZEPHYR_STACK_SZ (48*1024)
762+
#endif
763+
764+
threadStack = (void*)XMALLOC(
765+
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ),
766+
wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER);
767+
if (threadStack == NULL) {
768+
printf("error: k_thread_stack_alloc threadStack failed");
769+
ret = MEMORY_E;
770+
goto out;
771+
}
772+
773+
myStack = K_THREAD_STACK_BUFFER(threadStack);
774+
XMEMSET(myStack, STACK_CHECK_VAL, WOLFSSL_ZEPHYR_STACK_SZ);
775+
776+
#ifdef HAVE_STACK_SIZE_VERBOSE
777+
StackSizeCheck_stackSizeHWM = 0;
778+
shim_args.myStack = myStack;
779+
shim_args.stackSize = WOLFSSL_ZEPHYR_STACK_SZ;
780+
shim_args.stackSizeHWM_ptr = &StackSizeCheck_stackSizeHWM;
781+
shim_args.fn = tf;
782+
shim_args.args = args;
783+
784+
/* k_thread_create does not return any error codes */
785+
/* Casting to k_thread_entry_t should be fine since we just ignore the
786+
* extra arguments being passed in */
787+
k_thread_create(tid, threadStack, WOLFSSL_ZEPHYR_STACK_SZ,
788+
(k_thread_entry_t)debug_stack_size_verbose_shim, (void *)&shim_args,
789+
NULL, NULL, 5, 0, K_NO_WAIT);
790+
#else
791+
/* k_thread_create does not return any error codes */
792+
/* Casting to k_thread_entry_t should be fine since we just ignore the
793+
* extra arguments being passed in */
794+
k_thread_create(tid, threadStack, WOLFSSL_ZEPHYR_STACK_SZ,
795+
(k_thread_entry_t)tf, args, NULL, NULL, 5, 0, K_NO_WAIT);
796+
797+
#endif
798+
799+
err = k_thread_join(tid, K_FOREVER);
800+
if (err != 0) {
801+
printf("k_thread_join failed\n");
802+
ret = MEMORY_E;
803+
goto out;
804+
}
805+
806+
807+
for (i = 0; i < WOLFSSL_ZEPHYR_STACK_SZ; i++) {
808+
if (myStack[i] != STACK_CHECK_VAL) {
809+
break;
810+
}
811+
}
812+
813+
#ifdef HAVE_STACK_SIZE_VERBOSE
814+
printf("stack used = %lu\n", StackSizeCheck_stackSizeHWM > (WOLFSSL_ZEPHYR_STACK_SZ - i)
815+
? (unsigned long)StackSizeCheck_stackSizeHWM
816+
: (unsigned long)(WOLFSSL_ZEPHYR_STACK_SZ - i));
817+
StackSizeCheck_myStack = NULL;
818+
StackSizeCheck_stackOffsetPointer = NULL;
819+
#else
820+
{
821+
size_t used = WOLFSSL_ZEPHYR_STACK_SZ - i;
822+
printf("stack used = %lu\n", (unsigned long)used);
823+
}
824+
#endif
825+
826+
out:
827+
XFREE(tid, wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER);
828+
XFREE(threadStack, wolfsslThreadHeapHint,
829+
DYNAMIC_TYPE_TMP_BUFFER);
830+
831+
return ret;
832+
}
833+
834+
#define StackSizeCheck_launch(args, tf, threadId, stack_context) \
835+
((void)(args), (void)(tf), (void)(threadId), (void)(stack_context), \
836+
(NOT_COMPILED_IN))
837+
838+
#define StackSizeCheck_reap(threadId, stack_context) \
839+
((void)(threadId), (void)(stack_context), (NOT_COMPILED_IN))
840+
841+
#elif defined(HAVE_PTHREAD)
737842

738843
static WC_INLINE int StackSizeCheck(struct func_args* args, thread_func tf)
739844
{

0 commit comments

Comments
 (0)