Blame lib/timer.h

Packit c22fc9
/*
Packit c22fc9
 * Soft:        Keepalived is a failover program for the LVS project
Packit c22fc9
 *              <www.linuxvirtualserver.org>. It monitor & manipulate
Packit c22fc9
 *              a loadbalanced server pool using multi-layer checks.
Packit c22fc9
 *
Packit c22fc9
 * Part:        timer.c include file.
Packit c22fc9
 *
Packit c22fc9
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
Packit c22fc9
 *
Packit c22fc9
 *              This program is distributed in the hope that it will be useful,
Packit c22fc9
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c22fc9
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit c22fc9
 *              See the GNU General Public License for more details.
Packit c22fc9
 *
Packit c22fc9
 *              This program is free software; you can redistribute it and/or
Packit c22fc9
 *              modify it under the terms of the GNU General Public License
Packit c22fc9
 *              as published by the Free Software Foundation; either version
Packit c22fc9
 *              2 of the License, or (at your option) any later version.
Packit c22fc9
 *
Packit c22fc9
 * Copyright (C) 2001-2017 Alexandre Cassen, <acassen@gmail.com>
Packit c22fc9
 */
Packit c22fc9
Packit c22fc9
#ifndef _TIMER_H
Packit c22fc9
#define _TIMER_H
Packit c22fc9
Packit c22fc9
#include <sys/time.h>
Packit c22fc9
#include <limits.h>
Packit c22fc9
#include <string.h>
Packit c22fc9
#include <stdbool.h>
Packit c22fc9
Packit c22fc9
typedef struct timeval timeval_t;
Packit c22fc9
Packit c22fc9
/* Global vars */
Packit c22fc9
extern timeval_t time_now;
Packit c22fc9
Packit c22fc9
#ifdef _TIMER_CHECK_
Packit Service dfccb1
extern bool do_timer_check;
Packit c22fc9
#endif
Packit c22fc9
Packit c22fc9
/* Some defines */
Packit Service dfccb1
#define TIMER_HZ		1000000
Packit Service dfccb1
#define TIMER_HZ_FLOAT		1000000.0F
Packit Service dfccb1
#define TIMER_HZ_DOUBLE		((double)1000000.0F)
Packit Service dfccb1
#define TIMER_CENTI_HZ		10000
Packit c22fc9
#define TIMER_MAX_SEC		1000U
Packit c22fc9
#define TIMER_NEVER		ULONG_MAX	/* Used with time intervals in TIMER_HZ units */
Packit c22fc9
#define TIMER_DISABLED		LONG_MIN	/* Value in timeval_t tv_sec */
Packit c22fc9
Packit Service dfccb1
#define	NSEC_PER_SEC		1000000000	/* nanoseconds per second. Avoids typos by having a definition */
Packit c22fc9
Packit c22fc9
#ifdef _TIMER_CHECK_
Packit Service dfccb1
#define timer_now()	timer_now_r((__FILE__), (__func__), (__LINE__))
Packit Service dfccb1
#define set_time_now()	set_time_now_r((__FILE__), (__func__), (__LINE__))
Packit c22fc9
#endif
Packit c22fc9
Packit c22fc9
#define RB_TIMER_CMP(obj)					\
Packit c22fc9
static inline int						\
Packit Service dfccb1
obj##_timer_cmp(const obj##_t *r1, const obj##_t *r2)		\
Packit c22fc9
{								\
Packit c22fc9
	if (r1->sands.tv_sec == TIMER_DISABLED) {		\
Packit c22fc9
		if (r2->sands.tv_sec == TIMER_DISABLED)		\
Packit c22fc9
			return 0;				\
Packit c22fc9
		return 1;					\
Packit c22fc9
	}							\
Packit c22fc9
								\
Packit c22fc9
	if (r2->sands.tv_sec == TIMER_DISABLED)			\
Packit c22fc9
		return -1;					\
Packit c22fc9
								\
Packit c22fc9
	if (r1->sands.tv_sec != r2->sands.tv_sec)		\
Packit c22fc9
		return r1->sands.tv_sec - r2->sands.tv_sec;	\
Packit c22fc9
								\
Packit c22fc9
	return r1->sands.tv_usec - r2->sands.tv_usec;		\
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/* timer sub from current time */
Packit c22fc9
static inline timeval_t
Packit c22fc9
timer_sub_now(timeval_t a)
Packit c22fc9
{
Packit c22fc9
	timersub(&a, &time_now, &a);
Packit c22fc9
Packit c22fc9
	return a;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/* timer add to current time */
Packit c22fc9
static inline timeval_t
Packit c22fc9
timer_add_now(timeval_t a)
Packit c22fc9
{
Packit c22fc9
	timeradd(&time_now, &a, &a);
Packit c22fc9
Packit c22fc9
	return a;
Packit c22fc9
}
Packit c22fc9
Packit Service dfccb1
/* Returns true if time a + diff_hz < time_now */
Packit Service dfccb1
static inline bool
Packit Service dfccb1
timer_cmp_now_diff(timeval_t a, unsigned long diff_hz)
Packit Service dfccb1
{
Packit Service dfccb1
	timeval_t b = { .tv_sec = diff_hz / TIMER_HZ, .tv_usec = diff_hz % TIMER_HZ };
Packit Service dfccb1
Packit Service dfccb1
	timeradd(&b, &a, &b);
Packit Service dfccb1
Packit Service dfccb1
	return !!timercmp(&b, &time_now, <);
Packit Service dfccb1
}
Packit Service dfccb1
Packit c22fc9
/* Return time as unsigned long */
Packit c22fc9
static inline unsigned long
Packit c22fc9
timer_long(timeval_t a)
Packit c22fc9
{
Packit c22fc9
	return (unsigned long)a.tv_sec * TIMER_HZ + (unsigned long)a.tv_usec;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
#ifdef _INCLUDE_UNUSED_CODE_
Packit c22fc9
/* print timer value */
Packit c22fc9
static inline void
Packit c22fc9
timer_dump(timeval_t a)
Packit c22fc9
{
Packit c22fc9
	printf("=> %lu (usecs)\n", timer_tol(a));
Packit c22fc9
}
Packit c22fc9
#endif
Packit c22fc9
Packit c22fc9
/* prototypes */
Packit c22fc9
#ifdef _TIMER_CHECK_
Packit c22fc9
extern timeval_t timer_now_r(const char *, const char *, int);
Packit c22fc9
extern timeval_t set_time_now_r(const char *, const char *, int);
Packit c22fc9
#else
Packit c22fc9
extern timeval_t timer_now(void);
Packit c22fc9
extern timeval_t set_time_now(void);
Packit c22fc9
#endif
Packit Service dfccb1
extern timeval_t timer_add_long(timeval_t, unsigned long) __attribute__((const));
Packit Service dfccb1
extern timeval_t timer_sub_long(timeval_t, unsigned long) __attribute__((const));
Packit c22fc9
Packit c22fc9
#endif