From: phk Date: Thu, 30 Mar 2006 08:05:11 +0000 (+0000) Subject: Promote the poll mutex to be a session mutex so that we can use it X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=463ee958efb9a10e93a43ebc37fb3d74f056f036;p=varnish Promote the poll mutex to be a session mutex so that we can use it for the VCL reference as well, this saves locking operations. Call the atual VCL code when we get the request. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@85 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index cb530c72..2e87481b 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -8,6 +8,9 @@ void *vca_main(void *arg); /* cache_httpd.c */ void HttpdAnalyze(struct sess *sp); +/* cache_main.c */ +pthread_mutex_t sessmtx; + /* cache_pool.c */ void CacheInitPool(void); void DealWithSession(struct sess *sp); @@ -20,6 +23,8 @@ void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...); #endif /* cache_vcl.c */ +void RelVCL(struct VCL_conf *vc); +struct VCL_conf *GetVCL(void); int CVCL_Load(const char *fn, const char *name); #ifdef CLI_PRIV_H cli_func_t cli_func_config_list; diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 98e57126..9fcb1c60 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -25,6 +25,8 @@ static struct event ev_keepalive; static pthread_t vca_thread; +pthread_mutex_t sessmtx; + /*--------------------------------------------------------------------*/ static void @@ -100,6 +102,7 @@ child_main(void) setbuf(stderr, NULL); printf("Child starts\n"); + AZ(pthread_mutex_init(&sessmtx, NULL)); VSL_Init(); CacheInitPool(); diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 769435d0..58cc0028 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -12,7 +12,6 @@ static TAILQ_HEAD(, sess) shd = TAILQ_HEAD_INITIALIZER(shd); -static pthread_mutex_t shdmtx; static pthread_cond_t shdcnd; static void * @@ -20,31 +19,38 @@ CacheWorker(void *priv __unused) { struct sess *sp; + AZ(pthread_mutex_lock(&sessmtx)); while (1) { - AZ(pthread_mutex_lock(&shdmtx)); while (1) { sp = TAILQ_FIRST(&shd); if (sp != NULL) break; - AZ(pthread_cond_wait(&shdcnd, &shdmtx)); + AZ(pthread_cond_wait(&shdcnd, &sessmtx)); } TAILQ_REMOVE(&shd, sp, list); - AZ(pthread_mutex_unlock(&shdmtx)); + sp->vcl = GetVCL(); + AZ(pthread_mutex_unlock(&sessmtx)); HttpdAnalyze(sp); - /* - * XXX send session to acceptor for reuse/disposal - */ + /* Call the VCL program */ + sp->vcl->main_func(sp); + + printf("Handling: %d\n", sp->handling); + + AZ(pthread_mutex_lock(&sessmtx)); + RelVCL(sp->vcl); + sp->vcl = NULL; + /* XXX send session to acceptor for reuse/disposal */ } } void DealWithSession(struct sess *sp) { - AZ(pthread_mutex_lock(&shdmtx)); + AZ(pthread_mutex_lock(&sessmtx)); TAILQ_INSERT_TAIL(&shd, sp, list); - AZ(pthread_mutex_unlock(&shdmtx)); + AZ(pthread_mutex_unlock(&sessmtx)); AZ(pthread_cond_signal(&shdcnd)); } @@ -53,7 +59,6 @@ CacheInitPool(void) { pthread_t tp; - AZ(pthread_mutex_init(&shdmtx, NULL)); AZ(pthread_cond_init(&shdcnd, NULL)); AZ(pthread_create(&tp, NULL, CacheWorker, NULL)); diff --git a/varnish-cache/bin/varnishd/cache_vcl.c b/varnish-cache/bin/varnishd/cache_vcl.c index e69c8e79..54ef7b78 100644 --- a/varnish-cache/bin/varnishd/cache_vcl.c +++ b/varnish-cache/bin/varnishd/cache_vcl.c @@ -12,6 +12,7 @@ #include "cli.h" #include "cli_priv.h" #include "vcl_lang.h" +#include "libvarnish.h" #include "cache.h" struct vcls { @@ -21,10 +22,41 @@ struct vcls { struct VCL_conf *conf; }; +/* + * XXX: Presently all modifications to this list happen from the + * CLI event-engine, so no locking is necessary + */ static TAILQ_HEAD(, vcls) vcl_head = TAILQ_HEAD_INITIALIZER(vcl_head); -static struct vcls *active_vcl; + +static struct vcls *active_vcl; /* protected by sessmtx */ + + +/*--------------------------------------------------------------------*/ + +struct VCL_conf * +GetVCL(void) +{ + struct VCL_conf *vc; + + /* XXX: assert sessmtx (procects active_vcl && ->busy) */ + assert(active_vcl != NULL); + vc = active_vcl->conf; + assert(vc != NULL); + vc->busy++; + return (vc); +} + +void +RelVCL(struct VCL_conf *vc) +{ + + /* XXX: assert sessmtx (procects ->busy) */ + vc->busy--; +} + +/*--------------------------------------------------------------------*/ int CVCL_Load(const char *fn, const char *name) @@ -56,8 +88,10 @@ CVCL_Load(const char *fn, const char *name) vcl->name = strdup(name); assert(vcl->name != NULL); TAILQ_INSERT_TAIL(&vcl_head, vcl, list); + AZ(pthread_mutex_lock(&sessmtx)); if (active_vcl == NULL) active_vcl = vcl; + AZ(pthread_mutex_unlock(&sessmtx)); fprintf(stderr, "Loaded \"%s\" as \"%s\"\n", fn , name); return (0); } @@ -68,24 +102,34 @@ cli_func_config_list(struct cli *cli, char **av, void *priv) struct vcls *vcl; TAILQ_FOREACH(vcl, &vcl_head, list) { - cli_out(cli, "%s%s\n", + cli_out(cli, "%s %6u %s\n", vcl == active_vcl ? "* " : " ", + vcl->conf->busy, vcl->name); } } +static struct vcls * +find_vcls(const char *name) +{ + struct vcls *vcl; + + TAILQ_FOREACH(vcl, &vcl_head, list) + if (!strcmp(vcl->name, name)) + return (vcl); + return (NULL); +} void cli_func_config_load(struct cli *cli, char **av, void *priv) { struct vcls *vcl; - TAILQ_FOREACH(vcl, &vcl_head, list) { - if (!strcmp(vcl->name, av[2])) { - cli_out(cli, "Config '%s' already loaded", av[2]); - cli_result(cli, CLIS_PARAM); - return; - } + vcl = find_vcls(av[2]); + if (vcl != NULL) { + cli_out(cli, "Config '%s' already loaded", av[2]); + cli_result(cli, CLIS_PARAM); + return; } vcl = calloc(sizeof *vcl, 1); assert(vcl != NULL); @@ -115,9 +159,8 @@ cli_func_config_load(struct cli *cli, char **av, void *priv) vcl->name = strdup(av[2]); assert(vcl->name != NULL); TAILQ_INSERT_TAIL(&vcl_head, vcl, list); - if (active_vcl == NULL) - active_vcl = vcl; cli_out(cli, "Loaded \"%s\" from \"%s\"\n", vcl->name , av[3]); + return; } void @@ -131,16 +174,15 @@ cli_func_config_use(struct cli *cli, char **av, void *priv) { struct vcls *vcl; - TAILQ_FOREACH(vcl, &vcl_head, list) { - if (!strcmp(vcl->name, av[2])) - break; - } - if (vcl == NULL) { + vcl = find_vcls(av[2]); + if (vcl != NULL) { + AZ(pthread_mutex_lock(&sessmtx)); + active_vcl = vcl; + AZ(pthread_mutex_unlock(&sessmtx)); + } else { cli_out(cli, "No config named '%s' loaded", av[2]); cli_result(cli, CLIS_PARAM); - return; } - active_vcl = vcl; } /*--------------------------------------------------------------------*/ diff --git a/varnish-cache/include/vcl_lang.h b/varnish-cache/include/vcl_lang.h index 3d400688..fcb5e095 100644 --- a/varnish-cache/include/vcl_lang.h +++ b/varnish-cache/include/vcl_lang.h @@ -6,6 +6,7 @@ */ #include +#include struct vcl_ref { unsigned line; @@ -52,17 +53,28 @@ struct sess { TAILQ_ENTRY(sess) list; + struct VCL_conf *vcl; + /* Various internal stuff */ struct event *rd_e; struct sessmem *mem; }; +struct be_conn { + TAILQ_ENTRY(be_conn) list; + int fd; +}; + struct backend { unsigned ip; double responsetime; double timeout; double bandwidth; int down; + + /* Internals */ + TAILQ_HEAD(,be_conn) bec_head; + unsigned nbec; }; #define VCL_FARGS struct sess *sess @@ -90,4 +102,5 @@ struct VCL_conf { struct backend *default_backend; struct vcl_ref *ref; unsigned nref; + unsigned busy; }; diff --git a/varnish-cache/lib/libvcl/vcl_fixed_token.c b/varnish-cache/lib/libvcl/vcl_fixed_token.c index 3f2f4bbd..9fd5f86e 100644 --- a/varnish-cache/lib/libvcl/vcl_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcl_fixed_token.c @@ -387,6 +387,7 @@ vcl_output_lang_h(FILE *f) fputs(" */\n", f); fputs("\n", f); fputs("#include \n", f); + fputs("#include \n", f); fputs("\n", f); fputs("struct vcl_ref {\n", f); fputs(" unsigned line;\n", f); @@ -404,36 +405,45 @@ vcl_output_lang_h(FILE *f) fputs("#define VCA_ADDRBUFSIZE 32\n", f); fputs("\n", f); fputs("struct sess {\n", f); - fputs(" int fd;\n", f); + fputs(" int fd;\n", f); fputs("\n", f); fputs(" /* formatted ascii client address */\n", f); - fputs(" char addr[VCA_ADDRBUFSIZE];\n", f); + fputs(" char addr[VCA_ADDRBUFSIZE];\n", f); fputs("\n", f); fputs(" /* Receive buffer for HTTP header */\n", f); - fputs(" char rcv[VCA_RXBUFSIZE + 1];\n", f); - fputs(" unsigned rcv_len;\n", f); + fputs(" char rcv[VCA_RXBUFSIZE + 1];\n", f); + fputs(" unsigned rcv_len;\n", f); fputs("\n", f); fputs(" /* HTTP request info, points into rcv */\n", f); - fputs(" const char *req_b;\n", f); - fputs(" const char *req_e;\n", f); - fputs(" const char *url_b;\n", f); - fputs(" const char *url_e;\n", f); - fputs(" const char *proto_b;\n", f); - fputs(" const char *proto_e;\n", f); - fputs(" const char *hdr_b;\n", f); - fputs(" const char *hdr_e;\n", f); + fputs(" const char *req_b;\n", f); + fputs(" const char *req_e;\n", f); + fputs(" const char *url_b;\n", f); + fputs(" const char *url_e;\n", f); + fputs(" const char *proto_b;\n", f); + fputs(" const char *proto_e;\n", f); + fputs(" const char *hdr_b;\n", f); + fputs(" const char *hdr_e;\n", f); fputs("\n", f); fputs(" enum {\n", f); fputs(" HND_Unclass,\n", f); fputs(" HND_Handle,\n", f); fputs(" HND_Pass\n", f); - fputs(" } handling;\n", f); + fputs(" } handling;\n", f); fputs("\n", f); - fputs(" char done;\n", f); + fputs(" char done;\n", f); + fputs("\n", f); + fputs(" TAILQ_ENTRY(sess) list;\n", f); + fputs("\n", f); + fputs(" struct VCL_conf *vcl;\n", f); fputs("\n", f); fputs(" /* Various internal stuff */\n", f); - fputs(" struct event *rd_e;\n", f); - fputs(" struct sessmem *mem;\n", f); + fputs(" struct event *rd_e;\n", f); + fputs(" struct sessmem *mem;\n", f); + fputs("};\n", f); + fputs("\n", f); + fputs("struct be_conn {\n", f); + fputs(" TAILQ_ENTRY(be_conn) list;\n", f); + fputs(" int fd;\n", f); fputs("};\n", f); fputs("\n", f); fputs("struct backend {\n", f); @@ -442,6 +452,10 @@ vcl_output_lang_h(FILE *f) fputs(" double timeout;\n", f); fputs(" double bandwidth;\n", f); fputs(" int down;\n", f); + fputs("\n", f); + fputs(" /* Internals */\n", f); + fputs(" TAILQ_HEAD(,be_conn) bec_head;\n", f); + fputs(" unsigned nbec;\n", f); fputs("};\n", f); fputs("\n", f); fputs("#define VCL_FARGS struct sess *sess\n", f); @@ -469,5 +483,6 @@ vcl_output_lang_h(FILE *f) fputs(" struct backend *default_backend;\n", f); fputs(" struct vcl_ref *ref;\n", f); fputs(" unsigned nref;\n", f); + fputs(" unsigned busy;\n", f); fputs("};\n", f); }