[experimental-ims] b52b3a6 Start separating out the worker thread from the worker pool stuff.

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


commit b52b3a688e2d13c57034a53bdd09565209e81c78
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat Sep 17 09:32:18 2011 +0000

    Start separating out the worker thread from the worker pool stuff.

diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index 0e0bc83..288f1f1 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -48,6 +48,7 @@ varnishd_SOURCES = \
 	cache_vrt_re.c \
 	cache_vrt_var.c \
 	cache_vrt_vmod.c \
+	cache_wrk.c \
 	cache_wrw.c \
 	cache_ws.c \
 	hash_classic.c \
diff --git a/bin/varnishd/cache.h b/bin/varnishd/cache.h
index e2faf30..1abd6db 100644
--- a/bin/varnishd/cache.h
+++ b/bin/varnishd/cache.h
@@ -840,8 +840,11 @@ void PipeSession(struct sess *sp);
 
 /* cache_pool.c */
 void WRK_Init(void);
+void WRK2_Init(void);
 int WRK_QueueSession(struct sess *sp);
 void WRK_SumStat(struct worker *w);
+int WRK_TrySumStat(struct worker *w);
+void WRK_thread_real(void *priv, struct worker *w);
 
 #define WRW_IsReleased(w)	((w)->wrw.wfd == NULL)
 int WRW_Error(const struct worker *w);
@@ -932,6 +935,10 @@ void ESI_DeliverChild(const struct sess *);
 /* cache_vrt_vmod.c */
 void VMOD_Init(void);
 
+/* cache_wrk.c */
+
+void *WRK_thread(void *priv);
+
 /* cache_ws.c */
 
 void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);
diff --git a/bin/varnishd/cache_pool.c b/bin/varnishd/cache_pool.c
index 0875e07..8286162 100644
--- a/bin/varnishd/cache_pool.c
+++ b/bin/varnishd/cache_pool.c
@@ -82,73 +82,16 @@ static unsigned			nthr_max;
 
 static pthread_cond_t		herder_cond;
 static struct lock		herder_mtx;
-static struct lock		wstat_mtx;
 
 /*--------------------------------------------------------------------*/
 
-static void
-wrk_sumstat(struct worker *w)
-{
-
-	Lck_AssertHeld(&wstat_mtx);
-#define L0(n)
-#define L1(n) (VSC_C_main->n += w->stats.n)
-#define VSC_DO_MAIN
-#define VSC_F(n, t, l, f, d) L##l(n);
-#include "vsc_fields.h"
-#undef VSC_F
-#undef VSC_DO_MAIN
-#undef L0
-#undef L1
-	memset(&w->stats, 0, sizeof w->stats);
-}
-
 void
-WRK_SumStat(struct worker *w)
-{
-
-	Lck_Lock(&wstat_mtx);
-	wrk_sumstat(w);
-	Lck_Unlock(&wstat_mtx);
-}
-
-/*--------------------------------------------------------------------*/
-
-static void *
-wrk_thread_real(struct wq *qp, unsigned shm_workspace, unsigned sess_workspace,
-    uint16_t nhttp, unsigned http_space, unsigned siov)
+WRK_thread_real(void *priv, struct worker *w)
 {
-	struct worker *w, ww;
-	uint32_t wlog[shm_workspace / 4];
-	/* XXX: can we trust these to be properly aligned ? */
-	unsigned char ws[sess_workspace];
-	unsigned char http0[http_space];
-	unsigned char http1[http_space];
-	unsigned char http2[http_space];
-	struct iovec iov[siov];
-	struct SHA256Context sha256;
+	struct wq *qp;
 	int stats_clean;
 
-	THR_SetName("cache-worker");
-	w = &ww;
-	memset(w, 0, sizeof *w);
-	w->magic = WORKER_MAGIC;
-	w->lastused = NAN;
-	w->wlb = w->wlp = wlog;
-	w->wle = wlog + (sizeof wlog) / 4;
-	w->sha256ctx = &sha256;
-	w->bereq = HTTP_create(http0, nhttp);
-	w->beresp = HTTP_create(http1, nhttp);
-	w->resp = HTTP_create(http2, nhttp);
-	w->wrw.iov = iov;
-	w->wrw.siov = siov;
-	w->wrw.ciov = siov;
-	AZ(pthread_cond_init(&w->cond, NULL));
-
-	WS_Init(w->ws, "wrk", ws, sess_workspace);
-
-	VSL(SLT_WorkThread, 0, "%p start", w);
-
+	CAST_OBJ_NOTNULL(qp, priv, WQ_MAGIC);
 	Lck_Lock(&qp->mtx);
 	qp->nthr++;
 	stats_clean = 1;
@@ -195,43 +138,11 @@ wrk_thread_real(struct wq *qp, unsigned shm_workspace, unsigned sess_workspace,
 			if (w->vcl != NULL)
 				VCL_Rel(&w->vcl);
 		}
-		if (!Lck_Trylock(&wstat_mtx)) {
-			wrk_sumstat(w);
-			Lck_Unlock(&wstat_mtx);
-			stats_clean = 1;
-		}
+		stats_clean = WRK_TrySumStat(w);
 		Lck_Lock(&qp->mtx);
 	}
 	qp->nthr--;
 	Lck_Unlock(&qp->mtx);
-
-	VSL(SLT_WorkThread, 0, "%p end", w);
-	if (w->vcl != NULL)
-		VCL_Rel(&w->vcl);
-	AZ(pthread_cond_destroy(&w->cond));
-	HSH_Cleanup(w);
-	WRK_SumStat(w);
-	return (NULL);
-}
-
-static void *
-wrk_thread(void *priv)
-{
-	struct wq *qp;
-	uint16_t nhttp;
-	unsigned siov;
-
-	CAST_OBJ_NOTNULL(qp, priv, WQ_MAGIC);
-	assert(params->http_max_hdr <= 65535);
-	/* We need to snapshot these two for consistency */
-	nhttp = (uint16_t)params->http_max_hdr;
-	siov = nhttp * 2;
-	if (siov > IOV_MAX)
-		siov = IOV_MAX;
-	return (wrk_thread_real(qp,
-	    params->shm_workspace,
-	    params->wthread_workspace,
-	    nhttp, HTTP_estimate(nhttp), siov));
 }
 
 /*--------------------------------------------------------------------
@@ -481,7 +392,7 @@ wrk_breed_flock(struct wq *qp, const pthread_attr_t *tp_attr)
 	    qp->lqueue > qp->last_lqueue)) {	/* not getting better since last */
 		if (qp->nthr >= nthr_max) {
 			VSC_C_main->n_wrk_max++;
-		} else if (pthread_create(&tp, tp_attr, wrk_thread, qp)) {
+		} else if (pthread_create(&tp, tp_attr, WRK_thread, qp)) {
 			VSL(SLT_Debug, 0, "Create worker thread failed %d %s",
 			    errno, strerror(errno));
 			VSC_C_main->n_wrk_failed++;
@@ -600,13 +511,12 @@ WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func, void *priv)
 /*--------------------------------------------------------------------*/
 
 void
-WRK_Init(void)
+WRK2_Init(void)
 {
 	pthread_t tp;
 
 	AZ(pthread_cond_init(&herder_cond, NULL));
 	Lck_New(&herder_mtx, lck_herder);
-	Lck_New(&wstat_mtx, lck_wstat);
 
 	wrk_addpools(params->wthread_pools);
 	AZ(pthread_create(&tp, NULL, wrk_herdtimer_thread, NULL));
diff --git a/bin/varnishd/cache_wrk.c b/bin/varnishd/cache_wrk.c
new file mode 100644
index 0000000..5b78c19
--- /dev/null
+++ b/bin/varnishd/cache_wrk.c
@@ -0,0 +1,160 @@
+/*-
+ * 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.
+ *
+ * Worker thread stuff unrealted to the worker thread pools.
+ */
+
+#include "config.h"
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "vcl.h"
+#include "cli_priv.h"
+#include "cache.h"
+#include "stevedore.h"
+#include "hash_slinger.h"
+#include "vsha256.h"
+
+static struct lock		wstat_mtx;
+
+/*--------------------------------------------------------------------*/
+
+static void
+wrk_sumstat(struct worker *w)
+{
+
+	Lck_AssertHeld(&wstat_mtx);
+#define L0(n)
+#define L1(n) (VSC_C_main->n += w->stats.n)
+#define VSC_DO_MAIN
+#define VSC_F(n, t, l, f, d) L##l(n);
+#include "vsc_fields.h"
+#undef VSC_F
+#undef VSC_DO_MAIN
+#undef L0
+#undef L1
+	memset(&w->stats, 0, sizeof w->stats);
+}
+
+void
+WRK_SumStat(struct worker *w)
+{
+
+	Lck_Lock(&wstat_mtx);
+	wrk_sumstat(w);
+	Lck_Unlock(&wstat_mtx);
+}
+
+int
+WRK_TrySumStat(struct worker *w)
+{
+	if (Lck_Trylock(&wstat_mtx)) 
+		return (0);
+	wrk_sumstat(w);
+	Lck_Unlock(&wstat_mtx);
+	return (1);
+}
+
+/*--------------------------------------------------------------------*/
+
+static void *
+wrk_thread_real(void *priv, unsigned shm_workspace, unsigned sess_workspace,
+    uint16_t nhttp, unsigned http_space, unsigned siov)
+{
+	struct worker *w, ww;
+	uint32_t wlog[shm_workspace / 4];
+	/* XXX: can we trust these to be properly aligned ? */
+	unsigned char ws[sess_workspace];
+	unsigned char http0[http_space];
+	unsigned char http1[http_space];
+	unsigned char http2[http_space];
+	struct iovec iov[siov];
+	struct SHA256Context sha256;
+
+	THR_SetName("cache-worker");
+	w = &ww;
+	memset(w, 0, sizeof *w);
+	w->magic = WORKER_MAGIC;
+	w->lastused = NAN;
+	w->wlb = w->wlp = wlog;
+	w->wle = wlog + (sizeof wlog) / 4;
+	w->sha256ctx = &sha256;
+	w->bereq = HTTP_create(http0, nhttp);
+	w->beresp = HTTP_create(http1, nhttp);
+	w->resp = HTTP_create(http2, nhttp);
+	w->wrw.iov = iov;
+	w->wrw.siov = siov;
+	w->wrw.ciov = siov;
+	AZ(pthread_cond_init(&w->cond, NULL));
+
+	WS_Init(w->ws, "wrk", ws, sess_workspace);
+
+	VSL(SLT_WorkThread, 0, "%p start", w);
+
+	WRK_thread_real(priv, w);
+
+	VSL(SLT_WorkThread, 0, "%p end", w);
+	if (w->vcl != NULL)
+		VCL_Rel(&w->vcl);
+	AZ(pthread_cond_destroy(&w->cond));
+	HSH_Cleanup(w);
+	WRK_SumStat(w);
+	return (NULL);
+}
+
+void *
+WRK_thread(void *priv)
+{
+	uint16_t nhttp;
+	unsigned siov;
+
+	assert(params->http_max_hdr <= 65535);
+	/* We need to snapshot these two for consistency */
+	nhttp = (uint16_t)params->http_max_hdr;
+	siov = nhttp * 2;
+	if (siov > IOV_MAX)
+		siov = IOV_MAX;
+	return (wrk_thread_real(priv,
+	    params->shm_workspace,
+	    params->wthread_workspace,
+	    nhttp, HTTP_estimate(nhttp), siov));
+}
+
+void
+WRK_Init(void)
+{
+	Lck_New(&wstat_mtx, lck_wstat);
+	WRK2_Init();
+}



More information about the varnish-commit mailing list