source: rtems/cpukit/libcsupport/src/__times.c @ 80fca28

4.115
Last change on this file since 80fca28 was 5f7f469, checked in by Sebastian Huber <sebastian.huber@…>, on 05/04/15 at 07:22:26

libcsupport: Avoid Giant lock in _times()

  • Property mode set to 100644
File size: 2.9 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 <time.h>
30#include <sys/time.h>
31#include <errno.h>
32#include <rtems/seterr.h>
33#include <rtems/score/todimpl.h>
34#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
35  #include <rtems/score/timestamp.h>
36#endif
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  Thread_Control *executing;
48
49  if ( !ptms )
50    rtems_set_errno_and_return_minus_one( EFAULT );
51
52  /*
53   *  This call does not depend on TOD being initialized and can't fail.
54   */
55
56  ticks = rtems_clock_get_ticks_since_boot();
57  us_per_tick = rtems_configuration_get_microseconds_per_tick();
58
59  /*
60   *  RTEMS technically has no notion of system versus user time
61   *  since there is no separation of OS from application tasks.
62   *  But we can at least make a distinction between the number
63   *  of ticks since boot and the number of ticks executed by this
64   *  this thread.
65   */
66
67  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
68    {
69      Timestamp_Control  per_tick;
70      uint32_t           ticks_of_executing;
71      uint32_t           fractional_ticks;
72      Per_CPU_Control   *cpu_self;
73
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
82      cpu_self = _Thread_Dispatch_disable();
83      executing = _Thread_Executing;
84      _Thread_Update_cpu_time_used(
85        executing,
86        &_Thread_Time_of_last_context_switch
87      );
88      _Timestamp_Divide(
89        &executing->cpu_time_used,
90        &per_tick,
91        &ticks_of_executing,
92        &fractional_ticks
93      );
94      _Thread_Dispatch_enable( cpu_self );
95      ptms->tms_utime = ticks_of_executing * us_per_tick;
96    }
97  #else
98    executing = _Thread_Get_executing();
99    ptms->tms_utime  = executing->cpu_time_used * us_per_tick;
100  #endif
101  ptms->tms_stime  = ticks * us_per_tick;
102  ptms->tms_cutime = 0;
103  ptms->tms_cstime = 0;
104
105  return ticks * us_per_tick;
106}
107
108/**
109 *  times() system call wrapper for _times() above.
110 */
111clock_t times(
112   struct tms  *ptms
113)
114{
115  return _times( ptms );
116}
117
118#if defined(RTEMS_NEWLIB)
119
120#include <reent.h>
121
122/**
123 *  This is the Newlib dependent reentrant version of times().
124 */
125clock_t _times_r(
126   struct _reent *ptr __attribute__((unused)),
127   struct tms  *ptms
128)
129{
130  return _times( ptms );
131}
132#endif
Note: See TracBrowser for help on using the repository browser.