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 <limits.h>
00021 #ifdef _WIN32
00022 #include <windows.h>
00023 #else
00024 #include <sys/times.h>
00025 #include <sys/timeb.h>
00026 #endif
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifdef _WIN32
00042 static const longlong UNIX_BASE_TIME = 116444736000000000;
00043 #else
00044 static const longlong UNIX_BASE_TIME = 0;
00045 #endif
00046
00047
00048
00049
00050 longlong System::adjustTime(TimeType typ, longlong t) {
00051 if ( t != -1 ) {
00052 #ifndef _WIN32
00053
00054
00058
00060
00061 switch( typ ) {
00062 case Year: t /= 365;
00063 case Day: t /= 24;
00064 case Hour: t /= 60;
00065 case Min: t /= 60;
00066 case Sec: t /= 1000;
00067 case milliSec: t /= 1000;
00068 case microSec: t /= 10; break;
00069 case nanoSec: t *= 100; break;
00070 case Month: t /= (12*24*3600);
00071 t /= 10000000; break;
00072 default: break;
00073 }
00074 }
00075 return t;
00076 }
00077
00079 longlong System::tickCount() {
00080 longlong count = 10000;
00081 #ifdef _WIN32
00082 count *= ::GetTickCount();
00083 #else
00084 count *= 10*times(0);
00085 #endif
00086 return count;
00087 }
00088
00089 #include <iostream>
00090
00092 longlong System::currentTime(TimeType typ) {
00093 longlong current = 0;
00094 #ifdef _WIN32
00095 ::GetSystemTimeAsFileTime((FILETIME*)¤t);
00096 current -= UNIX_BASE_TIME;
00097 #else
00098 timeb tb;
00099 ftime(&tb);
00100 current = tb.time;
00101 current *= 1000;
00102 current += tb.millitm;
00103 current *= 10000;
00104 #endif
00105 return adjustTime(typ, current);
00106 }
00107
00109 longlong System::systemStart(TimeType typ) {
00110 static longlong sys_start = 0;
00111 if ( 0 == sys_start ) {
00112 longlong c = currentTime(microSec);
00113 longlong t = tickCount();
00114 sys_start = 10*c - t;
00115 }
00116 return adjustTime(typ, sys_start);
00117 }
00118
00120 longlong System::upTime(TimeType typ) {
00121 static longlong sys_start = 10*systemStart(microSec);
00122 return adjustTime(typ, 10*currentTime(microSec)-sys_start);
00123 }
00124
00126 longlong System::creationTime(TimeType typ, InfoType fetch, long pid) {
00127 longlong created = 0;
00128 KERNEL_USER_TIMES info;
00129 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00130 created = adjustTime(typ, info.CreateTime-UNIX_BASE_TIME);
00131 }
00132 return created;
00133 }
00134
00136 longlong System::remainingTime(TimeType typ, InfoType fetch, long pid) {
00137 longlong left = 0;
00138 QUOTA_LIMITS quota;
00139 if ( fetch != NoFetch && getProcess()->query(pid, fetch, "a) ) {
00140 if ( left == -1 ) {
00141
00142 }
00143 else {
00144 left = adjustTime(typ, quota.TimeLimit);
00145 }
00146 }
00147 return left;
00148 }
00149
00151 longlong System::ellapsedTime(TimeType typ, InfoType fetch, long pid) {
00152 longlong ellapsed = currentTime(microSec)*10;
00153 KERNEL_USER_TIMES info;
00154 getProcess()->query(pid, fetch, &info);
00155 ellapsed = adjustTime(typ, ellapsed+UNIX_BASE_TIME-info.CreateTime);
00156 return ellapsed;
00157 }
00158
00160 longlong System::kernelTime(TimeType typ, InfoType fetch, long pid) {
00161 longlong kerneltime = 0;
00162 KERNEL_USER_TIMES info;
00163 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00164 kerneltime = adjustTime(typ, info.KernelTime );
00165 }
00166 return kerneltime;
00167 }
00168
00170 longlong System::userTime(TimeType typ, InfoType fetch, long pid) {
00171 longlong usertime = 0;
00172 KERNEL_USER_TIMES info;
00173 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00174 usertime = adjustTime(typ, info.UserTime );
00175 }
00176 return usertime;
00177 }
00178
00180 longlong System::cpuTime(TimeType typ, InfoType fetch, long pid) {
00181 longlong cputime = 0;
00182 KERNEL_USER_TIMES info;
00183 if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) ) {
00184 cputime = adjustTime(typ, info.KernelTime+info.UserTime );
00185 }
00186 return cputime;
00187 }
00188