varnish-cache/bin/varnishd/cache/cache_pool.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * We maintain a number of worker thread pools, to spread lock contention.
31
 *
32
 * Pools can be added on the fly, as a means to mitigate lock contention,
33
 * but can only be removed again by a restart. (XXX: we could fix that)
34
 *
35
 */
36
37
#include "config.h"
38
39
#include <stdlib.h>
40
41
#include "cache_varnishd.h"
42
#include "cache_pool.h"
43
44
static pthread_t                thr_pool_herder;
45
46
static struct lock              wstat_mtx;
47
struct lock                     pool_mtx;
48
static VTAILQ_HEAD(,pool)       pools = VTAILQ_HEAD_INITIALIZER(pools);
49
50
/*--------------------------------------------------------------------
51
 * Summing of stats into global stats counters
52
 */
53
54
void
55 133388
Pool_Sumstat(const struct worker *wrk)
56
{
57
58 133388
        Lck_Lock(&wstat_mtx);
59 133388
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
60 133388
        Lck_Unlock(&wstat_mtx);
61 133388
        memset(wrk->stats, 0, sizeof *wrk->stats);
62 133388
}
63
64
int
65 0
Pool_TrySumstat(const struct worker *wrk)
66
{
67 0
        if (Lck_Trylock(&wstat_mtx))
68 0
                return (0);
69 0
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
70 0
        Lck_Unlock(&wstat_mtx);
71 0
        memset(wrk->stats, 0, sizeof *wrk->stats);
72 0
        return (1);
73 0
}
74
75
/*--------------------------------------------------------------------
76
 * Facility for scheduling a task on any convenient pool.
77
 */
78
79
int
80 4796
Pool_Task_Any(struct pool_task *task, enum task_prio prio)
81
{
82
        struct pool *pp;
83
84 4796
        Lck_Lock(&pool_mtx);
85 4796
        pp = VTAILQ_FIRST(&pools);
86 4796
        if (pp != NULL) {
87 4796
                VTAILQ_REMOVE(&pools, pp, list);
88 4796
                VTAILQ_INSERT_TAIL(&pools, pp, list);
89 4796
        }
90 4796
        Lck_Unlock(&pool_mtx);
91 4796
        if (pp == NULL)
92 0
                return (-1);
93
        // NB: When we remove pools, is there a race here ?
94 4796
        return (Pool_Task(pp, task, prio));
95 4796
}
96
97
/*--------------------------------------------------------------------
98
 * Helper function to update stats for purges under lock
99
 */
100
101
void
102 275
Pool_PurgeStat(unsigned nobj)
103
{
104 275
        Lck_Lock(&wstat_mtx);
105 275
        VSC_C_main->n_purges++;
106 275
        VSC_C_main->n_obj_purged += nobj;
107 275
        Lck_Unlock(&wstat_mtx);
108 275
}
109
110
/*--------------------------------------------------------------------
111
 * Special function to summ stats
112
 */
113
114
void v_matchproto_(task_func_t)
115 115465
pool_stat_summ(struct worker *wrk, void *priv)
116
{
117
        struct VSC_main_wrk *src;
118
        struct pool *pp;
119
120 115465
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
121 115465
        pp = wrk->pool;
122 115465
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
123 115465
        AN(priv);
124 115465
        src = priv;
125
126 115465
        Lck_Lock(&wstat_mtx);
127 115465
        VSC_main_Summ_wrk(VSC_C_main, src);
128
129 115465
        Lck_Lock(&pp->mtx);
130 115465
        VSC_main_Summ_pool(VSC_C_main, pp->stats);
131 115465
        Lck_Unlock(&pp->mtx);
132 115465
        memset(pp->stats, 0, sizeof pp->stats);
133
134 115465
        Lck_Unlock(&wstat_mtx);
135 115465
        memset(src, 0, sizeof *src);
136
137 115465
        AZ(pp->b_stat);
138 115465
        pp->b_stat = src;
139 115465
}
140
141
/*--------------------------------------------------------------------
142
 * Add a thread pool
143
 */
144
145
static struct pool *
146 43944
pool_mkpool(unsigned pool_no)
147
{
148
        struct pool *pp;
149
        int i;
150
151 43944
        ALLOC_OBJ(pp, POOL_MAGIC);
152 43944
        if (pp == NULL)
153 0
                return (NULL);
154 43944
        pp->a_stat = calloc(1, sizeof *pp->a_stat);
155 43944
        AN(pp->a_stat);
156 43944
        pp->b_stat = calloc(1, sizeof *pp->b_stat);
157 43944
        AN(pp->b_stat);
158 43944
        Lck_New(&pp->mtx, lck_perpool);
159
160 43944
        VTAILQ_INIT(&pp->idle_queue);
161 43944
        VTAILQ_INIT(&pp->poolsocks);
162 263664
        for (i = 0; i < TASK_QUEUE_RESERVE; i++)
163 219720
                VTAILQ_INIT(&pp->queues[i]);
164 43944
        PTOK(pthread_cond_init(&pp->herder_cond, NULL));
165 43944
        PTOK(pthread_create(&pp->herder_thr, NULL, pool_herder, pp));
166
167 87888
        while (VTAILQ_EMPTY(&pp->idle_queue))
168 43944
                (void)usleep(10000);
169
170 43944
        SES_NewPool(pp, pool_no);
171 43944
        VCA_NewPool(pp);
172
173 43944
        return (pp);
174 43944
}
175
176
/*--------------------------------------------------------------------
177
 * This thread adjusts the number of pools to match the parameter.
178
 *
179
 * NB: This is quite silly.  The master should tell the child through
180
 * NB: CLI when parameters change and an appropriate call-out table
181
 * NB: be maintained for params which require action.
182
 */
183
184
static void * v_matchproto_()
185 0
pool_poolherder(void *priv)
186
{
187
        unsigned nwq;
188
        struct pool *pp, *ppx;
189
        uint64_t u;
190
        void *rvp;
191
192 0
        THR_SetName("pool_poolherder");
193 0
        THR_Init();
194 0
        (void)priv;
195
196 0
        nwq = 0;
197 42183
        while (1) {
198 86127
                if (nwq < cache_param->wthread_pools) {
199 43944
                        pp = pool_mkpool(nwq);
200 43944
                        if (pp != NULL) {
201 43944
                                Lck_Lock(&pool_mtx);
202 43944
                                VTAILQ_INSERT_TAIL(&pools, pp, list);
203 43944
                                Lck_Unlock(&pool_mtx);
204 43944
                                VSC_C_main->pools++;
205 43944
                                nwq++;
206 43944
                                continue;
207
                        }
208 42183
                } else if (nwq > cache_param->wthread_pools &&
209 50
                                EXPERIMENT(EXPERIMENT_DROP_POOLS)) {
210 50
                        Lck_Lock(&pool_mtx);
211 50
                        pp = VTAILQ_FIRST(&pools);
212 50
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
213 50
                        VTAILQ_REMOVE(&pools, pp, list);
214 50
                        VTAILQ_INSERT_TAIL(&pools, pp, list);
215 50
                        if (!pp->die)
216 50
                                nwq--;
217 50
                        Lck_Unlock(&pool_mtx);
218 50
                        if (!pp->die) {
219 50
                                VSL(SLT_Debug, NO_VXID, "XXX Kill Pool %p", pp);
220 50
                                pp->die = 1;
221 50
                                VCA_DestroyPool(pp);
222 50
                                PTOK(pthread_cond_signal(&pp->herder_cond));
223 50
                        }
224 50
                }
225 42183
                (void)sleep(1);
226 42183
                u = 0;
227 42183
                ppx = NULL;
228 42183
                Lck_Lock(&pool_mtx);
229 80293
                VTAILQ_FOREACH(pp, &pools, list) {
230 38110
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
231
232 38110
                        if (pp->die && pp->nthr == 0)
233 50
                                ppx = pp;
234 38110
                        u += pp->lqueue;
235 38110
                }
236 42183
                if (ppx != NULL) {
237 50
                        VTAILQ_REMOVE(&pools, ppx, list);
238 50
                        PTOK(pthread_join(ppx->herder_thr, &rvp));
239 50
                        PTOK(pthread_cond_destroy(&ppx->herder_cond));
240 50
                        free(ppx->a_stat);
241 50
                        free(ppx->b_stat);
242 50
                        SES_DestroyPool(ppx);
243 50
                        Lck_Delete(&ppx->mtx);
244 50
                        FREE_OBJ(ppx);
245 50
                        VSC_C_main->pools--;
246 50
                }
247 42183
                Lck_Unlock(&pool_mtx);
248 42183
                VSC_C_main->thread_queue_len = u;
249
        }
250
        NEEDLESS(return (NULL));
251
}
252
253
/*--------------------------------------------------------------------*/
254
void
255 297
pan_pool(struct vsb *vsb)
256
{
257
        struct pool *pp;
258
259 297
        VSB_cat(vsb, "pools = {\n");
260 297
        VSB_indent(vsb, 2);
261 891
        VTAILQ_FOREACH(pp, &pools, list) {
262 594
                if (PAN_dump_struct(vsb, pp, POOL_MAGIC, "pool"))
263 0
                        continue;
264 594
                VSB_printf(vsb, "nidle = %u,\n", pp->nidle);
265 594
                VSB_printf(vsb, "nthr = %u,\n", pp->nthr);
266 594
                VSB_printf(vsb, "lqueue = %u\n", pp->lqueue);
267 594
                VSB_indent(vsb, -2);
268 594
                VSB_cat(vsb, "},\n");
269 594
        }
270 297
        VSB_indent(vsb, -2);
271 297
        VSB_cat(vsb, "},\n");
272 297
}
273
274
/*--------------------------------------------------------------------*/
275
276
void
277 22197
Pool_Init(void)
278
{
279
280 22197
        Lck_New(&wstat_mtx, lck_wstat);
281 22197
        Lck_New(&pool_mtx, lck_wq);
282 22197
        PTOK(pthread_create(&thr_pool_herder, NULL, pool_poolherder, NULL));
283 66588
        while (!VSC_C_main->pools)
284 44391
                (void)usleep(10000);
285 22197
}