| | varnish-cache/bin/varnishd/cache/cache_mempool.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2011 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
* |
| 29 |
|
* Generic memory pool |
| 30 |
|
*/ |
| 31 |
|
|
| 32 |
|
#include "config.h" |
| 33 |
|
|
| 34 |
|
#include "cache_varnishd.h" |
| 35 |
|
|
| 36 |
|
#include <stdio.h> |
| 37 |
|
#include <stdlib.h> |
| 38 |
|
|
| 39 |
|
#include "vtim.h" |
| 40 |
|
|
| 41 |
|
#include "VSC_mempool.h" |
| 42 |
|
|
| 43 |
|
struct memitem { |
| 44 |
|
unsigned magic; |
| 45 |
|
#define MEMITEM_MAGIC 0x42e55401 |
| 46 |
|
unsigned size; |
| 47 |
|
VTAILQ_ENTRY(memitem) list; |
| 48 |
|
vtim_real touched; // XXX -> mono? |
| 49 |
|
}; |
| 50 |
|
|
| 51 |
|
VTAILQ_HEAD(memhead_s, memitem); |
| 52 |
|
|
| 53 |
|
struct mempool { |
| 54 |
|
unsigned magic; |
| 55 |
|
#define MEMPOOL_MAGIC 0x37a75a8d |
| 56 |
|
char name[12]; |
| 57 |
|
struct memhead_s list; |
| 58 |
|
struct memhead_s surplus; |
| 59 |
|
struct lock mtx; |
| 60 |
|
volatile struct poolparam *param; |
| 61 |
|
volatile unsigned *cur_size; |
| 62 |
|
uint64_t live; |
| 63 |
|
struct vsc_seg *vsc_seg; |
| 64 |
|
struct VSC_mempool *vsc; |
| 65 |
|
unsigned n_pool; |
| 66 |
|
pthread_t thread; |
| 67 |
|
vtim_real t_now; // XXX -> mono? |
| 68 |
|
int self_destruct; |
| 69 |
|
}; |
| 70 |
|
|
| 71 |
|
/*--------------------------------------------------------------------- |
| 72 |
|
*/ |
| 73 |
|
|
| 74 |
|
static struct memitem * |
| 75 |
1904242 |
mpl_alloc(const struct mempool *mpl) |
| 76 |
|
{ |
| 77 |
|
unsigned tsz; |
| 78 |
|
struct memitem *mi; |
| 79 |
|
|
| 80 |
1904242 |
CHECK_OBJ_NOTNULL(mpl, MEMPOOL_MAGIC); |
| 81 |
1904242 |
tsz = *mpl->cur_size; |
| 82 |
1904242 |
mi = calloc(1, tsz); |
| 83 |
1904242 |
AN(mi); |
| 84 |
1904242 |
mi->magic = MEMITEM_MAGIC; |
| 85 |
1904242 |
mi->size = tsz; |
| 86 |
1904242 |
mpl->vsc->sz_wanted = tsz; |
| 87 |
1904242 |
mpl->vsc->sz_actual = tsz - sizeof *mi; |
| 88 |
1904242 |
return (mi); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
/*--------------------------------------------------------------------- |
| 92 |
|
* Pool-guard |
| 93 |
|
* Attempt to keep number of free items in pool inside bounds with |
| 94 |
|
* minimum locking activity, and keep an eye on items at the tail |
| 95 |
|
* of the list not getting too old. |
| 96 |
|
*/ |
| 97 |
|
|
| 98 |
|
static void * |
| 99 |
156 |
mpl_guard(void *priv) |
| 100 |
|
{ |
| 101 |
|
struct mempool *mpl; |
| 102 |
156 |
struct memitem *mi = NULL; |
| 103 |
|
vtim_dur v_statevariable_(mpl_slp); |
| 104 |
156 |
vtim_real last = 0; |
| 105 |
|
|
| 106 |
156 |
CAST_OBJ_NOTNULL(mpl, priv, MEMPOOL_MAGIC); |
| 107 |
156 |
THR_SetName(mpl->name); |
| 108 |
156 |
THR_Init(); |
| 109 |
156 |
mpl_slp = 0.15; // random |
| 110 |
2028125 |
while (1) { |
| 111 |
2498056 |
VTIM_sleep(mpl_slp); |
| 112 |
2498056 |
mpl_slp = 0.814; // random |
| 113 |
2498056 |
mpl->t_now = VTIM_real(); |
| 114 |
|
|
| 115 |
2498056 |
if (mi != NULL && (mpl->n_pool > mpl->param->max_pool || |
| 116 |
22 |
mi->size < *mpl->cur_size)) { |
| 117 |
0 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 118 |
0 |
FREE_OBJ(mi); |
| 119 |
0 |
} |
| 120 |
|
|
| 121 |
2498056 |
if (mi == NULL && mpl->n_pool < mpl->param->min_pool) |
| 122 |
1870254 |
mi = mpl_alloc(mpl); |
| 123 |
|
|
| 124 |
2498056 |
if (mpl->n_pool < mpl->param->min_pool && mi != NULL) { |
| 125 |
|
/* can do */ |
| 126 |
2498060 |
} else if (mpl->n_pool > mpl->param->max_pool && mi == NULL) { |
| 127 |
|
/* can do */ |
| 128 |
592701 |
} else if (!VTAILQ_EMPTY(&mpl->surplus)) { |
| 129 |
|
/* can do */ |
| 130 |
591776 |
} else if (last + .1 * mpl->param->max_age < mpl->t_now) { |
| 131 |
|
/* should do */ |
| 132 |
591736 |
} else if (mpl->self_destruct) { |
| 133 |
|
/* can do */ |
| 134 |
1 |
} else { |
| 135 |
467970 |
continue; /* nothing to do */ |
| 136 |
|
} |
| 137 |
|
|
| 138 |
2030090 |
mpl_slp = 0.314; // random |
| 139 |
|
|
| 140 |
2030090 |
if (Lck_Trylock(&mpl->mtx)) |
| 141 |
1961 |
continue; |
| 142 |
|
|
| 143 |
2028129 |
if (mpl->self_destruct) { |
| 144 |
160 |
AZ(mpl->live); |
| 145 |
1772 |
while (1) { |
| 146 |
1772 |
if (mi == NULL) { |
| 147 |
1767 |
mi = VTAILQ_FIRST(&mpl->list); |
| 148 |
1767 |
if (mi != NULL) { |
| 149 |
1609 |
mpl->vsc->pool = --mpl->n_pool; |
| 150 |
1609 |
VTAILQ_REMOVE(&mpl->list, |
| 151 |
|
mi, list); |
| 152 |
1609 |
} |
| 153 |
1767 |
} |
| 154 |
1772 |
if (mi == NULL) { |
| 155 |
160 |
mi = VTAILQ_FIRST(&mpl->surplus); |
| 156 |
160 |
if (mi != NULL) |
| 157 |
0 |
VTAILQ_REMOVE(&mpl->surplus, |
| 158 |
|
mi, list); |
| 159 |
160 |
} |
| 160 |
1772 |
if (mi == NULL) |
| 161 |
160 |
break; |
| 162 |
1612 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 163 |
1612 |
FREE_OBJ(mi); |
| 164 |
|
} |
| 165 |
160 |
VSC_mempool_Destroy(&mpl->vsc_seg); |
| 166 |
160 |
Lck_Unlock(&mpl->mtx); |
| 167 |
160 |
Lck_Delete(&mpl->mtx); |
| 168 |
160 |
FREE_OBJ(mpl); |
| 169 |
160 |
break; |
| 170 |
|
} |
| 171 |
|
|
| 172 |
3931995 |
if (mpl->n_pool < mpl->param->min_pool && |
| 173 |
1904649 |
mi != NULL && mi->size >= *mpl->cur_size) { |
| 174 |
1903677 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 175 |
1903677 |
mpl->vsc->pool = ++mpl->n_pool; |
| 176 |
1903677 |
mi->touched = mpl->t_now; |
| 177 |
1903677 |
VTAILQ_INSERT_HEAD(&mpl->list, mi, list); |
| 178 |
1903677 |
mi = NULL; |
| 179 |
1903677 |
mpl_slp = .01; // random |
| 180 |
|
|
| 181 |
1903677 |
} |
| 182 |
2027969 |
if (mpl->n_pool > mpl->param->max_pool && mi == NULL) { |
| 183 |
926 |
mi = VTAILQ_FIRST(&mpl->list); |
| 184 |
926 |
CHECK_OBJ_NOTNULL(mi, MEMITEM_MAGIC); |
| 185 |
926 |
mpl->vsc->pool = --mpl->n_pool; |
| 186 |
926 |
mpl->vsc->surplus++; |
| 187 |
926 |
VTAILQ_REMOVE(&mpl->list, mi, list); |
| 188 |
926 |
mpl_slp = .01; // random |
| 189 |
926 |
} |
| 190 |
2027969 |
if (mi == NULL) { |
| 191 |
2027206 |
mi = VTAILQ_FIRST(&mpl->surplus); |
| 192 |
2027206 |
if (mi != NULL) { |
| 193 |
40 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 194 |
40 |
VTAILQ_REMOVE(&mpl->surplus, mi, list); |
| 195 |
40 |
mpl_slp = .01; // random |
| 196 |
40 |
} |
| 197 |
2027206 |
} |
| 198 |
2027969 |
if (mi == NULL && mpl->n_pool > mpl->param->min_pool) { |
| 199 |
21730 |
mi = VTAILQ_LAST(&mpl->list, memhead_s); |
| 200 |
21730 |
CHECK_OBJ_NOTNULL(mi, MEMITEM_MAGIC); |
| 201 |
21730 |
if (mi->touched + mpl->param->max_age < mpl->t_now) { |
| 202 |
6260 |
mpl->vsc->pool = --mpl->n_pool; |
| 203 |
6260 |
mpl->vsc->timeout++; |
| 204 |
6260 |
VTAILQ_REMOVE(&mpl->list, mi, list); |
| 205 |
6260 |
mpl_slp = .01; // random |
| 206 |
6260 |
} else { |
| 207 |
15470 |
mi = NULL; |
| 208 |
15470 |
last = mpl->t_now; |
| 209 |
|
} |
| 210 |
2027969 |
} else if (mpl->n_pool <= mpl->param->min_pool) { |
| 211 |
2005723 |
last = mpl->t_now; |
| 212 |
2005723 |
} |
| 213 |
|
|
| 214 |
2027969 |
Lck_Unlock(&mpl->mtx); |
| 215 |
|
|
| 216 |
2027969 |
if (mi != NULL) { |
| 217 |
7225 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 218 |
7225 |
FREE_OBJ(mi); |
| 219 |
7225 |
} |
| 220 |
|
} |
| 221 |
160 |
return (NULL); |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
/*--------------------------------------------------------------------- |
| 225 |
|
* Create a new memory pool, and start the guard thread for it. |
| 226 |
|
*/ |
| 227 |
|
|
| 228 |
|
struct mempool * |
| 229 |
188403 |
MPL_New(const char *name, |
| 230 |
|
volatile struct poolparam *pp, volatile unsigned *cur_size) |
| 231 |
|
{ |
| 232 |
|
struct mempool *mpl; |
| 233 |
|
|
| 234 |
188403 |
ALLOC_OBJ(mpl, MEMPOOL_MAGIC); |
| 235 |
188403 |
AN(mpl); |
| 236 |
188403 |
bprintf(mpl->name, "MPL_%s", name); |
| 237 |
188403 |
mpl->param = pp; |
| 238 |
188403 |
mpl->cur_size = cur_size; |
| 239 |
188403 |
VTAILQ_INIT(&mpl->list); |
| 240 |
188403 |
VTAILQ_INIT(&mpl->surplus); |
| 241 |
188403 |
Lck_New(&mpl->mtx, lck_mempool); |
| 242 |
|
/* XXX: prealloc min_pool */ |
| 243 |
188403 |
mpl->vsc = VSC_mempool_New(NULL, &mpl->vsc_seg, mpl->name + 4); |
| 244 |
188403 |
AN(mpl->vsc); |
| 245 |
188403 |
PTOK(pthread_create(&mpl->thread, NULL, mpl_guard, mpl)); |
| 246 |
188403 |
PTOK(pthread_detach(mpl->thread)); |
| 247 |
188403 |
return (mpl); |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
/*--------------------------------------------------------------------- |
| 251 |
|
* Destroy a memory pool. There must be no live items, and we cheat |
| 252 |
|
* and leave all the hard work to the guard thread. |
| 253 |
|
*/ |
| 254 |
|
|
| 255 |
|
void |
| 256 |
160 |
MPL_Destroy(struct mempool **mpp) |
| 257 |
|
{ |
| 258 |
|
struct mempool *mpl; |
| 259 |
|
|
| 260 |
160 |
TAKE_OBJ_NOTNULL(mpl, mpp, MEMPOOL_MAGIC); |
| 261 |
160 |
Lck_Lock(&mpl->mtx); |
| 262 |
160 |
AZ(mpl->live); |
| 263 |
160 |
mpl->self_destruct = 1; |
| 264 |
160 |
Lck_Unlock(&mpl->mtx); |
| 265 |
160 |
} |
| 266 |
|
|
| 267 |
|
/*--------------------------------------------------------------------- |
| 268 |
|
*/ |
| 269 |
|
|
| 270 |
|
void * |
| 271 |
303534 |
MPL_Get(struct mempool *mpl, unsigned *size) |
| 272 |
|
{ |
| 273 |
|
struct memitem *mi; |
| 274 |
|
|
| 275 |
303534 |
CHECK_OBJ_NOTNULL(mpl, MEMPOOL_MAGIC); |
| 276 |
303534 |
AN(size); |
| 277 |
|
|
| 278 |
303534 |
Lck_Lock(&mpl->mtx); |
| 279 |
|
|
| 280 |
303534 |
mpl->vsc->allocs++; |
| 281 |
303534 |
mpl->vsc->live = ++mpl->live; |
| 282 |
|
|
| 283 |
303534 |
do { |
| 284 |
303566 |
mi = VTAILQ_FIRST(&mpl->list); |
| 285 |
303566 |
if (mi == NULL) { |
| 286 |
817 |
mpl->vsc->randry++; |
| 287 |
817 |
break; |
| 288 |
|
} |
| 289 |
302749 |
mpl->vsc->pool = --mpl->n_pool; |
| 290 |
302749 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 291 |
302749 |
VTAILQ_REMOVE(&mpl->list, mi, list); |
| 292 |
302749 |
if (mi->size < *mpl->cur_size) { |
| 293 |
40 |
mpl->vsc->toosmall++; |
| 294 |
40 |
VTAILQ_INSERT_HEAD(&mpl->surplus, mi, list); |
| 295 |
40 |
mi = NULL; |
| 296 |
40 |
} else { |
| 297 |
302709 |
mpl->vsc->recycle++; |
| 298 |
|
} |
| 299 |
302749 |
} while (mi == NULL); |
| 300 |
|
|
| 301 |
303546 |
Lck_Unlock(&mpl->mtx); |
| 302 |
|
|
| 303 |
303546 |
if (mi == NULL) |
| 304 |
817 |
mi = mpl_alloc(mpl); |
| 305 |
303546 |
*size = mi->size - sizeof *mi; |
| 306 |
|
|
| 307 |
303546 |
CHECK_OBJ(mi, MEMITEM_MAGIC); |
| 308 |
|
/* Throw away sizeof info for FlexeLint: */ |
| 309 |
303546 |
return ((void *)(uintptr_t)(mi + 1)); |
| 310 |
|
} |
| 311 |
|
|
| 312 |
|
void |
| 313 |
302566 |
MPL_Free(struct mempool *mpl, void *item) |
| 314 |
|
{ |
| 315 |
|
struct memitem *mi; |
| 316 |
|
|
| 317 |
302566 |
CHECK_OBJ_NOTNULL(mpl, MEMPOOL_MAGIC); |
| 318 |
302566 |
AN(item); |
| 319 |
|
|
| 320 |
302566 |
mi = (void*)((uintptr_t)item - sizeof(*mi)); |
| 321 |
302566 |
CHECK_OBJ_NOTNULL(mi, MEMITEM_MAGIC); |
| 322 |
302566 |
memset(item, 0, mi->size - sizeof *mi); |
| 323 |
|
|
| 324 |
302566 |
Lck_Lock(&mpl->mtx); |
| 325 |
|
|
| 326 |
302566 |
mpl->vsc->frees++; |
| 327 |
302566 |
mpl->vsc->live = --mpl->live; |
| 328 |
|
|
| 329 |
302566 |
if (mi->size < *mpl->cur_size) { |
| 330 |
0 |
mpl->vsc->toosmall++; |
| 331 |
0 |
VTAILQ_INSERT_HEAD(&mpl->surplus, mi, list); |
| 332 |
0 |
} else { |
| 333 |
302566 |
mpl->vsc->pool = ++mpl->n_pool; |
| 334 |
302566 |
mi->touched = mpl->t_now; |
| 335 |
302566 |
VTAILQ_INSERT_HEAD(&mpl->list, mi, list); |
| 336 |
|
} |
| 337 |
|
|
| 338 |
302566 |
Lck_Unlock(&mpl->mtx); |
| 339 |
302566 |
} |
| 340 |
|
|
| 341 |
|
void |
| 342 |
122248 |
MPL_AssertSane(const void *item) |
| 343 |
|
{ |
| 344 |
|
struct memitem *mi; |
| 345 |
122248 |
mi = (void*)((uintptr_t)item - sizeof(*mi)); |
| 346 |
122248 |
CHECK_OBJ_NOTNULL(mi, MEMITEM_MAGIC); |
| 347 |
122248 |
} |