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