source: bin/varnishd/storage_persistent_mgt.c @ 8aa1d8

Revision 8aa1d8, 5.8 KB checked in by Poul-Henning Kamp <phk@…>, 3 years ago (diff)

Eliminate nested <*.h> includes from include/*

Sort #includes according to rules which are for me to know and you
to guess.

  • Property mode set to 100644
Line 
1/*-
2 * Copyright (c) 2008-2011 Varnish Software AS
3 * All rights reserved.
4 *
5 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * Persistent storage method
29 *
30 * XXX: Before we start the client or maybe after it stops, we should give the
31 * XXX: stevedores a chance to examine their storage for consistency.
32 *
33 * XXX: Do we ever free the LRU-lists ?
34 */
35
36#include "config.h"
37
38#include <sys/mman.h>
39
40#include <math.h>
41#include <stdio.h>
42#include <stdlib.h>
43
44#include "cache.h"
45
46#include "stevedore.h"
47#include "vsha256.h"
48
49#include "persistent.h"
50#include "storage_persistent.h"
51
52#ifndef MAP_NOCORE
53#define MAP_NOCORE 0 /* XXX Linux */
54#endif
55
56#ifndef MAP_NOSYNC
57#define MAP_NOSYNC 0 /* XXX Linux */
58#endif
59
60/*--------------------------------------------------------------------
61 * Calculate cleaner metrics from silo dimensions
62 */
63
64static void
65smp_metrics(struct smp_sc *sc)
66{
67
68        /*
69         * We do not want to loose too big chunks of the silos
70         * content when we are forced to clean a segment.
71         *
72         * For now insist that a segment covers no more than 1% of the silo.
73         *
74         * XXX: This should possibly depend on the size of the silo so
75         * XXX: trivially small silos do not run into trouble along
76         * XXX: the lines of "one object per segment".
77         */
78
79        sc->min_nseg = 10;
80        sc->max_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->min_nseg;
81
82        fprintf(stderr, "min_nseg = %u, max_segl = %ju\n",
83            sc->min_nseg, (uintmax_t)sc->max_segl);
84
85        /*
86         * The number of segments are limited by the size of the segment
87         * table(s) and from that follows the minimum size of a segmement.
88         */
89
90        sc->max_nseg = smp_stuff_len(sc, SMP_SEG1_STUFF) / sc->min_nseg;
91        sc->min_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->max_nseg;
92
93        while (sc->min_segl < sizeof(struct object)) {
94                sc->max_nseg /= 2;
95                sc->min_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->max_nseg;
96        }
97
98        fprintf(stderr, "max_nseg = %u, min_segl = %ju\n",
99            sc->max_nseg, (uintmax_t)sc->min_segl);
100
101        /*
102         * Set our initial aim point at the exponential average of the
103         * two extremes.
104         *
105         * XXX: This is a pretty arbitrary choice, but having no idea
106         * XXX: object count, size distribution or ttl pattern at this
107         * XXX: point, we have to do something.
108         */
109
110        sc->aim_nseg =
111           (unsigned) exp((log(sc->min_nseg) + log(sc->max_nseg))*.5);
112        sc->aim_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->aim_nseg;
113
114        fprintf(stderr, "aim_nseg = %u, aim_segl = %ju\n",
115            sc->aim_nseg, (uintmax_t)sc->aim_segl);
116
117        /*
118         * How much space in the free reserve pool ?
119         */
120        sc->free_reserve = sc->aim_segl * 10;
121
122        fprintf(stderr, "free_reserve = %ju\n", (uintmax_t)sc->free_reserve);
123}
124
125/*--------------------------------------------------------------------
126 * Set up persistent storage silo in the master process.
127 */
128
129void
130smp_mgt_init(struct stevedore *parent, int ac, char * const *av)
131{
132        struct smp_sc           *sc;
133        struct smp_sign         sgn;
134        void *target;
135        int i;
136
137        ASSERT_MGT();
138
139        AZ(av[ac]);
140#define SIZOF(foo)       fprintf(stderr, \
141    "sizeof(%s) = %zu = 0x%zx\n", #foo, sizeof(foo), sizeof(foo));
142        SIZOF(struct smp_ident);
143        SIZOF(struct smp_sign);
144        SIZOF(struct smp_segptr);
145        SIZOF(struct smp_object);
146#undef SIZOF
147
148        /* See comments in persistent.h */
149        assert(sizeof(struct smp_ident) == SMP_IDENT_SIZE);
150
151        /* Allocate softc */
152        ALLOC_OBJ(sc, SMP_SC_MAGIC);
153        XXXAN(sc);
154        sc->parent = parent;
155        sc->fd = -1;
156        VTAILQ_INIT(&sc->segments);
157
158        /* Argument processing */
159        if (ac != 2)
160                ARGV_ERR("(-spersistent) wrong number of arguments\n");
161
162        i = STV_GetFile(av[0], &sc->fd, &sc->filename, "-spersistent");
163        if (i == 2)
164                ARGV_ERR("(-spersistent) need filename (not directory)\n");
165
166        sc->align = sizeof(void*) * 2;
167        sc->granularity = getpagesize();
168        sc->mediasize = STV_FileSize(sc->fd, av[1], &sc->granularity,
169            "-spersistent");
170
171        AZ(ftruncate(sc->fd, sc->mediasize));
172
173        /* Try to determine correct mmap address */
174        i = read(sc->fd, &sgn, sizeof sgn);
175        assert(i == sizeof sgn);
176        if (!strcmp(sgn.ident, "SILO"))
177                target = (void*)(uintptr_t)sgn.mapped;
178        else
179                target = NULL;
180
181        sc->base = mmap(target, sc->mediasize, PROT_READ|PROT_WRITE,
182            MAP_NOCORE | MAP_NOSYNC | MAP_SHARED, sc->fd, 0);
183
184        if (sc->base == MAP_FAILED)
185                ARGV_ERR("(-spersistent) failed to mmap (%s)\n",
186                    strerror(errno));
187
188        smp_def_sign(sc, &sc->idn, 0, "SILO");
189        sc->ident = SIGN_DATA(&sc->idn);
190
191        i = smp_valid_silo(sc);
192        if (i) {
193                printf("Warning SILO (%s) not reloaded (reason=%d)\n",
194                    sc->filename, i);
195                smp_newsilo(sc);
196        }
197        AZ(smp_valid_silo(sc));
198
199        smp_metrics(sc);
200
201        parent->priv = sc;
202
203        /* XXX: only for sendfile I guess... */
204        mgt_child_inherit(sc->fd, "storage_persistent");
205}
Note: See TracBrowser for help on using the repository browser.