[6.1] 7891dbc3d typedefs for real/mono time and durations

hermunn hermunn at varnish-software.com
Wed Oct 24 09:29:22 UTC 2018


commit 7891dbc3dc23af1b48cbbc6ac6defdb39160155b
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Oct 8 10:19:50 2018 +0200

    typedefs for real/mono time and durations
    
    We use double for all time representations, yet monotonic time,
    real time and durations are not to be confused.
    
    Also, we might want to change the representation of time in the
    future.
    
    To get an implicit documentation of the semantic type of a time
    value and to facilitate working on the latter, we start off by
    introducing simple typedefs which neither inply any change nor
    offer any type checking. But they pave the way...

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index e8dc2f3de..ba91f2f0f 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -251,7 +251,7 @@ struct worker {
 
 	struct pool_task	task;
 
-	double			lastused;
+	vtim_real		lastused;
 	int			strangelove;
 
 	struct v1l		*v1l;
@@ -413,9 +413,9 @@ struct busyobj {
 #include "tbl/bo_flags.h"
 
 	/* Timeouts */
-	double			connect_timeout;
-	double			first_byte_timeout;
-	double			between_bytes_timeout;
+	vtim_dur		connect_timeout;
+	vtim_dur		first_byte_timeout;
+	vtim_dur		between_bytes_timeout;
 
 	/* Timers */
 	double			t_first;	/* First timestamp logged */
@@ -564,9 +564,8 @@ struct sess {
 
 	struct ws		ws[1];
 
-	/* Timestamps, all on TIM_real() timescale */
-	double			t_open;		/* fd accepted */
-	double			t_idle;		/* fd accepted or resp sent */
+	vtim_real		t_open;		/* fd accepted */
+	vtim_real		t_idle;		/* fd accepted or resp sent */
 
 };
 
@@ -662,7 +661,7 @@ int Lck__Owned(const struct lock *lck);
 
 /* public interface: */
 void Lck_Delete(struct lock *lck);
-int Lck_CondWait(pthread_cond_t *cond, struct lock *lck, double);
+int Lck_CondWait(pthread_cond_t *cond, struct lock *lck, vtim_real);
 
 #define Lck_New(a, b) Lck__New(a, b, #b)
 #define Lck_Lock(a) Lck__Lock(a, __func__, __LINE__)
diff --git a/bin/varnishd/cache/cache_acceptor.c b/bin/varnishd/cache/cache_acceptor.c
index 2b920720a..c0d44850f 100644
--- a/bin/varnishd/cache/cache_acceptor.c
+++ b/bin/varnishd/cache/cache_acceptor.c
@@ -52,7 +52,7 @@
 #include "vtim.h"
 
 static pthread_t	VCA_thread;
-static double vca_pace = 0.0;
+static vtim_dur vca_pace = 0.0;
 static struct lock pace_mtx;
 static unsigned pool_accepting;
 static pthread_mutex_t shut_mtx = PTHREAD_MUTEX_INITIALIZER;
@@ -137,9 +137,9 @@ static unsigned		need_test;
  * into the vca_acct() loop which we are running anyway
  */
 static void
-vca_periodic(double t0)
+vca_periodic(vtim_real t0)
 {
-	double now;
+	vtim_real now;
 
 	now = VTIM_real();
 	VSC_C_main->uptime = (uint64_t)(now - t0);
@@ -270,7 +270,7 @@ vca_tcp_opt_set(const int sock, const unsigned uds, const int force)
 static void
 vca_pace_check(void)
 {
-	double p;
+	vtim_dur p;
 
 	if (vca_pace == 0.0)
 		return;
@@ -598,7 +598,7 @@ static void * v_matchproto_()
 vca_acct(void *arg)
 {
 	struct listen_sock *ls;
-	double t0;
+	vtim_real t0;
 
 	// XXX Actually a mis-nomer now because the accept happens in a pool
 	// thread. Rename to accept-nanny or so?
diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 9f08c2385..c714af99f 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -114,7 +114,7 @@ vbe_dir_getfd(struct worker *wrk, struct backend *bp, struct busyobj *bo,
 {
 	struct pfd *pfd;
 	int *fdp, err;
-	double tmod;
+	vtim_dur tmod;
 	char abuf1[VTCP_ADDRBUFSIZE], abuf2[VTCP_ADDRBUFSIZE];
 	char pbuf1[VTCP_PORTBUFSIZE], pbuf2[VTCP_PORTBUFSIZE];
 
@@ -619,7 +619,7 @@ void
 VBE_Poll(void)
 {
 	struct backend *be, *be2;
-	double now = VTIM_real();
+	vtim_real now = VTIM_real();
 
 	ASSERT_CLI();
 	Lck_Lock(&backends_mtx);
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index f5c016860..796be9b32 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -66,7 +66,7 @@ struct backend {
 
 	VCL_BACKEND		director;
 
-	double			cooled;
+	vtim_real		cooled;
 };
 
 /*---------------------------------------------------------------------
diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index 1b1190ebd..899847c81 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -76,11 +76,11 @@ struct vbp_target {
 #define BITMAP(n, c, t, b)	uintmax_t	n;
 #include "tbl/backend_poll.h"
 
-	double				last;
-	double				avg;
+	vtim_dur			last;
+	vtim_dur			avg;
 	double				rate;
 
-	double				due;
+	vtim_real			due;
 	int				running;
 	int				heap_idx;
 	struct pool_task		task;
@@ -278,7 +278,7 @@ static void
 vbp_poke(struct vbp_target *vt)
 {
 	int s, tmo, i, proxy_header, err;
-	double t_start, t_now, t_end;
+	vtim_real t_start, t_now, t_end;
 	unsigned rlen, resp;
 	char buf[8192], *p;
 	struct pollfd pfda[1], *pfd = pfda;
@@ -454,7 +454,7 @@ vbp_task(struct worker *wrk, void *priv)
 static void * v_matchproto_(bgthread_t)
 vbp_thread(struct worker *wrk, void *priv)
 {
-	double now, nxt;
+	vtim_real now, nxt;
 	struct vbp_target *vt;
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
diff --git a/bin/varnishd/cache/cache_lck.c b/bin/varnishd/cache/cache_lck.c
index 6fa816a68..ad182c4b0 100644
--- a/bin/varnishd/cache/cache_lck.c
+++ b/bin/varnishd/cache/cache_lck.c
@@ -187,7 +187,7 @@ Lck__Owned(const struct lock *lck)
 }
 
 int v_matchproto_()
-Lck_CondWait(pthread_cond_t *cond, struct lock *lck, double when)
+Lck_CondWait(pthread_cond_t *cond, struct lock *lck, vtim_real when)
 {
 	struct ilck *ilck;
 	struct timespec ts;
diff --git a/bin/varnishd/cache/cache_tcp_pool.c b/bin/varnishd/cache/cache_tcp_pool.c
index f33668b2f..5a04eb5aa 100644
--- a/bin/varnishd/cache/cache_tcp_pool.c
+++ b/bin/varnishd/cache/cache_tcp_pool.c
@@ -540,7 +540,7 @@ VCP_Get(struct conn_pool *cp, double tmo, struct worker *wrk,
  */
 
 static int
-VCP_Wait(struct worker *wrk, struct pfd *pfd, double tmo)
+VCP_Wait(struct worker *wrk, struct pfd *pfd, vtim_real tmo)
 {
 	struct conn_pool *cp;
 	int r;
@@ -834,7 +834,7 @@ VTP_Close(struct pfd **pfdp)
  */
 
 struct pfd *
-VTP_Get(struct tcp_pool *tp, double tmo, struct worker *wrk,
+VTP_Get(struct tcp_pool *tp, vtim_dur tmo, struct worker *wrk,
 	unsigned force_fresh, int *err)
 {
 
@@ -845,7 +845,7 @@ VTP_Get(struct tcp_pool *tp, double tmo, struct worker *wrk,
  */
 
 int
-VTP_Wait(struct worker *wrk, struct pfd *pfd, double tmo)
+VTP_Wait(struct worker *wrk, struct pfd *pfd, vtim_real tmo)
 {
 	return (VCP_Wait(wrk, pfd, tmo));
 }
diff --git a/bin/varnishd/cache/cache_tcp_pool.h b/bin/varnishd/cache/cache_tcp_pool.h
index 7c25afda2..57564ba96 100644
--- a/bin/varnishd/cache/cache_tcp_pool.h
+++ b/bin/varnishd/cache/cache_tcp_pool.h
@@ -88,14 +88,14 @@ void VTP_Recycle(const struct worker *, struct pfd **);
 	 * Recycle an open connection.
 	 */
 
-struct pfd *VTP_Get(struct tcp_pool *, double tmo, struct worker *,
+struct pfd *VTP_Get(struct tcp_pool *, vtim_dur tmo, struct worker *,
     unsigned force_fresh, int *err);
 	/*
 	 * Get a (possibly) recycled connection.
 	 * errno will be stored in err
 	 */
 
-int VTP_Wait(struct worker *, struct pfd *, double tmo);
+int VTP_Wait(struct worker *, struct pfd *, vtim_real tmo);
 	/*
 	 * If the connection was recycled (state != VTP_STATE_USED) call this
 	 * function before attempting to receive on the connection.
diff --git a/include/vdef.h b/include/vdef.h
index 185265794..3a5e40329 100644
--- a/include/vdef.h
+++ b/include/vdef.h
@@ -165,3 +165,8 @@
 #   define ___Static_assert(x, y) \
 		typedef char __assert_## y[(x) ? 1 : -1] v_unused_
 #endif
+
+/* VTIM API overhaul WIP */
+typedef double vtim_mono;
+typedef double vtim_real;
+typedef double vtim_dur;
diff --git a/lib/libvarnish/vtim.c b/lib/libvarnish/vtim.c
index c774e2ab8..14b9752d6 100644
--- a/lib/libvarnish/vtim.c
+++ b/lib/libvarnish/vtim.c
@@ -124,7 +124,7 @@ init(void)
  * gethrtime(), which is the case on modern Solaris descendents.
  */
 
-double
+vtim_mono
 VTIM_mono(void)
 {
 #if defined(HAVE_CLOCK_GETTIME) && !defined(USE_GETHRTIME)
@@ -143,7 +143,7 @@ VTIM_mono(void)
 #endif
 }
 
-double
+vtim_real
 VTIM_real(void)
 {
 #ifdef HAVE_CLOCK_GETTIME
@@ -160,7 +160,7 @@ VTIM_real(void)
 }
 
 void
-VTIM_format(double t, char *p)
+VTIM_format(vtim_real t, char *p)
 {
 	struct tm tm;
 	time_t tt;
@@ -236,10 +236,10 @@ VTIM_format(double t, char *p)
 		DIGIT(1, sec);					\
 	} while(0)
 
-double
+vtim_real
 VTIM_parse(const char *p)
 {
-	double t;
+	vtim_real t;
 	int month = 0, year = 0, weekday = -1, mday = 0;
 	int hour = 0, min = 0, sec = 0;
 	int d, leap;
@@ -394,7 +394,7 @@ VTIM_parse(const char *p)
 }
 
 void
-VTIM_sleep(double t)
+VTIM_sleep(vtim_dur t)
 {
 #ifdef HAVE_NANOSLEEP
 	struct timespec ts;
@@ -415,7 +415,7 @@ VTIM_sleep(double t)
 }
 
 struct timeval
-VTIM_timeval(double t)
+VTIM_timeval(vtim_real t)
 {
 	struct timeval tv;
 
@@ -426,7 +426,7 @@ VTIM_timeval(double t)
 }
 
 struct timespec
-VTIM_timespec(double t)
+VTIM_timespec(vtim_real t)
 {
 	struct timespec tv;
 
@@ -464,8 +464,9 @@ tst(const char *s, time_t good)
 	}
 }
 
+/* XXX keep as double for the time being */
 static int
-tst_delta_check(const char *name, double begin, double end, double ref)
+tst_delta_check(const char *name, double begin, double end, vtim_dur ref)
 {
 	const double tol_max = 1.1;
 	const double tol_min = 1;
@@ -487,9 +488,9 @@ tst_delta_check(const char *name, double begin, double end, double ref)
 static void
 tst_delta()
 {
-	double m_begin, m_end;
-	double r_begin, r_end;
-	const double ref = 1;
+	vtim_mono m_begin, m_end;
+	vtim_real r_begin, r_end;
+	const vtim_dur ref = 1;
 	int err = 0;
 
 	r_begin = VTIM_real();
@@ -510,53 +511,56 @@ tst_delta()
 static void
 bench()
 {
-	double s, e, t;
+	vtim_mono s, e;
+	vtim_mono t_m;
+	vtim_real t_r;
+	unsigned long t_i;
 	int i;
 	char buf[64];
 
-	t = 0;
-	s = VTIM_real();
+	t_r = 0;
+	s = VTIM_mono();
 	for (i=0; i<100000; i++)
-		t += VTIM_real();
-	e = VTIM_real();
+		t_r += VTIM_real();
+	e = VTIM_mono();
 	printf("real: %fs / %d = %fns - tst val %f\n",
-	    e - s, i, 1e9 * (e - s) / i, t);
+	    e - s, i, 1e9 * (e - s) / i, t_r);
 
-	t = 0;
-	s = VTIM_real();
+	t_i = 0;
+	s = VTIM_mono();
 	for (i=0; i<100000; i++)
-		t += VTIM_mono();
-	e = VTIM_real();
+		t_m += VTIM_mono();
+	e = VTIM_mono();
 	printf("mono: %fs / %d = %fns - tst val %f\n",
-	    e - s, i, 1e9 * (e - s) / i, t);
+	    e - s, i, 1e9 * (e - s) / i, t_m);
 
-	t = 0;
+	t_i = 0;
 	s = VTIM_mono();
 	for (i=0; i<100000; i++) {
 		snprintf(buf, sizeof(buf), "%.6f", s);
-		t += buf[4];
+		t_i += buf[4];
 	}
 	e = VTIM_mono();
-	printf("printf %%.6f: %fs / %d = %fns - tst val %f %s\n",
-	    e - s, i, 1e9 * (e - s) / i, t, buf);
+	printf("printf %%.6f: %fs / %d = %fns - tst val %lu %s\n",
+	    e - s, i, 1e9 * (e - s) / i, t_i, buf);
 
-	t = 0;
+	t_i = 0;
 	s = VTIM_mono();
 	for (i=0; i<100000; i++) {
 		snprintf(buf, sizeof(buf), "%ju.%06ju",
 		    (uint64_t)floor(s),
 		    (uint64_t)floor((s * 1e6)) % 1000000UL);
-		t += buf[4];
+		t_i += buf[4];
 	}
 	e = VTIM_mono();
-	printf("printf %%ju.%%06ju: %fs / %d = %fns - tst val %f %s\n",
-	    e - s, i, 1e9 * (e - s) / i, t, buf);
+	printf("printf %%ju.%%06ju: %fs / %d = %fns - tst val %lu %s\n",
+	    e - s, i, 1e9 * (e - s) / i, t_i, buf);
 }
 
 void
 parse_check(time_t t, const char *s)
 {
-	double tt;
+	vtim_real tt;
 	char buf[BUFSIZ];
 
 	tt = VTIM_parse(s);


More information about the varnish-commit mailing list