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