1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSImplCPUUsageReporting |
---|
7 | * |
---|
8 | * @brief This source file contains the definition of |
---|
9 | * rtems_cpu_usage_report() and rtems_cpu_usage_report_with_plugin(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * COPYRIGHT (c) 1989-2010. |
---|
14 | * On-Line Applications Research Corporation (OAR). |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifdef HAVE_CONFIG_H |
---|
39 | #include "config.h" |
---|
40 | #endif |
---|
41 | |
---|
42 | #include <string.h> |
---|
43 | #include <stdlib.h> |
---|
44 | #include <stdio.h> |
---|
45 | #include <ctype.h> |
---|
46 | #include <inttypes.h> |
---|
47 | |
---|
48 | #include <rtems/cpuuse.h> |
---|
49 | #include <rtems/printer.h> |
---|
50 | #include <rtems/score/threadimpl.h> |
---|
51 | #include <rtems/score/todimpl.h> |
---|
52 | |
---|
53 | #include "cpuuseimpl.h" |
---|
54 | |
---|
55 | typedef struct { |
---|
56 | const rtems_printer *printer; |
---|
57 | Timestamp_Control total; |
---|
58 | Timestamp_Control uptime_at_last_reset; |
---|
59 | } cpu_usage_context; |
---|
60 | |
---|
61 | static bool cpu_usage_visitor( Thread_Control *the_thread, void *arg ) |
---|
62 | { |
---|
63 | cpu_usage_context *ctx; |
---|
64 | char name[ 38 ]; |
---|
65 | uint32_t ival; |
---|
66 | uint32_t fval; |
---|
67 | Timestamp_Control uptime; |
---|
68 | Timestamp_Control used; |
---|
69 | uint32_t seconds; |
---|
70 | uint32_t nanoseconds; |
---|
71 | |
---|
72 | ctx = arg; |
---|
73 | _Thread_Get_name( the_thread, name, sizeof( name ) ); |
---|
74 | |
---|
75 | used = _Thread_Get_CPU_time_used_after_last_reset( the_thread ); |
---|
76 | _TOD_Get_uptime( &uptime ); |
---|
77 | _Timestamp_Subtract( &ctx->uptime_at_last_reset, &uptime, &ctx->total ); |
---|
78 | _Timestamp_Divide( &used, &ctx->total, &ival, &fval ); |
---|
79 | seconds = _Timestamp_Get_seconds( &used ); |
---|
80 | nanoseconds = _Timestamp_Get_nanoseconds( &used ) / |
---|
81 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
82 | |
---|
83 | rtems_printf( |
---|
84 | ctx->printer, |
---|
85 | " 0x%08" PRIx32 " | %-38s |" |
---|
86 | "%7" PRIu32 ".%06" PRIu32 " |%4" PRIu32 ".%03" PRIu32 "\n", |
---|
87 | the_thread->Object.id, |
---|
88 | name, |
---|
89 | seconds, nanoseconds, |
---|
90 | ival, fval |
---|
91 | ); |
---|
92 | |
---|
93 | return false; |
---|
94 | } |
---|
95 | |
---|
96 | /* |
---|
97 | * rtems_cpu_usage_report |
---|
98 | */ |
---|
99 | void rtems_cpu_usage_report_with_plugin( |
---|
100 | const rtems_printer *printer |
---|
101 | ) |
---|
102 | { |
---|
103 | cpu_usage_context ctx; |
---|
104 | uint32_t seconds; |
---|
105 | uint32_t nanoseconds; |
---|
106 | |
---|
107 | ctx.printer = printer; |
---|
108 | |
---|
109 | /* |
---|
110 | * When not using nanosecond CPU usage resolution, we have to count |
---|
111 | * the number of "ticks" we gave credit for to give the user a rough |
---|
112 | * guideline as to what each number means proportionally. |
---|
113 | */ |
---|
114 | _Timestamp_Set_to_zero( &ctx.total ); |
---|
115 | ctx.uptime_at_last_reset = CPU_usage_Uptime_at_last_reset; |
---|
116 | |
---|
117 | rtems_printf( |
---|
118 | printer, |
---|
119 | "-------------------------------------------------------------------------------\n" |
---|
120 | " CPU USAGE BY THREAD\n" |
---|
121 | "------------+----------------------------------------+---------------+---------\n" |
---|
122 | " ID | NAME | SECONDS | PERCENT\n" |
---|
123 | "------------+----------------------------------------+---------------+---------\n" |
---|
124 | ); |
---|
125 | |
---|
126 | rtems_task_iterate( cpu_usage_visitor, &ctx ); |
---|
127 | |
---|
128 | seconds = _Timestamp_Get_seconds( &ctx.total ); |
---|
129 | nanoseconds = _Timestamp_Get_nanoseconds( &ctx.total ) / |
---|
130 | TOD_NANOSECONDS_PER_MICROSECOND; |
---|
131 | rtems_printf( |
---|
132 | printer, |
---|
133 | "------------+----------------------------------------+---------------+---------\n" |
---|
134 | " TIME SINCE LAST CPU USAGE RESET IN SECONDS: %7" PRIu32 ".%06" PRIu32 "\n" |
---|
135 | "-------------------------------------------------------------------------------\n", |
---|
136 | seconds, nanoseconds |
---|
137 | ); |
---|
138 | } |
---|
139 | |
---|
140 | void rtems_cpu_usage_report( void ) |
---|
141 | { |
---|
142 | rtems_printer printer; |
---|
143 | rtems_print_printer_printk( &printer ); |
---|
144 | rtems_cpu_usage_report_with_plugin( &printer ); |
---|
145 | } |
---|