Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Timing.cpp

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiKernel/src/Lib/Timing.cpp,v 1.1.1.1 2001/04/18 18:14:18 tlindner Exp $
00002 //====================================================================
00003 //      Timing.cpp
00004 //--------------------------------------------------------------------
00005 //
00006 //      Package    : System (The LHCb System service)
00007 //
00008 //  Description: Implementation of Systems internals
00009 //
00010 //      Author     : M.Frank
00011 //  Created    : 13/1/99
00012 //      Changes    : 
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 struct __FILETIME {
00030   long dwHighDateTime;
00031   long dwLowDateTime;
00032 };
00033 
00034 inline static void UnixTimeToFileTime(time_t t, __FILETIME* pft)   {
00035  // Note that LONGLONG is a 64-bit value
00036  longlong ll = Int32x32To64(t, 10000000) + 116444736000000000;
00037  pft->dwLowDateTime = (long)ll;
00038  pft->dwHighDateTime = ll >> 32;
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 // convert time from internal representation to the appropriate type
00048 // Internal representation for WIN32: 100 nanosecond intervals
00049 //                             Unix:    1 clock tick (usually 10 mille seconds)
00050 longlong System::adjustTime(TimeType typ, longlong t)   {
00051   if ( t != -1 )   {
00052 #ifndef _WIN32
00053 
00054     //  t /= CLK_TCK ;     // needs division by clock tick unit
00058 
00060 #endif
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(); // Number of milliSec since system startup
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*)&current);
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, &quota) )   {
00140     if ( left == -1 )     {
00141       //left = _I64_MAX;
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 

Generated at Wed Nov 21 12:22:06 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000