From: phk Date: Mon, 10 Dec 2007 08:55:17 +0000 (+0000) Subject: Go over the shmlog header processing. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3def9e587139d30eeb184dabbca5dba8b2e81ec3;p=varnish 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. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2287 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/shmlog.c b/varnish-cache/bin/varnishd/shmlog.c index f97e393f..05ea8844 100644 --- a/varnish-cache/bin/varnishd/shmlog.c +++ b/varnish-cache/bin/varnishd/shmlog.c @@ -80,13 +80,14 @@ static void 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 @@ VSLR(enum shmlogtag tag, int id, txt t) 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 @@ VSL(enum shmlogtag tag, int id, const char *fmt, ...) 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 @@ WSLR(struct worker *w, enum shmlogtag tag, int id, txt t) 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 @@ WSL(struct worker *w, enum shmlogtag tag, int id, const char *fmt, ...) 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++; } diff --git a/varnish-cache/bin/varnishlog/varnishlog.c b/varnish-cache/bin/varnishlog/varnishlog.c index 8175e7f7..995c9b92 100644 --- a/varnish-cache/bin/varnishlog/varnishlog.c +++ b/varnish-cache/bin/varnishlog/varnishlog.c @@ -243,7 +243,7 @@ open_log(const char *w_arg, int a_flag) 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 @@ do_write(struct VSL_data *vd, const char *w_arg, int a_flag) 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); diff --git a/varnish-cache/bin/varnishtop/varnishtop.c b/varnish-cache/bin/varnishtop/varnishtop.c index b6aac8db..84242c03 100644 --- a/varnish-cache/bin/varnishtop/varnishtop.c +++ b/varnish-cache/bin/varnishtop/varnishtop.c @@ -74,14 +74,15 @@ accumulate(const unsigned char *p) { 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 @@ accumulate(const unsigned char *p) 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 @@ accumulate(const unsigned char *p) 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 @@ static void 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 @@ update(void) 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; diff --git a/varnish-cache/include/shmlog.h b/varnish-cache/include/shmlog.h index e2037a7f..36040c77 100644 --- a/varnish-cache/include/shmlog.h +++ b/varnish-cache/include/shmlog.h @@ -69,11 +69,22 @@ struct shmloghead { * 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 diff --git a/varnish-cache/lib/libvarnishapi/shmlog.c b/varnish-cache/lib/libvarnishapi/shmlog.c index 67bd40a1..7c10dd0c 100644 --- a/varnish-cache/lib/libvarnishapi/shmlog.c +++ b/varnish-cache/lib/libvarnishapi/shmlog.c @@ -63,7 +63,7 @@ struct VSL_data { /* 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 @@ VSL_OpenLog(struct VSL_data *vd, const char *varnish_name) 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 @@ static int 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 @@ vsl_nextlog(struct VSL_data *vd, unsigned char **pp) 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 @@ VSL_NextLog(struct VSL_data *vd, unsigned char **pp) { unsigned char *p; regmatch_t rm; - unsigned u; + unsigned u, l; int i; CHECK_OBJ_NOTNULL(vd, VSL_MAGIC); @@ -271,8 +273,9 @@ VSL_NextLog(struct VSL_data *vd, unsigned char **pp) 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 @@ VSL_NextLog(struct VSL_data *vd, unsigned char **pp) 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 @@ VSL_NextLog(struct VSL_data *vd, unsigned char **pp) 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 @@ int 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 @@ VSL_Dispatch(struct VSL_data *vd, vsl_handler *func, void *priv) 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); } }