The Gaudi Framework  v40r0 (475e45c1)
Timing.h
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 #pragma once
12 
13 #include <GaudiKernel/Kernel.h>
14 #include <GaudiKernel/SystemBase.h>
15 
16 #ifdef _WIN32
17 # include <windows.h>
18 #else
19 # include <sys/time.h>
20 #endif
21 
52 namespace System {
55 
57  GAUDI_API long long adjustTime( TimeType typ, long long timevalue );
58 
60  template <TimeType T>
61  inline long long adjustTime( long long timevalue );
62 
68  GAUDI_API long long ellapsedTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
76  GAUDI_API long long kernelTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
84  GAUDI_API long long userTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
92  GAUDI_API long long cpuTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
100  GAUDI_API long long remainingTime( TimeType typ = milliSec, InfoType fetch = Quota, long pid = -1 );
108  GAUDI_API long long creationTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
113  GAUDI_API long long systemStart( TimeType typ = Sec );
118  GAUDI_API long long upTime( TimeType typ = Hour );
124  template <TimeType T>
126  GAUDI_API long long currentTime();
127 
129  GAUDI_API long long currentTime( TimeType typ = milliSec );
130 
134  GAUDI_API long long tickCount();
135 
143  class ProcessTime {
144  public:
145  typedef long long TimeValueType;
146 
148  ProcessTime() : i_kernel( 0 ), i_user( 0 ), i_elapsed( 0 ) {}
149 
152 
154  template <TimeType T>
155  inline TimeValueType kernelTime() const {
156  return adjustTime<T>( i_kernel );
157  }
158 
160  template <TimeType T>
161  inline TimeValueType userTime() const {
162  return adjustTime<T>( i_user );
163  }
164 
166  template <TimeType T>
167  inline TimeValueType elapsedTime() const {
168  return adjustTime<T>( i_elapsed );
169  }
170 
172  template <TimeType T>
173  inline TimeValueType cpuTime() const {
174  return adjustTime<T>( i_user + i_kernel );
175  }
176 
178  inline ProcessTime operator-( const ProcessTime& rhs ) const {
179  return ProcessTime( i_kernel - rhs.i_kernel, i_user - rhs.i_user, i_elapsed - rhs.i_elapsed );
180  }
182  inline ProcessTime& operator+=( const ProcessTime& rhs ) {
183  i_kernel += rhs.i_kernel;
184  i_user += rhs.i_user;
185  i_elapsed += rhs.i_elapsed;
186  return *this;
187  }
188 
189  private:
192  };
193 
199  GAUDI_API ProcessTime getProcessTime( long pid = -1 );
200 } // namespace System
201 
202 // implementation of the templated functions
203 namespace System {
204  template <>
205  inline long long adjustTime<Year>( long long t ) {
206  return ( t == -1 ) ? t : t /= ( 1LL * 365 * 24 * 60 * 60 * 1000 * 1000 * 10 );
207  }
208  template <>
209  inline long long adjustTime<Day>( long long t ) {
210  return ( t == -1 ) ? t : t /= ( 1LL * 24 * 60 * 60 * 1000 * 1000 * 10 );
211  }
212  template <>
213  inline long long adjustTime<Hour>( long long t ) {
214  return ( t == -1 ) ? t : t /= ( 1LL * 60 * 60 * 1000 * 1000 * 10 );
215  }
216  template <>
217  inline long long adjustTime<Min>( long long t ) {
218  return ( t == -1 ) ? t : t /= ( 60 * 1000 * 1000 * 10 );
219  }
220  template <>
221  inline long long adjustTime<Sec>( long long t ) {
222  return ( t == -1 ) ? t : t /= ( 1000 * 1000 * 10 );
223  }
224  template <>
225  inline long long adjustTime<milliSec>( long long t ) {
226  return ( t == -1 ) ? t : t /= ( 1000 * 10 );
227  }
228  template <>
229  inline long long adjustTime<microSec>( long long t ) {
230  return ( t == -1 ) ? t : t /= ( 10LL );
231  }
232  template <>
233  inline long long adjustTime<nanoSec>( long long t ) {
234  return ( t == -1 ) ? t : t *= 100LL;
235  }
236  template <>
237  inline long long adjustTime<Month>( long long t ) {
238  return ( t == -1 ) ? t : t /= ( 1LL * 30 * 24 * 60 * 60 * 1000 * 1000 * 10 );
239  }
240  template <>
241  inline long long adjustTime<Native>( long long t ) {
242  return t;
243  }
244 
245  // This is frequently used and thus we inline it if possible
246  template <TimeType T>
247  inline long long currentTime() {
248 #ifdef _WIN32
249  long long current = 0;
250  ::GetSystemTimeAsFileTime( (FILETIME*)&current );
251  return adjustTime<T>( current - UNIX_BASE_TIME );
252 #else
253  struct timeval tv;
254  ::gettimeofday( &tv, 0 );
255  return adjustTime<T>( ( tv.tv_sec * 1000000 + tv.tv_usec ) * 10 );
256 #endif
257  }
258 
259  // Define all template versions here to avoid code bloat
260  template long long currentTime<Year>();
261  template long long currentTime<Month>();
262  template long long currentTime<Day>();
263  template long long currentTime<Hour>();
264  template long long currentTime<Min>();
265  template long long currentTime<Sec>();
266  template long long currentTime<milliSec>();
267  template long long currentTime<microSec>();
268  template long long currentTime<nanoSec>();
269  template long long currentTime<Native>();
270 } // namespace System
System::milliSec
@ milliSec
Definition: Timing.h:54
System::currentTime< Month >
template long long currentTime< Month >()
System::Hour
@ Hour
Definition: Timing.h:54
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:15
System::ProcessTime::ProcessTime
ProcessTime()
Constructor.
Definition: Timing.h:148
System::adjustTime< Month >
long long adjustTime< Month >(long long t)
Definition: Timing.h:237
System::Year
@ Year
Definition: Timing.h:54
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:155
System::microSec
@ microSec
Definition: Timing.h:54
NewInputWrite.fetch
fetch
Definition: NewInputWrite.py:45
System::ProcessTime::kernelTime
TimeValueType kernelTime() const
Retrieve the kernel time in the requested unit.
Definition: Timing.h:155
System::currentTime< Hour >
template long long currentTime< Hour >()
System::ProcessTime::ProcessTime
ProcessTime(TimeValueType k, TimeValueType u, TimeValueType e)
Constructor.
Definition: Timing.h:151
System::TimeType
TimeType
Time type for conversion.
Definition: Timing.h:54
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:179
System::nanoSec
@ nanoSec
Definition: Timing.h:54
System::Month
@ Month
Definition: Timing.h:54
System::currentTime< Year >
template long long currentTime< Year >()
bug_34121.t
t
Definition: bug_34121.py:31
System::ProcessTime::i_user
TimeValueType i_user
Definition: Timing.h:191
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:169
System::adjustTime< milliSec >
long long adjustTime< milliSec >(long long t)
Definition: Timing.h:225
System::ProcessTime::elapsedTime
TimeValueType elapsedTime() const
Retrieve the elapsed time in the requested unit.
Definition: Timing.h:167
System::adjustTime< Day >
long long adjustTime< Day >(long long t)
Definition: Timing.h:209
System::adjustTime< Year >
long long adjustTime< Year >(long long t)
Definition: Timing.h:205
System::adjustTime< Native >
long long adjustTime< Native >(long long t)
Definition: Timing.h:241
System::ProcessTime::i_elapsed
TimeValueType i_elapsed
Definition: Timing.h:191
System::currentTime< milliSec >
template long long currentTime< milliSec >()
System::ProcessTime
Simple class to hold the time information of a process.
Definition: Timing.h:143
System::tickCount
GAUDI_API long long tickCount()
Retrieve the number of ticks since system startup.
Definition: Timing.cpp:87
System::currentTime
GAUDI_API long long currentTime()
Retrieve absolute system time.
Definition: Timing.h:247
System::adjustTime< nanoSec >
long long adjustTime< nanoSec >(long long t)
Definition: Timing.h:233
System::ProcessTime::i_kernel
TimeValueType i_kernel
Internal storage.
Definition: Timing.h:191
System::ProcessTime::operator-
ProcessTime operator-(const ProcessTime &rhs) const
Return the delta between two ProcessTime objects.
Definition: Timing.h:178
System::adjustTime< microSec >
long long adjustTime< microSec >(long long t)
Definition: Timing.h:229
System::upTime
GAUDI_API long long upTime(TimeType typ=Hour)
Maximum processing time left for this process.
Definition: Timing.cpp:139
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:197
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:189
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:217
System::adjustTime< Hour >
long long adjustTime< Hour >(long long t)
Definition: Timing.h:213
System::Times
@ Times
Definition: SystemBase.h:15
System::Sec
@ Sec
Definition: Timing.h:54
System::Quota
@ Quota
Definition: SystemBase.h:15
System::currentTime< Sec >
template long long currentTime< Sec >()
System::Day
@ Day
Definition: Timing.h:54
System::adjustTime< Sec >
long long adjustTime< Sec >(long long t)
Definition: Timing.h:221
System::currentTime< Native >
template long long currentTime< Native >()
System::Native
@ Native
Definition: Timing.h:54
System::currentTime< Day >
template long long currentTime< Day >()
SystemBase.h
System::Min
@ Min
Definition: Timing.h:54
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:47
Kernel.h
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:15
System::ProcessTime::TimeValueType
long long TimeValueType
Definition: Timing.h:145
System::ProcessTime::userTime
TimeValueType userTime() const
Retrieve the user time in the requested unit.
Definition: Timing.h:161
System::ProcessTime::cpuTime
TimeValueType cpuTime() const
Retrieve the CPU (user+kernel) time in the requested unit.
Definition: Timing.h:173
System::getProcessTime
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:207
System::systemStart
GAUDI_API long long systemStart(TimeType typ=Sec)
Maximum processing time left for this process.
Definition: Timing.cpp:128
System::ProcessTime::operator+=
ProcessTime & operator+=(const ProcessTime &rhs)
Add the timings to the current objects.
Definition: Timing.h:182
System::currentTime< nanoSec >
template long long currentTime< nanoSec >()
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:83
System::creationTime
GAUDI_API long long creationTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Process Creation time.
Definition: Timing.cpp:145