blob: 1554243f08736cd30594f37cf66d64d43df50adc [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 */
12
William Lallemande7ed8852016-11-19 02:25:36 +010013#include <types/applet.h>
14#include <types/cli.h>
Willy Tarreau12833bb2014-01-28 16:49:56 +010015#include <types/global.h>
William Lallemande7ed8852016-11-19 02:25:36 +010016#include <types/stats.h>
17
Willy Tarreau50e608d2007-05-13 18:26:08 +020018#include <common/config.h>
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020019#include <common/debug.h>
Willy Tarreaue18db9e2018-10-16 10:28:54 +020020#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020022#include <common/memory.h>
23#include <common/mini-clist.h>
24#include <common/standard.h>
25
William Lallemande7ed8852016-11-19 02:25:36 +010026#include <proto/applet.h>
27#include <proto/cli.h>
28#include <proto/channel.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020029#include <proto/log.h>
William Lallemande7ed8852016-11-19 02:25:36 +010030#include <proto/stream_interface.h>
31#include <proto/stats.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020032
Willy Tarreau0a93b642018-10-16 07:58:39 +020033/* 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 */
36struct pool_head pool_base_start[MAX_BASE_POOLS] = { };
37unsigned int pool_base_count = 0;
38
Willy Tarreaue18db9e2018-10-16 10:28:54 +020039THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS] = { };
40THREAD_LOCAL struct list pool_lru_head = { }; /* oldest objects */
41THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */
42THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */
43
Willy Tarreau50e608d2007-05-13 18:26:08 +020044static struct list pools = LIST_HEAD_INIT(pools);
Willy Tarreau067ac9f2015-10-08 14:12:13 +020045int mem_poison_byte = -1;
Willy Tarreau50e608d2007-05-13 18:26:08 +020046
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 Tarreau581bf812016-01-25 02:19:13 +010049 * 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 Tarreau50e608d2007-05-13 18:26:08 +020052 */
53struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
54{
55 struct pool_head *pool;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020056 struct pool_head *entry;
57 struct list *start;
Willy Tarreau50e608d2007-05-13 18:26:08 +020058 unsigned int align;
59
Willy Tarreauac421112015-10-28 15:09:29 +010060 /* We need to store a (void *) at the end of the chunks. Since we know
Willy Tarreau50e608d2007-05-13 18:26:08 +020061 * 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 Tarreauac421112015-10-28 15:09:29 +010064 * This extra (void *) is not accounted for in the size computation
65 * so that the visible parts outside are not affected.
Willy Tarreau30f931e2018-10-23 14:40:23 +020066 *
67 * Note: for the LRU cache, we need to store 2 doubly-linked lists.
Willy Tarreau50e608d2007-05-13 18:26:08 +020068 */
69
Willy Tarreau581bf812016-01-25 02:19:13 +010070 if (!(flags & MEM_F_EXACT)) {
Willy Tarreau30f931e2018-10-23 14:40:23 +020071 align = 4 * sizeof(void *); // 2 lists = 4 pointers min
Willy Tarreau581bf812016-01-25 02:19:13 +010072 size = ((size + POOL_EXTRA + align - 1) & -align) - POOL_EXTRA;
73 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020074
Christopher Fauletb349e482017-08-29 09:52:38 +020075 /* TODO: thread: we do not lock pool list for now because all pools are
76 * created during HAProxy startup (so before threads creation) */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020077 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +020078 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020079
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 Tarreau50e608d2007-05-13 18:26:08 +020088 pool = entry;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020089 DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
Willy Tarreau50e608d2007-05-13 18:26:08 +020090 break;
91 }
92 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020093 else if (entry->size > size) {
94 /* insert before this one */
95 start = &entry->list;
96 break;
97 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020098 }
99
100 if (!pool) {
Willy Tarreau0a93b642018-10-16 07:58:39 +0200101 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 Tarreau50e608d2007-05-13 18:26:08 +0200117 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 Tarreau7dcd46d2007-05-14 00:16:13 +0200123 LIST_ADDQ(start, &pool->list);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200124 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200125 pool->users++;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100126#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_SPIN_INIT(&pool->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100128#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +0200129 return pool;
130}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100131
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100132#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +0100133/* 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 */
141void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
142{
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200143 void *ptr = NULL, **free_list;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100144 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 Houchard8b2c8a72018-10-21 01:52:59 +0200174 } while (HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr) == 0);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100175 }
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}
186void *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 */
196void pool_flush(struct pool_head *pool)
197{
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200198 void **next, *temp;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100199 int removed = 0;
200
201 if (!pool)
202 return;
203 do {
204 next = pool->free_list;
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200205 } while (!HA_ATOMIC_CAS(&pool->free_list, &next, NULL));
Olivier Houchardcf975d42018-01-24 18:38:31 +0100206 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 */
224void 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 Tarreaue18db9e2018-10-16 10:28:54 +0200254
255/* frees an object to the local cache, possibly pushing oldest objects to the
256 * global pool. Must not be called directly.
257 */
258void __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 Tarreauf161d0f2018-02-22 14:05:55 +0100295#else /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau50e608d2007-05-13 18:26:08 +0200296
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100297/* 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 Tarreau50e608d2007-05-13 18:26:08 +0200304 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200305void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200306{
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100307 void *ptr = NULL;
308 int failed = 0;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200309
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100310 /* stop point */
311 avail += pool->used;
312
313 while (1) {
314 if (pool->limit && pool->allocated >= pool->limit)
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200315 return NULL;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100316
Willy Tarreauf13322e2017-11-22 10:50:54 +0100317 ptr = pool_alloc_area(pool->size + POOL_EXTRA);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100318 if (!ptr) {
Willy Tarreau58102cf2015-10-28 16:24:21 +0100319 pool->failed++;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100320 if (failed)
321 return NULL;
322 failed++;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100323 pool_gc(pool);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100324 continue;
325 }
326 if (++pool->allocated > avail)
327 break;
328
Willy Tarreauac421112015-10-28 15:09:29 +0100329 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100330 pool->free_list = ptr;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200331 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200332 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100333#ifdef DEBUG_MEMORY_POOLS
334 /* keep track of where the element was allocated from */
335 *POOL_LINK(pool, ptr) = (void *)pool;
336#endif
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100337 return ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200338}
Christopher Fauletb349e482017-08-29 09:52:38 +0200339void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
340{
341 void *ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200342
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200344 ptr = __pool_refill_alloc(pool, avail);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200346 return ptr;
347}
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200348/*
349 * This function frees whatever can be freed in pool <pool>.
350 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100351void pool_flush(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200352{
353 void *temp, *next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200354 if (!pool)
355 return;
356
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100357 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200358 next = pool->free_list;
359 while (next) {
360 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100361 next = *POOL_LINK(pool, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200362 pool->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100363 pool_free_area(temp, pool->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200364 }
365 pool->free_list = next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100366 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200367 /* 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 Tarreaub7f9d122009-04-21 02:17:45 +0200372 * the minimum thresholds imposed by owners. It takes care of avoiding
373 * recursion because it may be called from a signal handler.
Christopher Fauletb349e482017-08-29 09:52:38 +0200374 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100375 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200376 * 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 Tarreaue6ce59d2007-05-13 19:38:49 +0200378 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100379void pool_gc(struct pool_head *pool_ctx)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200380{
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200381 static int recurse;
Christopher Fauletb349e482017-08-29 09:52:38 +0200382 int cur_recurse = 0;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200383 struct pool_head *entry;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200384
Christopher Fauletb349e482017-08-29 09:52:38 +0200385 if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
386 return;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200387
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200388 list_for_each_entry(entry, &pools, list) {
389 void *temp, *next;
390 //qfprintf(stderr, "Flushing pool %s\n", entry->name);
Christopher Fauletb349e482017-08-29 09:52:38 +0200391 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100392 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200393 next = entry->free_list;
394 while (next &&
Willy Tarreau57767b82014-12-22 21:40:55 +0100395 (int)(entry->allocated - entry->used) > (int)entry->minavail) {
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200396 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100397 next = *POOL_LINK(entry, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200398 entry->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100399 pool_free_area(temp, entry->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200400 }
401 entry->free_list = next;
Christopher Fauletb349e482017-08-29 09:52:38 +0200402 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100403 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200404 }
Christopher Fauletb349e482017-08-29 09:52:38 +0200405
406 HA_ATOMIC_STORE(&recurse, 0);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200407}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100408#endif
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200409
410/*
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200411 * 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 Tarreaue6ce59d2007-05-13 19:38:49 +0200416 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100417void *pool_destroy(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200418{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200419 if (pool) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100420 pool_flush(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200421 if (pool->used)
422 return pool;
423 pool->users--;
424 if (!pool->users) {
425 LIST_DEL(&pool->list);
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100426#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100427 HA_SPIN_DESTROY(&pool->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100428#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +0200429 if ((pool - pool_base_start) < MAX_BASE_POOLS)
430 memset(pool, 0, sizeof(*pool));
431 else
432 free(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200433 }
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200434 }
435 return NULL;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200436}
437
Willy Tarreau12833bb2014-01-28 16:49:56 +0100438/* This function dumps memory usage information into the trash buffer. */
439void dump_pools_to_trash()
Willy Tarreau50e608d2007-05-13 18:26:08 +0200440{
441 struct pool_head *entry;
442 unsigned long allocated, used;
443 int nbpools;
444
445 allocated = used = nbpools = 0;
Willy Tarreau12833bb2014-01-28 16:49:56 +0100446 chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200447 list_for_each_entry(entry, &pools, list) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100448#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100450#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +0200451 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 +0200452 entry->name, entry->size, entry->allocated,
Willy Tarreau58102cf2015-10-28 16:24:21 +0100453 entry->size * entry->allocated, entry->used, entry->failed,
Willy Tarreau0a93b642018-10-16 07:58:39 +0200454 entry->users, entry, (int)pool_get_index(entry),
455 (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200456
457 allocated += entry->allocated * entry->size;
458 used += entry->used * entry->size;
459 nbpools++;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100460#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100462#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +0200463 }
Willy Tarreau12833bb2014-01-28 16:49:56 +0100464 chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n",
Willy Tarreau50e608d2007-05-13 18:26:08 +0200465 nbpools, allocated, used);
466}
467
Willy Tarreau12833bb2014-01-28 16:49:56 +0100468/* Dump statistics on pools usage. */
469void dump_pools(void)
470{
471 dump_pools_to_trash();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200472 qfprintf(stderr, "%s", trash.area);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100473}
474
Willy Tarreau58102cf2015-10-28 16:24:21 +0100475/* This function returns the total number of failed pool allocations */
476int 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) */
487unsigned 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) */
498unsigned 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 Lallemande7ed8852016-11-19 02:25:36 +0100508/* 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 */
512static int cli_io_handler_dump_pools(struct appctx *appctx)
513{
514 struct stream_interface *si = appctx->owner;
515
516 dump_pools_to_trash();
Willy Tarreau06d80a92017-10-19 14:32:15 +0200517 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100518 si_rx_room_blk(si);
William Lallemande7ed8852016-11-19 02:25:36 +0100519 return 0;
520 }
521 return 1;
522}
523
William Lallemande7ed8852016-11-19 02:25:36 +0100524/* register cli keywords */
525static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaue9ecec82016-12-16 18:55:23 +0100526 { { "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 +0100527 {{},}
528}};
529
Willy Tarreau0108d902018-11-25 19:14:37 +0100530INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemande7ed8852016-11-19 02:25:36 +0100531
Willy Tarreau50e608d2007-05-13 18:26:08 +0200532/*
533 * Local variables:
534 * c-indent-level: 8
535 * c-basic-offset: 8
536 * End:
537 */