blob: abc3203432bcfbbeba8ef03fa0a38a76b63476f4 [file] [log] [blame]
Willy Tarreau36467772020-05-30 18:27:03 +02001/*
2 * include/haproxy/pool-t.h
3 * Memory pools configuration and type definitions.
4 *
5 * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _HAPROXY_POOL_T_H
23#define _HAPROXY_POOL_T_H
24
Willy Tarreauc03d7632020-06-29 10:11:24 +020025#include <haproxy/api-t.h>
26#include <haproxy/list-t.h>
27#include <haproxy/thread-t.h>
Willy Tarreau36467772020-05-30 18:27:03 +020028
Willy Tarreau2d6f6282021-04-15 16:24:00 +020029/* Pools are always enabled unless explicitly disabled. When disabled, the
30 * calls are directly passed to the underlying OS functions.
31 */
32#if !defined(DEBUG_NO_POOLS) && !defined(DEBUG_UAF) && !defined(DEBUG_FAIL_ALLOC)
33#define CONFIG_HAP_POOLS
34#endif
35
Willy Tarreau36467772020-05-30 18:27:03 +020036/* On architectures supporting threads and double-word CAS, we can implement
37 * lock-less memory pools. This isn't supported for debugging modes however.
38 */
Willy Tarreaub8498e92021-04-18 10:23:02 +020039#if defined(USE_THREAD) && defined(HA_HAVE_CAS_DW) && defined(CONFIG_HAP_POOLS) && !defined(DEBUG_NO_LOCKLESS_POOLS)
Willy Tarreau36467772020-05-30 18:27:03 +020040#define CONFIG_HAP_LOCKLESS_POOLS
41#endif
42
Willy Tarreau0bae0752021-03-02 20:05:09 +010043/* On modern architectures with many threads, a fast memory allocator, and
44 * local pools, the global pools with their single list can be way slower than
45 * the standard allocator which already has its own per-thread arenas. In this
46 * case we disable global pools. The global pools may still be enforced
47 * using CONFIG_HAP_GLOBAL_POOLS though.
48 */
Willy Tarreau53a7fe42021-04-15 16:43:18 +020049#if defined(USE_THREAD) && defined(HA_HAVE_FAST_MALLOC) && !defined(CONFIG_HAP_GLOBAL_POOLS)
Willy Tarreau0bae0752021-03-02 20:05:09 +010050#define CONFIG_HAP_NO_GLOBAL_POOLS
51#endif
52
Willy Tarreau36467772020-05-30 18:27:03 +020053#define MEM_F_SHARED 0x1
Willy Tarreau36467772020-05-30 18:27:03 +020054#define MEM_F_EXACT 0x2
55
56/* By default, free objects are linked by a pointer stored at the beginning of
57 * the memory area. When DEBUG_MEMORY_POOLS is set, the allocated area is
58 * inflated by the size of a pointer so that the link is placed at the end
59 * of the objects. Hence free objects in pools remain intact. In addition,
60 * this location is used to keep a pointer to the pool the object was
61 * allocated from, and verify it's freed into the appropriate one.
62 */
63#ifdef DEBUG_MEMORY_POOLS
64#define POOL_EXTRA (sizeof(void *))
65#define POOL_LINK(pool, item) (void **)(((char *)(item)) + ((pool)->size))
66#else
67#define POOL_EXTRA (0)
68#define POOL_LINK(pool, item) ((void **)(item))
69#endif
70
Willy Tarreau36467772020-05-30 18:27:03 +020071#define POOL_AVG_SAMPLES 1024
72
Willy Tarreaude749a92021-03-22 20:54:15 +010073/* possible flags for __pool_alloc() */
74#define POOL_F_NO_POISON 0x00000001 // do not poison the area
75#define POOL_F_MUST_ZERO 0x00000002 // zero the returned area
Willy Tarreau207c0952021-04-17 16:00:08 +020076#define POOL_F_NO_FAIL 0x00000004 // do not randomly fail
Willy Tarreaude749a92021-03-22 20:54:15 +010077
Willy Tarreau36467772020-05-30 18:27:03 +020078
79struct pool_cache_head {
80 struct list list; /* head of objects in this pool */
Willy Tarreau36467772020-05-30 18:27:03 +020081 unsigned int count; /* number of objects in this pool */
Willy Tarreau9f3129e2021-04-17 00:31:38 +020082} THREAD_ALIGNED(64);
Willy Tarreau36467772020-05-30 18:27:03 +020083
84struct pool_cache_item {
85 struct list by_pool; /* link to objects in this pool */
86 struct list by_lru; /* link to objects by LRU order */
87};
88
89struct pool_free_list {
90 void **free_list;
91 uintptr_t seq;
92};
93
94/* Note below, in case of lockless pools, we still need the lock only for
95 * the flush() operation.
96 */
97struct pool_head {
98 void **free_list;
99#ifdef CONFIG_HAP_LOCKLESS_POOLS
100 uintptr_t seq;
101#endif
102 __decl_thread(HA_SPINLOCK_T lock); /* the spin lock */
103 unsigned int used; /* how many chunks are currently in use */
104 unsigned int needed_avg;/* floating indicator between used and allocated */
105 unsigned int allocated; /* how many chunks have been allocated */
106 unsigned int limit; /* hard limit on the number of chunks */
107 unsigned int minavail; /* how many chunks are expected to be used */
108 unsigned int size; /* chunk size */
109 unsigned int flags; /* MEM_F_* */
110 unsigned int users; /* number of pools sharing this zone */
111 unsigned int failed; /* failed allocations */
112 struct list list; /* list of all known pools */
113 char name[12]; /* name of the pool */
Willy Tarreau2d6f6282021-04-15 16:24:00 +0200114#ifdef CONFIG_HAP_POOLS
Willy Tarreau9f3129e2021-04-17 00:31:38 +0200115 struct pool_cache_head cache[MAX_THREADS]; /* pool caches */
116#endif
Willy Tarreau36467772020-05-30 18:27:03 +0200117} __attribute__((aligned(64)));
118
119#endif /* _HAPROXY_POOL_T_H */
120
121/*
122 * Local variables:
123 * c-indent-level: 8
124 * c-basic-offset: 8
125 * End:
126 */