User-Level Native Thread Primitives (GThread) Library White Paper (G06.26+)
User-Level Native Thread Primitives (GThread Library) 03/31/2005
Hewlett-Packard Company--540065-001 Page 27 of 46
2.11. Measuring and Checking Stack Usage
A simple method clients can use to analyze the stack usage of a given thread is to allocate a very large
memory space for the stack and fill that space with a fixed pattern. The thread is then allowed to run
(with emphasis on testing all possible code paths) and prior to terminating the thread, the memory space
is examined. Stack usage is determined by walking the memory space to locate the point at which the
fixed pattern has been overwritten. This determination requires the client to understand the mechanics of
stack growth:
- For TNS platforms, the stack grows from low to high addresses
- For non TNS/E native platforms, the stack grows from high to low addresses
- For TNS/E native platforms, there are two stacks growing in opposite directions
The GThreads library provides the GTH_THREAD_USAGE_ and GTH_VALIDATE_GUARD_ API’s
to aid the client in determining stack usage without having to understand platform specifics.
Additionally, the Swaptions argument to GTHREAD_INITIATE_ allows the client to specify that a
fixed pattern should be written into the memory allocated for the static stack. A new define,
GTS_STATIC_FILL is created for this purpose. The define can be logically ORed with any other
GTS_STATIC_xxx define. (If both GTS_STATIC_FILL and GTS_SWAPPED occur in the same call,
GTHREAD_INITIATE_ reports GT_BAD_SWAPTIONS.) A call to GTHREAD_INITIATE_ utilizing
the new define would be as follows:
GTHREAD_INITIATE_(...,
length,
(GTS_STATIC_FILL | GTS_STATIC_RegPerCent(30)),
...);
2.11.1. Thread Stack Usage
GTH_THREAD_USAGE_( GThread_CB cb, int32* mem_alloc, int32* mem_used, int32* reg_alloc,
int32* reg_used )
Description:
Returns information on the stack allocation and usage for the specified thread. This function returns
useful information only for a static thread created by GTHREAD_INITIATE_ with the
GTS_STATIC_FILL value included in the Swaptions parameter. It is not callable for the Main
thread.
Input:
cb - The address of the thread control block for the thread being analyzed.
mem_alloc– Pointer to location for the size of the allocated space, in bytes, reserved for the
mem stack.