"Fossies" - the Fresh Open Source Software Archive

Member "haproxy-2.0.0/include/common/time.h" (16 Jun 2019, 20404 Bytes) of package /linux/misc/haproxy-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "time.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.9.8_vs_2.0.0.

    1 /*
    2  * include/common/time.h
    3  * Time calculation functions and macros.
    4  *
    5  * Copyright (C) 2000-2011 Willy Tarreau - w@1wt.eu
    6  *
    7  * This library is free software; you can redistribute it and/or
    8  * modify it under the terms of the GNU Lesser General Public
    9  * License as published by the Free Software Foundation, version 2.1
   10  * exclusively.
   11  *
   12  * This library is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15  * Lesser General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU Lesser General Public
   18  * License along with this library; if not, write to the Free Software
   19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   20  */
   21 
   22 #ifndef _COMMON_TIME_H
   23 #define _COMMON_TIME_H
   24 
   25 #include <inttypes.h>
   26 #include <stdlib.h>
   27 #include <unistd.h>
   28 #include <sys/time.h>
   29 #include <common/config.h>
   30 #include <common/hathreads.h>
   31 #include <common/standard.h>
   32 
   33 /* eternity when exprimed in timeval */
   34 #ifndef TV_ETERNITY
   35 #define TV_ETERNITY     (~0UL)
   36 #endif
   37 
   38 /* eternity when exprimed in ms */
   39 #ifndef TV_ETERNITY_MS
   40 #define TV_ETERNITY_MS  (-1)
   41 #endif
   42 
   43 #define TIME_ETERNITY   (TV_ETERNITY_MS)
   44 
   45 /* we want to be able to detect time jumps. Fix the maximum wait time to a low
   46  * value so that we know the time has changed if we wait longer.
   47  */
   48 #define MAX_DELAY_MS    1000
   49 
   50 
   51 /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
   52 #define MINTIME(old, new)   (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
   53 #define SETNOW(a)       (*a=now)
   54 
   55 extern THREAD_LOCAL unsigned int   curr_sec_ms;      /* millisecond of current second (0..999) */
   56 extern THREAD_LOCAL unsigned int   ms_left_scaled;   /* milliseconds left for current second (0..2^32-1) */
   57 extern THREAD_LOCAL unsigned int   curr_sec_ms_scaled;  /* millisecond of current second (0..2^32-1) */
   58 extern THREAD_LOCAL unsigned int   now_ms;           /* internal date in milliseconds (may wrap) */
   59 extern THREAD_LOCAL unsigned int   samp_time;        /* total elapsed time over current sample */
   60 extern THREAD_LOCAL unsigned int   idle_time;        /* total idle time over current sample */
   61 extern THREAD_LOCAL struct timeval now;              /* internal date is a monotonic function of real clock */
   62 extern THREAD_LOCAL struct timeval date;             /* the real current date */
   63 extern struct timeval start_date;       /* the process's start date */
   64 extern THREAD_LOCAL struct timeval before_poll;      /* system date before calling poll() */
   65 extern THREAD_LOCAL struct timeval after_poll;       /* system date after leaving poll() */
   66 
   67 
   68 /**** exported functions *************************************************/
   69 /*
   70  * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
   71  */
   72 REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
   73 
   74 /*
   75  * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
   76  * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
   77  */
   78 REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
   79 
   80 /*
   81  * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
   82  * assuming that TV_ETERNITY is greater than everything.
   83  */
   84 REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
   85 
   86 /**** general purpose functions and macros *******************************/
   87 
   88 
   89 /* tv_now: sets <tv> to the current time */
   90 REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
   91 {
   92     gettimeofday(tv, NULL);
   93     return tv;
   94 }
   95 
   96 /* tv_udpate_date: sets <date> to system time, and sets <now> to something as
   97  * close as possible to real time, following a monotonic function. The main
   98  * principle consists in detecting backwards and forwards time jumps and adjust
   99  * an offset to correct them. This function should be called only once after
  100  * each poll. The poll's timeout should be passed in <max_wait>, and the return
  101  * value in <interrupted> (a non-zero value means that we have not expired the
  102  * timeout).
  103  */
  104 REGPRM2 void tv_update_date(int max_wait, int interrupted);
  105 
  106 /*
  107  * sets a struct timeval to its highest value so that it can never happen
  108  * note that only tv_usec is necessary to detect it since a tv_usec > 999999
  109  * is normally not possible.
  110  */
  111 REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
  112 {
  113     tv->tv_sec  = (typeof(tv->tv_sec))TV_ETERNITY;
  114     tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY;
  115     return tv;
  116 }
  117 
  118 /*
  119  * sets a struct timeval to 0
  120  *
  121  */
  122 REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
  123     tv->tv_sec = tv->tv_usec = 0;
  124     return tv;
  125 }
  126 
  127 /*
  128  * returns non null if tv is [eternity], otherwise 0.
  129  */
  130 #define tv_iseternity(tv)       ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY)
  131 
  132 /*
  133  * returns 0 if tv is [eternity], otherwise non-zero.
  134  */
  135 #define tv_isset(tv)       ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY)
  136 
  137 /*
  138  * returns non null if tv is [0], otherwise 0.
  139  */
  140 #define tv_iszero(tv)           (((tv)->tv_sec | (tv)->tv_usec) == 0)
  141 
  142 /*
  143  * Converts a struct timeval to a number of milliseconds.
  144  */
  145 REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv)
  146 {
  147     unsigned long ret;
  148 
  149     ret  = tv->tv_sec * 1000;
  150     ret += tv->tv_usec / 1000;
  151     return ret;
  152 }
  153 
  154 /*
  155  * Converts a struct timeval to a number of milliseconds.
  156  */
  157 REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
  158 {
  159     tv->tv_sec = ms / 1000;
  160     tv->tv_usec = (ms % 1000) * 1000;
  161     return tv;
  162 }
  163 
  164 /* Return a number of 1024Hz ticks between 0 and 1023 for input number of
  165  * usecs between 0 and 999999. This function has been optimized to remove
  166  * any divide and multiply, as it is completely optimized away by the compiler
  167  * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
  168  * which is almost twice as low as a direct usec to ms conversion. This version
  169  * also has the benefit of returning 1024 for 1000000.
  170  */
  171 REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec)
  172 {
  173     return (usec * 1073 + 742516) >> 20;
  174 }
  175 
  176 
  177 /**** comparison functions and macros ***********************************/
  178 
  179 
  180 /* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
  181 REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
  182 {
  183     if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
  184         return -1;
  185     else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
  186         return 1;
  187     else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
  188         return -1;
  189     else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
  190         return 1;
  191     else
  192         return 0;
  193 }
  194 
  195 /* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
  196 #define tv_iseq __tv_iseq
  197 REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
  198 {
  199     return ((unsigned)tv1->tv_sec  == (unsigned)tv2->tv_sec) &&
  200         ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
  201 }
  202 
  203 /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
  204 #define tv_isgt _tv_isgt
  205 REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
  206 REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
  207 {
  208     return
  209         ((unsigned)tv1->tv_sec  == (unsigned)tv2->tv_sec) ?
  210         ((unsigned)tv1->tv_usec >  (unsigned)tv2->tv_usec) :
  211         ((unsigned)tv1->tv_sec  >  (unsigned)tv2->tv_sec);
  212 }
  213 
  214 /* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
  215 #define tv_isge __tv_isge
  216 REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
  217 {
  218     return
  219         ((unsigned)tv1->tv_sec  == (unsigned)tv2->tv_sec) ?
  220         ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
  221         ((unsigned)tv1->tv_sec  >  (unsigned)tv2->tv_sec);
  222 }
  223 
  224 /* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
  225 #define tv_islt __tv_islt
  226 REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
  227 {
  228     return
  229         ((unsigned)tv1->tv_sec  == (unsigned)tv2->tv_sec) ?
  230         ((unsigned)tv1->tv_usec <  (unsigned)tv2->tv_usec) :
  231         ((unsigned)tv1->tv_sec  <  (unsigned)tv2->tv_sec);
  232 }
  233 
  234 /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
  235 #define tv_isle _tv_isle
  236 REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
  237 REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
  238 {
  239     return
  240         ((unsigned)tv1->tv_sec  == (unsigned)tv2->tv_sec) ?
  241         ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
  242         ((unsigned)tv1->tv_sec  <  (unsigned)tv2->tv_sec);
  243 }
  244 
  245 /*
  246  * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
  247  * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
  248  */
  249 #define tv_ms_cmp _tv_ms_cmp
  250 REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
  251 REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
  252 {
  253     if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
  254         if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
  255             return -1;
  256         else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
  257             return 1;
  258         else
  259             return 0;
  260     }
  261     else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
  262          (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
  263           ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
  264         return -1;
  265     else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
  266          (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
  267           ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
  268         return 1;
  269     else
  270         return 0;
  271 }
  272 
  273 /*
  274  * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
  275  * assuming that TV_ETERNITY is greater than everything.
  276  */
  277 #define tv_ms_cmp2 _tv_ms_cmp2
  278 REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
  279 REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
  280 {
  281     if (tv_iseternity(tv1))
  282         if (tv_iseternity(tv2))
  283             return 0; /* same */
  284         else
  285             return 1; /* tv1 later than tv2 */
  286     else if (tv_iseternity(tv2))
  287         return -1; /* tv2 later than tv1 */
  288     return tv_ms_cmp(tv1, tv2);
  289 }
  290 
  291 /*
  292  * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
  293  * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
  294  * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
  295  * occurrences of (tv_ms_cmp2(tv,now) <= 0).
  296  */
  297 #define tv_ms_le2 _tv_ms_le2
  298 REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
  299 REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
  300 {
  301     if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
  302         return 0;
  303 
  304     if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
  305         return 1;
  306 
  307     if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
  308         if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
  309             return 1;
  310         else
  311             return 0;
  312     }
  313 
  314     if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
  315              ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
  316         return 0;
  317     else
  318         return 1;
  319 }
  320 
  321 
  322 /**** operators **********************************************************/
  323 
  324 
  325 /*
  326  * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
  327  * Must not be used when either argument is eternity.
  328  */
  329 #define tv_ms_elapsed __tv_ms_elapsed
  330 REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
  331 REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
  332 {
  333     unsigned long ret;
  334 
  335     ret  = ((signed long)(tv2->tv_sec  - tv1->tv_sec))  * 1000;
  336     ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
  337     return ret;
  338 }
  339 
  340 /*
  341  * returns the remaining time between tv1=now and event=tv2
  342  * if tv2 is passed, 0 is returned.
  343  * Must not be used when either argument is eternity.
  344  */
  345 
  346 #define tv_ms_remain __tv_ms_remain
  347 REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
  348 REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
  349 {
  350     if (tv_ms_cmp(tv1, tv2) >= 0)
  351         return 0; /* event elapsed */
  352 
  353     return __tv_ms_elapsed(tv1, tv2);
  354 }
  355 
  356 /*
  357  * returns the remaining time between tv1=now and event=tv2
  358  * if tv2 is passed, 0 is returned.
  359  * Returns TIME_ETERNITY if tv2 is eternity.
  360  */
  361 #define tv_ms_remain2 _tv_ms_remain2
  362 REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
  363 REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
  364 {
  365     if (tv_iseternity(tv2))
  366         return TIME_ETERNITY;
  367 
  368     return tv_ms_remain(tv1, tv2);
  369 }
  370 
  371 /*
  372  * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
  373  */
  374 #define tv_add _tv_add
  375 REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
  376 REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
  377 {
  378     tv->tv_usec = from->tv_usec + inc->tv_usec;
  379     tv->tv_sec  = from->tv_sec  + inc->tv_sec;
  380     if (tv->tv_usec >= 1000000) {
  381         tv->tv_usec -= 1000000;
  382         tv->tv_sec++;
  383     }
  384     return tv;
  385 }
  386 
  387 
  388 /*
  389  * If <inc> is set, then add it to <from> and set the result to <tv>, then
  390  * return 1, otherwise return 0. It is meant to be used in if conditions.
  391  */
  392 #define tv_add_ifset _tv_add_ifset
  393 REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
  394 REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
  395 {
  396     if (tv_iseternity(inc))
  397         return 0;
  398     tv->tv_usec = from->tv_usec + inc->tv_usec;
  399     tv->tv_sec  = from->tv_sec  + inc->tv_sec;
  400     if (tv->tv_usec >= 1000000) {
  401         tv->tv_usec -= 1000000;
  402         tv->tv_sec++;
  403     }
  404     return 1;
  405 }
  406 
  407 /*
  408  * adds <inc> to <tv> and returns a pointer <tv>
  409  */
  410 REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
  411 {
  412     tv->tv_usec += inc->tv_usec;
  413     tv->tv_sec  += inc->tv_sec;
  414     if (tv->tv_usec >= 1000000) {
  415         tv->tv_usec -= 1000000;
  416         tv->tv_sec++;
  417     }
  418     return tv;
  419 }
  420 
  421 
  422 /*
  423  * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
  424  * 0 is returned. The result is stored into tv.
  425  */
  426 #define tv_remain _tv_remain
  427 REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
  428 REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
  429 {
  430     tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
  431     tv->tv_sec  = tv2->tv_sec  - tv1->tv_sec;
  432     if ((signed)tv->tv_sec > 0) {
  433         if ((signed)tv->tv_usec < 0) {
  434             tv->tv_usec += 1000000;
  435             tv->tv_sec--;
  436         }
  437     } else if (tv->tv_sec == 0) {
  438         if ((signed)tv->tv_usec < 0)
  439             tv->tv_usec = 0;
  440     } else {
  441         tv->tv_sec = 0;
  442         tv->tv_usec = 0;
  443     }
  444     return tv;
  445 }
  446 
  447 
  448 /*
  449  * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
  450  * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
  451  * eternity.
  452  */
  453 #define tv_remain2 _tv_remain2
  454 REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
  455 REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
  456 {
  457     if (tv_iseternity(tv2))
  458         return tv_eternity(tv);
  459     return __tv_remain(tv1, tv2, tv);
  460 }
  461 
  462 
  463 /*
  464  * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
  465  */
  466 #define tv_ms_add _tv_ms_add
  467 REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
  468 REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
  469 {
  470     tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
  471     tv->tv_sec  = from->tv_sec  + (ms / 1000);
  472     while (tv->tv_usec >= 1000000) {
  473         tv->tv_usec -= 1000000;
  474         tv->tv_sec++;
  475     }
  476     return tv;
  477 }
  478 
  479 
  480 /*
  481  * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
  482  * This should be very fast because it's used in schedulers.
  483  * It has been optimized to return 1  (so call it in a loop which continues
  484  * as long as tv1<=tv2)
  485  */
  486 
  487 #define tv_isbefore(tv1, tv2)                                               \
  488     (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 :  \
  489      (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
  490       unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
  491 
  492 /*
  493  * returns the first event between <tv1> and <tv2> into <tvmin>.
  494  * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
  495  * to be the same as <tv1> or <tv2>, it is recommended to use
  496  * tv_bound instead.
  497  */
  498 #define tv_min(tvmin, tv1, tv2) ({      \
  499         if (tv_isbefore(tv1, tv2)) {    \
  500                 *tvmin = *tv1;          \
  501         }                               \
  502         else {                          \
  503                 *tvmin = *tv2;          \
  504         }                               \
  505         tvmin;                          \
  506 })
  507 
  508 /*
  509  * returns the first event between <tv1> and <tv2> into <tvmin>.
  510  * a zero tv is ignored. <tvmin> is returned. This function has been
  511  * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
  512  */
  513 #define tv_bound(tv1, tv2) ({      \
  514         if (tv_isbefore(tv2, tv1)) \
  515                   *tv1 = *tv2;     \
  516         tv1;                       \
  517 })
  518 
  519 /* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
  520 static inline uint64_t now_mono_time()
  521 {
  522 #if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
  523     struct timespec ts;
  524     clock_gettime(CLOCK_MONOTONIC, &ts);
  525     return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  526 #else
  527     return 0;
  528 #endif
  529 }
  530 
  531 /* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
  532 static inline uint64_t now_cpu_time()
  533 {
  534 #if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
  535     struct timespec ts;
  536     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
  537     return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  538 #else
  539     return 0;
  540 #endif
  541 }
  542 
  543 /* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
  544 static inline uint64_t now_cpu_time_thread(const struct thread_info *thr)
  545 {
  546 #if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
  547     struct timespec ts;
  548     clock_gettime(thr->clock_id, &ts);
  549     return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  550 #else
  551     return 0;
  552 #endif
  553 }
  554 
  555 /* Update the idle time value twice a second, to be called after
  556  * tv_update_date() when called after poll(). It relies on <before_poll> to be
  557  * updated to the system time before calling poll().
  558  */
  559 static inline void measure_idle()
  560 {
  561     /* Let's compute the idle to work ratio. We worked between after_poll
  562      * and before_poll, and slept between before_poll and date. The idle_pct
  563      * is updated at most twice every second. Note that the current second
  564      * rarely changes so we avoid a multiply when not needed.
  565      */
  566     int delta;
  567 
  568     if ((delta = date.tv_sec - before_poll.tv_sec))
  569         delta *= 1000000;
  570     idle_time += delta + (date.tv_usec - before_poll.tv_usec);
  571 
  572     if ((delta = date.tv_sec - after_poll.tv_sec))
  573         delta *= 1000000;
  574     samp_time += delta + (date.tv_usec - after_poll.tv_usec);
  575 
  576     after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
  577     if (samp_time < 500000)
  578         return;
  579 
  580     ti->idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
  581     idle_time = samp_time = 0;
  582 }
  583 
  584 /* Collect date and time information before calling poll(). This will be used
  585  * to count the run time of the past loop and the sleep time of the next poll.
  586  */
  587 static inline void tv_entering_poll()
  588 {
  589     gettimeofday(&before_poll, NULL);
  590 }
  591 
  592 /* Collect date and time information after leaving poll(). <timeout> must be
  593  * set to the maximum sleep time passed to poll (in milliseconds), and
  594  * <interrupted> must be zero if the poller reached the timeout or non-zero
  595  * otherwise, which generally is provided by the poller's return value.
  596  */
  597 static inline void tv_leaving_poll(int timeout, int interrupted)
  598 {
  599     measure_idle();
  600     ti->prev_cpu_time  = now_cpu_time();
  601     ti->prev_mono_time = now_mono_time();
  602 }
  603 
  604 #endif /* _COMMON_TIME_H */
  605 
  606 /*
  607  * Local variables:
  608  *  c-indent-level: 8
  609  *  c-basic-offset: 8
  610  * End:
  611  */