1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief CPU Usage Report |
---|
5 | * @ingroup libmisc_cpuuse CPU Usage |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2010. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #ifdef HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
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/score/objectimpl.h> |
---|
29 | #include <rtems/score/threadimpl.h> |
---|
30 | #include <rtems/score/todimpl.h> |
---|
31 | #include <rtems/score/watchdogimpl.h> |
---|
32 | |
---|
33 | /* |
---|
34 | * rtems_cpu_usage_report |
---|
35 | */ |
---|
36 | void rtems_cpu_usage_report_with_plugin( |
---|
37 | void *context, |
---|
38 | rtems_printk_plugin_t print |
---|
39 | ) |
---|
40 | { |
---|
41 | uint32_t i; |
---|
42 | uint32_t api_index; |
---|
43 | Thread_Control *the_thread; |
---|
44 | Objects_Information *information; |
---|
45 | char name[13]; |
---|
46 | uint32_t ival, fval; |
---|
47 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
48 | Timestamp_Control uptime, total, ran, uptime_at_last_reset; |
---|
49 | uint32_t seconds, nanoseconds; |
---|
50 | #else |
---|
51 | uint32_t total_units = 0; |
---|
52 | #endif |
---|
53 | |
---|
54 | if ( !print ) |
---|
55 | return; |
---|
56 | |
---|
57 | /* |
---|
58 | * When not using nanosecond CPU usage resolution, we have to count |
---|
59 | * the number of "ticks" we gave credit for to give the user a rough |
---|
60 | * guideline as to what each number means proportionally. |
---|
61 | */ |
---|
62 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
63 | _Timestamp_Set_to_zero( &total ); |
---|
64 | uptime_at_last_reset = CPU_usage_Uptime_at_last_reset; |
---|
65 | #else |
---|
66 | for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; api_index++ ) { |
---|
67 | #if !defined(RTEMS_POSIX_API) || defined(RTEMS_DEBUG) |
---|
68 | if ( !_Objects_Information_table[ api_index ] ) |
---|
69 | continue; |
---|
70 | #endif |
---|
71 | |
---|
72 | information = _Objects_Information_table[ api_index ][ 1 ]; |
---|
73 | if ( information ) { |
---|
74 | for ( i=1 ; i <= information->maximum ; i++ ) { |
---|
75 | the_thread = (Thread_Control *)information->local_table[ i ]; |
---|
76 | |
---|
77 | if ( the_thread ) |
---|
78 | total_units += the_thread->cpu_time_used; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | #endif |
---|
83 | |
---|
84 | (*print)( |
---|
85 | context, |
---|
86 | "-------------------------------------------------------------------------------\n" |
---|
87 | " CPU USAGE BY THREAD\n" |
---|
88 | "------------+----------------------------------------+---------------+---------\n" |
---|
89 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
90 | " ID | NAME | SECONDS | PERCENT\n" |
---|
91 | #else |
---|
92 | " ID | NAME | TICKS | PERCENT\n" |
---|
93 | #endif |
---|
94 | "------------+----------------------------------------+---------------+---------\n" |
---|
95 | ); |
---|
96 | |
---|
97 | for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; api_index++ ) { |
---|
98 | #if !defined(RTEMS_POSIX_API) || defined(RTEMS_DEBUG) |
---|
99 | if ( !_Objects_Information_table[ api_index ] ) |
---|
100 | continue; |
---|
101 | #endif |
---|
102 | |
---|
103 | information = _Objects_Information_table[ api_index ][ 1 ]; |
---|
104 | if ( information ) { |
---|
105 | for ( i=1 ; i <= information->maximum ; i++ ) { |
---|
106 | the_thread = (Thread_Control *)information->local_table[ i ]; |
---|
107 | |
---|
108 | if ( !the_thread ) |
---|
109 | continue; |
---|
110 | |
---|
111 | rtems_object_get_name( the_thread->Object.id, sizeof(name), name ); |
---|
112 | |
---|
113 | (*print)( |
---|
114 | context, |
---|
115 | " 0x%08" PRIx32 " | %-38s |", |
---|
116 | the_thread->Object.id, |
---|
117 | name |
---|
118 | ); |
---|
119 | |
---|
120 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
121 | { |
---|
122 | Timestamp_Control last; |
---|
123 | |
---|
124 | /* |
---|
125 | * If this is the currently executing thread, account for time |
---|
126 | * since the last context switch. |
---|
127 | */ |
---|
128 | ran = the_thread->cpu_time_used; |
---|
129 | if ( _Thread_Get_time_of_last_context_switch( the_thread, &last ) ) { |
---|
130 | Timestamp_Control used; |
---|
131 | _TOD_Get_uptime( &uptime ); |
---|
132 | _Timestamp_Subtract( &last, &uptime, &used ); |
---|
133 | _Timestamp_Add_to( &ran, &used ); |
---|
134 | } else { |
---|
135 | _TOD_Get_uptime( &uptime ); |
---|
136 | } |
---|
137 | _Timestamp_Subtract( &uptime_at_last_reset, &uptime, &total ); |
---|
138 | _Timestamp_Divide( &ran, &total, &ival, &fval ); |
---|
139 | |
---|
140 | /* |
---|
141 | * Print the information |
---|
142 | */ |
---|
143 | |
---|
144 | seconds = _Timestamp_Get_seconds( &ran ); |
---|
145 | nanoseconds = _Timestamp_Get_nanoseconds( &ran ) / |
---|
146 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
147 | (*print)( context, |
---|
148 | "%7" PRIu32 ".%06" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", |
---|
149 | seconds, nanoseconds, |
---|
150 | ival, fval |
---|
151 | ); |
---|
152 | } |
---|
153 | #else |
---|
154 | if (total_units) { |
---|
155 | uint64_t ival_64; |
---|
156 | |
---|
157 | ival_64 = the_thread->cpu_time_used; |
---|
158 | ival_64 *= 100000; |
---|
159 | ival = ival_64 / total_units; |
---|
160 | } else { |
---|
161 | ival = 0; |
---|
162 | } |
---|
163 | |
---|
164 | fval = ival % 1000; |
---|
165 | ival /= 1000; |
---|
166 | (*print)( context, |
---|
167 | "%14" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", |
---|
168 | the_thread->cpu_time_used, |
---|
169 | ival, |
---|
170 | fval |
---|
171 | ); |
---|
172 | #endif |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
178 | seconds = _Timestamp_Get_seconds( &total ); |
---|
179 | nanoseconds = _Timestamp_Get_nanoseconds( &total ) / |
---|
180 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
181 | (*print)( |
---|
182 | context, |
---|
183 | "------------+----------------------------------------+---------------+---------\n" |
---|
184 | " TIME SINCE LAST CPU USAGE RESET IN SECONDS: %7" PRIu32 ".%06" PRIu32 "\n" |
---|
185 | "-------------------------------------------------------------------------------\n", |
---|
186 | seconds, nanoseconds |
---|
187 | ); |
---|
188 | #else |
---|
189 | (*print)( |
---|
190 | context, |
---|
191 | "------------+----------------------------------------+---------------+---------\n" |
---|
192 | " TICKS SINCE LAST SYSTEM RESET: %14" PRIu32 "\n" |
---|
193 | " TOTAL UNITS: %14" PRIu32 "\n" |
---|
194 | "-------------------------------------------------------------------------------\n", |
---|
195 | _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset, |
---|
196 | total_units |
---|
197 | ); |
---|
198 | #endif |
---|
199 | } |
---|
200 | |
---|
201 | void rtems_cpu_usage_report( void ) |
---|
202 | { |
---|
203 | rtems_cpu_usage_report_with_plugin( NULL, printk_plugin ); |
---|
204 | } |
---|