blob: fc1f62483d5f464fc7b886b802fc416813faae56 [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 Tarreau50e608d2007-05-13 18:26:08 +020020#include <common/memory.h>
21#include <common/mini-clist.h>
22#include <common/standard.h>
23
William Lallemande7ed8852016-11-19 02:25:36 +010024#include <proto/applet.h>
25#include <proto/cli.h>
26#include <proto/channel.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020027#include <proto/log.h>
William Lallemande7ed8852016-11-19 02:25:36 +010028#include <proto/stream_interface.h>
29#include <proto/stats.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020030
31static struct list pools = LIST_HEAD_INIT(pools);
Willy Tarreau067ac9f2015-10-08 14:12:13 +020032int mem_poison_byte = -1;
Willy Tarreau50e608d2007-05-13 18:26:08 +020033
34/* Try to find an existing shared pool with the same characteristics and
35 * returns it, otherwise creates this one. NULL is returned if no memory
Willy Tarreau581bf812016-01-25 02:19:13 +010036 * is available for a new creation. Two flags are supported :
37 * - MEM_F_SHARED to indicate that the pool may be shared with other users
38 * - MEM_F_EXACT to indicate that the size must not be rounded up
Willy Tarreau50e608d2007-05-13 18:26:08 +020039 */
40struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
41{
42 struct pool_head *pool;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020043 struct pool_head *entry;
44 struct list *start;
Willy Tarreau50e608d2007-05-13 18:26:08 +020045 unsigned int align;
46
Willy Tarreauac421112015-10-28 15:09:29 +010047 /* We need to store a (void *) at the end of the chunks. Since we know
Willy Tarreau50e608d2007-05-13 18:26:08 +020048 * that the malloc() function will never return such a small size,
49 * let's round the size up to something slightly bigger, in order to
50 * ease merging of entries. Note that the rounding is a power of two.
Willy Tarreauac421112015-10-28 15:09:29 +010051 * This extra (void *) is not accounted for in the size computation
52 * so that the visible parts outside are not affected.
Willy Tarreau50e608d2007-05-13 18:26:08 +020053 */
54
Willy Tarreau581bf812016-01-25 02:19:13 +010055 if (!(flags & MEM_F_EXACT)) {
56 align = 16;
57 size = ((size + POOL_EXTRA + align - 1) & -align) - POOL_EXTRA;
58 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020059
Christopher Fauletb349e482017-08-29 09:52:38 +020060 /* TODO: thread: we do not lock pool list for now because all pools are
61 * created during HAProxy startup (so before threads creation) */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020062 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +020063 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020064
65 list_for_each_entry(entry, &pools, list) {
66 if (entry->size == size) {
67 /* either we can share this place and we take it, or
68 * we look for a sharable one or for the next position
69 * before which we will insert a new one.
70 */
71 if (flags & entry->flags & MEM_F_SHARED) {
72 /* we can share this one */
Willy Tarreau50e608d2007-05-13 18:26:08 +020073 pool = entry;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020074 DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
Willy Tarreau50e608d2007-05-13 18:26:08 +020075 break;
76 }
77 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020078 else if (entry->size > size) {
79 /* insert before this one */
80 start = &entry->list;
81 break;
82 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020083 }
84
85 if (!pool) {
David Carlierb781dbe2017-07-21 08:44:40 +010086 pool = calloc(1, sizeof(*pool));
Willy Tarreau50e608d2007-05-13 18:26:08 +020087 if (!pool)
88 return NULL;
89 if (name)
90 strlcpy2(pool->name, name, sizeof(pool->name));
91 pool->size = size;
92 pool->flags = flags;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020093 LIST_ADDQ(start, &pool->list);
Willy Tarreau50e608d2007-05-13 18:26:08 +020094 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020095 pool->users++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +010096 HA_SPIN_INIT(&pool->lock);
Willy Tarreau50e608d2007-05-13 18:26:08 +020097 return pool;
98}
99
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100100/* Allocates new entries for pool <pool> until there are at least <avail> + 1
101 * available, then returns the last one for immediate use, so that at least
102 * <avail> are left available in the pool upon return. NULL is returned if the
103 * last entry could not be allocated. It's important to note that at least one
104 * allocation is always performed even if there are enough entries in the pool.
105 * A call to the garbage collector is performed at most once in case malloc()
106 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200107 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200108void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200109{
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100110 void *ptr = NULL;
111 int failed = 0;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200112
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100113 /* stop point */
114 avail += pool->used;
115
116 while (1) {
117 if (pool->limit && pool->allocated >= pool->limit)
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200118 return NULL;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100119
Willy Tarreauf13322e2017-11-22 10:50:54 +0100120 ptr = pool_alloc_area(pool->size + POOL_EXTRA);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100121 if (!ptr) {
Willy Tarreau58102cf2015-10-28 16:24:21 +0100122 pool->failed++;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100123 if (failed)
124 return NULL;
125 failed++;
Christopher Fauletb349e482017-08-29 09:52:38 +0200126 pool_gc2(pool);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100127 continue;
128 }
129 if (++pool->allocated > avail)
130 break;
131
Willy Tarreauac421112015-10-28 15:09:29 +0100132 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100133 pool->free_list = ptr;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200134 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200135 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100136#ifdef DEBUG_MEMORY_POOLS
137 /* keep track of where the element was allocated from */
138 *POOL_LINK(pool, ptr) = (void *)pool;
139#endif
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100140 return ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200141}
Christopher Fauletb349e482017-08-29 09:52:38 +0200142void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
143{
144 void *ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200145
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100146 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200147 ptr = __pool_refill_alloc(pool, avail);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200149 return ptr;
150}
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200151/*
152 * This function frees whatever can be freed in pool <pool>.
153 */
154void pool_flush2(struct pool_head *pool)
155{
156 void *temp, *next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200157 if (!pool)
158 return;
159
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100160 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200161 next = pool->free_list;
162 while (next) {
163 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100164 next = *POOL_LINK(pool, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200165 pool->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100166 pool_free_area(temp, pool->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200167 }
168 pool->free_list = next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100169 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200170 /* here, we should have pool->allocate == pool->used */
171}
172
173/*
174 * This function frees whatever can be freed in all pools, but respecting
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200175 * the minimum thresholds imposed by owners. It takes care of avoiding
176 * recursion because it may be called from a signal handler.
Christopher Fauletb349e482017-08-29 09:52:38 +0200177 *
178 * <pool_ctx> is used when pool_gc2 is called to release resources to allocate
179 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
180 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200181 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200182void pool_gc2(struct pool_head *pool_ctx)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200183{
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200184 static int recurse;
Christopher Fauletb349e482017-08-29 09:52:38 +0200185 int cur_recurse = 0;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200186 struct pool_head *entry;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200187
Christopher Fauletb349e482017-08-29 09:52:38 +0200188 if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
189 return;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200190
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200191 list_for_each_entry(entry, &pools, list) {
192 void *temp, *next;
193 //qfprintf(stderr, "Flushing pool %s\n", entry->name);
Christopher Fauletb349e482017-08-29 09:52:38 +0200194 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100195 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200196 next = entry->free_list;
197 while (next &&
Willy Tarreau57767b82014-12-22 21:40:55 +0100198 (int)(entry->allocated - entry->used) > (int)entry->minavail) {
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200199 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100200 next = *POOL_LINK(entry, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200201 entry->allocated--;
Willy Tarreauf13322e2017-11-22 10:50:54 +0100202 pool_free_area(temp, entry->size + POOL_EXTRA);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200203 }
204 entry->free_list = next;
Christopher Fauletb349e482017-08-29 09:52:38 +0200205 if (entry != pool_ctx)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100206 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200207 }
Christopher Fauletb349e482017-08-29 09:52:38 +0200208
209 HA_ATOMIC_STORE(&recurse, 0);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200210}
211
212/*
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200213 * This function destroys a pool by freeing it completely, unless it's still
214 * in use. This should be called only under extreme circumstances. It always
215 * returns NULL if the resulting pool is empty, easing the clearing of the old
216 * pointer, otherwise it returns the pool.
217 * .
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200218 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200219void *pool_destroy2(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200220{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200221 if (pool) {
222 pool_flush2(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200223 if (pool->used)
224 return pool;
225 pool->users--;
226 if (!pool->users) {
227 LIST_DEL(&pool->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100228 HA_SPIN_DESTROY(&pool->lock);
David Carlierb781dbe2017-07-21 08:44:40 +0100229 free(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200230 }
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200231 }
232 return NULL;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200233}
234
Willy Tarreau12833bb2014-01-28 16:49:56 +0100235/* This function dumps memory usage information into the trash buffer. */
236void dump_pools_to_trash()
Willy Tarreau50e608d2007-05-13 18:26:08 +0200237{
238 struct pool_head *entry;
239 unsigned long allocated, used;
240 int nbpools;
241
242 allocated = used = nbpools = 0;
Willy Tarreau12833bb2014-01-28 16:49:56 +0100243 chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200244 list_for_each_entry(entry, &pools, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100245 HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
Willy Tarreau58102cf2015-10-28 16:24:21 +0100246 chunk_appendf(&trash, " - Pool %s (%d bytes) : %d allocated (%u bytes), %d used, %d failures, %d users%s\n",
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200247 entry->name, entry->size, entry->allocated,
Willy Tarreau58102cf2015-10-28 16:24:21 +0100248 entry->size * entry->allocated, entry->used, entry->failed,
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200249 entry->users, (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200250
251 allocated += entry->allocated * entry->size;
252 used += entry->used * entry->size;
253 nbpools++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100254 HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200255 }
Willy Tarreau12833bb2014-01-28 16:49:56 +0100256 chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n",
Willy Tarreau50e608d2007-05-13 18:26:08 +0200257 nbpools, allocated, used);
258}
259
Willy Tarreau12833bb2014-01-28 16:49:56 +0100260/* Dump statistics on pools usage. */
261void dump_pools(void)
262{
263 dump_pools_to_trash();
264 qfprintf(stderr, "%s", trash.str);
265}
266
Willy Tarreau58102cf2015-10-28 16:24:21 +0100267/* This function returns the total number of failed pool allocations */
268int pool_total_failures()
269{
270 struct pool_head *entry;
271 int failed = 0;
272
273 list_for_each_entry(entry, &pools, list)
274 failed += entry->failed;
275 return failed;
276}
277
278/* This function returns the total amount of memory allocated in pools (in bytes) */
279unsigned long pool_total_allocated()
280{
281 struct pool_head *entry;
282 unsigned long allocated = 0;
283
284 list_for_each_entry(entry, &pools, list)
285 allocated += entry->allocated * entry->size;
286 return allocated;
287}
288
289/* This function returns the total amount of memory used in pools (in bytes) */
290unsigned long pool_total_used()
291{
292 struct pool_head *entry;
293 unsigned long used = 0;
294
295 list_for_each_entry(entry, &pools, list)
296 used += entry->used * entry->size;
297 return used;
298}
299
William Lallemande7ed8852016-11-19 02:25:36 +0100300/* This function dumps memory usage information onto the stream interface's
301 * read buffer. It returns 0 as long as it does not complete, non-zero upon
302 * completion. No state is used.
303 */
304static int cli_io_handler_dump_pools(struct appctx *appctx)
305{
306 struct stream_interface *si = appctx->owner;
307
308 dump_pools_to_trash();
Willy Tarreau06d80a92017-10-19 14:32:15 +0200309 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemande7ed8852016-11-19 02:25:36 +0100310 si_applet_cant_put(si);
311 return 0;
312 }
313 return 1;
314}
315
William Lallemande7ed8852016-11-19 02:25:36 +0100316/* register cli keywords */
317static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaue9ecec82016-12-16 18:55:23 +0100318 { { "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 +0100319 {{},}
320}};
321
322__attribute__((constructor))
323static void __memory_init(void)
324{
325 cli_register_kw(&cli_kws);
326}
327
Willy Tarreau50e608d2007-05-13 18:26:08 +0200328/*
329 * Local variables:
330 * c-indent-level: 8
331 * c-basic-offset: 8
332 * End:
333 */