Geant4-11
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// G4Timer class implementation
27//
28// Author: P.Kent, 21.08.95 - First implementation
29// Revision: G.Cosmo, 29.04.97 - Added timings for Windows
30// --------------------------------------------------------------------
31
32#include "G4Timer.hh"
33#include "G4ios.hh"
34
35#include <iomanip>
36
37// Global error function
39void G4Exception(const char* originOfException, const char* exceptionCode,
40 G4ExceptionSeverity severity, const char* comments);
41
42#if defined(IRIX6_2)
43# if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE_EXTENDED == 1)
44# define __vfork vfork
45# endif
46#endif
47
48#ifdef WIN32
49# include <sys/types.h>
50# include <windows.h>
51
52// extract milliseconds time unit
53G4int sysconf(G4int a)
54{
55 if(a == _SC_CLK_TCK)
56 return 1000;
57 else
58 return 0;
59}
60
61static clock_t filetime2msec(FILETIME* t)
62{
63 return (clock_t)((((G4float) t->dwHighDateTime) * 429496.7296) +
64 (((G4float) t->dwLowDateTime) * .0001));
65}
66
67clock_t times(struct tms* t)
68{
69 FILETIME ct = { 0, 0 }, et = { 0, 0 }, st = { 0, 0 }, ut = { 0, 0 },
70 rt = { 0, 0 };
71 SYSTEMTIME realtime;
72
73 GetSystemTime(&realtime);
74 SystemTimeToFileTime(&realtime, &rt); // get real time in 10^-9 sec
75 if(t != 0)
76 {
77 GetProcessTimes(GetCurrentProcess(), &ct, &et, &st, &ut);
78 // 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 on std::ostream
87//
88std::ostream& operator<<(std::ostream& os, const G4Timer& t)
89{
90 // so fixed doesn't propagate
91 std::stringstream ss;
92 ss << std::fixed;
93 if(t.IsValid())
94 {
95 ss << "User=" << t.GetUserElapsed() << "s Real=" << t.GetRealElapsed()
96 << "s Sys=" << t.GetSystemElapsed() << "s";
97#ifdef G4MULTITHREADED
98 // avoid possible FPE error
99 if(t.GetRealElapsed() > 1.0e-6)
100 {
101 G4double cpu_util = (t.GetUserElapsed() + t.GetSystemElapsed()) /
102 t.GetRealElapsed() * 100.0;
103 ss << std::setprecision(1);
104 ss << " [Cpu=" << std::setprecision(1) << cpu_util << "%]";
105 }
106#endif
107 }
108 else
109 {
110 ss << "User=****s Real=****s Sys=****s";
111 }
112 os << ss.str();
113
114 return os;
115}
116
118 : fValidTimes(false)
119{}
120
122{
123 if(!fValidTimes)
124 {
125 G4Exception("G4Timer::GetRealElapsed()", "InvalidCondition", FatalException,
126 "Timer not stopped or times not recorded!");
127 }
128 std::chrono::duration<G4double> diff = fEndRealTime - fStartRealTime;
129 return diff.count();
130}
131
133{
134 if(!fValidTimes)
135 {
136 G4Exception("G4Timer::GetSystemElapsed()", "InvalidCondition",
137 FatalException, "Timer not stopped or times not recorded!");
138 }
139 G4double diff = fEndTimes.tms_stime - fStartTimes.tms_stime;
140 return diff / sysconf(_SC_CLK_TCK);
141}
142
144{
145 if(!fValidTimes)
146 {
147 G4Exception("G4Timer::GetUserElapsed()", "InvalidCondition", FatalException,
148 "Timer not stopped or times not recorded");
149 }
150 G4double diff = fEndTimes.tms_utime - fStartTimes.tms_utime;
151 return diff / sysconf(_SC_CLK_TCK);
152}
G4ExceptionSeverity
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:35
float G4float
Definition: G4Types.hh:84
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
std::chrono::time_point< clock_type > fStartRealTime
Definition: G4Timer.hh:125
tms fEndTimes
Definition: G4Timer.hh:126
G4double GetSystemElapsed() const
Definition: G4Timer.cc:132
std::chrono::time_point< clock_type > fEndRealTime
Definition: G4Timer.hh:125
tms fStartTimes
Definition: G4Timer.hh:126
G4Timer()
Definition: G4Timer.cc:117
G4bool fValidTimes
Definition: G4Timer.hh:124
G4bool IsValid() const
G4double GetUserElapsed() const
Definition: G4Timer.cc:143
G4double GetRealElapsed() const
Definition: G4Timer.cc:121
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)