]>
Commit | Line | Data |
---|---|---|
fe4da5cc | 1 | /*> ROUTINE TIMEL |
2 | CERN PROGLIB# Z007 TIMEST .VERSION KERNFOR 4.38 931108 | |
3 | ORIG. 01/03/85 FCA, mod 03/11/93 GF | |
4 | Version for Windows NT/Windows 95 by Valery Fine 30/05/96 (fine@vxcern.cern.ch) | |
5 | */ | |
6 | #include <windows.h> | |
7 | #include <sys\types.h> | |
8 | ||
9 | #ifndef gTicks | |
10 | # define gTicks 1.0e-7; | |
11 | #endif | |
12 | ||
13 | #define time_t double | |
14 | ||
15 | static float timlim; | |
16 | static time_t timstart, timlast; | |
17 | static HANDLE hProcess; | |
18 | ||
19 | ||
20 | static int tml_init = 1; | |
21 | double deftim = 999.; | |
22 | ||
23 | #if defined(CERNLIB_QXCAPT) | |
24 | #define timest type_of_call TIMEST | |
25 | #define timex type_of_call TIMEX | |
26 | #define timed type_of_call TIMED | |
27 | #define timel type_of_call TIMEL | |
28 | #elif defined(CERNLIB_QX_SC) | |
29 | #define timest type_of_call timest_ | |
30 | #define timex type_of_call timex_ | |
31 | #define timed type_of_call timed_ | |
32 | #define timel type_of_call timel_ | |
33 | #endif | |
34 | ||
35 | ||
36 | //______________________________________________________________________________ | |
37 | double GetRealTime(){ | |
38 | union {FILETIME ftFileTime; | |
39 | __int64 ftInt64; | |
40 | } ftRealTime; // time the process has spent in kernel mode | |
41 | SYSTEMTIME st; | |
42 | GetSystemTime(&st); | |
43 | SystemTimeToFileTime(&st,&ftRealTime.ftFileTime); | |
44 | return (double)ftRealTime.ftInt64 * gTicks; | |
45 | } | |
46 | ||
47 | //______________________________________________________________________________ | |
48 | double GetCPUTime(){ | |
49 | OSVERSIONINFO OsVersionInfo; | |
50 | ||
51 | //*-* Value Platform | |
52 | //*-* ---------------------------------------------------- | |
53 | //*-* VER_PLATFORM_WIN32s Win32s on Windows 3.1 | |
54 | //*-* VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95 | |
55 | //*-* VER_PLATFORM_WIN32_NT Windows NT | |
56 | //*-* | |
57 | OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); | |
58 | GetVersionEx(&OsVersionInfo); | |
59 | if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) { | |
60 | DWORD ret; | |
61 | FILETIME ftCreate, // when the process was created | |
62 | ftExit; // when the process exited | |
63 | ||
64 | union {FILETIME ftFileTime; | |
65 | __int64 ftInt64; | |
66 | } ftKernel; // time the process has spent in kernel mode | |
67 | ||
68 | union {FILETIME ftFileTime; | |
69 | __int64 ftInt64; | |
70 | } ftUser; // time the process has spent in user mode | |
71 | ||
72 | ret = GetProcessTimes (hProcess, &ftCreate, &ftExit, | |
73 | &ftKernel.ftFileTime, | |
74 | &ftUser.ftFileTime); | |
75 | if (ret != TRUE){ | |
76 | ret = GetLastError (); | |
77 | printf("GetCPUTime", " Error on GetProcessTimes 0x%lx", (int)ret); | |
78 | } | |
79 | ||
80 | /* | |
81 | * Process times are returned in a 64-bit structure, as the number of | |
82 | * 100 nanosecond ticks since 1 January 1601. User mode and kernel mode | |
83 | * times for this process are in separate 64-bit structures. | |
84 | * To convert to floating point seconds, we will: | |
85 | * | |
86 | * Convert sum of high 32-bit quantities to 64-bit int | |
87 | */ | |
88 | ||
89 | return (double) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks; | |
90 | } | |
91 | else | |
92 | return GetRealTime(); | |
93 | ||
94 | } | |
95 | ||
96 | /* local routine called by timst, and time_init */ | |
97 | //_______________________________________________________________ | |
98 | static void time_st(timl) | |
99 | float timl; | |
100 | { | |
101 | hProcess = GetCurrentProcess(); | |
102 | timstart = GetCPUTime(); | |
103 | timlast = timstart; | |
104 | timlim = timl; | |
105 | tml_init = 0; | |
106 | return; | |
107 | } | |
108 | /* local routine to start by default */ | |
109 | //_______________________________________________________________ | |
110 | static void time_init() | |
111 | { | |
112 | float maxtime; | |
113 | maxtime=deftim; | |
114 | time_st(maxtime); | |
115 | return; | |
116 | } | |
117 | ||
118 | //_______________________________________________________________ | |
119 | void timest(timl) | |
120 | float *timl; | |
121 | { | |
122 | float maxtime; | |
123 | ||
124 | if (tml_init != 0) { | |
125 | maxtime = *timl; | |
126 | time_st(maxtime); | |
127 | } | |
128 | return; | |
129 | } | |
130 | //_______________________________________________________________ | |
131 | void timex(tx) | |
132 | /* | |
133 | C | |
134 | CERN PROGLIB# Z007 TIMEX .VERSION KERNFOR 4.38 931108 | |
135 | C | |
136 | */ | |
137 | float *tx; | |
138 | { | |
139 | time_t timnow; | |
140 | if (tml_init) { | |
141 | time_init(); | |
142 | *tx = 0.; | |
143 | } | |
144 | else { | |
145 | timnow= GetCPUTime(); | |
146 | *tx = (float) (timnow - timstart); | |
147 | } | |
148 | return; | |
149 | } | |
150 | ||
151 | //_______________________________________________________________ | |
152 | void timed(td) | |
153 | /* | |
154 | C | |
155 | CERN PROGLIB# Z007 TIMED .VERSION KERNFOR 4.38 931108 | |
156 | C | |
157 | */ | |
158 | float *td; | |
159 | { | |
160 | time_t timnow; | |
161 | if (tml_init) { | |
162 | time_init(); | |
163 | *td = timlim; | |
164 | } | |
165 | else { | |
166 | timnow=GetCPUTime(); | |
167 | *td = (float) (timnow - timlast); | |
168 | timlast = timnow; | |
169 | } | |
170 | return; | |
171 | } | |
172 | ||
173 | //_______________________________________________________________ | |
174 | void timel(tl) | |
175 | /* | |
176 | C | |
177 | CERN PROGLIB# Z007 TIMEL .VERSION KERNFOR 4.38 931108 | |
178 | C | |
179 | */ | |
180 | float *tl; | |
181 | { | |
182 | time_t timnow; | |
183 | if (tml_init) { | |
184 | time_init(); | |
185 | *tl = timlim; | |
186 | } | |
187 | else { | |
188 | timnow= GetCPUTime(); | |
189 | *tl = timlim - (float) (timnow - timstart); | |
190 | } | |
191 | return; | |
192 | } | |
193 | /*> END <----------------------------------------------------------*/ |