[experimental-ims] b37c01d Since we're cleaning up, rename TIM_ to VTIM_ and take out of libvarnish.h

Geoff Simmons geoff at varnish-cache.org
Mon Jan 9 21:52:16 CET 2012


commit b37c01dfe71edd703758f555fdc8c51554ab4993
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun Oct 9 18:51:03 2011 +0000

    Since we're cleaning up, rename TIM_ to VTIM_ and take out of libvarnish.h

diff --git a/bin/varnishd/cache_acceptor.c b/bin/varnishd/cache_acceptor.c
index 87a9180..4feefb6 100644
--- a/bin/varnishd/cache_acceptor.c
+++ b/bin/varnishd/cache_acceptor.c
@@ -34,6 +34,7 @@
 
 #include "vcli.h"
 #include "vtcp.h"
+#include "vtim.h"
 #include "cli_priv.h"
 
 static pthread_t	VCA_thread;
@@ -167,7 +168,7 @@ vca_pace_check(void)
 	p = vca_pace;
 	Lck_Unlock(&pace_mtx);
 	if (p > 0.0)
-		TIM_sleep(p);
+		VTIM_sleep(p);
 }
 
 static void
@@ -273,7 +274,7 @@ VCA_SetupSess(struct worker *w)
 	sp->fd = wa->acceptsock;
 	sp->vsl_id = wa->acceptsock | VSL_CLIENTMARKER ;
 	wa->acceptsock = -1;
-	sp->t_open = TIM_real();
+	sp->t_open = VTIM_real();
 	sp->t_end = sp->t_open;
 	sp->mylsock = wa->acceptlsock;
 	CHECK_OBJ_NOTNULL(sp->mylsock, LISTEN_SOCK_MAGIC);
@@ -313,14 +314,14 @@ vca_acct(void *arg)
 	hack_ready = 1;
 
 	need_test = 1;
-	t0 = TIM_real();
+	t0 = VTIM_real();
 	while (1) {
 		(void)sleep(1);
 #ifdef SO_SNDTIMEO_WORKS
 		if (params->send_timeout != send_timeout) {
 			need_test = 1;
 			send_timeout = params->send_timeout;
-			tv_sndtimeo = TIM_timeval(send_timeout);
+			tv_sndtimeo = VTIM_timeval(send_timeout);
 			VTAILQ_FOREACH(ls, &heritage.socks, list) {
 				if (ls->sock < 0)
 					continue;
@@ -334,7 +335,7 @@ vca_acct(void *arg)
 		if (params->sess_timeout != sess_timeout) {
 			need_test = 1;
 			sess_timeout = params->sess_timeout;
-			tv_rcvtimeo = TIM_timeval(sess_timeout);
+			tv_rcvtimeo = VTIM_timeval(sess_timeout);
 			VTAILQ_FOREACH(ls, &heritage.socks, list) {
 				if (ls->sock < 0)
 					continue;
@@ -344,7 +345,7 @@ vca_acct(void *arg)
 			}
 		}
 #endif
-		now = TIM_real();
+		now = VTIM_real();
 		VSC_C_main->uptime = (uint64_t)(now - t0);
 	}
 	NEEDLESS_RETURN(NULL);
diff --git a/bin/varnishd/cache_backend_poll.c b/bin/varnishd/cache_backend_poll.c
index c90cda9..c9497bb 100644
--- a/bin/varnishd/cache_backend_poll.c
+++ b/bin/varnishd/cache_backend_poll.c
@@ -46,6 +46,7 @@
 #include "cache.h"
 #include "vrt.h"
 #include "vtcp.h"
+#include "vtim.h"
 #include "cache_backend.h"
 
 /* Default averaging rate, we want something pretty responsive */
@@ -131,28 +132,28 @@ vbp_poke(struct vbp_target *vt)
 	bp = vt->backend;
 	CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC);
 
-	t_start = t_now = TIM_real();
+	t_start = t_now = VTIM_real();
 	t_end = t_start + vt->probe.timeout;
 	tmo = (int)round((t_end - t_now) * 1e3);
 
 	s = -1;
 	if (params->prefer_ipv6 && bp->ipv6 != NULL) {
 		s = vbp_connect(PF_INET6, bp->ipv6, bp->ipv6len, tmo);
-		t_now = TIM_real();
+		t_now = VTIM_real();
 		tmo = (int)round((t_end - t_now) * 1e3);
 		if (s >= 0)
 			vt->good_ipv6 |= 1;
 	}
 	if (tmo > 0 && s < 0 && bp->ipv4 != NULL) {
 		s = vbp_connect(PF_INET, bp->ipv4, bp->ipv4len, tmo);
-		t_now = TIM_real();
+		t_now = VTIM_real();
 		tmo = (int)round((t_end - t_now) * 1e3);
 		if (s >= 0)
 			vt->good_ipv4 |= 1;
 	}
 	if (tmo > 0 && s < 0 && bp->ipv6 != NULL) {
 		s = vbp_connect(PF_INET6, bp->ipv6, bp->ipv6len, tmo);
-		t_now = TIM_real();
+		t_now = VTIM_real();
 		tmo = (int)round((t_end - t_now) * 1e3);
 		if (s >= 0)
 			vt->good_ipv6 |= 1;
@@ -208,7 +209,7 @@ vbp_poke(struct vbp_target *vt)
 		return;
 
 	/* So we have a good receive ... */
-	t_now = TIM_real();
+	t_now = VTIM_real();
 	vt->last = t_now - t_start;
 	vt->good_recv |= 1;
 
@@ -350,7 +351,7 @@ vbp_wrk_poll_backend(void *priv)
 		vbp_has_poked(vt);
 
 		if (!vt->stop)
-			TIM_sleep(vt->probe.interval);
+			VTIM_sleep(vt->probe.interval);
 	}
 	return (NULL);
 }
diff --git a/bin/varnishd/cache_ban.c b/bin/varnishd/cache_ban.c
index f346b4d..8396c7f 100644
--- a/bin/varnishd/cache_ban.c
+++ b/bin/varnishd/cache_ban.c
@@ -70,6 +70,7 @@
 
 #include "vcli.h"
 #include "vend.h"
+#include "vtim.h"
 #include "cli_priv.h"
 #include "hash_slinger.h"
 
@@ -402,7 +403,7 @@ BAN_Insert(struct ban *b)
 	b->spec = malloc(ln + 13L);	/* XXX */
 	XXXAN(b->spec);
 
-	t0 = TIM_real();
+	t0 = VTIM_real();
 	memcpy(b->spec, &t0, sizeof t0);
 	b->spec[12] = (b->flags & BAN_F_REQ) ? 1 : 0;
 	memcpy(b->spec + 13, VSB_data(b->vsb), ln);
@@ -835,10 +836,10 @@ ban_lurker(struct sess *sp, void *priv)
 	while (1) {
 		if (params->ban_lurker_sleep == 0.0) {
 			/* Lurker is disabled.  */
-			TIM_sleep(1.0);
+			VTIM_sleep(1.0);
 			continue;
 		}
-		TIM_sleep(params->ban_lurker_sleep);
+		VTIM_sleep(params->ban_lurker_sleep);
 		ban_lurker_work(sp);
 		WSL_Flush(sp->wrk, 0);
 		WRK_SumStat(sp->wrk);
diff --git a/bin/varnishd/cache_center.c b/bin/varnishd/cache_center.c
index 1415170..6ab8a11 100644
--- a/bin/varnishd/cache_center.c
+++ b/bin/varnishd/cache_center.c
@@ -71,6 +71,7 @@ DOT acceptor -> start [style=bold,color=green]
 
 #include "vcl.h"
 #include "vtcp.h"
+#include "vtim.h"
 #include "cli_priv.h"
 #include "hash_slinger.h"
 #include "stevedore.h"
@@ -211,7 +212,7 @@ cnt_prepresp(struct sess *sp)
 		}
 	}
 
-	sp->t_resp = TIM_real();
+	sp->t_resp = VTIM_real();
 	if (sp->obj->objcore != NULL) {
 		if ((sp->t_resp - sp->obj->last_lru) > params->lru_timeout &&
 		    EXP_Touch(sp->obj->objcore))
@@ -326,7 +327,7 @@ cnt_done(struct sess *sp)
 
 	SES_Charge(sp);
 
-	sp->t_end = TIM_real();
+	sp->t_end = VTIM_real();
 	sp->wrk->lastused = sp->t_end;
 	if (sp->xid == 0) {
 		sp->t_req = sp->t_end;
@@ -466,7 +467,7 @@ cnt_error(struct sess *sp)
 
 	http_PutProtocol(w, sp->vsl_id, h, "HTTP/1.1");
 	http_PutStatus(h, sp->err_code);
-	TIM_format(TIM_real(), date);
+	VTIM_format(VTIM_real(), date);
 	http_PrintfHeader(w, sp->vsl_id, h, "Date: %s", date);
 	http_PrintfHeader(w, sp->vsl_id, h, "Server: Varnish");
 
@@ -581,7 +582,7 @@ cnt_fetch(struct sess *sp)
 		 * What does RFC2616 think about TTL ?
 		 */
 		EXP_Clr(&sp->wrk->exp);
-		sp->wrk->exp.entered = TIM_real();
+		sp->wrk->exp.entered = VTIM_real();
 		RFC2616_Ttl(sp);
 
 		/* pass from vclrecv{} has negative TTL */
@@ -823,7 +824,7 @@ cnt_fetchbody(struct sess *sp)
 	http_CopyHome(sp->wrk, sp->vsl_id, hp2);
 
 	if (http_GetHdr(hp, H_Last_Modified, &b))
-		sp->obj->last_modified = TIM_parse(b);
+		sp->obj->last_modified = VTIM_parse(b);
 	else
 		sp->obj->last_modified = floor(sp->wrk->exp.entered);
 
@@ -1464,7 +1465,7 @@ cnt_start(struct sess *sp)
 
 	/* Update stats of various sorts */
 	sp->wrk->stats.client_req++;
-	sp->t_req = TIM_real();
+	sp->t_req = VTIM_real();
 	sp->wrk->lastused = sp->t_req;
 	sp->wrk->acct_tmp.req++;
 
diff --git a/bin/varnishd/cache_expire.c b/bin/varnishd/cache_expire.c
index 35d0a1e..aed6807 100644
--- a/bin/varnishd/cache_expire.c
+++ b/bin/varnishd/cache_expire.c
@@ -57,6 +57,7 @@
 #include "cache.h"
 #include "hash_slinger.h"
 #include "stevedore.h"
+#include "vtim.h"
 
 static pthread_t exp_thread;
 static struct binheap *exp_heap;
@@ -337,14 +338,14 @@ exp_timer(struct sess *sp, void *priv)
 	struct object *o;
 
 	(void)priv;
-	t = TIM_real();
+	t = VTIM_real();
 	oc = NULL;
 	while (1) {
 		if (oc == NULL) {
 			WSL_Flush(sp->wrk, 0);
 			WRK_SumStat(sp->wrk);
-			TIM_sleep(params->expiry_sleep);
-			t = TIM_real();
+			VTIM_sleep(params->expiry_sleep);
+			t = VTIM_real();
 		}
 
 		Lck_Lock(&exp_mtx);
@@ -360,7 +361,7 @@ exp_timer(struct sess *sp, void *priv)
 		 * got out of date, refresh it and check again.
 		 */
 		if (oc->timer_when > t)
-			t = TIM_real();
+			t = VTIM_real();
 		if (oc->timer_when > t) {
 			Lck_Unlock(&exp_mtx);
 			oc = NULL;
diff --git a/bin/varnishd/cache_pipe.c b/bin/varnishd/cache_pipe.c
index 74343ba..418ded1 100644
--- a/bin/varnishd/cache_pipe.c
+++ b/bin/varnishd/cache_pipe.c
@@ -36,6 +36,7 @@
 
 #include "cache.h"
 #include "vtcp.h"
+#include "vtim.h"
 
 static int
 rdf(int fd0, int fd1)
@@ -90,7 +91,7 @@ PipeSession(struct sess *sp)
 		return;
 	}
 
-	sp->t_resp = TIM_real();
+	sp->t_resp = VTIM_real();
 
 	memset(fds, 0, sizeof fds);
 
diff --git a/bin/varnishd/cache_pool.c b/bin/varnishd/cache_pool.c
index e146043..4421969 100644
--- a/bin/varnishd/cache_pool.c
+++ b/bin/varnishd/cache_pool.c
@@ -51,6 +51,7 @@
 #include "cache_waiter.h"
 #include "hash_slinger.h"
 #include "vtcp.h"
+#include "vtim.h"
 
 /*--------------------------------------------------------------------
  * MAC OS/X is incredibly moronic when it comes to time and such...
@@ -224,7 +225,7 @@ Pool_Work_Thread(void *priv, struct worker *w)
 		} else if (VTAILQ_EMPTY(&pp->socks)) {
 			/* Nothing to do: To sleep, perchance to dream ... */
 			if (isnan(w->lastused))
-				w->lastused = TIM_real();
+				w->lastused = VTIM_real();
 			VTAILQ_INSERT_HEAD(&pp->idle, w, list);
 			if (!stats_clean)
 				WRK_SumStat(w);
@@ -343,7 +344,7 @@ Pool_Schedule(struct pool *pp, struct sess *sp)
 	 * XXX: a notice might be polite, but would potentially
 	 * XXX: sleep whichever thread got us here
 	 */
-	sp->t_end = TIM_real();
+	sp->t_end = VTIM_real();
 	if (sp->vcl != NULL) {
 		/*
 		 * A session parked on a busy object can come here
@@ -401,10 +402,10 @@ pool_breed(struct pool *qp, const pthread_attr_t *tp_attr)
 			Lck_Lock(&pool_mtx);
 			VSC_C_main->threads_limited++;
 			Lck_Unlock(&pool_mtx);
-			TIM_sleep(params->wthread_fail_delay * 1e-3);
+			VTIM_sleep(params->wthread_fail_delay * 1e-3);
 		} else {
 			AZ(pthread_detach(tp));
-			TIM_sleep(params->wthread_add_delay * 1e-3);
+			VTIM_sleep(params->wthread_add_delay * 1e-3);
 			qp->nthr++;
 			Lck_Lock(&pool_mtx);
 			VSC_C_main->threads++;
@@ -476,7 +477,7 @@ pool_herder(void *priv)
 		if (pp->nthr <= params->wthread_min) 
 			continue;
 
-		t_idle = TIM_real() - params->wthread_timeout;
+		t_idle = VTIM_real() - params->wthread_timeout;
 
 		Lck_Lock(&pp->mtx);
 		VSC_C_main->sess_queued += pp->nqueued;
diff --git a/bin/varnishd/cache_response.c b/bin/varnishd/cache_response.c
index 4d7c88b..b4459da 100644
--- a/bin/varnishd/cache_response.c
+++ b/bin/varnishd/cache_response.c
@@ -38,6 +38,7 @@
 #include "cache.h"
 #include "stevedore.h"
 #include "vct.h"
+#include "vtim.h"
 
 /*--------------------------------------------------------------------*/
 
@@ -133,7 +134,7 @@ RES_BuildHttp(const struct sess *sp)
 		http_PrintfHeader(sp->wrk, sp->vsl_id, sp->wrk->resp,
 		    "Transfer-Encoding: chunked");
 
-	TIM_format(TIM_real(), time_str);
+	VTIM_format(VTIM_real(), time_str);
 	http_PrintfHeader(sp->wrk, sp->vsl_id, sp->wrk->resp, "Date: %s", time_str);
 
 	if (sp->xid != sp->obj->xid)
diff --git a/bin/varnishd/cache_shmlog.c b/bin/varnishd/cache_shmlog.c
index 8c06897..75f662d 100644
--- a/bin/varnishd/cache_shmlog.c
+++ b/bin/varnishd/cache_shmlog.c
@@ -34,6 +34,7 @@
 #include "cache.h"
 #include "vmb.h"
 #include "vsm.h"
+#include "vtim.h"
 
 /* These cannot be struct lock, which depends on vsm/vsl working */
 static pthread_mutex_t vsl_mtx;
@@ -285,7 +286,7 @@ VSL_Init(void)
 	vsl_ptr = vsl_start + 1;
 
 	vsl_wrap();
-	VSM_head->starttime = (intmax_t)TIM_real();
+	VSM_head->starttime = (intmax_t)VTIM_real();
 	memset(VSM_head->panicstr, '\0', sizeof *VSM_head->panicstr);
 	memset(VSC_C_main, 0, sizeof *VSC_C_main);
 	VSM_head->child_pid = getpid();
diff --git a/bin/varnishd/cache_vrt.c b/bin/varnishd/cache_vrt.c
index a53799e..5c633e2 100644
--- a/bin/varnishd/cache_vrt.c
+++ b/bin/varnishd/cache_vrt.c
@@ -43,6 +43,7 @@
 #include "vrt_obj.h"
 #include "vav.h"
 #include "vcl.h"
+#include "vtim.h"
 #include "hash_slinger.h"
 #include "cache_backend.h"
 
@@ -276,7 +277,7 @@ VRT_r_now(const struct sess *sp)
 {
 
 	(void)sp;
-	return (TIM_real());
+	return (VTIM_real());
 }
 
 /*--------------------------------------------------------------------*/
@@ -339,8 +340,8 @@ VRT_time_string(const struct sess *sp, double t)
 {
 	char *p;
 
-	AN(p = WS_Alloc(sp->http->ws, TIM_FORMAT_SIZE));
-	TIM_format(t, p);
+	AN(p = WS_Alloc(sp->http->ws, VTIM_FORMAT_SIZE));
+	VTIM_format(t, p);
 	return p;
 }
 
diff --git a/bin/varnishd/cache_vrt_var.c b/bin/varnishd/cache_vrt_var.c
index 1343ae4..ff523df 100644
--- a/bin/varnishd/cache_vrt_var.c
+++ b/bin/varnishd/cache_vrt_var.c
@@ -37,6 +37,7 @@
 
 #include "vrt.h"
 #include "vtcp.h"
+#include "vtim.h"
 #include "vrt_obj.h"
 #include "cache_backend.h"
 #include "hash_slinger.h"
@@ -535,7 +536,7 @@ VRT_r_obj_lastuse(const struct sess *sp)
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC);	/* XXX */
-	return (TIM_real() - sp->obj->last_use);
+	return (VTIM_real() - sp->obj->last_use);
 }
 
 unsigned
diff --git a/bin/varnishd/cache_waiter_epoll.c b/bin/varnishd/cache_waiter_epoll.c
index d4168db..c66d15a 100644
--- a/bin/varnishd/cache_waiter_epoll.c
+++ b/bin/varnishd/cache_waiter_epoll.c
@@ -42,6 +42,7 @@
 
 #include "cache.h"
 #include "cache_waiter.h"
+#include "vtim.h"
 
 #ifndef EPOLLRDHUP
 #  define EPOLLRDHUP 0
@@ -186,7 +187,7 @@ vwe_thread(void *priv)
 			continue;
 
 		/* check for timeouts */
-		deadline = TIM_real() - params->sess_timeout;
+		deadline = VTIM_real() - params->sess_timeout;
 		for (;;) {
 			sp = VTAILQ_FIRST(&vwe->sesshead);
 			if (sp == NULL)
@@ -215,7 +216,7 @@ vwe_sess_timeout_ticker(void *priv)
 	while (1) {
 		/* ticking */
 		assert(write(vwe->timer_pipes[1], &ticker, 1));
-		TIM_sleep(100 * 1e-3);
+		VTIM_sleep(100 * 1e-3);
 	}
 	return NULL;
 }
diff --git a/bin/varnishd/cache_waiter_kqueue.c b/bin/varnishd/cache_waiter_kqueue.c
index f023c3f..e7cb299 100644
--- a/bin/varnishd/cache_waiter_kqueue.c
+++ b/bin/varnishd/cache_waiter_kqueue.c
@@ -43,6 +43,7 @@
 
 #include "cache.h"
 #include "cache_waiter.h"
+#include "vtim.h"
 
 #define NKEV	100
 
@@ -183,7 +184,7 @@ vwk_thread(void *priv)
 		 * would not know we meant "the old fd of this number".
 		 */
 		vwk_kq_flush(vwk);
-		deadline = TIM_real() - params->sess_timeout;
+		deadline = VTIM_real() - params->sess_timeout;
 		for (;;) {
 			sp = VTAILQ_FIRST(&vwk->sesshead);
 			if (sp == NULL)
diff --git a/bin/varnishd/cache_waiter_poll.c b/bin/varnishd/cache_waiter_poll.c
index df0439b..f3f3289 100644
--- a/bin/varnishd/cache_waiter_poll.c
+++ b/bin/varnishd/cache_waiter_poll.c
@@ -36,6 +36,7 @@
 #include "cache.h"
 
 #include "cache_waiter.h"
+#include "vtim.h"
 
 #define NEEV	128
 
@@ -139,7 +140,7 @@ vwp_main(void *priv)
 		assert(vwp->pollfd[vwp->pipes[1]].fd == -1);
 		v = poll(vwp->pollfd, vwp->hpoll + 1, 100);
 		assert(v >= 0);
-		deadline = TIM_real() - params->sess_timeout;
+		deadline = VTIM_real() - params->sess_timeout;
 		v2 = v;
 		VTAILQ_FOREACH_SAFE(sp, &vwp->sesshead, list, sp2) {
 			if (v != 0 && v2 == 0)
diff --git a/bin/varnishd/cache_waiter_ports.c b/bin/varnishd/cache_waiter_ports.c
index 04c6e92..ca7e229 100644
--- a/bin/varnishd/cache_waiter_ports.c
+++ b/bin/varnishd/cache_waiter_ports.c
@@ -43,6 +43,7 @@
 
 #include "cache.h"
 #include "cache_waiter.h"
+#include "vtim.h"
 
 #define MAX_EVENTS 256
 
@@ -195,7 +196,7 @@ vws_thread(void *priv)
 			vws_port_ev(vws, ev + ei);
 
 		/* check for timeouts */
-		now = TIM_real();
+		now = VTIM_real();
 		deadline = now - params->sess_timeout;
 
 		/*
@@ -234,7 +235,7 @@ vws_thread(void *priv)
 			} else if (tmo > max_t) {
 				timeout = &max_ts;
 			} else {
-				ts = TIM_timespec(tmo);
+				ts = VTIM_timespec(tmo);
 				timeout = &ts;
 			}
 		} else {
diff --git a/bin/varnishd/hash_critbit.c b/bin/varnishd/hash_critbit.c
index 7428097..e5b0746 100644
--- a/bin/varnishd/hash_critbit.c
+++ b/bin/varnishd/hash_critbit.c
@@ -38,6 +38,7 @@
 #include "hash_slinger.h"
 #include "cli_priv.h"
 #include "vmb.h"
+#include "vtim.h"
 
 static struct lock hcb_mtx;
 
@@ -367,7 +368,7 @@ hcb_cleaner(void *priv)
 		VTAILQ_CONCAT(&dead_h, &cool_h, hoh_list);
 		Lck_Unlock(&hcb_mtx);
 		WRK_SumStat(&ww);
-		TIM_sleep(params->critbit_cooloff);
+		VTIM_sleep(params->critbit_cooloff);
 	}
 	NEEDLESS_RETURN(NULL);
 }
diff --git a/bin/varnishd/mgt_child.c b/bin/varnishd/mgt_child.c
index 6c1cf71..e676f48 100644
--- a/bin/varnishd/mgt_child.c
+++ b/bin/varnishd/mgt_child.c
@@ -53,6 +53,7 @@
 #include "vss.h"
 #include "vbm.h"
 #include "vtcp.h"
+#include "vtim.h"
 
 pid_t		child_pid = -1;
 
@@ -439,7 +440,7 @@ mgt_save_panic(void)
 		VSB_delete(child_panic);
 	child_panic = VSB_new_auto();
 	XXXAN(child_panic);
-	TIM_format(TIM_real(), time_str);
+	VTIM_format(VTIM_real(), time_str);
 	VSB_printf(child_panic, "Last panic at: %s\n", time_str);
 	VSB_cat(child_panic, VSM_head->panicstr);
 	AZ(VSB_finish(child_panic));
diff --git a/bin/varnishd/rfc2616.c b/bin/varnishd/rfc2616.c
index 36936b6..6de52e2 100644
--- a/bin/varnishd/rfc2616.c
+++ b/bin/varnishd/rfc2616.c
@@ -33,6 +33,7 @@
 #include <math.h>
 
 #include "cache.h"
+#include "vtim.h"
 
 /*--------------------------------------------------------------------
  * TTL and Age calculation in Varnish
@@ -88,10 +89,10 @@ RFC2616_Ttl(const struct sess *sp)
 		sp->wrk->exp.age = age;
 	}
 	if (http_GetHdr(hp, H_Expires, &p))
-		h_expires = TIM_parse(p);
+		h_expires = VTIM_parse(p);
 
 	if (http_GetHdr(hp, H_Date, &p))
-		h_date = TIM_parse(p);
+		h_date = VTIM_parse(p);
 
 	switch (sp->err_code) {
 	default:
@@ -315,7 +316,7 @@ RFC2616_Do_Cond(const struct sess *sp)
 	if (http_GetHdr(sp->http, H_If_Modified_Since, &p) ) {
 		if (!sp->obj->last_modified)
 			return (0);
-		ims = TIM_parse(p);
+		ims = VTIM_parse(p);
 		if (ims > sp->t_req)	/* [RFC2616 14.25] */
 			return (0);
 		if (sp->obj->last_modified > ims)
diff --git a/bin/varnishd/storage_persistent_silo.c b/bin/varnishd/storage_persistent_silo.c
index ba84b79..2defc0d 100644
--- a/bin/varnishd/storage_persistent_silo.c
+++ b/bin/varnishd/storage_persistent_silo.c
@@ -41,6 +41,7 @@
 #include "stevedore.h"
 #include "hash_slinger.h"
 #include "vsha256.h"
+#include "vtim.h"
 
 #include "persistent.h"
 #include "storage_persistent.h"
@@ -122,7 +123,7 @@ smp_load_seg(const struct sess *sp, const struct smp_sc *sc,
 	struct smp_object *so;
 	struct objcore *oc;
 	uint32_t no;
-	double t_now = TIM_real();
+	double t_now = VTIM_real();
 	struct smp_signctx ctx[1];
 
 	ASSERT_SILO_THREAD(sc);
diff --git a/bin/varnishd/varnishd.c b/bin/varnishd/varnishd.c
index 2d7db59..360cdad 100644
--- a/bin/varnishd/varnishd.c
+++ b/bin/varnishd/varnishd.c
@@ -55,6 +55,7 @@
 
 #include "vav.h"
 #include "vin.h"
+#include "vtim.h"
 #include "heritage.h"
 #include "mgt.h"
 #include "hash_slinger.h"
@@ -376,9 +377,9 @@ main(int argc, char * const *argv)
 	 */
 	AZ(setenv("TZ", "UTC", 1));
 	tzset();
-	assert(TIM_parse("Sun, 06 Nov 1994 08:49:37 GMT") == 784111777);
-	assert(TIM_parse("Sunday, 06-Nov-94 08:49:37 GMT") == 784111777);
-	assert(TIM_parse("Sun Nov  6 08:49:37 1994") == 784111777);
+	assert(VTIM_parse("Sun, 06 Nov 1994 08:49:37 GMT") == 784111777);
+	assert(VTIM_parse("Sunday, 06-Nov-94 08:49:37 GMT") == 784111777);
+	assert(VTIM_parse("Sun Nov  6 08:49:37 1994") == 784111777);
 
 	/*
 	 * Check that our SHA256 works
diff --git a/bin/varnishd/vsm.c b/bin/varnishd/vsm.c
index 28f98e5..dc265ba 100644
--- a/bin/varnishd/vsm.c
+++ b/bin/varnishd/vsm.c
@@ -54,6 +54,7 @@
 #include "common.h"
 #include "vsm.h"
 #include "vmb.h"
+#include "vtim.h"
 
 /* These two come from beyond (mgt_shmem.c actually) */
 struct VSM_head		*VSM_head;
@@ -87,7 +88,7 @@ vsm_release(unsigned seq)
 static void
 vsm_cleanup(void)
 {
-	unsigned now = (unsigned)TIM_mono();
+	unsigned now = (unsigned)VTIM_mono();
 	struct VSM_chunk *sha, *sha2;
 	unsigned seq;
 
@@ -203,7 +204,7 @@ VSM__Free(const void *ptr)
 	AN(sha);
 	seq = vsm_mark();
 	bprintf(sha->class, "%s", VSM_CLASS_COOL);
-	sha->state = (unsigned)TIM_mono();
+	sha->state = (unsigned)VTIM_mono();
 	vsm_release(seq);
 }
 
diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index e31eaca..83bbb22 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -40,6 +40,7 @@
 #include "vtc.h"
 
 #include "vav.h"
+#include "vtim.h"
 #include "libvarnish.h"
 
 
@@ -129,10 +130,10 @@ macro_get(const char *b, const char *e)
 	l = e - b;
 
 	if (l == 4 && !memcmp(b, "date", l)) {
-		double t = TIM_real();
+		double t = VTIM_real();
 		retval = malloc(64);
 		AN(retval);
-		TIM_format(t, retval);
+		VTIM_format(t, retval);
 		return (retval);
 	}
 
@@ -415,7 +416,7 @@ cmd_delay(CMD_ARGS)
 	AZ(av[2]);
 	f = strtod(av[1], NULL);
 	vtc_log(vl, 3, "delaying %g second(s)", f);
-	TIM_sleep(f);
+	VTIM_sleep(f);
 }
 
 /**********************************************************************
diff --git a/bin/varnishtest/vtc_log.c b/bin/varnishtest/vtc_log.c
index 51c6646..c06f319 100644
--- a/bin/varnishtest/vtc_log.c
+++ b/bin/varnishtest/vtc_log.c
@@ -36,6 +36,7 @@
 #include "vtc.h"
 
 #include "libvarnish.h"
+#include "vtim.h"
 
 static pthread_mutex_t	vtclog_mtx;
 static char		*vtclog_buf;
@@ -58,7 +59,7 @@ void
 vtc_loginit(char *buf, unsigned buflen)
 {
 
-	t0 = TIM_mono();
+	t0 = VTIM_mono();
 	vtclog_buf = buf;
 	vtclog_left = buflen;
 	AZ(pthread_mutex_init(&vtclog_mtx, NULL));
@@ -126,7 +127,7 @@ vtc_log(struct vtclog *vl, unsigned lvl, const char *fmt, ...)
 	double tx;
 
 	CHECK_OBJ_NOTNULL(vl, VTCLOG_MAGIC);
-	tx = TIM_mono() - t0;
+	tx = VTIM_mono() - t0;
 	AZ(pthread_mutex_lock(&vl->mtx));
 	assert(lvl < NLEAD);
 	VSB_clear(vl->vsb);
@@ -162,7 +163,7 @@ vtc_dump(struct vtclog *vl, unsigned lvl, const char *pfx, const char *str, int
 	double tx;
 
 	CHECK_OBJ_NOTNULL(vl, VTCLOG_MAGIC);
-	tx = TIM_mono() - t0;
+	tx = VTIM_mono() - t0;
 	assert(lvl < NLEAD);
 	AZ(pthread_mutex_lock(&vl->mtx));
 	VSB_clear(vl->vsb);
@@ -225,7 +226,7 @@ vtc_hexdump(struct vtclog *vl, unsigned lvl, const char *pfx, const unsigned cha
 	double tx;
 
 	CHECK_OBJ_NOTNULL(vl, VTCLOG_MAGIC);
-	tx = TIM_mono() - t0;
+	tx = VTIM_mono() - t0;
 	assert(len >= 0);
 	assert(lvl < NLEAD);
 	AZ(pthread_mutex_lock(&vl->mtx));
diff --git a/bin/varnishtest/vtc_main.c b/bin/varnishtest/vtc_main.c
index d201946..040afdc 100644
--- a/bin/varnishtest/vtc_main.c
+++ b/bin/varnishtest/vtc_main.c
@@ -42,6 +42,7 @@
 
 #include "libvarnish.h"
 #include "vev.h"
+#include "vtim.h"
 
 #ifndef HAVE_SRANDOMDEV
 #include "compat/srandomdev.h"
@@ -193,7 +194,7 @@ tst_cb(const struct vev *ve, int what)
 		njob--;
 		px = wait4(jp->child, &stx, 0, NULL);
 		assert(px == jp->child);
-		t = TIM_mono() - jp->t0;
+		t = VTIM_mono() - jp->t0;
 		AZ(close(ve->fd));
 
 		if (stx && vtc_verbosity)
@@ -283,7 +284,7 @@ start_test(void)
 	AZ(pipe(p));
 	assert(p[0] > STDERR_FILENO);
 	assert(p[1] > STDERR_FILENO);
-	jp->t0 = TIM_mono();
+	jp->t0 = VTIM_mono();
 	jp->child = fork();
 	assert(jp->child >= 0);
 	if (jp->child == 0) {
diff --git a/include/Makefile.am b/include/Makefile.am
index 2cbe5d1..d62cab3 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -57,7 +57,8 @@ nobase_noinst_HEADERS = \
 	vsub.h \
 	vsha256.h \
 	vss.h \
-	vtcp.h
+	vtcp.h \
+	vtim.h
 
 tbl/vrt_stv_var.h tbl/vcl_returns.h vcl.h vrt_obj.h: $(top_srcdir)/lib/libvcl/generate.py $(top_srcdir)/include/vrt.h
 	mkdir -p tbl
diff --git a/include/libvarnish.h b/include/libvarnish.h
index 5bbe073..c0851c8 100644
--- a/include/libvarnish.h
+++ b/include/libvarnish.h
@@ -41,16 +41,6 @@ struct vsb;
 /* from libvarnish/num.c */
 const char *str2bytes(const char *p, uintmax_t *r, uintmax_t rel);
 
-/* from libvarnish/time.c */
-#define TIM_FORMAT_SIZE 30
-void TIM_format(double t, char *p);
-double TIM_parse(const char *p);
-double TIM_mono(void);
-double TIM_real(void);
-void TIM_sleep(double t);
-struct timespec TIM_timespec(double t);
-struct timeval TIM_timeval(double t);
-
 /* from libvarnish/version.c */
 void VCS_Message(const char *);
 
diff --git a/include/vtim.h b/include/vtim.h
new file mode 100644
index 0000000..8db4fbe
--- /dev/null
+++ b/include/vtim.h
@@ -0,0 +1,39 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+ *
+ */
+
+/* from libvarnish/vtim.c */
+#define VTIM_FORMAT_SIZE 30
+void VTIM_format(double t, char *p);
+double VTIM_parse(const char *p);
+double VTIM_mono(void);
+double VTIM_real(void);
+void VTIM_sleep(double t);
+struct timespec VTIM_timespec(double t);
+struct timeval VTIM_timeval(double t);
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index 0148460..3c045e6 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -14,7 +14,7 @@ libvarnish_la_SOURCES = \
 	cli_serve.c \
 	flopen.c \
 	num.c \
-	time.c \
+	vtim.c \
 	vtcp.c \
 	vct.c \
 	version.c \
diff --git a/lib/libvarnish/time.c b/lib/libvarnish/time.c
deleted file mode 100644
index 31fc284..0000000
--- a/lib/libvarnish/time.c
+++ /dev/null
@@ -1,293 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
- *
- * Semi-trivial functions to handle HTTP header timestamps according to
- * RFC 2616 section 3.3.
- *
- * In the highly unlikely event of performance trouble, handbuilt versions
- * would likely be faster than relying on the OS time functions.
- *
- * We must parse three different formats:
- *       000000000011111111112222222222
- *       012345678901234567890123456789
- *       ------------------------------
- *	"Sun, 06 Nov 1994 08:49:37 GMT"		RFC822 & RFC1123
- *	"Sunday, 06-Nov-94 08:49:37 GMT"	RFC850
- *	"Sun Nov  6 08:49:37 1994"		ANSI-C asctime()
- *
- * And always output the RFC1123 format.
- *
- */
-
-#include "config.h"
-
-#include <sys/time.h>
-
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include <math.h>
-
-#include "libvarnish.h"
-
-/*
- * Note on Solaris: for some reason, clock_gettime(CLOCK_MONOTONIC, &ts) is not
- * implemented in assembly, but falls into a syscall, while gethrtime() doesn't,
- * so we save a syscall by using gethrtime() if it is defined.
- */
-
-double
-TIM_mono(void)
-{
-#ifdef HAVE_GETHRTIME
-	return (gethrtime() * 1e-9);
-#elif  HAVE_CLOCK_GETTIME
-	struct timespec ts;
-
-	assert(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
-	return (ts.tv_sec + 1e-9 * ts.tv_nsec);
-#else
-	struct timeval tv;
-
-	assert(gettimeofday(&tv, NULL) == 0);
-	return (tv.tv_sec + 1e-6 * tv.tv_usec);
-#endif
-}
-
-double
-TIM_real(void)
-{
-#ifdef HAVE_CLOCK_GETTIME
-	struct timespec ts;
-
-	assert(clock_gettime(CLOCK_REALTIME, &ts) == 0);
-	return (ts.tv_sec + 1e-9 * ts.tv_nsec);
-#else
-	struct timeval tv;
-
-	assert(gettimeofday(&tv, NULL) == 0);
-	return (tv.tv_sec + 1e-6 * tv.tv_usec);
-#endif
-}
-
-void
-TIM_format(double t, char *p)
-{
-	struct tm tm;
-	time_t tt;
-
-	tt = (time_t) t;
-	(void)gmtime_r(&tt, &tm);
-	AN(strftime(p, TIM_FORMAT_SIZE, "%a, %d %b %Y %T GMT", &tm));
-}
-
-/* XXX: add statistics ? */
-static const char *fmts[] = {
-	"%a, %d %b %Y %T GMT",	/* RFC 822 & RFC 1123 */
-	"%A, %d-%b-%y %T GMT",	/* RFC 850 */
-	"%a %b %d %T %Y",	/* ANSI-C asctime() */
-	"%F %T",		/* ISO 8601 */
-	NULL
-};
-
-double
-TIM_parse(const char *p)
-{
-	double t;
-	struct tm tm;
-	const char **r;
-
-	for (r = fmts; *r != NULL; r++) {
-		memset(&tm, 0, sizeof tm);
-		if (strptime(p, *r, &tm) != NULL) {
-			/*
-			 * Make sure this is initialized on the off-chance
-			 * that some raving loonie would apply DST to UTC.
-			 */
-			tm.tm_isdst = -1;
-#if defined(HAVE_TIMEGM)
-			t = timegm(&tm);
-#else
-			/*
-			 * Ahh, another POSIX_STUPIDITY, how unexpected.
-			 * Instead of, as would have been logical, to have
-			 * tm_timezone element, mktime() is standardized as
-			 * always working in localtime.  This brilliant idea
-			 * came from the same people who said "leap-seconds ?
-			 * Naah, screw it!".
-			 *
-			 * On broken systems without a working timegm(),
-			 * it is the responsibility of the calling program
-			 * to set the timezone to UTC.  We check that.
-			 */
-			t = mktime(&tm);
-			assert(!strcmp(tzname[0], "UTC"));
-#endif
-			return (t);
-		}
-	}
-	return (0);
-}
-
-void
-TIM_sleep(double t)
-{
-#ifdef HAVE_NANOSLEEP
-	struct timespec ts;
-
-	ts = TIM_timespec(t);
-
-	(void)nanosleep(&ts, NULL);
-#else
-	if (t >= 1.) {
-		(void)sleep(floor(t));
-		t -= floor(t);
-	}
-	/* XXX: usleep() is not mandated to be thread safe */
-	t *= 1e6;
-	if (t > 0)
-		(void)usleep(floor(t));
-#endif
-}
-
-struct timeval
-TIM_timeval(double t)
-{
-	struct timeval tv;
-
-	tv.tv_sec = (time_t)trunc(t);
-	tv.tv_usec = (int)(1e6 * (t - tv.tv_sec));
-	return (tv);
-}
-
-struct timespec
-TIM_timespec(double t)
-{
-	struct timespec tv;
-
-	tv.tv_sec = (time_t)trunc(t);
-	tv.tv_nsec = (int)(1e9 * (t - tv.tv_sec));
-	return (tv);
-}
-
-
-#ifdef TEST_DRIVER
-
-#include <stdlib.h>
-
-/*
- * Compile with:
- *  cc -o foo -DTEST_DRIVER -I../.. -I../../include time.c assert.c
- * (Solaris)
- *  cc -o foo -DTEST_DRIVER -I../.. -I../../include -lm time.c assert.c
- * Test with:
- *  env TZ=UTC ./foo
- *  env TZ=CET ./foo
- */
-
-static void
-tst(const char *s, time_t good)
-{
-	time_t t;
-	char buf[BUFSIZ];
-
-	t = TIM_parse(s);
-	TIM_format(t, buf);
-	printf("%-30s -> %12jd -> %s\n", s, (intmax_t)t, buf);
-	if (t != good) {
-		printf("Parse error! Got: %jd should have %jd diff %jd\n",
-		    (intmax_t)t, (intmax_t)good, (intmax_t)(t - good));
-		exit (2);
-	}
-}
-
-static int
-tst_delta_check(const char *name, double begin, double end, double ref)
-{
-	const double tol_max = 1.1;
-	const double tol_min = 1;
-
-	printf("%s delta for %fs sleep: %f\n", name, ref, (end - begin));
-
-	if ((end - begin) > tol_max * ref) {
-		printf("%s delta above tolerance: ((%f - %f) = %f) > %f\n",
-		    name, end, begin, (end - begin), tol_max);
-		return (1);
-	} else if ((end - begin) < tol_min * ref) {
-		printf("%s delta below tolerance: ((%f - %f) = %f) < %f\n",
-		    name, end, begin, (end - begin), tol_min);
-		return (1);
-	}
-	return (0);
-}
-
-static void
-tst_delta()
-{
-	double m_begin, m_end;
-	double r_begin, r_end;
-	const double ref = 1;
-	int err = 0;
-
-	r_begin = TIM_real();
-	m_begin = TIM_mono();
-	TIM_sleep(ref);
-	r_end = TIM_real();
-	m_end = TIM_mono();
-
-	err += tst_delta_check("TIM_mono", m_begin, m_end, ref);
-	err += tst_delta_check("TIM_real", r_begin, r_end, ref);
-
-	if (err) {
-		printf("%d time delta test errrors\n", err);
-		exit (2);
-	}
-}
-
-int
-main(int argc, char **argv)
-{
-	time_t t;
-	char buf[BUFSIZ];
-
-	time(&t);
-	memset(buf, 0x55, sizeof buf);
-	TIM_format(t, buf);
-	printf("scan = %d <%s>\n", TIM_parse(buf), buf);
-
-	/* Examples from RFC2616 section 3.3.1 */
-	tst("Sun, 06 Nov 1994 08:49:37 GMT", 784111777);
-	tst("Sunday, 06-Nov-94 08:49:37 GMT", 784111777);
-	tst("Sun Nov  6 08:49:37 1994", 784111777);
-
-	tst_delta();
-
-	return (0);
-}
-#endif
diff --git a/lib/libvarnish/vev.c b/lib/libvarnish/vev.c
index 8c64562..c55ebfc 100644
--- a/lib/libvarnish/vev.c
+++ b/lib/libvarnish/vev.c
@@ -39,6 +39,7 @@
 #include <pthread.h>
 
 #include "libvarnish.h"
+#include "vtim.h"
 #include "vev.h"
 #include "miniobj.h"
 #include "binary_heap.h"
@@ -278,7 +279,7 @@ vev_add(struct vev_base *evb, struct vev *e)
 	e->magic = VEV_MAGIC;	/* before binheap_insert() */
 
 	if (e->timeout != 0.0) {
-		e->__when += TIM_mono() + e->timeout;
+		e->__when += VTIM_mono() + e->timeout;
 		binheap_insert(evb->binheap, e);
 		assert(e->__binheap_idx > 0);
 		DBG(evb, "... bidx = %d\n", e->__binheap_idx);
@@ -455,7 +456,7 @@ vev_schedule_one(struct vev_base *evb)
 	if (e != NULL) {
 		CHECK_OBJ_NOTNULL(e, VEV_MAGIC);
 		assert(e->__binheap_idx == 1);
-		t = TIM_mono();
+		t = VTIM_mono();
 		if (e->__when <= t)
 			return (vev_sched_timeout(evb, e, t));
 		tmo = (int)((e->__when - t) * 1e3);
@@ -478,7 +479,7 @@ vev_schedule_one(struct vev_base *evb)
 		return (vev_sched_signal(evb));
 	if (i == 0) {
 		assert(e != NULL);
-		t = TIM_mono();
+		t = VTIM_mono();
 		if (e->__when <= t)
 			return (vev_sched_timeout(evb, e, t));
 	}
diff --git a/lib/libvarnish/vtim.c b/lib/libvarnish/vtim.c
new file mode 100644
index 0000000..edfb8a5
--- /dev/null
+++ b/lib/libvarnish/vtim.c
@@ -0,0 +1,295 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+ *
+ * Semi-trivial functions to handle HTTP header timestamps according to
+ * RFC 2616 section 3.3.
+ *
+ * In the highly unlikely event of performance trouble, handbuilt versions
+ * would likely be faster than relying on the OS time functions.
+ *
+ * We must parse three different formats:
+ *       000000000011111111112222222222
+ *       012345678901234567890123456789
+ *       ------------------------------
+ *	"Sun, 06 Nov 1994 08:49:37 GMT"		RFC822 & RFC1123
+ *	"Sunday, 06-Nov-94 08:49:37 GMT"	RFC850
+ *	"Sun Nov  6 08:49:37 1994"		ANSI-C asctime()
+ *
+ * And always output the RFC1123 format.
+ *
+ */
+
+#include "config.h"
+
+#include <sys/time.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <math.h>
+
+#include "vas.h"
+#include "vtim.h"
+//#include "libvarnish.h"
+
+/*
+ * Note on Solaris: for some reason, clock_gettime(CLOCK_MONOTONIC, &ts) is not
+ * implemented in assembly, but falls into a syscall, while gethrtime() doesn't,
+ * so we save a syscall by using gethrtime() if it is defined.
+ */
+
+double
+VTIM_mono(void)
+{
+#ifdef HAVE_GETHRTIME
+	return (gethrtime() * 1e-9);
+#elif  HAVE_CLOCK_GETTIME
+	struct timespec ts;
+
+	assert(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
+	return (ts.tv_sec + 1e-9 * ts.tv_nsec);
+#else
+	struct timeval tv;
+
+	assert(gettimeofday(&tv, NULL) == 0);
+	return (tv.tv_sec + 1e-6 * tv.tv_usec);
+#endif
+}
+
+double
+VTIM_real(void)
+{
+#ifdef HAVE_CLOCK_GETTIME
+	struct timespec ts;
+
+	assert(clock_gettime(CLOCK_REALTIME, &ts) == 0);
+	return (ts.tv_sec + 1e-9 * ts.tv_nsec);
+#else
+	struct timeval tv;
+
+	assert(gettimeofday(&tv, NULL) == 0);
+	return (tv.tv_sec + 1e-6 * tv.tv_usec);
+#endif
+}
+
+void
+VTIM_format(double t, char *p)
+{
+	struct tm tm;
+	time_t tt;
+
+	tt = (time_t) t;
+	(void)gmtime_r(&tt, &tm);
+	AN(strftime(p, VTIM_FORMAT_SIZE, "%a, %d %b %Y %T GMT", &tm));
+}
+
+/* XXX: add statistics ? */
+static const char *fmts[] = {
+	"%a, %d %b %Y %T GMT",	/* RFC 822 & RFC 1123 */
+	"%A, %d-%b-%y %T GMT",	/* RFC 850 */
+	"%a %b %d %T %Y",	/* ANSI-C asctime() */
+	"%F %T",		/* ISO 8601 */
+	NULL
+};
+
+double
+VTIM_parse(const char *p)
+{
+	double t;
+	struct tm tm;
+	const char **r;
+
+	for (r = fmts; *r != NULL; r++) {
+		memset(&tm, 0, sizeof tm);
+		if (strptime(p, *r, &tm) != NULL) {
+			/*
+			 * Make sure this is initialized on the off-chance
+			 * that some raving loonie would apply DST to UTC.
+			 */
+			tm.tm_isdst = -1;
+#if defined(HAVE_TIMEGM)
+			t = timegm(&tm);
+#else
+			/*
+			 * Ahh, another POSIX_STUPIDITY, how unexpected.
+			 * Instead of, as would have been logical, to have
+			 * tm_timezone element, mktime() is standardized as
+			 * always working in localtime.  This brilliant idea
+			 * came from the same people who said "leap-seconds ?
+			 * Naah, screw it!".
+			 *
+			 * On broken systems without a working timegm(),
+			 * it is the responsibility of the calling program
+			 * to set the timezone to UTC.  We check that.
+			 */
+			t = mktime(&tm);
+			assert(!strcmp(tzname[0], "UTC"));
+#endif
+			return (t);
+		}
+	}
+	return (0);
+}
+
+void
+VTIM_sleep(double t)
+{
+#ifdef HAVE_NANOSLEEP
+	struct timespec ts;
+
+	ts = VTIM_timespec(t);
+
+	(void)nanosleep(&ts, NULL);
+#else
+	if (t >= 1.) {
+		(void)sleep(floor(t));
+		t -= floor(t);
+	}
+	/* XXX: usleep() is not mandated to be thread safe */
+	t *= 1e6;
+	if (t > 0)
+		(void)usleep(floor(t));
+#endif
+}
+
+struct timeval
+VTIM_timeval(double t)
+{
+	struct timeval tv;
+
+	tv.tv_sec = (time_t)trunc(t);
+	tv.tv_usec = (int)(1e6 * (t - tv.tv_sec));
+	return (tv);
+}
+
+struct timespec
+VTIM_timespec(double t)
+{
+	struct timespec tv;
+
+	tv.tv_sec = (time_t)trunc(t);
+	tv.tv_nsec = (int)(1e9 * (t - tv.tv_sec));
+	return (tv);
+}
+
+
+#ifdef TEST_DRIVER
+
+#include <stdlib.h>
+
+/*
+ * Compile with:
+ *  cc -o foo -DTEST_DRIVER -I../.. -I../../include time.c assert.c
+ * (Solaris)
+ *  cc -o foo -DTEST_DRIVER -I../.. -I../../include -lm time.c assert.c
+ * Test with:
+ *  env TZ=UTC ./foo
+ *  env TZ=CET ./foo
+ */
+
+static void
+tst(const char *s, time_t good)
+{
+	time_t t;
+	char buf[BUFSIZ];
+
+	t = VTIM_parse(s);
+	VTIM_format(t, buf);
+	printf("%-30s -> %12jd -> %s\n", s, (intmax_t)t, buf);
+	if (t != good) {
+		printf("Parse error! Got: %jd should have %jd diff %jd\n",
+		    (intmax_t)t, (intmax_t)good, (intmax_t)(t - good));
+		exit (2);
+	}
+}
+
+static int
+tst_delta_check(const char *name, double begin, double end, double ref)
+{
+	const double tol_max = 1.1;
+	const double tol_min = 1;
+
+	printf("%s delta for %fs sleep: %f\n", name, ref, (end - begin));
+
+	if ((end - begin) > tol_max * ref) {
+		printf("%s delta above tolerance: ((%f - %f) = %f) > %f\n",
+		    name, end, begin, (end - begin), tol_max);
+		return (1);
+	} else if ((end - begin) < tol_min * ref) {
+		printf("%s delta below tolerance: ((%f - %f) = %f) < %f\n",
+		    name, end, begin, (end - begin), tol_min);
+		return (1);
+	}
+	return (0);
+}
+
+static void
+tst_delta()
+{
+	double m_begin, m_end;
+	double r_begin, r_end;
+	const double ref = 1;
+	int err = 0;
+
+	r_begin = VTIM_real();
+	m_begin = VTIM_mono();
+	VTIM_sleep(ref);
+	r_end = VTIM_real();
+	m_end = VTIM_mono();
+
+	err += tst_delta_check("VTIM_mono", m_begin, m_end, ref);
+	err += tst_delta_check("VTIM_real", r_begin, r_end, ref);
+
+	if (err) {
+		printf("%d time delta test errrors\n", err);
+		exit (2);
+	}
+}
+
+int
+main(int argc, char **argv)
+{
+	time_t t;
+	char buf[BUFSIZ];
+
+	time(&t);
+	memset(buf, 0x55, sizeof buf);
+	VTIM_format(t, buf);
+	printf("scan = %d <%s>\n", VTIM_parse(buf), buf);
+
+	/* Examples from RFC2616 section 3.3.1 */
+	tst("Sun, 06 Nov 1994 08:49:37 GMT", 784111777);
+	tst("Sunday, 06-Nov-94 08:49:37 GMT", 784111777);
+	tst("Sun Nov  6 08:49:37 1994", 784111777);
+
+	tst_delta();
+
+	return (0);
+}
+#endif



More information about the varnish-commit mailing list