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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/*
* Copyright (C) 2013-2020 Argonne National Laboratory, Department of Energy,
* UChicago Argonne, LLC and The HDF Group.
* All rights reserved.
*
* The full copyright notice, including terms governing use, modification,
* and redistribution, is contained in the COPYING file that can be
* found at the root of the source code distribution tree.
*/
#ifndef MERCURY_TIME_H
#define MERCURY_TIME_H
#include "mercury_util_config.h"
#if defined(_WIN32)
#include <windows.h>
#elif defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
#include <time.h>
#elif defined(__APPLE__) && defined(HG_UTIL_HAS_SYSTIME_H)
#include <mach/mach_time.h>
#include <sys/time.h>
#else
#include <stdio.h>
#include <unistd.h>
#if defined(HG_UTIL_HAS_SYSTIME_H)
#include <sys/time.h>
#else
#error "Not supported on this platform."
#endif
#endif
/*************************************/
/* Public Type and Struct Definition */
/*************************************/
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
typedef struct timespec hg_time_t;
#else
typedef struct hg_time hg_time_t;
struct hg_time {
long tv_sec;
long tv_usec;
};
#endif
/*****************/
/* Public Macros */
/*****************/
/*********************/
/* Public Prototypes */
/*********************/
#ifdef __cplusplus
extern "C" {
#endif
/**
* Get an elapsed time on the calling processor.
*
* \param tv [OUT] pointer to returned time structure
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_time_get_current(hg_time_t *tv);
/**
* Get an elapsed time on the calling processor (resolution is ms).
*
* \param tv [OUT] pointer to returned time structure
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_time_get_current_ms(hg_time_t *tv);
/**
* Convert hg_time_t to double.
*
* \param tv [IN] time structure
*
* \return Converted time in seconds
*/
static HG_UTIL_INLINE double hg_time_to_double(hg_time_t tv);
/**
* Convert double to hg_time_t.
*
* \param d [IN] time in seconds
*
* \return Converted time structure
*/
static HG_UTIL_INLINE hg_time_t hg_time_from_double(double d);
/**
* Convert (integer) milliseconds to hg_time_t.
*
* \param ms [IN] time in milliseconds
*
* \return Converted time structure
*/
static HG_UTIL_INLINE hg_time_t hg_time_from_ms(unsigned int ms);
/**
* Convert hg_time_t to (integer) milliseconds.
*
* \param tv [IN] time structure
*
* \return Time in milliseconds
*/
static HG_UTIL_INLINE unsigned int hg_time_to_ms(hg_time_t tv);
/**
* Compare time values.
*
* \param in1 [IN] time structure
* \param in2 [IN] time structure
*
* \return 1 if in1 < in2, 0 otherwise
*/
static HG_UTIL_INLINE int hg_time_less(hg_time_t in1, hg_time_t in2);
/**
* Diff time values and return the number of seconds elapsed between
* time \in2 and time \in1.
*
* \param in2 [IN] time structure
* \param in1 [IN] time structure
*
* \return Subtracted time
*/
static HG_UTIL_INLINE double hg_time_diff(hg_time_t in2, hg_time_t in1);
/**
* Add time values.
*
* \param in1 [IN] time structure
* \param in2 [IN] time structure
*
* \return Summed time structure
*/
static HG_UTIL_INLINE hg_time_t hg_time_add(hg_time_t in1, hg_time_t in2);
/**
* Subtract time values.
*
* \param in1 [IN] time structure
* \param in2 [IN] time structure
*
* \return Subtracted time structure
*/
static HG_UTIL_INLINE hg_time_t hg_time_subtract(hg_time_t in1, hg_time_t in2);
/**
* Sleep until the time specified in rqt has elapsed.
*
* \param reqt [IN] time structure
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_time_sleep(const hg_time_t rqt);
/**
* Get a string containing current time/date stamp.
*
* \return Valid string or NULL on failure
*/
static HG_UTIL_INLINE char *hg_time_stamp(void);
/*---------------------------------------------------------------------------*/
#ifdef _WIN32
static HG_UTIL_INLINE LARGE_INTEGER
get_FILETIME_offset(void)
{
SYSTEMTIME s;
FILETIME f;
LARGE_INTEGER t;
s.wYear = 1970;
s.wMonth = 1;
s.wDay = 1;
s.wHour = 0;
s.wMinute = 0;
s.wSecond = 0;
s.wMilliseconds = 0;
SystemTimeToFileTime(&s, &f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
return t;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_get_current(hg_time_t *tv)
{
LARGE_INTEGER t;
FILETIME f;
double t_usec;
static LARGE_INTEGER offset;
static double freq_to_usec;
static int initialized = 0;
static BOOL use_perf_counter = 0;
if (!initialized) {
LARGE_INTEGER perf_freq;
initialized = 1;
use_perf_counter = QueryPerformanceFrequency(&perf_freq);
if (use_perf_counter) {
QueryPerformanceCounter(&offset);
freq_to_usec = (double)perf_freq.QuadPart / 1000000.;
}
else {
offset = get_FILETIME_offset();
freq_to_usec = 10.;
}
}
if (use_perf_counter) {
QueryPerformanceCounter(&t);
}
else {
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
}
t.QuadPart -= offset.QuadPart;
t_usec = (double)t.QuadPart / freq_to_usec;
t.QuadPart = t_usec;
tv->tv_sec = t.QuadPart / 1000000;
tv->tv_usec = t.QuadPart % 1000000;
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_get_current_ms(hg_time_t *tv)
{
return hg_time_get_current(tv);
}
/*---------------------------------------------------------------------------*/
#elif defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
static HG_UTIL_INLINE int
hg_time_get_current(hg_time_t *tv)
{
clock_gettime(CLOCK_MONOTONIC, tv);
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_get_current_ms(hg_time_t *tv)
{
/* ppc/32 and ppc/64 do not support CLOCK_MONOTONIC_COARSE in vdso */
#if defined(__ppc64__) || defined(__ppc__) || defined(__PPC64__) || defined(__PPC__) || \
!defined(HG_UTIL_HAS_CLOCK_MONOTONIC_COARSE)
clock_gettime(CLOCK_MONOTONIC, tv);
#else
/* We don't need fine grain time stamps, _COARSE resolution is 1ms */
clock_gettime(CLOCK_MONOTONIC_COARSE, tv);
#endif
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
#elif defined(__APPLE__) && defined(HG_UTIL_HAS_SYSTIME_H)
static HG_UTIL_INLINE int
hg_time_get_current(hg_time_t *tv)
{
static uint64_t monotonic_timebase_factor = 0;
uint64_t monotonic_nsec;
if (monotonic_timebase_factor == 0) {
mach_timebase_info_data_t timebase_info;
(void)mach_timebase_info(&timebase_info);
monotonic_timebase_factor = timebase_info.numer / timebase_info.denom;
}
monotonic_nsec = (mach_absolute_time() * monotonic_timebase_factor);
tv->tv_sec = (long)(monotonic_nsec / 1000000000);
tv->tv_usec = (long)((monotonic_nsec - (uint64_t)tv->tv_sec) / 1000);
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_get_current_ms(hg_time_t *tv)
{
return hg_time_get_current(tv);
}
#else
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_get_current(hg_time_t *tv)
{
gettimeofday((struct timeval *)tv, NULL);
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_get_current_ms(hg_time_t *tv)
{
return hg_time_get_current(tv);
}
#endif
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE double
hg_time_to_double(hg_time_t tv)
{
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
return (double)tv.tv_sec + (double)(tv.tv_nsec) * 0.000000001;
#else
return (double)tv.tv_sec + (double)(tv.tv_usec) * 0.000001;
#endif
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE hg_time_t
hg_time_from_double(double d)
{
hg_time_t tv;
tv.tv_sec = (long)d;
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
tv.tv_nsec = (long)((d - (double)(tv.tv_sec)) * 1000000000);
#else
tv.tv_usec = (long)((d - (double)(tv.tv_sec)) * 1000000);
#endif
return tv;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE unsigned int
hg_time_to_ms(hg_time_t tv)
{
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
return (unsigned int)(tv.tv_sec * 1000 + tv.tv_nsec / 1000000);
#else
return (unsigned int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
#endif
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE hg_time_t
hg_time_from_ms(unsigned int ms)
{
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
return (hg_time_t){.tv_sec = ms / 1000, .tv_nsec = (ms - (ms / 1000) * 1000) * 1000000};
#else
return (hg_time_t){.tv_sec = ms / 1000, .tv_usec = (ms - (ms / 1000) * 1000) * 1000};
#endif
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_less(hg_time_t in1, hg_time_t in2)
{
return ((in1.tv_sec < in2.tv_sec) || ((in1.tv_sec == in2.tv_sec) &&
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
(in1.tv_nsec < in2.tv_nsec)));
#else
(in1.tv_usec < in2.tv_usec)));
#endif
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE double
hg_time_diff(hg_time_t in2, hg_time_t in1)
{
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
return ((double)in2.tv_sec + (double)(in2.tv_nsec) * 0.000000001) -
((double)in1.tv_sec + (double)(in1.tv_nsec) * 0.000000001);
#else
return ((double)in2.tv_sec + (double)(in2.tv_usec) * 0.000001) -
((double)in1.tv_sec + (double)(in1.tv_usec) * 0.000001);
#endif
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE hg_time_t
hg_time_add(hg_time_t in1, hg_time_t in2)
{
hg_time_t out;
out.tv_sec = in1.tv_sec + in2.tv_sec;
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
out.tv_nsec = in1.tv_nsec + in2.tv_nsec;
if (out.tv_nsec > 1000000000) {
out.tv_nsec -= 1000000000;
out.tv_sec += 1;
}
#else
out.tv_usec = in1.tv_usec + in2.tv_usec;
if (out.tv_usec > 1000000) {
out.tv_usec -= 1000000;
out.tv_sec += 1;
}
#endif
return out;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE hg_time_t
hg_time_subtract(hg_time_t in1, hg_time_t in2)
{
hg_time_t out;
out.tv_sec = in1.tv_sec - in2.tv_sec;
#if defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
out.tv_nsec = in1.tv_nsec - in2.tv_nsec;
if (out.tv_nsec < 0) {
out.tv_nsec += 1000000000;
out.tv_sec -= 1;
}
#else
out.tv_usec = in1.tv_usec - in2.tv_usec;
if (out.tv_usec < 0) {
out.tv_usec += 1000000;
out.tv_sec -= 1;
}
#endif
return out;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_time_sleep(const hg_time_t rqt)
{
#ifdef _WIN32
DWORD dwMilliseconds = (DWORD)(hg_time_to_double(rqt) / 1000);
Sleep(dwMilliseconds);
#elif defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
if (nanosleep(&rqt, NULL))
return HG_UTIL_FAIL;
#else
useconds_t usec = (useconds_t)rqt.tv_sec * 1000000 + (useconds_t)rqt.tv_usec;
if (usleep(usec))
return HG_UTIL_FAIL;
#endif
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
#define HG_UTIL_STAMP_MAX 128
static HG_UTIL_INLINE char *
hg_time_stamp(void)
{
static char buf[HG_UTIL_STAMP_MAX] = {'\0'};
#if defined(_WIN32)
/* TODO not implemented */
#elif defined(HG_UTIL_HAS_TIME_H) && defined(HG_UTIL_HAS_CLOCK_GETTIME)
struct tm *local_time;
time_t t;
t = time(NULL);
local_time = localtime(&t);
if (local_time == NULL)
return NULL;
if (strftime(buf, HG_UTIL_STAMP_MAX, "%a, %d %b %Y %T %Z", local_time) == 0)
return NULL;
#else
struct timeval tv;
struct timezone tz;
unsigned long days, hours, minutes, seconds;
gettimeofday(&tv, &tz);
days = (unsigned long)tv.tv_sec / (3600 * 24);
hours = ((unsigned long)tv.tv_sec - days * 24 * 3600) / 3600;
minutes = ((unsigned long)tv.tv_sec - days * 24 * 3600 - hours * 3600) / 60;
seconds = (unsigned long)tv.tv_sec - days * 24 * 3600 - hours * 3600 - minutes * 60;
hours -= (unsigned long)tz.tz_minuteswest / 60;
snprintf(buf, HG_UTIL_STAMP_MAX, "%02lu:%02lu:%02lu (GMT-%d)", hours, minutes, seconds,
tz.tz_minuteswest / 60);
#endif
return buf;
}
#ifdef __cplusplus
}
#endif
#endif /* MERCURY_TIME_H */
|