Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $Id: G4Timer.cc 110674 2018-06-07 10:30:11Z 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 #include <iomanip>
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  // so fixed doesn't propagate
90  std::stringstream ss;
91  ss << std::fixed;
92  if (t.IsValid())
93  {
94  ss << "User=" << t.GetUserElapsed()
95  << "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  double cpu_util = (t.GetUserElapsed()+t.GetRealElapsed()) /
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 }
121 
123 {
124  if (!fValidTimes)
125  {
126  G4Exception("G4Timer::GetRealElapsed()", "InvalidCondition",
127  FatalException, "Timer not stopped or times not recorded!");
128  }
129  std::chrono::duration<double> diff=fEndRealTime-fStartRealTime;
130  return diff.count();
131 }
132 
133 
135 {
136  if (!fValidTimes)
137  {
138  G4Exception("G4Timer::GetSystemElapsed()", "InvalidCondition",
139  FatalException, "Timer not stopped or times not recorded!");
140  }
141  G4double diff=fEndTimes.tms_stime-fStartTimes.tms_stime;
142  return diff/sysconf(_SC_CLK_TCK);
143 }
144 
146 {
147  if (!fValidTimes)
148  {
149  G4Exception("G4Timer::GetUserElapsed()", "InvalidCondition",
150  FatalException, "Timer not stopped or times not recorded");
151  }
152  G4double diff=fEndTimes.tms_utime-fStartTimes.tms_utime;
153  return diff/sysconf(_SC_CLK_TCK);
154 }
155 
G4double GetRealElapsed() const
Definition: G4Timer.cc:122
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
std::chrono::time_point< clock_type > fEndRealTime
Definition: G4Timer.hh:131
G4Timer()
Definition: G4Timer.cc:117
double G4double
Definition: G4Types.hh:76
G4double GetSystemElapsed() const
Definition: G4Timer.cc:134
G4double GetUserElapsed() const
Definition: G4Timer.cc:145
G4ExceptionSeverity
G4bool fValidTimes
Definition: G4Timer.hh:130
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
tms fStartTimes
Definition: G4Timer.hh:132
tms fEndTimes
Definition: G4Timer.hh:132
std::chrono::time_point< clock_type > fStartRealTime
Definition: G4Timer.hh:131
G4bool IsValid() const