#ifndef UCS_TIME_H
#define UCS_TIME_H
#include <ucs/arch/cpu.h>
#include <ucs/time/time_def.h>
#include <sys/time.h>
#include <limits.h>
BEGIN_C_DECLS
typedef uint32_t ucs_short_time_t;
#define UCS_SHORT_TIME_CMP UCS_CIRCULAR_COMPARE32
#define UCS_TIME_INFINITY ULLONG_MAX
#define UCS_TIME_AUTO (UCS_TIME_INFINITY - 1)
#define UCS_MSEC_PER_SEC 1000ull
#define UCS_USEC_PER_SEC 1000000ul
#define UCS_NSEC_PER_SEC 1000000000ul
#define UCS_NSEC_PER_USEC (UCS_NSEC_PER_SEC / UCS_USEC_PER_SEC)
double ucs_get_cpu_clocks_per_sec();
static inline ucs_time_t ucs_get_time()
{
return (ucs_time_t)ucs_arch_read_hres_clock();
}
static inline double ucs_get_accurate_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (tv.tv_usec / (double)UCS_USEC_PER_SEC);
}
static inline double ucs_time_sec_value()
{
return ucs_get_cpu_clocks_per_sec();
}
static inline ucs_time_t ucs_time_from_sec(double sec)
{
return (ucs_time_t)(sec * ucs_time_sec_value() + 0.5);
}
static inline ucs_time_t ucs_time_from_msec(double msec)
{
return ucs_time_from_sec(msec / UCS_MSEC_PER_SEC);
}
static inline ucs_time_t ucs_time_from_usec(double usec)
{
return ucs_time_from_sec(usec / UCS_USEC_PER_SEC);
}
static inline double ucs_time_to_sec(ucs_time_t t)
{
return t / ucs_time_sec_value();
}
static inline double ucs_time_to_msec(ucs_time_t t)
{
return ucs_time_to_sec(t) * UCS_MSEC_PER_SEC;
}
static inline double ucs_time_to_usec(ucs_time_t t)
{
return ucs_time_to_sec(t) * UCS_USEC_PER_SEC;
}
static inline double ucs_time_to_nsec(ucs_time_t t)
{
return ucs_time_to_sec(t) * UCS_NSEC_PER_SEC;
}
static inline double ucs_time_interval_to_nsec(ucs_time_t t)
{
return ucs_time_to_sec(t * UCS_NSEC_PER_SEC);
}
static inline void ucs_sec_to_timeval(double seconds, struct timeval *tv)
{
int64_t usec = (int64_t)( (seconds * UCS_USEC_PER_SEC) + 0.5 );
tv->tv_sec = usec / UCS_USEC_PER_SEC;
tv->tv_usec = usec % UCS_USEC_PER_SEC;
}
static inline void ucs_sec_to_timespec(double seconds, struct timespec *ts)
{
int64_t nsec = (int64_t)( (seconds * UCS_NSEC_PER_SEC) + 0.5 );
ts->tv_sec = nsec / UCS_NSEC_PER_SEC;
ts->tv_nsec = nsec % UCS_NSEC_PER_SEC;
}
END_C_DECLS
#endif