[master] 6340ce0 Drop the incestous relationship between VSM and VSC, and stack VSC neatly on top of VSM.

Poul-Henning Kamp phk at FreeBSD.org
Wed Sep 6 20:56:05 UTC 2017


commit 6340ce0eb63327d61e1ec4266922c9cbf8871d5d
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Sep 6 20:54:50 2017 +0000

    Drop the incestous relationship between VSM and VSC, and stack
    VSC neatly on top of VSM.

diff --git a/bin/varnishstat/varnishstat.c b/bin/varnishstat/varnishstat.c
index c9270cf..1f24b25 100644
--- a/bin/varnishstat/varnishstat.c
+++ b/bin/varnishstat/varnishstat.c
@@ -74,7 +74,7 @@ do_xml_cb(void *priv, const struct VSC_point * const pt)
 }
 
 static void
-do_xml(struct vsm *vd)
+do_xml(struct vsc *vsc)
 {
 	char time_stamp[20];
 	time_t now;
@@ -83,7 +83,7 @@ do_xml(struct vsm *vd)
 	now = time(NULL);
 	(void)strftime(time_stamp, 20, "%Y-%m-%dT%H:%M:%S", localtime(&now));
 	printf("<varnishstat timestamp=\"%s\">\n", time_stamp);
-	(void)VSC_Iter(vd, NULL, do_xml_cb, NULL);
+	(void)VSC_Iter(vsc, NULL, do_xml_cb, NULL);
 	printf("</varnishstat>\n");
 }
 
@@ -124,7 +124,7 @@ do_json_cb(void *priv, const struct VSC_point * const pt)
 }
 
 static void
-do_json(struct vsm *vd)
+do_json(struct vsc *vsc)
 {
 	char time_stamp[20];
 	time_t now;
@@ -137,7 +137,7 @@ do_json(struct vsm *vd)
 
 	(void)strftime(time_stamp, 20, "%Y-%m-%dT%H:%M:%S", localtime(&now));
 	printf("  \"timestamp\": \"%s\",\n", time_stamp);
-	(void)VSC_Iter(vd, NULL, do_json_cb, &jp);
+	(void)VSC_Iter(vsc, NULL, do_json_cb, &jp);
 	printf("\n}\n");
 }
 
@@ -194,15 +194,15 @@ do_once_cb(void *priv, const struct VSC_point * const pt)
 }
 
 static void
-do_once(struct vsm *vd)
+do_once(struct vsc *vsc)
 {
 	struct once_priv op;
 
 	memset(&op, 0, sizeof op);
 	op.pad = 18;
 
-	(void)VSC_Iter(vd, NULL, do_once_cb_first, &op);
-	(void)VSC_Iter(vd, NULL, do_once_cb, &op);
+	(void)VSC_Iter(vsc, NULL, do_once_cb_first, &op);
+	(void)VSC_Iter(vsc, NULL, do_once_cb, &op);
 }
 
 /*--------------------------------------------------------------------*/
@@ -226,13 +226,13 @@ do_list_cb(void *priv, const struct VSC_point * const pt)
 }
 
 static void
-list_fields(struct vsm *vd)
+list_fields(struct vsc *vsc)
 {
 	printf("Varnishstat -f option fields:\n");
 	printf("Field name                     Description\n");
 	printf("----------                     -----------\n");
 
-	(void)VSC_Iter(vd, NULL, do_list_cb, NULL);
+	(void)VSC_Iter(vsc, NULL, do_list_cb, NULL);
 }
 
 /*--------------------------------------------------------------------*/
@@ -256,10 +256,13 @@ main(int argc, char * const *argv)
 	int once = 0, xml = 0, json = 0, f_list = 0, curses = 0;
 	signed char opt;
 	int i;
+	struct vsc *vsc;
 
 	VUT_Init(progname, argc, argv, &vopt_spec);
 	vd = VSM_New();
 	AN(vd);
+	vsc = VSC_New(vd);
+	AN(vsc);
 
 	while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
 		switch (opt) {
@@ -282,7 +285,7 @@ main(int argc, char * const *argv)
 			json = 1;
 			break;
 		default:
-			i = VSC_Arg(vd, opt, optarg);
+			i = VSC_Arg(vsc, opt, optarg);
 			if (i < 0)
 				VUT_Error(1, "%s", VSM_Error(vd));
 			if (!i)
@@ -300,15 +303,15 @@ main(int argc, char * const *argv)
 		VUT_Error(1, "%s", VSM_Error(vd));
 
 	if (curses)
-		do_curses(vd, 1.0);
+		do_curses(vd, vsc, 1.0);
 	else if (xml)
-		do_xml(vd);
+		do_xml(vsc);
 	else if (json)
-		do_json(vd);
+		do_json(vsc);
 	else if (once)
-		do_once(vd);
+		do_once(vsc);
 	else if (f_list)
-		list_fields(vd);
+		list_fields(vsc);
 	else
 		assert(0);
 
diff --git a/bin/varnishstat/varnishstat.h b/bin/varnishstat/varnishstat.h
index 54fd0ba..6a73ef2 100644
--- a/bin/varnishstat/varnishstat.h
+++ b/bin/varnishstat/varnishstat.h
@@ -35,4 +35,4 @@
 #include "vas.h"
 #include "vcs.h"
 
-void do_curses(struct vsm *vd, double delay);
+void do_curses(struct vsm *,  struct vsc *, double);
diff --git a/bin/varnishstat/varnishstat_curses.c b/bin/varnishstat/varnishstat_curses.c
index 7d99c7e..104af2b 100644
--- a/bin/varnishstat/varnishstat_curses.c
+++ b/bin/varnishstat/varnishstat_curses.c
@@ -313,7 +313,7 @@ build_pt_list_cb(void *priv, const struct VSC_point *vpt)
 }
 
 static void
-build_pt_list(struct vsm *vd, struct vsm_fantom *fantom)
+build_pt_list(struct vsc *vsc, struct vsm_fantom *fantom)
 {
 	struct pt_priv pt_priv;
 	int i;
@@ -334,7 +334,7 @@ build_pt_list(struct vsm *vd, struct vsm_fantom *fantom)
 	main_cache_hit = NULL;
 	main_cache_miss = NULL;
 
-	(void)VSC_Iter(vd, fantom, build_pt_list_cb, &pt_priv);
+	(void)VSC_Iter(vsc, fantom, build_pt_list_cb, &pt_priv);
 	delete_pt_list();
 	AN(VTAILQ_EMPTY(&ptlist));
 	AZ(n_ptlist);
@@ -1044,7 +1044,7 @@ handle_keypress(int ch)
 }
 
 void
-do_curses(struct vsm *vd, double delay)
+do_curses(struct vsm *vd, struct vsc *vsc, double delay)
 {
 	struct pollfd pollfd;
 	long t;
@@ -1073,7 +1073,7 @@ do_curses(struct vsm *vd, double delay)
 		    (VSM_Status(vd) & (VSM_MGT_CHANGED|VSM_WRK_CHANGED))) {
 			init_hitrate();
 			delete_pt_list();
-			build_pt_list(vd, &f_iter);
+			build_pt_list(vsc, &f_iter);
 			initial = 0;
 		}
 
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index 7a10731..45218bc 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -78,6 +78,7 @@ struct varnish {
 
 	struct vsm		*vsm_vsl;
 	struct vsm		*vsm_vsc;
+	struct vsc		*vsc;
 	int			has_a_arg;
 
 	unsigned		vsl_tag_count[256];
@@ -521,7 +522,10 @@ varnish_launch(struct varnish *v)
 	free(r);
 
 	v->vsm_vsc = VSM_New();
-	(void)VSM_Arg(v->vsm_vsc, 'n', v->workdir);
+	AN(v->vsm_vsc);
+	v->vsc = VSC_New(v->vsm_vsc);
+	AN(v->vsc);
+	(void)VSC_Arg(v->vsc, 'n', v->workdir);
 	AZ(VSM_Attach(v->vsm_vsc, -1));
 
 	v->vsm_vsl = VSM_New();
@@ -846,7 +850,7 @@ varnish_vsc(const struct varnish *v, const char *arg)
 	dp.arg = arg;
 	(void)VSM_Status(v->vsm_vsc);
 
-	(void)VSC_Iter(v->vsm_vsc, NULL, do_stat_dump_cb, &dp);
+	(void)VSC_Iter(v->vsc, NULL, do_stat_dump_cb, &dp);
 }
 
 /**********************************************************************
@@ -912,7 +916,7 @@ varnish_expect(const struct varnish *v, char * const *av)
 	for (i = 0; i < 50; i++, (void)usleep(100000)) {
 		(void)VSM_Status(v->vsm_vsc);
 
-		good = VSC_Iter(v->vsm_vsc, NULL, do_expect_cb, &sp);
+		good = VSC_Iter(v->vsc, NULL, do_expect_cb, &sp);
 		if (!good) {
 			good = -2;
 			continue;
diff --git a/include/vapi/vsc.h b/include/vapi/vsc.h
index 7a87f70..97c85e1 100644
--- a/include/vapi/vsc.h
+++ b/include/vapi/vsc.h
@@ -38,13 +38,17 @@
 #include "vapi/vsc_int.h"
 
 struct vsm;
+struct vsc;
 struct vsm_fantom;
 
 /*---------------------------------------------------------------------
  * VSC level access functions
  */
 
-int VSC_Arg(struct vsm *vd, char arg, const char *opt);
+struct vsc *VSC_New(struct vsm *);
+void VSC_Destroy(struct vsc **);
+
+int VSC_Arg(struct vsc *, char arg, const char *opt);
 	/*
 	 * Handle standard stat-presenter arguments
 	 * Return:
@@ -80,8 +84,7 @@ void VSC_Destroy_Point(struct VSC_point **);
 
 typedef int VSC_iter_f(void *priv, const struct VSC_point *const pt);
 
-int VSC_Iter(struct vsm *vd, struct vsm_fantom *fantom, VSC_iter_f *func,
-    void *priv);
+int VSC_Iter(struct vsc *, struct vsm_fantom *, VSC_iter_f *func, void *priv);
 	/*
 	 * Iterate over all statistics counters, calling "func" for
 	 * each counter not suppressed by any "-f" arguments.
diff --git a/lib/libvarnishapi/Makefile.am b/lib/libvarnishapi/Makefile.am
index fbd4014..d0e8b64 100644
--- a/lib/libvarnishapi/Makefile.am
+++ b/lib/libvarnishapi/Makefile.am
@@ -14,7 +14,6 @@ libvarnishapi_la_LDFLAGS = $(AM_LDFLAGS) -version-info 1:6:0
 libvarnishapi_la_SOURCES = \
 	vjsn.c \
 	vjsn.h \
-	vsc_priv.h \
 	vsl_api.h \
 	vxp.h \
 	vxp_tokens.h \
diff --git a/lib/libvarnishapi/libvarnishapi.map b/lib/libvarnishapi/libvarnishapi.map
index 6a7cab2..b5d1b00 100644
--- a/lib/libvarnishapi/libvarnishapi.map
+++ b/lib/libvarnishapi/libvarnishapi.map
@@ -193,4 +193,6 @@ LIBVARNISHAPI_1.7 {
 	VSM_Status;
 	VSM_Arg;
 	VSM_Dup;
+	VSC_New;
+	VSC_Destroy;
 } LIBVARNISHAPI_1.0;
diff --git a/lib/libvarnishapi/vsc.c b/lib/libvarnishapi/vsc.c
index cd933ad..de998cf 100644
--- a/lib/libvarnishapi/vsc.c
+++ b/lib/libvarnishapi/vsc.c
@@ -51,8 +51,6 @@
 #include "vapi/vsc.h"
 #include "vapi/vsm.h"
 
-#include "vsc_priv.h"
-
 struct vsc_sf {
 	unsigned		magic;
 #define VSC_SF_MAGIC		0x558478dd
@@ -65,6 +63,7 @@ struct vsc {
 	unsigned		magic;
 #define VSC_MAGIC		0x3373554a
 
+	struct vsm		*vsm;
 	struct vsc_sf_head	sf_list_include;
 	struct vsc_sf_head	sf_list_exclude;
 };
@@ -116,20 +115,18 @@ VSC_Destroy_Point(struct VSC_point **p)
 
 /*--------------------------------------------------------------------*/
 
-static struct vsc *
-vsc_setup(struct vsm *vd)
+struct vsc *
+VSC_New(struct vsm *vsm)
 {
 	struct vsc *vsc;
 
-	vsc = VSM_GetVSC(vd);
-	if (vsc == NULL) {
-		ALLOC_OBJ(vsc, VSC_MAGIC);
-		AN(vsc);
-		VTAILQ_INIT(&vsc->sf_list_include);
-		VTAILQ_INIT(&vsc->sf_list_exclude);
-		VSM_SetVSC(vd, vsc);
-	}
-	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
+	AN(vsm);
+	ALLOC_OBJ(vsc, VSC_MAGIC);
+	if (vsc == NULL)
+		return (vsc);
+	vsc->vsm = vsm;
+	VTAILQ_INIT(&vsc->sf_list_include);
+	VTAILQ_INIT(&vsc->sf_list_exclude);
 	return (vsc);
 }
 
@@ -150,10 +147,11 @@ vsc_delete_sf_list(struct vsc_sf_head *head)
 }
 
 void
-VSC_Delete(struct vsc *vsc)
+VSC_Destroy(struct vsc **vscp)
 {
+	struct vsc *vsc;
 
-	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
+	TAKE_OBJ_NOTNULL(vsc, vscp, VSC_MAGIC);
 	vsc_delete_sf_list(&vsc->sf_list_include);
 	vsc_delete_sf_list(&vsc->sf_list_exclude);
 	FREE_OBJ(vsc);
@@ -162,13 +160,11 @@ VSC_Delete(struct vsc *vsc)
 /*--------------------------------------------------------------------*/
 
 static int
-vsc_f_arg(struct vsm *vd, const char *opt)
+vsc_f_arg(struct vsc *vsc, const char *opt)
 {
-	struct vsc *vsc = vsc_setup(vd);
 	struct vsc_sf *sf;
 	unsigned exclude = 0;
 
-	AN(vd);
 	AN(opt);
 
 	ALLOC_OBJ(sf, VSC_SF_MAGIC);
@@ -193,14 +189,16 @@ vsc_f_arg(struct vsm *vd, const char *opt)
 /*--------------------------------------------------------------------*/
 
 int
-VSC_Arg(struct vsm *vd, char arg, const char *opt)
+VSC_Arg(struct vsc *vsc, char arg, const char *opt)
 {
 
+	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
+
 	switch (arg) {
-	case 'f': return (vsc_f_arg(vd, opt));
+	case 'f': return (vsc_f_arg(vsc, opt));
 	case 'n':
 	case 't':
-		return (VSM_Arg(vd, arg, opt));
+		return (VSM_Arg(vsc->vsm, arg, opt));
 	default:
 		return (0);
 	}
@@ -210,11 +208,11 @@ VSC_Arg(struct vsm *vd, char arg, const char *opt)
  */
 
 static int
-vsc_filter(struct vsm *vd, const char *nm)
+vsc_filter(const struct vsc *vsc, const char *nm)
 {
-	struct vsc *vsc = vsc_setup(vd);
 	struct vsc_sf *sf;
 
+	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
 	VTAILQ_FOREACH(sf, &vsc->sf_list_exclude, list)
 		if (!fnmatch(sf->pattern, nm, 0))
 			return (1);
@@ -230,12 +228,13 @@ vsc_filter(struct vsm *vd, const char *nm)
  */
 
 static int
-vsc_iter_elem(struct vsm *vd, const struct vsm_fantom *fantom,
+vsc_iter_elem(const struct vsc *vsc, const struct vsm_fantom *fantom,
     const struct vjsn_val *vv, struct vsb *vsb, VSC_iter_f *func, void *priv)
 {
 	struct VSC_point	point;
 	struct vjsn_val *vt;
 
+	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
 	memset(&point, 0, sizeof point);
 
 	vt = vjsn_child(vv, "name");
@@ -246,7 +245,7 @@ vsc_iter_elem(struct vsm *vd, const struct vsm_fantom *fantom,
 	VSB_printf(vsb, "%s.%s", fantom->ident, vt->value);
 	AZ(VSB_finish(vsb));
 
-	if (vsc_filter(vd, VSB_data(vsb)))
+	if (vsc_filter(vsc, VSB_data(vsb)))
 		return (0);
 
 	point.name = VSB_data(vsb);
@@ -315,7 +314,7 @@ vsc_iter_elem(struct vsm *vd, const struct vsm_fantom *fantom,
 }
 
 static int
-vsc_iter_fantom(struct vsm *vd, const struct vsm_fantom *fantom,
+vsc_iter_fantom(const struct vsc *vsc, const struct vsm_fantom *fantom,
     struct vsb *vsb, VSC_iter_f *func, void *priv)
 {
 	int i = 0;
@@ -324,6 +323,8 @@ vsc_iter_fantom(struct vsm *vd, const struct vsm_fantom *fantom,
 	struct vjsn *vj;
 	struct vjsn_val *vv, *vve;
 
+	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
+
 	p = (char*)fantom->b + 8 + vbe64dec(fantom->b);
 	assert (p < (char*)fantom->e);
 	vj = vjsn_parse(p, &e);
@@ -332,7 +333,7 @@ vsc_iter_fantom(struct vsm *vd, const struct vsm_fantom *fantom,
 	vve = vjsn_child(vj->value, "elem");
 	AN(vve);
 	VTAILQ_FOREACH(vv, &vve->children, list) {
-		i = vsc_iter_elem(vd, fantom, vv, vsb, func, priv);
+		i = vsc_iter_elem(vsc, fantom, vv, vsb, func, priv);
 		if (i)
 			break;
 	}
@@ -343,21 +344,21 @@ vsc_iter_fantom(struct vsm *vd, const struct vsm_fantom *fantom,
 /*--------------------------------------------------------------------
  */
 
-int
-VSC_Iter(struct vsm *vd, struct vsm_fantom *fantom, VSC_iter_f *func,
-    void *priv)
+int __match_proto__()	// We don't want vsc to be const
+VSC_Iter(struct vsc *vsc, struct vsm_fantom *f, VSC_iter_f *func, void *priv)
 {
 	struct vsm_fantom	ifantom;
 	uint64_t u;
 	int i = 0;
 	struct vsb *vsb;
 
+	CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
 	vsb = VSB_new_auto();
 	AN(vsb);
-	VSM_FOREACH(&ifantom, vd) {
+	VSM_FOREACH(&ifantom, vsc->vsm) {
 		if (strcmp(ifantom.class, VSC_CLASS))
 			continue;
-		AZ(VSM_Map(vd, &ifantom));
+		AZ(VSM_Map(vsc->vsm, &ifantom));
 		u = vbe64dec(ifantom.b);
 		if (u == 0) {
 			VRMB();
@@ -365,11 +366,12 @@ VSC_Iter(struct vsm *vd, struct vsm_fantom *fantom, VSC_iter_f *func,
 			u = vbe64dec(ifantom.b);
 		}
 		assert(u > 0);
-		if (fantom != NULL)
-			*fantom = ifantom;
-		i = vsc_iter_fantom(vd, &ifantom, vsb, func, priv);
-		if (fantom == NULL)
-			AZ(VSM_Unmap(vd, &ifantom));
+		i = vsc_iter_fantom(vsc, &ifantom, vsb, func, priv);
+		if (f != NULL) {
+			*f = ifantom;
+		} else {
+			AZ(VSM_Unmap(vsc->vsm, &ifantom));
+		}
 		if (i)
 			break;
 	}
diff --git a/lib/libvarnishapi/vsc_priv.h b/lib/libvarnishapi/vsc_priv.h
deleted file mode 100644
index 48e24c6..0000000
--- a/lib/libvarnishapi/vsc_priv.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2015 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.
- *
- */
-
-struct vsc;
-struct vsm;
-
-void VSM_SetVSC(struct vsm *, struct vsc *);
-struct vsc *VSM_GetVSC(const struct vsm *);
-void VSC_Delete(struct vsc *);
diff --git a/lib/libvarnishapi/vsm.c b/lib/libvarnishapi/vsm.c
index bbdc8ab..96c171b 100644
--- a/lib/libvarnishapi/vsm.c
+++ b/lib/libvarnishapi/vsm.c
@@ -52,7 +52,6 @@
 #include "vin.h"
 #include "vsb.h"
 #include "vsm_priv.h"
-#include "vsc_priv.h"
 #include "vqueue.h"
 #include "vtim.h"
 
@@ -115,7 +114,6 @@ struct vsm {
 	struct stat		dst;
 	char			*dname;
 
-	struct vsc		*vsc;
 	struct vsm_set		*mgt;
 	struct vsm_set		*child;
 
@@ -239,8 +237,6 @@ VSM_Destroy(struct vsm **vdp)
 
 	TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC);
 
-	if (vd->vsc != NULL)
-		VSC_Delete(vd->vsc);
 	VSM_ResetError(vd);
 	free(vd->dname);
 	vsm_delset(&vd->mgt);
@@ -252,24 +248,6 @@ VSM_Destroy(struct vsm **vdp)
 
 /*--------------------------------------------------------------------*/
 
-void
-VSM_SetVSC(struct vsm *vd, struct vsc *vsc)
-{
-	CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
-
-	vd->vsc = vsc;
-}
-
-struct vsc *
-VSM_GetVSC(const struct vsm *vd)
-{
-	CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
-
-	return (vd->vsc);
-}
-
-/*--------------------------------------------------------------------*/
-
 const char *
 VSM_Error(const struct vsm *vd)
 {


More information about the varnish-commit mailing list