## NAME

thread_numb(), thread_util_t, threads_utilization_start(), threads_utilization_stop()

## SYNOPSIS

#include <sys/sys.h>

int thread_numb();

void thread_utilization_start(thread_util_t thread_table[]);

void thread_utilization_stop();

## DESCRIPTION

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:

- uint thread_id
- char *thread_name
- u_long util

Using the two first parameters, the thread information can be found. Using the util parameter the utilization can be found. The util parameter contains the % of the processor utilized by the thread with the upper sixteen bits providing the % and the lower 16 bits providing one extra byte of resolution after the decimal point. For example, 12.5% has the 12 encoded in the upper 16 bits and the 5 encoded in the lower 16 bits. See the Example for more details.

## RETURN VALUES

thread_numb returns the number of threads in the system including the idle thread.

## ERRORS

The above calls cannot fail and return no error codes.

## EXAMPLES

    #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);
    }

## NOTES

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.

## SEE ALSO

For a tutorial with discussion and examples, see the Unison or DSPnano Programmer's Guides and the demo directory.

#### Home page (Kernel)

## < Copyright Rowebots Research Inc. and Multiprocessor Toolsmiths Inc. 1987-2018 >