Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | #include <common/config.h> |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 14 | #include <common/debug.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 15 | #include <common/memory.h> |
| 16 | #include <common/mini-clist.h> |
| 17 | #include <common/standard.h> |
| 18 | |
| 19 | #include <proto/log.h> |
| 20 | |
| 21 | static struct list pools = LIST_HEAD_INIT(pools); |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 22 | char mem_poison_byte = 0; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 23 | |
| 24 | /* Try to find an existing shared pool with the same characteristics and |
| 25 | * returns it, otherwise creates this one. NULL is returned if no memory |
| 26 | * is available for a new creation. |
| 27 | */ |
| 28 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags) |
| 29 | { |
| 30 | struct pool_head *pool; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 31 | struct pool_head *entry; |
| 32 | struct list *start; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 33 | unsigned int align; |
| 34 | |
| 35 | /* We need to store at least a (void *) in the chunks. Since we know |
| 36 | * that the malloc() function will never return such a small size, |
| 37 | * let's round the size up to something slightly bigger, in order to |
| 38 | * ease merging of entries. Note that the rounding is a power of two. |
| 39 | */ |
| 40 | |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 41 | align = 16; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 42 | size = (size + align - 1) & -align; |
| 43 | |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 44 | start = &pools; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 45 | pool = NULL; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 46 | |
| 47 | list_for_each_entry(entry, &pools, list) { |
| 48 | if (entry->size == size) { |
| 49 | /* either we can share this place and we take it, or |
| 50 | * we look for a sharable one or for the next position |
| 51 | * before which we will insert a new one. |
| 52 | */ |
| 53 | if (flags & entry->flags & MEM_F_SHARED) { |
| 54 | /* we can share this one */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 55 | pool = entry; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 56 | DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 60 | else if (entry->size > size) { |
| 61 | /* insert before this one */ |
| 62 | start = &entry->list; |
| 63 | break; |
| 64 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (!pool) { |
| 68 | pool = CALLOC(1, sizeof(*pool)); |
| 69 | if (!pool) |
| 70 | return NULL; |
| 71 | if (name) |
| 72 | strlcpy2(pool->name, name, sizeof(pool->name)); |
| 73 | pool->size = size; |
| 74 | pool->flags = flags; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 75 | LIST_ADDQ(start, &pool->list); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 76 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 77 | pool->users++; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 78 | return pool; |
| 79 | } |
| 80 | |
| 81 | /* Allocate a new entry for pool <pool>, and return it for immediate use. |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 82 | * NULL is returned if no memory is available for a new creation. A call |
| 83 | * to the garbage collector is performed before returning NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 84 | */ |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 85 | void *pool_refill_alloc(struct pool_head *pool) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 86 | { |
| 87 | void *ret; |
| 88 | |
| 89 | if (pool->limit && (pool->allocated >= pool->limit)) |
| 90 | return NULL; |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 91 | ret = CALLOC(1, pool->size); |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 92 | if (!ret) { |
| 93 | pool_gc2(); |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 94 | ret = CALLOC(1, pool->size); |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 95 | if (!ret) |
| 96 | return NULL; |
| 97 | } |
Willy Tarreau | 6e06443 | 2012-05-08 15:40:42 +0200 | [diff] [blame] | 98 | if (mem_poison_byte) |
| 99 | memset(ret, mem_poison_byte, pool->size); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 100 | pool->allocated++; |
| 101 | pool->used++; |
| 102 | return ret; |
| 103 | } |
| 104 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 105 | /* |
| 106 | * This function frees whatever can be freed in pool <pool>. |
| 107 | */ |
| 108 | void pool_flush2(struct pool_head *pool) |
| 109 | { |
| 110 | void *temp, *next; |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 111 | if (!pool) |
| 112 | return; |
| 113 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 114 | next = pool->free_list; |
| 115 | while (next) { |
| 116 | temp = next; |
| 117 | next = *(void **)temp; |
| 118 | pool->allocated--; |
| 119 | FREE(temp); |
| 120 | } |
| 121 | pool->free_list = next; |
| 122 | |
| 123 | /* here, we should have pool->allocate == pool->used */ |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * This function frees whatever can be freed in all pools, but respecting |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 128 | * the minimum thresholds imposed by owners. It takes care of avoiding |
| 129 | * recursion because it may be called from a signal handler. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 130 | */ |
| 131 | void pool_gc2() |
| 132 | { |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 133 | static int recurse; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 134 | struct pool_head *entry; |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 135 | |
| 136 | if (recurse++) |
| 137 | goto out; |
| 138 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 139 | list_for_each_entry(entry, &pools, list) { |
| 140 | void *temp, *next; |
| 141 | //qfprintf(stderr, "Flushing pool %s\n", entry->name); |
| 142 | next = entry->free_list; |
| 143 | while (next && |
| 144 | entry->allocated > entry->minavail && |
| 145 | entry->allocated > entry->used) { |
| 146 | temp = next; |
| 147 | next = *(void **)temp; |
| 148 | entry->allocated--; |
| 149 | FREE(temp); |
| 150 | } |
| 151 | entry->free_list = next; |
| 152 | } |
Willy Tarreau | b7f9d12 | 2009-04-21 02:17:45 +0200 | [diff] [blame] | 153 | out: |
| 154 | recurse--; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 158 | * This function destroys a pool by freeing it completely, unless it's still |
| 159 | * in use. This should be called only under extreme circumstances. It always |
| 160 | * returns NULL if the resulting pool is empty, easing the clearing of the old |
| 161 | * pointer, otherwise it returns the pool. |
| 162 | * . |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 163 | */ |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 164 | void *pool_destroy2(struct pool_head *pool) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 165 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 166 | if (pool) { |
| 167 | pool_flush2(pool); |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 168 | if (pool->used) |
| 169 | return pool; |
| 170 | pool->users--; |
| 171 | if (!pool->users) { |
| 172 | LIST_DEL(&pool->list); |
| 173 | FREE(pool); |
| 174 | } |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 175 | } |
| 176 | return NULL; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 179 | /* Dump statistics on pools usage. |
| 180 | */ |
| 181 | void dump_pools(void) |
| 182 | { |
| 183 | struct pool_head *entry; |
| 184 | unsigned long allocated, used; |
| 185 | int nbpools; |
| 186 | |
| 187 | allocated = used = nbpools = 0; |
| 188 | qfprintf(stderr, "Dumping pools usage.\n"); |
| 189 | list_for_each_entry(entry, &pools, list) { |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 190 | qfprintf(stderr, " - Pool %s (%d bytes) : %d allocated (%u bytes), %d used, %d users%s\n", |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 191 | entry->name, entry->size, entry->allocated, |
| 192 | entry->size * entry->allocated, entry->used, |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 193 | entry->users, (entry->flags & MEM_F_SHARED) ? " [SHARED]" : ""); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 194 | |
| 195 | allocated += entry->allocated * entry->size; |
| 196 | used += entry->used * entry->size; |
| 197 | nbpools++; |
| 198 | } |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 199 | qfprintf(stderr, "Total: %d pools, %lu bytes allocated, %lu used.\n", |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 200 | nbpools, allocated, used); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Local variables: |
| 205 | * c-indent-level: 8 |
| 206 | * c-basic-offset: 8 |
| 207 | * End: |
| 208 | */ |