source: bin/varnishd/storage_persistent_mgt.c @ c11726

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

Update copyright notices to reflect development and copyright assignment
from Redpill Linpro AS to Varnish Software AS

  • 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 <math.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <sys/mman.h>
42
43#include "cache.h"
44#include "stevedore.h"
45#include "vsha256.h"
46
47#include "persistent.h"
48#include "storage_persistent.h"
49
50/*--------------------------------------------------------------------
51 * Calculate cleaner metrics from silo dimensions
52 */
53
54static void
55smp_metrics(struct smp_sc *sc)
56{
57
58        /*
59         * We do not want to loose too big chunks of the silos
60         * content when we are forced to clean a segment.
61         *
62         * For now insist that a segment covers no more than 1% of the silo.
63         *
64         * XXX: This should possibly depend on the size of the silo so
65         * XXX: trivially small silos do not run into trouble along
66         * XXX: the lines of "one object per segment".
67         */
68
69        sc->min_nseg = 10;
70        sc->max_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->min_nseg;
71
72        fprintf(stderr, "min_nseg = %u, max_segl = %ju\n",
73            sc->min_nseg, (uintmax_t)sc->max_segl);
74
75        /*
76         * The number of segments are limited by the size of the segment
77         * table(s) and from that follows the minimum size of a segmement.
78         */
79
80        sc->max_nseg = smp_stuff_len(sc, SMP_SEG1_STUFF) / sc->min_nseg;
81        sc->min_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->max_nseg;
82
83        while (sc->min_segl < sizeof(struct object)) {
84                sc->max_nseg /= 2;
85                sc->min_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->max_nseg;
86        }
87
88        fprintf(stderr, "max_nseg = %u, min_segl = %ju\n",
89            sc->max_nseg, (uintmax_t)sc->min_segl);
90
91        /*
92         * Set our initial aim point at the exponential average of the
93         * two extremes.
94         *
95         * XXX: This is a pretty arbitrary choice, but having no idea
96         * XXX: object count, size distribution or ttl pattern at this
97         * XXX: point, we have to do something.
98         */
99
100        sc->aim_nseg =
101           (unsigned) exp((log(sc->min_nseg) + log(sc->max_nseg))*.5);
102        sc->aim_segl = smp_stuff_len(sc, SMP_SPC_STUFF) / sc->aim_nseg;
103
104        fprintf(stderr, "aim_nseg = %u, aim_segl = %ju\n",
105            sc->aim_nseg, (uintmax_t)sc->aim_segl);
106
107        /*
108         * How much space in the free reserve pool ?
109         */
110        sc->free_reserve = sc->aim_segl * 10;
111
112        fprintf(stderr, "free_reserve = %ju\n", sc->free_reserve);
113}
114
115/*--------------------------------------------------------------------
116 * Set up persistent storage silo in the master process.
117 */
118
119void
120smp_mgt_init(struct stevedore *parent, int ac, char * const *av)
121{
122        struct smp_sc           *sc;
123        int i;
124
125        ASSERT_MGT();
126
127        AZ(av[ac]);
128#define SIZOF(foo)       fprintf(stderr, \
129    "sizeof(%s) = %zu = 0x%zx\n", #foo, sizeof(foo), sizeof(foo));
130        SIZOF(struct smp_ident);
131        SIZOF(struct smp_sign);
132        SIZOF(struct smp_segptr);
133        SIZOF(struct smp_object);
134#undef SIZOF
135
136        /* See comments in persistent.h */
137        assert(sizeof(struct smp_ident) == SMP_IDENT_SIZE);
138
139        /* Allocate softc */
140        ALLOC_OBJ(sc, SMP_SC_MAGIC);
141        XXXAN(sc);
142        sc->parent = parent;
143        sc->fd = -1;
144        VTAILQ_INIT(&sc->segments);
145
146        /* Argument processing */
147        if (ac != 2)
148                ARGV_ERR("(-spersistent) wrong number of arguments\n");
149
150        i = STV_GetFile(av[0], &sc->fd, &sc->filename, "-spersistent");
151        if (i == 2)
152                ARGV_ERR("(-spersistent) need filename (not directory)\n");
153
154        sc->align = sizeof(void*) * 2;
155        sc->granularity = getpagesize();
156        sc->mediasize = STV_FileSize(sc->fd, av[1], &sc->granularity,
157            "-spersistent");
158
159        AZ(ftruncate(sc->fd, sc->mediasize));
160
161        sc->base = mmap(NULL, sc->mediasize, PROT_READ|PROT_WRITE,
162            MAP_NOCORE | MAP_NOSYNC | MAP_SHARED, sc->fd, 0);
163
164        if (sc->base == MAP_FAILED)
165                ARGV_ERR("(-spersistent) failed to mmap (%s)\n",
166                    strerror(errno));
167
168        smp_def_sign(sc, &sc->idn, 0, "SILO");
169        sc->ident = SIGN_DATA(&sc->idn);
170
171        i = smp_valid_silo(sc);
172        if (i)
173                smp_newsilo(sc);
174        AZ(smp_valid_silo(sc));
175
176        smp_metrics(sc);
177
178        parent->priv = sc;
179
180        /* XXX: only for sendfile I guess... */
181        mgt_child_inherit(sc->fd, "storage_persistent");
182}
Note: See TracBrowser for help on using the repository browser.