Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:44

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 //====================================================================
0012 //  Timing.h
0013 //--------------------------------------------------------------------
0014 //
0015 //  Package    : Gaudi/System (The LHCb System service)
0016 //
0017 //  Description: Definition of Systems internals
0018 //
0019 //  Author     : M.Frank
0020 //      Created    : 13/1/99
0021 //====================================================================
0022 #ifndef GAUDIKERNEL_TIMING_H
0023 #define GAUDIKERNEL_TIMING_H
0024 
0025 // Framework include files
0026 #include "GaudiKernel/Kernel.h"
0027 #include "GaudiKernel/SystemBase.h"
0028 
0029 #ifdef _WIN32
0030 #  include <windows.h>
0031 #else
0032 #  include <sys/time.h>
0033 #endif
0034 
0035 /** Note: OS specific details for process timing
0036 
0037     Entrypoints:
0038       - remainingTime returns the time the process could still execute
0039       - ellapsedTime: returns elapsed time since program start
0040       - kernelTime:   returns the amount of time the process has spent in kernel mode
0041       - userTime:     returns the amount of time the process has spent in user mode
0042       - cpuTime:      returns kernel+user time
0043 
0044 
0045       On Windows NT Time is expressed as
0046       the amount of time that has elapsed since midnight on
0047       January 1, 1601 at Greenwich, England.
0048 
0049       On Unix time is expressed as
0050       the amount of time that has elapsed since midnight on
0051       January 1, 1970 at Greenwich, England.
0052 
0053     <P> History    :
0054 
0055     <PRE>
0056     +---------+----------------------------------------------+--------+
0057     |    Date |                 Comment                      | Who    |
0058     +---------+----------------------------------------------+--------+
0059     | 11/11/00| Initial version.                             | MF     |
0060     +---------+----------------------------------------------+--------+
0061     </PRE>
0062     @author:  M.Frank
0063     @version: 1.0
0064 */
0065 namespace System {
0066   /// Time type for conversion
0067   enum TimeType { Year, Month, Day, Hour, Min, Sec, milliSec, microSec, nanoSec, Native };
0068 
0069   /// Convert time from OS native time to requested representation (Experts only)
0070   GAUDI_API long long adjustTime( TimeType typ, long long timevalue );
0071 
0072   /// Convert the time from OS native time to requested representation (Experts only)
0073   template <TimeType T>
0074   inline long long adjustTime( long long timevalue );
0075 
0076   /** Elapsed time since start of process in milliseconds.
0077         @param typ       Indicator or the unit the time will be returned.
0078         @param timevalue Time value to be converted.
0079         @return          Requested value in the indicated units.
0080     */
0081   GAUDI_API long long ellapsedTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
0082   /** CPU kernel mode time of process in milliseconds.
0083       @param typ     Indicator or the unit the time will be returned.
0084       @param fetch   Indicator of the information to be fetched.
0085                      If Fetch_None, the information will not be updated.
0086       @param pid     Process ID of which the information will be returned
0087       @return        Requested value in the indicated units.
0088   */
0089   GAUDI_API long long kernelTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
0090   /** CPU user mode time of process in milliseconds.
0091       @param typ     Indicator or the unit the time will be returned.
0092       @param fetch   Indicator of the information to be fetched.
0093                      If Fetch_None, the information will not be updated.
0094       @param pid     Process ID of which the information will be returned
0095       @return        Requested value in the indicated units.
0096   */
0097   GAUDI_API long long userTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
0098   /** Consumed CPU time of process in milliseconds.
0099       @param typ     Indicator or the unit the time will be returned.
0100       @param fetch   Indicator of the information to be fetched.
0101                      If Fetch_None, the information will not be updated.
0102       @param pid     Process ID of which the information will be returned
0103       @return        Requested value in the indicated units.
0104   */
0105   GAUDI_API long long cpuTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
0106   /** Maximum processing time left for this process.
0107       @param typ     Indicator or the unit the time will be returned.
0108       @param fetch   Indicator of the information to be fetched.
0109                      If Fetch_None, the information will not be updated.
0110       @param pid     Process ID of which the information will be returned
0111       @return        Requested value in the indicated units.
0112   */
0113   GAUDI_API long long remainingTime( TimeType typ = milliSec, InfoType fetch = Quota, long pid = -1 );
0114   /** Process Creation time.
0115       @param typ     Indicator or the unit the time will be returned.
0116       @param fetch   Indicator of the information to be fetched.
0117                      If Fetch_None, the information will not be updated.
0118       @param pid     Process ID of which the information will be returned
0119       @return        Requested value in the indicated units.
0120   */
0121   GAUDI_API long long creationTime( TimeType typ = milliSec, InfoType fetch = Times, long pid = -1 );
0122   /** Maximum processing time left for this process.
0123       @param typ     Indicator or the unit the time will be returned.
0124       @return        Requested value in the indicated units.
0125   */
0126   GAUDI_API long long systemStart( TimeType typ = Sec );
0127   /** Maximum processing time left for this process.
0128       @param typ     Indicator or the unit the time will be returned.
0129       @return        Requested value in the indicated units.
0130   */
0131   GAUDI_API long long upTime( TimeType typ = Hour );
0132   /** Retrieve absolute system time
0133       @param typ     Indicator or the unit the time will be returned.
0134       @return        Requested value in the indicated units.
0135   */
0136 
0137   /// Get current time in specificed units via template parameter (inlined)
0138   template <TimeType T>
0139   GAUDI_API long long currentTime();
0140 
0141   /// Get current time in specificed units
0142   GAUDI_API long long currentTime( TimeType typ = milliSec );
0143 
0144   /** Retrieve the number of ticks since system startup
0145       @return        Requested value in the indicated units.
0146   */
0147   GAUDI_API long long tickCount();
0148 
0149   /** Simple class to hold the time information of a process.
0150    *
0151    * Simplify the simultaneous handling of kernel, user and elapsed times of a
0152    * process.
0153    *
0154    * \see {<a href="http://savannah.cern.ch/bugs/?87341">bug #87341</a>}
0155    */
0156   class ProcessTime {
0157   public:
0158     typedef long long TimeValueType;
0159 
0160     /// Constructor
0161     ProcessTime() : i_kernel( 0 ), i_user( 0 ), i_elapsed( 0 ) {}
0162 
0163     /// Constructor
0164     ProcessTime( TimeValueType k, TimeValueType u, TimeValueType e ) : i_kernel( k ), i_user( u ), i_elapsed( e ) {}
0165 
0166     /// Retrieve the kernel time in the requested unit.
0167     template <TimeType T>
0168     inline TimeValueType kernelTime() const {
0169       return adjustTime<T>( i_kernel );
0170     }
0171 
0172     /// Retrieve the user time in the requested unit.
0173     template <TimeType T>
0174     inline TimeValueType userTime() const {
0175       return adjustTime<T>( i_user );
0176     }
0177 
0178     /// Retrieve the elapsed time in the requested unit.
0179     template <TimeType T>
0180     inline TimeValueType elapsedTime() const {
0181       return adjustTime<T>( i_elapsed );
0182     }
0183 
0184     /// Retrieve the CPU (user+kernel) time in the requested unit.
0185     template <TimeType T>
0186     inline TimeValueType cpuTime() const {
0187       return adjustTime<T>( i_user + i_kernel );
0188     }
0189 
0190     /// Return the delta between two \c ProcessTime objects.
0191     inline ProcessTime operator-( const ProcessTime& rhs ) const {
0192       return ProcessTime( i_kernel - rhs.i_kernel, i_user - rhs.i_user, i_elapsed - rhs.i_elapsed );
0193     }
0194     /// Add the timings to the current objects
0195     inline ProcessTime& operator+=( const ProcessTime& rhs ) {
0196       i_kernel += rhs.i_kernel;
0197       i_user += rhs.i_user;
0198       i_elapsed += rhs.i_elapsed;
0199       return *this;
0200     }
0201 
0202   private:
0203     /// Internal storage.
0204     TimeValueType i_kernel, i_user, i_elapsed;
0205   };
0206 
0207   /** Retrieve the process time data for a process.
0208    *
0209    * Get the process time data for a process (by default the current) as a
0210    * \c ProcessTime object.
0211    */
0212   GAUDI_API ProcessTime getProcessTime( long pid = -1 );
0213 } // namespace System
0214 
0215 // implementation of the templated functions
0216 namespace System {
0217   template <>
0218   inline long long adjustTime<Year>( long long t ) {
0219     return ( t == -1 ) ? t : t /= ( 1LL * 365 * 24 * 60 * 60 * 1000 * 1000 * 10 );
0220   }
0221   template <>
0222   inline long long adjustTime<Day>( long long t ) {
0223     return ( t == -1 ) ? t : t /= ( 1LL * 24 * 60 * 60 * 1000 * 1000 * 10 );
0224   }
0225   template <>
0226   inline long long adjustTime<Hour>( long long t ) {
0227     return ( t == -1 ) ? t : t /= ( 1LL * 60 * 60 * 1000 * 1000 * 10 );
0228   }
0229   template <>
0230   inline long long adjustTime<Min>( long long t ) {
0231     return ( t == -1 ) ? t : t /= ( 60 * 1000 * 1000 * 10 );
0232   }
0233   template <>
0234   inline long long adjustTime<Sec>( long long t ) {
0235     return ( t == -1 ) ? t : t /= ( 1000 * 1000 * 10 );
0236   }
0237   template <>
0238   inline long long adjustTime<milliSec>( long long t ) {
0239     return ( t == -1 ) ? t : t /= ( 1000 * 10 );
0240   }
0241   template <>
0242   inline long long adjustTime<microSec>( long long t ) {
0243     return ( t == -1 ) ? t : t /= ( 10LL );
0244   }
0245   template <>
0246   inline long long adjustTime<nanoSec>( long long t ) {
0247     return ( t == -1 ) ? t : t *= 100LL;
0248   }
0249   template <>
0250   inline long long adjustTime<Month>( long long t ) {
0251     return ( t == -1 ) ? t : t /= ( 1LL * 30 * 24 * 60 * 60 * 1000 * 1000 * 10 );
0252   }
0253   template <>
0254   inline long long adjustTime<Native>( long long t ) {
0255     return t;
0256   }
0257 
0258   // This is frequently used and thus we inline it if possible
0259   template <TimeType T>
0260   inline long long currentTime() {
0261 #ifdef _WIN32
0262     long long current = 0;
0263     ::GetSystemTimeAsFileTime( (FILETIME*)&current );
0264     return adjustTime<T>( current - UNIX_BASE_TIME );
0265 #else
0266     struct timeval tv;
0267     ::gettimeofday( &tv, 0 );
0268     return adjustTime<T>( ( tv.tv_sec * 1000000 + tv.tv_usec ) * 10 );
0269 #endif
0270   }
0271 
0272   // Define all template versions here to avoid code bloat
0273   template long long currentTime<Year>();
0274   template long long currentTime<Month>();
0275   template long long currentTime<Day>();
0276   template long long currentTime<Hour>();
0277   template long long currentTime<Min>();
0278   template long long currentTime<Sec>();
0279   template long long currentTime<milliSec>();
0280   template long long currentTime<microSec>();
0281   template long long currentTime<nanoSec>();
0282   template long long currentTime<Native>();
0283 } // namespace System
0284 
0285 #endif // GAUDIKERNEL_TIMING_H