source: rtems/cpukit/libcsupport/src/__times.c @ d37adfe5

5
Last change on this file since d37adfe5 was d37adfe5, checked in by Sebastian Huber <sebastian.huber@…>, on 03/03/16 at 06:02:03

score: Fix CPU time used by executing threads

The CPU time used of a thread was previously maintained per-processor
mostly during _Thread_Dispatch(). However, on SMP configurations the
actual processor of a thread is difficult to figure out since thread
dispatching is a highly asynchronous process (e.g. via inter-processor
interrupts). Only the intended processor of a thread is known to the
scheduler easily. Do the CPU usage accounting during thread heir
updates in the context of the scheduler operations. Provide the
function _Thread_Get_CPU_time_used() to get the CPU usage of a thread
using proper locks to get a consistent value.

Close #2627.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Get Process Times
5 *  @ingroup libcsupport
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2013.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21/*
22 *  Needed to get the prototype for this newlib helper method
23 */
24#define _COMPILING_NEWLIB
25
26#include <rtems.h>
27
28#include <sys/times.h>
29#include <sys/time.h>
30
31#include <string.h>
32#include <time.h>
33
34#include <rtems/seterr.h>
35#include <rtems/score/todimpl.h>
36#include <rtems/score/timestamp.h>
37#include <rtems/score/threadimpl.h>
38
39/**
40 *  POSIX 1003.1b 4.5.2 - Get Process Times
41 */
42clock_t _times(
43   struct tms  *ptms
44)
45{
46  rtems_interval ticks, us_per_tick;
47
48  if ( !ptms )
49    rtems_set_errno_and_return_minus_one( EFAULT );
50
51  memset( ptms, 0, sizeof( *ptms ) );
52
53  /*
54   *  This call does not depend on TOD being initialized and can't fail.
55   */
56
57  ticks = rtems_clock_get_ticks_since_boot();
58  us_per_tick = rtems_configuration_get_microseconds_per_tick();
59
60  /*
61   *  RTEMS technically has no notion of system versus user time
62   *  since there is no separation of OS from application tasks.
63   *  But we can at least make a distinction between the number
64   *  of ticks since boot and the number of ticks executed by this
65   *  this thread.
66   */
67  {
68    Timestamp_Control  cpu_time_used;
69    Timestamp_Control  per_tick;
70    uint32_t           ticks_of_executing;
71    uint32_t           fractional_ticks;
72
73    _Thread_Get_CPU_time_used( _Thread_Get_executing(), &cpu_time_used );
74    _Timestamp_Set(
75      &per_tick,
76      rtems_configuration_get_microseconds_per_tick() /
77          TOD_MICROSECONDS_PER_SECOND,
78      (rtems_configuration_get_nanoseconds_per_tick() %
79          TOD_NANOSECONDS_PER_SECOND)
80    );
81    _Timestamp_Divide(
82      &cpu_time_used,
83      &per_tick,
84      &ticks_of_executing,
85      &fractional_ticks
86    );
87
88    ptms->tms_utime = ticks_of_executing * us_per_tick;
89  }
90
91  ptms->tms_stime  = ticks * us_per_tick;
92
93  return ticks * us_per_tick;
94}
95
96/**
97 *  times() system call wrapper for _times() above.
98 */
99clock_t times(
100   struct tms  *ptms
101)
102{
103  return _times( ptms );
104}
105
106#if defined(RTEMS_NEWLIB)
107
108#include <reent.h>
109
110/**
111 *  This is the Newlib dependent reentrant version of times().
112 */
113clock_t _times_r(
114   struct _reent *ptr RTEMS_UNUSED,
115   struct tms  *ptms
116)
117{
118  return _times( ptms );
119}
120#endif
Note: See TracBrowser for help on using the repository browser.