Gaudi Framework, version v20r4

Generated: 8 Jan 2009

Timing.cpp

Go to the documentation of this file.
00001 // $Id: Timing.cpp,v 1.3 2002/11/12 18:34:30 mato 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 <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 // convert time from internal representation to the appropriate type
00036 // Internal representation for WIN32: 100 nanosecond intervals
00037 //                             Unix:    1 clock tick (usually 10 mille seconds)
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(); // Number of milliSec since system startup
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*)&current);
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, &quota) )   {
00130     if ( left == -1 )     {
00131       //left = _I64_MAX;
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 }

Generated at Thu Jan 8 17:44:22 2009 for Gaudi Framework, version v20r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004