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

4.104.114.84.95
Last change on this file since bc11ec2 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
RevLine 
[fc7bc51]1/*
2 *  CPU Usage Reporter
3 *
[bc11ec2]4 *  COPYRIGHT (c) 1989-2006
[fc7bc51]5 *  On-Line Applications Research Corporation (OAR).
6 *
[98e4ebf5]7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
[3160ff6]9 *  http://www.rtems.com/license/LICENSE.
[fc7bc51]10 *
[bc11ec2]11 *  $Id$
[fc7bc51]12 */
13
[550c3df7]14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
17
[fc7bc51]18#include <rtems.h>
19
20#include <assert.h>
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
[ca5fe67]24#include <ctype.h>
[3523321]25#include <inttypes.h>
[fc7bc51]26
[37de72b]27#include <rtems/cpuuse.h>
[fc7bc51]28
[3e08d4e]29uint32_t   CPU_usage_Ticks_at_last_reset;
[fc7bc51]30
31/*PAGE
32 *
[bc11ec2]33 *  rtems_cpu_usage_report
[fc7bc51]34 */
35
[bc11ec2]36void rtems_cpu_usage_report( void )
[fc7bc51]37{
[3e08d4e]38  uint32_t             i;
39  uint32_t             api_index;
[fc7bc51]40  Thread_Control      *the_thread;
41  Objects_Information *information;
[3e08d4e]42  uint32_t             u32_name;
[e482c592]43  char                *cname;
[fc7bc51]44  char                 name[5];
[1a561f8]45  uint32_t             ival, fval;
[3e08d4e]46  uint32_t             total_units = 0;
[fc7bc51]47
[63977bb4]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 ) {
[fc7bc51]55      for ( i=1 ; i <= information->maximum ; i++ ) {
56        the_thread = (Thread_Control *)information->local_table[ i ];
[aed742c]57
[fc7bc51]58        if ( the_thread )
59          total_units += the_thread->ticks_executed;
60      }
61    }
62  }
63
[714f06c]64  fprintf(stdout,"CPU Usage by thread\n");
65  fprintf(stdout, "   ID        NAME        TICKS    PERCENT\n" );
[fc7bc51]66
[aed742c]67  for ( api_index = 1 ;
68        api_index <= OBJECTS_APIS_LAST ;
[63977bb4]69        api_index++ ) {
70    if ( !_Objects_Information_table[ api_index ] )
71      continue;
72    information = _Objects_Information_table[ api_index ][ 1 ];
73    if ( information ) {
[fc7bc51]74      for ( i=1 ; i <= information->maximum ; i++ ) {
75        the_thread = (Thread_Control *)information->local_table[ i ];
[aed742c]76
[fc7bc51]77        if ( !the_thread )
78          continue;
79
[e482c592]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 {
[3e08d4e]88          u32_name = (uint32_t  )the_thread->Object.name;
[e482c592]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        }
[fc7bc51]95
[ca5fe67]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
[3523321]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",
[fc7bc51]108          the_thread->Object.id,
109          name,
110          the_thread->ticks_executed,
[3523321]111          ival,
112          fval
[fc7bc51]113        );
114      }
115    }
116  }
117
[714f06c]118  fprintf(stdout,
[3523321]119    "\nTicks since last reset = %" PRId32 "\n",
[fc7bc51]120    _Watchdog_Ticks_since_boot - CPU_usage_Ticks_at_last_reset
121  );
[3523321]122  fprintf(stdout, "\nTotal Units = %" PRId32 "\n", total_units );
[fc7bc51]123}
124
[236b701]125static void CPU_usage_Per_thread_handler(
126  Thread_Control *the_thread
127)
128{
129  the_thread->ticks_executed = 0;
130}
131
[bc11ec2]132/*
133 *  rtems_cpu_usage_reset
134 */
135void rtems_cpu_usage_reset( void )
[fc7bc51]136{
137  CPU_usage_Ticks_at_last_reset = _Watchdog_Ticks_since_boot;
138
[236b701]139  rtems_iterate_over_all_threads(CPU_usage_Per_thread_handler);
[fc7bc51]140}
Note: See TracBrowser for help on using the repository browser.