2007-05-13 16:26:08 +00:00
|
|
|
/*
|
|
|
|
* Memory management functions.
|
|
|
|
*
|
|
|
|
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-11-19 01:25:36 +00:00
|
|
|
#include <types/applet.h>
|
|
|
|
#include <types/cli.h>
|
2014-01-28 15:49:56 +00:00
|
|
|
#include <types/global.h>
|
2016-11-19 01:25:36 +00:00
|
|
|
#include <types/stats.h>
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
#include <common/config.h>
|
[MEDIUM] Fix memory freeing at exit
New functions implemented:
- deinit_pollers: called at the end of deinit())
- prune_acl: called via list_for_each_entry_safe
Add missing pool_destroy2 calls:
- p->hdr_idx_pool
- pool2_tree64
Implement all task stopping:
- health-check: needs new "struct task" in the struct server
- queue processing: queue_mgt
- appsess_refresh: appsession_refresh
before (idle system):
==6079== LEAK SUMMARY:
==6079== definitely lost: 1,112 bytes in 75 blocks.
==6079== indirectly lost: 53,356 bytes in 2,090 blocks.
==6079== possibly lost: 52 bytes in 1 blocks.
==6079== still reachable: 150,996 bytes in 504 blocks.
==6079== suppressed: 0 bytes in 0 blocks.
after (idle system):
==6945== LEAK SUMMARY:
==6945== definitely lost: 7,644 bytes in 137 blocks.
==6945== indirectly lost: 9,913 bytes in 587 blocks.
==6945== possibly lost: 0 bytes in 0 blocks.
==6945== still reachable: 0 bytes in 0 blocks.
==6945== suppressed: 0 bytes in 0 blocks.
before (running system for ~2m):
==9343== LEAK SUMMARY:
==9343== definitely lost: 1,112 bytes in 75 blocks.
==9343== indirectly lost: 54,199 bytes in 2,122 blocks.
==9343== possibly lost: 52 bytes in 1 blocks.
==9343== still reachable: 151,128 bytes in 509 blocks.
==9343== suppressed: 0 bytes in 0 blocks.
after (running system for ~2m):
==11616== LEAK SUMMARY:
==11616== definitely lost: 7,644 bytes in 137 blocks.
==11616== indirectly lost: 9,981 bytes in 591 blocks.
==11616== possibly lost: 0 bytes in 0 blocks.
==11616== still reachable: 4 bytes in 1 blocks.
==11616== suppressed: 0 bytes in 0 blocks.
Still not perfect but significant improvement.
2008-05-29 21:53:44 +00:00
|
|
|
#include <common/debug.h>
|
2018-10-16 08:28:54 +00:00
|
|
|
#include <common/hathreads.h>
|
2007-05-13 16:26:08 +00:00
|
|
|
#include <common/memory.h>
|
|
|
|
#include <common/mini-clist.h>
|
|
|
|
#include <common/standard.h>
|
|
|
|
|
2016-11-19 01:25:36 +00:00
|
|
|
#include <proto/applet.h>
|
|
|
|
#include <proto/cli.h>
|
|
|
|
#include <proto/channel.h>
|
2007-05-13 16:26:08 +00:00
|
|
|
#include <proto/log.h>
|
2016-11-19 01:25:36 +00:00
|
|
|
#include <proto/stream_interface.h>
|
|
|
|
#include <proto/stats.h>
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2018-10-16 05:58:39 +00:00
|
|
|
/* These are the most common pools, expected to be initialized first. These
|
|
|
|
* ones are allocated from an array, allowing to map them to an index.
|
|
|
|
*/
|
|
|
|
struct pool_head pool_base_start[MAX_BASE_POOLS] = { };
|
|
|
|
unsigned int pool_base_count = 0;
|
|
|
|
|
2018-10-16 08:28:54 +00:00
|
|
|
THREAD_LOCAL struct pool_cache_head pool_cache[MAX_BASE_POOLS] = { };
|
|
|
|
THREAD_LOCAL struct list pool_lru_head = { }; /* oldest objects */
|
|
|
|
THREAD_LOCAL size_t pool_cache_bytes = 0; /* total cache size */
|
|
|
|
THREAD_LOCAL size_t pool_cache_count = 0; /* #cache objects */
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
static struct list pools = LIST_HEAD_INIT(pools);
|
2015-10-08 12:12:13 +00:00
|
|
|
int mem_poison_byte = -1;
|
2007-05-13 16:26:08 +00:00
|
|
|
|
|
|
|
/* Try to find an existing shared pool with the same characteristics and
|
|
|
|
* returns it, otherwise creates this one. NULL is returned if no memory
|
2016-01-25 01:19:13 +00:00
|
|
|
* is available for a new creation. Two flags are supported :
|
|
|
|
* - MEM_F_SHARED to indicate that the pool may be shared with other users
|
|
|
|
* - MEM_F_EXACT to indicate that the size must not be rounded up
|
2007-05-13 16:26:08 +00:00
|
|
|
*/
|
|
|
|
struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
|
|
|
|
{
|
|
|
|
struct pool_head *pool;
|
2007-05-13 22:16:13 +00:00
|
|
|
struct pool_head *entry;
|
|
|
|
struct list *start;
|
2007-05-13 16:26:08 +00:00
|
|
|
unsigned int align;
|
|
|
|
|
2015-10-28 14:09:29 +00:00
|
|
|
/* We need to store a (void *) at the end of the chunks. Since we know
|
2007-05-13 16:26:08 +00:00
|
|
|
* that the malloc() function will never return such a small size,
|
|
|
|
* let's round the size up to something slightly bigger, in order to
|
|
|
|
* ease merging of entries. Note that the rounding is a power of two.
|
2015-10-28 14:09:29 +00:00
|
|
|
* This extra (void *) is not accounted for in the size computation
|
|
|
|
* so that the visible parts outside are not affected.
|
2018-10-23 12:40:23 +00:00
|
|
|
*
|
|
|
|
* Note: for the LRU cache, we need to store 2 doubly-linked lists.
|
2007-05-13 16:26:08 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-25 01:19:13 +00:00
|
|
|
if (!(flags & MEM_F_EXACT)) {
|
2018-10-23 12:40:23 +00:00
|
|
|
align = 4 * sizeof(void *); // 2 lists = 4 pointers min
|
2016-01-25 01:19:13 +00:00
|
|
|
size = ((size + POOL_EXTRA + align - 1) & -align) - POOL_EXTRA;
|
|
|
|
}
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2017-08-29 07:52:38 +00:00
|
|
|
/* TODO: thread: we do not lock pool list for now because all pools are
|
|
|
|
* created during HAProxy startup (so before threads creation) */
|
2007-05-13 22:16:13 +00:00
|
|
|
start = &pools;
|
2007-05-13 16:26:08 +00:00
|
|
|
pool = NULL;
|
2007-05-13 22:16:13 +00:00
|
|
|
|
|
|
|
list_for_each_entry(entry, &pools, list) {
|
|
|
|
if (entry->size == size) {
|
|
|
|
/* either we can share this place and we take it, or
|
|
|
|
* we look for a sharable one or for the next position
|
|
|
|
* before which we will insert a new one.
|
|
|
|
*/
|
|
|
|
if (flags & entry->flags & MEM_F_SHARED) {
|
|
|
|
/* we can share this one */
|
2007-05-13 16:26:08 +00:00
|
|
|
pool = entry;
|
[MEDIUM] Fix memory freeing at exit
New functions implemented:
- deinit_pollers: called at the end of deinit())
- prune_acl: called via list_for_each_entry_safe
Add missing pool_destroy2 calls:
- p->hdr_idx_pool
- pool2_tree64
Implement all task stopping:
- health-check: needs new "struct task" in the struct server
- queue processing: queue_mgt
- appsess_refresh: appsession_refresh
before (idle system):
==6079== LEAK SUMMARY:
==6079== definitely lost: 1,112 bytes in 75 blocks.
==6079== indirectly lost: 53,356 bytes in 2,090 blocks.
==6079== possibly lost: 52 bytes in 1 blocks.
==6079== still reachable: 150,996 bytes in 504 blocks.
==6079== suppressed: 0 bytes in 0 blocks.
after (idle system):
==6945== LEAK SUMMARY:
==6945== definitely lost: 7,644 bytes in 137 blocks.
==6945== indirectly lost: 9,913 bytes in 587 blocks.
==6945== possibly lost: 0 bytes in 0 blocks.
==6945== still reachable: 0 bytes in 0 blocks.
==6945== suppressed: 0 bytes in 0 blocks.
before (running system for ~2m):
==9343== LEAK SUMMARY:
==9343== definitely lost: 1,112 bytes in 75 blocks.
==9343== indirectly lost: 54,199 bytes in 2,122 blocks.
==9343== possibly lost: 52 bytes in 1 blocks.
==9343== still reachable: 151,128 bytes in 509 blocks.
==9343== suppressed: 0 bytes in 0 blocks.
after (running system for ~2m):
==11616== LEAK SUMMARY:
==11616== definitely lost: 7,644 bytes in 137 blocks.
==11616== indirectly lost: 9,981 bytes in 591 blocks.
==11616== possibly lost: 0 bytes in 0 blocks.
==11616== still reachable: 4 bytes in 1 blocks.
==11616== suppressed: 0 bytes in 0 blocks.
Still not perfect but significant improvement.
2008-05-29 21:53:44 +00:00
|
|
|
DPRINTF(stderr, "Sharing %s with %s\n", name, pool->name);
|
2007-05-13 16:26:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-13 22:16:13 +00:00
|
|
|
else if (entry->size > size) {
|
|
|
|
/* insert before this one */
|
|
|
|
start = &entry->list;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-13 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool) {
|
2018-10-16 05:58:39 +00:00
|
|
|
if (pool_base_count < MAX_BASE_POOLS)
|
|
|
|
pool = &pool_base_start[pool_base_count++];
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
/* look for a freed entry */
|
|
|
|
for (entry = pool_base_start; entry != pool_base_start + MAX_BASE_POOLS; entry++) {
|
|
|
|
if (!entry->size) {
|
|
|
|
pool = entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool)
|
|
|
|
pool = calloc(1, sizeof(*pool));
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
if (!pool)
|
|
|
|
return NULL;
|
|
|
|
if (name)
|
|
|
|
strlcpy2(pool->name, name, sizeof(pool->name));
|
|
|
|
pool->size = size;
|
|
|
|
pool->flags = flags;
|
2007-05-13 22:16:13 +00:00
|
|
|
LIST_ADDQ(start, &pool->list);
|
2007-05-13 16:26:08 +00:00
|
|
|
}
|
2007-05-13 22:16:13 +00:00
|
|
|
pool->users++;
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifndef CONFIG_HAP_LOCKLESS_POOLS
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_INIT(&pool->lock);
|
2018-01-24 17:38:31 +00:00
|
|
|
#endif
|
2007-05-13 16:26:08 +00:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifdef CONFIG_HAP_LOCKLESS_POOLS
|
2018-01-24 17:38:31 +00:00
|
|
|
/* Allocates new entries for pool <pool> until there are at least <avail> + 1
|
|
|
|
* available, then returns the last one for immediate use, so that at least
|
|
|
|
* <avail> are left available in the pool upon return. NULL is returned if the
|
|
|
|
* last entry could not be allocated. It's important to note that at least one
|
|
|
|
* allocation is always performed even if there are enough entries in the pool.
|
|
|
|
* A call to the garbage collector is performed at most once in case malloc()
|
|
|
|
* returns an error, before returning NULL.
|
|
|
|
*/
|
|
|
|
void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
|
|
|
|
{
|
2018-10-20 23:52:59 +00:00
|
|
|
void *ptr = NULL, **free_list;
|
2018-01-24 17:38:31 +00:00
|
|
|
int failed = 0;
|
|
|
|
int size = pool->size;
|
|
|
|
int limit = pool->limit;
|
|
|
|
int allocated = pool->allocated, allocated_orig = allocated;
|
|
|
|
|
|
|
|
/* stop point */
|
|
|
|
avail += pool->used;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (limit && allocated >= limit) {
|
|
|
|
HA_ATOMIC_ADD(&pool->allocated, allocated - allocated_orig);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = malloc(size + POOL_EXTRA);
|
|
|
|
if (!ptr) {
|
|
|
|
HA_ATOMIC_ADD(&pool->failed, 1);
|
|
|
|
if (failed)
|
|
|
|
return NULL;
|
|
|
|
failed++;
|
|
|
|
pool_gc(pool);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (++allocated > avail)
|
|
|
|
break;
|
|
|
|
|
|
|
|
free_list = pool->free_list;
|
|
|
|
do {
|
|
|
|
*POOL_LINK(pool, ptr) = free_list;
|
|
|
|
__ha_barrier_store();
|
2018-10-20 23:52:59 +00:00
|
|
|
} while (HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr) == 0);
|
2018-01-24 17:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HA_ATOMIC_ADD(&pool->allocated, allocated - allocated_orig);
|
|
|
|
HA_ATOMIC_ADD(&pool->used, 1);
|
|
|
|
|
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
/* keep track of where the element was allocated from */
|
|
|
|
*POOL_LINK(pool, ptr) = (void *)pool;
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = __pool_refill_alloc(pool, avail);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in pool <pool>.
|
|
|
|
*/
|
|
|
|
void pool_flush(struct pool_head *pool)
|
|
|
|
{
|
2018-10-20 23:52:59 +00:00
|
|
|
void **next, *temp;
|
2018-01-24 17:38:31 +00:00
|
|
|
int removed = 0;
|
|
|
|
|
|
|
|
if (!pool)
|
|
|
|
return;
|
|
|
|
do {
|
|
|
|
next = pool->free_list;
|
2018-10-20 23:52:59 +00:00
|
|
|
} while (!HA_ATOMIC_CAS(&pool->free_list, &next, NULL));
|
2018-01-24 17:38:31 +00:00
|
|
|
while (next) {
|
|
|
|
temp = next;
|
|
|
|
next = *POOL_LINK(pool, temp);
|
|
|
|
removed++;
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
pool->free_list = next;
|
|
|
|
HA_ATOMIC_SUB(&pool->allocated, removed);
|
|
|
|
/* here, we should have pool->allocate == pool->used */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in all pools, but respecting
|
|
|
|
* the minimum thresholds imposed by owners. It takes care of avoiding
|
|
|
|
* recursion because it may be called from a signal handler.
|
|
|
|
*
|
|
|
|
* <pool_ctx> is unused
|
|
|
|
*/
|
|
|
|
void pool_gc(struct pool_head *pool_ctx)
|
|
|
|
{
|
|
|
|
static int recurse;
|
|
|
|
int cur_recurse = 0;
|
|
|
|
struct pool_head *entry;
|
|
|
|
|
|
|
|
if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &pools, list) {
|
|
|
|
while ((int)((volatile int)entry->allocated - (volatile int)entry->used) > (int)entry->minavail) {
|
|
|
|
struct pool_free_list cmp, new;
|
|
|
|
|
|
|
|
cmp.seq = entry->seq;
|
|
|
|
__ha_barrier_load();
|
|
|
|
cmp.free_list = entry->free_list;
|
|
|
|
__ha_barrier_load();
|
|
|
|
if (cmp.free_list == NULL)
|
|
|
|
break;
|
|
|
|
new.free_list = *POOL_LINK(entry, cmp.free_list);
|
|
|
|
new.seq = cmp.seq + 1;
|
|
|
|
if (__ha_cas_dw(&entry->free_list, &cmp, &new) == 0)
|
|
|
|
continue;
|
|
|
|
free(cmp.free_list);
|
|
|
|
HA_ATOMIC_SUB(&entry->allocated, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HA_ATOMIC_STORE(&recurse, 0);
|
|
|
|
}
|
2018-10-16 08:28:54 +00:00
|
|
|
|
|
|
|
/* frees an object to the local cache, possibly pushing oldest objects to the
|
|
|
|
* global pool. Must not be called directly.
|
|
|
|
*/
|
|
|
|
void __pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t idx)
|
|
|
|
{
|
|
|
|
struct pool_cache_item *item = (struct pool_cache_item *)ptr;
|
|
|
|
struct pool_cache_head *ph = &pool_cache[idx];
|
|
|
|
|
|
|
|
/* never allocated or empty */
|
|
|
|
if (unlikely(ph->list.n == NULL)) {
|
|
|
|
LIST_INIT(&ph->list);
|
|
|
|
ph->size = pool->size;
|
|
|
|
if (pool_lru_head.n == NULL)
|
|
|
|
LIST_INIT(&pool_lru_head);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_ADD(&ph->list, &item->by_pool);
|
|
|
|
LIST_ADD(&pool_lru_head, &item->by_lru);
|
|
|
|
ph->count++;
|
|
|
|
pool_cache_count++;
|
|
|
|
pool_cache_bytes += ph->size;
|
|
|
|
|
|
|
|
if (pool_cache_bytes <= CONFIG_HAP_POOL_CACHE_SIZE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
|
|
|
item = LIST_PREV(&pool_lru_head, struct pool_cache_item *, by_lru);
|
|
|
|
/* note: by definition we remove oldest objects so they also are the
|
|
|
|
* oldest in their own pools, thus their next is the pool's head.
|
|
|
|
*/
|
|
|
|
ph = LIST_NEXT(&item->by_pool, struct pool_cache_head *, list);
|
|
|
|
LIST_DEL(&item->by_pool);
|
|
|
|
LIST_DEL(&item->by_lru);
|
|
|
|
ph->count--;
|
|
|
|
pool_cache_count--;
|
|
|
|
pool_cache_bytes -= ph->size;
|
|
|
|
__pool_free(pool_base_start + (ph - pool_cache), item);
|
|
|
|
} while (pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 7 / 8);
|
|
|
|
}
|
|
|
|
|
2018-02-22 13:05:55 +00:00
|
|
|
#else /* CONFIG_HAP_LOCKLESS_POOLS */
|
2018-01-24 17:38:31 +00:00
|
|
|
|
2014-12-03 14:25:28 +00:00
|
|
|
/* Allocates new entries for pool <pool> until there are at least <avail> + 1
|
|
|
|
* available, then returns the last one for immediate use, so that at least
|
|
|
|
* <avail> are left available in the pool upon return. NULL is returned if the
|
|
|
|
* last entry could not be allocated. It's important to note that at least one
|
|
|
|
* allocation is always performed even if there are enough entries in the pool.
|
|
|
|
* A call to the garbage collector is performed at most once in case malloc()
|
|
|
|
* returns an error, before returning NULL.
|
2007-05-13 16:26:08 +00:00
|
|
|
*/
|
2017-08-29 07:52:38 +00:00
|
|
|
void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
|
2007-05-13 16:26:08 +00:00
|
|
|
{
|
2014-12-03 14:25:28 +00:00
|
|
|
void *ptr = NULL;
|
|
|
|
int failed = 0;
|
|
|
|
|
|
|
|
/* stop point */
|
|
|
|
avail += pool->used;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (pool->limit && pool->allocated >= pool->limit)
|
2007-05-13 22:16:13 +00:00
|
|
|
return NULL;
|
2014-12-03 14:25:28 +00:00
|
|
|
|
2017-11-22 09:50:54 +00:00
|
|
|
ptr = pool_alloc_area(pool->size + POOL_EXTRA);
|
2014-12-03 14:25:28 +00:00
|
|
|
if (!ptr) {
|
2015-10-28 15:24:21 +00:00
|
|
|
pool->failed++;
|
2014-12-03 14:25:28 +00:00
|
|
|
if (failed)
|
|
|
|
return NULL;
|
|
|
|
failed++;
|
2017-11-24 16:34:44 +00:00
|
|
|
pool_gc(pool);
|
2014-12-03 14:25:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (++pool->allocated > avail)
|
|
|
|
break;
|
|
|
|
|
2015-10-28 14:09:29 +00:00
|
|
|
*POOL_LINK(pool, ptr) = (void *)pool->free_list;
|
2014-12-03 14:25:28 +00:00
|
|
|
pool->free_list = ptr;
|
2007-05-13 22:16:13 +00:00
|
|
|
}
|
2007-05-13 16:26:08 +00:00
|
|
|
pool->used++;
|
2015-10-28 14:23:51 +00:00
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
/* keep track of where the element was allocated from */
|
|
|
|
*POOL_LINK(pool, ptr) = (void *)pool;
|
|
|
|
#endif
|
2014-12-03 14:25:28 +00:00
|
|
|
return ptr;
|
2007-05-13 16:26:08 +00:00
|
|
|
}
|
2017-08-29 07:52:38 +00:00
|
|
|
void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
|
|
|
|
{
|
|
|
|
void *ptr;
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
|
2017-08-29 07:52:38 +00:00
|
|
|
ptr = __pool_refill_alloc(pool, avail);
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
|
2017-08-29 07:52:38 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
2007-05-13 17:38:49 +00:00
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in pool <pool>.
|
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
void pool_flush(struct pool_head *pool)
|
2007-05-13 17:38:49 +00:00
|
|
|
{
|
|
|
|
void *temp, *next;
|
2007-05-13 22:39:29 +00:00
|
|
|
if (!pool)
|
|
|
|
return;
|
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
|
2007-05-13 17:38:49 +00:00
|
|
|
next = pool->free_list;
|
|
|
|
while (next) {
|
|
|
|
temp = next;
|
2015-10-28 14:09:29 +00:00
|
|
|
next = *POOL_LINK(pool, temp);
|
2007-05-13 17:38:49 +00:00
|
|
|
pool->allocated--;
|
2017-11-22 09:50:54 +00:00
|
|
|
pool_free_area(temp, pool->size + POOL_EXTRA);
|
2007-05-13 17:38:49 +00:00
|
|
|
}
|
|
|
|
pool->free_list = next;
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
|
2007-05-13 17:38:49 +00:00
|
|
|
/* here, we should have pool->allocate == pool->used */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in all pools, but respecting
|
2009-04-21 00:17:45 +00:00
|
|
|
* the minimum thresholds imposed by owners. It takes care of avoiding
|
|
|
|
* recursion because it may be called from a signal handler.
|
2017-08-29 07:52:38 +00:00
|
|
|
*
|
2017-11-24 16:34:44 +00:00
|
|
|
* <pool_ctx> is used when pool_gc is called to release resources to allocate
|
2017-08-29 07:52:38 +00:00
|
|
|
* an element in __pool_refill_alloc. It is important because <pool_ctx> is
|
|
|
|
* already locked, so we need to skip the lock here.
|
2007-05-13 17:38:49 +00:00
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
void pool_gc(struct pool_head *pool_ctx)
|
2007-05-13 17:38:49 +00:00
|
|
|
{
|
2009-04-21 00:17:45 +00:00
|
|
|
static int recurse;
|
2017-08-29 07:52:38 +00:00
|
|
|
int cur_recurse = 0;
|
2007-05-13 17:38:49 +00:00
|
|
|
struct pool_head *entry;
|
2009-04-21 00:17:45 +00:00
|
|
|
|
2017-08-29 07:52:38 +00:00
|
|
|
if (recurse || !HA_ATOMIC_CAS(&recurse, &cur_recurse, 1))
|
|
|
|
return;
|
2009-04-21 00:17:45 +00:00
|
|
|
|
2007-05-13 17:38:49 +00:00
|
|
|
list_for_each_entry(entry, &pools, list) {
|
|
|
|
void *temp, *next;
|
|
|
|
//qfprintf(stderr, "Flushing pool %s\n", entry->name);
|
2017-08-29 07:52:38 +00:00
|
|
|
if (entry != pool_ctx)
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
|
2007-05-13 17:38:49 +00:00
|
|
|
next = entry->free_list;
|
|
|
|
while (next &&
|
2014-12-22 20:40:55 +00:00
|
|
|
(int)(entry->allocated - entry->used) > (int)entry->minavail) {
|
2007-05-13 17:38:49 +00:00
|
|
|
temp = next;
|
2015-10-28 14:09:29 +00:00
|
|
|
next = *POOL_LINK(entry, temp);
|
2007-05-13 17:38:49 +00:00
|
|
|
entry->allocated--;
|
2017-11-22 09:50:54 +00:00
|
|
|
pool_free_area(temp, entry->size + POOL_EXTRA);
|
2007-05-13 17:38:49 +00:00
|
|
|
}
|
|
|
|
entry->free_list = next;
|
2017-08-29 07:52:38 +00:00
|
|
|
if (entry != pool_ctx)
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
|
2007-05-13 17:38:49 +00:00
|
|
|
}
|
2017-08-29 07:52:38 +00:00
|
|
|
|
|
|
|
HA_ATOMIC_STORE(&recurse, 0);
|
2007-05-13 17:38:49 +00:00
|
|
|
}
|
2018-01-24 17:38:31 +00:00
|
|
|
#endif
|
2007-05-13 17:38:49 +00:00
|
|
|
|
|
|
|
/*
|
2007-06-16 21:19:53 +00:00
|
|
|
* This function destroys a pool by freeing it completely, unless it's still
|
|
|
|
* in use. This should be called only under extreme circumstances. It always
|
|
|
|
* returns NULL if the resulting pool is empty, easing the clearing of the old
|
|
|
|
* pointer, otherwise it returns the pool.
|
|
|
|
* .
|
2007-05-13 17:38:49 +00:00
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
void *pool_destroy(struct pool_head *pool)
|
2007-05-13 17:38:49 +00:00
|
|
|
{
|
2007-05-13 22:39:29 +00:00
|
|
|
if (pool) {
|
2017-11-24 16:34:44 +00:00
|
|
|
pool_flush(pool);
|
2007-06-16 21:19:53 +00:00
|
|
|
if (pool->used)
|
|
|
|
return pool;
|
|
|
|
pool->users--;
|
|
|
|
if (!pool->users) {
|
|
|
|
LIST_DEL(&pool->list);
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifndef CONFIG_HAP_LOCKLESS_POOLS
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_DESTROY(&pool->lock);
|
2018-01-24 17:38:31 +00:00
|
|
|
#endif
|
2018-10-16 05:58:39 +00:00
|
|
|
if ((pool - pool_base_start) < MAX_BASE_POOLS)
|
|
|
|
memset(pool, 0, sizeof(*pool));
|
|
|
|
else
|
|
|
|
free(pool);
|
2007-06-16 21:19:53 +00:00
|
|
|
}
|
2007-05-13 22:39:29 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2007-05-13 17:38:49 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 15:49:56 +00:00
|
|
|
/* This function dumps memory usage information into the trash buffer. */
|
|
|
|
void dump_pools_to_trash()
|
2007-05-13 16:26:08 +00:00
|
|
|
{
|
|
|
|
struct pool_head *entry;
|
|
|
|
unsigned long allocated, used;
|
|
|
|
int nbpools;
|
|
|
|
|
|
|
|
allocated = used = nbpools = 0;
|
2014-01-28 15:49:56 +00:00
|
|
|
chunk_printf(&trash, "Dumping pools usage. Use SIGQUIT to flush them.\n");
|
2007-05-13 16:26:08 +00:00
|
|
|
list_for_each_entry(entry, &pools, list) {
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifndef CONFIG_HAP_LOCKLESS_POOLS
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &entry->lock);
|
2018-01-24 17:38:31 +00:00
|
|
|
#endif
|
2018-10-16 05:58:39 +00:00
|
|
|
chunk_appendf(&trash, " - Pool %s (%d bytes) : %d allocated (%u bytes), %d used, %d failures, %d users, @%p=%02d%s\n",
|
2007-05-13 17:38:49 +00:00
|
|
|
entry->name, entry->size, entry->allocated,
|
2015-10-28 15:24:21 +00:00
|
|
|
entry->size * entry->allocated, entry->used, entry->failed,
|
2018-10-16 05:58:39 +00:00
|
|
|
entry->users, entry, (int)pool_get_index(entry),
|
|
|
|
(entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
|
2007-05-13 16:26:08 +00:00
|
|
|
|
|
|
|
allocated += entry->allocated * entry->size;
|
|
|
|
used += entry->used * entry->size;
|
|
|
|
nbpools++;
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifndef CONFIG_HAP_LOCKLESS_POOLS
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &entry->lock);
|
2018-01-24 17:38:31 +00:00
|
|
|
#endif
|
2007-05-13 16:26:08 +00:00
|
|
|
}
|
2014-01-28 15:49:56 +00:00
|
|
|
chunk_appendf(&trash, "Total: %d pools, %lu bytes allocated, %lu used.\n",
|
2007-05-13 16:26:08 +00:00
|
|
|
nbpools, allocated, used);
|
|
|
|
}
|
|
|
|
|
2014-01-28 15:49:56 +00:00
|
|
|
/* Dump statistics on pools usage. */
|
|
|
|
void dump_pools(void)
|
|
|
|
{
|
|
|
|
dump_pools_to_trash();
|
2018-07-13 08:54:26 +00:00
|
|
|
qfprintf(stderr, "%s", trash.area);
|
2014-01-28 15:49:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:24:21 +00:00
|
|
|
/* This function returns the total number of failed pool allocations */
|
|
|
|
int pool_total_failures()
|
|
|
|
{
|
|
|
|
struct pool_head *entry;
|
|
|
|
int failed = 0;
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &pools, list)
|
|
|
|
failed += entry->failed;
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function returns the total amount of memory allocated in pools (in bytes) */
|
|
|
|
unsigned long pool_total_allocated()
|
|
|
|
{
|
|
|
|
struct pool_head *entry;
|
|
|
|
unsigned long allocated = 0;
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &pools, list)
|
|
|
|
allocated += entry->allocated * entry->size;
|
|
|
|
return allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function returns the total amount of memory used in pools (in bytes) */
|
|
|
|
unsigned long pool_total_used()
|
|
|
|
{
|
|
|
|
struct pool_head *entry;
|
|
|
|
unsigned long used = 0;
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &pools, list)
|
|
|
|
used += entry->used * entry->size;
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
2016-11-19 01:25:36 +00:00
|
|
|
/* This function dumps memory usage information onto the stream interface's
|
|
|
|
* read buffer. It returns 0 as long as it does not complete, non-zero upon
|
|
|
|
* completion. No state is used.
|
|
|
|
*/
|
|
|
|
static int cli_io_handler_dump_pools(struct appctx *appctx)
|
|
|
|
{
|
|
|
|
struct stream_interface *si = appctx->owner;
|
|
|
|
|
|
|
|
dump_pools_to_trash();
|
REORG: channel: finally rename the last bi_* / bo_* functions
For HTTP/2 we'll need some buffer-only equivalent functions to some of
the ones applying to channels and still squatting the bi_* / bo_*
namespace. Since these names have kept being misleading for quite some
time now and are really getting annoying, it's time to rename them. This
commit will use "ci/co" as the prefix (for "channel in", "channel out")
instead of "bi/bo". The following ones were renamed :
bi_getblk_nc, bi_getline_nc, bi_putblk, bi_putchr,
bo_getblk, bo_getblk_nc, bo_getline, bo_getline_nc, bo_inject,
bi_putchk, bi_putstr, bo_getchr, bo_skip, bi_swpbuf
2017-10-19 12:32:15 +00:00
|
|
|
if (ci_putchk(si_ic(si), &trash) == -1) {
|
2018-11-15 10:08:52 +00:00
|
|
|
si_rx_room_blk(si);
|
2016-11-19 01:25:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register cli keywords */
|
|
|
|
static struct cli_kw_list cli_kws = {{ },{
|
2016-12-16 17:55:23 +00:00
|
|
|
{ { "show", "pools", NULL }, "show pools : report information about the memory pools usage", NULL, cli_io_handler_dump_pools },
|
2016-11-19 01:25:36 +00:00
|
|
|
{{},}
|
|
|
|
}};
|
|
|
|
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void __memory_init(void)
|
|
|
|
{
|
|
|
|
cli_register_kw(&cli_kws);
|
|
|
|
}
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|