source: rtems/cpukit/libmisc/cpuuse/cpuuse.c @ c69885a5

4.104.114.84.95
Last change on this file since c69885a5 was bc11ec2, checked in by Joel Sherrill <joel.sherrill@…>, on 09/14/06 at 15:07:19

2006-09-14 Joel Sherrill <joel@…>

  • libmisc/cpuuse/cpuuse.c, libmisc/cpuuse/cpuuse.h: Promote CPU Usage to first class citizen. Rename to start with rtems_ and include documentation in user guide.
  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 *  CPU Usage Reporter
3 *
4 *  COPYRIGHT (c) 1989-2006
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 <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <inttypes.h>
26
27#include <rtems/cpuuse.h>
28
29uint32_t   CPU_usage_Ticks_at_last_reset;
30
31/*PAGE
32 *
33 *  rtems_cpu_usage_report
34 */
35
36void rtems_cpu_usage_report( void )
37{
38  uint32_t             i;
39  uint32_t             api_index;
40  Thread_Control      *the_thread;
41  Objects_Information *information;
42  uint32_t             u32_name;
43  char                *cname;
44  char                 name[5];
45  uint32_t             ival, fval;
46  uint32_t             total_units = 0;
47
48  for ( api_index = 1 ;
49        api_index <= OBJECTS_APIS_LAST ;
50        api_index++ ) {
51    if ( !_Objects_Information_table[ api_index ] )
52      continue;
53    information = _Objects_Information_table[ api_index ][ 1 ];
54    if ( information ) {
55      for ( i=1 ; i <= information->maximum ; i++ ) {
56        the_thread = (Thread_Control *)information->local_table[ i ];
57
58        if ( the_thread )
59          total_units += the_thread->ticks_executed;
60      }
61    }
62  }
63
64  fprintf(stdout,"CPU Usage by thread\n");
65  fprintf(stdout, "   ID        NAME        TICKS    PERCENT\n" );
66
67  for ( api_index = 1 ;
68        api_index <= OBJECTS_APIS_LAST ;
69        api_index++ ) {
70    if ( !_Objects_Information_table[ api_index ] )
71      continue;
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        if ( information->is_string ) {
81          cname = the_thread->Object.name;
82          name[ 0 ] = cname[0];
83          name[ 1 ] = cname[1];
84          name[ 2 ] = cname[2];
85          name[ 3 ] = cname[3];
86          name[ 4 ] = '\0';
87        } else {
88          u32_name = (uint32_t  )the_thread->Object.name;
89          name[ 0 ] = (u32_name >> 24) & 0xff;
90          name[ 1 ] = (u32_name >> 16) & 0xff;
91          name[ 2 ] = (u32_name >>  8) & 0xff;
92          name[ 3 ] = (u32_name >>  0) & 0xff;
93          name[ 4 ] = '\0';
94        }
95
96        if ( !isprint(name[0]) ) name[0] = '*';
97        if ( !isprint(name[1]) ) name[1] = '*';
98        if ( !isprint(name[2]) ) name[2] = '*';
99        if ( !isprint(name[3]) ) name[3] = '*';
100
101        ival = total_units ?
102                 the_thread->ticks_executed * 10000 / total_units : 0;
103        fval = ival % 100;
104        ival /= 100;
105        fprintf(stdout,
106          "0x%08" PRIu32 "   %4s    %8" PRId32 "     %3" PRId32
107             ".%2.2" PRId32"\n",
108          the_thread->Object.id,
109          name,
110          the_thread->ticks_executed,
111          ival,
112          fval
113        );
114      }
115    }
116  }
117
118  fprintf(stdout,
119    "\nTicks since last reset = %" PRId32 "\n",
120    _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset
121  );
122  fprintf(stdout, "\nTotal Units = %" PRId32 "\n", total_units );
123}
124
125static void CPU_usage_Per_thread_handler(
126  Thread_Control *the_thread
127)
128{
129  the_thread->ticks_executed = 0;
130}
131
132/*
133 *  rtems_cpu_usage_reset
134 */
135void rtems_cpu_usage_reset( void )
136{
137  CPU_usage_Ticks_at_last_reset = _Watchdog_Ticks_since_boot;
138
139  rtems_iterate_over_all_threads(CPU_usage_Per_thread_handler);
140}
Note: See TracBrowser for help on using the repository browser.