ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Timer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4Timer.cc
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 //
28 //
29 // ----------------------------------------------------------------------
30 // class G4Timer
31 //
32 // Implementation
33 // 29.04.97 G.Cosmo Added timings for Windows systems
34 
35 #include "G4Timer.hh"
36 #include "G4ios.hh"
37 
38 #include <iomanip>
39 
40 // Global error function
41 #include "G4ExceptionSeverity.hh"
42 void G4Exception(const char* originOfException,
43  const char* exceptionCode,
44  G4ExceptionSeverity severity,
45  const char* comments);
46 
47 #if defined(IRIX6_2)
48 # if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE_EXTENDED==1)
49 # define __vfork vfork
50 # endif
51 #endif
52 
53 #ifdef WIN32
54 # include <sys/types.h>
55 # include <windows.h>
56 
57  // extract milliseconds time unit
58  G4int sysconf(G4int a)
59  {
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)((((G4float)t->dwHighDateTime)*429496.7296)+
67  (((G4float)t->dwLowDateTime)*.0001) );
68  }
69 
70 
71  clock_t times(struct tms * t)
72  {
73  FILETIME ct = {0,0}, et = {0,0}, st = {0,0}, ut = {0,0}, rt = {0,0};
74  SYSTEMTIME realtime;
75 
76  GetSystemTime( &realtime );
77  SystemTimeToFileTime( &realtime, &rt ); // get real time in 10^-9 sec
78  if( t != 0 )
79  {
80  GetProcessTimes( GetCurrentProcess(), &ct, &et, &st, &ut);
81  // get process time in 10^-9 sec
82  t->tms_utime = t->tms_cutime = filetime2msec(&ut);
83  t->tms_stime = t->tms_cstime = filetime2msec(&st);
84  }
85  return filetime2msec(&rt);
86  }
87 #endif /* WIN32 */
88 
89 // Print timer status on std::ostream
90 //
91 std::ostream& operator << (std::ostream& os, const G4Timer& t)
92 {
93  // so fixed doesn't propagate
94  std::stringstream ss;
95  ss << std::fixed;
96  if (t.IsValid())
97  {
98  ss << "User=" << t.GetUserElapsed()
99  << "s Real=" << t.GetRealElapsed()
100  << "s Sys=" << t.GetSystemElapsed() << "s";
101 #ifdef G4MULTITHREADED
102  // avoid possible FPE error
103  if(t.GetRealElapsed() > 1.0e-6)
104  {
105  G4double cpu_util = (t.GetUserElapsed()+t.GetSystemElapsed()) /
106  t.GetRealElapsed() * 100.0;
107  ss << std::setprecision(1);
108  ss << " [Cpu=" << std::setprecision(1) << cpu_util << "%]";
109  }
110 #endif
111  }
112  else
113  {
114  ss << "User=****s Real=****s Sys=****s";
115  }
116  os << ss.str();
117 
118  return os;
119 }
120 
122  : fValidTimes(false)
123 {
124 }
125 
127 {
128  if (!fValidTimes)
129  {
130  G4Exception("G4Timer::GetRealElapsed()", "InvalidCondition",
131  FatalException, "Timer not stopped or times not recorded!");
132  }
133  std::chrono::duration<G4double> diff=fEndRealTime-fStartRealTime;
134  return diff.count();
135 }
136 
137 
139 {
140  if (!fValidTimes)
141  {
142  G4Exception("G4Timer::GetSystemElapsed()", "InvalidCondition",
143  FatalException, "Timer not stopped or times not recorded!");
144  }
145  G4double diff=fEndTimes.tms_stime-fStartTimes.tms_stime;
146  return diff/sysconf(_SC_CLK_TCK);
147 }
148 
150 {
151  if (!fValidTimes)
152  {
153  G4Exception("G4Timer::GetUserElapsed()", "InvalidCondition",
154  FatalException, "Timer not stopped or times not recorded");
155  }
156  G4double diff=fEndTimes.tms_utime-fStartTimes.tms_utime;
157  return diff/sysconf(_SC_CLK_TCK);
158 }