Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Timing.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 //====================================================================
12 // Timing.cpp
13 //--------------------------------------------------------------------
14 //
15 // Package : System (The LHCb System service)
16 //
17 // Description: Implementation of Systems internals
18 //
19 // Author : M.Frank
20 // Created : 13/1/99
21 // Changes :
22 //====================================================================
23 #define GAUDIKERNEL_TIMING_CPP
24 
25 #include "ProcessDescriptor.h"
26 #include <GaudiKernel/Timing.h>
27 
28 #include <climits>
29 #include <ctime>
30 #include <sys/time.h>
31 #include <sys/times.h>
32 #include <unistd.h>
33 
34 static const long long UNIX_BASE_TIME = 0;
35 
36 // convert time from internal representation to the appropriate type
37 // Internal representation for Unix: 1 clock tick (usually 10 milliseconds)
38 long long System::adjustTime( TimeType typ, long long t ) {
39  if ( t != -1 ) {
41  // t /= CLK_TCK ; // needs division by clock tick unit
45 
47  switch ( typ ) {
48  case Year:
49  return adjustTime<Year>( t );
50  case Month:
51  return adjustTime<Month>( t );
52  case Day:
53  return adjustTime<Day>( t );
54  case Hour:
55  return adjustTime<Hour>( t );
56  case Min:
57  return adjustTime<Min>( t );
58  case Sec:
59  return adjustTime<Sec>( t );
60  case milliSec:
61  return adjustTime<milliSec>( t );
62  case microSec:
63  return adjustTime<microSec>( t );
64  case nanoSec:
65  return adjustTime<nanoSec>( t );
66  case Native:
67  return adjustTime<Native>( t );
68  default:
69  return t;
70  }
71  }
72  return t;
73 }
74 
76 long long System::tickCount() {
77  long long count = 10000;
78  struct tms buf;
79  count *= 10 * times( &buf );
80  return count;
81 }
82 
83 #include <iostream>
84 
86 long long System::currentTime( TimeType typ ) {
87  switch ( typ ) {
88  case Year:
89  return currentTime<Year>();
90  case Month:
91  return currentTime<Month>();
92  case Day:
93  return currentTime<Day>();
94  case Hour:
95  return currentTime<Hour>();
96  case Min:
97  return currentTime<Min>();
98  case Sec:
99  return currentTime<Sec>();
100  case milliSec:
101  return currentTime<milliSec>();
102  case microSec:
103  return currentTime<microSec>();
104  case nanoSec:
105  return currentTime<nanoSec>();
106  case Native:
107  return currentTime<Native>();
108  }
109  return currentTime<Native>();
110 }
111 
113 long long System::systemStart( TimeType typ ) {
114  static long long sys_start = 0;
115  if ( 0 == sys_start ) {
116  long long c = currentTime( microSec );
117  long long t = tickCount();
118  sys_start = 10 * c - t;
119  }
120  return adjustTime( typ, sys_start );
121 }
122 
124 long long System::upTime( TimeType typ ) {
125  static long long sys_start = 10 * systemStart( microSec );
126  return adjustTime( typ, 10 * currentTime( microSec ) - sys_start );
127 }
128 
130 long long System::creationTime( TimeType typ, InfoType fetch, long pid ) {
131  long long created = 0;
132  KERNEL_USER_TIMES info;
133  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
134  created = adjustTime( typ, info.CreateTime - UNIX_BASE_TIME );
135  }
136  return created;
137 }
138 
140 long long System::remainingTime( TimeType typ, InfoType fetch, long pid ) {
141  long long left = 0;
142  QUOTA_LIMITS quota;
143  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
144  if ( left == -1 ) {
145  // left = _I64_MAX;
146  } else {
147  left = adjustTime( typ, quota.TimeLimit );
148  }
149  }
150  return left;
151 }
152 
154 long long System::ellapsedTime( TimeType typ, InfoType fetch, long pid ) {
155  KERNEL_USER_TIMES info;
156  long long ellapsed = currentTime( microSec ) * 10;
157  if ( getProcess()->query( pid, fetch, &info ) ) {
158  ellapsed = adjustTime( typ, ellapsed + UNIX_BASE_TIME - info.CreateTime );
159  }
160  return ellapsed;
161 }
162 
164 long long System::kernelTime( TimeType typ, InfoType fetch, long pid ) {
165  KERNEL_USER_TIMES info;
166  long long kerneltime = 0;
167  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
168  kerneltime = adjustTime( typ, info.KernelTime );
169  }
170  return kerneltime;
171 }
172 
174 long long System::userTime( TimeType typ, InfoType fetch, long pid ) {
175  long long usertime = 0;
176  KERNEL_USER_TIMES info;
177  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) { usertime = adjustTime( typ, info.UserTime ); }
178  return usertime;
179 }
180 
182 long long System::cpuTime( TimeType typ, InfoType fetch, long pid ) {
183  long long cputime = 0;
184  KERNEL_USER_TIMES info;
185  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
186  cputime = adjustTime( typ, info.KernelTime + info.UserTime );
187  }
188  return cputime;
189 }
190 
191 namespace System {
193  KERNEL_USER_TIMES info;
194  if ( getProcess()->query( pid, Times, &info ) ) {
195  return ProcessTime( info.KernelTime, info.UserTime, currentTime<Native>() - info.CreateTime );
196  }
197  return ProcessTime(); // return 0s in case of problems
198  }
199 } // namespace System
System::milliSec
@ milliSec
Definition: Timing.h:45
System::KERNEL_USER_TIMES
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Definition: ProcessDescriptor.h:101
System::currentTime< Month >
template long long currentTime< Month >()
System::Hour
@ Hour
Definition: Timing.h:45
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:15
System::QUOTA_LIMITS::TimeLimit
long long TimeLimit
Definition: ProcessDescriptor.h:47
System::adjustTime< Month >
long long adjustTime< Month >(long long t)
Definition: Timing.h:228
System::Year
@ Year
Definition: Timing.h:45
System::KERNEL_USER_TIMES::UserTime
long long UserTime
Definition: ProcessDescriptor.h:105
System::remainingTime
GAUDI_API long long remainingTime(TimeType typ=milliSec, InfoType fetch=Quota, long pid=-1)
Maximum processing time left for this process.
Definition: Timing.cpp:140
System::microSec
@ microSec
Definition: Timing.h:45
NewInputWrite.fetch
fetch
Definition: NewInputWrite.py:45
System::currentTime< Hour >
template long long currentTime< Hour >()
gaudirun.c
c
Definition: gaudirun.py:525
System::TimeType
TimeType
Time type for conversion.
Definition: Timing.h:45
System::kernelTime
GAUDI_API long long kernelTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU kernel mode time of process in milliseconds.
Definition: Timing.cpp:164
System::nanoSec
@ nanoSec
Definition: Timing.h:45
System::Month
@ Month
Definition: Timing.h:45
System::currentTime< Year >
template long long currentTime< Year >()
bug_34121.t
t
Definition: bug_34121.py:31
System::ellapsedTime
GAUDI_API long long ellapsedTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Elapsed time since start of process in milliseconds.
Definition: Timing.cpp:154
System::adjustTime< milliSec >
long long adjustTime< milliSec >(long long t)
Definition: Timing.h:216
System::adjustTime< Day >
long long adjustTime< Day >(long long t)
Definition: Timing.h:200
System::adjustTime< Year >
long long adjustTime< Year >(long long t)
Definition: Timing.h:196
System::NoFetch
@ NoFetch
Definition: SystemBase.h:15
System::adjustTime< Native >
long long adjustTime< Native >(long long t)
Definition: Timing.h:232
System::QUOTA_LIMITS
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
Definition: ProcessDescriptor.h:41
System::currentTime< milliSec >
template long long currentTime< milliSec >()
System::ProcessTime
Simple class to hold the time information of a process.
Definition: Timing.h:134
System::tickCount
GAUDI_API long long tickCount()
Retrieve the number of ticks since system startup.
Definition: Timing.cpp:76
System::currentTime
GAUDI_API long long currentTime()
Retrieve absolute system time.
Definition: Timing.h:238
System::adjustTime< nanoSec >
long long adjustTime< nanoSec >(long long t)
Definition: Timing.h:224
System::adjustTime< microSec >
long long adjustTime< microSec >(long long t)
Definition: Timing.h:220
System::KERNEL_USER_TIMES::KernelTime
long long KernelTime
Definition: ProcessDescriptor.h:104
System::upTime
GAUDI_API long long upTime(TimeType typ=Hour)
Maximum processing time left for this process.
Definition: Timing.cpp:124
System::cpuTime
GAUDI_API long long cpuTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Consumed CPU time of process in milliseconds.
Definition: Timing.cpp:182
Timing.h
System::userTime
GAUDI_API long long userTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU user mode time of process in milliseconds.
Definition: Timing.cpp:174
System::currentTime< microSec >
template long long currentTime< microSec >()
System::currentTime< Min >
template long long currentTime< Min >()
System::adjustTime< Min >
long long adjustTime< Min >(long long t)
Definition: Timing.h:208
System::KERNEL_USER_TIMES::CreateTime
long long CreateTime
Definition: ProcessDescriptor.h:102
System::adjustTime< Hour >
long long adjustTime< Hour >(long long t)
Definition: Timing.h:204
System::Times
@ Times
Definition: SystemBase.h:15
System::Sec
@ Sec
Definition: Timing.h:45
System::currentTime< Sec >
template long long currentTime< Sec >()
System::Day
@ Day
Definition: Timing.h:45
System::adjustTime< Sec >
long long adjustTime< Sec >(long long t)
Definition: Timing.h:212
System::currentTime< Native >
template long long currentTime< Native >()
System::Native
@ Native
Definition: Timing.h:45
System::currentTime< Day >
template long long currentTime< Day >()
System::Min
@ Min
Definition: Timing.h:45
System::adjustTime
GAUDI_API long long adjustTime(TimeType typ, long long timevalue)
Convert time from OS native time to requested representation (Experts only)
Definition: Timing.cpp:38
System::getProcess
ProcessDescriptor * getProcess()
Retrieve Process structure.
Definition: ProcessDescriptor.h:141
System
Note: OS specific details for environment resolution.
Definition: Environment.h:25
ProcessDescriptor.h
System::getProcessTime
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:192
System::systemStart
GAUDI_API long long systemStart(TimeType typ=Sec)
Maximum processing time left for this process.
Definition: Timing.cpp:113
System::currentTime< nanoSec >
template long long currentTime< nanoSec >()
System::creationTime
GAUDI_API long long creationTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Process Creation time.
Definition: Timing.cpp:130