|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/uds-releases/jasper/src/uds/timeUtils.h#5 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef TIME_UTILS_H
|
|
Packit Service |
310c69 |
#define TIME_UTILS_H
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "compiler.h"
|
|
Packit Service |
310c69 |
#include "typeDefs.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifdef __KERNEL__
|
|
Packit Service |
310c69 |
#include <linux/ktime.h>
|
|
Packit Service |
310c69 |
#include <linux/time.h>
|
|
Packit Service |
310c69 |
#else
|
|
Packit Service |
310c69 |
#include <sys/time.h>
|
|
Packit Service |
310c69 |
#include <time.h>
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
// Absolute time.
|
|
Packit Service |
310c69 |
#ifdef __KERNEL__
|
|
Packit Service |
310c69 |
typedef int64_t AbsTime;
|
|
Packit Service |
310c69 |
#else
|
|
Packit Service |
310c69 |
typedef struct timespec AbsTime;
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
// Relative time, the length of a time interval, or the difference between
|
|
Packit Service |
310c69 |
// two times. A signed 64-bit number of nanoseconds.
|
|
Packit Service |
310c69 |
typedef int64_t RelTime;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef __KERNEL__
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Return true if the time is valid.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param time a time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return true if the time is valid
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @note an invalid time is generally returned from a failed attempt
|
|
Packit Service |
310c69 |
* to get the time from the system
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
bool isValidTime(AbsTime time);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Return the current time according to the specified clock type.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param clock Either CLOCK_REALTIME or CLOCK_MONOTONIC
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the current time according to the clock in question
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @note the precision of the clock is system specific
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
#ifdef __KERNEL__
|
|
Packit Service |
310c69 |
static INLINE AbsTime currentTime(clockid_t clock)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
// clock is always a constant, so gcc reduces this to a single call
|
|
Packit Service |
310c69 |
return clock == CLOCK_MONOTONIC ? ktime_get_ns() : ktime_get_real_ns();
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
#else
|
|
Packit Service |
310c69 |
AbsTime currentTime(clockid_t clock);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef __KERNEL__
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Return the timestamp a certain number of nanoseconds in the future.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param clock Either CLOCK_REALTIME or CLOCK_MONOTONIC
|
|
Packit Service |
310c69 |
* @param reltime The relative time to the clock value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the timestamp for that time (potentially rounded to the next
|
|
Packit Service |
310c69 |
* representable instant for the system in question)
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
AbsTime futureTime(clockid_t clock, RelTime reltime);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Return the difference between two timestamps.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param a A time
|
|
Packit Service |
310c69 |
* @param b Another time, based on the same clock as a.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the relative time between the two timestamps
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
#ifdef __KERNEL__
|
|
Packit Service |
310c69 |
static INLINE RelTime timeDifference(AbsTime a, AbsTime b)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return a - b;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
#else
|
|
Packit Service |
310c69 |
RelTime timeDifference(AbsTime a, AbsTime b);
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert seconds to a RelTime value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param seconds A number of seconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of seconds as a RelTime
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE RelTime secondsToRelTime(int64_t seconds)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return (RelTime) seconds * (1000 * 1000 * 1000);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert milliseconds to a RelTime value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param milliseconds A number of milliseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of milliseconds as a RelTime
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE RelTime millisecondsToRelTime(int64_t milliseconds)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return (RelTime) milliseconds * (1000 * 1000);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert microseconds to a RelTime value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param microseconds A number of microseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of microseconds as a RelTime
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE RelTime microsecondsToRelTime(int64_t microseconds)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return (RelTime) microseconds * 1000;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert nanoseconds to a RelTime value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param nanoseconds A number of nanoseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of nanoseconds as a RelTime
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE RelTime nanosecondsToRelTime(int64_t nanoseconds)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return (RelTime) nanoseconds;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert a RelTime value to milliseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param reltime The relative time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of milliseconds
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE int64_t relTimeToSeconds(RelTime reltime)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return reltime / (1000 * 1000 * 1000);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert a RelTime value to milliseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param reltime The relative time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of milliseconds
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE int64_t relTimeToMilliseconds(RelTime reltime)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return reltime / (1000 * 1000);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert a RelTime value to microseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param reltime The relative time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of microseconds
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE int64_t relTimeToMicroseconds(RelTime reltime)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return reltime / 1000;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert a RelTime value to nanoseconds
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param reltime The relative time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the equivalent number of nanoseconds
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE int64_t relTimeToNanoseconds(RelTime reltime)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return reltime;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Return the wall clock time in microseconds. The actual value is time
|
|
Packit Service |
310c69 |
* since the epoch (see "man gettimeofday"), but the typical use is to call
|
|
Packit Service |
310c69 |
* this twice and compute the difference, giving the elapsed time between
|
|
Packit Service |
310c69 |
* the two calls.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the time in microseconds
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
uint64_t nowUsec(void) __attribute__((warn_unused_result));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert from an AbsTime to a time_t
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param time an AbsTime time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return a time_t time
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE time_t asTimeT(AbsTime time)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#ifdef __KERNEL__
|
|
Packit Service |
310c69 |
return time / 1000000000;
|
|
Packit Service |
310c69 |
#else
|
|
Packit Service |
310c69 |
return time.tv_sec;
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert from a time_t to an AbsTime,
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param time a time_t time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return an AbsTime time
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE AbsTime fromTimeT(time_t time)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
#ifdef __KERNEL__
|
|
Packit Service |
310c69 |
return time * 1000000000;
|
|
Packit Service |
310c69 |
#else
|
|
Packit Service |
310c69 |
AbsTime abs;
|
|
Packit Service |
310c69 |
abs.tv_sec = time;
|
|
Packit Service |
310c69 |
abs.tv_nsec = 0;
|
|
Packit Service |
310c69 |
return abs;
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef __KERNEL__
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert from an AbsTime to a struct timespec
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param time an AbsTime time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return a time_t time
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE struct timespec asTimeSpec(AbsTime time)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return time;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef __KERNEL__
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Convert from an AbsTime to a struct timeval
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param time an AbsTime time
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return a time_t time
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
static INLINE struct timeval asTimeVal(AbsTime time)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
struct timeval tv = { time.tv_sec, time.tv_nsec / 1000 };
|
|
Packit Service |
310c69 |
return tv;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
#endif
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#endif /* TIME_UTILS_H */
|