Timing.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #define GAUDIKERNEL_TIMING_CPP
00015
00016 #include "GaudiKernel/Timing.h"
00017 #include "ProcessDescriptor.h"
00018
00019 #include <ctime>
00020 #include <climits>
00021 #ifdef _WIN32
00022 #include <windows.h>
00023 #else
00024 #include <sys/time.h>
00025 #include <sys/times.h>
00026 #include <unistd.h>
00027 #endif
00028
00029 #ifdef _WIN32
00030 static const longlong UNIX_BASE_TIME = 116444736000000000;
00031 #else
00032 static const longlong UNIX_BASE_TIME = 0;
00033 #endif
00034
00035
00036
00037
00038 longlong System::adjustTime(TimeType typ, longlong t) {
00039 if ( t != -1 ) {
00040 #ifndef _WIN32
00042 // t /= CLK_TCK ; // needs division by clock tick unit
00046
00048 #endif
00049 switch( typ ) {
00050 case Year: t /= 365;
00051 case Day: t /= 24;
00052 case Hour: t /= 60;
00053 case Min: t /= 60;
00054 case Sec: t /= 1000;
00055 case milliSec: t /= 1000;
00056 case microSec: t /= 10; break;
00057 case nanoSec: t *= 100; break;
00058 case Month: t /= (12*24*3600);
00059 t /= 10000000; break;
00060 default: break;
00061 }
00062 }
00063 return t;
00064 }
00065
00067 longlong System::tickCount() {
00068 longlong count = 10000;
00069 #ifdef _WIN32
00070 count *= ::GetTickCount();
00071 #else
00072 struct tms buf;
00073 count *= 10*times(&buf);
00074 #endif
00075 return count;
00076 }
00077
00078 #include <iostream>
00079
00081 longlong System::currentTime(TimeType typ) {
00082 longlong current = 0;
00083 #ifdef _WIN32
00084 ::GetSystemTimeAsFileTime((FILETIME*)¤t);
00085 current -= UNIX_BASE_TIME;
00086 #else
00087 struct timeval tv;
00088 struct timezone tz;
00089 ::gettimeofday(&tv, &tz);
00090 current = tv.tv_sec;
00091 current *= 1000000;
00092 current += tv.tv_usec;
00093 current *= 10;
00094 #endif
00095 return adjustTime(typ, current);
00096 }
00097
00099 longlong System::systemStart(TimeType typ) {
00100 static longlong sys_start = 0;
00101 if ( 0 == sys_start ) {
00102 longlong c = currentTime(microSec);
00103 longlong t = tickCount();
00104 sys_start = 10*c - t;
00105 }
00106 return adjustTime(typ, sys_start);
00107 }
00108
00110 longlong System::upTime(TimeType typ) {
00111 static longlong sys_start = 10*systemStart(microSec);
00112 return adjustTime(typ, 10*currentTime(microSec)-sys_start);
00113 }
00114
00116 longlong System::creationTime(TimeType typ, InfoType fetch, long pid) {
00117 longlong created = 0;
00118 KERNEL_USER_TIMES info;
00119 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00120 created = adjustTime(typ, info.CreateTime-UNIX_BASE_TIME);
00121 }
00122 return created;
00123 }
00124
00126 longlong System::remainingTime(TimeType typ, InfoType fetch, long pid) {
00127 longlong left = 0;
00128 QUOTA_LIMITS quota;
00129 if ( fetch != NoFetch && getProcess()->query(pid, fetch, "a) ) {
00130 if ( left == -1 ) {
00131
00132 }
00133 else {
00134 left = adjustTime(typ, quota.TimeLimit);
00135 }
00136 }
00137 return left;
00138 }
00139
00141 longlong System::ellapsedTime(TimeType typ, InfoType fetch, long pid) {
00142 KERNEL_USER_TIMES info;
00143 longlong ellapsed = currentTime(microSec)*10;
00144 getProcess()->query(pid, fetch, &info);
00145 ellapsed = adjustTime(typ, ellapsed+UNIX_BASE_TIME-info.CreateTime);
00146 return ellapsed;
00147 }
00148
00150 longlong System::kernelTime(TimeType typ, InfoType fetch, long pid) {
00151 KERNEL_USER_TIMES info;
00152 longlong kerneltime = 0;
00153 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00154 kerneltime = adjustTime(typ, info.KernelTime );
00155 }
00156 return kerneltime;
00157 }
00158
00160 longlong System::userTime(TimeType typ, InfoType fetch, long pid) {
00161 longlong usertime = 0;
00162 KERNEL_USER_TIMES info;
00163 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00164 usertime = adjustTime(typ, info.UserTime );
00165 }
00166 return usertime;
00167 }
00168
00170 longlong System::cpuTime(TimeType typ, InfoType fetch, long pid) {
00171 longlong cputime = 0;
00172 KERNEL_USER_TIMES info;
00173 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00174 cputime = adjustTime(typ, info.KernelTime+info.UserTime );
00175 }
00176 return cputime;
00177 }