All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Timing.cpp
Go to the documentation of this file.
1 // $Id: Timing.cpp,v 1.3 2002/11/12 18:34:30 mato Exp $
2 //====================================================================
3 // Timing.cpp
4 //--------------------------------------------------------------------
5 //
6 // Package : System (The LHCb System service)
7 //
8 // Description: Implementation of Systems internals
9 //
10 // Author : M.Frank
11 // Created : 13/1/99
12 // Changes :
13 //====================================================================
14 #define GAUDIKERNEL_TIMING_CPP
15 
16 #include "GaudiKernel/Timing.h"
17 #include "ProcessDescriptor.h"
18 
19 #include <ctime>
20 #include <climits>
21 #ifdef _WIN32
22 #include <windows.h>
23 #else
24 #include <sys/time.h>
25 #include <sys/times.h>
26 #include <unistd.h>
27 #endif
28 
29 #ifdef _WIN32
30 static const longlong UNIX_BASE_TIME = 116444736000000000;
31 #else
32 static const longlong UNIX_BASE_TIME = 0;
33 #endif
34 
35 // convert time from internal representation to the appropriate type
36 // Internal representation for WIN32: 100 nanosecond intervals
37 // Unix: 1 clock tick (usually 10 milliseconds)
39  if ( t != -1 ) {
40 #ifndef _WIN32
41  // t /= CLK_TCK ; // needs division by clock tick unit
46 
48 #endif
49  switch( typ ) {
50  case Year : return adjustTime<Year >(t);
51  case Month : return adjustTime<Month >(t);
52  case Day : return adjustTime<Day >(t);
53  case Hour : return adjustTime<Hour >(t);
54  case Min : return adjustTime<Min >(t);
55  case Sec : return adjustTime<Sec >(t);
56  case milliSec: return adjustTime<milliSec>(t);
57  case microSec: return adjustTime<microSec>(t);
58  case nanoSec : return adjustTime<nanoSec >(t);
59  case Native : return adjustTime<Native >(t);
60  }
61  }
62  return t;
63 }
64 
67  longlong count = 10000;
68 #ifdef _WIN32
69  count *= ::GetTickCount(); // Number of milliSec since system startup
70 #else
71  struct tms buf;
72  count *= 10*times(&buf);
73 #endif
74  return count;
75 }
76 
77 #include <iostream>
78 
81  longlong current = 0;
82 #ifdef _WIN32
83  ::GetSystemTimeAsFileTime((FILETIME*)&current);
84  current -= UNIX_BASE_TIME;
85 #else
86  struct timeval tv;
87  struct timezone tz;
88  ::gettimeofday(&tv, &tz);
89  current = tv.tv_sec;
90  current *= 1000000;
91  current += tv.tv_usec;
92  current *= 10;
93 #endif
94  return adjustTime(typ, current);
95 }
96 
99  static longlong sys_start = 0;
100  if ( 0 == sys_start ) {
102  longlong t = tickCount();
103  sys_start = 10*c - t;
104  }
105  return adjustTime(typ, sys_start);
106 }
107 
110  static longlong sys_start = 10*systemStart(microSec);
111  return adjustTime(typ, 10*currentTime(microSec)-sys_start);
112 }
113 
116  longlong created = 0;
117  KERNEL_USER_TIMES info;
118  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
119  created = adjustTime(typ, info.CreateTime-UNIX_BASE_TIME);
120  }
121  return created;
122 }
123 
126  longlong left = 0;
127  QUOTA_LIMITS quota;
128  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &quota) ) {
129  if ( left == -1 ) {
130  //left = _I64_MAX;
131  }
132  else {
133  left = adjustTime(typ, quota.TimeLimit);
134  }
135  }
136  return left;
137 }
138 
141  KERNEL_USER_TIMES info;
143  getProcess()->query(pid, fetch, &info);
144  ellapsed = adjustTime(typ, ellapsed+UNIX_BASE_TIME-info.CreateTime);
145  return ellapsed;
146 }
147 
150  KERNEL_USER_TIMES info;
151  longlong kerneltime = 0;
152  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
153  kerneltime = adjustTime(typ, info.KernelTime );
154  }
155  return kerneltime;
156 }
157 
160  longlong usertime = 0;
161  KERNEL_USER_TIMES info;
162  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
163  usertime = adjustTime(typ, info.UserTime );
164  }
165  return usertime;
166 }
167 
169 longlong System::cpuTime(TimeType typ, InfoType fetch, long pid) {
170  longlong cputime = 0;
171  KERNEL_USER_TIMES info;
172  if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
173  cputime = adjustTime(typ, info.KernelTime+info.UserTime );
174  }
175  return cputime;
176 }
177 
178 namespace System {
180  KERNEL_USER_TIMES info;
181  if (getProcess()->query(pid, Times, &info)) {
182  return ProcessTime(info.KernelTime,
183  info.UserTime,
184  currentTime(Native) - info.CreateTime);
185  }
186  return ProcessTime(); // return 0s in case of problems
187  }
188 }
ProcessDescriptor * getProcess()
Retrieve Process structure.
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:159
GAUDI_API longlong systemStart(TimeType typ=Sec)
Maximum processing time left for this process.
Definition: Timing.cpp:98
tuple c
Definition: gaudirun.py:341
GAUDI_API longlong cpuTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Consumed CPU time of process in milliseconds.
Definition: Timing.cpp:169
long long adjustTime< Native >(long long t)
Definition: Timing.h:219
long long adjustTime< Month >(long long t)
Definition: Timing.h:215
GAUDI_API longlong ellapsedTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Elapsed time since start of process in milliseconds.
Definition: Timing.cpp:140
GAUDI_API longlong upTime(TimeType typ=Hour)
Maximum processing time left for this process.
Definition: Timing.cpp:109
tuple ellapsed
Definition: ana.py:140
long long adjustTime< nanoSec >(long long t)
Definition: Timing.h:211
GAUDI_API longlong tickCount()
Retrieve the number of ticks since system startup.
Definition: Timing.cpp:66
long long adjustTime< Day >(long long t)
Definition: Timing.h:187
GAUDI_API longlong creationTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Process Creation time.
Definition: Timing.cpp:115
TimeType
Time type for conversion.
Definition: Timing.h:51
GAUDI_API longlong remainingTime(TimeType typ=milliSec, InfoType fetch=Quota, long pid=-1)
Maximum processing time left for this process.
Definition: Timing.cpp:125
GAUDI_API longlong kernelTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU kernel mode time of process in milliseconds.
Definition: Timing.cpp:149
long long adjustTime< milliSec >(long long t)
Definition: Timing.h:203
long long adjustTime< Year >(long long t)
Definition: Timing.h:183
Simple class to hold the time information of a process.
Definition: Timing.h:133
long long adjustTime< microSec >(long long t)
Definition: Timing.h:207
long long adjustTime< Min >(long long t)
Definition: Timing.h:195
GAUDI_API longlong adjustTime(TimeType typ, longlong timevalue)
Convert time from OS native time to requested representation (Experts only)
Definition: Timing.cpp:38
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:179
long long adjustTime< Hour >(long long t)
Definition: Timing.h:191
long long adjustTime< Sec >(long long t)
Definition: Timing.h:199
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:18
GAUDI_API longlong currentTime(TimeType typ=milliSec)
Retrieve absolute system time.
Definition: Timing.cpp:80