r4925 - in trunk/varnish-cache: bin/varnishadm bin/varnishstat include lib/libvarnishapi

phk at varnish-cache.org phk at varnish-cache.org
Tue Jun 8 10:42:16 CEST 2010


Author: phk
Date: 2010-06-08 10:42:15 +0200 (Tue, 08 Jun 2010)
New Revision: 4925

Modified:
   trunk/varnish-cache/bin/varnishadm/varnishadm.c
   trunk/varnish-cache/bin/varnishstat/varnishstat.c
   trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c
   trunk/varnish-cache/include/varnishapi.h
   trunk/varnish-cache/lib/libvarnishapi/vsl.c
   trunk/varnish-cache/lib/libvarnishapi/vsl.h
   trunk/varnish-cache/lib/libvarnishapi/vsl_arg.c
   trunk/varnish-cache/lib/libvarnishapi/vsl_log.c
   trunk/varnish-cache/lib/libvarnishapi/vsl_stat.c
Log:
Begin a major round of polishing of the VarnishAPI to make it sensible
general-ish and explainable.



Modified: trunk/varnish-cache/bin/varnishadm/varnishadm.c
===================================================================
--- trunk/varnish-cache/bin/varnishadm/varnishadm.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/bin/varnishadm/varnishadm.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -245,7 +245,7 @@
 	if (n_arg != NULL) {
 		vsd = VSL_New();
 		assert(VSL_Log_Arg(vsd, 'n', n_arg));
-		if (!VSL_Open(vsd)) {
+		if (!VSL_Open(vsd, 1)) {
 			if (T_arg == NULL) {
 				p = VSL_Find_Alloc(vsd, "Arg", "-T", "", NULL);
 				if (p != NULL) {

Modified: trunk/varnish-cache/bin/varnishstat/varnishstat.c
===================================================================
--- trunk/varnish-cache/bin/varnishstat/varnishstat.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/bin/varnishstat/varnishstat.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -207,7 +207,7 @@
 			(void)VSL_Stat_Arg(vd, c, optarg);
 			break;
 		case 'l':
-			if (VSL_Open(vd))
+			if (VSL_Open(vd, 1))
 				exit(1);
 			list_fields(vd);
 			exit(0);
@@ -227,7 +227,7 @@
 		}
 	}
 
-	if (VSL_Open(vd))
+	if (VSL_Open(vd, 1))
 		exit(1);
 
 	if ((VSL_stats = VSL_OpenStats(vd)) == NULL)

Modified: trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c
===================================================================
--- trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -164,7 +164,7 @@
 			 * Only check if it looks like nothing is happening.
 			 */
 			act = VSL_stats->cache_hit + VSL_stats->cache_miss + 1;
-			if (act == lact && VSL_ReOpen(vd))
+			if (act == lact && VSL_ReOpen(vd, 1))
 				break;
 			lact = act;
 

Modified: trunk/varnish-cache/include/varnishapi.h
===================================================================
--- trunk/varnish-cache/include/varnishapi.h	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/include/varnishapi.h	2010-06-08 08:42:15 UTC (rev 4925)
@@ -34,10 +34,87 @@
 
 #include "shmlog.h"
 
-/* base64.c */
-void base64_init(void);
-int base64_decode(char *d, unsigned dlen, const char *s);
+/*
+ * Various notes:
+ *	All malloc failures will result in assert tripping.
+ *	API use failures will trip assert.
+ */
 
+
+/*---------------------------------------------------------------------
+ * Level 0:  Create and destroy the VSL_data handle structure
+ */
+
+struct VSL_data *VSL_New(void);
+	/*
+	 * Allocate and initialize a VSL_data handle structure.
+	 * This is the first thing you will have to do, always.
+	 * You can have multiple active VSL_data handles at the same time
+	 * referencing the same or different shared memory files.
+	 * Returns:
+	 * 	Pointer to usable VSL_data handle.
+	 */
+
+typedef void vsl_diag_f(void *priv, const char *fmt, ...);
+
+void VSL_Diag(struct VSL_data *vd, vsl_diag_f *func, void *priv);
+	/*
+	 * Set the diagnostics reporting function.
+	 * Default is fprintf(stderr, ...)
+	 * If func is NULL, diagnostics are disabled.
+	 */
+
+int VSL_n_Arg(struct VSL_data *vd, const char *n_arg);
+	/*
+	 * Configure which varnishd instance to access.
+	 * Can also be, and normally is done through the VSL_Log_arg()
+	 * and VSL_Stat_Arg() functions.
+	 * Returns:
+	 *	 1 on success
+	 *	 -1 on failure, with diagnostic on stderr.
+	 */
+
+const char *VSL_Name(const struct VSL_data *vd);
+	/*
+	 * Return the instance name.
+	 */
+
+void VSL_Delete(struct VSL_data *vd);
+	/*
+	 * Close and deallocate all storage and mappings.
+	 */
+
+/* XXX: extension:  Patience argument for sleeps */
+
+/*---------------------------------------------------------------------
+ * Level 1:  Open/Close and find allocation in shared memory segment
+ */
+
+int VSL_Open(struct VSL_data *vd, int diag);
+	/*
+	 * Attempt to open and map the shared memory file.
+	 * If diag is non-zero, diagnostics are emitted.
+	 * Returns:
+	 *	0 on success
+	 * 	!= 0 on failure
+	 */
+
+int VSL_ReOpen(struct VSL_data *vd, int diag);
+	/*
+	 * Check if shared memory segment needs to be reopened/remapped
+	 * typically when the varnishd master process restarts.
+	 * diag is passed to VSL_Open()
+	 * Returns:
+	 *	0  No reopen needed.
+	 *	1  shared memory reopened/remapped.
+	 *	-1 failure to reopen.
+	 */
+
+void *VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type,
+    const char *ident, unsigned *lenp);
+void VSL_Close(struct VSL_data *vd);
+
+
 /* shmlog.c */
 typedef int vsl_handler(void *priv, enum shmlogtag tag, unsigned fd,
     unsigned len, unsigned spec, const char *ptr);
@@ -49,7 +126,6 @@
 			" [-r file] [-s skip] [-X regexp] [-x tag]"
 vsl_handler VSL_H_Print;
 struct VSL_data;
-struct VSL_data *VSL_New(void);
 void VSL_Select(const struct VSL_data *vd, unsigned tag);
 int VSL_OpenLog(struct VSL_data *vd);
 void VSL_NonBlocking(struct VSL_data *vd, int nb);
@@ -57,16 +133,9 @@
 int VSL_NextLog(struct VSL_data *lh, uint32_t **pp);
 int VSL_Log_Arg(struct VSL_data *vd, int arg, const char *opt);
 int VSL_Stat_Arg(struct VSL_data *vd, int arg, const char *opt);
-void VSL_Close(struct VSL_data *vd);
-int VSL_Open(struct VSL_data *vd);
-void VSL_Delete(struct VSL_data *vd);
 struct varnish_stats *VSL_OpenStats(struct VSL_data *vd);
-const char *VSL_Name(const struct VSL_data *vd);
 extern const char *VSL_tags[256];
-void *VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type,
-    const char *ident, unsigned *lenp);
 
-int VSL_ReOpen(struct VSL_data *vd);
 
 struct shmalloc *vsl_iter0(const struct VSL_data *vd);
 void vsl_itern(const struct VSL_data *vd, struct shmalloc **pp);
@@ -88,4 +157,8 @@
 
 int VSL_IterStat(const struct VSL_data *vd, vsl_stat_f *func, void *priv);
 
+/* base64.c */
+void base64_init(void);
+int base64_decode(char *d, unsigned dlen, const char *s);
+
 #endif

Modified: trunk/varnish-cache/lib/libvarnishapi/vsl.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/vsl.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/lib/libvarnishapi/vsl.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -64,12 +64,16 @@
 {
 	struct VSL_data *vd;
 
-	vd = calloc(sizeof *vd, 1);
-	assert(vd != NULL);
-	vd->regflags = 0;
-	vd->magic = VSL_MAGIC;
+	ALLOC_OBJ(vd, VSL_MAGIC);
+	AN(vd);
+
+	vd->diag = (vsl_diag_f*)fprintf;
+	vd->priv = stderr;
+
 	vd->vsl_fd = -1;
 
+	vd->regflags = 0;
+
 	/* XXX: Allocate only if log access */
 	vd->vbm_client = vbit_init(4096);
 	vd->vbm_backend = vbit_init(4096);
@@ -84,16 +88,59 @@
 
 	VTAILQ_INIT(&vd->sf_list);
 
+	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
 	return (vd);
 }
 
 /*--------------------------------------------------------------------*/
 
 void
+VSL_Diag(struct VSL_data *vd, vsl_diag_f *func, void *priv)
+{
+
+	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
+	if (func == NULL)
+		vd->diag = (vsl_diag_f*)getpid;
+	else
+		vd->diag = func;
+	vd->priv = priv;
+}
+
+/*--------------------------------------------------------------------*/
+
+int
+VSL_n_Arg(struct VSL_data *vd, const char *opt)
+{
+
+	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
+	REPLACE(vd->n_opt, opt);
+	AN(vd->n_opt);
+	if (vin_n_arg(vd->n_opt, NULL, NULL, &vd->fname)) {
+		vd->diag(vd->priv, "Invalid instance name: %s\n",
+		    strerror(errno));
+		return (-1);
+	}
+	return (1);
+}
+
+/*--------------------------------------------------------------------*/
+
+const char *
+VSL_Name(const struct VSL_data *vd)
+{
+
+	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
+	return (vd->n_opt);
+}
+
+/*--------------------------------------------------------------------*/
+
+void
 VSL_Delete(struct VSL_data *vd)
 {
 	struct vsl_sf *sf;
 
+	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
 	VSL_Close(vd);
 	vbit_destroy(vd->vbm_client);
 	vbit_destroy(vd->vbm_backend);
@@ -118,7 +165,7 @@
 /*--------------------------------------------------------------------*/
 
 static int
-vsl_open(struct VSL_data *vd, int rep)
+vsl_open(struct VSL_data *vd, int diag)
 {
 	int i;
 	struct shmloghead slh;
@@ -128,30 +175,30 @@
 
 	vd->vsl_fd = open(vd->fname, O_RDONLY);
 	if (vd->vsl_fd < 0) {
-		if (rep)
-			fprintf(stderr, "Cannot open %s: %s\n",
+		if (diag)
+			vd->diag(vd->priv, "Cannot open %s: %s\n",
 			    vd->fname, strerror(errno));
 		return (1);
 	}
 
 	assert(fstat(vd->vsl_fd, &vd->fstat) == 0);
 	if (!S_ISREG(vd->fstat.st_mode)) {
-		if (rep)
-			fprintf(stderr, "%s is not a regular file\n",
+		if (diag)
+			vd->diag(vd->priv, "%s is not a regular file\n",
 			    vd->fname);
 		return (1);
 	}
 
 	i = read(vd->vsl_fd, &slh, sizeof slh);
 	if (i != sizeof slh) {
-		if (rep)
-			fprintf(stderr, "Cannot read %s: %s\n",
+		if (diag)
+			vd->diag(vd->priv, "Cannot read %s: %s\n",
 			    vd->fname, strerror(errno));
 		return (1);
 	}
 	if (slh.magic != SHMLOGHEAD_MAGIC) {
-		if (rep)
-			fprintf(stderr, "Wrong magic number in file %s\n",
+		if (diag)
+			vd->diag(vd->priv, "Wrong magic number in file %s\n",
 			    vd->fname);
 		return (1);
 	}
@@ -159,24 +206,27 @@
 	vd->vsl_lh = (void *)mmap(NULL, slh.shm_size,
 	    PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vd->vsl_fd, 0);
 	if (vd->vsl_lh == MAP_FAILED) {
-		if (rep)
-			fprintf(stderr, "Cannot mmap %s: %s\n",
+		if (diag)
+			vd->diag(vd->priv, "Cannot mmap %s: %s\n",
 			    vd->fname, strerror(errno));
 		return (1);
 	}
 	vd->vsl_end = (uint8_t *)vd->vsl_lh + slh.shm_size;
 
 	while(slh.alloc_seq == 0)
-		usleep(50000);
+		usleep(50000);			/* XXX limit total sleep */
 	vd->alloc_seq = slh.alloc_seq;
 	return (0);
 }
 
+/*--------------------------------------------------------------------*/
+
 int
-VSL_Open(struct VSL_data *vd)
+VSL_Open(struct VSL_data *vd, int diag)
+
 {
 
-	return (vsl_open(vd, 1));
+	return (vsl_open(vd, diag));
 }
 
 /*--------------------------------------------------------------------*/
@@ -196,13 +246,12 @@
 /*--------------------------------------------------------------------*/
 
 int
-VSL_ReOpen(struct VSL_data *vd)
+VSL_ReOpen(struct VSL_data *vd, int diag)
 {
 	struct stat st;
 	int i;
 
-	if (vd->vsl_lh == NULL)
-		return (-1);
+	AN(vd->vsl_lh);
 
 	if (stat(vd->fname, &st))
 		return (0);
@@ -211,11 +260,11 @@
 		return (0);
 
 	VSL_Close(vd);
-	for (i = 0; i < 5; i++) {
+	for (i = 0; i < 5; i++) {		/* XXX param */
 		if (!vsl_open(vd, 0))
 			return (1);
 	}
-	if (vsl_open(vd, 1))
+	if (vsl_open(vd, diag))
 		return (-1);
 	return (1);
 }
@@ -281,8 +330,6 @@
 	struct shmalloc *sha;
 
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
-	if (VSL_Open(vd))
-		return (NULL);
 	sha = vsl_find_alloc(vd, class, type, ident);
 	if (sha == NULL)
 		return (NULL);
@@ -290,12 +337,3 @@
 		*lenp = sha->len - sizeof *sha;
 	return (SHA_PTR(sha));
 }
-
-/*--------------------------------------------------------------------*/
-
-const char *
-VSL_Name(const struct VSL_data *vd)
-{
-
-	return (vd->n_opt);
-}

Modified: trunk/varnish-cache/lib/libvarnishapi/vsl.h
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/vsl.h	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/lib/libvarnishapi/vsl.h	2010-06-08 08:42:15 UTC (rev 4925)
@@ -51,7 +51,13 @@
 	unsigned		magic;
 #define VSL_MAGIC		0x6e3bd69b
 
+	vsl_diag_f		*diag;
+	void			*priv;
+
+	char			*n_opt;
 	char			*fname;
+
+
 	struct stat		fstat;
 
 	int			vsl_fd;
@@ -78,7 +84,6 @@
 	uint32_t		*rbuf;
 
 	unsigned		L_opt;
-	char			*n_opt;
 	int			b_opt;
 	int			c_opt;
 	int			d_opt;

Modified: trunk/varnish-cache/lib/libvarnishapi/vsl_arg.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/vsl_arg.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/lib/libvarnishapi/vsl_arg.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -192,22 +192,6 @@
 
 /*--------------------------------------------------------------------*/
 
-static int
-vsl_n_arg(struct VSL_data *vd, const char *opt)
-{
-
-	REPLACE(vd->n_opt, opt);
-	AN(vd->n_opt);
-	if (vin_n_arg(vd->n_opt, NULL, NULL, &vd->fname)) {
-		fprintf(stderr, "Invalid instance name: %s\n",
-		    strerror(errno));
-		return (-1);
-	}
-	return (1);
-}
-
-/*--------------------------------------------------------------------*/
-
 int
 VSL_Log_Arg(struct VSL_data *vd, int arg, const char *opt)
 {
@@ -222,7 +206,7 @@
 		return (1);
 	case 'i': case 'x': return (vsl_ix_arg(vd, opt, arg));
 	case 'k': return (vsl_k_arg(vd, opt));
-	case 'n': return (vsl_n_arg(vd, opt));
+	case 'n': return (VSL_n_Arg(vd, opt));
 	case 'r': return (vsl_r_arg(vd, opt));
 	case 's': return (vsl_s_arg(vd, opt));
 	case 'I': case 'X': return (vsl_IX_arg(vd, opt, arg));
@@ -331,7 +315,7 @@
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
 	switch (arg) {
 	case 'f': return (vsl_sf_arg(vd, opt));
-	case 'n': return (vsl_n_arg(vd, opt));
+	case 'n': return (VSL_n_Arg(vd, opt));
 	default:
 		return (0);
 	}

Modified: trunk/varnish-cache/lib/libvarnishapi/vsl_log.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/vsl_log.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/lib/libvarnishapi/vsl_log.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -264,8 +264,6 @@
 	struct shmalloc *sha;
 
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
-	if (VSL_Open(vd))
-		return (-1);
 	sha = vsl_find_alloc(vd, VSL_CLASS_LOG, "", "");
 	assert(sha != NULL);
 

Modified: trunk/varnish-cache/lib/libvarnishapi/vsl_stat.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/vsl_stat.c	2010-06-07 12:56:09 UTC (rev 4924)
+++ trunk/varnish-cache/lib/libvarnishapi/vsl_stat.c	2010-06-08 08:42:15 UTC (rev 4925)
@@ -54,8 +54,6 @@
 
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
 
-	if (VSL_Open(vd))
-		return (NULL);
 	sha = vsl_find_alloc(vd, VSL_CLASS_STAT, "", "");
 	assert(sha != NULL);
 	return (SHA_PTR(sha));




More information about the varnish-commit mailing list