1*86d7f5d3SJohn Marino /* $NetBSD: timestamp.c,v 1.1.1.1 2008/12/22 00:18:13 haad Exp $ */
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino * Copyright (C) 2006 Rackable Systems All rights reserved.
5*86d7f5d3SJohn Marino *
6*86d7f5d3SJohn Marino * This file is part of LVM2.
7*86d7f5d3SJohn Marino *
8*86d7f5d3SJohn Marino * This copyrighted material is made available to anyone wishing to use,
9*86d7f5d3SJohn Marino * modify, copy, or redistribute it subject to the terms and conditions
10*86d7f5d3SJohn Marino * of the GNU Lesser General Public License v.2.1.
11*86d7f5d3SJohn Marino *
12*86d7f5d3SJohn Marino * You should have received a copy of the GNU Lesser General Public License
13*86d7f5d3SJohn Marino * along with this program; if not, write to the Free Software Foundation,
14*86d7f5d3SJohn Marino * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15*86d7f5d3SJohn Marino */
16*86d7f5d3SJohn Marino
17*86d7f5d3SJohn Marino /*
18*86d7f5d3SJohn Marino * Abstract out the time methods used so they can be adjusted later -
19*86d7f5d3SJohn Marino * the results of these routines should stay in-core. This implementation
20*86d7f5d3SJohn Marino * requires librt.
21*86d7f5d3SJohn Marino */
22*86d7f5d3SJohn Marino
23*86d7f5d3SJohn Marino #include "lib.h"
24*86d7f5d3SJohn Marino #include <stdlib.h>
25*86d7f5d3SJohn Marino
26*86d7f5d3SJohn Marino #include "timestamp.h"
27*86d7f5d3SJohn Marino
28*86d7f5d3SJohn Marino /*
29*86d7f5d3SJohn Marino * The realtime section uses clock_gettime with the CLOCK_MONOTONIC
30*86d7f5d3SJohn Marino * parameter to prevent issues with time warps
31*86d7f5d3SJohn Marino */
32*86d7f5d3SJohn Marino #ifdef HAVE_REALTIME
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino #include <time.h>
35*86d7f5d3SJohn Marino #include <bits/time.h>
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino struct timestamp {
38*86d7f5d3SJohn Marino struct timespec t;
39*86d7f5d3SJohn Marino };
40*86d7f5d3SJohn Marino
get_timestamp(void)41*86d7f5d3SJohn Marino struct timestamp *get_timestamp(void)
42*86d7f5d3SJohn Marino {
43*86d7f5d3SJohn Marino struct timestamp *ts = NULL;
44*86d7f5d3SJohn Marino
45*86d7f5d3SJohn Marino if (!(ts = dm_malloc(sizeof(*ts))))
46*86d7f5d3SJohn Marino return_NULL;
47*86d7f5d3SJohn Marino
48*86d7f5d3SJohn Marino if (clock_gettime(CLOCK_MONOTONIC, &ts->t)) {
49*86d7f5d3SJohn Marino log_sys_error("clock_gettime", "get_timestamp");
50*86d7f5d3SJohn Marino return NULL;
51*86d7f5d3SJohn Marino }
52*86d7f5d3SJohn Marino
53*86d7f5d3SJohn Marino return ts;
54*86d7f5d3SJohn Marino }
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino /* cmp_timestamp: Compare two timestamps
57*86d7f5d3SJohn Marino *
58*86d7f5d3SJohn Marino * Return: -1 if t1 is less than t2
59*86d7f5d3SJohn Marino * 0 if t1 is equal to t2
60*86d7f5d3SJohn Marino * 1 if t1 is greater than t2
61*86d7f5d3SJohn Marino */
cmp_timestamp(struct timestamp * t1,struct timestamp * t2)62*86d7f5d3SJohn Marino int cmp_timestamp(struct timestamp *t1, struct timestamp *t2)
63*86d7f5d3SJohn Marino {
64*86d7f5d3SJohn Marino if(t1->t.tv_sec < t2->t.tv_sec)
65*86d7f5d3SJohn Marino return -1;
66*86d7f5d3SJohn Marino if(t1->t.tv_sec > t2->t.tv_sec)
67*86d7f5d3SJohn Marino return 1;
68*86d7f5d3SJohn Marino
69*86d7f5d3SJohn Marino if(t1->t.tv_nsec < t2->t.tv_nsec)
70*86d7f5d3SJohn Marino return -1;
71*86d7f5d3SJohn Marino if(t1->t.tv_nsec > t2->t.tv_nsec)
72*86d7f5d3SJohn Marino return 1;
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino return 0;
75*86d7f5d3SJohn Marino }
76*86d7f5d3SJohn Marino
77*86d7f5d3SJohn Marino #else /* ! HAVE_REALTIME */
78*86d7f5d3SJohn Marino
79*86d7f5d3SJohn Marino /*
80*86d7f5d3SJohn Marino * The !realtime section just uses gettimeofday and is therefore subject
81*86d7f5d3SJohn Marino * to ntp-type time warps - not sure if should allow that.
82*86d7f5d3SJohn Marino */
83*86d7f5d3SJohn Marino
84*86d7f5d3SJohn Marino #include <sys/time.h>
85*86d7f5d3SJohn Marino
86*86d7f5d3SJohn Marino struct timestamp {
87*86d7f5d3SJohn Marino struct timeval t;
88*86d7f5d3SJohn Marino };
89*86d7f5d3SJohn Marino
get_timestamp(void)90*86d7f5d3SJohn Marino struct timestamp *get_timestamp(void)
91*86d7f5d3SJohn Marino {
92*86d7f5d3SJohn Marino struct timestamp *ts = NULL;
93*86d7f5d3SJohn Marino
94*86d7f5d3SJohn Marino if (!(ts = dm_malloc(sizeof(*ts))))
95*86d7f5d3SJohn Marino return_NULL;
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino if (gettimeofday(&ts->t, NULL)) {
98*86d7f5d3SJohn Marino log_sys_error("gettimeofday", "get_timestamp");
99*86d7f5d3SJohn Marino return NULL;
100*86d7f5d3SJohn Marino }
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino return ts;
103*86d7f5d3SJohn Marino }
104*86d7f5d3SJohn Marino
105*86d7f5d3SJohn Marino /* cmp_timestamp: Compare two timestamps
106*86d7f5d3SJohn Marino *
107*86d7f5d3SJohn Marino * Return: -1 if t1 is less than t2
108*86d7f5d3SJohn Marino * 0 if t1 is equal to t2
109*86d7f5d3SJohn Marino * 1 if t1 is greater than t2
110*86d7f5d3SJohn Marino */
cmp_timestamp(struct timestamp * t1,struct timestamp * t2)111*86d7f5d3SJohn Marino int cmp_timestamp(struct timestamp *t1, struct timestamp *t2)
112*86d7f5d3SJohn Marino {
113*86d7f5d3SJohn Marino if(t1->t.tv_sec < t2->t.tv_sec)
114*86d7f5d3SJohn Marino return -1;
115*86d7f5d3SJohn Marino if(t1->t.tv_sec > t2->t.tv_sec)
116*86d7f5d3SJohn Marino return 1;
117*86d7f5d3SJohn Marino
118*86d7f5d3SJohn Marino if(t1->t.tv_usec < t2->t.tv_usec)
119*86d7f5d3SJohn Marino return -1;
120*86d7f5d3SJohn Marino if(t1->t.tv_usec > t2->t.tv_usec)
121*86d7f5d3SJohn Marino return 1;
122*86d7f5d3SJohn Marino
123*86d7f5d3SJohn Marino return 0;
124*86d7f5d3SJohn Marino }
125*86d7f5d3SJohn Marino
126*86d7f5d3SJohn Marino #endif /* HAVE_REALTIME */
127*86d7f5d3SJohn Marino
destroy_timestamp(struct timestamp * t)128*86d7f5d3SJohn Marino void destroy_timestamp(struct timestamp *t)
129*86d7f5d3SJohn Marino {
130*86d7f5d3SJohn Marino if (t)
131*86d7f5d3SJohn Marino dm_free(t);
132*86d7f5d3SJohn Marino }
133