Gaudi Framework, version v23r0

Home   Generated: Mon Jan 30 2012

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 milliseconds)
00038 longlong System::adjustTime(TimeType typ, longlong t)   {
00039   if ( t != -1 )   {
00040 #ifndef _WIN32
00041 
00042     //  t /= CLK_TCK ;     // needs division by clock tick unit
00046 
00048 #endif
00049     switch( typ )   {
00050     case Year    :  return adjustTime<Year    >(t);
00051     case Month   :  return adjustTime<Month   >(t);
00052     case Day     :  return adjustTime<Day     >(t);
00053     case Hour    :  return adjustTime<Hour    >(t);
00054     case Min     :  return adjustTime<Min     >(t);
00055     case Sec     :  return adjustTime<Sec     >(t);
00056     case milliSec:  return adjustTime<milliSec>(t);
00057     case microSec:  return adjustTime<microSec>(t);
00058     case nanoSec :  return adjustTime<nanoSec >(t);
00059     case Native  :  return adjustTime<Native  >(t);
00060     }
00061   }
00062   return t;
00063 }
00064 
00066 longlong System::tickCount()    {
00067   longlong count = 10000;
00068 #ifdef _WIN32
00069   count *= ::GetTickCount(); // Number of milliSec since system startup
00070 #else
00071   struct tms buf;
00072   count *= 10*times(&buf);
00073 #endif
00074   return count;
00075 }
00076 
00077 #include <iostream>
00078 
00080 longlong System::currentTime(TimeType typ)    {
00081   longlong current = 0;
00082 #ifdef _WIN32
00083   ::GetSystemTimeAsFileTime((FILETIME*)&current);
00084   current -= UNIX_BASE_TIME;
00085 #else
00086   struct timeval tv;
00087   struct timezone tz;
00088   ::gettimeofday(&tv, &tz);
00089   current  = tv.tv_sec;
00090   current *= 1000000;
00091   current += tv.tv_usec;
00092   current *= 10;
00093 #endif
00094   return adjustTime(typ, current);
00095 }
00096 
00098 longlong System::systemStart(TimeType typ)    {
00099   static longlong sys_start = 0;
00100   if ( 0 == sys_start )   {
00101     longlong c = currentTime(microSec);
00102     longlong t = tickCount();
00103     sys_start = 10*c - t;
00104   }
00105   return adjustTime(typ, sys_start);
00106 }
00107 
00109 longlong System::upTime(TimeType typ)    {
00110   static longlong sys_start = 10*systemStart(microSec);
00111   return adjustTime(typ, 10*currentTime(microSec)-sys_start);
00112 }
00113 
00115 longlong System::creationTime(TimeType typ, InfoType fetch, long pid)   {
00116   longlong created = 0;
00117   KERNEL_USER_TIMES info;
00118   if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) )   {
00119     created = adjustTime(typ, info.CreateTime-UNIX_BASE_TIME);
00120   }
00121   return created;
00122 }
00123 
00125 longlong System::remainingTime(TimeType typ, InfoType fetch, long pid)   {
00126   longlong left = 0;
00127   QUOTA_LIMITS quota;
00128   if ( fetch != NoFetch && getProcess()->query(pid, fetch, &quota) )   {
00129     if ( left == -1 )     {
00130       //left = _I64_MAX;
00131     }
00132     else  {
00133       left = adjustTime(typ, quota.TimeLimit);
00134     }
00135   }
00136   return left;
00137 }
00138 
00140 longlong System::ellapsedTime(TimeType typ, InfoType fetch, long pid) {
00141   KERNEL_USER_TIMES info;
00142   longlong ellapsed = currentTime(microSec)*10;
00143   getProcess()->query(pid, fetch, &info);
00144   ellapsed = adjustTime(typ, ellapsed+UNIX_BASE_TIME-info.CreateTime);
00145   return ellapsed;
00146 }
00147 
00149 longlong System::kernelTime(TimeType typ, InfoType fetch, long pid) {
00150   KERNEL_USER_TIMES info;
00151   longlong kerneltime = 0;
00152   if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) )   {
00153     kerneltime = adjustTime(typ, info.KernelTime );
00154   }
00155   return kerneltime;
00156 }
00157 
00159 longlong System::userTime(TimeType typ, InfoType fetch, long pid) {
00160   longlong usertime = 0;
00161   KERNEL_USER_TIMES info;
00162   if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) )   {
00163     usertime = adjustTime(typ, info.UserTime );
00164   }
00165   return usertime;
00166 }
00167 
00169 longlong System::cpuTime(TimeType typ, InfoType fetch, long pid) {
00170   longlong cputime = 0;
00171   KERNEL_USER_TIMES info;
00172   if ( fetch != NoFetch && getProcess()->query(pid, fetch, &info) )   {
00173     cputime = adjustTime(typ, info.KernelTime+info.UserTime );
00174   }
00175   return cputime;
00176 }
00177 
00178 namespace System {
00179   ProcessTime getProcessTime(long pid) {
00180     KERNEL_USER_TIMES info;
00181     if (getProcess()->query(pid, Times, &info)) {
00182       return ProcessTime(info.KernelTime,
00183                          info.UserTime,
00184                          currentTime(Native) - info.CreateTime);
00185     }
00186     return ProcessTime(); // return 0s in case of problems
00187   }
00188 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Jan 30 2012 13:53:00 for Gaudi Framework, version v23r0 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004