Code Coverage: vinyl-cache/bin/vinyld/cache/cache_pool.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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_int.h"
42
#include "cache_pool.h"
43
44
#include "vtim.h"
45
46
static pthread_t                thr_pool_herder;
47
48
static struct lock              wstat_mtx;
49
struct lock                     pool_mtx;
50
static VTAILQ_HEAD(,pool)       pools = VTAILQ_HEAD_INITIALIZER(pools);
51
static pthread_cond_t           cond = PTHREAD_COND_INITIALIZER;
52
53
/*--------------------------------------------------------------------
54
 * Summing of stats into global stats counters
55
 */
56
57
void
58
Pool_Sumstat(const struct worker *wrk)
59 26759
{
60
61 26759
        Lck_Lock(&wstat_mtx);
62 26759
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
63 26759
        Lck_Unlock(&wstat_mtx);
64 26759
        memset(wrk->stats, 0, sizeof *wrk->stats);
65 26759
}
66
67
int
68
Pool_TrySumstat(const struct worker *wrk)
69 0
{
70 0
        if (Lck_Trylock(&wstat_mtx))
71 0
                return (0);
72 0
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
73 0
        Lck_Unlock(&wstat_mtx);
74 0
        memset(wrk->stats, 0, sizeof *wrk->stats);
75 0
        return (1);
76 0
}
77
78
/*--------------------------------------------------------------------
79
 * Facility for scheduling a task on any convenient pool.
80
 */
81
82
int
83
Pool_Task_Any(struct pool_task *task, enum task_prio prio)
84 259
{
85 259
        struct pool *pp;
86
87 259
        Lck_Lock(&pool_mtx);
88 259
        pp = VTAILQ_FIRST(&pools);
89 259
        if (pp != NULL) {
90 194
                VTAILQ_REMOVE(&pools, pp, list);
91 194
                VTAILQ_INSERT_TAIL(&pools, pp, list);
92 194
                AZ(pp->die);
93 194
        }
94 259
        Lck_Unlock(&pool_mtx);
95 259
        if (pp == NULL)
96 65
                return (-1);
97
98
        // We never see a dying pool here, but it might die right after the
99
        // Unlock, in which case Pool_Task races for a worker, but because of
100
        // the destruction delay it is highly unlikely that all threads are gone
101
102 194
        return (Pool_Task(pp, task, prio));
103 259
}
104
105
/*--------------------------------------------------------------------
106
 * Helper function to update stats for purges under lock
107
 */
108
109
void
110
Pool_PurgeStat(unsigned nobj)
111 14
{
112 14
        Lck_Lock(&wstat_mtx);
113 14
        VSC_C_main->n_purges++;
114 14
        VSC_C_main->n_obj_purged += nobj;
115 14
        Lck_Unlock(&wstat_mtx);
116 14
}
117
118
/*--------------------------------------------------------------------
119
 * Special function to summ stats
120
 */
121
122
void v_matchproto_(task_func_t)
123
pool_stat_summ(struct worker *wrk, void *priv)
124 8189
{
125 8189
        struct VSC_main_wrk *src;
126 8189
        struct pool *pp;
127
128 8189
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
129 8189
        pp = wrk->pool;
130 8189
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
131 8189
        AN(priv);
132 8189
        src = priv;
133
134 8189
        Lck_Lock(&wstat_mtx);
135 8189
        VSC_main_Summ_wrk(VSC_C_main, src);
136
137 8189
        Lck_Lock(&pp->mtx);
138 8189
        VSC_main_Summ_pool(VSC_C_main, pp->stats);
139 8189
        Lck_Unlock(&pp->mtx);
140 8189
        memset(pp->stats, 0, sizeof pp->stats);
141
142 8189
        Lck_Unlock(&wstat_mtx);
143 8189
        memset(src, 0, sizeof *src);
144
145 8189
        AZ(pp->b_stat);
146 8189
        pp->b_stat = src;
147 8189
}
148
149
/*--------------------------------------------------------------------
150
 * Add a thread pool
151
 */
152
153
static struct pool *
154
pool_mkpool(unsigned pool_no)
155 2038
{
156 2038
        struct pool *pp;
157 2038
        int i;
158
159 2038
        ALLOC_OBJ(pp, POOL_MAGIC);
160 2038
        if (pp == NULL)
161 0
                return (NULL);
162 2038
        pp->a_stat = calloc(1, sizeof *pp->a_stat);
163 2038
        AN(pp->a_stat);
164 2038
        pp->b_stat = calloc(1, sizeof *pp->b_stat);
165 2038
        AN(pp->b_stat);
166 2038
        Lck_New(&pp->mtx, lck_perpool);
167
168 2038
        VTAILQ_INIT(&pp->idle_queue);
169 2038
        VTAILQ_INIT(&pp->poolsocks);
170 12228
        for (i = 0; i < TASK_QUEUE_RESERVE; i++)
171 10190
                VTAILQ_INIT(&pp->queues[i]);
172 2038
        PTOK(pthread_cond_init(&pp->herder_cond, NULL));
173 2038
        PTOK(pthread_create(&pp->herder_thr, NULL, pool_herder, pp));
174
175 4076
        while (VTAILQ_EMPTY(&pp->idle_queue))
176 2038
                VTIM_sleep(0.01);
177
178 2038
        SES_NewPool(pp, pool_no);
179 2038
        VCA_NewPool(pp);
180
181 2038
        return (pp);
182 2038
}
183
184
static void
185
pool_destroy(struct pool *ppx)
186 2012
{
187 2012
        void *rvp;
188
189 2012
        CHECK_OBJ_NOTNULL(ppx, POOL_MAGIC);
190 2012
        PTOK(pthread_join(ppx->herder_thr, &rvp));
191 2012
        PTOK(pthread_cond_destroy(&ppx->herder_cond));
192 2012
        assert(VTAILQ_EMPTY(&ppx->poolsocks));
193 2012
        free(ppx->a_stat);
194 2012
        free(ppx->b_stat);
195 2012
        SES_DestroyPool(ppx);
196 2012
        Lck_Delete(&ppx->mtx);
197 2012
        FREE_OBJ(ppx);
198 2012
        VSC_C_main->pools--;
199 2012
}
200
201
/*--------------------------------------------------------------------
202
 * This thread adjusts the number of pools to match the parameter.
203
 *
204
 * NB: This is quite silly.  The master should tell the child through
205
 * NB: CLI when parameters change and an appropriate call-out table
206
 * NB: be maintained for params which require action.
207
 */
208
209
static void * v_matchproto_()
210
pool_poolherder(void *priv)
211 1026
{
212 1026
        VTAILQ_HEAD(,pool) deadpools = VTAILQ_HEAD_INITIALIZER(deadpools);
213 1026
        unsigned nwq, poolno;
214 1026
        struct pool *pp, *next;
215 1026
        uint64_t u;
216
217 1026
        THR_SetName("pool_poolherder");
218 1026
        THR_Init();
219 1026
        (void)priv;
220
221 1026
        nwq = poolno = 0;
222 8989
        while (cache_param->wthread_pools > 0 ||
223 8989
            VTAILQ_FIRST(&pools) ||
224 8989
            VTAILQ_FIRST(&deadpools)) {
225 7963
                if (nwq < cache_param->wthread_pools) {
226
                        // die before we would wrap
227 2038
                        assert(poolno < UINT_MAX);
228 2038
                        pp = pool_mkpool(poolno);
229 2038
                        if (pp != NULL) {
230 2038
                                Lck_Lock(&pool_mtx);
231 2038
                                VTAILQ_INSERT_TAIL(&pools, pp, list);
232 2038
                                Lck_Unlock(&pool_mtx);
233 2038
                                VSC_C_main->pools++;
234 2038
                                poolno++;
235 2038
                                nwq++;
236 2038
                                continue;
237 2038
                        }
238 5925
                } else if (nwq > cache_param->wthread_pools) {
239 2012
                        Lck_Lock(&pool_mtx);
240 2012
                        pp = VTAILQ_FIRST(&pools);
241 2012
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
242 2012
                        VTAILQ_REMOVE(&pools, pp, list);
243 2012
                        AZ(pp->die);
244 2012
                        Lck_Unlock(&pool_mtx);
245
246 2012
                        VTAILQ_INSERT_TAIL(&deadpools, pp, list);
247 2012
                        AN(nwq);
248 2012
                        nwq--;
249 2012
                        VSL(SLT_Debug, NO_VXID, "Kill Pool %p", pp);
250 2012
                        pp->die = 1;
251 2012
                        VCA_DestroyPool(pp);
252 2012
                        PTOK(pthread_cond_signal(&pp->herder_cond));
253 2012
                        continue;
254 2012
                }
255
256 4033
                VTAILQ_FOREACH_SAFE(pp, &deadpools, list, next) {
257 4033
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
258 4033
                        int active;
259
260 4033
                        AN(pp->die);
261 4033
                        if (pp->nthr > 0 || pp->wrk_dying > 0)
262 2021
                                continue;
263
264 2012
                        Lck_Lock(&pp->mtx);
265 2012
                        active = (pp->nthr > 0 || pp->wrk_dying > 0) ? 1 : 0;
266 2012
                        Lck_Unlock(&pp->mtx);
267
268 2012
                        if (active)
269 0
                                continue;
270
271 2012
                        VTAILQ_REMOVE(&deadpools, pp, list);
272 2012
                        pool_destroy(pp);
273 2012
                }
274
275 3913
                u = 0;
276 3913
                Lck_Lock(&pool_mtx);
277 3913
                VTAILQ_FOREACH(pp, &pools, list) {
278 3618
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
279
280 3618
                        u += pp->lqueue;
281 3618
                }
282 3913
                VSC_C_main->thread_queue_len = u;
283 3913
                if (nwq > 0 || cache_param->wthread_pools > 0 ||
284 3913
                    VTAILQ_FIRST(&deadpools) != NULL)
285 2900
                        (void)Lck_CondWaitTimeout(&cond, &pool_mtx, 1.0);
286 3913
                Lck_Unlock(&pool_mtx);
287 3913
        }
288 1026
        return (NULL);
289 1026
}
290
291
/*--------------------------------------------------------------------*/
292
void
293
pan_pool(struct vsb *vsb)
294 11
{
295 11
        struct pool *pp;
296
297 11
        VSB_cat(vsb, "pools = {\n");
298 11
        VSB_indent(vsb, 2);
299 22
        VTAILQ_FOREACH(pp, &pools, list) {
300 22
                if (PAN_dump_struct(vsb, pp, POOL_MAGIC, "pool"))
301 0
                        continue;
302 22
                VSB_printf(vsb, "nidle = %u,\n", pp->nidle);
303 22
                VSB_printf(vsb, "nthr = %u,\n", pp->nthr);
304 22
                VSB_printf(vsb, "lqueue = %u\n", pp->lqueue);
305 22
                VSB_indent(vsb, -2);
306 22
                VSB_cat(vsb, "},\n");
307 22
        }
308 11
        VSB_indent(vsb, -2);
309 11
        VSB_cat(vsb, "},\n");
310 11
}
311
312
/*--------------------------------------------------------------------*/
313
314
void
315
Pool_Init(void)
316 1026
{
317
318 1026
        Lck_New(&wstat_mtx, lck_wstat);
319 1026
        Lck_New(&pool_mtx, lck_wq);
320 1026
        PTOK(pthread_create(&thr_pool_herder, NULL, pool_poolherder, NULL));
321 3078
        while (!VSC_C_main->pools)
322 2052
                VTIM_sleep(0.01);
323 1026
}
324
325
void
326
Pool_Stop(void)
327 1013
{
328 1013
        Lck_Lock(&pool_mtx);
329 1013
        cache_param->wthread_pools = 0;
330 1013
        PTOK(pthread_cond_signal(&cond));
331 1013
        Lck_Unlock(&pool_mtx);
332 1013
}
333
334
void
335
Pool_Fini(void)
336 1013
{
337 1013
        AZ(pthread_join(thr_pool_herder, NULL));
338 1013
}