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 27555
Pool_Sumstat(const struct worker *wrk)
56
{
57
58 27555
        Lck_Lock(&wstat_mtx);
59 27555
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
60 27555
        Lck_Unlock(&wstat_mtx);
61 27555
        memset(wrk->stats, 0, sizeof *wrk->stats);
62 27555
}
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 992
Pool_Task_Any(struct pool_task *task, enum task_prio prio)
81
{
82
        struct pool *pp;
83
84 992
        Lck_Lock(&pool_mtx);
85 992
        pp = VTAILQ_FIRST(&pools);
86 992
        if (pp != NULL) {
87 992
                VTAILQ_REMOVE(&pools, pp, list);
88 992
                VTAILQ_INSERT_TAIL(&pools, pp, list);
89 992
        }
90 992
        Lck_Unlock(&pool_mtx);
91 992
        if (pp == NULL)
92 0
                return (-1);
93
        // NB: When we remove pools, is there a race here ?
94 992
        return (Pool_Task(pp, task, prio));
95 992
}
96
97
/*--------------------------------------------------------------------
98
 * Helper function to update stats for purges under lock
99
 */
100
101
void
102 55
Pool_PurgeStat(unsigned nobj)
103
{
104 55
        Lck_Lock(&wstat_mtx);
105 55
        VSC_C_main->n_purges++;
106 55
        VSC_C_main->n_obj_purged += nobj;
107 55
        Lck_Unlock(&wstat_mtx);
108 55
}
109
110
/*--------------------------------------------------------------------
111
 * Special function to summ stats
112
 */
113
114
void v_matchproto_(task_func_t)
115 24237
pool_stat_summ(struct worker *wrk, void *priv)
116
{
117
        struct VSC_main_wrk *src;
118
        struct pool *pp;
119
120 24237
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
121 24237
        pp = wrk->pool;
122 24237
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
123 24237
        AN(priv);
124 24237
        src = priv;
125
126 24237
        Lck_Lock(&wstat_mtx);
127 24237
        VSC_main_Summ_wrk(VSC_C_main, src);
128
129 24237
        Lck_Lock(&pp->mtx);
130 24237
        VSC_main_Summ_pool(VSC_C_main, pp->stats);
131 24237
        Lck_Unlock(&pp->mtx);
132 24237
        memset(pp->stats, 0, sizeof pp->stats);
133
134 24237
        Lck_Unlock(&wstat_mtx);
135 24237
        memset(src, 0, sizeof *src);
136
137 24237
        AZ(pp->b_stat);
138 24237
        pp->b_stat = src;
139 24237
}
140
141
/*--------------------------------------------------------------------
142
 * Add a thread pool
143
 */
144
145
static struct pool *
146 9126
pool_mkpool(unsigned pool_no)
147
{
148
        struct pool *pp;
149
        int i;
150
151 9126
        ALLOC_OBJ(pp, POOL_MAGIC);
152 9126
        if (pp == NULL)
153 0
                return (NULL);
154 9126
        pp->a_stat = calloc(1, sizeof *pp->a_stat);
155 9126
        AN(pp->a_stat);
156 9126
        pp->b_stat = calloc(1, sizeof *pp->b_stat);
157 9126
        AN(pp->b_stat);
158 9126
        Lck_New(&pp->mtx, lck_perpool);
159
160 9126
        VTAILQ_INIT(&pp->idle_queue);
161 9126
        VTAILQ_INIT(&pp->poolsocks);
162 54756
        for (i = 0; i < TASK_QUEUE_RESERVE; i++)
163 45630
                VTAILQ_INIT(&pp->queues[i]);
164 9126
        PTOK(pthread_cond_init(&pp->herder_cond, NULL));
165 9126
        PTOK(pthread_create(&pp->herder_thr, NULL, pool_herder, pp));
166
167 18252
        while (VTAILQ_EMPTY(&pp->idle_queue))
168 9126
                (void)usleep(10000);
169
170 9126
        SES_NewPool(pp, pool_no);
171 9126
        VCA_NewPool(pp);
172
173 9126
        return (pp);
174 9126
}
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 8755
        while (1) {
198 17881
                if (nwq < cache_param->wthread_pools) {
199 9126
                        pp = pool_mkpool(nwq);
200 9126
                        if (pp != NULL) {
201 9126
                                Lck_Lock(&pool_mtx);
202 9126
                                VTAILQ_INSERT_TAIL(&pools, pp, list);
203 9126
                                Lck_Unlock(&pool_mtx);
204 9126
                                VSC_C_main->pools++;
205 9126
                                nwq++;
206 9126
                                continue;
207
                        }
208 8755
                } else if (nwq > cache_param->wthread_pools &&
209 10
                                EXPERIMENT(EXPERIMENT_DROP_POOLS)) {
210 10
                        Lck_Lock(&pool_mtx);
211 10
                        pp = VTAILQ_FIRST(&pools);
212 10
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
213 10
                        VTAILQ_REMOVE(&pools, pp, list);
214 10
                        VTAILQ_INSERT_TAIL(&pools, pp, list);
215 10
                        if (!pp->die)
216 10
                                nwq--;
217 10
                        Lck_Unlock(&pool_mtx);
218 10
                        if (!pp->die) {
219 10
                                VSL(SLT_Debug, NO_VXID, "XXX Kill Pool %p", pp);
220 10
                                pp->die = 1;
221 10
                                VCA_DestroyPool(pp);
222 10
                                PTOK(pthread_cond_signal(&pp->herder_cond));
223 10
                        }
224 10
                }
225 8755
                (void)sleep(1);
226 8755
                u = 0;
227 8755
                ppx = NULL;
228 8755
                Lck_Lock(&pool_mtx);
229 16666
                VTAILQ_FOREACH(pp, &pools, list) {
230 7911
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
231
232 7911
                        if (pp->die && pp->nthr == 0)
233 10
                                ppx = pp;
234 7911
                        u += pp->lqueue;
235 7911
                }
236 8755
                if (ppx != NULL) {
237 10
                        VTAILQ_REMOVE(&pools, ppx, list);
238 10
                        PTOK(pthread_join(ppx->herder_thr, &rvp));
239 10
                        PTOK(pthread_cond_destroy(&ppx->herder_cond));
240 10
                        free(ppx->a_stat);
241 10
                        free(ppx->b_stat);
242 10
                        SES_DestroyPool(ppx);
243 10
                        Lck_Delete(&ppx->mtx);
244 10
                        FREE_OBJ(ppx);
245 10
                        VSC_C_main->pools--;
246 10
                }
247 8755
                Lck_Unlock(&pool_mtx);
248 8755
                VSC_C_main->thread_queue_len = u;
249
        }
250
        NEEDLESS(return (NULL));
251
}
252
253
/*--------------------------------------------------------------------*/
254
void
255 63
pan_pool(struct vsb *vsb)
256
{
257
        struct pool *pp;
258
259 63
        VSB_cat(vsb, "pools = {\n");
260 63
        VSB_indent(vsb, 2);
261 189
        VTAILQ_FOREACH(pp, &pools, list) {
262 126
                if (PAN_dump_struct(vsb, pp, POOL_MAGIC, "pool"))
263 0
                        continue;
264 126
                VSB_printf(vsb, "nidle = %u,\n", pp->nidle);
265 126
                VSB_printf(vsb, "nthr = %u,\n", pp->nthr);
266 126
                VSB_printf(vsb, "lqueue = %u\n", pp->lqueue);
267 126
                VSB_indent(vsb, -2);
268 126
                VSB_cat(vsb, "},\n");
269 126
        }
270 63
        VSB_indent(vsb, -2);
271 63
        VSB_cat(vsb, "},\n");
272 63
}
273
274
/*--------------------------------------------------------------------*/
275
276
void
277 4613
Pool_Init(void)
278
{
279
280 4613
        Lck_New(&wstat_mtx, lck_wstat);
281 4613
        Lck_New(&pool_mtx, lck_wq);
282 4613
        PTOK(pthread_create(&thr_pool_herder, NULL, pool_poolherder, NULL));
283 13838
        while (!VSC_C_main->pools)
284 9225
                (void)usleep(10000);
285 4613
}