This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
Stack usage measurement
- From: Szentirmai Gergely <reg at t-online dot hu>
- To: eCos Discuss <ecos-discuss at ecos dot sourceware dot org>
- Date: Sun, 19 Apr 2009 21:18:11 +0200
- Subject: [ECOS] Stack usage measurement
Hello
I had a bad feeling about stack usage (perhaps measurement), and I have
written a small testcode, to see what's really happening. It came with
an interesting result. It seems to me, that
cyg_thread_info.stack_used gives the max used stack size, not the
actual. Is that right? According to the docs, it should give the current
usage.
The test code is attached, I use arm-elf-gcc, ecos with default packages.
The result is also attached. I think that even the last stack usage
should be equal with 144 Bytes.
Maybe I am wrong... maybe not.
Thank you!
Gergely Szentirmai
#include <stdio.h>
#include <cyg/infra/diag.h>
#include <cyg/hal/hal_arch.h>
#include <cyg/kernel/kapi.h>
#include <stdlib.h>
/*******************/
/*** Threads ***/
/*******************/
#define MAIN_THREAD_STACK_SIZE CYGNUM_HAL_STACK_SIZE_MINIMUM*3
cyg_uint8 main_thread_stack[MAIN_THREAD_STACK_SIZE];
cyg_handle_t main_thread_handle;
cyg_thread main_thread_obj;
void show_thread(cyg_handle_t thread)
{
cyg_uint16 id=cyg_thread_get_id(thread);
cyg_thread_info info;
if( !cyg_thread_get_info( thread, id, &info ) )
{
diag_printf("Unable to get thread info\n");
return;
}
diag_write_dec(info.stack_used);
diag_printf(" Bytes used\n");
}
void stacktest3(void)
{
char test2[1512]="In this test we allocate (1512 Bytes), and we print out the stack usage\n\0";
diag_printf(test2);
show_thread(main_thread_handle);
}
void stacktest2(void)
{
char test2[512]="Test data2 allocated (512 Bytes) in the called function, on the stack\n\0";
diag_printf(test2);
}
void stacktest(void)
{
char test[1024]="Test data allocated (1024 Bytes) in the called function, on the stack\n\0";
diag_printf(test);
}
static void main_thread(cyg_addrword_t index)
{
show_thread(main_thread_handle);
show_thread(main_thread_handle);
diag_printf("Stack usage should be the same, but it is not.\n");
show_thread(main_thread_handle);
diag_printf("Calling the test function\n");
stacktest();
diag_printf("function returned, let's see the stack size, it should be the same as before\n");
show_thread(main_thread_handle);
diag_printf("now we call it again\n");
stacktest();
diag_printf("function returned, let's see the stack size, it should be the same as before\n");
show_thread(main_thread_handle);
diag_printf("now we the second test function\n");
stacktest2();
diag_printf("function returned, let's see the stack size, it should be the same as before\n");
show_thread(main_thread_handle);
diag_printf("now we the third test function\n");
stacktest3();
diag_printf("function returned, let's see the stack size, it should be the same as before call\n");
show_thread(main_thread_handle);
while(1)
{
diag_printf("main loop\n");
cyg_thread_delay(1000);
show_thread(main_thread_handle);
}
}
/****************/
/*** Init ***/
/****************/
void cyg_user_start(void)
{
diag_init();
diag_printf("\n");
diag_printf("---- User-start ---\n");
cyg_thread_create(
10, // priority
main_thread, // thread function
0, // thread parameter
NULL, // thread name
&main_thread_stack, // stack base addr
MAIN_THREAD_STACK_SIZE, // stack size
&main_thread_handle, // thread handle
&main_thread_obj); // thread object
cyg_thread_resume(main_thread_handle);
}
---- User-start ---
144 Bytes used
332 Bytes used
Stack usage should be the same, but it is not.
440 Bytes used
Calling the test function
Test data allocated (1024 Bytes) in the called function, on the stack
function returned, let's see the stack size, it should be the same as before
1484 Bytes used
now we call it again
Test data allocated (1024 Bytes) in the called function, on the stack
function returned, let's see the stack size, it should be the same as before
1484 Bytes used
now we the second test function
Test data2 allocated (512 Bytes) in the called function, on the stack
function returned, let's see the stack size, it should be the same as before
1484 Bytes used
now we the third test function
In this test we allocate (1512 Bytes), and we print out the stack usage
1972 Bytes used
function returned, let's see the stack size, it should be the same as before call
1972 Bytes used
main loop
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss