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/printer.h> |
---|
29 | #include <rtems/score/threadimpl.h> |
---|
30 | #include <rtems/score/todimpl.h> |
---|
31 | |
---|
32 | #include "cpuuseimpl.h" |
---|
33 | |
---|
34 | typedef struct { |
---|
35 | const rtems_printer *printer; |
---|
36 | Timestamp_Control total; |
---|
37 | Timestamp_Control uptime_at_last_reset; |
---|
38 | } cpu_usage_context; |
---|
39 | |
---|
40 | static bool cpu_usage_visitor( Thread_Control *the_thread, void *arg ) |
---|
41 | { |
---|
42 | cpu_usage_context *ctx; |
---|
43 | char name[ 13 ]; |
---|
44 | uint32_t ival; |
---|
45 | uint32_t fval; |
---|
46 | Timestamp_Control uptime; |
---|
47 | Timestamp_Control used; |
---|
48 | uint32_t seconds; |
---|
49 | uint32_t nanoseconds; |
---|
50 | |
---|
51 | ctx = arg; |
---|
52 | rtems_object_get_name( the_thread->Object.id, sizeof( name ), name ); |
---|
53 | |
---|
54 | rtems_printf( |
---|
55 | ctx->printer, |
---|
56 | " 0x%08" PRIx32 " | %-38s |", |
---|
57 | the_thread->Object.id, |
---|
58 | name |
---|
59 | ); |
---|
60 | |
---|
61 | _Thread_Get_CPU_time_used( the_thread, &used ); |
---|
62 | _TOD_Get_uptime( &uptime ); |
---|
63 | _Timestamp_Subtract( &ctx->uptime_at_last_reset, &uptime, &ctx->total ); |
---|
64 | _Timestamp_Divide( &used, &ctx->total, &ival, &fval ); |
---|
65 | |
---|
66 | /* |
---|
67 | * Print the information |
---|
68 | */ |
---|
69 | |
---|
70 | seconds = _Timestamp_Get_seconds( &used ); |
---|
71 | nanoseconds = _Timestamp_Get_nanoseconds( &used ) / |
---|
72 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
73 | rtems_printf( ctx->printer, |
---|
74 | "%7" PRIu32 ".%06" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", |
---|
75 | seconds, nanoseconds, |
---|
76 | ival, fval |
---|
77 | ); |
---|
78 | |
---|
79 | return false; |
---|
80 | } |
---|
81 | |
---|
82 | /* |
---|
83 | * rtems_cpu_usage_report |
---|
84 | */ |
---|
85 | void rtems_cpu_usage_report_with_plugin( |
---|
86 | const rtems_printer *printer |
---|
87 | ) |
---|
88 | { |
---|
89 | cpu_usage_context ctx; |
---|
90 | uint32_t seconds; |
---|
91 | uint32_t nanoseconds; |
---|
92 | |
---|
93 | ctx.printer = printer; |
---|
94 | |
---|
95 | /* |
---|
96 | * When not using nanosecond CPU usage resolution, we have to count |
---|
97 | * the number of "ticks" we gave credit for to give the user a rough |
---|
98 | * guideline as to what each number means proportionally. |
---|
99 | */ |
---|
100 | _Timestamp_Set_to_zero( &ctx.total ); |
---|
101 | ctx.uptime_at_last_reset = CPU_usage_Uptime_at_last_reset; |
---|
102 | |
---|
103 | rtems_printf( |
---|
104 | printer, |
---|
105 | "-------------------------------------------------------------------------------\n" |
---|
106 | " CPU USAGE BY THREAD\n" |
---|
107 | "------------+----------------------------------------+---------------+---------\n" |
---|
108 | " ID | NAME | SECONDS | PERCENT\n" |
---|
109 | "------------+----------------------------------------+---------------+---------\n" |
---|
110 | ); |
---|
111 | |
---|
112 | rtems_task_iterate( cpu_usage_visitor, &ctx ); |
---|
113 | |
---|
114 | seconds = _Timestamp_Get_seconds( &ctx.total ); |
---|
115 | nanoseconds = _Timestamp_Get_nanoseconds( &ctx.total ) / |
---|
116 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
117 | rtems_printf( |
---|
118 | printer, |
---|
119 | "------------+----------------------------------------+---------------+---------\n" |
---|
120 | " TIME SINCE LAST CPU USAGE RESET IN SECONDS: %7" PRIu32 ".%06" PRIu32 "\n" |
---|
121 | "-------------------------------------------------------------------------------\n", |
---|
122 | seconds, nanoseconds |
---|
123 | ); |
---|
124 | } |
---|
125 | |
---|
126 | void rtems_cpu_usage_report( void ) |
---|
127 | { |
---|
128 | rtems_printer printer; |
---|
129 | rtems_print_printer_printk( &printer ); |
---|
130 | rtems_cpu_usage_report_with_plugin( &printer ); |
---|
131 | } |
---|