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