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
|
/* $Id: fpm_clock.c,v 1.4 2008/09/18 23:19:59 anight Exp $ */
/* (c) 2007,2008 Andrei Nigmatulin */
#include "fpm_config.h"
#if defined(HAVE_CLOCK_GETTIME)
#include <time.h> /* for CLOCK_MONOTONIC */
#endif
#include "fpm_clock.h"
#include "zlog.h"
/* posix monotonic clock - preferred source of time */
#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
static int monotonic_works;
int fpm_clock_init() /* {{{ */
{
struct timespec ts;
monotonic_works = 0;
if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
monotonic_works = 1;
}
return 0;
}
/* }}} */
int fpm_clock_get(struct timeval *tv) /* {{{ */
{
if (monotonic_works) {
struct timespec ts;
if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
zlog(ZLOG_SYSERROR, "clock_gettime() failed");
return -1;
}
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
return 0;
}
return gettimeofday(tv, 0);
}
/* }}} */
/* macosx clock */
#elif defined(HAVE_CLOCK_GET_TIME)
#include <mach/mach.h>
#include <mach/clock.h>
#include <mach/mach_error.h>
static clock_serv_t mach_clock;
/* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
/* mach_clock also should be re-initialized in child process after fork */
int fpm_clock_init() /* {{{ */
{
kern_return_t ret;
mach_timespec_t aTime;
ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
if (ret != KERN_SUCCESS) {
zlog(ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
return -1;
}
/* test if it works */
ret = clock_get_time(mach_clock, &aTime);
if (ret != KERN_SUCCESS) {
zlog(ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
return -1;
}
return 0;
}
/* }}} */
int fpm_clock_get(struct timeval *tv) /* {{{ */
{
kern_return_t ret;
mach_timespec_t aTime;
ret = clock_get_time(mach_clock, &aTime);
if (ret != KERN_SUCCESS) {
zlog(ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
return -1;
}
tv->tv_sec = aTime.tv_sec;
tv->tv_usec = aTime.tv_nsec / 1000;
return 0;
}
/* }}} */
#else /* no clock */
int fpm_clock_init() /* {{{ */
{
return 0;
}
/* }}} */
int fpm_clock_get(struct timeval *tv) /* {{{ */
{
return gettimeofday(tv, 0);
}
/* }}} */
#endif
|