r2287 - in trunk/varnish-cache: bin/varnishd bin/varnishlog bin/varnishtop include lib/libvarnishapi

phk at projects.linpro.no phk at projects.linpro.no
Mon Dec 10 09:55:18 CET 2007


Author: phk
Date: 2007-12-10 09:55:17 +0100 (Mon, 10 Dec 2007)
New Revision: 2287

Modified:
   trunk/varnish-cache/bin/varnishd/shmlog.c
   trunk/varnish-cache/bin/varnishlog/varnishlog.c
   trunk/varnish-cache/bin/varnishtop/varnishtop.c
   trunk/varnish-cache/include/shmlog.h
   trunk/varnish-cache/lib/libvarnishapi/shmlog.c
Log:
Go over the shmlog header processing.

Add a byte to the length field, but retain the 255 bytes restriction
for now.

Add symbolic names for the header fields and macros for multi-byte
fields, and uses these throughout.   This will make it easier next time.



Modified: trunk/varnish-cache/bin/varnishd/shmlog.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/shmlog.c	2007-12-07 12:03:58 UTC (rev 2286)
+++ trunk/varnish-cache/bin/varnishd/shmlog.c	2007-12-10 08:55:17 UTC (rev 2287)
@@ -80,13 +80,14 @@
 vsl_hdr(enum shmlogtag tag, unsigned char *p, unsigned len, int id)
 {
 
-	p[1] = len & 0xff;
-	p[2] = (id >> 8) & 0xff;
-	p[3] = id & 0xff;
-	p[4 + len] = '\0';
-	p[5 + len] = SLT_ENDMARKER;	
+	p[__SHMLOG_LEN_HIGH] = (len >> 8) & 0xff;
+	p[__SHMLOG_LEN_LOW] = len & 0xff;
+	p[__SHMLOG_ID_HIGH] = (id >> 8) & 0xff;
+	p[__SHMLOG_ID_LOW] = id & 0xff;
+	p[SHMLOG_DATA + len] = '\0';
+	p[SHMLOG_NEXTTAG + len] = SLT_ENDMARKER;	
 	/* XXX: Write barrier here */
-	p[0] = tag;
+	p[SHMLOG_TAG] = tag;
 }
 
 /*--------------------------------------------------------------------
@@ -116,14 +117,14 @@
 	assert(loghead->ptr < loghead->size);
 
 	/* Wrap if necessary */
-	if (loghead->ptr + 5 + l + 1 >= loghead->size)
+	if (loghead->ptr + SHMLOG_NEXTTAG + l + 1 >= loghead->size)
 		vsl_wrap();
 	p = logstart + loghead->ptr;
-	loghead->ptr += 5 + l;
+	loghead->ptr += SHMLOG_NEXTTAG + l;
 	assert(loghead->ptr < loghead->size);
 	UNLOCKSHM(&vsl_mtx);
 
-	memcpy(p + 4, t.b, l);
+	memcpy(p + SHMLOG_DATA, t.b, l);
 	vsl_hdr(tag, p, l, id);
 }
 
@@ -151,22 +152,21 @@
 		assert(loghead->ptr < loghead->size);
 
 		/* Wrap if we cannot fit a full size record */
-		if (loghead->ptr + 5 + 255 + 1 >= loghead->size)
+		if (loghead->ptr + SHMLOG_NEXTTAG + 255 + 1 >= loghead->size)
 			vsl_wrap();
 
 		p = logstart + loghead->ptr;
-		n = vsnprintf((char *)(p + 4), 256, fmt, ap);
+		n = vsnprintf((char *)(p + SHMLOG_DATA), 256, fmt, ap);
 		if (n > 255)
 			n = 255; 	/* we truncate long fields */
 		vsl_hdr(tag, p, n, id);
-		loghead->ptr += 5 + n;
+		loghead->ptr += SHMLOG_NEXTTAG + n;
 		assert(loghead->ptr < loghead->size);
 		UNLOCKSHM(&vsl_mtx);
 	}
 	va_end(ap);
 }
 
-
 /*--------------------------------------------------------------------*/
 
 void
@@ -215,12 +215,12 @@
 	assert(w->wlp < w->wle);
 
 	/* Wrap if necessary */
-	if (w->wlp + 5 + l + 1 >= w->wle)
+	if (w->wlp + SHMLOG_NEXTTAG + l + 1 >= w->wle)
 		WSL_Flush(w);
 	p = w->wlp;
-	w->wlp += 5 + l;
+	w->wlp += SHMLOG_NEXTTAG + l;
 	assert(w->wlp < w->wle);
-	memcpy(p + 4, t.b, l);
+	memcpy(p + SHMLOG_DATA, t.b, l);
 	vsl_hdr(tag, p, l, id);
 	w->wlr++;
 }
@@ -246,15 +246,15 @@
 		assert(w->wlp < w->wle);
 
 		/* Wrap if we cannot fit a full size record */
-		if (w->wlp + 5 + 255 + 1 >= w->wle)
+		if (w->wlp + SHMLOG_NEXTTAG + 255 + 1 >= w->wle)
 			WSL_Flush(w);
 
 		p = w->wlp;
-		n = vsnprintf((char *)(p + 4), 256, fmt, ap);
+		n = vsnprintf((char *)(p + SHMLOG_DATA), 256, fmt, ap);
 		if (n > 255)
 			n = 255; 	/* we truncate long fields */
 		vsl_hdr(tag, p, n, id);
-		w->wlp += 5 + n;
+		w->wlp += SHMLOG_NEXTTAG + n;
 		assert(w->wlp < w->wle);
 		w->wlr++;
 	}

Modified: trunk/varnish-cache/bin/varnishlog/varnishlog.c
===================================================================
--- trunk/varnish-cache/bin/varnishlog/varnishlog.c	2007-12-07 12:03:58 UTC (rev 2286)
+++ trunk/varnish-cache/bin/varnishlog/varnishlog.c	2007-12-10 08:55:17 UTC (rev 2287)
@@ -243,7 +243,7 @@
 static void
 do_write(struct VSL_data *vd, const char *w_arg, int a_flag)
 {
-	int fd, i;
+	int fd, i, l;
 	unsigned char *p;
 
 	fd = open_log(w_arg, a_flag);
@@ -253,7 +253,8 @@
 		if (i < 0)
 			break;
 		if (i > 0) {
-			i = write(fd, p, 5 + p[1]);
+			l = SHMLOG_LEN(p);
+			i = write(fd, p, SHMLOG_NEXTTAG + l);
 			if (i < 0) {
 				perror(w_arg);
 				exit(1);

Modified: trunk/varnish-cache/bin/varnishtop/varnishtop.c
===================================================================
--- trunk/varnish-cache/bin/varnishtop/varnishtop.c	2007-12-07 12:03:58 UTC (rev 2286)
+++ trunk/varnish-cache/bin/varnishtop/varnishtop.c	2007-12-10 08:55:17 UTC (rev 2287)
@@ -74,14 +74,15 @@
 {
 	struct top *tp, *tp2;
 	const unsigned char *q;
-	unsigned int u;
+	unsigned int u, l;
 	int i;
 
 	// fprintf(stderr, "%*.*s\n", p[1], p[1], p + 4);
 
 	u = 0;
-	q = p + 4;
-	for (i = 0; i < p[1]; i++, q++) {
+	q = p + SHMLOG_DATA;
+	l = SHMLOG_LEN(p);
+	for (i = 0; i < l; i++, q++) {
 		if (f_flag && (*q == ':' || isspace(*q)))
 			break;
 		u += *q;
@@ -90,11 +91,12 @@
 	VTAILQ_FOREACH(tp, &top_head, list) {
 		if (tp->hash != u)
 			continue;
-		if (tp->rec[0] != p[0])
+		if (tp->rec[SHMLOG_TAG] != p[SHMLOG_TAG])
 			continue;
 		if (tp->clen != q - p)
 			continue;
-		if (memcmp(p + 4, tp->rec + 4, q - (p + 4)))
+		if (memcmp(p + SHMLOG_DATA, tp->rec + SHMLOG_DATA,
+		    q - (p + SHMLOG_DATA)))
 			continue;
 		tp->count += 1.0;
 		break;
@@ -108,7 +110,7 @@
 		tp->clen = q - p;
 		VTAILQ_INSERT_TAIL(&top_head, tp, list);
 	}
-	memcpy(tp->rec, p, 4 + p[1]);
+	memcpy(tp->rec, p, SHMLOG_DATA + l);
 	while (1) {
 		tp2 = VTAILQ_PREV(tp, tophead, list);
 		if (tp2 == NULL || tp2->count >= tp->count)
@@ -129,7 +131,7 @@
 update(void)
 {
 	struct top *tp, *tp2;
-	int l;
+	int l, len;
 	double t = 0;
 	static time_t last;
 	time_t now;
@@ -145,12 +147,12 @@
 	mvprintw(0, 0, "list length %u", ntop);
 	VTAILQ_FOREACH_SAFE(tp, &top_head, list, tp2) {
 		if (++l < LINES) {
-			int len = tp->rec[1];
+			len = SHMLOG_LEN(tp->rec);
 			if (len > COLS - 20)
 				len = COLS - 20;
 			mvprintw(l, 0, "%9.2f %-9.9s %*.*s\n",
-			    tp->count, VSL_tags[tp->rec[0]],
-			    len, len, tp->rec + 4);
+			    tp->count, VSL_tags[tp->rec[SHMLOG_TAG]],
+			    len, len, tp->rec + SHMLOG_DATA);
 			t = tp->count;
 		}
 		tp->count *= .999;

Modified: trunk/varnish-cache/include/shmlog.h
===================================================================
--- trunk/varnish-cache/include/shmlog.h	2007-12-07 12:03:58 UTC (rev 2286)
+++ trunk/varnish-cache/include/shmlog.h	2007-12-10 08:55:17 UTC (rev 2287)
@@ -69,11 +69,22 @@
  * Record format is as follows:
  *
  *	1 byte		field type (enum shmlogtag)
- *	1 byte		length of contents
- *	2 byte		record identifier
+ *	2 bytes		length of contents
+ *	2 bytes		record identifier
  *	n bytes		field contents (isgraph(c) || isspace(c)) allowed.
  */
 
+#define SHMLOG_TAG	0
+#define __SHMLOG_LEN_HIGH	1
+#define __SHMLOG_LEN_LOW	2
+#define __SHMLOG_ID_HIGH	3
+#define __SHMLOG_ID_LOW	4
+#define SHMLOG_DATA	5
+#define SHMLOG_NEXTTAG	6	/* ... + len */
+
+#define SHMLOG_LEN(p)	(((p)[__SHMLOG_LEN_HIGH] << 8) | (p)[__SHMLOG_LEN_LOW])
+#define SHMLOG_ID(p)	(((p)[__SHMLOG_ID_HIGH] << 8) | (p)[__SHMLOG_ID_LOW])
+
 /*
  * The identifiers in shmlogtag are "SLT_" + XML tag.  A script may be run
  * on this file to extract the table rather than handcode it

Modified: trunk/varnish-cache/lib/libvarnishapi/shmlog.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/shmlog.c	2007-12-07 12:03:58 UTC (rev 2286)
+++ trunk/varnish-cache/lib/libvarnishapi/shmlog.c	2007-12-10 08:55:17 UTC (rev 2287)
@@ -63,7 +63,7 @@
 
 	/* for -r option */
 	FILE			*fi;
-	unsigned char		rbuf[5 + 255 + 1];
+	unsigned char		rbuf[SHMLOG_NEXTTAG + 255 + 1];
 
 	int			b_opt;
 	int			c_opt;
@@ -199,7 +199,7 @@
 
 	if (!vd->d_opt && vd->fi == NULL) {
 		for (p = vd->ptr; *p != SLT_ENDMARKER; )
-			p += p[1] + 5;
+			p += SHMLOG_LEN(p) + SHMLOG_NEXTTAG;
 		vd->ptr = p;
 	}
 	return (0);
@@ -222,15 +222,16 @@
 vsl_nextlog(struct VSL_data *vd, unsigned char **pp)
 {
 	unsigned char *p;
-	unsigned w;
+	unsigned w, l;
 	int i;
 
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
 	if (vd->fi != NULL) {
-		i = fread(vd->rbuf, 4, 1, vd->fi);
+		i = fread(vd->rbuf, SHMLOG_DATA, 1, vd->fi);
 		if (i != 1)
 			return (-1);
-		i = fread(vd->rbuf + 4, vd->rbuf[1] + 1, 1, vd->fi);
+		i = fread(vd->rbuf + SHMLOG_DATA,
+		    SHMLOG_LEN(vd->rbuf) + 1, 1, vd->fi);
 		if (i != 1)
 			return (-1);
 		*pp = vd->rbuf;
@@ -250,7 +251,8 @@
 			usleep(SLEEP_USEC);
 			continue;
 		}
-		vd->ptr = p + p[1] + 5;
+		l = SHMLOG_LEN(p);
+		vd->ptr = p + l + SHMLOG_NEXTTAG;
 		*pp = p;
 		return (1);
 	}
@@ -263,7 +265,7 @@
 {
 	unsigned char *p;
 	regmatch_t rm;
-	unsigned u;
+	unsigned u, l;
 	int i;
 
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
@@ -271,8 +273,9 @@
 		i = vsl_nextlog(vd, &p);
 		if (i != 1)
 			return (i);
-		u = (p[2] << 8) | p[3];
-		switch(p[0]) {
+		u = SHMLOG_ID(p);
+		l = SHMLOG_LEN(p);
+		switch(p[SHMLOG_TAG]) {
 		case SLT_SessionOpen:
 		case SLT_ReqStart:
 			vd->map[u] |= M_CLIENT;
@@ -286,11 +289,11 @@
 		default:
 			break;
 		}
-		if (vd->map[p[0]] & M_SELECT) {
+		if (vd->map[p[SHMLOG_TAG]] & M_SELECT) {
 			*pp = p;
 			return (1);
 		}
-		if (vd->map[p[0]] & M_SUPPRESS)
+		if (vd->map[p[SHMLOG_TAG]] & M_SUPPRESS)
 			continue;
 		if (vd->b_opt && !(vd->map[u] & M_BACKEND))
 			continue;
@@ -298,15 +301,17 @@
 			continue;
 		if (vd->regincl != NULL) {
 			rm.rm_so = 0;
-			rm.rm_eo = p[1];
-			i = regexec(vd->regincl, (char *)p + 4, 1, &rm, 0);
+			rm.rm_eo = l;
+			i = regexec(vd->regincl,
+			    (char *)p + SHMLOG_DATA, 1, &rm, 0);
 			if (i == REG_NOMATCH)
 				continue;
 		}
 		if (vd->regexcl != NULL) {
 			rm.rm_so = 0;
-			rm.rm_eo = p[1];
-			i = regexec(vd->regexcl, (char *)p + 4, 1, &rm, 0);
+			rm.rm_eo = l;
+			i = regexec(vd->regexcl,
+			    (char *)p + SHMLOG_DATA, 1, &rm, 0);
 			if (i != REG_NOMATCH)
 				continue;
 		}
@@ -321,7 +326,7 @@
 VSL_Dispatch(struct VSL_data *vd, vsl_handler *func, void *priv)
 {
 	int i;
-	unsigned u;
+	unsigned u, l;
 	unsigned char *p;
 
 	CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
@@ -329,11 +334,12 @@
 		i = VSL_NextLog(vd, &p);
 		if (i <= 0)
 			return (i);
-		u = (p[2] << 8) | p[3];
+		u = SHMLOG_ID(p);
+		l = SHMLOG_LEN(p);
 		if (func(priv,
-		    p[0], u, p[1],
+		    p[SHMLOG_TAG], u, l,
 		    vd->map[u] & (VSL_S_CLIENT|VSL_S_BACKEND),
-		    (char *)p + 4))
+		    (char *)p + SHMLOG_DATA))
 			return (1);
 	}
 }




More information about the varnish-commit mailing list