mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-21 13:34:40 +03:00
libdm: Add dm_timestamp functions.
This commit is contained in:
parent
a5491d3698
commit
a28fb37b9e
@ -1,5 +1,6 @@
|
||||
Version 1.02.104 -
|
||||
=================================
|
||||
Add dm_timestamp functions to libdevmapper.
|
||||
|
||||
Version 1.02.103 - 24th July 2015
|
||||
=================================
|
||||
|
@ -122,11 +122,6 @@ SOURCES =\
|
||||
uuid/uuid.c \
|
||||
zero/zero.c
|
||||
|
||||
ifeq ("@HAVE_REALTIME@", "yes")
|
||||
SOURCES +=\
|
||||
misc/timestamp.c
|
||||
endif
|
||||
|
||||
ifeq ("@LVM1@", "internal")
|
||||
SOURCES +=\
|
||||
format1/disk-rep.c \
|
||||
|
@ -1,129 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2006 Rackable Systems All rights reserved.
|
||||
*
|
||||
* This file is part of LVM2.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License v.2.1.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/*
|
||||
* Abstract out the time methods used so they can be adjusted later -
|
||||
* the results of these routines should stay in-core. This implementation
|
||||
* requires librt.
|
||||
*/
|
||||
|
||||
#include "lib.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "timestamp.h"
|
||||
|
||||
/*
|
||||
* The realtime section uses clock_gettime with the CLOCK_MONOTONIC
|
||||
* parameter to prevent issues with time warps
|
||||
*/
|
||||
#ifdef HAVE_REALTIME
|
||||
|
||||
#include <time.h>
|
||||
#include <bits/time.h>
|
||||
|
||||
struct timestamp {
|
||||
struct timespec t;
|
||||
};
|
||||
|
||||
struct timestamp *get_timestamp(void)
|
||||
{
|
||||
struct timestamp *ts = NULL;
|
||||
|
||||
if (!(ts = dm_malloc(sizeof(*ts))))
|
||||
return_NULL;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts->t)) {
|
||||
log_sys_error("clock_gettime", "get_timestamp");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
/* cmp_timestamp: Compare two timestamps
|
||||
*
|
||||
* Return: -1 if t1 is less than t2
|
||||
* 0 if t1 is equal to t2
|
||||
* 1 if t1 is greater than t2
|
||||
*/
|
||||
int cmp_timestamp(struct timestamp *t1, struct timestamp *t2)
|
||||
{
|
||||
if(t1->t.tv_sec < t2->t.tv_sec)
|
||||
return -1;
|
||||
if(t1->t.tv_sec > t2->t.tv_sec)
|
||||
return 1;
|
||||
|
||||
if(t1->t.tv_nsec < t2->t.tv_nsec)
|
||||
return -1;
|
||||
if(t1->t.tv_nsec > t2->t.tv_nsec)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else /* ! HAVE_REALTIME */
|
||||
|
||||
/*
|
||||
* The !realtime section just uses gettimeofday and is therefore subject
|
||||
* to ntp-type time warps - not sure if should allow that.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
struct timestamp {
|
||||
struct timeval t;
|
||||
};
|
||||
|
||||
struct timestamp *get_timestamp(void)
|
||||
{
|
||||
struct timestamp *ts = NULL;
|
||||
|
||||
if (!(ts = dm_malloc(sizeof(*ts))))
|
||||
return_NULL;
|
||||
|
||||
if (gettimeofday(&ts->t, NULL)) {
|
||||
log_sys_error("gettimeofday", "get_timestamp");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
/* cmp_timestamp: Compare two timestamps
|
||||
*
|
||||
* Return: -1 if t1 is less than t2
|
||||
* 0 if t1 is equal to t2
|
||||
* 1 if t1 is greater than t2
|
||||
*/
|
||||
int cmp_timestamp(struct timestamp *t1, struct timestamp *t2)
|
||||
{
|
||||
if(t1->t.tv_sec < t2->t.tv_sec)
|
||||
return -1;
|
||||
if(t1->t.tv_sec > t2->t.tv_sec)
|
||||
return 1;
|
||||
|
||||
if(t1->t.tv_usec < t2->t.tv_usec)
|
||||
return -1;
|
||||
if(t1->t.tv_usec > t2->t.tv_usec)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* HAVE_REALTIME */
|
||||
|
||||
void destroy_timestamp(struct timestamp *t)
|
||||
{
|
||||
dm_free(t);
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2006 Rackable Systems All rights reserved.
|
||||
*
|
||||
* This file is part of LVM2.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License v.2.1.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef _LVM_TIMESTAMP_H
|
||||
#define _LVM_TIMESTAMP_H
|
||||
|
||||
struct timestamp;
|
||||
|
||||
struct timestamp *get_timestamp(void);
|
||||
|
||||
/* cmp_timestamp: Compare two timestamps
|
||||
*
|
||||
* Return: -1 if t1 is less than t2
|
||||
* 0 if t1 is equal to t2
|
||||
* 1 if t1 is greater than t2
|
||||
*/
|
||||
int cmp_timestamp(struct timestamp *t1, struct timestamp *t2);
|
||||
|
||||
void destroy_timestamp(struct timestamp *t);
|
||||
|
||||
#endif /* _LVM_TIMESTAMP_H */
|
||||
|
@ -1 +1,6 @@
|
||||
dm_size_to_string
|
||||
dm_timestamp_alloc
|
||||
dm_timestamp_compare
|
||||
dm_timestamp_get
|
||||
dm_timestamp_delta
|
||||
dm_timestamp_destroy
|
||||
|
@ -25,6 +25,7 @@ SOURCES =\
|
||||
libdm-deptree.c \
|
||||
libdm-string.c \
|
||||
libdm-report.c \
|
||||
libdm-timestamp.c \
|
||||
libdm-config.c \
|
||||
mm/dbg_malloc.c \
|
||||
mm/pool.c \
|
||||
|
@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
|
||||
* Copyright (C) 2004-2014 Red Hat, Inc. All rights reserved.
|
||||
* Copyright (C) 2004-2015 Red Hat, Inc. All rights reserved.
|
||||
* Copyright (C) 2006 Rackable Systems All rights reserved.
|
||||
*
|
||||
* This file is part of the device-mapper userspace tools.
|
||||
*
|
||||
@ -1664,6 +1665,45 @@ typedef int32_t dm_percent_t;
|
||||
float dm_percent_to_float(dm_percent_t percent);
|
||||
dm_percent_t dm_make_percent(uint64_t numerator, uint64_t denominator);
|
||||
|
||||
/********************
|
||||
* timestamp handling
|
||||
********************/
|
||||
|
||||
struct dm_timestamp;
|
||||
|
||||
/*
|
||||
* Create a dm_timestamp object to use with dm_timestamp_get.
|
||||
*/
|
||||
struct dm_timestamp *dm_timestamp_alloc(void);
|
||||
|
||||
/*
|
||||
* Update dm_timestamp object to represent the current time.
|
||||
*/
|
||||
int dm_timestamp_get(struct dm_timestamp *ts);
|
||||
|
||||
/*
|
||||
* Compare two timestamps.
|
||||
*
|
||||
* Return: -1 if ts1 is less than ts2
|
||||
* 0 if ts1 is equal to ts2
|
||||
* 1 if ts1 is greater than ts2
|
||||
*/
|
||||
int dm_timestamp_compare(struct dm_timestamp *ts1, struct dm_timestamp *ts2);
|
||||
|
||||
/*
|
||||
* Return the absolute difference in nanoseconds between
|
||||
* the dm_timestamp objects ts1 and ts2.
|
||||
*
|
||||
* Callers that need to know whether ts1 is before, equal to, or after ts2
|
||||
* in addition to the magnitude should use dm_timestamp_compare.
|
||||
*/
|
||||
uint64_t dm_timestamp_delta(struct dm_timestamp *ts1, struct dm_timestamp *ts2);
|
||||
|
||||
/*
|
||||
* Destroy a dm_timestamp object.
|
||||
*/
|
||||
void dm_timestamp_destroy(struct dm_timestamp *ts);
|
||||
|
||||
/*********************
|
||||
* reporting functions
|
||||
*********************/
|
||||
|
170
libdm/libdm-timestamp.c
Normal file
170
libdm/libdm-timestamp.c
Normal file
@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 2006 Rackable Systems All rights reserved.
|
||||
* Copyright (C) 2015 Red Hat, Inc. All rights reserved.
|
||||
*
|
||||
* This file is part of the device-mapper userspace tools.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use,
|
||||
* modify, copy, or redistribute it subject to the terms and conditions
|
||||
* of the GNU Lesser General Public License v.2.1.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/*
|
||||
* Abstract out the time methods used so they can be adjusted later -
|
||||
* the results of these routines should stay in-core.
|
||||
*/
|
||||
|
||||
#include "dmlib.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NSEC_PER_USEC UINT64_C(1000)
|
||||
#define NSEC_PER_MSEC UINT64_C(1000000)
|
||||
#define NSEC_PER_SEC UINT64_C(1000000000)
|
||||
|
||||
/*
|
||||
* The realtime section uses clock_gettime with the CLOCK_MONOTONIC
|
||||
* parameter to prevent issues with time warps
|
||||
* This implementation requires librt.
|
||||
*/
|
||||
#ifdef HAVE_REALTIME
|
||||
|
||||
#include <time.h>
|
||||
#include <bits/time.h>
|
||||
|
||||
struct dm_timestamp {
|
||||
struct timespec t;
|
||||
};
|
||||
|
||||
static uint64_t _timestamp_to_uint64(struct dm_timestamp *ts)
|
||||
{
|
||||
uint64_t stamp = 0;
|
||||
|
||||
stamp += (uint64_t) ts->t.tv_sec * NSEC_PER_SEC;
|
||||
stamp += (uint64_t) ts->t.tv_nsec;
|
||||
|
||||
return stamp;
|
||||
}
|
||||
|
||||
struct dm_timestamp *dm_timestamp_alloc(void)
|
||||
{
|
||||
struct dm_timestamp *ts = NULL;
|
||||
|
||||
if (!(ts = dm_malloc(sizeof(*ts))))
|
||||
stack;
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
int dm_timestamp_get(struct dm_timestamp *ts)
|
||||
{
|
||||
if (!ts)
|
||||
return 0;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts->t)) {
|
||||
log_sys_error("clock_gettime", "get_timestamp");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else /* ! HAVE_REALTIME */
|
||||
|
||||
/*
|
||||
* The !realtime section just uses gettimeofday and is therefore subject
|
||||
* to ntp-type time warps - not sure if should allow that.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
struct dm_timestamp {
|
||||
struct timeval t;
|
||||
};
|
||||
|
||||
static uint64_t _timestamp_to_uint64(struct dm_timestamp *ts)
|
||||
{
|
||||
uint64_t stamp = 0;
|
||||
|
||||
stamp += ts->t.tv_sec * NSEC_PER_SEC;
|
||||
stamp += ts->t.tv_usec * NSEC_PER_USEC;
|
||||
|
||||
return stamp;
|
||||
}
|
||||
|
||||
struct dm_timestamp *dm_timestamp_alloc(void)
|
||||
{
|
||||
struct dm_timestamp *ts;
|
||||
|
||||
if (!(ts = dm_malloc(sizeof(*ts))))
|
||||
stack;
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
int dm_timestamp_get(struct dm_timestamp *ts)
|
||||
{
|
||||
if (!ts)
|
||||
return 0;
|
||||
|
||||
if (gettimeofday(&ts->t, NULL)) {
|
||||
log_sys_error("gettimeofday", "get_timestamp");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif /* HAVE_REALTIME */
|
||||
|
||||
/*
|
||||
* Compare two timestamps.
|
||||
*
|
||||
* Return: -1 if ts1 is less than ts2
|
||||
* 0 if ts1 is equal to ts2
|
||||
* 1 if ts1 is greater than ts2
|
||||
*/
|
||||
int dm_timestamp_compare(struct dm_timestamp *ts1, struct dm_timestamp *ts2)
|
||||
{
|
||||
uint64_t t1, t2;
|
||||
|
||||
t1 = _timestamp_to_uint64(ts1);
|
||||
t2 = _timestamp_to_uint64(ts2);
|
||||
|
||||
if (t2 < t1)
|
||||
return 1;
|
||||
|
||||
if (t1 < t2)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the absolute difference in nanoseconds between
|
||||
* the dm_timestamp objects ts1 and ts2.
|
||||
*
|
||||
* Callers that need to know whether ts1 is before, equal to, or after ts2
|
||||
* in addition to the magnitude should use dm_timestamp_compare.
|
||||
*/
|
||||
uint64_t dm_timestamp_delta(struct dm_timestamp *ts1, struct dm_timestamp *ts2)
|
||||
{
|
||||
uint64_t t1, t2;
|
||||
|
||||
t1 = _timestamp_to_uint64(ts1);
|
||||
t2 = _timestamp_to_uint64(ts2);
|
||||
|
||||
if (t1 > t2)
|
||||
return t1 - t2;
|
||||
|
||||
return t2 - t1;
|
||||
}
|
||||
|
||||
void dm_timestamp_destroy(struct dm_timestamp *ts)
|
||||
{
|
||||
dm_free(ts);
|
||||
}
|
Loading…
Reference in New Issue
Block a user