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 | const rtems_printer *printer |
---|
38 | ) |
---|
39 | { |
---|
40 | uint32_t i; |
---|
41 | uint32_t api_index; |
---|
42 | Thread_Control *the_thread; |
---|
43 | Objects_Information *information; |
---|
44 | char name[13]; |
---|
45 | uint32_t ival, fval; |
---|
46 | Timestamp_Control uptime, total, used, uptime_at_last_reset; |
---|
47 | uint32_t seconds, nanoseconds; |
---|
48 | |
---|
49 | /* |
---|
50 | * When not using nanosecond CPU usage resolution, we have to count |
---|
51 | * the number of "ticks" we gave credit for to give the user a rough |
---|
52 | * guideline as to what each number means proportionally. |
---|
53 | */ |
---|
54 | _Timestamp_Set_to_zero( &total ); |
---|
55 | uptime_at_last_reset = CPU_usage_Uptime_at_last_reset; |
---|
56 | |
---|
57 | rtems_printf( |
---|
58 | printer, |
---|
59 | "-------------------------------------------------------------------------------\n" |
---|
60 | " CPU USAGE BY THREAD\n" |
---|
61 | "------------+----------------------------------------+---------------+---------\n" |
---|
62 | " ID | NAME | SECONDS | PERCENT\n" |
---|
63 | "------------+----------------------------------------+---------------+---------\n" |
---|
64 | ); |
---|
65 | |
---|
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 | continue; |
---|
79 | |
---|
80 | rtems_object_get_name( the_thread->Object.id, sizeof(name), name ); |
---|
81 | |
---|
82 | rtems_printf( |
---|
83 | printer, |
---|
84 | " 0x%08" PRIx32 " | %-38s |", |
---|
85 | the_thread->Object.id, |
---|
86 | name |
---|
87 | ); |
---|
88 | |
---|
89 | _Thread_Get_CPU_time_used( the_thread, &used ); |
---|
90 | _TOD_Get_uptime( &uptime ); |
---|
91 | _Timestamp_Subtract( &uptime_at_last_reset, &uptime, &total ); |
---|
92 | _Timestamp_Divide( &used, &total, &ival, &fval ); |
---|
93 | |
---|
94 | /* |
---|
95 | * Print the information |
---|
96 | */ |
---|
97 | |
---|
98 | seconds = _Timestamp_Get_seconds( &used ); |
---|
99 | nanoseconds = _Timestamp_Get_nanoseconds( &used ) / |
---|
100 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
101 | rtems_printf( printer, |
---|
102 | "%7" PRIu32 ".%06" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", |
---|
103 | seconds, nanoseconds, |
---|
104 | ival, fval |
---|
105 | ); |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | seconds = _Timestamp_Get_seconds( &total ); |
---|
111 | nanoseconds = _Timestamp_Get_nanoseconds( &total ) / |
---|
112 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
113 | rtems_printf( |
---|
114 | printer, |
---|
115 | "------------+----------------------------------------+---------------+---------\n" |
---|
116 | " TIME SINCE LAST CPU USAGE RESET IN SECONDS: %7" PRIu32 ".%06" PRIu32 "\n" |
---|
117 | "-------------------------------------------------------------------------------\n", |
---|
118 | seconds, nanoseconds |
---|
119 | ); |
---|
120 | } |
---|
121 | |
---|
122 | void rtems_cpu_usage_report( void ) |
---|
123 | { |
---|
124 | rtems_printer printer; |
---|
125 | rtems_print_printer_printk( &printer ); |
---|
126 | rtems_cpu_usage_report_with_plugin( &printer ); |
---|
127 | } |
---|