From 7cae44509dde94f534f4ce5cfc8628868dba3862 Mon Sep 17 00:00:00 2001 From: phk Date: Sat, 5 Aug 2006 17:04:25 +0000 Subject: [PATCH] Add some undocumented code to look for something that worries me. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@679 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 3 ++ varnish-cache/bin/varnishd/cache_cli.c | 5 +++ varnish-cache/bin/varnishd/cache_pool.c | 57 ++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 665f1461..add40e89 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -30,6 +30,7 @@ struct sbuf; struct sess; struct object; struct objhead; +struct workreq; /*--------------------------------------------------------------------*/ @@ -94,6 +95,7 @@ struct worker { unsigned nbr; pthread_cond_t cv; TAILQ_ENTRY(worker) list; + struct workreq *wrq; int *wfd; unsigned werr; /* valid after WRK_Flush() */ @@ -407,6 +409,7 @@ cli_func_t cli_func_config_list; cli_func_t cli_func_config_load; cli_func_t cli_func_config_discard; cli_func_t cli_func_config_use; +cli_func_t cli_func_dump_pool; #endif /* rfc2616.c */ diff --git a/varnish-cache/bin/varnishd/cache_cli.c b/varnish-cache/bin/varnishd/cache_cli.c index 5498b5e0..967d16ee 100644 --- a/varnish-cache/bin/varnishd/cache_cli.c +++ b/varnish-cache/bin/varnishd/cache_cli.c @@ -44,6 +44,11 @@ struct cli_proto CLI_cmds[] = { { CLI_CONFIG_LIST, cli_func_config_list }, { CLI_CONFIG_DISCARD, cli_func_config_discard }, { CLI_CONFIG_USE, cli_func_config_use }, + + /* Undocumented */ + { "dump.pool", "dump.pool", + "\tDump the worker thread pool state\n", + 0, 0, cli_func_dump_pool }, { NULL } }; diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 7bf689f7..90f0e16e 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -14,6 +14,7 @@ #include "heritage.h" #include "shmlog.h" #include "vcl.h" +#include "cli_priv.h" #include "cache.h" static pthread_mutex_t wrk_mtx; @@ -21,7 +22,8 @@ static pthread_mutex_t wrk_mtx; /* Number of work requests queued in excess of worker threads available */ static unsigned wrk_overflow; -static TAILQ_HEAD(, worker) wrk_head = TAILQ_HEAD_INITIALIZER(wrk_head); +static TAILQ_HEAD(, worker) wrk_idle = TAILQ_HEAD_INITIALIZER(wrk_idle); +static TAILQ_HEAD(, worker) wrk_busy = TAILQ_HEAD_INITIALIZER(wrk_busy); static TAILQ_HEAD(, workreq) wrk_reqhead = TAILQ_HEAD_INITIALIZER(wrk_reqhead); /*-------------------------------------------------------------------- @@ -107,7 +109,9 @@ wrk_do_one(struct worker *w) VSL_stats->n_wrk_queue--; AZ(pthread_mutex_unlock(&wrk_mtx)); CHECK_OBJ_NOTNULL(wrq->sess, SESS_MAGIC); + assert(wrq->sess->wrk == NULL); wrq->sess->wrk = w; + w->wrq = wrq; if (wrq->sess->srcaddr == NULL) { w->acct.sess++; SES_RefSrcAddr(wrq->sess); @@ -121,6 +125,8 @@ wrk_do_one(struct worker *w) CHECK_OBJ(w->nobj, OBJECT_MAGIC); if (w->nobjhead != NULL) CHECK_OBJ(w->nobjhead, OBJHEAD_MAGIC); + wrq->sess->wrk = NULL; + w->wrq = NULL; AZ(pthread_mutex_lock(&wrk_mtx)); VSL_stats->n_wrk_busy--; } @@ -143,6 +149,7 @@ wrk_thread(void *priv) VSL_stats->n_wrk_create++; VSL(SLT_WorkThread, 0, "%u born dynamic", w->nbr); } + TAILQ_INSERT_HEAD(&wrk_busy, w, list); while (1) { CHECK_OBJ_NOTNULL(w, WORKER_MAGIC); @@ -153,7 +160,8 @@ wrk_thread(void *priv) continue; } - TAILQ_INSERT_HEAD(&wrk_head, w, list); + TAILQ_REMOVE(&wrk_busy, w, list); + TAILQ_INSERT_HEAD(&wrk_idle, w, list); /* If we are a reserved thread we don't die */ if (priv != NULL) { @@ -164,7 +172,7 @@ wrk_thread(void *priv) ts.tv_sec += heritage.wthread_timeout; if (pthread_cond_timedwait(&w->cv, &wrk_mtx, &ts)) { VSL_stats->n_wrk--; - TAILQ_REMOVE(&wrk_head, w, list); + TAILQ_REMOVE(&wrk_idle, w, list); AZ(pthread_mutex_unlock(&wrk_mtx)); VSL(SLT_WorkThread, 0, "%u suicide", w->nbr); AZ(pthread_cond_destroy(&w->cv)); @@ -172,7 +180,7 @@ wrk_thread(void *priv) } } - /* we are already removed from wrk_head */ + /* we are already removed from wrk_idle */ wrk_do_one(w); } } @@ -194,10 +202,11 @@ WRK_QueueSession(struct sess *sp) VSL_stats->n_wrk_queue++; /* If there are idle threads, we tickle the first one into action */ - w = TAILQ_FIRST(&wrk_head); + w = TAILQ_FIRST(&wrk_idle); if (w != NULL) { AZ(pthread_cond_signal(&w->cv)); - TAILQ_REMOVE(&wrk_head, w, list); + TAILQ_REMOVE(&wrk_idle, w, list); + TAILQ_INSERT_TAIL(&wrk_busy, w, list); AZ(pthread_mutex_unlock(&wrk_mtx)); return; } @@ -248,3 +257,39 @@ WRK_Init(void) AZ(pthread_detach(tp)); } } + + +/*--------------------------------------------------------------------*/ + +void +cli_func_dump_pool(struct cli *cli, char **av, void *priv) +{ + unsigned u; + struct sess *s; + time_t t; + + (void)av; + (void)priv; + struct worker *w; + AZ(pthread_mutex_lock(&wrk_mtx)); + t = time(NULL); + TAILQ_FOREACH(w, &wrk_busy, list) { + cli_out(cli, "\n"); + cli_out(cli, "W %p nbr %d ", w, w->nbr); + if (w->wrq == NULL) + continue; + s = w->wrq->sess; + if (s == NULL) + continue; + cli_out(cli, "sess %p fd %d xid %u step %d handling %d age %d", + s, s->fd, s->xid, s->step, s->handling, + t - s->t_req.tv_sec); + } + cli_out(cli, "\n"); + + u = 0; + TAILQ_FOREACH(w, &wrk_idle, list) + u++; + cli_out(cli, "%u idle workers\n", u); + AZ(pthread_mutex_unlock(&wrk_mtx)); +} -- 2.39.5