1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* Copyright (C) 2003 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
static const volatile char cvsid[] = "$Id: ttTime.c,v 1.1 2003/09/23 12:43:46 johan Exp $";
/*
* $Revision: 1.1 $
* (c) Copyright 1996-2003, TimesTen, Inc.
* All rights reserved.
*
*/
/* Contains functions for performing elapsed-time calculations
in a portable manner */
#include "ttTime.h"
#ifdef WIN32
#include <stdio.h>
#include <mapiutil.h>
/*------------*/
/* NT VERSION */
/*------------*/
/*********************************************************************
*
* FUNCTION: ttGetThreadTimes
*
* DESCRIPTION: This function sets the supplied parameter's
* user and kernel time for the current thread.
*
* PARAMETERS: ttThreadTimes* timesP thread time structure
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
ttGetThreadTimes(ttThreadTimes* timesP)
{
BOOL rc;
HANDLE curThread;
FILETIME creationTime;
FILETIME exitTime;
FILETIME kTime;
FILETIME uTime;
memset (&kTime, 0, sizeof (FILETIME));
memset (&uTime, 0, sizeof (FILETIME));
curThread = GetCurrentThread();
rc = GetThreadTimes(curThread,
&creationTime,
&exitTime,
&kTime,
&uTime);
timesP->kernelTime = kTime;
timesP->userTime = uTime;
}
/*********************************************************************
*
* FUNCTION: ttCalcElapsedThreadTimes
*
* DESCRIPTION: This function calculates the user and kernel
* time deltas.
*
* PARAMETERS: ttThreadTimes* beforeP beginning timestamp (IN)
* ttThreadTimes* afterP ending timestamp (IN)
* double* kernelDeltaP kernel time delta (OUT)
* double* userDeltaP user time delta (OUT)
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
ttCalcElapsedThreadTimes(ttThreadTimes* beforeP,
ttThreadTimes* afterP,
double* kernelDeltaP,
double* userDeltaP)
{
static const double secPerHi = (double) 4.294967296; /* 2**32 * 10**-9 */
FILETIME *before, *after;
before = &beforeP->kernelTime;
after = &afterP->kernelTime;
*kernelDeltaP = (double) ((after->dwHighDateTime - before->dwHighDateTime) * secPerHi
+ (after->dwLowDateTime - before->dwLowDateTime) * 100e-9);
before = &beforeP->userTime;
after = &afterP->userTime;
*userDeltaP = (double) ((after->dwHighDateTime - before->dwHighDateTime) * secPerHi
+ (after->dwLowDateTime - before->dwLowDateTime) * 100e-9);
}
/*********************************************************************
*
* FUNCTION: ttGetWallClockTime
*
* DESCRIPTION: This function gets the current wall-clock time.
*
* PARAMETERS: ttWallClockTime* timeP tms time structure (OUT)
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
ttGetWallClockTime(ttWallClockTime* timeP)
{
LARGE_INTEGER frequency;
if ( QueryPerformanceFrequency(&frequency) ) {
QueryPerformanceCounter(&(timeP->time64));
}
else {
_ftime(&(timeP->notSoLargeTime));
}
}
/*********************************************************************
*
* FUNCTION: ttCalcElapsedWallClockTime
*
* DESCRIPTION: This function calculates the elapsed wall-clock
* time in msec.
*
* PARAMETERS: ttWallClockTime* beforeP starting timestamp
* ttWallClockTime* afterP ending timestamp
* double* nmillisecondsP elapsed time (OUT)
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
ttCalcElapsedWallClockTime(ttWallClockTime* beforeP,
ttWallClockTime* afterP,
double* nmillisecondsP)
{
LARGE_INTEGER frequency;
if ( QueryPerformanceFrequency(&frequency) ) {
*nmillisecondsP = 1000 * ((double) (afterP->time64.QuadPart
- beforeP->time64.QuadPart))
/ frequency.QuadPart;
}
else {
double start;
double end;
start = (double) beforeP->notSoLargeTime.time * 1000. +
(double) beforeP->notSoLargeTime.millitm;
end = (double) afterP->notSoLargeTime.time * 1000. +
(double) afterP->notSoLargeTime.millitm;
*nmillisecondsP = (double) (end - start);
}
}
#elif defined (RTSYS_VXWORKS)
/*-----------------*/
/* VxWorks VERSION */
/*-----------------*/
/*
* The TimeBase registers have a period of 60ns, i.e.
* 0.00000006 or (6e-8) seconds.
*/
#define TIMER_MSEC_PER_CYC (6e-5)
void
ttGetWallClockTime(ttWallClockTime* timeP)
{
vxTimeBaseGet(&timeP->sep.upper32, &timeP->sep.lower32);
}
void
ttCalcElapsedWallClockTime(ttWallClockTime* beforeP,
ttWallClockTime* afterP,
double* nmillisecondsP)
{
*nmillisecondsP = (double)(afterP->val - beforeP->val) * TIMER_MSEC_PER_CYC;
}
#else
/*--------------*/
/* UNIX VERSION */
/*--------------*/
#include <unistd.h>
/*********************************************************************
*
* FUNCTION: ttGetThreadTimes
*
* DESCRIPTION: This function sets the supplied parameter's
* tms structure.
*
* PARAMETERS: ttThreadTimes* timesP tms time structure
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
#ifdef SB_P_OS_CHORUS
void ttGetThreadTimes(ttThreadTimes* timesP)
{
KnCap actorCap;
if (acap (agetId(), &actorCap) == -1) {
timesP->ins.tmSec = 0;
timesP->ins.tmNSec = 0;
timesP->ext.tmSec = 0;
timesP->ext.tmNSec = 0;
}
else {
(void) threadTimes (&actorCap, K_ALLACTORTHREADS,
×P->ins, ×P->ext);
}
}
#else
void ttGetThreadTimes(ttThreadTimes* timesP)
{
(void) times(timesP);
}
#endif
/*********************************************************************
*
* FUNCTION: ttCalcElapsedThreadTimes
*
* DESCRIPTION: This function calculates the user and kernel
* time deltas.
*
* PARAMETERS: ttThreadTimes* beforeP beginning timestamp (IN)
* ttThreadTimes* afterP ending timestamp (IN)
* double* kernelDeltaP kernel time delta (OUT)
* double* userDeltaP user time delta (OUT)
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
#ifdef SB_P_OS_CHORUS
void
ttCalcElapsedThreadTimes(ttThreadTimes* beforeP,
ttThreadTimes* afterP,
double* kernelDeltaP,
double* userDeltaP)
{
double kernelBefore;
double kernelAfter;
double userBefore;
double userAfter;
kernelBefore = (beforeP->ext.tmSec) + (beforeP->ext.tmNSec / 1e9);
kernelAfter = (afterP->ext.tmSec) + (afterP->ext.tmNSec / 1e9);
*kernelDeltaP = kernelAfter - kernelBefore;
userBefore = (beforeP->ins.tmSec) + (beforeP->ins.tmNSec / 1e9);
userAfter = (afterP->ins.tmSec) + (afterP->ins.tmNSec / 1e9);
*userDeltaP = userAfter - userBefore;
}
#else
void
ttCalcElapsedThreadTimes(ttThreadTimes* beforeP,
ttThreadTimes* afterP,
double* kernelDeltaP,
double* userDeltaP)
{
double ticks = (double)sysconf(_SC_CLK_TCK);
*kernelDeltaP = (afterP->tms_stime - beforeP->tms_stime) / ticks;
*userDeltaP = (afterP->tms_utime - beforeP->tms_utime) / ticks;
}
#endif
/*********************************************************************
*
* FUNCTION: ttGetWallClockTime
*
* DESCRIPTION: This function gets the current wall-clock time.
*
* PARAMETERS: ttWallClockTime* timeP tms time structure (OUT)
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
ttGetWallClockTime(ttWallClockTime* timeP)
{
gettimeofday(timeP, NULL);
}
/*********************************************************************
*
* FUNCTION: ttCalcElapsedWallClockTime
*
* DESCRIPTION: This function calculates the elapsed wall-clock
* time is msec.
*
* PARAMETERS: ttWallClockTime* beforeP starting timestamp
* ttWallClockTime* afterP ending timestamp
* double* nmillisecondsP elapsed time (OUT)
*
* RETURNS: void
*
* NOTES: NONE
*
*********************************************************************/
void
ttCalcElapsedWallClockTime(ttWallClockTime* beforeP,
ttWallClockTime* afterP,
double* nmillisP)
{
*nmillisP = (afterP->tv_sec - beforeP->tv_sec)*1000.0 +
(afterP->tv_usec - beforeP->tv_usec)/1000.0;
}
#endif
/* Emacs variable settings */
/* Local Variables: */
/* tab-width:8 */
/* indent-tabs-mode:nil */
/* c-basic-offset:2 */
/* End: */
|