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