blob: ced9af5506f0330148218612dcc81f263f52885a [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 Tarreau50e608d2007-05-13 18:26:08 +020021#include <common/memory.h>
22#include <common/mini-clist.h>
23#include <common/standard.h>
24
William Lallemande7ed8852016-11-19 02:25:36 +010025#include <proto/applet.h>
26#include <proto/cli.h>
27#include <proto/channel.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020028#include <proto/log.h>
William Lallemande7ed8852016-11-19 02:25:36 +010029#include <proto/stream_interface.h>
30#include <proto/stats.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020031
Willy Tarreau0a93b642018-10-16 07:58:39 +020032/* These are the most common pools, expected to be initialized first. These
33 * ones are allocated from an array, allowing to map them to an index.
34 */
35struct pool_head pool_base_start[MAX_BASE_POOLS] = { };
36unsigned int pool_base_count = 0;
37
Willy Tarreaue18db9e2018-10-16 10:28:54 +020038THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS] = { };
39THREAD_LOCAL struct list pool_lru_head = { }; /* oldest objects */
40THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */
41THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */
42
Willy Tarreau50e608d2007-05-13 18:26:08 +020043static struct list pools = LIST_HEAD_INIT(pools);
Willy Tarreau067ac9f2015-10-08 14:12:13 +020044int mem_poison_byte = -1;
Willy Tarreau50e608d2007-05-13 18:26:08 +020045
46/* Try to find an existing shared pool with the same characteristics and
47 * returns it, otherwise creates this one. NULL is returned if no memory
Willy Tarreau581bf812016-01-25 02:19:13 +010048 * is available for a new creation. Two flags are supported :
49 * - MEM_F_SHARED to indicate that the pool may be shared with other users
50 * - MEM_F_EXACT to indicate that the size must not be rounded up
Willy Tarreau50e608d2007-05-13 18:26:08 +020051 */
52struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
53{
54 struct pool_head *pool;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020055 struct pool_head *entry;
56 struct list *start;
Willy Tarreau50e608d2007-05-13 18:26:08 +020057 unsigned int align;
58
Willy Tarreauac421112015-10-28 15:09:29 +010059 /* We need to store a (void *) at the end of the chunks. Since we know
Willy Tarreau50e608d2007-05-13 18:26:08 +020060 * that the malloc() function will never return such a small size,
61 * let's round the size up to something slightly bigger, in order to
62 * ease merging of entries. Note that the rounding is a power of two.
Willy Tarreauac421112015-10-28 15:09:29 +010063 * This extra (void *) is not accounted for in the size computation
64 * so that the visible parts outside are not affected.
Willy Tarreau30f931e2018-10-23 14:40:23 +020065 *
66 * Note: for the LRU cache, we need to store 2 doubly-linked lists.
Willy Tarreau50e608d2007-05-13 18:26:08 +020067 */
68
Willy Tarreau581bf812016-01-25 02:19:13 +010069 if (!(flags & MEM_F_EXACT)) {
Willy Tarreau30f931e2018-10-23 14:40:23 +020070 align = 4 * sizeof(void *); // 2 lists = 4 pointers min
Willy Tarreau581bf812016-01-25 02:19:13 +010071 size = ((size + POOL_EXTRA + align - 1) & -align) - POOL_EXTRA;
72 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020073
Christopher Fauletb349e482017-08-29 09:52:38 +020074 /* TODO: thread: we do not lock pool list for now because all pools are
75 * created during HAProxy startup (so before threads creation) */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020076 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +020077 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020078
79 list_for_each_entry(entry, &pools, list) {
80 if (entry->size == size) {
81 /* either we can share this place and we take it, or
82 * we look for a sharable one or for the next position
83 * before which we will insert a new one.
84 */
85 if (flags & entry->flags & MEM_F_SHARED) {
86 /* we can share this one */
Willy Tarreau50e608d2007-05-13 18:26:08 +020087 pool = entry;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020088 DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
Willy Tarreau50e608d2007-05-13 18:26:08 +020089 break;
90 }
91 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020092 else if (entry->size > size) {
93 /* insert before this one */
94 start = &entry->list;
95 break;
96 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020097 }
98
99 if (!pool) {
Willy Tarreau0a93b642018-10-16 07:58:39 +0200100 if (pool_base_count < MAX_BASE_POOLS)
101 pool = &pool_base_start[pool_base_count++];
102
103 if (!pool) {
104 /* look for a freed entry */
105 for (entry = pool_base_start; entry != pool_base_start + MAX_BASE_POOLS; entry++) {
106 if (!entry->size) {
107 pool = entry;
108 break;
109 }
110 }
111 }
112
113 if (!pool)
114 pool = calloc(1, sizeof(*pool));
115
Willy Tarreau50e608d2007-05-13 18:26:08 +0200116 if (!pool)
117 return NULL;
118 if (name)
119 strlcpy2(pool->name, name, sizeof(pool->name));
120 pool->size = size;
121 pool->flags = flags;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200122 LIST_ADDQ(start, &pool->list);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200123 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200124 pool->users++;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100125#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100126 HA_SPIN_INIT(&pool->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100127#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +0200128 return pool;
129}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100130
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100131#ifdef CONFIG_HAP_LOCKLESS_POOLS
Olivier Houchardcf975d42018-01-24 18:38:31 +0100132/* Allocates new entries for pool <pool> until there are at least <avail> + 1
133 * available, then returns the last one for immediate use, so that at least
134 * <avail> are left available in the pool upon return. NULL is returned if the
135 * last entry could not be allocated. It's important to note that at least one
136 * allocation is always performed even if there are enough entries in the pool.
137 * A call to the garbage collector is performed at most once in case malloc()
138 * returns an error, before returning NULL.
139 */
140void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
141{
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200142 void *ptr = NULL, **free_list;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100143 int failed = 0;
144 int size = pool->size;
145 int limit = pool->limit;
146 int allocated = pool->allocated, allocated_orig = allocated;
147
148 /* stop point */
149 avail += pool->used;
150
151 while (1) {
152 if (limit && allocated >= limit) {
153 HA_ATOMIC_ADD(&pool->allocated, allocated - allocated_orig);
154 return NULL;
155 }
156
157 ptr = malloc(size + POOL_EXTRA);
158 if (!ptr) {
159 HA_ATOMIC_ADD(&pool->failed, 1);
160 if (failed)
161 return NULL;
162 failed++;
163 pool_gc(pool);
164 continue;
165 }
166 if (++allocated > avail)
167 break;
168
169 free_list = pool->free_list;
170 do {
171 *POOL_LINK(pool, ptr) = free_list;
172 __ha_barrier_store();
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200173 } while (HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr) == 0);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100174 }
175
176 HA_ATOMIC_ADD(&pool->allocated, allocated - allocated_orig);
177 HA_ATOMIC_ADD(&pool->used, 1);
178
179#ifdef DEBUG_MEMORY_POOLS
180 /* keep track of where the element was allocated from */
181 *POOL_LINK(pool, ptr) = (void *)pool;
182#endif
183 return ptr;
184}
185void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
186{
187 void *ptr;
188
189 ptr = __pool_refill_alloc(pool, avail);
190 return ptr;
191}
192/*
193 * This function frees whatever can be freed in pool <pool>.
194 */
195void pool_flush(struct pool_head *pool)
196{
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200197 void **next, *temp;
Olivier Houchardcf975d42018-01-24 18:38:31 +0100198 int removed = 0;
199
200 if (!pool)
201 return;
202 do {
203 next = pool->free_list;
Olivier Houchard8b2c8a72018-10-21 01:52:59 +0200204 } while (!HA_ATOMIC_CAS(&pool->free_list, &next, NULL));
Olivier Houchardcf975d42018-01-24 18:38:31 +0100205 while (next) {
206 temp = next;
207 next = *POOL_LINK(pool, temp);
208 removed++;
209 free(temp);
210 }
211 pool->free_list = next;
212 HA_ATOMIC_SUB(&pool->allocated, removed);
213 /* here, we should have pool->allocate == pool->used */
214}
215
216/*
217 * This function frees whatever can be freed in all pools, but respecting
218 * the minimum thresholds imposed by owners. It takes care of avoiding
219 * recursion because it may be called from a signal handler.
220 *
221 * <pool_ctx> is unused
222 */
223void pool_gc(struct pool_head *pool_ctx)
224{
225 static int recurse;
226 int cur_recurse = 0;
227 struct pool_head *entry;
228
229 if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
230 return;
231
232 list_for_each_entry(entry, &pools, list) {
233 while ((int)((volatile int)entry->allocated - (volatile int)entry->used) > (int)entry->minavail) {
234 struct pool_free_list cmp, new;
235
236 cmp.seq = entry->seq;
237 __ha_barrier_load();
238 cmp.free_list = entry->free_list;
239 __ha_barrier_load();
240 if (cmp.free_list == NULL)
241 break;
242 new.free_list = *POOL_LINK(entry, cmp.free_list);
243 new.seq = cmp.seq + 1;
244 if (__ha_cas_dw(&entry->free_list, &cmp, &new) == 0)
245 continue;
246 free(cmp.free_list);
247 HA_ATOMIC_SUB(&entry->allocated, 1);
248 }
249 }
250
251 HA_ATOMIC_STORE(&recurse, 0);
252}
Willy Tarreaue18db9e2018-10-16 10:28:54 +0200253
254/* frees an object to the local cache, possibly pushing oldest objects to the
255 * global pool. Must not be called directly.
256 */
257void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx)
258{
259 struct pool_cache_item *item = (struct pool_cache_item *)ptr;
260 struct pool_cache_head *ph = &pool_cache[idx];
261
262 /* never allocated or empty */
263 if (unlikely(ph->list.n == NULL)) {
264 LIST_INIT(&ph->list);
265 ph->size = pool->size;
266 if (pool_lru_head.n == NULL)
267 LIST_INIT(&pool_lru_head);
268 }
269
270 LIST_ADD(&ph->list, &item->by_pool);
271 LIST_ADD(&pool_lru_head, &item->by_lru);
272 ph->count++;
273 pool_cache_count++;
274 pool_cache_bytes += ph->size;
275
276 if (pool_cache_bytes <= CONFIG_HAP_POOL_CACHE_SIZE)
277 return;
278
279 do {
280 item = LIST_PREV(&pool_lru_head, struct pool_cache_item *, by_lru);
281 /* note: by definition we remove oldest objects so they also are the
282 * oldest in their own pools, thus their next is the pool's head.
283 */
284 ph = LIST_NEXT(&item->by_pool, struct pool_cache_head *, list);
285 LIST_DEL(&item->by_pool);
286 LIST_DEL(&item->by_lru);
287 ph->count--;
288 pool_cache_count--;
289 pool_cache_bytes -= ph->size;
290 __pool_free(pool_base_start + (ph - pool_cache), item);
291 } while (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 7 / 8);
292}
293
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100294#else /* CONFIG_HAP_LOCKLESS_POOLS */
Willy Tarreau50e608d2007-05-13 18:26:08 +0200295
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100296/* Allocates new entries for pool <pool> until there are at least <avail> + 1
297 * available, then returns the last one for immediate use, so that at least
298 * <avail> are left available in the pool upon return. NULL is returned if the
299 * last entry could not be allocated. It's important to note that at least one
300 * allocation is always performed even if there are enough entries in the pool.
301 * A call to the garbage collector is performed at most once in case malloc()
302 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200303 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200304void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200305{
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100306 void *ptr = NULL;
307 int failed = 0;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200308
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100309 /* stop point */
310 avail += pool->used;
311
312 while (1) {
313 if (pool->limit && pool->allocated >= pool->limit)
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200314 return NULL;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100315
Willy Tarreauf13322e2017-11-22 10:50:54 +0100316 ptr = pool_alloc_area(pool->size + POOL_EXTRA);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100317 if (!ptr) {
Willy Tarreau58102cf2015-10-28 16:24:21 +0100318 pool->failed++;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100319 if (failed)
320 return NULL;
321 failed++;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100322 pool_gc(pool);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100323 continue;
324 }
325 if (++pool->allocated > avail)
326 break;
327
Willy Tarreauac421112015-10-28 15:09:29 +0100328 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100329 pool->free_list = ptr;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200330 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200331 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100332#ifdef DEBUG_MEMORY_POOLS
333 /* keep track of where the element was allocated from */
334 *POOL_LINK(pool, ptr) = (void *)pool;
335#endif
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100336 return ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200337}
Christopher Fauletb349e482017-08-29 09:52:38 +0200338void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
339{
340 void *ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200341
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100342 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200343 ptr = __pool_refill_alloc(pool, avail);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200345 return ptr;
346}
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200347/*
348 * This function frees whatever can be freed in pool <pool>.
349 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100350void pool_flush(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200351{
352 void *temp, *next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200353 if (!pool)
354 return;
355
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100356 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200357 next = pool->free_list;
358 while (next) {
359 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100360 next = *POOL_LINK(pool, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200361 pool->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100362 pool_free_area(temp, pool->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200363 }
364 pool->free_list = next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100365 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200366 /* here, we should have pool->allocate == pool->used */
367}
368
369/*
370 * This function frees whatever can be freed in all pools, but respecting
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200371 * the minimum thresholds imposed by owners. It takes care of avoiding
372 * recursion because it may be called from a signal handler.
Christopher Fauletb349e482017-08-29 09:52:38 +0200373 *
Willy Tarreaubafbe012017-11-24 17:34:44 +0100374 * <pool_ctx> is used when pool_gc is called to release resources to allocate
Christopher Fauletb349e482017-08-29 09:52:38 +0200375 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
376 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200377 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100378void pool_gc(struct pool_head *pool_ctx)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200379{
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200380 static int recurse;
Christopher Fauletb349e482017-08-29 09:52:38 +0200381 int cur_recurse = 0;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200382 struct pool_head *entry;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200383
Christopher Fauletb349e482017-08-29 09:52:38 +0200384 if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
385 return;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200386
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200387 list_for_each_entry(entry, &pools, list) {
388 void *temp, *next;
389 //qfprintf(stderr, "Flushing pool %s\n", entry->name);
Christopher Fauletb349e482017-08-29 09:52:38 +0200390 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100391 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200392 next = entry->free_list;
393 while (next &&
Willy Tarreau57767b82014-12-22 21:40:55 +0100394 (int)(entry->allocated - entry->used) > (int)entry->minavail) {
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200395 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100396 next = *POOL_LINK(entry, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200397 entry->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100398 pool_free_area(temp, entry->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200399 }
400 entry->free_list = next;
Christopher Fauletb349e482017-08-29 09:52:38 +0200401 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100402 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200403 }
Christopher Fauletb349e482017-08-29 09:52:38 +0200404
405 HA_ATOMIC_STORE(&recurse, 0);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200406}
Olivier Houchardcf975d42018-01-24 18:38:31 +0100407#endif
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200408
409/*
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200410 * This function destroys a pool by freeing it completely, unless it's still
411 * in use. This should be called only under extreme circumstances. It always
412 * returns NULL if the resulting pool is empty, easing the clearing of the old
413 * pointer, otherwise it returns the pool.
414 * .
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200415 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100416void *pool_destroy(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200417{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200418 if (pool) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100419 pool_flush(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200420 if (pool->used)
421 return pool;
422 pool->users--;
423 if (!pool->users) {
424 LIST_DEL(&pool->list);
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100425#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100426 HA_SPIN_DESTROY(&pool->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100427#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +0200428 if ((pool - pool_base_start) < MAX_BASE_POOLS)
429 memset(pool, 0, sizeof(*pool));
430 else
431 free(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200432 }
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200433 }
434 return NULL;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200435}
436
Willy Tarreau12833bb2014-01-28 16:49:56 +0100437/* This function dumps memory usage information into the trash buffer. */
438void dump_pools_to_trash()
Willy Tarreau50e608d2007-05-13 18:26:08 +0200439{
440 struct pool_head *entry;
441 unsigned long allocated, used;
442 int nbpools;
443
444 allocated = used = nbpools = 0;
Willy Tarreau12833bb2014-01-28 16:49:56 +0100445 chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200446 list_for_each_entry(entry, &pools, list) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100447#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100448 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100449#endif
Willy Tarreau0a93b642018-10-16 07:58:39 +0200450 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 +0200451 entry->name, entry->size, entry->allocated,
Willy Tarreau58102cf2015-10-28 16:24:21 +0100452 entry->size * entry->allocated, entry->used, entry->failed,
Willy Tarreau0a93b642018-10-16 07:58:39 +0200453 entry->users, entry, (int)pool_get_index(entry),
454 (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200455
456 allocated += entry->allocated * entry->size;
457 used += entry->used * entry->size;
458 nbpools++;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100459#ifndef CONFIG_HAP_LOCKLESS_POOLS
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100460 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100461#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +0200462 }
Willy Tarreau12833bb2014-01-28 16:49:56 +0100463 chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n",
Willy Tarreau50e608d2007-05-13 18:26:08 +0200464 nbpools, allocated, used);
465}
466
Willy Tarreau12833bb2014-01-28 16:49:56 +0100467/* Dump statistics on pools usage. */
468void dump_pools(void)
469{
470 dump_pools_to_trash();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 qfprintf(stderr, "%s", trash.area);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100472}
473
Willy Tarreau58102cf2015-10-28 16:24:21 +0100474/* This function returns the total number of failed pool allocations */
475int pool_total_failures()
476{
477 struct pool_head *entry;
478 int failed = 0;
479
480 list_for_each_entry(entry, &pools, list)
481 failed += entry->failed;
482 return failed;
483}
484
485/* This function returns the total amount of memory allocated in pools (in bytes) */
486unsigned long pool_total_allocated()
487{
488 struct pool_head *entry;
489 unsigned long allocated = 0;
490
491 list_for_each_entry(entry, &pools, list)
492 allocated += entry->allocated * entry->size;
493 return allocated;
494}
495
496/* This function returns the total amount of memory used in pools (in bytes) */
497unsigned long pool_total_used()
498{
499 struct pool_head *entry;
500 unsigned long used = 0;
501
502 list_for_each_entry(entry, &pools, list)
503 used += entry->used * entry->size;
504 return used;
505}
506
William Lallemande7ed8852016-11-19 02:25:36 +0100507/* This function dumps memory usage information onto the stream interface's
508 * read buffer. It returns 0 as long as it does not complete, non-zero upon
509 * completion. No state is used.
510 */
511static int cli_io_handler_dump_pools(struct appctx *appctx)
512{
513 struct stream_interface *si = appctx->owner;
514
515 dump_pools_to_trash();
Willy Tarreau06d80a92017-10-19 14:32:15 +0200516 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100517 si_cant_put(si);
William Lallemande7ed8852016-11-19 02:25:36 +0100518 return 0;
519 }
520 return 1;
521}
522
William Lallemande7ed8852016-11-19 02:25:36 +0100523/* register cli keywords */
524static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaue9ecec82016-12-16 18:55:23 +0100525 { { "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 +0100526 {{},}
527}};
528
529__attribute__((constructor))
530static void __memory_init(void)
531{
532 cli_register_kw(&cli_kws);
533}
534
Willy Tarreau50e608d2007-05-13 18:26:08 +0200535/*
536 * Local variables:
537 * c-indent-level: 8
538 * c-basic-offset: 8
539 * End:
540 */