1141963Swpaul/* $NetBSD$ */ 2141963Swpaul 3141963Swpaul/* 4141963Swpaul * Copyright (C) 2006 Rackable Systems All rights reserved. 5141963Swpaul * 6141963Swpaul * This file is part of LVM2. 7141963Swpaul * 8141963Swpaul * This copyrighted material is made available to anyone wishing to use, 9141963Swpaul * modify, copy, or redistribute it subject to the terms and conditions 10141963Swpaul * of the GNU Lesser General Public License v.2.1. 11141963Swpaul * 12141963Swpaul * You should have received a copy of the GNU Lesser General Public License 13141963Swpaul * along with this program; if not, write to the Free Software Foundation, 14141963Swpaul * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15141963Swpaul */ 16141963Swpaul 17141963Swpaul/* 18141963Swpaul * Abstract out the time methods used so they can be adjusted later - 19141963Swpaul * the results of these routines should stay in-core. This implementation 20141963Swpaul * requires librt. 21141963Swpaul */ 22141963Swpaul 23141963Swpaul#include "lib.h" 24141963Swpaul#include <stdlib.h> 25141963Swpaul 26141963Swpaul#include "timestamp.h" 27141963Swpaul 28141963Swpaul/* 29141963Swpaul * The realtime section uses clock_gettime with the CLOCK_MONOTONIC 30141963Swpaul * parameter to prevent issues with time warps 31141963Swpaul */ 32123474Swpaul#ifdef HAVE_REALTIME 33123474Swpaul 34123474Swpaul#include <time.h> 35141963Swpaul#include <bits/time.h> 36189488Sweongyo 37141963Swpaulstruct timestamp { 38123474Swpaul struct timespec t; 39123474Swpaul}; 40123474Swpaul 41189488Sweongyostruct timestamp *get_timestamp(void) 42141963Swpaul{ 43189488Sweongyo struct timestamp *ts = NULL; 44189488Sweongyo 45141963Swpaul if (!(ts = dm_malloc(sizeof(*ts)))) 46123474Swpaul return_NULL; 47123474Swpaul 48123474Swpaul if (clock_gettime(CLOCK_MONOTONIC, &ts->t)) { 49123474Swpaul log_sys_error("clock_gettime", "get_timestamp"); 50123474Swpaul return NULL; 51189488Sweongyo } 52189488Sweongyo 53189488Sweongyo return ts; 54189488Sweongyo} 55189488Sweongyo 56189488Sweongyo/* cmp_timestamp: Compare two timestamps 57189488Sweongyo * 58189488Sweongyo * Return: -1 if t1 is less than t2 59189488Sweongyo * 0 if t1 is equal to t2 60189488Sweongyo * 1 if t1 is greater than t2 61189488Sweongyo */ 62189488Sweongyoint cmp_timestamp(struct timestamp *t1, struct timestamp *t2) 63189488Sweongyo{ 64189488Sweongyo if(t1->t.tv_sec < t2->t.tv_sec) 65189488Sweongyo return -1; 66189488Sweongyo if(t1->t.tv_sec > t2->t.tv_sec) 67189488Sweongyo return 1; 68189488Sweongyo 69123474Swpaul if(t1->t.tv_nsec < t2->t.tv_nsec) 70123474Swpaul return -1; 71123474Swpaul if(t1->t.tv_nsec > t2->t.tv_nsec) 72123474Swpaul return 1; 73189488Sweongyo 74189488Sweongyo return 0; 75189488Sweongyo} 76189488Sweongyo 77189488Sweongyo#else /* ! HAVE_REALTIME */ 78189488Sweongyo 79189488Sweongyo/* 80189488Sweongyo * The !realtime section just uses gettimeofday and is therefore subject 81189488Sweongyo * to ntp-type time warps - not sure if should allow that. 82189488Sweongyo */ 83189488Sweongyo 84189488Sweongyo#include <sys/time.h> 85123474Swpaul 86123474Swpaulstruct timestamp { 87123474Swpaul struct timeval t; 88123474Swpaul}; 89123474Swpaul 90123474Swpaulstruct timestamp *get_timestamp(void) 91123474Swpaul{ 92123474Swpaul struct timestamp *ts = NULL; 93123474Swpaul 94123474Swpaul if (!(ts = dm_malloc(sizeof(*ts)))) 95123474Swpaul return_NULL; 96123474Swpaul 97189488Sweongyo if (gettimeofday(&ts->t, NULL)) { 98189488Sweongyo log_sys_error("gettimeofday", "get_timestamp"); 99123474Swpaul return NULL; 100123474Swpaul } 101123474Swpaul 102189488Sweongyo return ts; 103189488Sweongyo} 104189488Sweongyo 105189488Sweongyo/* cmp_timestamp: Compare two timestamps 106123474Swpaul * 107189488Sweongyo * Return: -1 if t1 is less than t2 108189488Sweongyo * 0 if t1 is equal to t2 109189488Sweongyo * 1 if t1 is greater than t2 110123474Swpaul */ 111123474Swpaulint cmp_timestamp(struct timestamp *t1, struct timestamp *t2) 112123474Swpaul{ 113189488Sweongyo if(t1->t.tv_sec < t2->t.tv_sec) 114189488Sweongyo return -1; 115189488Sweongyo if(t1->t.tv_sec > t2->t.tv_sec) 116189488Sweongyo return 1; 117189488Sweongyo 118189488Sweongyo if(t1->t.tv_usec < t2->t.tv_usec) 119189488Sweongyo return -1; 120189488Sweongyo if(t1->t.tv_usec > t2->t.tv_usec) 121123474Swpaul return 1; 122123474Swpaul 123123474Swpaul return 0; 124189488Sweongyo} 125189488Sweongyo 126189488Sweongyo#endif /* HAVE_REALTIME */ 127189488Sweongyo 128189488Sweongyovoid destroy_timestamp(struct timestamp *t) 129189488Sweongyo{ 130189488Sweongyo if (t) 131189488Sweongyo dm_free(t); 132123474Swpaul} 133123474Swpaul