vinyl-cache/bin/vinyld/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_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
52
/*--------------------------------------------------------------------
53
 * Summing of stats into global stats counters
54
 */
55
56
void
57 50976
Pool_Sumstat(const struct worker *wrk)
58
{
59
60 50976
        Lck_Lock(&wstat_mtx);
61 50976
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
62 50976
        Lck_Unlock(&wstat_mtx);
63 50976
        memset(wrk->stats, 0, sizeof *wrk->stats);
64 50976
}
65
66
int
67 0
Pool_TrySumstat(const struct worker *wrk)
68
{
69 0
        if (Lck_Trylock(&wstat_mtx))
70 0
                return (0);
71 0
        VSC_main_Summ_wrk(VSC_C_main, wrk->stats);
72 0
        Lck_Unlock(&wstat_mtx);
73 0
        memset(wrk->stats, 0, sizeof *wrk->stats);
74 0
        return (1);
75 0
}
76
77
/*--------------------------------------------------------------------
78
 * Facility for scheduling a task on any convenient pool.
79
 */
80
81
int
82 807
Pool_Task_Any(struct pool_task *task, enum task_prio prio)
83
{
84
        struct pool *pp;
85
86 807
        Lck_Lock(&pool_mtx);
87 807
        pp = VTAILQ_FIRST(&pools);
88 807
        if (pp != NULL) {
89 807
                VTAILQ_REMOVE(&pools, pp, list);
90 807
                VTAILQ_INSERT_TAIL(&pools, pp, list);
91 807
        }
92 807
        Lck_Unlock(&pool_mtx);
93 807
        if (pp == NULL)
94 0
                return (-1);
95
        // NB: When we remove pools, is there a race here ?
96 807
        return (Pool_Task(pp, task, prio));
97 807
}
98
99
/*--------------------------------------------------------------------
100
 * Helper function to update stats for purges under lock
101
 */
102
103
void
104 56
Pool_PurgeStat(unsigned nobj)
105
{
106 56
        Lck_Lock(&wstat_mtx);
107 56
        VSC_C_main->n_purges++;
108 56
        VSC_C_main->n_obj_purged += nobj;
109 56
        Lck_Unlock(&wstat_mtx);
110 56
}
111
112
/*--------------------------------------------------------------------
113
 * Special function to summ stats
114
 */
115
116
void v_matchproto_(task_func_t)
117 21313
pool_stat_summ(struct worker *wrk, void *priv)
118
{
119
        struct VSC_main_wrk *src;
120
        struct pool *pp;
121
122 21313
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
123 21313
        pp = wrk->pool;
124 21313
        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
125 21313
        AN(priv);
126 21313
        src = priv;
127
128 21313
        Lck_Lock(&wstat_mtx);
129 21313
        VSC_main_Summ_wrk(VSC_C_main, src);
130
131 21313
        Lck_Lock(&pp->mtx);
132 21313
        VSC_main_Summ_pool(VSC_C_main, pp->stats);
133 21313
        Lck_Unlock(&pp->mtx);
134 21313
        memset(pp->stats, 0, sizeof pp->stats);
135
136 21313
        Lck_Unlock(&wstat_mtx);
137 21313
        memset(src, 0, sizeof *src);
138
139 21313
        AZ(pp->b_stat);
140 21313
        pp->b_stat = src;
141 21313
}
142
143
/*--------------------------------------------------------------------
144
 * Add a thread pool
145
 */
146
147
static struct pool *
148 7744
pool_mkpool(unsigned pool_no)
149
{
150
        struct pool *pp;
151
        int i;
152
153 7744
        ALLOC_OBJ(pp, POOL_MAGIC);
154 7744
        if (pp == NULL)
155 0
                return (NULL);
156 7744
        pp->a_stat = calloc(1, sizeof *pp->a_stat);
157 7744
        AN(pp->a_stat);
158 7744
        pp->b_stat = calloc(1, sizeof *pp->b_stat);
159 7744
        AN(pp->b_stat);
160 7744
        Lck_New(&pp->mtx, lck_perpool);
161
162 7744
        VTAILQ_INIT(&pp->idle_queue);
163 7744
        VTAILQ_INIT(&pp->poolsocks);
164 46464
        for (i = 0; i < TASK_QUEUE_RESERVE; i++)
165 38720
                VTAILQ_INIT(&pp->queues[i]);
166 7744
        PTOK(pthread_cond_init(&pp->herder_cond, NULL));
167 7744
        PTOK(pthread_create(&pp->herder_thr, NULL, pool_herder, pp));
168
169 15488
        while (VTAILQ_EMPTY(&pp->idle_queue))
170 7744
                VTIM_sleep(0.01);
171
172 7744
        SES_NewPool(pp, pool_no);
173 7744
        VCA_NewPool(pp);
174
175 7744
        return (pp);
176 7744
}
177
178
/*--------------------------------------------------------------------
179
 * This thread adjusts the number of pools to match the parameter.
180
 *
181
 * NB: This is quite silly.  The master should tell the child through
182
 * NB: CLI when parameters change and an appropriate call-out table
183
 * NB: be maintained for params which require action.
184
 */
185
186
static void * v_matchproto_()
187 0
pool_poolherder(void *priv)
188
{
189
        unsigned nwq;
190
        struct pool *pp, *ppx;
191
        uint64_t u;
192
        void *rvp;
193
194 0
        THR_SetName("pool_poolherder");
195 0
        THR_Init();
196 0
        (void)priv;
197
198 0
        nwq = 0;
199 10598
        while (1) {
200 18342
                if (nwq < cache_param->wthread_pools) {
201 7744
                        pp = pool_mkpool(nwq);
202 7744
                        if (pp != NULL) {
203 7744
                                Lck_Lock(&pool_mtx);
204 7744
                                VTAILQ_INSERT_TAIL(&pools, pp, list);
205 7744
                                Lck_Unlock(&pool_mtx);
206 7744
                                VSC_C_main->pools++;
207 7744
                                nwq++;
208 7744
                                continue;
209
                        }
210 10598
                } else if (nwq > cache_param->wthread_pools) {
211 3084
                        Lck_Lock(&pool_mtx);
212 3084
                        pp = VTAILQ_FIRST(&pools);
213 3084
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
214 3084
                        VTAILQ_REMOVE(&pools, pp, list);
215 3084
                        VTAILQ_INSERT_TAIL(&pools, pp, list);
216 3084
                        if (!pp->die)
217 3084
                                nwq--;
218 3084
                        Lck_Unlock(&pool_mtx);
219 3084
                        if (!pp->die) {
220 3084
                                VSL(SLT_Debug, NO_VXID, "Kill Pool %p", pp);
221 3084
                                pp->die = 1;
222 3084
                                VCA_DestroyPool(pp);
223 3084
                                PTOK(pthread_cond_signal(&pp->herder_cond));
224 3084
                        }
225 3084
                }
226 10598
                (void)sleep(1);
227 10598
                u = 0;
228 10598
                ppx = NULL;
229 10598
                Lck_Lock(&pool_mtx);
230 23580
                VTAILQ_FOREACH(pp, &pools, list) {
231 12982
                        CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
232
233 12982
                        if (pp->die && pp->nthr == 0)
234 8
                                ppx = pp;
235 12982
                        u += pp->lqueue;
236 12982
                }
237 10598
                if (ppx != NULL) {
238 8
                        VTAILQ_REMOVE(&pools, ppx, list);
239 8
                        PTOK(pthread_join(ppx->herder_thr, &rvp));
240 8
                        PTOK(pthread_cond_destroy(&ppx->herder_cond));
241 8
                        free(ppx->a_stat);
242 8
                        free(ppx->b_stat);
243 8
                        SES_DestroyPool(ppx);
244 8
                        Lck_Delete(&ppx->mtx);
245 8
                        FREE_OBJ(ppx);
246 8
                        VSC_C_main->pools--;
247 8
                }
248 10598
                Lck_Unlock(&pool_mtx);
249 10598
                VSC_C_main->thread_queue_len = u;
250
        }
251
        NEEDLESS(return (NULL));
252
}
253
254
/*--------------------------------------------------------------------*/
255
void
256 52
pan_pool(struct vsb *vsb)
257
{
258
        struct pool *pp;
259
260 52
        VSB_cat(vsb, "pools = {\n");
261 52
        VSB_indent(vsb, 2);
262 156
        VTAILQ_FOREACH(pp, &pools, list) {
263 104
                if (PAN_dump_struct(vsb, pp, POOL_MAGIC, "pool"))
264 0
                        continue;
265 104
                VSB_printf(vsb, "nidle = %u,\n", pp->nidle);
266 104
                VSB_printf(vsb, "nthr = %u,\n", pp->nthr);
267 104
                VSB_printf(vsb, "lqueue = %u\n", pp->lqueue);
268 104
                VSB_indent(vsb, -2);
269 104
                VSB_cat(vsb, "},\n");
270 104
        }
271 52
        VSB_indent(vsb, -2);
272 52
        VSB_cat(vsb, "},\n");
273 52
}
274
275
/*--------------------------------------------------------------------*/
276
277
void
278 3916
Pool_Init(void)
279
{
280
281 3916
        Lck_New(&wstat_mtx, lck_wstat);
282 3916
        Lck_New(&pool_mtx, lck_wq);
283 3916
        PTOK(pthread_create(&thr_pool_herder, NULL, pool_poolherder, NULL));
284 11747
        while (!VSC_C_main->pools)
285 7831
                VTIM_sleep(0.01);
286 3916
}