]> git.ipfire.org Git - thirdparty/squid.git/blame - src/mem/Pool.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / mem / Pool.h
CommitLineData
5c193dec 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
5c193dec
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
8cfaefed
HN
9#ifndef _MEM_POOL_H_
10#define _MEM_POOL_H_
d96ceb8e 11
63be0a78 12/**
13 \defgroup MemPoolsAPI Memory Management (Memory Pool Allocator)
14 \ingroup Components
15 *
16 *\par
17 * MemPools are a pooled memory allocator running on top of malloc(). It's
18 * purpose is to reduce memory fragmentation and provide detailed statistics
19 * on memory consumption.
20 *
21 \par
22 * Preferably all memory allocations in Squid should be done using MemPools
23 * or one of the types built on top of it (i.e. cbdata).
24 *
25 \note Usually it is better to use cbdata types as these gives you additional
26 * safeguards in references and typechecking. However, for high usage pools where
27 * the cbdata functionality of cbdata is not required directly using a MemPool
28 * might be the way to go.
29 */
30
9663db1c 31#include "mem/Meter.h"
b001e822 32#include "splay.h"
ed6e9fb9 33#include "util.h"
d96ceb8e 34
35#if HAVE_GNUMALLOC_H
36#include <gnumalloc.h>
482aa790 37#elif HAVE_MALLOC_H
d96ceb8e 38#include <malloc.h>
39#endif
d96ceb8e 40#if HAVE_MEMORY_H
41#include <memory.h>
42#endif
43
44#if !M_MMAP_MAX
45#if USE_DLMALLOC
46#define M_MMAP_MAX -4
47#endif
48#endif
49
63be0a78 50/// \ingroup MemPoolsAPI
c63f0322 51#define toMB(size) ( ((double) size) / ((double)(1024*1024)) )
63be0a78 52/// \ingroup MemPoolsAPI
d96ceb8e 53#define toKB(size) ( (size + 1024 - 1) / 1024 )
54
63be0a78 55/// \ingroup MemPoolsAPI
d96ceb8e 56#define MEM_PAGE_SIZE 4096
63be0a78 57/// \ingroup MemPoolsAPI
d96ceb8e 58#define MEM_MIN_FREE 32
63be0a78 59/// \ingroup MemPoolsAPI
f53969cc 60#define MEM_MAX_FREE 65535 /* unsigned short is max number of items per chunk */
d96ceb8e 61
b001e822 62class MemImplementingAllocator;
b001e822 63class MemPoolStats;
83d8f9f4 64
63be0a78 65/// \ingroup MemPoolsAPI
66/// \todo Kill this typedef for C++
d96ceb8e 67typedef struct _MemPoolGlobalStats MemPoolGlobalStats;
83d8f9f4 68
63be0a78 69/// \ingroup MemPoolsAPI
b001e822 70class MemPoolIterator
83d8f9f4 71{
c5dd4956 72public:
b001e822 73 MemImplementingAllocator *pool;
d96ceb8e 74 MemPoolIterator * next;
75};
76
63be0a78 77/**
78 \ingroup MemPoolsAPI
79 * Object to track per-pool cumulative counters
80 */
b001e822 81class mgb_t
83d8f9f4 82{
c5dd4956
AJ
83public:
84 mgb_t() : count(0), bytes(0) {}
d96ceb8e 85 double count;
86 double bytes;
b001e822 87};
d96ceb8e 88
63be0a78 89/**
90 \ingroup MemPoolsAPI
91 * Object to track per-pool memory usage (alloc = inuse+idle)
92 */
b001e822 93class MemPoolMeter
83d8f9f4 94{
c5dd4956 95public:
903a6eec 96 MemPoolMeter();
b001e822 97 void flush();
9663db1c
AJ
98
99 Mem::Meter alloc;
100 Mem::Meter inuse;
101 Mem::Meter idle;
63be0a78 102
63be0a78 103 /** history Allocations */
903a6eec
HN
104 mgb_t gb_allocated;
105 mgb_t gb_oallocated;
106
107 /** account Saved Allocations */
108 mgb_t gb_saved;
63be0a78 109
110 /** account Free calls */
111 mgb_t gb_freed;
d96ceb8e 112};
113
b001e822 114class MemImplementingAllocator;
115
63be0a78 116/// \ingroup MemPoolsAPI
c5dd4956 117class MemPools
b001e822 118{
c5dd4956 119public:
b001e822 120 static MemPools &GetInstance();
121 MemPools();
b001e822 122 void flushMeters();
63be0a78 123
124 /**
f53969cc
SM
125 \param label Name for the pool. Displayed in stats.
126 \param obj_size Size of elements in MemPool.
63be0a78 127 */
b001e822 128 MemImplementingAllocator * create(const char *label, size_t obj_size);
63be0a78 129
63be0a78 130 /**
131 * Sets upper limit in bytes to amount of free ram kept in pools. This is
132 * not strict upper limit, but a hint. When MemPools are over this limit,
133 * totally free chunks are immediately considered for release. Otherwise
134 * only chunks that have not been referenced for a long time are checked.
135 */
89646bd7
HN
136 void setIdleLimit(ssize_t new_idle_limit);
137 ssize_t idleLimit() const;
63be0a78 138
139 /**
140 \par
141 * Main cleanup handler. For MemPools to stay within upper idle limits,
142 * this function needs to be called periodically, preferrably at some
143 * constant rate, eg. from Squid event. It looks through all pools and
144 * chunks, cleans up internal states and checks for releasable chunks.
145 *
146 \par
147 * Between the calls to this function objects are placed onto internal
148 * cache instead of returning to their home chunks, mainly for speedup
149 * purpose. During that time state of chunk is not known, it is not
150 * known whether chunk is free or in use. This call returns all objects
151 * to their chunks and restores consistency.
152 *
153 \par
154 * Should be called relatively often, as it sorts chunks in suitable
155 * order as to reduce free memory fragmentation and increase chunk
156 * utilisation.
157 * Suitable frequency for cleanup is in range of few tens of seconds to
158 * few minutes, depending of memory activity.
159 *
160 \todo DOCS: Re-write this shorter!
161 *
162 \param maxage Release all totally idle chunks that
163 * have not been referenced for maxage seconds.
164 */
b001e822 165 void clean(time_t maxage);
63be0a78 166
b001e822 167 void setDefaultPoolChunking(bool const &);
168 MemImplementingAllocator *pools;
89646bd7 169 ssize_t mem_idle_limit;
b001e822 170 int poolCount;
171 bool defaultIsChunked;
b001e822 172};
173
63be0a78 174/**
175 \ingroup MemPoolsAPI
176 * a pool is a [growing] space for objects of the same size
177 */
b001e822 178class MemAllocator
179{
180public:
181 MemAllocator (char const *aLabel);
182 virtual ~MemAllocator() {}
63be0a78 183
184 /**
f53969cc
SM
185 \param stats Object to be filled with statistical data about pool.
186 \retval Number of objects in use, ie. allocated.
63be0a78 187 */
8cfaefed 188 virtual int getStats(MemPoolStats * stats, int accumulate = 0) = 0;
63be0a78 189
b001e822 190 virtual MemPoolMeter const &getMeter() const = 0;
63be0a78 191
192 /**
193 * Allocate one element from the pool
194 */
b001e822 195 virtual void *alloc() = 0;
63be0a78 196
197 /**
198 * Free a element allocated by MemAllocator::alloc()
199 */
dc47f531 200 virtual void freeOne(void *) = 0;
63be0a78 201
b001e822 202 virtual char const *objectType() const;
203 virtual size_t objectSize() const = 0;
9f9e06f3 204 virtual int getInUseCount() = 0;
3b08e399 205 void zeroBlocks(bool doIt) {doZero = doIt;}
a3efa961 206 int inUseCount();
63be0a78 207
208 /**
209 * Allows you tune chunk size of pooling. Objects are allocated in chunks
210 * instead of individually. This conserves memory, reduces fragmentation.
211 * Because of that memory can be freed also only in chunks. Therefore
212 * there is tradeoff between memory conservation due to chunking and free
213 * memory fragmentation.
214 *
215 \note As a general guideline, increase chunk size only for pools that keep
216 * very many items for relatively long time.
217 */
ced8def3 218 virtual void setChunkSize(size_t) {}
cb6d4984 219
63be0a78 220 /**
f53969cc 221 \param minSize Minimum size needed to be allocated.
63be0a78 222 \retval n Smallest size divisible by sizeof(void*)
223 */
cb6d4984 224 static size_t RoundedSize(size_t minSize);
63be0a78 225
60da11d3 226protected:
3b08e399
AJ
227 /** Whether to zero memory on initial allocation and on return to the pool.
228 *
229 * We do this on some pools because many object constructors are/were incomplete
230 * and we are afraid some code may use the object after free.
231 * These probems are becoming less common, so when possible set this to false.
232 */
233 bool doZero;
63be0a78 234
b001e822 235private:
236 const char *label;
237};
238
63be0a78 239/// \ingroup MemPoolsAPI
b001e822 240class MemImplementingAllocator : public MemAllocator
241{
c5dd4956 242public:
b001e822 243 MemImplementingAllocator(char const *aLabel, size_t aSize);
2be7332c 244 virtual ~MemImplementingAllocator();
b001e822 245 virtual MemPoolMeter const &getMeter() const;
246 virtual MemPoolMeter &getMeter();
247 virtual void flushMetersFull();
248 virtual void flushMeters();
63be0a78 249
250 /**
251 * Allocate one element from the pool
252 */
b001e822 253 virtual void *alloc();
63be0a78 254
255 /**
256 * Free a element allocated by MemImplementingAllocator::alloc()
257 */
dc47f531 258 virtual void freeOne(void *);
63be0a78 259
b001e822 260 virtual bool idleTrigger(int shift) const = 0;
261 virtual void clean(time_t maxage) = 0;
b001e822 262 virtual size_t objectSize() const;
9f9e06f3 263 virtual int getInUseCount() = 0;
c5dd4956 264protected:
b001e822 265 virtual void *allocate() = 0;
2be7332c 266 virtual void deallocate(void *, bool aggressive) = 0;
b001e822 267 MemPoolMeter meter;
8cfaefed 268 int memPID;
c5dd4956 269public:
b001e822 270 MemImplementingAllocator *next;
c5dd4956 271public:
b001e822 272 size_t alloc_calls;
273 size_t free_calls;
903a6eec 274 size_t saved_calls;
d96ceb8e 275 size_t obj_size;
b001e822 276};
277
63be0a78 278/// \ingroup MemPoolsAPI
b001e822 279class MemPoolStats
83d8f9f4 280{
c5dd4956 281public:
b001e822 282 MemAllocator *pool;
d96ceb8e 283 const char *label;
284 MemPoolMeter *meter;
285 int obj_size;
286 int chunk_capacity;
287 int chunk_size;
288
289 int chunks_alloc;
290 int chunks_inuse;
291 int chunks_partial;
292 int chunks_free;
293
294 int items_alloc;
295 int items_inuse;
296 int items_idle;
297
298 int overhead;
299};
300
63be0a78 301/// \ingroup MemPoolsAPI
302/// \todo Classify and add constructor/destructor to initialize properly.
c5dd4956 303struct _MemPoolGlobalStats {
d96ceb8e 304 MemPoolMeter *TheMeter;
305
306 int tot_pools_alloc;
307 int tot_pools_inuse;
308 int tot_pools_mempid;
309
310 int tot_chunks_alloc;
311 int tot_chunks_inuse;
312 int tot_chunks_partial;
313 int tot_chunks_free;
314
315 int tot_items_alloc;
316 int tot_items_inuse;
317 int tot_items_idle;
318
319 int tot_overhead;
89646bd7 320 ssize_t mem_idle_limit;
d96ceb8e 321};
322
63be0a78 323/// \ingroup MemPoolsAPI
04eb0689 324#define memPoolCreate MemPools::GetInstance().create
325
d96ceb8e 326/* Allocator API */
63be0a78 327/**
328 \ingroup MemPoolsAPI
329 * Initialise iteration through all of the pools.
330 \retval Iterator for use by memPoolIterateNext() and memPoolIterateDone()
331 */
b001e822 332extern MemPoolIterator * memPoolIterate(void);
63be0a78 333
334/**
335 \ingroup MemPoolsAPI
336 * Get next pool pointer, until getting NULL pointer.
337 */
b001e822 338extern MemImplementingAllocator * memPoolIterateNext(MemPoolIterator * iter);
63be0a78 339
340/**
341 \ingroup MemPoolsAPI
342 * Should be called after finished with iterating through all pools.
343 */
b001e822 344extern void memPoolIterateDone(MemPoolIterator ** iter);
d96ceb8e 345
63be0a78 346/**
347 \ingroup MemPoolsAPI
348 \todo Stats API - not sured how to refactor yet
349 *
350 * Fills MemPoolGlobalStats with statistical data about overall
351 * usage for all pools.
352 *
353 \retval Number of pools that have at least one object in use.
354 * Ie. number of dirty pools.
355 */
b001e822 356extern int memPoolGetGlobalStats(MemPoolGlobalStats * stats);
d96ceb8e 357
63be0a78 358/// \ingroup MemPoolsAPI
b001e822 359extern int memPoolsTotalAllocated(void);
d96ceb8e 360
8cfaefed 361#endif /* _MEM_POOL_H_ */
f53969cc 362