Show misc_time.h syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* $Id: misc_time.h,v 1.18 2005/09/15 03:18:35 august Exp $ */
/**
* \file misc_time.h time related utilities
*
*/
#ifndef __MISC_TIME_H__
#define __MISC_TIME_H__
#include <sys/time.h>
/**
* \brief struct timeval comparison function, retval is similar to strcmp(3)
*/
int misc_tv_cmp(struct timeval *t1, struct timeval *t2);
/**
* \brief struct timeval subtraction: t1 -= t2: negative values OK
*/
void misc_tv_sub_neg(struct timeval *t1, const struct timeval *t2);
/**
* \brief struct timeval subtraction: t1 -= t2: negative values turned into 0
*/
int misc_tv_sub(struct timeval *t1, const struct timeval *t2);
/**
* \brief similar to tv_sub and tv_sub_neg, but these functions return number
* of microseconds instead of returning a timeval
*/
unsigned long misc_tv_offset(const struct timeval *late, const struct timeval *early);
long misc_tv_offset_neg(const struct timeval *late, const struct timeval *early);
int64_t misc_tv_offset_neg64(const struct timeval *late, const struct timeval *early);
/**
* \brief ...and, similar to tv_sub and tv_sub_neg, but MILLIseconds
*/
unsigned long misc_tv_offset_msec(const struct timeval *late, const struct timeval *early);
long misc_tv_offset_neg_msec(const struct timeval *late, const struct timeval *early);
/**
* simple function for computing how many msec since the last time
* something happened -- but with protection for the clock going
* backwards. if the result is negative, a very large positive number
* is returned, i.e. to make it look like the event happened far in
* the past so it'll happen again.
*/
unsigned long msec_since(const struct timeval *when);
/**
* \brief add two timevals: t1 += t2
*/
void misc_tv_add(struct timeval *t1, struct timeval *t2);
/**
* \brief add "ms" milliseconds or "us" microseconds to the timeval t1
*/
void misc_tv_addms(struct timeval *t1, long ms);
void misc_tv_addus(struct timeval *t1, long us);
int misc_tv_overlap(struct timeval *tv1, int ms_dur1, struct timeval *tv2, int ms_dur2,
struct timeval *end);
int misc_tv_overlap_us(struct timeval *tv1, int us_dur1, struct timeval *tv2, int us_dur2,
struct timeval *end);
/** convert a timeval to microseconds as a long */
long misc_tv_usec_l(struct timeval *t);
/** convert a timeval to seconds or milliseconds as a float */
float misc_tv_msec_f(struct timeval *t);
float misc_tv_sec_f(struct timeval *t);
/*
* Functions for printing strings. These functions are not
* threadsafe since they return pointers to internal buffers.
*/
/** print a number of seconds in human readable format, e.g. "5m 40s" */
char *misc_secs_to_str(int t);
/**
* \brief convert timeval to a date string like "Tue Jul 20 14:52:18 PDT 2004"
*
* this is basically an interface to asctime, but
* 1 -- it takes a timeval rather than an int
* 2 -- has a static buf ring so it can be called multiple times from
* printf without overwriting previous results
* 3 -- removes the trailing lf for you
*/
char *misc_print_date(const struct timeval *tv);
/** convert timeval to a localtime string like "Tue Jul 20 14:52:18 PDT 2004"
* where format in this example is "%a %b %e %T %Z %Y"
* see the strftime() function for more choices */
char *misc_print_formated_date(const struct timeval *tv, const char *format);
/** emit a timeval in format similar to "3412312.135533" */
char *misc_tv_to_str(const struct timeval *tv);
/* conversion from timeval to int64 */
void misc_int64_to_timeval(struct timeval *tv, int64_t index);
int64_t misc_timeval_to_int64(struct timeval *tv);
/* conversion from character string to a time-val */
void misc_str_to_tv(char* time_str, struct timeval* tv);
/*
* conversion between 64-bit int and struct timeval
*/
static inline
uint64_t tv_to_ll(const struct timeval *tv)
{
return ((uint64_t)tv->tv_sec * (uint64_t)MILLION_I) + (uint64_t)tv->tv_usec;
}
static inline
void ll_to_tv(struct timeval *tv, uint64_t ll)
{
tv->tv_sec = ll / (uint64_t)MILLION_I;
tv->tv_usec = ll % MILLION_I;
}
/*
* Time relative to start of program
*/
/* returns current elapsed time since start of program */
uint64_t misc_time_elapsed();
/* returns current elapsed time since start of system (from emrun) */
uint64_t misc_time_elapsed_sys();
/* resets the start time -- called by misc_init() */
void misc_time_set_start(struct timeval *tv);
void misc_time_get_start(struct timeval *tv);
int64_t misc_time_get_start64();
/* resets the sys start time -- called by emrun_init() */
void misc_time_set_sys_start(struct timeval *tv);
int64_t misc_time_get_sys_start64();
/* busywait loop.. */
void misc_busy_wait(int microseconds);
void misc_busy_wait_until(struct timeval *tv);
#endif /* #ifndef __MISC_TIME_H__ */
See more files for this project here