From d0ef43969922f19a401e1d0406eb882b037ffb94 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 2 Jun 2020 09:38:52 +0200 Subject: [PATCH] REORG: include: move common/memory.h to haproxy/pool.h Now the file is ready to be stored into its final destination. A few minor reorderings were performed to keep the file properly organized, making the various sections more visible (cache & lockless). In addition and to stay consistent, memory.c was renamed to pool.c. --- Makefile | 2 +- .../prometheus-exporter/service-prometheus.c | 2 +- include/common/buffer.h | 2 +- include/common/chunk.h | 2 +- include/common/hpack-tbl.h | 2 +- include/{common/memory.h => haproxy/pool.h} | 117 ++++++++---------- include/proto/applet.h | 2 +- include/proto/connection.h | 2 +- include/proto/log.h | 2 +- include/proto/obj_type.h | 2 +- include/proto/queue.h | 2 +- include/proto/session.h | 2 +- include/proto/stream.h | 2 +- include/proto/task.h | 2 +- src/action.c | 2 +- src/buffer.c | 2 +- src/cfgparse.c | 2 +- src/compression.c | 2 +- src/flt_spoe.c | 2 +- src/haproxy.c | 2 +- src/http_acl.c | 2 +- src/http_act.c | 2 +- src/http_conv.c | 2 +- src/http_fetch.c | 2 +- src/http_rules.c | 2 +- src/pipe.c | 2 +- src/{memory.c => pool.c} | 2 +- src/proxy.c | 2 +- src/queue.c | 2 +- src/session.c | 2 +- src/stats.c | 2 +- src/stick_table.c | 2 +- src/stream.c | 2 +- src/task.c | 2 +- 34 files changed, 82 insertions(+), 101 deletions(-) rename include/{common/memory.h => haproxy/pool.h} (87%) rename src/{memory.c => pool.c} (99%) diff --git a/Makefile b/Makefile index f7155d02a..6e4cad605 100644 --- a/Makefile +++ b/Makefile @@ -802,7 +802,7 @@ OBJS = src/mux_h2.o src/stream.o src/mux_fcgi.o src/cfgparse-listen.o \ src/payload.o src/vars.o src/debug.o src/mux_pt.o src/http_act.o \ src/h2.o src/queue.o src/fd.o src/proto_uxst.o src/lb_chash.o \ src/ring.o src/frontend.o src/raw_sock.o src/xprt_handshake.o \ - src/htx.o src/memory.o src/applet.o src/channel.o src/signal.o \ + src/htx.o src/pool.o src/applet.o src/channel.o src/signal.o \ src/lb_fwrr.o src/ev_select.o src/sink.o src/http_conv.o \ src/proto_sockpair.o src/mworker-prog.o src/activity.o src/lb_fwlc.o \ src/http.o src/lb_fas.o src/uri_auth.o src/thread.o src/regex.o \ diff --git a/contrib/prometheus-exporter/service-prometheus.c b/contrib/prometheus-exporter/service-prometheus.c index 6afa43feb..fe3431ccb 100644 --- a/contrib/prometheus-exporter/service-prometheus.c +++ b/contrib/prometheus-exporter/service-prometheus.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include diff --git a/include/common/buffer.h b/include/common/buffer.h index 132cbff6c..e9aae2585 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include diff --git a/include/common/chunk.h b/include/common/chunk.h index 46dfae70d..279b80224 100644 --- a/include/common/chunk.h +++ b/include/common/chunk.h @@ -28,7 +28,7 @@ #include #include #include -#include +#include extern struct pool_head *pool_head_trash; diff --git a/include/common/hpack-tbl.h b/include/common/hpack-tbl.h index 1d2a8bd5f..07e0a9fa3 100644 --- a/include/common/hpack-tbl.h +++ b/include/common/hpack-tbl.h @@ -31,7 +31,7 @@ #include #include #include -#include +#include /* Dynamic Headers Table, usable for tables up to 4GB long and values of 64kB-1. * The model can be improved by using offsets relative to the table entry's end diff --git a/include/common/memory.h b/include/haproxy/pool.h similarity index 87% rename from include/common/memory.h rename to include/haproxy/pool.h index d61226352..a62d06efa 100644 --- a/include/common/memory.h +++ b/include/haproxy/pool.h @@ -1,8 +1,8 @@ /* - * include/common/memory.h + * include/haproxy/pool.h * Memory management definitions.. * - * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu + * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef _COMMON_MEMORY_H -#define _COMMON_MEMORY_H +#ifndef _HAPROXY_POOL_H +#define _HAPROXY_POOL_H #include @@ -31,36 +31,6 @@ #include #include -#ifdef CONFIG_HAP_LOCAL_POOLS -extern struct pool_head pool_base_start[MAX_BASE_POOLS]; -extern unsigned int pool_base_count; -extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS]; -extern struct list pool_lru_head[MAX_THREADS]; -extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */ -extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */ -#endif - -/* poison each newly allocated area with this byte if >= 0 */ -extern int mem_poison_byte; - -/* Allocates new entries for pool until there are at least + 1 - * available, then returns the last one for immediate use, so that at least - * 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); -void *pool_refill_alloc(struct pool_head *pool, unsigned int avail); - -/* Try to find an existing shared pool with the same characteristics and - * returns it, otherwise creates this one. NULL is returned if no memory - * is available for a new creation. - */ -struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); -void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size); - /* This registers a call to create_pool_callback(ptr, name, size) */ #define REGISTER_POOL(ptr, name, size) \ INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size)) @@ -75,48 +45,23 @@ void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size) static struct pool_head *(ptr); \ REGISTER_POOL(&ptr, name, size) -/* Dump statistics on pools usage. - */ +/* poison each newly allocated area with this byte if >= 0 */ +extern int mem_poison_byte; + +void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail); +void *pool_refill_alloc(struct pool_head *pool, unsigned int avail); void dump_pools_to_trash(); void dump_pools(void); int pool_total_failures(); unsigned long pool_total_allocated(); unsigned long pool_total_used(); - -/* - * This function frees whatever can be freed in pool . - */ void pool_flush(struct pool_head *pool); - -/* - * This function frees whatever can be freed in all pools, but respecting - * the minimum thresholds imposed by owners. - * - * is used when pool_gc is called to release resources to allocate - * an element in __pool_refill_alloc. It is important because is - * already locked, so we need to skip the lock here. - */ void pool_gc(struct pool_head *pool_ctx); - -/* - * This function destroys a pull by freeing it completely. - * This should be called only under extreme circumstances. - */ +struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); +void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size); void *pool_destroy(struct pool_head *pool); void pool_destroy_all(); -/* returns the pool index for pool , or -1 if this pool has no index */ -static inline ssize_t pool_get_index(const struct pool_head *pool) -{ -#ifdef CONFIG_HAP_LOCAL_POOLS - size_t idx; - - idx = pool - pool_base_start; - if (idx < MAX_BASE_POOLS) - return idx; -#endif - return -1; -} /* returns true if the pool is considered to have too many free objects */ static inline int pool_is_crowded(const struct pool_head *pool) @@ -125,9 +70,31 @@ static inline int pool_is_crowded(const struct pool_head *pool) (int)(pool->allocated - pool->used) >= pool->minavail; } + #ifdef CONFIG_HAP_LOCAL_POOLS + +/****************** Thread-local cache management ******************/ + +extern struct pool_head pool_base_start[MAX_BASE_POOLS]; +extern unsigned int pool_base_count; +extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS]; +extern struct list pool_lru_head[MAX_THREADS]; +extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */ +extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */ + void pool_evict_from_cache(); +/* returns the pool index for pool , or -1 if this pool has no index */ +static inline ssize_t pool_get_index(const struct pool_head *pool) +{ + size_t idx; + + idx = pool - pool_base_start; + if (idx < MAX_BASE_POOLS) + return idx; + return -1; +} + /* Tries to retrieve an object from the local pool cache corresponding to pool * . Returns NULL if none is available. */ @@ -175,9 +142,19 @@ static inline void pool_put_to_cache(struct pool_head *pool, void *ptr, ssize_t if (unlikely(pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE)) pool_evict_from_cache(pool, ptr, idx); } + +#else // CONFIG_HAP_LOCAL_POOLS + +/* always return index -1 when thread-local pools are disabled */ +#define pool_get_index(pool) ((ssize_t)-1) + #endif // CONFIG_HAP_LOCAL_POOLS + #ifdef CONFIG_HAP_LOCKLESS_POOLS + +/****************** Lockless pools implementation ******************/ + /* * Returns a pointer to type taken from the pool if * available, otherwise returns NULL. No malloc() is attempted, and poisonning @@ -232,6 +209,9 @@ static inline void __pool_free(struct pool_head *pool, void *ptr) } #else /* CONFIG_HAP_LOCKLESS_POOLS */ + +/****************** Locked pools implementation ******************/ + /* * Returns a pointer to type taken from the pool if * available, otherwise returns NULL. No malloc() is attempted, and poisonning @@ -284,6 +264,8 @@ static inline void __pool_free(struct pool_head *pool, void *ptr) #endif /* CONFIG_HAP_LOCKLESS_POOLS */ +/****************** Common high-level code ******************/ + static inline void *pool_get_first(struct pool_head *pool) { void *p; @@ -384,8 +366,7 @@ static inline void pool_free(struct pool_head *pool, void *ptr) } } - -#endif /* _COMMON_MEMORY_H */ +#endif /* _HAPROXY_POOL_H */ /* * Local variables: diff --git a/include/proto/applet.h b/include/proto/applet.h index aaa49038b..80744ba13 100644 --- a/include/proto/applet.h +++ b/include/proto/applet.h @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include #include diff --git a/include/proto/connection.h b/include/proto/connection.h index 7ffe1fd8c..de976d7a7 100644 --- a/include/proto/connection.h +++ b/include/proto/connection.h @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include #include diff --git a/include/proto/log.h b/include/proto/log.h index ef792c81a..48802dc10 100644 --- a/include/proto/log.h +++ b/include/proto/log.h @@ -27,7 +27,7 @@ #include #include -#include +#include #include #include diff --git a/include/proto/obj_type.h b/include/proto/obj_type.h index f4816b137..402d08413 100644 --- a/include/proto/obj_type.h +++ b/include/proto/obj_type.h @@ -23,7 +23,7 @@ #define _PROTO_OBJ_TYPE_H #include -#include +#include #include #include #include diff --git a/include/proto/queue.h b/include/proto/queue.h index 9bec9b42b..d4da40c73 100644 --- a/include/proto/queue.h +++ b/include/proto/queue.h @@ -23,7 +23,7 @@ #define _PROTO_QUEUE_H #include -#include +#include #include #include diff --git a/include/proto/session.h b/include/proto/session.h index 522e43fe5..866217ec2 100644 --- a/include/proto/session.h +++ b/include/proto/session.h @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include diff --git a/include/proto/stream.h b/include/proto/stream.h index b821cbae2..80c56ef88 100644 --- a/include/proto/stream.h +++ b/include/proto/stream.h @@ -23,7 +23,7 @@ #define _PROTO_STREAM_H #include -#include +#include #include #include #include diff --git a/include/proto/task.h b/include/proto/task.h index d5cded3cf..5a3d0210d 100644 --- a/include/proto/task.h +++ b/include/proto/task.h @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/action.c b/src/action.c index cb897b66a..1125eca1e 100644 --- a/src/action.c +++ b/src/action.c @@ -11,7 +11,7 @@ */ #include -#include +#include #include #include diff --git a/src/buffer.c b/src/buffer.c index c8d7a69f2..ec07e76e6 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -16,7 +16,7 @@ #include #include -#include +#include #include diff --git a/src/cfgparse.c b/src/cfgparse.c index a1917fa23..373a32194 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/compression.c b/src/compression.c index 154d629f8..45471c333 100644 --- a/src/compression.c +++ b/src/compression.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/flt_spoe.c b/src/flt_spoe.c index df67a29d7..52771ef13 100644 --- a/src/flt_spoe.c +++ b/src/flt_spoe.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/haproxy.c b/src/haproxy.c index a2ceeec57..917b14fc0 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -85,7 +85,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/http_acl.c b/src/http_acl.c index fe71658da..c55c72099 100644 --- a/src/http_acl.c +++ b/src/http_acl.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/http_act.c b/src/http_act.c index da57d8fe0..8e394cce9 100644 --- a/src/http_act.c +++ b/src/http_act.c @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/http_conv.c b/src/http_conv.c index 109952145..b82883a48 100644 --- a/src/http_conv.c +++ b/src/http_conv.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/http_fetch.c b/src/http_fetch.c index 96d44ab60..c253c8de9 100644 --- a/src/http_fetch.c +++ b/src/http_fetch.c @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/http_rules.c b/src/http_rules.c index 6a5a5f33b..01a820ebd 100644 --- a/src/http_rules.c +++ b/src/http_rules.c @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/pipe.c b/src/pipe.c index 8c828214a..6fe7b0524 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -15,7 +15,7 @@ #include #include -#include +#include #include #include diff --git a/src/memory.c b/src/pool.c similarity index 99% rename from src/memory.c rename to src/pool.c index afab8f84f..3a4b34cf5 100644 --- a/src/memory.c +++ b/src/pool.c @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include diff --git a/src/proxy.c b/src/proxy.c index 5e29ec95c..a8ca53a31 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/queue.c b/src/queue.c index e354e0a48..43a21b1e1 100644 --- a/src/queue.c +++ b/src/queue.c @@ -70,7 +70,7 @@ */ #include -#include +#include #include #include #include diff --git a/src/session.c b/src/session.c index 3f26cd477..804416901 100644 --- a/src/session.c +++ b/src/session.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/stats.c b/src/stats.c index a08931831..0869d5499 100644 --- a/src/stats.c +++ b/src/stats.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/stick_table.c b/src/stick_table.c index 0fb0927ba..bd7f97f10 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -16,7 +16,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/stream.c b/src/stream.c index 92c1b5064..955bc4466 100644 --- a/src/stream.c +++ b/src/stream.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/task.c b/src/task.c index 96c09a388..5663abac7 100644 --- a/src/task.c +++ b/src/task.c @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include #include