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 // Timing.cpp
3 //--------------------------------------------------------------------
4 //
5 // Package : System (The LHCb System service)
6 //
7 // Description: Implementation of Systems internals
8 //
9 // Author : M.Frank
10 // Created : 13/1/99
11 // Changes :
12 //====================================================================
13 #define GAUDIKERNEL_TIMING_CPP
14 
15 #include "GaudiKernel/Timing.h"
16 #include "ProcessDescriptor.h"
17 
18 #include <ctime>
19 #include <climits>
20 #ifdef _WIN32
21 #include <windows.h>
22 #else
23 #include <sys/time.h>
24 #include <sys/times.h>
25 #include <unistd.h>
26 #endif
27 
28 #ifdef _WIN32
29 static const longlong UNIX_BASE_TIME = 116444736000000000;
30 #else
31 static const longlong UNIX_BASE_TIME = 0;
32 #endif
33 
34 // convert time from internal representation to the appropriate type
35 // Internal representation for WIN32: 100 nanosecond intervals
36 // Unix: 1 clock tick (usually 10 milliseconds)
38  if ( t != -1 ) {
39 #ifndef _WIN32
40  // t /= CLK_TCK ; // needs division by clock tick unit
45 
47 #endif
48  switch( typ ) {
49  case Year : return adjustTime<Year >(t);
50  case Month : return adjustTime<Month >(t);
51  case Day : return adjustTime<Day >(t);
52  case Hour : return adjustTime<Hour >(t);
53  case Min : return adjustTime<Min >(t);
54  case Sec : return adjustTime<Sec >(t);
55  case milliSec: return adjustTime<milliSec>(t);
56  case microSec: return adjustTime<microSec>(t);
57  case nanoSec : return adjustTime<nanoSec >(t);
58  case Native : return adjustTime<Native >(t);
59  }
60  }
61  return t;
62 }
63 
66  longlong count = 10000;
67 #ifdef _WIN32
68  count *= ::GetTickCount(); // Number of milliSec since system startup
69 #else
70  struct tms buf;
71  count *= 10*times(&buf);
72 #endif
73  return count;
74 }
75 
76 #include <iostream>
77 
80  switch( typ ) {
81  case Year : return currentTime<Year >();
82  case Month : return currentTime<Month >();
83  case Day : return currentTime<Day >();
84  case Hour : return currentTime<Hour >();
85  case Min : return currentTime<Min >();
86  case Sec : return currentTime<Sec >();
87  case milliSec: return currentTime<milliSec>();
88  case microSec: return currentTime<microSec>();
89  case nanoSec : return currentTime<nanoSec >();
90  case Native : return currentTime<Native >();
91  }
92  return currentTime<Native>();
93 }
94 
97  static longlong sys_start = 0;
98  if ( 0 == sys_start ) {
100  longlong t = tickCount();
101  sys_start = 10*c - t;
102  }
103  return adjustTime(typ, sys_start);
104 }
105 
108  static longlong sys_start = 10*systemStart(microSec);
109  return adjustTime(typ, 10*currentTime(microSec)-sys_start);
110 }
111 
114  longlong created = 0;
115  KERNEL_USER_TIMES info;
116  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
117  created = adjustTime(typ, info.CreateTime-UNIX_BASE_TIME);
118  }
119  return created;
120 }
121 
124  longlong left = 0;
125  QUOTA_LIMITS quota;
126  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &quota) ) {
127  if ( left == -1 ) {
128  //left = _I64_MAX;
129  }
130  else {
131  left = adjustTime(typ, quota.TimeLimit);
132  }
133  }
134  return left;
135 }
136 
139  KERNEL_USER_TIMES info;
140  longlong ellapsed = currentTime(microSec)*10;
141  getProcess()->query(pid, fetch, &info);
142  ellapsed = adjustTime(typ, ellapsed+UNIX_BASE_TIME-info.CreateTime);
143  return ellapsed;
144 }
145 
148  KERNEL_USER_TIMES info;
149  longlong kerneltime = 0;
150  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
151  kerneltime = adjustTime(typ, info.KernelTime );
152  }
153  return kerneltime;
154 }
155 
158  longlong usertime = 0;
159  KERNEL_USER_TIMES info;
160  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
161  usertime = adjustTime(typ, info.UserTime );
162  }
163  return usertime;
164 }
165 
167 longlong System::cpuTime(TimeType typ, InfoType fetch, long pid) {
168  longlong cputime = 0;
169  KERNEL_USER_TIMES info;
170  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
171  cputime = adjustTime(typ, info.KernelTime+info.UserTime );
172  }
173  return cputime;
174 }
175 
176 namespace System {
178  KERNEL_USER_TIMES info;
179  if (getProcess()->query(pid, Times, &info)) {
180  return ProcessTime(info.KernelTime,
181  info.UserTime,
183  }
184  return ProcessTime(); // return 0s in case of problems
185  }
186 }
187 
188 
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
GAUDI_API longlong userTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU user mode time of process in milliseconds.
Definition: Timing.cpp:157
template longlong currentTime< milliSec >()
template longlong currentTime< microSec >()
GAUDI_API longlong systemStart(TimeType typ=Sec)
Maximum processing time left for this process.
Definition: Timing.cpp:96
Note: OS specific details for environment resolution.
Definition: Debugger.h:19
GAUDI_API longlong cpuTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Consumed CPU time of process in milliseconds.
Definition: Timing.cpp:167
long long adjustTime< Native >(long long t)
Definition: Timing.h:239
GAUDI_API longlong ellapsedTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Elapsed time since start of process in milliseconds.
Definition: Timing.cpp:138
GAUDI_API longlong upTime(TimeType typ=Hour)
Maximum processing time left for this process.
Definition: Timing.cpp:107
long long adjustTime< nanoSec >(long long t)
Definition: Timing.h:231
GAUDI_API longlong tickCount()
Retrieve the number of ticks since system startup.
Definition: Timing.cpp:65
GAUDI_API longlong creationTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Process Creation time.
Definition: Timing.cpp:113
TimeType
Time type for conversion.
Definition: Timing.h:57
GAUDI_API longlong remainingTime(TimeType typ=milliSec, InfoType fetch=Quota, long pid=-1)
Maximum processing time left for this process.
Definition: Timing.cpp:123
template longlong currentTime< Native >()
template longlong currentTime< nanoSec >()
GAUDI_API longlong kernelTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU kernel mode time of process in milliseconds.
Definition: Timing.cpp:147
long long adjustTime< milliSec >(long long t)
Definition: Timing.h:223
Simple class to hold the time information of a process.
Definition: Timing.h:146
long long adjustTime< microSec >(long long t)
Definition: Timing.h:227
GAUDI_API longlong currentTime()
Retrieve absolute system time.
Definition: Timing.h:245
GAUDI_API longlong adjustTime(TimeType typ, longlong timevalue)
Convert time from OS native time to requested representation (Experts only)
Definition: Timing.cpp:37
ProcessDescriptor * getProcess()
Retrieve Process structure.
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
long query(long pid, InfoType info, PROCESS_BASIC_INFORMATION *buffer)
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition: Timing.cpp:177
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:18