Get run-time stack usage by debug trace

Please refer to Use syslog to trace Neo3 for time performance measurement on K81 to add trace before using the functions.

Add pthread_get_stacksize_np and calculate remain stack size in __cyg_profile_func_exit function like below.

void __attribute__((__no_instrument_function__)) __cyg_profile_func_enter(void *this_func, void *call_site) {   syslog(LOG_CRIT, "[Start].%p\n", this_func); } void __attribute__((__no_instrument_function__)) __cyg_profile_func_exit(void *this_func, void *call_site) {   pthread_t currentThread = pthread_self();   /* Get run-time stack address and size */   uint8_t *stack_base = pthread_get_stackaddr_np(currentThread);   size_t stack_size = pthread_get_stacksize_np(currentThread);   uint8_t *frame_addr = __builtin_frame_address(0);   size_t remainingStackSize = stack_size - (size_t)(frame_addr - stack_base);   syslog(LOG_CRIT, "[End:%d].%p\n", remainingStackSize, this_func); }

After running for a while eg. a ctls transaction, a syslog file should be saved.

Copy the syslog file to Linux PC. Use the script file gethreadstackusage.py to calculate stack usage for each thread like below.

$ python3 gethreadstackusage.py syslog Thread/TC Stack Free (bytes) ------------------------- TC_K81_START 2744 pc_k81start_primary_ss 896 CTLS_transaction_thread 336 pt-CTLS_transaction_thread_level1 200 TC_K81_READER 184 TC_K81_SYSTEM 184 IDG_Server_Request_Handler 168 pc_CTLS_txn_thread_level1 168 pc_k81reader_reader_ss 152 pc_k81system_system_ss 152 pc_k81start_native_ss 144 pc_k81system_pki_stm 144 pt-k81system_system_ui_ss 136 pt-k81system_pki_stm_loop 136 pc_TAL_main_thread 136 k81start 128 pt-k81start_primary_ss 128 k81reader 120 k81system 120

The second column shows the remaind stack size after a ctls transaction 0240.

From the list, thread TC_K81_START remains 2744bytes stack size. So we can say the stack usage of thread TC_K81_START is very little during a ctls transaction.

A syslog trace file for a ctls transaction.