CLEANUP: pool: include freq_ctr.h and remove locally duplicated functions

In memory.h we had to reimplement the swrate* functions just because of
a broken circular dependency around freq_ctr.h. Now that this one is
solved, let's get rid of this copy and use the original ones instead.
This commit is contained in:
Willy Tarreau 2020-06-01 12:35:03 +02:00
parent 6634794992
commit 606135ac88
2 changed files with 10 additions and 45 deletions

View File

@ -29,6 +29,7 @@
#include <unistd.h> #include <unistd.h>
#include <haproxy/api.h> #include <haproxy/api.h>
#include <haproxy/freq_ctr.h>
#include <haproxy/list.h> #include <haproxy/list.h>
#include <haproxy/thread.h> #include <haproxy/thread.h>
@ -57,6 +58,8 @@
#define MAX_BASE_POOLS 32 #define MAX_BASE_POOLS 32
#define POOL_AVG_SAMPLES 1024
struct pool_cache_head { struct pool_cache_head {
struct list list; /* head of objects in this pool */ struct list list; /* head of objects in this pool */
size_t size; /* size of an object */ size_t size; /* size of an object */
@ -181,48 +184,10 @@ static inline ssize_t pool_get_index(const struct pool_head *pool)
return idx; return idx;
} }
/* The two functions below were copied from freq_ctr.h's swrate_add, impossible
* to use here due to include dependency hell again!
*/
#define POOL_AVG_SAMPLES 1024
static inline unsigned int pool_avg_add(unsigned int *sum, unsigned int v)
{
unsigned int new_sum, old_sum;
unsigned int n = POOL_AVG_SAMPLES;
old_sum = *sum;
do {
new_sum = old_sum - (old_sum + n - 1) / n + v;
} while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
return new_sum;
}
/* make the new value <v> count for 1/4 of the total sum */
static inline unsigned int pool_avg_bump(unsigned int *sum, unsigned int v)
{
unsigned int new_sum, old_sum;
unsigned int n = POOL_AVG_SAMPLES;
old_sum = *sum;
do {
new_sum = old_sum - (old_sum + 3) / 4;
new_sum += (n * v + 3) / 4;
} while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
return new_sum;
}
static inline unsigned int pool_avg(unsigned int sum)
{
unsigned int n = POOL_AVG_SAMPLES;
return (sum + n - 1) / n;
}
/* returns true if the pool is considered to have too many free objects */ /* returns true if the pool is considered to have too many free objects */
static inline int pool_is_crowded(const struct pool_head *pool) static inline int pool_is_crowded(const struct pool_head *pool)
{ {
return pool->allocated >= pool_avg(pool->needed_avg + pool->needed_avg / 4) && return pool->allocated >= swrate_avg(pool->needed_avg + pool->needed_avg / 4, POOL_AVG_SAMPLES) &&
(int)(pool->allocated - pool->used) >= pool->minavail; (int)(pool->allocated - pool->used) >= pool->minavail;
} }
@ -351,7 +316,7 @@ static inline void __pool_free(struct pool_head *pool, void *ptr)
} while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr)); } while (!_HA_ATOMIC_CAS(&pool->free_list, &free_list, ptr));
__ha_barrier_atomic_store(); __ha_barrier_atomic_store();
} }
pool_avg_add(&pool->needed_avg, pool->used); swrate_add(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used);
} }
/* frees an object to the local cache, possibly pushing oldest objects to the /* frees an object to the local cache, possibly pushing oldest objects to the
@ -566,7 +531,7 @@ static inline void pool_free(struct pool_head *pool, void *ptr)
*POOL_LINK(pool, ptr) = (void *)pool->free_list; *POOL_LINK(pool, ptr) = (void *)pool->free_list;
pool->free_list = (void *)ptr; pool->free_list = (void *)ptr;
} }
pool_avg_add(&pool->needed_avg, pool->used); swrate_add(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used);
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
#else /* release the entry for real to detect use after free */ #else /* release the entry for real to detect use after free */
/* ensure we crash on double free or free of a const area*/ /* ensure we crash on double free or free of a const area*/
@ -575,7 +540,7 @@ static inline void pool_free(struct pool_head *pool, void *ptr)
HA_SPIN_LOCK(POOL_LOCK, &pool->lock); HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
pool->allocated--; pool->allocated--;
pool->used--; pool->used--;
pool_avg_add(&pool->needed_avg, pool->used); swrate_add(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used);
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
#endif /* DEBUG_UAF */ #endif /* DEBUG_UAF */
} }

View File

@ -170,7 +170,7 @@ void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
return NULL; return NULL;
} }
pool_avg_bump(&pool->needed_avg, pool->allocated); swrate_add_scaled(&pool->needed_avg, POOL_AVG_SAMPLES, pool->allocated, POOL_AVG_SAMPLES/4);
ptr = malloc(size + POOL_EXTRA); ptr = malloc(size + POOL_EXTRA);
if (!ptr) { if (!ptr) {
@ -338,7 +338,7 @@ void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail)
return NULL; return NULL;
} }
pool_avg_bump(&pool->needed_avg, pool->allocated); swrate_add_scaled(&pool->needed_avg, POOL_AVG_SAMPLES, pool->allocated, POOL_AVG_SAMPLES/4);
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
ptr = pool_alloc_area(pool->size + POOL_EXTRA); ptr = pool_alloc_area(pool->size + POOL_EXTRA);
#ifdef DEBUG_MEMORY_POOLS #ifdef DEBUG_MEMORY_POOLS
@ -486,7 +486,7 @@ void dump_pools_to_trash()
chunk_appendf(&trash, " - Pool %s (%u bytes) : %u allocated (%u bytes), %u used, needed_avg %u, %u failures, %u users, @%p=%02d%s\n", chunk_appendf(&trash, " - Pool %s (%u bytes) : %u allocated (%u bytes), %u used, needed_avg %u, %u failures, %u users, @%p=%02d%s\n",
entry->name, entry->size, entry->allocated, entry->name, entry->size, entry->allocated,
entry->size * entry->allocated, entry->used, entry->size * entry->allocated, entry->used,
pool_avg(entry->needed_avg), entry->failed, swrate_avg(entry->needed_avg, POOL_AVG_SAMPLES), entry->failed,
entry->users, entry, (int)pool_get_index(entry), entry->users, entry, (int)pool_get_index(entry),
(entry->flags & MEM_F_SHARED) ? " [SHARED]" : ""); (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");