1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* Soft: Keepalived is a failover program for the LVS project
* <www.linuxvirtualserver.org>. It monitor & manipulate
* a loadbalanced server pool using multi-layer checks.
*
* Part: Timer manipulations.
*
* Version: $Id: timer.c,v 1.1.15 2007/09/15 04:07:41 acassen Exp $
*
* Author: Alexandre Cassen, <acassen@linux-vs.org>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Copyright (C) 2001-2007 Alexandre Cassen, <acassen@freebox.fr>
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "timer.h"
/* time_now holds current time */
TIMEVAL time_now = { tv_sec: 0, tv_usec: 0 };
/* set a timer to a specific value */
TIMEVAL
timer_dup(TIMEVAL b)
{
TIMEVAL a;
TIMER_RESET(a);
a.tv_sec = b.tv_sec;
a.tv_usec = b.tv_usec;
return a;
}
/* timer compare */
int
timer_cmp(TIMEVAL a, TIMEVAL b)
{
if (a.tv_sec > b.tv_sec)
return 1;
if (a.tv_sec < b.tv_sec)
return -1;
if (a.tv_usec > b.tv_usec)
return 1;
if (a.tv_usec < b.tv_usec)
return -1;
return 0;
}
/* timer sub */
TIMEVAL
timer_sub(TIMEVAL a, TIMEVAL b)
{
TIMEVAL ret;
TIMER_RESET(ret);
ret.tv_usec = a.tv_usec - b.tv_usec;
ret.tv_sec = a.tv_sec - b.tv_sec;
if (ret.tv_usec < 0) {
ret.tv_usec += TIMER_HZ;
ret.tv_sec--;
}
return ret;
}
/* timer add */
TIMEVAL
timer_add_long(TIMEVAL a, long b)
{
TIMEVAL ret;
TIMER_RESET(ret);
ret.tv_usec = a.tv_usec + b % TIMER_HZ;
ret.tv_sec = a.tv_sec + b / TIMER_HZ;
if (ret.tv_usec >= TIMER_HZ) {
ret.tv_sec++;
ret.tv_usec -= TIMER_HZ;
}
return ret;
}
/* current time */
TIMEVAL
timer_now(void)
{
TIMEVAL curr_time;
int old_errno = errno;
/* init timer */
TIMER_RESET(curr_time);
gettimeofday(&curr_time, NULL);
errno = old_errno;
return curr_time;
}
/* sets and returns current time from system time */
TIMEVAL
set_time_now(void)
{
int old_errno = errno;
/* init timer */
TIMER_RESET(time_now);
gettimeofday(&time_now, NULL);
errno = old_errno;
return time_now;
}
/* timer sub from current time */
TIMEVAL
timer_sub_now(TIMEVAL a)
{
return timer_sub(time_now, a);
}
/* print timer value */
void
timer_dump(TIMEVAL a)
{
unsigned long timer;
timer = a.tv_sec * TIMER_HZ + a.tv_usec;
printf("=> %lu (usecs)\n", timer);
}
unsigned long
timer_tol(TIMEVAL a)
{
unsigned long timer;
timer = a.tv_sec * TIMER_HZ + a.tv_usec;
return timer;
}
|