[master] 0e9e4d1 Use VTLA VWP for poll waiter, turn it into an object.

Poul-Henning Kamp phk at varnish-cache.org
Sat Sep 17 12:32:15 CEST 2011


commit 0e9e4d1bf784e9cc875ca5928ed926667710f101
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat Sep 17 10:31:36 2011 +0000

    Use VTLA VWP for poll waiter, turn it into an object.

diff --git a/bin/varnishd/cache_waiter_poll.c b/bin/varnishd/cache_waiter_poll.c
index 528a880..44b4033 100644
--- a/bin/varnishd/cache_waiter_poll.c
+++ b/bin/varnishd/cache_waiter_poll.c
@@ -42,23 +42,29 @@
 
 #define NEEV	128
 
-static pthread_t vca_poll_thread;
-static struct pollfd *pollfd;
-static unsigned npoll, hpoll;
-
-static VTAILQ_HEAD(,sess) sesshead = VTAILQ_HEAD_INITIALIZER(sesshead);
+struct vwp {
+	unsigned		magic;
+#define VWP_MAGIC		0x4b2cc735
+	int			pipes[2];
+	pthread_t		poll_thread;
+	struct pollfd		*pollfd;
+	unsigned		npoll;
+	unsigned		hpoll;
+
+	VTAILQ_HEAD(,sess)	sesshead;
+};
 
 /*--------------------------------------------------------------------*/
 
 static void
-vca_pollspace(unsigned fd)
+vwp_pollspace(struct vwp *vwp, unsigned fd)
 {
-	struct pollfd *newpollfd = pollfd;
+	struct pollfd *newpollfd = vwp->pollfd;
 	unsigned newnpoll;
 
-	if (fd < npoll)
+	if (fd < vwp->npoll)
 		return;
-	newnpoll = npoll;
+	newnpoll = vwp->npoll;
 	if (newnpoll == 0)
 		newnpoll = 1;
 	while (fd >= newnpoll)
@@ -66,123 +72,125 @@ vca_pollspace(unsigned fd)
 	VSL(SLT_Debug, 0, "Acceptor poll space increased to %u", newnpoll);
 	newpollfd = realloc(newpollfd, newnpoll * sizeof *newpollfd);
 	XXXAN(newpollfd);
-	memset(newpollfd + npoll, 0, (newnpoll - npoll) * sizeof *newpollfd);
-	pollfd = newpollfd;
-	while (npoll < newnpoll)
-		pollfd[npoll++].fd = -1;
-	assert(fd < npoll);
+	memset(newpollfd + vwp->npoll, 0,
+	    (newnpoll - vwp->npoll) * sizeof *newpollfd);
+	vwp->pollfd = newpollfd;
+	while (vwp->npoll < newnpoll)
+		vwp->pollfd[vwp->npoll++].fd = -1;
+	assert(fd < vwp->npoll);
 }
 
 /*--------------------------------------------------------------------*/
 
 static void
-vca_poll(int fd)
+vwp_poll(struct vwp *vwp, int fd)
 {
 
 	assert(fd >= 0);
-	vca_pollspace((unsigned)fd);
-	assert(fd < npoll);
+	vwp_pollspace(vwp, (unsigned)fd);
+	assert(fd < vwp->npoll);
 
-	if (hpoll < fd)
-		hpoll = fd;
+	if (vwp->hpoll < fd)
+		vwp->hpoll = fd;
 
-	assert(pollfd[fd].fd == -1);
-	assert(pollfd[fd].events == 0);
-	assert(pollfd[fd].revents == 0);
+	assert(vwp->pollfd[fd].fd == -1);
+	assert(vwp->pollfd[fd].events == 0);
+	assert(vwp->pollfd[fd].revents == 0);
 
-	pollfd[fd].fd = fd;
-	pollfd[fd].events = POLLIN;
+	vwp->pollfd[fd].fd = fd;
+	vwp->pollfd[fd].events = POLLIN;
 }
 
 static void
-vca_unpoll(int fd)
+vwp_unpoll(struct vwp *vwp, int fd)
 {
 
 	assert(fd >= 0);
-	assert(fd < npoll);
-	vca_pollspace((unsigned)fd);
+	assert(fd < vwp->npoll);
+	vwp_pollspace(vwp, (unsigned)fd);
 
-	assert(pollfd[fd].fd == fd);
-	assert(pollfd[fd].events == POLLIN);
-	assert(pollfd[fd].revents == 0);
+	assert(vwp->pollfd[fd].fd == fd);
+	assert(vwp->pollfd[fd].events == POLLIN);
+	assert(vwp->pollfd[fd].revents == 0);
 
-	pollfd[fd].fd = -1;
-	pollfd[fd].events = 0;
+	vwp->pollfd[fd].fd = -1;
+	vwp->pollfd[fd].events = 0;
 }
 
 /*--------------------------------------------------------------------*/
 
 static void *
-vca_main(void *arg)
+vwp_main(void *priv)
 {
 	int v;
+	struct vwp *vwp;
 	struct sess *ss[NEEV], *sp, *sp2;
 	double deadline;
 	int i, j, fd;
 
+	CAST_OBJ_NOTNULL(vwp, priv, VWP_MAGIC);
 	THR_SetName("cache-poll");
-	(void)arg;
 
-	vca_poll(vca_pipes[0]);
+	vwp_poll(vwp, vwp->pipes[0]);
 
 	while (1) {
-		assert(hpoll < npoll);
-		while (hpoll > 0 && pollfd[hpoll].fd == -1)
-			hpoll--;
-		assert(vca_pipes[0] <= hpoll);
-		assert(pollfd[vca_pipes[0]].fd == vca_pipes[0]);
-		assert(pollfd[vca_pipes[1]].fd == -1);
-		v = poll(pollfd, hpoll + 1, 100);
+		assert(vwp->hpoll < vwp->npoll);
+		while (vwp->hpoll > 0 && vwp->pollfd[vwp->hpoll].fd == -1)
+			vwp->hpoll--;
+		assert(vwp->pipes[0] <= vwp->hpoll);
+		assert(vwp->pollfd[vwp->pipes[0]].fd == vwp->pipes[0]);
+		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;
-		VTAILQ_FOREACH_SAFE(sp, &sesshead, list, sp2) {
+		VTAILQ_FOREACH_SAFE(sp, &vwp->sesshead, list, sp2) {
 			if (v == 0)
 				break;
 			CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 			fd = sp->fd;
 			assert(fd >= 0);
-			assert(fd <= hpoll);
-			assert(fd < npoll);
-			assert(pollfd[fd].fd == fd);
-			if (pollfd[fd].revents) {
+			assert(fd <= vwp->hpoll);
+			assert(fd < vwp->npoll);
+			assert(vwp->pollfd[fd].fd == fd);
+			if (vwp->pollfd[fd].revents) {
 				v--;
 				i = HTC_Rx(sp->htc);
-				if (pollfd[fd].revents != POLLIN)
+				if (vwp->pollfd[fd].revents != POLLIN)
 					VSL(SLT_Debug, fd, "Poll: %x / %d",
-					    pollfd[fd].revents, i);
-				pollfd[fd].revents = 0;
-				VTAILQ_REMOVE(&sesshead, sp, list);
+					    vwp->pollfd[fd].revents, i);
+				vwp->pollfd[fd].revents = 0;
+				VTAILQ_REMOVE(&vwp->sesshead, sp, list);
 				if (i == 0) {
 					/* Mov to front of list for speed */
-					VTAILQ_INSERT_HEAD(&sesshead, sp, list);
+					VTAILQ_INSERT_HEAD(&vwp->sesshead, sp, list);
 				} else {
-					vca_unpoll(fd);
+					vwp_unpoll(vwp, fd);
 					vca_handover(sp, i);
 				}
 			} else if (sp->t_open <= deadline) {
-				VTAILQ_REMOVE(&sesshead, sp, list);
-				vca_unpoll(fd);
+				VTAILQ_REMOVE(&vwp->sesshead, sp, list);
+				vwp_unpoll(vwp, fd);
 				// XXX: not yet (void)VTCP_linger(sp->fd, 0);
 				vca_close_session(sp, "timeout");
 				SES_Delete(sp);
 			}
 		}
-		if (v && pollfd[vca_pipes[0]].revents) {
+		if (v && vwp->pollfd[vwp->pipes[0]].revents) {
 
-			if (pollfd[vca_pipes[0]].revents != POLLIN)
+			if (vwp->pollfd[vwp->pipes[0]].revents != POLLIN)
 				VSL(SLT_Debug, 0, "pipe.revents= 0x%x",
-				    pollfd[vca_pipes[0]].revents);
-			assert(pollfd[vca_pipes[0]].revents == POLLIN);
-			pollfd[vca_pipes[0]].revents = 0;
+				    vwp->pollfd[vwp->pipes[0]].revents);
+			assert(vwp->pollfd[vwp->pipes[0]].revents == POLLIN);
+			vwp->pollfd[vwp->pipes[0]].revents = 0;
 			v--;
-			i = read(vca_pipes[0], ss, sizeof ss);
+			i = read(vwp->pipes[0], ss, sizeof ss);
 			assert(i >= 0);
 			assert(((unsigned)i % sizeof ss[0]) == 0);
 			for (j = 0; j * sizeof ss[0] < i; j++) {
 				CHECK_OBJ_NOTNULL(ss[j], SESS_MAGIC);
 				assert(ss[j]->fd >= 0);
-				VTAILQ_INSERT_TAIL(&sesshead, ss[j], list);
-				vca_poll(ss[j]->fd);
+				VTAILQ_INSERT_TAIL(&vwp->sesshead, ss[j], list);
+				vwp_poll(vwp, ss[j]->fd);
 			}
 		}
 		assert(v == 0);
@@ -191,29 +199,37 @@ vca_main(void *arg)
 }
 
 /*--------------------------------------------------------------------*/
+
 static void
-vca_poll_pass(void *priv, struct sess *sp)
+vwp_poll_pass(void *priv, struct sess *sp)
 {
+	struct vwp *vwp;
 
-	(void)priv;
-	assert(sizeof sp == write(vca_pipes[1], &sp, sizeof sp));
+	CAST_OBJ_NOTNULL(vwp, priv, VWP_MAGIC);
+
+	assert(sizeof sp == write(vwp->pipes[1], &sp, sizeof sp));
 }
 
 /*--------------------------------------------------------------------*/
 
 static void *
-vca_poll_init(void)
+vwp_poll_init(void)
 {
-
-	vca_pollspace(256);
-	AZ(pthread_create(&vca_poll_thread, NULL, vca_main, NULL));
-	return (NULL);
+	struct vwp *vwp;
+
+	ALLOC_OBJ(vwp, VWP_MAGIC);
+	AN(vwp);
+	VTAILQ_INIT(&vwp->sesshead);
+	AZ(pipe(vwp->pipes));
+	vwp_pollspace(vwp, 256);
+	AZ(pthread_create(&vwp->poll_thread, NULL, vwp_main, vwp));
+	return (vwp);
 }
 
 /*--------------------------------------------------------------------*/
 
 struct waiter waiter_poll = {
 	.name =		"poll",
-	.init =		vca_poll_init,
-	.pass =		vca_poll_pass,
+	.init =		vwp_poll_init,
+	.pass =		vwp_poll_pass,
 };



More information about the varnish-commit mailing list