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

4.104.114.84.95
Last change on this file since c3330a8 was c3330a8, checked in by Joel Sherrill <joel.sherrill@…>, on May 17, 2007 at 10:46:45 PM

2007-05-17 Joel Sherrill <joel.sherrill@…>

  • ChangeLog?, configure.ac, libcsupport/src/times.c, libmisc/cpuuse/cpuuse.c, libmisc/stackchk/check.c, rtems/include/rtems/rtems/ratemon.h, rtems/src/ratemongetstatus.c, rtems/src/ratemonperiod.c, rtems/src/ratemonreportstatistics.c, rtems/src/ratemonresetall.c, rtems/src/ratemontimeout.c, score/Makefile.am, score/include/rtems/score/thread.h, score/include/rtems/score/timespec.h, score/src/threaddispatch.c, score/src/threadinitialize.c, score/src/threadtickletimeslice.c, score/src/timespecdivide.c: Add nanoseconds granularity to the rate monotonic period statistics and CPU usage statistics. This capability is enabled by default although may be conditionally disabled by the user. It could be too much overhead on small targets but it does not appear to be bad in early testing. Its impact on code size has not been evaluated either. It is possible that both forms of statistics gathering could be disabled with further tweaking of the conditional compilation.
  • score/src/timespecdividebyinteger.c: New file.
  • Property mode set to 100644
File size: 4.5 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#ifndef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
39  uint32_t   CPU_usage_Ticks_at_last_reset;
40#endif
41
42/*PAGE
43 *
44 *  rtems_cpu_usage_report
45 */
46
47void rtems_cpu_usage_report( void )
48{
49  uint32_t             i;
50  uint32_t             api_index;
51  Thread_Control      *the_thread;
52  Objects_Information *information;
53  char                 name[5];
54  uint32_t             ival, fval;
55  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
56    struct timespec    uptime;
57  #else
58    uint32_t           total_units = 0;
59  #endif
60
61  /*
62   *  When not using nanosecond CPU usage resolution, we have to count
63   *  the number of "ticks" we gave credit for to give the user a rough
64   *  guideline as to what each number means proportionally.
65   */
66  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
67    _TOD_Get_uptime( &uptime );
68  #else
69    for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST ; 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            total_units += the_thread->ticks_executed;
79        }
80      }
81    }
82  #endif
83 
84  printk( "CPU Usage by thread\n"
85          "   ID        NAME     TICKS    PERCENT\n"
86  );
87
88  for ( api_index = 1 ;
89        api_index <= OBJECTS_APIS_LAST ;
90        api_index++ ) {
91    if ( !_Objects_Information_table[ api_index ] )
92      continue;
93    information = _Objects_Information_table[ api_index ][ 1 ];
94    if ( information ) {
95      for ( i=1 ; i <= information->maximum ; i++ ) {
96        the_thread = (Thread_Control *)information->local_table[ i ];
97
98        if ( !the_thread )
99          continue;
100
101        rtems_object_get_name( the_thread->Object.id, sizeof(name), name );
102 
103        printk( "0x%08" PRIx32 "   %4s    ", the_thread->Object.id, name );
104
105        #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
106          _Timespec_Divide( &the_thread->cpu_time_used, &uptime, &ival, &fval );
107
108          printk(
109            "%" PRId32 ".%06d"                 /* cpu time used */     
110            " %3" PRId32 ".%02" PRId32 "\n",  /* percentage */
111            the_thread->cpu_time_used.tv_sec,
112              the_thread->cpu_time_used.tv_nsec /
113                 TOD_NANOSECONDS_PER_MICROSECOND,
114            ival,
115            fval
116          );
117        #else
118          ival = (total_units) ?
119                   the_thread->ticks_executed * 10000 / total_units : 0;
120          fval = ival % 100;
121          ival /= 100;
122          printk(
123            "%8" PRId32 "     %3" PRId32 ".%02" PRId32"\n",
124            the_thread->ticks_executed,
125            ival,
126            fval
127          );
128        #endif
129      }
130    }
131  }
132
133  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
134    printk( "Uptime %d.%06d seconds\n\n",
135       uptime.tv_sec,
136       uptime.tv_nsec / TOD_NANOSECONDS_PER_MICROSECOND
137    );
138  #else
139    printk(
140      "Ticks since last reset = %" PRId32 "\n",
141      _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset
142    );
143    printk( "Total Units = %" PRId32 "\n\n", total_units );
144  #endif
145}
146
147static void CPU_usage_Per_thread_handler(
148  Thread_Control *the_thread
149)
150{
151  #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
152    the_thread->cpu_time_used.tv_sec  = 0;
153    the_thread->cpu_time_used.tv_nsec = 0;
154  #else
155    the_thread->ticks_executed = 0;
156  #endif
157}
158
159/*
160 *  rtems_cpu_usage_reset
161 */
162void rtems_cpu_usage_reset( void )
163{
164  #ifndef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS
165    CPU_usage_Ticks_at_last_reset = _Watchdog_Ticks_since_boot;
166  #endif
167
168  rtems_iterate_over_all_threads(CPU_usage_Per_thread_handler);
169}
Note: See TracBrowser for help on using the repository browser.