[fc7bc51] | 1 | /* |
---|
| 2 | * CPU Usage Reporter |
---|
| 3 | * |
---|
[08311cc3] | 4 | * COPYRIGHT (c) 1989-1999. 1996. |
---|
[fc7bc51] | 5 | * On-Line Applications Research Corporation (OAR). |
---|
| 6 | * |
---|
[98e4ebf5] | 7 | * The license and distribution terms for this file may be |
---|
| 8 | * found in the file LICENSE in this distribution or at |
---|
[3160ff6] | 9 | * http://www.rtems.com/license/LICENSE. |
---|
[fc7bc51] | 10 | * |
---|
[6f9c75c3] | 11 | * $Id$ |
---|
[fc7bc51] | 12 | * |
---|
| 13 | */ |
---|
| 14 | |
---|
[550c3df7] | 15 | #ifdef HAVE_CONFIG_H |
---|
| 16 | #include "config.h" |
---|
| 17 | #endif |
---|
| 18 | |
---|
[fc7bc51] | 19 | #include <rtems.h> |
---|
| 20 | |
---|
| 21 | #include <assert.h> |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | #include <stdlib.h> |
---|
[ca5fe67] | 25 | #include <ctype.h> |
---|
[fc7bc51] | 26 | |
---|
[37de72b] | 27 | #include <rtems/cpuuse.h> |
---|
[fc7bc51] | 28 | |
---|
| 29 | unsigned32 CPU_usage_Ticks_at_last_reset; |
---|
| 30 | |
---|
| 31 | /*PAGE |
---|
| 32 | * |
---|
| 33 | * CPU_usage_Dump |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | void CPU_usage_Dump( void ) |
---|
| 37 | { |
---|
| 38 | unsigned32 i; |
---|
[63977bb4] | 39 | unsigned32 api_index; |
---|
[fc7bc51] | 40 | Thread_Control *the_thread; |
---|
| 41 | Objects_Information *information; |
---|
| 42 | unsigned32 u32_name; |
---|
[e482c592] | 43 | char *cname; |
---|
[fc7bc51] | 44 | char name[5]; |
---|
| 45 | unsigned32 total_units = 0; |
---|
| 46 | |
---|
[63977bb4] | 47 | for ( api_index = 1 ; |
---|
| 48 | api_index <= OBJECTS_APIS_LAST ; |
---|
| 49 | api_index++ ) { |
---|
| 50 | if ( !_Objects_Information_table[ api_index ] ) |
---|
| 51 | continue; |
---|
| 52 | information = _Objects_Information_table[ api_index ][ 1 ]; |
---|
| 53 | if ( information ) { |
---|
[fc7bc51] | 54 | for ( i=1 ; i <= information->maximum ; i++ ) { |
---|
| 55 | the_thread = (Thread_Control *)information->local_table[ i ]; |
---|
| 56 | |
---|
| 57 | if ( the_thread ) |
---|
| 58 | total_units += the_thread->ticks_executed; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | printf("CPU Usage by thread\n"); |
---|
| 64 | #if defined(unix) || ( CPU_HARDWARE_FP == TRUE ) |
---|
| 65 | printf( " ID NAME TICKS PERCENT\n" ); |
---|
| 66 | #else |
---|
| 67 | printf( " ID NAME TICKS\n" ); |
---|
| 68 | #endif |
---|
| 69 | |
---|
[63977bb4] | 70 | for ( api_index = 1 ; |
---|
| 71 | api_index <= OBJECTS_APIS_LAST ; |
---|
| 72 | api_index++ ) { |
---|
| 73 | if ( !_Objects_Information_table[ api_index ] ) |
---|
| 74 | continue; |
---|
| 75 | information = _Objects_Information_table[ api_index ][ 1 ]; |
---|
| 76 | if ( information ) { |
---|
[fc7bc51] | 77 | for ( i=1 ; i <= information->maximum ; i++ ) { |
---|
| 78 | the_thread = (Thread_Control *)information->local_table[ i ]; |
---|
| 79 | |
---|
| 80 | if ( !the_thread ) |
---|
| 81 | continue; |
---|
| 82 | |
---|
[e482c592] | 83 | if ( information->is_string ) { |
---|
| 84 | cname = the_thread->Object.name; |
---|
| 85 | name[ 0 ] = cname[0]; |
---|
| 86 | name[ 1 ] = cname[1]; |
---|
| 87 | name[ 2 ] = cname[2]; |
---|
| 88 | name[ 3 ] = cname[3]; |
---|
| 89 | name[ 4 ] = '\0'; |
---|
| 90 | } else { |
---|
[ca5fe67] | 91 | u32_name = (unsigned32)the_thread->Object.name; |
---|
[e482c592] | 92 | name[ 0 ] = (u32_name >> 24) & 0xff; |
---|
| 93 | name[ 1 ] = (u32_name >> 16) & 0xff; |
---|
| 94 | name[ 2 ] = (u32_name >> 8) & 0xff; |
---|
| 95 | name[ 3 ] = (u32_name >> 0) & 0xff; |
---|
| 96 | name[ 4 ] = '\0'; |
---|
| 97 | } |
---|
[fc7bc51] | 98 | |
---|
[ca5fe67] | 99 | if ( !isprint(name[0]) ) name[0] = '*'; |
---|
| 100 | if ( !isprint(name[1]) ) name[1] = '*'; |
---|
| 101 | if ( !isprint(name[2]) ) name[2] = '*'; |
---|
| 102 | if ( !isprint(name[3]) ) name[3] = '*'; |
---|
| 103 | |
---|
[fc7bc51] | 104 | #if defined(unix) || ( CPU_HARDWARE_FP == TRUE ) |
---|
| 105 | printf( "0x%08x %4s %8d %5.3f\n", |
---|
| 106 | the_thread->Object.id, |
---|
| 107 | name, |
---|
| 108 | the_thread->ticks_executed, |
---|
| 109 | (total_units) ? |
---|
| 110 | (double)the_thread->ticks_executed / (double)total_units : |
---|
| 111 | (double)total_units |
---|
| 112 | ); |
---|
| 113 | #else |
---|
| 114 | printf( "0x%08x %4s %8d\n", |
---|
| 115 | the_thread->Object.id, |
---|
| 116 | name, |
---|
| 117 | the_thread->ticks_executed |
---|
| 118 | ); |
---|
| 119 | #endif |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | printf( |
---|
| 125 | "\nTicks since last reset = %d\n", |
---|
| 126 | _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset |
---|
| 127 | ); |
---|
| 128 | printf( "\nTotal Units = %d\n", total_units ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /*PAGE |
---|
| 132 | * |
---|
| 133 | * CPU_usage_Reset |
---|
| 134 | */ |
---|
| 135 | |
---|
[236b701] | 136 | static void CPU_usage_Per_thread_handler( |
---|
| 137 | Thread_Control *the_thread |
---|
| 138 | ) |
---|
| 139 | { |
---|
| 140 | the_thread->ticks_executed = 0; |
---|
| 141 | } |
---|
| 142 | |
---|
[fc7bc51] | 143 | void CPU_usage_Reset( void ) |
---|
| 144 | { |
---|
| 145 | CPU_usage_Ticks_at_last_reset = _Watchdog_Ticks_since_boot; |
---|
| 146 | |
---|
[236b701] | 147 | rtems_iterate_over_all_threads(CPU_usage_Per_thread_handler); |
---|
[fc7bc51] | 148 | } |
---|
| 149 | |
---|