source: bin/varnishd/cache/cache_wrk.c @ 67a482

Revision 67a482, 4.9 KB checked in by Poul-Henning Kamp <phk@…>, 2 years ago (diff)

Move beresp and bereq to busyobj, but leave the memory allocation in
worker for now.

  • Property mode set to 100644
Line 
1/*-
2 * Copyright (c) 2006 Verdens Gang AS
3 * Copyright (c) 2006-2011 Varnish Software AS
4 * All rights reserved.
5 *
6 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * Worker thread stuff unrelated to the worker thread pools.
30 */
31
32#include "config.h"
33
34#include <math.h>
35#include <stdlib.h>
36
37#include "cache.h"
38
39#include "hash/hash_slinger.h"
40#include "vsha256.h"
41
42static struct lock              wstat_mtx;
43
44/*--------------------------------------------------------------------*/
45
46static void
47wrk_sumstat(struct worker *w)
48{
49
50        Lck_AssertHeld(&wstat_mtx);
51#define L0(n)
52#define L1(n) (VSC_C_main->n += w->stats.n)
53#define VSC_F(n, t, l, f, d, e) L##l(n);
54#include "tbl/vsc_f_main.h"
55#undef VSC_F
56#undef L0
57#undef L1
58        memset(&w->stats, 0, sizeof w->stats);
59}
60
61void
62WRK_SumStat(struct worker *w)
63{
64
65        Lck_Lock(&wstat_mtx);
66        wrk_sumstat(w);
67        Lck_Unlock(&wstat_mtx);
68}
69
70int
71WRK_TrySumStat(struct worker *w)
72{
73        if (Lck_Trylock(&wstat_mtx))
74                return (0);
75        wrk_sumstat(w);
76        Lck_Unlock(&wstat_mtx);
77        return (1);
78}
79
80/*--------------------------------------------------------------------
81 * Create and starte a back-ground thread which as its own worker and
82 * session data structures;
83 */
84
85struct bgthread {
86        unsigned        magic;
87#define BGTHREAD_MAGIC  0x23b5152b
88        const char      *name;
89        bgthread_t      *func;
90        void            *priv;
91};
92
93static void *
94wrk_bgthread(void *arg)
95{
96        struct bgthread *bt;
97        struct worker ww;
98        struct sess *sp;
99        uint32_t logbuf[1024];  /* XXX:  size ? */
100
101        CAST_OBJ_NOTNULL(bt, arg, BGTHREAD_MAGIC);
102        THR_SetName(bt->name);
103        sp = SES_Alloc();
104        XXXAN(sp);
105        memset(&ww, 0, sizeof ww);
106        sp->wrk = &ww;
107        ww.magic = WORKER_MAGIC;
108        ww.wlp = ww.wlb = logbuf;
109        ww.wle = logbuf + (sizeof logbuf) / 4;
110
111        (void)bt->func(sp, bt->priv);
112
113        WRONG("BgThread terminated");
114
115        NEEDLESS_RETURN(NULL);
116}
117
118void
119WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func, void *priv)
120{
121        struct bgthread *bt;
122
123        ALLOC_OBJ(bt, BGTHREAD_MAGIC);
124        AN(bt);
125
126        bt->name = name;
127        bt->func = func;
128        bt->priv = priv;
129        AZ(pthread_create(thr, NULL, wrk_bgthread, bt));
130}
131
132/*--------------------------------------------------------------------*/
133
134static void *
135wrk_thread_real(void *priv, unsigned shm_workspace, unsigned sess_workspace,
136    uint16_t nhttp, unsigned http_space, unsigned siov)
137{
138        struct worker *w, ww;
139        uint32_t wlog[shm_workspace / 4];
140        /* XXX: can we trust these to be properly aligned ? */
141        unsigned char ws[sess_workspace];
142        unsigned char http0[http_space];
143        unsigned char http1[http_space];
144        unsigned char http2[http_space];
145        struct iovec iov[siov];
146        struct SHA256Context sha256;
147
148        THR_SetName("cache-worker");
149        w = &ww;
150        memset(w, 0, sizeof *w);
151        w->magic = WORKER_MAGIC;
152        w->lastused = NAN;
153        w->wlb = w->wlp = wlog;
154        w->wle = wlog + (sizeof wlog) / 4;
155        w->sha256ctx = &sha256;
156        w->x_bereq = HTTP_create(http0, nhttp);
157        w->x_beresp = HTTP_create(http1, nhttp);
158        w->resp = HTTP_create(http2, nhttp);
159        w->wrw.iov = iov;
160        w->wrw.siov = siov;
161        w->wrw.ciov = siov;
162        AZ(pthread_cond_init(&w->cond, NULL));
163
164        WS_Init(w->ws, "wrk", ws, sess_workspace);
165
166        VSL(SLT_WorkThread, 0, "%p start", w);
167
168        Pool_Work_Thread(priv, w);
169        AZ(w->pool);
170
171        VSL(SLT_WorkThread, 0, "%p end", w);
172        if (w->vcl != NULL)
173                VCL_Rel(&w->vcl);
174        AZ(pthread_cond_destroy(&w->cond));
175        HSH_Cleanup(w);
176        WRK_SumStat(w);
177        return (NULL);
178}
179
180void *
181WRK_thread(void *priv)
182{
183        uint16_t nhttp;
184        unsigned siov;
185
186        assert(cache_param->http_max_hdr <= 65535);
187        /* We need to snapshot these two for consistency */
188        nhttp = (uint16_t)cache_param->http_max_hdr;
189        siov = nhttp * 2;
190        if (siov > IOV_MAX)
191                siov = IOV_MAX;
192        return (wrk_thread_real(priv,
193            cache_param->shm_workspace,
194            cache_param->wthread_workspace,
195            nhttp, HTTP_estimate(nhttp), siov));
196}
197
198void
199WRK_Init(void)
200{
201        Lck_New(&wstat_mtx, lck_wstat);
202}
Note: See TracBrowser for help on using the repository browser.