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 |
| 125 | * the minimum thresholds imposed by owners. |
| 126 | */ |
| 127 | void pool_gc2() |
| 128 | { |
| 129 | struct pool_head *entry; |
| 130 | list_for_each_entry(entry, &pools, list) { |
| 131 | void *temp, *next; |
| 132 | //qfprintf(stderr, "Flushing pool %s\n", entry->name); |
| 133 | next = entry->free_list; |
| 134 | while (next && |
| 135 | entry->allocated > entry->minavail && |
| 136 | entry->allocated > entry->used) { |
| 137 | temp = next; |
| 138 | next = *(void **)temp; |
| 139 | entry->allocated--; |
| 140 | FREE(temp); |
| 141 | } |
| 142 | entry->free_list = next; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 147 | * This function destroys a pool by freeing it completely, unless it's still |
| 148 | * in use. This should be called only under extreme circumstances. It always |
| 149 | * returns NULL if the resulting pool is empty, easing the clearing of the old |
| 150 | * pointer, otherwise it returns the pool. |
| 151 | * . |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 152 | */ |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 153 | void *pool_destroy2(struct pool_head *pool) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 154 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 155 | if (pool) { |
| 156 | pool_flush2(pool); |
Willy Tarreau | dae4aa8 | 2007-06-16 23:19:53 +0200 | [diff] [blame] | 157 | if (pool->used) |
| 158 | return pool; |
| 159 | pool->users--; |
| 160 | if (!pool->users) { |
| 161 | LIST_DEL(&pool->list); |
| 162 | FREE(pool); |
| 163 | } |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 164 | } |
| 165 | return NULL; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 168 | /* Dump statistics on pools usage. |
| 169 | */ |
| 170 | void dump_pools(void) |
| 171 | { |
| 172 | struct pool_head *entry; |
| 173 | unsigned long allocated, used; |
| 174 | int nbpools; |
| 175 | |
| 176 | allocated = used = nbpools = 0; |
| 177 | qfprintf(stderr, "Dumping pools usage.\n"); |
| 178 | list_for_each_entry(entry, &pools, list) { |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 179 | qfprintf(stderr, " - Pool %s (%d bytes) : %d allocated (%lu bytes), %d used, %d users%s\n", |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 180 | entry->name, entry->size, entry->allocated, |
| 181 | entry->size * entry->allocated, entry->used, |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 182 | entry->users, (entry->flags & MEM_F_SHARED) ? " [SHARED]" : ""); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 183 | |
| 184 | allocated += entry->allocated * entry->size; |
| 185 | used += entry->used * entry->size; |
| 186 | nbpools++; |
| 187 | } |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 188 | qfprintf(stderr, "Total: %d pools, %lu bytes allocated, %lu used.\n", |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 189 | nbpools, allocated, used); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Local variables: |
| 194 | * c-indent-level: 8 |
| 195 | * c-basic-offset: 8 |
| 196 | * End: |
| 197 | */ |