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