Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Memory management functions. |
| 3 | * |
| 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame] | 12 | #include <errno.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 13 | |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 14 | #include <types/applet.h> |
| 15 | #include <types/cli.h> |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 16 | #include <types/global.h> |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 17 | #include <types/stats.h> |
| 18 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 19 | #include <common/config.h> |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 20 | #include <common/debug.h> |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 21 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 22 | #include <common/initcall.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 23 | #include <common/memory.h> |
| 24 | #include <common/mini-clist.h> |
| 25 | #include <common/standard.h> |
| 26 | |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 27 | #include <proto/applet.h> |
| 28 | #include <proto/cli.h> |
| 29 | #include <proto/channel.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 30 | #include <proto/log.h> |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 31 | #include <proto/stream_interface.h> |
| 32 | #include <proto/stats.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 34 | /* These are the most common pools, expected to be initialized first. These |
| 35 | * ones are allocated from an array, allowing to map them to an index. |
| 36 | */ |
| 37 | struct pool_head pool_base_start[MAX_BASE_POOLS] = { }; |
| 38 | unsigned int pool_base_count = 0; |
| 39 | |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 40 | THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS] = { }; |
| 41 | THREAD_LOCAL struct list pool_lru_head = { }; /* oldest objects */ |
| 42 | THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */ |
| 43 | THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */ |
| 44 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 45 | static struct list pools = LIST_HEAD_INIT(pools); |
Willy Tarreau | 067ac9f | 2015-10-08 14:12:13 +0200 | [diff] [blame] | 46 | int mem_poison_byte = -1; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 47 | |
| 48 | /* Try to find an existing shared pool with the same characteristics and |
| 49 | * returns it, otherwise creates this one. NULL is returned if no memory |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 50 | * is available for a new creation. Two flags are supported : |
| 51 | * - MEM_F_SHARED to indicate that the pool may be shared with other users |
| 52 | * - MEM_F_EXACT to indicate that the size must not be rounded up |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 53 | */ |
| 54 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags) |
| 55 | { |
| 56 | struct pool_head *pool; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 57 | struct pool_head *entry; |
| 58 | struct list *start; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 59 | unsigned int align; |
| 60 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 61 | /* We need to store a (void *) at the end of the chunks. Since we know |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 62 | * that the malloc() function will never return such a small size, |
| 63 | * let's round the size up to something slightly bigger, in order to |
| 64 | * ease merging of entries. Note that the rounding is a power of two. |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 65 | * This extra (void *) is not accounted for in the size computation |
| 66 | * so that the visible parts outside are not affected. |
Willy Tarreau | 30f931e | 2018-10-23 14:40:23 +0200 | [diff] [blame] | 67 | * |
| 68 | * Note: for the LRU cache, we need to store 2 doubly-linked lists. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 69 | */ |
| 70 | |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 71 | if (!(flags & MEM_F_EXACT)) { |
Willy Tarreau | 30f931e | 2018-10-23 14:40:23 +0200 | [diff] [blame] | 72 | align = 4 * sizeof(void *); // 2 lists = 4 pointers min |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 73 | size = ((size + POOL_EXTRA + align - 1) & -align) - POOL_EXTRA; |
| 74 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 75 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 76 | /* TODO: thread: we do not lock pool list for now because all pools are |
| 77 | * created during HAProxy startup (so before threads creation) */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 78 | start = &pools; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 79 | pool = NULL; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 80 | |
| 81 | list_for_each_entry(entry, &pools, list) { |
| 82 | if (entry->size == size) { |
| 83 | /* either we can share this place and we take it, or |
| 84 | * we look for a sharable one or for the next position |
| 85 | * before which we will insert a new one. |
| 86 | */ |
| 87 | if (flags & entry->flags & MEM_F_SHARED) { |
| 88 | /* we can share this one */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 89 | pool = entry; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 90 | DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 94 | else if (entry->size > size) { |
| 95 | /* insert before this one */ |
| 96 | start = &entry->list; |
| 97 | break; |
| 98 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (!pool) { |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 102 | if (pool_base_count < MAX_BASE_POOLS) |
| 103 | pool = &pool_base_start[pool_base_count++]; |
| 104 | |
| 105 | if (!pool) { |
| 106 | /* look for a freed entry */ |
| 107 | for (entry = pool_base_start; entry != pool_base_start + MAX_BASE_POOLS; entry++) { |
| 108 | if (!entry->size) { |
| 109 | pool = entry; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!pool) |
| 116 | pool = calloc(1, sizeof(*pool)); |
| 117 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 118 | if (!pool) |
| 119 | return NULL; |
| 120 | if (name) |
| 121 | strlcpy2(pool->name, name, sizeof(pool->name)); |
| 122 | pool->size = size; |
| 123 | pool->flags = flags; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 124 | LIST_ADDQ(start, &pool->list); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 125 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 126 | pool->users++; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 127 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 128 | HA_SPIN_INIT(&pool->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 129 | #endif |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 130 | return pool; |
| 131 | } |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 132 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 133 | #ifdef CONFIG_HAP_LOCKLESS_POOLS |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 134 | /* Allocates new entries for pool <pool> until there are at least <avail> + 1 |
| 135 | * available, then returns the last one for immediate use, so that at least |
| 136 | * <avail> are left available in the pool upon return. NULL is returned if the |
| 137 | * last entry could not be allocated. It's important to note that at least one |
| 138 | * allocation is always performed even if there are enough entries in the pool. |
| 139 | * A call to the garbage collector is performed at most once in case malloc() |
| 140 | * returns an error, before returning NULL. |
| 141 | */ |
| 142 | void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail) |
| 143 | { |
Olivier Houchard | 8b2c8a7 | 2018-10-21 01:52:59 +0200 | [diff] [blame] | 144 | void *ptr = NULL, **free_list; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 145 | int failed = 0; |
| 146 | int size = pool->size; |
| 147 | int limit = pool->limit; |
| 148 | int allocated = pool->allocated, allocated_orig = allocated; |
| 149 | |
| 150 | /* stop point */ |
| 151 | avail += pool->used; |
| 152 | |
| 153 | while (1) { |
| 154 | if (limit && allocated >= limit) { |
| 155 | HA_ATOMIC_ADD(&pool->allocated, allocated - allocated_orig); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | ptr = malloc(size + POOL_EXTRA); |
| 160 | if (!ptr) { |
| 161 | HA_ATOMIC_ADD(&pool->failed, 1); |
| 162 | if (failed) |
| 163 | return NULL; |
| 164 | failed++; |
| 165 | pool_gc(pool); |
| 166 | continue; |
| 167 | } |
| 168 | if (++allocated > avail) |
| 169 | break; |
| 170 | |
| 171 | free_list = pool->free_list; |
| 172 | do { |
| 173 | *POOL_LINK(pool, ptr) = free_list; |
| 174 | __ha_barrier_store(); |
Olivier Houchard | 8b2c8a7 | 2018-10-21 01:52:59 +0200 | [diff] [blame] | 175 | } while (HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr) == 0); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | HA_ATOMIC_ADD(&pool->allocated, allocated - allocated_orig); |
| 179 | HA_ATOMIC_ADD(&pool->used, 1); |
| 180 | |
| 181 | #ifdef DEBUG_MEMORY_POOLS |
| 182 | /* keep track of where the element was allocated from */ |
| 183 | *POOL_LINK(pool, ptr) = (void *)pool; |
| 184 | #endif |
| 185 | return ptr; |
| 186 | } |
| 187 | void *pool_refill_alloc(struct pool_head *pool, unsigned int avail) |
| 188 | { |
| 189 | void *ptr; |
| 190 | |
| 191 | ptr = __pool_refill_alloc(pool, avail); |
| 192 | return ptr; |
| 193 | } |
| 194 | /* |
| 195 | * This function frees whatever can be freed in pool <pool>. |
| 196 | */ |
| 197 | void pool_flush(struct pool_head *pool) |
| 198 | { |
Olivier Houchard | 8b2c8a7 | 2018-10-21 01:52:59 +0200 | [diff] [blame] | 199 | void **next, *temp; |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 200 | int removed = 0; |
| 201 | |
| 202 | if (!pool) |
| 203 | return; |
| 204 | do { |
| 205 | next = pool->free_list; |
Olivier Houchard | 8b2c8a7 | 2018-10-21 01:52:59 +0200 | [diff] [blame] | 206 | } while (!HA_ATOMIC_CAS(&pool->free_list, &next, NULL)); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 207 | while (next) { |
| 208 | temp = next; |
| 209 | next = *POOL_LINK(pool, temp); |
| 210 | removed++; |
| 211 | free(temp); |
| 212 | } |
| 213 | pool->free_list = next; |
| 214 | HA_ATOMIC_SUB(&pool->allocated, removed); |
| 215 | /* here, we should have pool->allocate == pool->used */ |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * This function frees whatever can be freed in all pools, but respecting |
| 220 | * the minimum thresholds imposed by owners. It takes care of avoiding |
| 221 | * recursion because it may be called from a signal handler. |
| 222 | * |
| 223 | * <pool_ctx> is unused |
| 224 | */ |
| 225 | void pool_gc(struct pool_head *pool_ctx) |
| 226 | { |
| 227 | static int recurse; |
| 228 | int cur_recurse = 0; |
| 229 | struct pool_head *entry; |
| 230 | |
| 231 | if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1)) |
| 232 | return; |
| 233 | |
| 234 | list_for_each_entry(entry, &pools, list) { |
| 235 | while ((int)((volatile int)entry->allocated - (volatile int)entry->used) > (int)entry->minavail) { |
| 236 | struct pool_free_list cmp, new; |
| 237 | |
| 238 | cmp.seq = entry->seq; |
| 239 | __ha_barrier_load(); |
| 240 | cmp.free_list = entry->free_list; |
| 241 | __ha_barrier_load(); |
| 242 | if (cmp.free_list == NULL) |
| 243 | break; |
| 244 | new.free_list = *POOL_LINK(entry, cmp.free_list); |
| 245 | new.seq = cmp.seq + 1; |
| 246 | if (__ha_cas_dw(&entry->free_list, &cmp, &new) == 0) |
| 247 | continue; |
| 248 | free(cmp.free_list); |
| 249 | HA_ATOMIC_SUB(&entry->allocated, 1); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | HA_ATOMIC_STORE(&recurse, 0); |
| 254 | } |
Willy Tarreau | e18db9e | 2018-10-16 10:28:54 +0200 | [diff] [blame] | 255 | |
| 256 | /* frees an object to the local cache, possibly pushing oldest objects to the |
| 257 | * global pool. Must not be called directly. |
| 258 | */ |
| 259 | void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx) |
| 260 | { |
| 261 | struct pool_cache_item *item = (struct pool_cache_item *)ptr; |
| 262 | struct pool_cache_head *ph = &pool_cache[idx]; |
| 263 | |
| 264 | /* never allocated or empty */ |
| 265 | if (unlikely(ph->list.n == NULL)) { |
| 266 | LIST_INIT(&ph->list); |
| 267 | ph->size = pool->size; |
| 268 | if (pool_lru_head.n == NULL) |
| 269 | LIST_INIT(&pool_lru_head); |
| 270 | } |
| 271 | |
| 272 | LIST_ADD(&ph->list, &item->by_pool); |
| 273 | LIST_ADD(&pool_lru_head, &item->by_lru); |
| 274 | ph->count++; |
| 275 | pool_cache_count++; |
| 276 | pool_cache_bytes += ph->size; |
| 277 | |
| 278 | if (pool_cache_bytes <= CONFIG_HAP_POOL_CACHE_SIZE) |
| 279 | return; |
| 280 | |
| 281 | do { |
| 282 | item = LIST_PREV(&pool_lru_head, struct pool_cache_item *, by_lru); |
| 283 | /* note: by definition we remove oldest objects so they also are the |
| 284 | * oldest in their own pools, thus their next is the pool's head. |
| 285 | */ |
| 286 | ph = LIST_NEXT(&item->by_pool, struct pool_cache_head *, list); |
| 287 | LIST_DEL(&item->by_pool); |
| 288 | LIST_DEL(&item->by_lru); |
| 289 | ph->count--; |
| 290 | pool_cache_count--; |
| 291 | pool_cache_bytes -= ph->size; |
| 292 | __pool_free(pool_base_start + (ph - pool_cache), item); |
| 293 | } while (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 7 / 8); |
| 294 | } |
| 295 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 296 | #else /* CONFIG_HAP_LOCKLESS_POOLS */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 297 | |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 298 | /* Allocates new entries for pool <pool> until there are at least <avail> + 1 |
| 299 | * available, then returns the last one for immediate use, so that at least |
| 300 | * <avail> are left available in the pool upon return. NULL is returned if the |
| 301 | * last entry could not be allocated. It's important to note that at least one |
| 302 | * allocation is always performed even if there are enough entries in the pool. |
| 303 | * A call to the garbage collector is performed at most once in case malloc() |
| 304 | * returns an error, before returning NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 305 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 306 | void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 307 | { |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 308 | void *ptr = NULL; |
| 309 | int failed = 0; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 310 | |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 311 | /* stop point */ |
| 312 | avail += pool->used; |
| 313 | |
| 314 | while (1) { |
| 315 | if (pool->limit && pool->allocated >= pool->limit) |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 316 | return NULL; |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 317 | |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame] | 318 | ptr = pool_alloc_area(pool->size + POOL_EXTRA); |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 319 | if (!ptr) { |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 320 | pool->failed++; |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 321 | if (failed) |
| 322 | return NULL; |
| 323 | failed++; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 324 | pool_gc(pool); |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | if (++pool->allocated > avail) |
| 328 | break; |
| 329 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 330 | *POOL_LINK(pool, ptr) = (void *)pool->free_list; |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 331 | pool->free_list = ptr; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 332 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 333 | pool->used++; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 334 | #ifdef DEBUG_MEMORY_POOLS |
| 335 | /* keep track of where the element was allocated from */ |
| 336 | *POOL_LINK(pool, ptr) = (void *)pool; |
| 337 | #endif |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 338 | return ptr; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 339 | } |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 340 | void *pool_refill_alloc(struct pool_head *pool, unsigned int avail) |
| 341 | { |
| 342 | void *ptr; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 343 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 344 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 345 | ptr = __pool_refill_alloc(pool, avail); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 346 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 347 | return ptr; |
| 348 | } |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 349 | /* |
| 350 | * This function frees whatever can be freed in pool <pool>. |
| 351 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 352 | void pool_flush(struct pool_head *pool) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 353 | { |
| 354 | void *temp, *next; |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 355 | if (!pool) |
| 356 | return; |
| 357 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 358 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 359 | next = pool->free_list; |
| 360 | while (next) { |
| 361 | temp = next; |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 362 | next = *POOL_LINK(pool, temp); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 363 | pool->allocated--; |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame] | 364 | pool_free_area(temp, pool->size + POOL_EXTRA); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 365 | } |
| 366 | pool->free_list = next; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 367 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 368 | /* here, we should have pool->allocate == pool->used */ |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * This function frees whatever can be freed in all pools, but respecting |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 373 | * the minimum thresholds imposed by owners. It takes care of avoiding |
| 374 | * recursion because it may be called from a signal handler. |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 375 | * |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 376 | * <pool_ctx> is used when pool_gc is called to release resources to allocate |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 377 | * an element in __pool_refill_alloc. It is important because <pool_ctx> is |
| 378 | * already locked, so we need to skip the lock here. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 379 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 380 | void pool_gc(struct pool_head *pool_ctx) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 381 | { |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 382 | static int recurse; |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 383 | int cur_recurse = 0; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 384 | struct pool_head *entry; |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 385 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 386 | if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1)) |
| 387 | return; |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 388 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 389 | list_for_each_entry(entry, &pools, list) { |
| 390 | void *temp, *next; |
| 391 | //qfprintf(stderr, "Flushing pool %s\n", entry->name); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 392 | if (entry != pool_ctx) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 393 | HA_SPIN_LOCK(POOL_LOCK, &entry->lock); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 394 | next = entry->free_list; |
| 395 | while (next && |
Willy Tarreau | 57767b8 | 2014-12-22 21:40:55 +0100 | [diff] [blame] | 396 | (int)(entry->allocated - entry->used) > (int)entry->minavail) { |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 397 | temp = next; |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 398 | next = *POOL_LINK(entry, temp); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 399 | entry->allocated--; |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame] | 400 | pool_free_area(temp, entry->size + POOL_EXTRA); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 401 | } |
| 402 | entry->free_list = next; |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 403 | if (entry != pool_ctx) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 404 | HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 405 | } |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 406 | |
| 407 | HA_ATOMIC_STORE(&recurse, 0); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 408 | } |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 409 | #endif |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 410 | |
| 411 | /* |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 412 | * This function destroys a pool by freeing it completely, unless it's still |
| 413 | * in use. This should be called only under extreme circumstances. It always |
| 414 | * returns NULL if the resulting pool is empty, easing the clearing of the old |
| 415 | * pointer, otherwise it returns the pool. |
| 416 | * . |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 417 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 418 | void *pool_destroy(struct pool_head *pool) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 419 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 420 | if (pool) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 421 | pool_flush(pool); |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 422 | if (pool->used) |
| 423 | return pool; |
| 424 | pool->users--; |
| 425 | if (!pool->users) { |
| 426 | LIST_DEL(&pool->list); |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 427 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 428 | HA_SPIN_DESTROY(&pool->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 429 | #endif |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 430 | if ((pool - pool_base_start) < MAX_BASE_POOLS) |
| 431 | memset(pool, 0, sizeof(*pool)); |
| 432 | else |
| 433 | free(pool); |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 434 | } |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 435 | } |
| 436 | return NULL; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 439 | /* This function dumps memory usage information into the trash buffer. */ |
| 440 | void dump_pools_to_trash() |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 441 | { |
| 442 | struct pool_head *entry; |
| 443 | unsigned long allocated, used; |
| 444 | int nbpools; |
| 445 | |
| 446 | allocated = used = nbpools = 0; |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 447 | chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n"); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 448 | list_for_each_entry(entry, &pools, list) { |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 449 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 450 | HA_SPIN_LOCK(POOL_LOCK, &entry->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 451 | #endif |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 452 | chunk_appendf(&trash, " - Pool %s (%d bytes) : %d allocated (%u bytes), %d used, %d failures, %d users, @%p=%02d%s\n", |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 453 | entry->name, entry->size, entry->allocated, |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 454 | entry->size * entry->allocated, entry->used, entry->failed, |
Willy Tarreau | 0a93b64 | 2018-10-16 07:58:39 +0200 | [diff] [blame] | 455 | entry->users, entry, (int)pool_get_index(entry), |
| 456 | (entry->flags & MEM_F_SHARED) ? " [SHARED]" : ""); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 457 | |
| 458 | allocated += entry->allocated * entry->size; |
| 459 | used += entry->used * entry->size; |
| 460 | nbpools++; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 461 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 462 | HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 463 | #endif |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 464 | } |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 465 | chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n", |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 466 | nbpools, allocated, used); |
| 467 | } |
| 468 | |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 469 | /* Dump statistics on pools usage. */ |
| 470 | void dump_pools(void) |
| 471 | { |
| 472 | dump_pools_to_trash(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 473 | qfprintf(stderr, "%s", trash.area); |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 476 | /* This function returns the total number of failed pool allocations */ |
| 477 | int pool_total_failures() |
| 478 | { |
| 479 | struct pool_head *entry; |
| 480 | int failed = 0; |
| 481 | |
| 482 | list_for_each_entry(entry, &pools, list) |
| 483 | failed += entry->failed; |
| 484 | return failed; |
| 485 | } |
| 486 | |
| 487 | /* This function returns the total amount of memory allocated in pools (in bytes) */ |
| 488 | unsigned long pool_total_allocated() |
| 489 | { |
| 490 | struct pool_head *entry; |
| 491 | unsigned long allocated = 0; |
| 492 | |
| 493 | list_for_each_entry(entry, &pools, list) |
| 494 | allocated += entry->allocated * entry->size; |
| 495 | return allocated; |
| 496 | } |
| 497 | |
| 498 | /* This function returns the total amount of memory used in pools (in bytes) */ |
| 499 | unsigned long pool_total_used() |
| 500 | { |
| 501 | struct pool_head *entry; |
| 502 | unsigned long used = 0; |
| 503 | |
| 504 | list_for_each_entry(entry, &pools, list) |
| 505 | used += entry->used * entry->size; |
| 506 | return used; |
| 507 | } |
| 508 | |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 509 | /* This function dumps memory usage information onto the stream interface's |
| 510 | * read buffer. It returns 0 as long as it does not complete, non-zero upon |
| 511 | * completion. No state is used. |
| 512 | */ |
| 513 | static int cli_io_handler_dump_pools(struct appctx *appctx) |
| 514 | { |
| 515 | struct stream_interface *si = appctx->owner; |
| 516 | |
| 517 | dump_pools_to_trash(); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 518 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 519 | si_rx_room_blk(si); |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | return 1; |
| 523 | } |
| 524 | |
Willy Tarreau | 7107c8b | 2018-11-26 11:44:35 +0100 | [diff] [blame] | 525 | /* callback used to create early pool <name> of size <size> and store the |
| 526 | * resulting pointer into <ptr>. If the allocation fails, it quits with after |
| 527 | * emitting an error message. |
| 528 | */ |
| 529 | void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size) |
| 530 | { |
| 531 | *ptr = create_pool(name, size, MEM_F_SHARED); |
| 532 | if (!*ptr) { |
| 533 | ha_alert("Failed to allocate pool '%s' of size %u : %s. Aborting.\n", |
| 534 | name, size, strerror(errno)); |
| 535 | exit(1); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 540 | /* register cli keywords */ |
| 541 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | e9ecec8 | 2016-12-16 18:55:23 +0100 | [diff] [blame] | 542 | { { "show", "pools", NULL }, "show pools : report information about the memory pools usage", NULL, cli_io_handler_dump_pools }, |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 543 | {{},} |
| 544 | }}; |
| 545 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 546 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | e7ed885 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 547 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 548 | /* |
| 549 | * Local variables: |
| 550 | * c-indent-level: 8 |
| 551 | * c-basic-offset: 8 |
| 552 | * End: |
| 553 | */ |