| | 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 |
258001 |
sma_alloc(const struct stevedore *st, size_t size) |
| 69 |
|
{ |
| 70 |
|
struct sma_sc *sma_sc; |
| 71 |
258001 |
struct sma *sma = NULL; |
| 72 |
|
void *p; |
| 73 |
|
|
| 74 |
258001 |
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC); |
| 75 |
258001 |
Lck_Lock(&sma_sc->sma_mtx); |
| 76 |
258001 |
sma_sc->stats->c_req++; |
| 77 |
258001 |
if (sma_sc->sma_alloc + (VCL_BYTES)size > sma_sc->sma_max) { |
| 78 |
6448 |
sma_sc->stats->c_fail++; |
| 79 |
6448 |
size = 0; |
| 80 |
6448 |
} else { |
| 81 |
251553 |
sma_sc->sma_alloc += size; |
| 82 |
251553 |
sma_sc->stats->c_bytes += size; |
| 83 |
251553 |
sma_sc->stats->g_alloc++; |
| 84 |
251553 |
sma_sc->stats->g_bytes += size; |
| 85 |
251553 |
if (sma_sc->sma_max != VRT_INTEGER_MAX) |
| 86 |
111444 |
sma_sc->stats->g_space -= size; |
| 87 |
|
} |
| 88 |
258001 |
Lck_Unlock(&sma_sc->sma_mtx); |
| 89 |
|
|
| 90 |
258001 |
if (size == 0) |
| 91 |
6452 |
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 |
251549 |
p = malloc(size); |
| 101 |
251549 |
if (p != NULL) { |
| 102 |
251549 |
ALLOC_OBJ(sma, SMA_MAGIC); |
| 103 |
251549 |
if (sma != NULL) |
| 104 |
251549 |
sma->s.ptr = p; |
| 105 |
|
else |
| 106 |
0 |
free(p); |
| 107 |
251549 |
} |
| 108 |
251549 |
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 |
251549 |
sma->sc = sma_sc; |
| 125 |
251549 |
sma->sz = size; |
| 126 |
251549 |
sma->s.priv = sma; |
| 127 |
251549 |
sma->s.len = 0; |
| 128 |
251549 |
sma->s.space = size; |
| 129 |
251549 |
sma->s.magic = STORAGE_MAGIC; |
| 130 |
251549 |
return (&sma->s); |
| 131 |
258001 |
} |
| 132 |
|
|
| 133 |
|
static void v_matchproto_(sml_free_f) |
| 134 |
177885 |
sma_free(struct storage *s) |
| 135 |
|
{ |
| 136 |
|
struct sma_sc *sma_sc; |
| 137 |
|
struct sma *sma; |
| 138 |
|
|
| 139 |
177885 |
CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC); |
| 140 |
177885 |
CAST_OBJ_NOTNULL(sma, s->priv, SMA_MAGIC); |
| 141 |
177885 |
sma_sc = sma->sc; |
| 142 |
177885 |
assert(sma->sz == sma->s.space); |
| 143 |
177885 |
Lck_Lock(&sma_sc->sma_mtx); |
| 144 |
177885 |
sma_sc->sma_alloc -= sma->sz; |
| 145 |
177885 |
sma_sc->stats->g_alloc--; |
| 146 |
177885 |
sma_sc->stats->g_bytes -= sma->sz; |
| 147 |
177885 |
sma_sc->stats->c_freed += sma->sz; |
| 148 |
177885 |
if (sma_sc->sma_max != VRT_INTEGER_MAX) |
| 149 |
40884 |
sma_sc->stats->g_space += sma->sz; |
| 150 |
177885 |
Lck_Unlock(&sma_sc->sma_mtx); |
| 151 |
177885 |
free(sma->s.ptr); |
| 152 |
177885 |
FREE_OBJ(sma); |
| 153 |
177885 |
} |
| 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 |
74746 |
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 |
74746 |
ALLOC_OBJ(sc, SMA_SC_MAGIC); |
| 181 |
74746 |
AN(sc); |
| 182 |
74746 |
sc->sma_max = VRT_INTEGER_MAX; |
| 183 |
74746 |
assert(sc->sma_max == VRT_INTEGER_MAX); |
| 184 |
74746 |
parent->priv = sc; |
| 185 |
|
|
| 186 |
74746 |
AZ(av[ac]); |
| 187 |
74746 |
if (ac > 1) |
| 188 |
0 |
ARGV_ERR("(-s%s) too many arguments\n", parent->name); |
| 189 |
|
|
| 190 |
74746 |
if (ac == 0 || *av[0] == '\0') |
| 191 |
38106 |
return; |
| 192 |
|
|
| 193 |
36640 |
e = VNUM_2bytes(av[0], &u, 0); |
| 194 |
36640 |
if (e != NULL) |
| 195 |
0 |
ARGV_ERR("(-s%s) size \"%s\": %s\n", parent->name, av[0], e); |
| 196 |
36640 |
if ((u != (uintmax_t)(size_t)u)) |
| 197 |
0 |
ARGV_ERR("(-s%s) size \"%s\": too big\n", parent->name, av[0]); |
| 198 |
36640 |
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 |
36640 |
sc->sma_max = u; |
| 204 |
74746 |
} |
| 205 |
|
|
| 206 |
|
static void v_matchproto_(storage_open_f) |
| 207 |
74618 |
sma_open(struct stevedore *st) |
| 208 |
|
{ |
| 209 |
|
struct sma_sc *sma_sc; |
| 210 |
|
|
| 211 |
74618 |
ASSERT_CLI(); |
| 212 |
74618 |
st->lru = LRU_Alloc(); |
| 213 |
74618 |
if (lck_sma == NULL) |
| 214 |
37949 |
lck_sma = Lck_CreateClass(NULL, "sma"); |
| 215 |
74618 |
CAST_OBJ_NOTNULL(sma_sc, st->priv, SMA_SC_MAGIC); |
| 216 |
74618 |
Lck_New(&sma_sc->sma_mtx, lck_sma); |
| 217 |
74618 |
sma_sc->stats = VSC_sma_New(NULL, NULL, st->ident); |
| 218 |
74618 |
if (sma_sc->sma_max != VRT_INTEGER_MAX) |
| 219 |
36640 |
sma_sc->stats->g_space = sma_sc->sma_max; |
| 220 |
74618 |
} |
| 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 |
|
}; |