From: phk Date: Sat, 19 Aug 2006 20:15:09 +0000 (+0000) Subject: We have a number of adjustable parameters, things like "default TTL" which X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a44b20736d83aa6d7a8193039132fb14205a4e9;p=varnish We have a number of adjustable parameters, things like "default TTL" which should be adjustable at runtime. We need to make adjustments in such a way that a restart of the child also uses the new paramters. We can either do this by parsing the CLI in both mgt+child and have both update their private copy, or we can parse it only in one of them and update a shared copy. We opt for the latter method. Add a "struct params" which holds the adjustable parameters and put on in the shmlog segment, between struct shmloghead and the round-robin buffer. Move parameters from heritage to params. We put it there without exposing it in struct shmloghead which is the public view of the shared memory because we do not want to make it a public API or even to tempt people to think that it is one. Now I just need to add the CLI functions to actually twiddle the parameters. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@835 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache_backend.c b/varnish-cache/bin/varnishd/cache_backend.c index 5e7519fe..2edc1e6c 100644 --- a/varnish-cache/bin/varnishd/cache_backend.c +++ b/varnish-cache/bin/varnishd/cache_backend.c @@ -39,7 +39,7 @@ vbe_new_conn(void) struct vbe_conn *vbc; unsigned char *p; - vbc = calloc(sizeof *vbc + heritage.mem_workspace * 2, 1); + vbc = calloc(sizeof *vbc + params->mem_workspace * 2, 1); if (vbc == NULL) return (NULL); VSL_stats->n_vbe_conn++; @@ -48,9 +48,9 @@ vbe_new_conn(void) vbc->http2 = &vbc->http_mem[1]; vbc->fd = -1; p = (void *)(vbc + 1); - http_Setup(vbc->http, p, heritage.mem_workspace); - p += heritage.mem_workspace; - http_Setup(vbc->http2, p, heritage.mem_workspace); + http_Setup(vbc->http, p, params->mem_workspace); + p += params->mem_workspace; + http_Setup(vbc->http2, p, params->mem_workspace); return (vbc); } diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 4eccc6af..32d80cbb 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -192,7 +192,7 @@ wrk_thread(void *priv) } else { /* If we are a dynamic thread, time out and die */ AZ(clock_gettime(CLOCK_REALTIME, &ts)); - ts.tv_sec += heritage.wthread_timeout; + ts.tv_sec += params->wthread_timeout; if (pthread_cond_timedwait(&w->cv, &wrk_mtx, &ts)) { VSL_stats->n_wrk--; TAILQ_REMOVE(&wrk_idle, w, list); @@ -237,7 +237,7 @@ WRK_QueueSession(struct sess *sp) wrk_overflow++; /* Can we create more threads ? */ - if (VSL_stats->n_wrk >= heritage.wthread_max) { + if (VSL_stats->n_wrk >= params->wthread_max) { VSL_stats->n_wrk_max++; AZ(pthread_mutex_unlock(&wrk_mtx)); return; @@ -273,8 +273,8 @@ WRK_Init(void) AZ(pthread_mutex_init(&wrk_mtx, NULL)); - VSL(SLT_Debug, 0, "Starting %u worker threads", heritage.wthread_min); - for (i = 0; i < heritage.wthread_min; i++) { + VSL(SLT_Debug, 0, "Starting %u worker threads", params->wthread_min); + for (i = 0; i < params->wthread_min; i++) { VSL_stats->n_wrk++; AZ(pthread_create(&tp, NULL, wrk_thread, &i)); AZ(pthread_detach(tp)); diff --git a/varnish-cache/bin/varnishd/cache_session.c b/varnish-cache/bin/varnishd/cache_session.c index abe36f13..77240424 100644 --- a/varnish-cache/bin/varnishd/cache_session.c +++ b/varnish-cache/bin/varnishd/cache_session.c @@ -186,7 +186,7 @@ SES_New(struct sockaddr *addr, unsigned len) struct sessmem *sm; sm = calloc( - sizeof *sm + heritage.mem_workspace, + sizeof *sm + params->mem_workspace, 1); if (sm == NULL) return (NULL); @@ -203,7 +203,7 @@ SES_New(struct sockaddr *addr, unsigned len) sm->sess.sockaddrlen = len; } - http_Setup(&sm->http, (void *)(sm + 1), heritage.mem_workspace); + http_Setup(&sm->http, (void *)(sm + 1), params->mem_workspace); sm->sess.acct.first = time(NULL); diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index 0fa64dc3..1829bf29 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -25,6 +25,11 @@ struct heritage { /* Hash method */ struct hash_slinger *hash; +}; + +struct params { + + /* TTL used for lack of anything better */ unsigned default_ttl; /* Worker threads */ @@ -36,6 +41,7 @@ struct heritage { unsigned mem_workspace; }; +extern struct params *params; extern struct heritage heritage; void child_main(void); diff --git a/varnish-cache/bin/varnishd/rfc2616.c b/varnish-cache/bin/varnishd/rfc2616.c index 38001449..2c74b1a9 100644 --- a/varnish-cache/bin/varnishd/rfc2616.c +++ b/varnish-cache/bin/varnishd/rfc2616.c @@ -111,7 +111,7 @@ RFC2616_Ttl(struct sess *sp, struct http *hp, struct object *obj) retirement_age = h_expires - h_date; } if (retirement_age == INT_MAX) - retirement_age = heritage.default_ttl; + retirement_age = params->default_ttl; ttd = obj->entered + retirement_age; } diff --git a/varnish-cache/bin/varnishd/shmlog.c b/varnish-cache/bin/varnishd/shmlog.c index 0c08980d..e62f9216 100644 --- a/varnish-cache/bin/varnishd/shmlog.c +++ b/varnish-cache/bin/varnishd/shmlog.c @@ -135,44 +135,68 @@ VSL_Init(void) /*--------------------------------------------------------------------*/ -void -VSL_MgtInit(const char *fn, unsigned size) +static int +vsl_goodold(int fd) { struct shmloghead slh; - int i = 0; + int i; memset(&slh, 0, sizeof slh); /* XXX: for flexelint */ - heritage.vsl_fd = open(fn, O_RDWR, 0644); - if (heritage.vsl_fd >= 0) - i = read(heritage.vsl_fd, &slh, sizeof slh); - if (heritage.vsl_fd < 0 || i != sizeof slh || - slh.magic != SHMLOGHEAD_MAGIC || - slh.hdrsize != sizeof slh) { - /* XXX more checks */ + i = read(fd, &slh, sizeof slh); + if (i != sizeof slh) + return (0); + if (slh.magic != SHMLOGHEAD_MAGIC) + return (0); + if (slh.hdrsize != sizeof slh) + return (0); + if (slh.start != sizeof slh + sizeof *params) + return (0); + /* XXX more checks */ + heritage.vsl_size = slh.size + slh.start; + return (1); +} +static void +vsl_buildnew(const char *fn, unsigned size) +{ + struct shmloghead slh; + int i; + + (void)unlink(fn); + heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0644); + if (heritage.vsl_fd < 0) { + fprintf(stderr, "Could not open %s: %s\n", + fn, strerror(errno)); + exit (1); + } + + memset(&slh, 0, sizeof slh); + slh.magic = SHMLOGHEAD_MAGIC; + slh.hdrsize = sizeof slh; + slh.size = size; + slh.ptr = 0; + slh.start = sizeof slh + sizeof *params; + i = write(heritage.vsl_fd, &slh, sizeof slh); + assert(i == sizeof slh); + heritage.vsl_size = slh.start + size; + AZ(ftruncate(heritage.vsl_fd, (off_t)heritage.vsl_size)); +} + +void +VSL_MgtInit(const char *fn, unsigned size) +{ + int i; + struct params *pp; + + i = open(fn, O_RDWR, 0644); + if (i >= 0 && vsl_goodold(i)) { + fprintf(stderr, "Using old SHMFILE\n"); + heritage.vsl_fd = i; + } else { fprintf(stderr, "Creating new SHMFILE\n"); - if (heritage.vsl_fd >= 0) - close(heritage.vsl_fd); - (void)unlink(fn); - heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0644); - if (heritage.vsl_fd < 0) { - fprintf(stderr, "Could not open %s: %s\n", - fn, strerror(errno)); - exit (1); - } - - memset(&slh, 0, sizeof slh); - slh.magic = SHMLOGHEAD_MAGIC; - slh.hdrsize = sizeof slh; - slh.size = size; - slh.ptr = 0; - slh.start = sizeof slh; - AZ(lseek(heritage.vsl_fd, 0, SEEK_SET)); - i = write(heritage.vsl_fd, &slh, sizeof slh); - assert(i == sizeof slh); - AZ(ftruncate(heritage.vsl_fd, (off_t)sizeof slh + (off_t)size)); + (void)close(i); + vsl_buildnew(fn, size); } - heritage.vsl_size = slh.size + slh.start; loghead = mmap(NULL, heritage.vsl_size, PROT_READ|PROT_WRITE, @@ -180,4 +204,7 @@ VSL_MgtInit(const char *fn, unsigned size) heritage.vsl_fd, 0); assert(loghead != MAP_FAILED); VSL_stats = &loghead->stats; + pp = (void *)(loghead + 1); + memcpy(pp, params, sizeof *pp); + params = pp; } diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index bd503006..2a1f340d 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -32,6 +32,7 @@ #endif struct heritage heritage; +struct params *params; /*--------------------------------------------------------------------*/ @@ -188,13 +189,13 @@ tackle_warg(const char *argv) usage(); if (ua < 1) usage(); - heritage.wthread_min = ua; - heritage.wthread_max = ua; - heritage.wthread_timeout = 10; + params->wthread_min = ua; + params->wthread_max = ua; + params->wthread_timeout = 10; if (i >= 2) - heritage.wthread_max = ub; + params->wthread_max = ub; if (i >= 3) - heritage.wthread_timeout = uc; + params->wthread_timeout = uc; } /*-------------------------------------------------------------------- @@ -323,17 +324,20 @@ main(int argc, char *argv[]) const char *sflag = "file"; const char *hflag = "classic"; const char *Tflag = NULL; + struct params param; setbuf(stdout, NULL); setbuf(stderr, NULL); + memset(¶m, 0, sizeof param); + params = ¶m; mgt_vcc_init(); - heritage.default_ttl = 120; - heritage.wthread_min = 1; - heritage.wthread_max = UINT_MAX; - heritage.wthread_timeout = 10; - heritage.mem_workspace = 4096; + params->default_ttl = 120; + params->wthread_min = 1; + params->wthread_max = UINT_MAX; + params->wthread_timeout = 10; + params->mem_workspace = 4096; while ((o = getopt(argc, argv, "b:df:h:p:s:t:T:Vw:")) != -1) switch (o) { @@ -356,7 +360,7 @@ main(int argc, char *argv[]) sflag = optarg; break; case 't': - heritage.default_ttl = strtoul(optarg, NULL, 0); + params->default_ttl = strtoul(optarg, NULL, 0); break; case 'T': Tflag = optarg;