source: rtems/cpukit/libmisc/cpuuse/cpuusagereport.c @ 793dd46

4.104.114.84.95
Last change on this file since 793dd46 was 793dd46, checked in by Joel Sherrill <joel.sherrill@…>, on 07/18/07 at 21:31:49

2007-07-18 Joel Sherrill <joel.sherrill@…>

  • libmisc/Makefile.am, libmisc/cpuuse/cpuusagereport.c, libmisc/cpuuse/cpuusagereset.c: Fix bug where cpu usage calculation was always using uptime not time since last cpu usage reset when using nanoseconds granularity.
  • libmisc/cpuuse/cpuusagedata.c: New file.
  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 *  CPU Usage Reporter
3 *
4 *  COPYRIGHT (c) 1989-2007
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 <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include <inttypes.h>
25
26#include <rtems/cpuuse.h>
27#include <rtems/bspIo.h>
28
29#if defined(RTEMS_ENABLE_NANOSECOND_RATE_MONOTONIC_STATISTICS) || \
30    defined(RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS)
31  #include <rtems/score/timespec.h>
32
33  /* We print to 1/10's of milliseconds */
34  #define NANOSECONDS_DIVIDER 100000
35  #define PERCENT_FMT "%04" PRId32
36#endif
37
38#ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
39  extern struct timespec    CPU_usage_Uptime_at_last_reset;
40#else
41  extern uint32_t           CPU_usage_Ticks_at_last_reset;
42#endif
43
44/*PAGE
45 *
46 *  rtems_cpu_usage_report
47 */
48
49void rtems_cpu_usage_report( void )
50{
51  uint32_t             i;
52  uint32_t             api_index;
53  Thread_Control      *the_thread;
54  Objects_Information *information;
55  char                 name[5];
56  uint32_t             ival, fval;
57  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
58    struct timespec    uptime, total;
59  #else
60    uint32_t           total_units = 0;
61  #endif
62
63  /*
64   *  When not using nanosecond CPU usage resolution, we have to count
65   *  the number of "ticks" we gave credit for to give the user a rough
66   *  guideline as to what each number means proportionally.
67   */
68  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
69    _TOD_Get_uptime( &uptime );
70    _Timespec_Subtract( &CPU_usage_Uptime_at_last_reset, &uptime, &total );
71  #else
72    for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; api_index++ ) {
73      if ( !_Objects_Information_table[ api_index ] )
74        continue;
75      information = _Objects_Information_table[ api_index ][ 1 ];
76      if ( information ) {
77        for ( i=1 ; i <= information->maximum ; i++ ) {
78          the_thread = (Thread_Control *)information->local_table[ i ];
79
80          if ( the_thread )
81            total_units += the_thread->ticks_executed;
82        }
83      }
84    }
85  #endif
86 
87  printk( "CPU Usage by thread\n"
88    #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
89          "   ID        NAME    SECONDS   PERCENT\n"
90    #else
91          "   ID        NAME     TICKS    PERCENT\n"
92    #endif
93  );
94
95  for ( api_index = 1 ;
96        api_index <= OBJECTS_APIS_LAST ;
97        api_index++ ) {
98    if ( !_Objects_Information_table[ api_index ] )
99      continue;
100    information = _Objects_Information_table[ api_index ][ 1 ];
101    if ( information ) {
102      for ( i=1 ; i <= information->maximum ; i++ ) {
103        the_thread = (Thread_Control *)information->local_table[ i ];
104
105        if ( !the_thread )
106          continue;
107
108        rtems_object_get_name( the_thread->Object.id, sizeof(name), name );
109 
110        printk( "0x%08" PRIx32 "   %4s    ", the_thread->Object.id, name );
111
112        #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
113          _Timespec_Divide( &the_thread->cpu_time_used, &total, &ival, &fval );
114
115          printk(
116            "%" PRId32 ".%06d"                 /* cpu time used */     
117            " %3" PRId32 ".%02" PRId32 "\n",  /* percentage */
118            the_thread->cpu_time_used.tv_sec,
119              the_thread->cpu_time_used.tv_nsec /
120                 TOD_NANOSECONDS_PER_MICROSECOND,
121            ival,
122            fval
123          );
124        #else
125          ival = (total_units) ?
126                   the_thread->ticks_executed * 10000 / total_units : 0;
127          fval = ival % 100;
128          ival /= 100;
129          printk(
130            "%8" PRId32 "     %3" PRId32 ".%02" PRId32"\n",
131            the_thread->ticks_executed,
132            ival,
133            fval
134          );
135        #endif
136      }
137    }
138  }
139
140  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
141    printk( "Time since last reset %d.%06d seconds\n",
142       total.tv_sec,
143       total.tv_nsec / TOD_NANOSECONDS_PER_MICROSECOND
144    );
145  #else
146    printk(
147      "Ticks since last reset = %" PRId32 "\n",
148      _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset
149    );
150    printk( "Total Units = %" PRId32 "\n\n", total_units );
151  #endif
152}
Note: See TracBrowser for help on using the repository browser.