Geant4  10.02
G4Timer.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4Timer.cc 67970 2013-03-13 10:10:06Z gcosmo $
28 //
29 //
30 // ----------------------------------------------------------------------
31 // class G4Timer
32 //
33 // Implementation
34 // 29.04.97 G.Cosmo Added timings for Windows systems
35 
36 #include "G4Timer.hh"
37 #include "G4ios.hh"
38 
39 #undef times
40 
41 // Global error function
42 #include "G4ExceptionSeverity.hh"
43 void G4Exception(const char* originOfException,
44  const char* exceptionCode,
45  G4ExceptionSeverity severity,
46  const char* comments);
47 
48 #if defined(IRIX6_2)
49 # if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE_EXTENDED==1)
50 # define __vfork vfork
51 # endif
52 #endif
53 
54 #ifdef WIN32
55 # include <sys/types.h>
56 # include <windows.h>
57 
58  // extract milliseconds time unit
59  int sysconf(int a){
60  if( a == _SC_CLK_TCK ) return 1000;
61  else return 0;
62  }
63 
64  static clock_t filetime2msec( FILETIME* t ){
65 
66  return (clock_t)((((float)t->dwHighDateTime)*429496.7296)+
67  (((float)t->dwLowDateTime)*.0001) );
68  }
69 
70 
71  clock_t times(struct tms * t){
72  FILETIME ct = {0,0}, et = {0,0}, st = {0,0}, ut = {0,0}, rt = {0,0};
73  SYSTEMTIME realtime;
74 
75  GetSystemTime( &realtime );
76  SystemTimeToFileTime( &realtime, &rt ); // get real time in 10^-9 sec
77  if( t != 0 ){
78  GetProcessTimes( GetCurrentProcess(), &ct, &et, &st, &ut);// get process time in 10^-9 sec
79  t->tms_utime = t->tms_cutime = filetime2msec(&ut);
80  t->tms_stime = t->tms_cstime = filetime2msec(&st);
81  }
82  return filetime2msec(&rt);
83  }
84 #endif /* WIN32 */
85 
86 // Print timer status n std::ostream
87 std::ostream& operator << (std::ostream& os, const G4Timer& t)
88 {
89  if (t.IsValid())
90  {
91  os << "User=" << t.GetUserElapsed()
92  << "s Real=" << t.GetRealElapsed()
93  << "s Sys=" << t.GetSystemElapsed() << "s";
94  }
95  else
96  {
97  os << "User=****s Real=****s Sys=****s";
98  }
99  return os;
100 }
101 
103  : fValidTimes(false)
104 {
105 }
106 
108 {
109  if (!fValidTimes)
110  {
111  G4Exception("G4Timer::GetRealElapsed()", "InvalidCondition",
112  FatalException, "Timer not stopped or times not recorded!");
113  }
115  return diff/sysconf(_SC_CLK_TCK);
116 }
117 
118 
120 {
121  if (!fValidTimes)
122  {
123  G4Exception("G4Timer::GetSystemElapsed()", "InvalidCondition",
124  FatalException, "Timer not stopped or times not recorded!");
125  }
126  G4double diff=fEndTimes.tms_stime-fStartTimes.tms_stime;
127  return diff/sysconf(_SC_CLK_TCK);
128 }
129 
131 {
132  if (!fValidTimes)
133  {
134  G4Exception("G4Timer::GetUserElapsed()", "InvalidCondition",
135  FatalException, "Timer not stopped or times not recorded");
136  }
137  G4double diff=fEndTimes.tms_utime-fStartTimes.tms_utime;
138  return diff/sysconf(_SC_CLK_TCK);
139 }
140 
G4bool fValidTimes
Definition: G4Timer.hh:126
G4double GetSystemElapsed() const
Definition: G4Timer.cc:119
G4bool IsValid() const
G4double a
Definition: TRTMaterials.hh:39
tms fStartTimes
Definition: G4Timer.hh:128
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4double GetUserElapsed() const
Definition: G4Timer.cc:130
G4ExceptionSeverity
clock_t fStartRealTime
Definition: G4Timer.hh:127
clock_t fEndRealTime
Definition: G4Timer.hh:127
G4double GetRealElapsed() const
Definition: G4Timer.cc:107
#define times
std::ostream & operator<<(std::ostream &os, const G4Timer &t)
Definition: G4Timer.cc:87
G4Timer()
Definition: G4Timer.cc:102
double G4double
Definition: G4Types.hh:76
tms fEndTimes
Definition: G4Timer.hh:128