#include <sys/sys.h>
int thread_numb();
void thread_utilization_start(thread_util_t thread_table[]);
void thread_utilization_stop();
The function thread_numb gets the current number of threads in the system, returning an int. With this information, enough space may be allocated on a thread by thread basis to record scheduling results.
The thread_utilization_start function starts the utilization measurement and the thread_utilization_stop function indicates the end of the utilization measurements.
At the end of the measurements, the thread_util_t structures allocated for the thread table contain the following information for each thread:
thread_numb returns the number of threads in the system including the idle thread.
The above calls cannot fail and return no error codes.
#include < sys.h >
thread_util_t *threads_table = NULL;
int utilization_begin(void)
{
int threads_count;
threads_count = threads_numb();
xprintf("found %d threads\n", threads_count);
// Create table for utilization count
threads_table = malloc(sizeof(thread_util_t) * threads_count);
//Start utilization count
threads_utilization_start(threads_table, threads_count);
return threads_count;
}
void utilization_end(int threads_count)
{
int i;
threads_utilization_stop();
for (i=0; i < threads_count; i++)
{
xprintf("'%s' - %d.%d %%\n", threads_table[i].thread_name,
(int)(threads_table[i].util >> 16),
(int)(threads_table[i].util & 0xffff));
}
free(threads_table);
}
The utilization measurements do not include the time spent in interrupt service routines. If your system spends a great deal of time in interrupt processing, these results are no less accurate in terms of thread processing time, but they reflect only the time spent for threads. For example, if interrupts flood the system and only one thread runs, it will indicate 100% of the utlization for the one thread. It will not tell you that there is significant interrupt processing that has not been measured.
The utilization measurements rely upon periodic timer measurements. It is rare but possible to end up with synchronization between the clock and the thread which distorts the utilization time measurement. In this case, there are modifications that can eliminate this problem. Please contact RoweBots for help.
For a tutorial with discussion and examples, see the Unison or DSPnano Programmer's Guides and the demo directory.