blob: 9f8396d5b554b30b9f1a054f3a3c0cfd0a960868 [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
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020060 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +020061 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020062
63 list_for_each_entry(entry, &pools, list) {
64 if (entry->size == size) {
65 /* either we can share this place and we take it, or
66 * we look for a sharable one or for the next position
67 * before which we will insert a new one.
68 */
69 if (flags & entry->flags & MEM_F_SHARED) {
70 /* we can share this one */
Willy Tarreau50e608d2007-05-13 18:26:08 +020071 pool = entry;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020072 DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
Willy Tarreau50e608d2007-05-13 18:26:08 +020073 break;
74 }
75 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020076 else if (entry->size > size) {
77 /* insert before this one */
78 start = &entry->list;
79 break;
80 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020081 }
82
83 if (!pool) {
David Carlierb781dbe2017-07-21 08:44:40 +010084 pool = calloc(1, sizeof(*pool));
Willy Tarreau50e608d2007-05-13 18:26:08 +020085 if (!pool)
86 return NULL;
87 if (name)
88 strlcpy2(pool->name, name, sizeof(pool->name));
89 pool->size = size;
90 pool->flags = flags;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020091 LIST_ADDQ(start, &pool->list);
Willy Tarreau50e608d2007-05-13 18:26:08 +020092 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020093 pool->users++;
Willy Tarreau50e608d2007-05-13 18:26:08 +020094 return pool;
95}
96
Willy Tarreaua885f6d2014-12-03 15:25:28 +010097/* Allocates new entries for pool <pool> until there are at least <avail> + 1
98 * available, then returns the last one for immediate use, so that at least
99 * <avail> are left available in the pool upon return. NULL is returned if the
100 * last entry could not be allocated. It's important to note that at least one
101 * allocation is always performed even if there are enough entries in the pool.
102 * A call to the garbage collector is performed at most once in case malloc()
103 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200104 */
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100105void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
Willy Tarreau50e608d2007-05-13 18:26:08 +0200106{
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100107 void *ptr = NULL;
108 int failed = 0;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200109
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100110 /* stop point */
111 avail += pool->used;
112
113 while (1) {
114 if (pool->limit && pool->allocated >= pool->limit)
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200115 return NULL;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100116
David Carlierb781dbe2017-07-21 08:44:40 +0100117 ptr = malloc(pool->size + POOL_EXTRA);
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100118 if (!ptr) {
Willy Tarreau58102cf2015-10-28 16:24:21 +0100119 pool->failed++;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100120 if (failed)
121 return NULL;
122 failed++;
123 pool_gc2();
124 continue;
125 }
126 if (++pool->allocated > avail)
127 break;
128
Willy Tarreauac421112015-10-28 15:09:29 +0100129 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100130 pool->free_list = ptr;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200131 }
Willy Tarreau50e608d2007-05-13 18:26:08 +0200132 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100133#ifdef DEBUG_MEMORY_POOLS
134 /* keep track of where the element was allocated from */
135 *POOL_LINK(pool, ptr) = (void *)pool;
136#endif
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100137 return ptr;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200138}
139
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200140/*
141 * This function frees whatever can be freed in pool <pool>.
142 */
143void pool_flush2(struct pool_head *pool)
144{
145 void *temp, *next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200146 if (!pool)
147 return;
148
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200149 next = pool->free_list;
150 while (next) {
151 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100152 next = *POOL_LINK(pool, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200153 pool->allocated--;
David Carlierb781dbe2017-07-21 08:44:40 +0100154 free(temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200155 }
156 pool->free_list = next;
157
158 /* here, we should have pool->allocate == pool->used */
159}
160
161/*
162 * This function frees whatever can be freed in all pools, but respecting
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200163 * the minimum thresholds imposed by owners. It takes care of avoiding
164 * recursion because it may be called from a signal handler.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200165 */
166void pool_gc2()
167{
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200168 static int recurse;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200169 struct pool_head *entry;
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200170
171 if (recurse++)
172 goto out;
173
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200174 list_for_each_entry(entry, &pools, list) {
175 void *temp, *next;
176 //qfprintf(stderr, "Flushing pool %s\n", entry->name);
177 next = entry->free_list;
178 while (next &&
Willy Tarreau57767b82014-12-22 21:40:55 +0100179 (int)(entry->allocated - entry->used) > (int)entry->minavail) {
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200180 temp = next;
Willy Tarreauac421112015-10-28 15:09:29 +0100181 next = *POOL_LINK(entry, temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200182 entry->allocated--;
David Carlierb781dbe2017-07-21 08:44:40 +0100183 free(temp);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200184 }
185 entry->free_list = next;
186 }
Willy Tarreaub7f9d122009-04-21 02:17:45 +0200187 out:
188 recurse--;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200189}
190
191/*
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200192 * This function destroys a pool by freeing it completely, unless it's still
193 * in use. This should be called only under extreme circumstances. It always
194 * returns NULL if the resulting pool is empty, easing the clearing of the old
195 * pointer, otherwise it returns the pool.
196 * .
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200197 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200198void *pool_destroy2(struct pool_head *pool)
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200199{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200200 if (pool) {
201 pool_flush2(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200202 if (pool->used)
203 return pool;
204 pool->users--;
205 if (!pool->users) {
206 LIST_DEL(&pool->list);
David Carlierb781dbe2017-07-21 08:44:40 +0100207 free(pool);
Willy Tarreaudae4aa82007-06-16 23:19:53 +0200208 }
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200209 }
210 return NULL;
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200211}
212
Willy Tarreau12833bb2014-01-28 16:49:56 +0100213/* This function dumps memory usage information into the trash buffer. */
214void dump_pools_to_trash()
Willy Tarreau50e608d2007-05-13 18:26:08 +0200215{
216 struct pool_head *entry;
217 unsigned long allocated, used;
218 int nbpools;
219
220 allocated = used = nbpools = 0;
Willy Tarreau12833bb2014-01-28 16:49:56 +0100221 chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200222 list_for_each_entry(entry, &pools, list) {
Willy Tarreau58102cf2015-10-28 16:24:21 +0100223 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 +0200224 entry->name, entry->size, entry->allocated,
Willy Tarreau58102cf2015-10-28 16:24:21 +0100225 entry->size * entry->allocated, entry->used, entry->failed,
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200226 entry->users, (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200227
228 allocated += entry->allocated * entry->size;
229 used += entry->used * entry->size;
230 nbpools++;
231 }
Willy Tarreau12833bb2014-01-28 16:49:56 +0100232 chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n",
Willy Tarreau50e608d2007-05-13 18:26:08 +0200233 nbpools, allocated, used);
234}
235
Willy Tarreau12833bb2014-01-28 16:49:56 +0100236/* Dump statistics on pools usage. */
237void dump_pools(void)
238{
239 dump_pools_to_trash();
240 qfprintf(stderr, "%s", trash.str);
241}
242
Willy Tarreau58102cf2015-10-28 16:24:21 +0100243/* This function returns the total number of failed pool allocations */
244int pool_total_failures()
245{
246 struct pool_head *entry;
247 int failed = 0;
248
249 list_for_each_entry(entry, &pools, list)
250 failed += entry->failed;
251 return failed;
252}
253
254/* This function returns the total amount of memory allocated in pools (in bytes) */
255unsigned long pool_total_allocated()
256{
257 struct pool_head *entry;
258 unsigned long allocated = 0;
259
260 list_for_each_entry(entry, &pools, list)
261 allocated += entry->allocated * entry->size;
262 return allocated;
263}
264
265/* This function returns the total amount of memory used in pools (in bytes) */
266unsigned long pool_total_used()
267{
268 struct pool_head *entry;
269 unsigned long used = 0;
270
271 list_for_each_entry(entry, &pools, list)
272 used += entry->used * entry->size;
273 return used;
274}
275
William Lallemande7ed8852016-11-19 02:25:36 +0100276/* This function dumps memory usage information onto the stream interface's
277 * read buffer. It returns 0 as long as it does not complete, non-zero upon
278 * completion. No state is used.
279 */
280static int cli_io_handler_dump_pools(struct appctx *appctx)
281{
282 struct stream_interface *si = appctx->owner;
283
284 dump_pools_to_trash();
285 if (bi_putchk(si_ic(si), &trash) == -1) {
286 si_applet_cant_put(si);
287 return 0;
288 }
289 return 1;
290}
291
William Lallemande7ed8852016-11-19 02:25:36 +0100292/* register cli keywords */
293static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaue9ecec82016-12-16 18:55:23 +0100294 { { "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 +0100295 {{},}
296}};
297
298__attribute__((constructor))
299static void __memory_init(void)
300{
301 cli_register_kw(&cli_kws);
302}
303
Willy Tarreau50e608d2007-05-13 18:26:08 +0200304/*
305 * Local variables:
306 * c-indent-level: 8
307 * c-basic-offset: 8
308 * End:
309 */