| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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)
38 longlong System::adjustTime(TimeType typ, longlong t) {
39 if ( t != -1 ) {
40 #ifndef _WIN32
41
42 // 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
66 longlong System::tickCount() {
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
80 longlong System::currentTime(TimeType typ) {
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
98 longlong System::systemStart(TimeType typ) {
99 static longlong sys_start = 0;
100 if ( 0 == sys_start ) {
101 longlong c = currentTime(microSec);
102 longlong t = tickCount();
103 sys_start = 10*c - t;
104 }
105 return adjustTime(typ, sys_start);
106 }
107
109 longlong System::upTime(TimeType typ) {
110 static longlong sys_start = 10*systemStart(microSec);
111 return adjustTime(typ, 10*currentTime(microSec)-sys_start);
112 }
113
115 longlong System::creationTime(TimeType typ, InfoType fetch, long pid) {
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
125 longlong System::remainingTime(TimeType typ, InfoType fetch, long pid) {
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
140 longlong System::ellapsedTime(TimeType typ, InfoType fetch, long pid) {
141 KERNEL_USER_TIMES info;
142 longlong ellapsed = currentTime(microSec)*10;
143 getProcess()->query(pid, fetch, &info);
144 ellapsed = adjustTime(typ, ellapsed+UNIX_BASE_TIME-info.CreateTime);
145 return ellapsed;
146 }
147
149 longlong System::kernelTime(TimeType typ, InfoType fetch, long pid) {
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
159 longlong System::userTime(TimeType typ, InfoType fetch, long pid) {
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 {
179 ProcessTime getProcessTime(long pid) {
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 }