blob: 587702a2ea3a22fbb3c765f4e538d3af5cdc5344 [file] [log] [blame]
Willy Tarreau50e608d2007-05-13 18:26:08 +02001/*
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 Tarreau7107c8b2018-11-26 11:44:35 +010012#include <errno.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020013
William Lallemande7ed8852016-11-19 02:25:36 +010014#include <types/applet.h>
15#include <types/cli.h>
Willy Tarreau12833bb2014-01-28 16:49:56 +010016#include <types/global.h>
William Lallemande7ed8852016-11-19 02:25:36 +010017#include <types/stats.h>
18
Willy Tarreau50e608d2007-05-13 18:26:08 +020019#include <common/config.h>
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020020#include <common/debug.h>
Willy Tarreaue18db9e2018-10-16 10:28:54 +020021#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020023#include <common/memory.h>
24#include <common/mini-clist.h>
25#include <common/standard.h>
26
William Lallemande7ed8852016-11-19 02:25:36 +010027#include <proto/applet.h>
28#include <proto/cli.h>
29#include <proto/channel.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020030#include <proto/log.h>
William Lallemande7ed8852016-11-19 02:25:36 +010031#include <proto/stream_interface.h>
32#include <proto/stats.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020033
Willy Tarreau0a93b642018-10-16 07:58:39 +020034/* 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 */
37struct pool_head pool_base_start[MAX_BASE_POOLS] = { };
38unsigned int pool_base_count = 0;
39
Willy Tarreaue18db9e2018-10-16 10:28:54 +020040THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS] = { };
41THREAD_LOCAL struct list pool_lru_head = { }; /* oldest objects */
42THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */
43THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */
44
Willy Tarreau50e608d2007-05-13 18:26:08 +020045static struct list pools = LIST_HEAD_INIT(pools);
Willy Tarreau067ac9f2015-10-08 14:12:13 +020046int mem_poison_byte = -1;
Willy Tarreau50e608d2007-05-13 18:26:08 +020047
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 Tarreau581bf812016-01-25 02:19:13 +010050 * 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 Tarreau50e608d2007-05-13 18:26:08 +020053 */
54struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
55{
56 struct pool_head *pool;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020057 struct pool_head *entry;
58 struct list *start;
Willy Tarreau50e608d2007-05-13 18:26:08 +020059 unsigned int align;
60
Willy Tarreauac421112015-10-28 15:09:29 +010061 /* We need to store a (void *) at the end of the chunks. Since we know
Willy Tarreau50e608d2007-05-13 18:26:08 +020062 * 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 Tarreauac421112015-10-28 15:09:29 +010065 * This extra (void *) is not accounted for in the size computation
66 * so that the visible parts outside are not affected.
Willy Tarreau30f931e2018-10-23 14:40:23 +020067 *
68 * Note: for the LRU cache, we need to store 2 doubly-linked lists.
Willy Tarreau50e608d2007-05-13 18:26:08 +020069 */
70
Willy Tarreau581bf812016-01-25 02:19:13 +010071 if (!(flags & MEM_F_EXACT)) {
Willy Tarreau30f931e2018-10-23 14:40:23 +020072 align = 4 * sizeof(void *); // 2 lists = 4 pointers min
Willy Tarreau581bf812016-01-25 02:19:13 +010073 size = ((size + POOL_EXTRA + align - 1) & -align) - POOL_EXTRA;
74 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020075
Christopher Fauletb349e482017-08-29 09:52:38 +020076 /* TODO: thread: we do not lock pool list for now because all pools are
77 * created during HAProxy startup (so before threads creation) */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020078 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +020079 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020080
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 Tarreau50e608d2007-05-13 18:26:08 +020089 pool = entry;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020090 DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
Willy Tarreau50e608d2007-05-13 18:26:08 +020091 break;
92 }
93 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020094 else if (entry->size > size) {
95 /* insert before this one */
96 start = &entry->list;
97 break;
98 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020099 }
100
101 if (!pool) {
Willy Tarreau0a93b642018-10-16 07:58:39 +0200102 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 Tarreau50e608d2007-05-13 18:26:08 +0200118 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 Tarreau7dcd46d2007-05-14 00:16:13 +0200124 LIST_ADDQ(start, &pool->list);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200125 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200126 pool->users++;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100127#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100128 HA_SPIN_INIT(&pool->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100129#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +0200130 return pool;
131}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100132
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100133#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +0100134/* 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 */
142void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
143{
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200144 void *ptr = NULL, **free_list;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100145 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 Houchard8b2c8a72018-10-21 01:52:59 +0200175 } while (HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr) == 0);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100176 }
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}
187void *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 */
197void pool_flush(struct pool_head *pool)
198{
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200199 void **next, *temp;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100200 int removed = 0;
201
202 if (!pool)
203 return;
204 do {
205 next = pool->free_list;
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200206 } while (!HA_ATOMIC_CAS(&pool->free_list, &next, NULL));
Olivier Houchardcf975d42018-01-24 18:38:31 +0100207 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 */
225void 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 Tarreaue18db9e2018-10-16 10:28:54 +0200255
256/* frees an object to the local cache, possibly pushing oldest objects to the
257 * global pool. Must not be called directly.
258 */
259void __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 Tarreauf161d0f2018-02-22 14:05:55 +0100296#else /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau50e608d2007-05-13 18:26:08 +0200297
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100298/* 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 Tarreau50e608d2007-05-13 18:26:08 +0200305 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200306void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200307{
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100308 void *ptr = NULL;
309 int failed = 0;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200310
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100311 /* stop point */
312 avail += pool->used;
313
314 while (1) {
315 if (pool->limit && pool->allocated >= pool->limit)
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200316 return NULL;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100317
Willy Tarreauf13322e2017-11-22 10:50:54 +0100318 ptr = pool_alloc_area(pool->size + POOL_EXTRA);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100319 if (!ptr) {
Willy Tarreau58102cf2015-10-28 16:24:21 +0100320 pool->failed++;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100321 if (failed)
322 return NULL;
323 failed++;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100324 pool_gc(pool);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100325 continue;
326 }
327 if (++pool->allocated > avail)
328 break;
329
Willy Tarreauac421112015-10-28 15:09:29 +0100330 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100331 pool->free_list = ptr;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200332 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200333 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100334#ifdef DEBUG_MEMORY_POOLS
335 /* keep track of where the element was allocated from */
336 *POOL_LINK(pool, ptr) = (void *)pool;
337#endif
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100338 return ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200339}
Christopher Fauletb349e482017-08-29 09:52:38 +0200340void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
341{
342 void *ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200343
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200345 ptr = __pool_refill_alloc(pool, avail);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200347 return ptr;
348}
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200349/*
350 * This function frees whatever can be freed in pool <pool>.
351 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100352void pool_flush(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200353{
354 void *temp, *next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200355 if (!pool)
356 return;
357
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100358 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200359 next = pool->free_list;
360 while (next) {
361 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100362 next = *POOL_LINK(pool, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200363 pool->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100364 pool_free_area(temp, pool->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200365 }
366 pool->free_list = next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100367 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200368 /* 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 Tarreaub7f9d122009-04-21 02:17:45 +0200373 * the minimum thresholds imposed by owners. It takes care of avoiding
374 * recursion because it may be called from a signal handler.
Christopher Fauletb349e482017-08-29 09:52:38 +0200375 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100376 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200377 * 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 Tarreaue6ce59d2007-05-13 19:38:49 +0200379 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100380void pool_gc(struct pool_head *pool_ctx)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200381{
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200382 static int recurse;
Christopher Fauletb349e482017-08-29 09:52:38 +0200383 int cur_recurse = 0;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200384 struct pool_head *entry;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200385
Christopher Fauletb349e482017-08-29 09:52:38 +0200386 if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
387 return;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200388
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200389 list_for_each_entry(entry, &pools, list) {
390 void *temp, *next;
391 //qfprintf(stderr, "Flushing pool %s\n", entry->name);
Christopher Fauletb349e482017-08-29 09:52:38 +0200392 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100393 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200394 next = entry->free_list;
395 while (next &&
Willy Tarreau57767b82014-12-22 21:40:55 +0100396 (int)(entry->allocated - entry->used) > (int)entry->minavail) {
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200397 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100398 next = *POOL_LINK(entry, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200399 entry->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100400 pool_free_area(temp, entry->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200401 }
402 entry->free_list = next;
Christopher Fauletb349e482017-08-29 09:52:38 +0200403 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100404 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200405 }
Christopher Fauletb349e482017-08-29 09:52:38 +0200406
407 HA_ATOMIC_STORE(&recurse, 0);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200408}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100409#endif
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200410
411/*
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200412 * 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 Tarreaue6ce59d2007-05-13 19:38:49 +0200417 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100418void *pool_destroy(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200419{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200420 if (pool) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100421 pool_flush(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200422 if (pool->used)
423 return pool;
424 pool->users--;
425 if (!pool->users) {
426 LIST_DEL(&pool->list);
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100427#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_DESTROY(&pool->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100429#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +0200430 if ((pool - pool_base_start) < MAX_BASE_POOLS)
431 memset(pool, 0, sizeof(*pool));
432 else
433 free(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200434 }
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200435 }
436 return NULL;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200437}
438
Willy Tarreau12833bb2014-01-28 16:49:56 +0100439/* This function dumps memory usage information into the trash buffer. */
440void dump_pools_to_trash()
Willy Tarreau50e608d2007-05-13 18:26:08 +0200441{
442 struct pool_head *entry;
443 unsigned long allocated, used;
444 int nbpools;
445
446 allocated = used = nbpools = 0;
Willy Tarreau12833bb2014-01-28 16:49:56 +0100447 chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200448 list_for_each_entry(entry, &pools, list) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100449#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100450 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100451#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +0200452 chunk_appendf(&trash, " - Pool %s (%d bytes) : %d allocated (%u bytes), %d used, %d failures, %d users, @%p=%02d%s\n",
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200453 entry->name, entry->size, entry->allocated,
Willy Tarreau58102cf2015-10-28 16:24:21 +0100454 entry->size * entry->allocated, entry->used, entry->failed,
Willy Tarreau0a93b642018-10-16 07:58:39 +0200455 entry->users, entry, (int)pool_get_index(entry),
456 (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200457
458 allocated += entry->allocated * entry->size;
459 used += entry->used * entry->size;
460 nbpools++;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100461#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100462 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100463#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +0200464 }
Willy Tarreau12833bb2014-01-28 16:49:56 +0100465 chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n",
Willy Tarreau50e608d2007-05-13 18:26:08 +0200466 nbpools, allocated, used);
467}
468
Willy Tarreau12833bb2014-01-28 16:49:56 +0100469/* Dump statistics on pools usage. */
470void dump_pools(void)
471{
472 dump_pools_to_trash();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 qfprintf(stderr, "%s", trash.area);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100474}
475
Willy Tarreau58102cf2015-10-28 16:24:21 +0100476/* This function returns the total number of failed pool allocations */
477int 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) */
488unsigned 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) */
499unsigned 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 Lallemande7ed8852016-11-19 02:25:36 +0100509/* 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 */
513static int cli_io_handler_dump_pools(struct appctx *appctx)
514{
515 struct stream_interface *si = appctx->owner;
516
517 dump_pools_to_trash();
Willy Tarreau06d80a92017-10-19 14:32:15 +0200518 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100519 si_rx_room_blk(si);
William Lallemande7ed8852016-11-19 02:25:36 +0100520 return 0;
521 }
522 return 1;
523}
524
Willy Tarreau7107c8b2018-11-26 11:44:35 +0100525/* 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 */
529void 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 Lallemande7ed8852016-11-19 02:25:36 +0100540/* register cli keywords */
541static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaue9ecec82016-12-16 18:55:23 +0100542 { { "show", "pools", NULL }, "show pools : report information about the memory pools usage", NULL, cli_io_handler_dump_pools },
William Lallemande7ed8852016-11-19 02:25:36 +0100543 {{},}
544}};
545
Willy Tarreau0108d902018-11-25 19:14:37 +0100546INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemande7ed8852016-11-19 02:25:36 +0100547
Willy Tarreau50e608d2007-05-13 18:26:08 +0200548/*
549 * Local variables:
550 * c-indent-level: 8
551 * c-basic-offset: 8
552 * End:
553 */