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