| | 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 |
239094 |
sma_alloc(const struct stevedore *st, size_t size) |
69 |
|
{ |
70 |
|
struct sma_sc *sma_sc; |
71 |
239094 |
struct sma *sma = NULL; |
72 |
|
void *p; |
73 |
|
|
74 |
239094 |
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC); |
75 |
239094 |
Lck_Lock(&sma_sc->sma_mtx); |
76 |
239094 |
sma_sc->stats->c_req++; |
77 |
239094 |
if (sma_sc->sma_alloc + (VCL_BYTES)size > sma_sc->sma_max) { |
78 |
6480 |
sma_sc->stats->c_fail++; |
79 |
6480 |
size = 0; |
80 |
6480 |
} else { |
81 |
232614 |
sma_sc->sma_alloc += size; |
82 |
232614 |
sma_sc->stats->c_bytes += size; |
83 |
232614 |
sma_sc->stats->g_alloc++; |
84 |
232614 |
sma_sc->stats->g_bytes += size; |
85 |
232614 |
if (sma_sc->sma_max != VRT_INTEGER_MAX) |
86 |
107440 |
sma_sc->stats->g_space -= size; |
87 |
|
} |
88 |
239094 |
Lck_Unlock(&sma_sc->sma_mtx); |
89 |
|
|
90 |
239094 |
if (size == 0) |
91 |
6480 |
return (NULL); |
92 |
|
|
93 |
|
/* |
94 |
|
* Do not collapse 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 catastrophe with chunksized |
97 |
|
* allocations growing another full page, just to accommodate the sma. |
98 |
|
*/ |
99 |
|
|
100 |
232614 |
p = malloc(size); |
101 |
232614 |
if (p != NULL) { |
102 |
232614 |
ALLOC_OBJ(sma, SMA_MAGIC); |
103 |
232612 |
if (sma != NULL) |
104 |
232612 |
sma->s.ptr = p; |
105 |
|
else |
106 |
0 |
free(p); |
107 |
232612 |
} |
108 |
232612 |
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 |
232612 |
sma->sc = sma_sc; |
125 |
232612 |
sma->sz = size; |
126 |
232612 |
sma->s.priv = sma; |
127 |
232612 |
sma->s.len = 0; |
128 |
232612 |
sma->s.space = size; |
129 |
232612 |
sma->s.magic = STORAGE_MAGIC; |
130 |
232612 |
return (&sma->s); |
131 |
239092 |
} |
132 |
|
|
133 |
|
static void v_matchproto_(sml_free_f) |
134 |
161431 |
sma_free(struct storage *s) |
135 |
|
{ |
136 |
|
struct sma_sc *sma_sc; |
137 |
|
struct sma *sma; |
138 |
|
|
139 |
161431 |
CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC); |
140 |
161431 |
CAST_OBJ_NOTNULL(sma, s->priv, SMA_MAGIC); |
141 |
161431 |
sma_sc = sma->sc; |
142 |
161431 |
assert(sma->sz == sma->s.space); |
143 |
161431 |
Lck_Lock(&sma_sc->sma_mtx); |
144 |
161431 |
sma_sc->sma_alloc -= sma->sz; |
145 |
161431 |
sma_sc->stats->g_alloc--; |
146 |
161431 |
sma_sc->stats->g_bytes -= sma->sz; |
147 |
161431 |
sma_sc->stats->c_freed += sma->sz; |
148 |
161431 |
if (sma_sc->sma_max != VRT_INTEGER_MAX) |
149 |
39160 |
sma_sc->stats->g_space += sma->sz; |
150 |
161431 |
Lck_Unlock(&sma_sc->sma_mtx); |
151 |
161431 |
free(sma->s.ptr); |
152 |
161431 |
FREE_OBJ(sma); |
153 |
161431 |
} |
154 |
|
|
155 |
|
static VCL_BYTES v_matchproto_(stv_var_used_space) |
156 |
160 |
sma_used_space(const struct stevedore *st) |
157 |
|
{ |
158 |
|
struct sma_sc *sma_sc; |
159 |
|
|
160 |
160 |
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC); |
161 |
160 |
return (sma_sc->sma_alloc); |
162 |
|
} |
163 |
|
|
164 |
|
static VCL_BYTES v_matchproto_(stv_var_free_space) |
165 |
200 |
sma_free_space(const struct stevedore *st) |
166 |
|
{ |
167 |
|
struct sma_sc *sma_sc; |
168 |
|
|
169 |
200 |
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC); |
170 |
200 |
return (sma_sc->sma_max - sma_sc->sma_alloc); |
171 |
|
} |
172 |
|
|
173 |
|
static void v_matchproto_(storage_init_f) |
174 |
72032 |
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 |
72032 |
ALLOC_OBJ(sc, SMA_SC_MAGIC); |
181 |
72032 |
AN(sc); |
182 |
72032 |
sc->sma_max = VRT_INTEGER_MAX; |
183 |
72032 |
assert(sc->sma_max == VRT_INTEGER_MAX); |
184 |
72032 |
parent->priv = sc; |
185 |
|
|
186 |
72032 |
AZ(av[ac]); |
187 |
72032 |
if (ac > 1) |
188 |
0 |
ARGV_ERR("(-s%s) too many arguments\n", parent->name); |
189 |
|
|
190 |
72032 |
if (ac == 0 || *av[0] == '\0') |
191 |
36672 |
return; |
192 |
|
|
193 |
35360 |
e = VNUM_2bytes(av[0], &u, 0); |
194 |
35360 |
if (e != NULL) |
195 |
0 |
ARGV_ERR("(-s%s) size \"%s\": %s\n", parent->name, av[0], e); |
196 |
35360 |
if ((u != (uintmax_t)(size_t)u)) |
197 |
0 |
ARGV_ERR("(-s%s) size \"%s\": too big\n", parent->name, av[0]); |
198 |
35360 |
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 |
35360 |
sc->sma_max = u; |
204 |
72032 |
} |
205 |
|
|
206 |
|
static void v_matchproto_(storage_open_f) |
207 |
71942 |
sma_open(struct stevedore *st) |
208 |
|
{ |
209 |
|
struct sma_sc *sma_sc; |
210 |
|
|
211 |
71942 |
ASSERT_CLI(); |
212 |
71942 |
st->lru = LRU_Alloc(); |
213 |
71942 |
if (lck_sma == NULL) |
214 |
36591 |
lck_sma = Lck_CreateClass(NULL, "sma"); |
215 |
71942 |
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC); |
216 |
71942 |
Lck_New(&sma_sc->sma_mtx, lck_sma); |
217 |
71942 |
sma_sc->stats = VSC_sma_New(NULL, NULL, st->ident); |
218 |
71942 |
if (sma_sc->sma_max != VRT_INTEGER_MAX) |
219 |
35360 |
sma_sc->stats->g_space = sma_sc->sma_max; |
220 |
71942 |
} |
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 |
|
}; |