varnish-cache/bin/varnishd/storage/storage_malloc.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
 * Storage method based on malloc(3)
31
 */
32
33
#include "config.h"
34
35
#include "cache/cache_varnishd.h"
36
#include "common/heritage.h"
37
38
#include <stdio.h>
39
#include <stdlib.h>
40
41
#include "storage/storage.h"
42
#include "storage/storage_simple.h"
43
44
#include "vnum.h"
45
46
#include "VSC_sma.h"
47
48
struct sma_sc {
49
        unsigned                magic;
50
#define SMA_SC_MAGIC            0x1ac8a345
51
        struct lock             sma_mtx;
52
        VCL_BYTES               sma_max;
53
        VCL_BYTES               sma_alloc;
54
        struct VSC_sma          *stats;
55
};
56
57
struct sma {
58
        unsigned                magic;
59
#define SMA_MAGIC               0x69ae9bb9
60
        struct storage          s;
61
        size_t                  sz;
62
        struct sma_sc           *sc;
63
};
64
65
static struct VSC_lck *lck_sma;
66
67
static struct storage * v_matchproto_(sml_alloc_f)
68 142961
sma_alloc(const struct stevedore *st, size_t size)
69
{
70
        struct sma_sc *sma_sc;
71 142961
        struct sma *sma = NULL;
72
        void *p;
73
74 142961
        CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
75 142961
        Lck_Lock(&sma_sc->sma_mtx);
76 142961
        sma_sc->stats->c_req++;
77 142961
        if (sma_sc->sma_alloc + (VCL_BYTES)size > sma_sc->sma_max) {
78 4049
                sma_sc->stats->c_fail++;
79 4049
                size = 0;
80 4049
        } else {
81 138912
                sma_sc->sma_alloc += size;
82 138912
                sma_sc->stats->c_bytes += size;
83 138912
                sma_sc->stats->g_alloc++;
84 138912
                sma_sc->stats->g_bytes += size;
85 138912
                if (sma_sc->sma_max != VRT_INTEGER_MAX)
86 66398
                        sma_sc->stats->g_space -= size;
87
        }
88 142961
        Lck_Unlock(&sma_sc->sma_mtx);
89
90 142961
        if (size == 0)
91 4050
                return (NULL);
92
93
        /*
94
         * Do not collaps the sma allocation with sma->s.ptr: it is not
95
         * a good idea.  Not only would it make ->trim impossible,
96
         * performance-wise it would be a catastropy with chunksized
97
         * allocations growing another full page, just to accommodate the sma.
98
         */
99
100 138911
        p = malloc(size);
101 138911
        if (p != NULL) {
102 138911
                ALLOC_OBJ(sma, SMA_MAGIC);
103 138911
                if (sma != NULL)
104 138911
                        sma->s.ptr = p;
105
                else
106 0
                        free(p);
107 138911
        }
108 138911
        if (sma == NULL) {
109 0
                Lck_Lock(&sma_sc->sma_mtx);
110
                /*
111
                 * XXX: Not nice to have counters go backwards, but we do
112
                 * XXX: Not want to pick up the lock twice just for stats.
113
                 */
114 0
                sma_sc->stats->c_fail++;
115 0
                sma_sc->sma_alloc -= size;
116 0
                sma_sc->stats->c_bytes -= size;
117 0
                sma_sc->stats->g_alloc--;
118 0
                sma_sc->stats->g_bytes -= size;
119 0
                if (sma_sc->sma_max != VRT_INTEGER_MAX)
120 0
                        sma_sc->stats->g_space += size;
121 0
                Lck_Unlock(&sma_sc->sma_mtx);
122 0
                return (NULL);
123
        }
124 138911
        sma->sc = sma_sc;
125 138911
        sma->sz = size;
126 138911
        sma->s.priv = sma;
127 138911
        sma->s.len = 0;
128 138911
        sma->s.space = size;
129 138911
        sma->s.magic = STORAGE_MAGIC;
130 138911
        return (&sma->s);
131 142961
}
132
133
static void v_matchproto_(sml_free_f)
134 95017
sma_free(struct storage *s)
135
{
136
        struct sma_sc *sma_sc;
137
        struct sma *sma;
138
139 95017
        CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
140 95017
        CAST_OBJ_NOTNULL(sma, s->priv, SMA_MAGIC);
141 95017
        sma_sc = sma->sc;
142 95017
        assert(sma->sz == sma->s.space);
143 95017
        Lck_Lock(&sma_sc->sma_mtx);
144 95017
        sma_sc->sma_alloc -= sma->sz;
145 95017
        sma_sc->stats->g_alloc--;
146 95017
        sma_sc->stats->g_bytes -= sma->sz;
147 95017
        sma_sc->stats->c_freed += sma->sz;
148 95017
        if (sma_sc->sma_max != VRT_INTEGER_MAX)
149 24298
                sma_sc->stats->g_space += sma->sz;
150 95017
        Lck_Unlock(&sma_sc->sma_mtx);
151 95017
        free(sma->s.ptr);
152 95017
        FREE_OBJ(sma);
153 95017
}
154
155
static VCL_BYTES v_matchproto_(stv_var_used_space)
156 100
sma_used_space(const struct stevedore *st)
157
{
158
        struct sma_sc *sma_sc;
159
160 100
        CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
161 100
        return (sma_sc->sma_alloc);
162
}
163
164
static VCL_BYTES v_matchproto_(stv_var_free_space)
165 125
sma_free_space(const struct stevedore *st)
166
{
167
        struct sma_sc *sma_sc;
168
169 125
        CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
170 125
        return (sma_sc->sma_max - sma_sc->sma_alloc);
171
}
172
173
static void v_matchproto_(storage_init_f)
174 43569
sma_init(struct stevedore *parent, int ac, char * const *av)
175
{
176
        const char *e;
177
        uintmax_t u;
178
        struct sma_sc *sc;
179
180 43569
        ALLOC_OBJ(sc, SMA_SC_MAGIC);
181 43569
        AN(sc);
182 43569
        sc->sma_max = VRT_INTEGER_MAX;
183 43569
        assert(sc->sma_max == VRT_INTEGER_MAX);
184 43569
        parent->priv = sc;
185
186 43569
        AZ(av[ac]);
187 43569
        if (ac > 1)
188 0
                ARGV_ERR("(-s%s) too many arguments\n", parent->name);
189
190 43569
        if (ac == 0 || *av[0] == '\0')
191 22194
                 return;
192
193 21375
        e = VNUM_2bytes(av[0], &u, 0);
194 21375
        if (e != NULL)
195 0
                ARGV_ERR("(-s%s) size \"%s\": %s\n", parent->name, av[0], e);
196 21375
        if ((u != (uintmax_t)(size_t)u))
197 0
                ARGV_ERR("(-s%s) size \"%s\": too big\n", parent->name, av[0]);
198 21375
        if (u < 1024*1024)
199 0
                ARGV_ERR("(-s%s) size \"%s\": too small, "
200
                    "did you forget to specify M or G?\n", parent->name,
201
                    av[0]);
202
203 21375
        sc->sma_max = u;
204 43569
}
205
206
static void v_matchproto_(storage_open_f)
207 43513
sma_open(struct stevedore *st)
208
{
209
        struct sma_sc *sma_sc;
210
211 43513
        ASSERT_CLI();
212 43513
        st->lru = LRU_Alloc();
213 43513
        if (lck_sma == NULL)
214 22144
                lck_sma = Lck_CreateClass(NULL, "sma");
215 43513
        CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC);
216 43513
        Lck_New(&sma_sc->sma_mtx, lck_sma);
217 43513
        sma_sc->stats = VSC_sma_New(NULL, NULL, st->ident);
218 43513
        if (sma_sc->sma_max != VRT_INTEGER_MAX)
219 21375
                sma_sc->stats->g_space = sma_sc->sma_max;
220 43513
}
221
222
const struct stevedore sma_stevedore = {
223
        .magic          =       STEVEDORE_MAGIC,
224
        .name           =       "malloc",
225
        .init           =       sma_init,
226
        .open           =       sma_open,
227
        .sml_alloc      =       sma_alloc,
228
        .sml_free       =       sma_free,
229
        .allocobj       =       SML_allocobj,
230
        .panic          =       SML_panic,
231
        .methods        =       &SML_methods,
232
        .var_free_space =       sma_free_space,
233
        .var_used_space =       sma_used_space,
234
        .allocbuf       =       SML_AllocBuf,
235
        .freebuf        =       SML_FreeBuf,
236
};