The Gaudi Framework  master (37c0b60a)
Timing.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 #ifdef _WIN32
31 # include <windows.h>
32 #else
33 # include <sys/time.h>
34 # include <sys/times.h>
35 # include <unistd.h>
36 #endif
37 
38 #ifdef _WIN32
39 static const long long UNIX_BASE_TIME = 116444736000000000;
40 #else
41 static const long long UNIX_BASE_TIME = 0;
42 #endif
43 
44 // convert time from internal representation to the appropriate type
45 // Internal representation for WIN32: 100 nanosecond intervals
46 // Unix: 1 clock tick (usually 10 milliseconds)
47 long long System::adjustTime( TimeType typ, long long t ) {
48  if ( t != -1 ) {
49 #ifndef _WIN32
50 // t /= CLK_TCK ; // needs division by clock tick unit
55 
57 #endif
58  switch ( typ ) {
59  case Year:
60  return adjustTime<Year>( t );
61  case Month:
62  return adjustTime<Month>( t );
63  case Day:
64  return adjustTime<Day>( t );
65  case Hour:
66  return adjustTime<Hour>( t );
67  case Min:
68  return adjustTime<Min>( t );
69  case Sec:
70  return adjustTime<Sec>( t );
71  case milliSec:
72  return adjustTime<milliSec>( t );
73  case microSec:
74  return adjustTime<microSec>( t );
75  case nanoSec:
76  return adjustTime<nanoSec>( t );
77  case Native:
78  return adjustTime<Native>( t );
79  default:
80  return t;
81  }
82  }
83  return t;
84 }
85 
87 long long System::tickCount() {
88  long long count = 10000;
89 #ifdef _WIN32
90  count *= ::GetTickCount(); // Number of milliSec since system startup
91 #else
92  struct tms buf;
93  count *= 10 * times( &buf );
94 #endif
95  return count;
96 }
97 
98 #include <iostream>
99 
101 long long System::currentTime( TimeType typ ) {
102  switch ( typ ) {
103  case Year:
104  return currentTime<Year>();
105  case Month:
106  return currentTime<Month>();
107  case Day:
108  return currentTime<Day>();
109  case Hour:
110  return currentTime<Hour>();
111  case Min:
112  return currentTime<Min>();
113  case Sec:
114  return currentTime<Sec>();
115  case milliSec:
116  return currentTime<milliSec>();
117  case microSec:
118  return currentTime<microSec>();
119  case nanoSec:
120  return currentTime<nanoSec>();
121  case Native:
122  return currentTime<Native>();
123  }
124  return currentTime<Native>();
125 }
126 
128 long long System::systemStart( TimeType typ ) {
129  static long long sys_start = 0;
130  if ( 0 == sys_start ) {
131  long long c = currentTime( microSec );
132  long long t = tickCount();
133  sys_start = 10 * c - t;
134  }
135  return adjustTime( typ, sys_start );
136 }
137 
139 long long System::upTime( TimeType typ ) {
140  static long long sys_start = 10 * systemStart( microSec );
141  return adjustTime( typ, 10 * currentTime( microSec ) - sys_start );
142 }
143 
145 long long System::creationTime( TimeType typ, InfoType fetch, long pid ) {
146  long long created = 0;
147  KERNEL_USER_TIMES info;
148  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
149  created = adjustTime( typ, info.CreateTime - UNIX_BASE_TIME );
150  }
151  return created;
152 }
153 
155 long long System::remainingTime( TimeType typ, InfoType fetch, long pid ) {
156  long long left = 0;
157  QUOTA_LIMITS quota;
158  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
159  if ( left == -1 ) {
160  // left = _I64_MAX;
161  } else {
162  left = adjustTime( typ, quota.TimeLimit );
163  }
164  }
165  return left;
166 }
167 
169 long long System::ellapsedTime( TimeType typ, InfoType fetch, long pid ) {
170  KERNEL_USER_TIMES info;
171  long long ellapsed = currentTime( microSec ) * 10;
172  if ( getProcess()->query( pid, fetch, &info ) ) {
173  ellapsed = adjustTime( typ, ellapsed + UNIX_BASE_TIME - info.CreateTime );
174  }
175  return ellapsed;
176 }
177 
179 long long System::kernelTime( TimeType typ, InfoType fetch, long pid ) {
180  KERNEL_USER_TIMES info;
181  long long kerneltime = 0;
182  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
183  kerneltime = adjustTime( typ, info.KernelTime );
184  }
185  return kerneltime;
186 }
187 
189 long long System::userTime( TimeType typ, InfoType fetch, long pid ) {
190  long long usertime = 0;
191  KERNEL_USER_TIMES info;
192  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) { usertime = adjustTime( typ, info.UserTime ); }
193  return usertime;
194 }
195 
197 long long System::cpuTime( TimeType typ, InfoType fetch, long pid ) {
198  long long cputime = 0;
199  KERNEL_USER_TIMES info;
200  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
201  cputime = adjustTime( typ, info.KernelTime + info.UserTime );
202  }
203  return cputime;
204 }
205 
206 namespace System {
208  KERNEL_USER_TIMES info;
209  if ( getProcess()->query( pid, Times, &info ) ) {
210  return ProcessTime( info.KernelTime, info.UserTime, currentTime<Native>() - info.CreateTime );
211  }
212  return ProcessTime(); // return 0s in case of problems
213  }
214 } // namespace System
System::milliSec
@ milliSec
Definition: Timing.h:67
System::KERNEL_USER_TIMES
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Definition: ProcessDescriptor.h:102
System::currentTime< Month >
template long long currentTime< Month >()
System::Hour
@ Hour
Definition: Timing.h:67
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:28
System::QUOTA_LIMITS::TimeLimit
long long TimeLimit
Definition: ProcessDescriptor.h:48
System::adjustTime< Month >
long long adjustTime< Month >(long long t)
Definition: Timing.h:250
System::Year
@ Year
Definition: Timing.h:67
System::KERNEL_USER_TIMES::UserTime
long long UserTime
Definition: ProcessDescriptor.h:106
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:67
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:67
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:67
System::Month
@ Month
Definition: Timing.h:67
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:169
System::adjustTime< milliSec >
long long adjustTime< milliSec >(long long t)
Definition: Timing.h:238
System::adjustTime< Day >
long long adjustTime< Day >(long long t)
Definition: Timing.h:222
System::adjustTime< Year >
long long adjustTime< Year >(long long t)
Definition: Timing.h:218
System::NoFetch
@ NoFetch
Definition: SystemBase.h:28
System::adjustTime< Native >
long long adjustTime< Native >(long long t)
Definition: Timing.h:254
System::QUOTA_LIMITS
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
Definition: ProcessDescriptor.h:42
System::currentTime< milliSec >
template long long currentTime< milliSec >()
System::ProcessTime
Simple class to hold the time information of a process.
Definition: Timing.h:156
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:260
System::adjustTime< nanoSec >
long long adjustTime< nanoSec >(long long t)
Definition: Timing.h:246
System::adjustTime< microSec >
long long adjustTime< microSec >(long long t)
Definition: Timing.h:242
System::KERNEL_USER_TIMES::KernelTime
long long KernelTime
Definition: ProcessDescriptor.h:105
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
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: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:230
System::KERNEL_USER_TIMES::CreateTime
long long CreateTime
Definition: ProcessDescriptor.h:103
System::adjustTime< Hour >
long long adjustTime< Hour >(long long t)
Definition: Timing.h:226
System::Times
@ Times
Definition: SystemBase.h:28
System::Sec
@ Sec
Definition: Timing.h:67
System::currentTime< Sec >
template long long currentTime< Sec >()
System::Day
@ Day
Definition: Timing.h:67
System::adjustTime< Sec >
long long adjustTime< Sec >(long long t)
Definition: Timing.h:234
System::currentTime< Native >
template long long currentTime< Native >()
System::Native
@ Native
Definition: Timing.h:67
System::currentTime< Day >
template long long currentTime< Day >()
System::Min
@ Min
Definition: Timing.h:67
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
System::getProcess
ProcessDescriptor * getProcess()
Retrieve Process structure.
Definition: ProcessDescriptor.h:142
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:29
ProcessDescriptor.h
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::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:145