1 | /* |
---|
2 | * CPU Usage Reporter |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2007 |
---|
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 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #ifdef HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <rtems.h> |
---|
19 | |
---|
20 | #include <assert.h> |
---|
21 | #include <string.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <ctype.h> |
---|
25 | #include <inttypes.h> |
---|
26 | |
---|
27 | #include <rtems/cpuuse.h> |
---|
28 | #include <rtems/bspIo.h> |
---|
29 | |
---|
30 | #if defined(RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS) |
---|
31 | #include <rtems/score/timespec.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
35 | extern struct timespec CPU_usage_Uptime_at_last_reset; |
---|
36 | #else |
---|
37 | extern uint32_t CPU_usage_Ticks_at_last_reset; |
---|
38 | #endif |
---|
39 | |
---|
40 | /*PAGE |
---|
41 | * |
---|
42 | * rtems_cpu_usage_report |
---|
43 | */ |
---|
44 | |
---|
45 | void rtems_cpu_usage_report_with_plugin( |
---|
46 | void *context, |
---|
47 | rtems_printk_plugin_t print |
---|
48 | ) |
---|
49 | { |
---|
50 | uint32_t i; |
---|
51 | uint32_t api_index; |
---|
52 | Thread_Control *the_thread; |
---|
53 | Objects_Information *information; |
---|
54 | char name[5]; |
---|
55 | uint32_t ival, fval; |
---|
56 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
57 | struct timespec uptime, total, ran; |
---|
58 | #else |
---|
59 | uint32_t total_units = 0; |
---|
60 | #endif |
---|
61 | |
---|
62 | if ( !print ) |
---|
63 | return; |
---|
64 | |
---|
65 | /* |
---|
66 | * When not using nanosecond CPU usage resolution, we have to count |
---|
67 | * the number of "ticks" we gave credit for to give the user a rough |
---|
68 | * guideline as to what each number means proportionally. |
---|
69 | */ |
---|
70 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
71 | _TOD_Get_uptime( &uptime ); |
---|
72 | _Timespec_Subtract( &CPU_usage_Uptime_at_last_reset, &uptime, &total ); |
---|
73 | #else |
---|
74 | for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; api_index++ ) { |
---|
75 | if ( !_Objects_Information_table[ api_index ] ) |
---|
76 | continue; |
---|
77 | information = _Objects_Information_table[ api_index ][ 1 ]; |
---|
78 | if ( information ) { |
---|
79 | for ( i=1 ; i <= information->maximum ; i++ ) { |
---|
80 | the_thread = (Thread_Control *)information->local_table[ i ]; |
---|
81 | |
---|
82 | if ( the_thread ) |
---|
83 | total_units += the_thread->ticks_executed; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | #endif |
---|
88 | |
---|
89 | (*print)( context, "CPU Usage by thread\n" |
---|
90 | #if defined(RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS) |
---|
91 | " ID NAME SECONDS PERCENT\n" |
---|
92 | #else |
---|
93 | " ID NAME TICKS PERCENT\n" |
---|
94 | #endif |
---|
95 | ); |
---|
96 | |
---|
97 | for ( api_index = 1 ; |
---|
98 | api_index <= OBJECTS_APIS_LAST ; |
---|
99 | api_index++ ) { |
---|
100 | if ( !_Objects_Information_table[ api_index ] ) |
---|
101 | continue; |
---|
102 | information = _Objects_Information_table[ api_index ][ 1 ]; |
---|
103 | if ( information ) { |
---|
104 | for ( i=1 ; i <= information->maximum ; i++ ) { |
---|
105 | the_thread = (Thread_Control *)information->local_table[ i ]; |
---|
106 | |
---|
107 | if ( !the_thread ) |
---|
108 | continue; |
---|
109 | |
---|
110 | rtems_object_get_name( the_thread->Object.id, sizeof(name), name ); |
---|
111 | |
---|
112 | (*print)( context, "0x%08" PRIx32 " %4s ", the_thread->Object.id, name ); |
---|
113 | |
---|
114 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
115 | /* |
---|
116 | * If this is the currently executing thread, account for time |
---|
117 | * since the last context switch. |
---|
118 | */ |
---|
119 | ran = the_thread->cpu_time_used; |
---|
120 | if ( _Thread_Executing->Object.id == the_thread->Object.id ) { |
---|
121 | struct timespec used; |
---|
122 | _Timespec_Subtract( |
---|
123 | &_Thread_Time_of_last_context_switch, &uptime, &used |
---|
124 | ); |
---|
125 | _Timespec_Add_to( &ran, &used ); |
---|
126 | }; |
---|
127 | _Timespec_Divide( &ran, &total, &ival, &fval ); |
---|
128 | |
---|
129 | /* |
---|
130 | * Print the information |
---|
131 | */ |
---|
132 | |
---|
133 | (*print)( context, |
---|
134 | "%3" PRId32 ".%06" PRId32 " %3" PRId32 ".%03" PRId32 "\n", |
---|
135 | ran.tv_sec, ran.tv_nsec / TOD_NANOSECONDS_PER_MICROSECOND, |
---|
136 | ival, fval |
---|
137 | ); |
---|
138 | #else |
---|
139 | ival = (total_units) ? |
---|
140 | the_thread->ticks_executed * 10000 / total_units : 0; |
---|
141 | fval = ival % 100; |
---|
142 | ival /= 100; |
---|
143 | (*print)( context, |
---|
144 | "%8" PRId32 " %3" PRId32 ".%02" PRId32"\n", |
---|
145 | the_thread->ticks_executed, |
---|
146 | ival, |
---|
147 | fval |
---|
148 | ); |
---|
149 | #endif |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
155 | (*print)( context, "Time since last CPU Usage reset %" PRId32 |
---|
156 | ".%06" PRId32 " seconds\n", |
---|
157 | total.tv_sec, |
---|
158 | total.tv_nsec / TOD_NANOSECONDS_PER_MICROSECOND |
---|
159 | ); |
---|
160 | #else |
---|
161 | (*print)( context, |
---|
162 | "Ticks since last reset = %" PRId32 "\n", |
---|
163 | _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset |
---|
164 | ); |
---|
165 | (*print)( context, "Total Units = %" PRId32 "\n", total_units ); |
---|
166 | #endif |
---|
167 | } |
---|
168 | |
---|
169 | void rtems_cpu_usage_report( void ) |
---|
170 | { |
---|
171 | rtems_cpu_usage_report_with_plugin( NULL, printk_plugin ); |
---|
172 | } |
---|