Pool.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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
9/*
10 * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins
11 */
12
13#include "squid.h"
14#include "mem/Pool.h"
15#include "mem/PoolChunked.h"
16#include "mem/PoolMalloc.h"
17#include "mem/Stats.h"
18
19#include <cassert>
20#include <cstring>
21
22extern time_t squid_curtime;
23
25
28{
29 // We must initialize on first use (which may happen during static
30 // initialization) and preserve until the last user is gone (which
31 // may happen long after main() exit). We currently preserve forever.
32 static MemPools *Instance = new MemPools;
33 return *Instance;
34}
35
36/* Change the default value of defaultIsChunked to override
37 * all pools - including those used before main() starts where
38 * MemPools::GetInstance().setDefaultPoolChunking() can be called.
39 */
41{
42 if (char *cfg = getenv("MEMPOOLS"))
43 defaultIsChunked = atoi(cfg);
44}
45
47MemPools::create(const char *label, size_t obj_size)
48{
49 // TODO Use ref-counted Pointer for pool lifecycle management
50 // that is complicated by all the global static pool pointers.
51 // For now leak these Allocator descendants on shutdown.
52
53 Mem::Allocator *newPool;
55 newPool = new MemPoolChunked(label, obj_size);
56 else
57 newPool = new MemPoolMalloc(label, obj_size);
58 pools.push_back(newPool);
59 return pools.back();
60}
61
62void
64{
65 defaultIsChunked = aBool;
66}
67
68/*
69 * Updates all pool counters, and recreates TheMeter totals from all pools
70 */
71void
73{
74 // Does reset of the historic gb_* counters in TheMeter.
75 // This is okay as they get regenerated from pool historic counters.
77
78 for (const auto pool: pools) {
79 // ensure the pool's meter reflect the latest calls
80 pool->flushCounters();
81
82 // Accumulate current volumes (in bytes) across all pools.
83 TheMeter.alloc += pool->meter.alloc.currentLevel() * pool->objectSize;
84 TheMeter.inuse += pool->meter.inuse.currentLevel() * pool->objectSize;
85 TheMeter.idle += pool->meter.idle.currentLevel() * pool->objectSize;
86 // We cannot calculate the global peak because individual pools peak at different times.
87
88 // regenerate gb_* values from original pool stats
89 TheMeter.gb_allocated += pool->meter.gb_allocated;
90 TheMeter.gb_saved += pool->meter.gb_saved;
91 TheMeter.gb_freed += pool->meter.gb_freed;
92 }
93}
94
95/*
96 * Returns all cached frees to their home chunks
97 * If chunks unreferenced age is over, destroys Idle chunk
98 * Flushes meters for a pool
99 * If pool is not specified, iterates through all pools.
100 * When used for all pools, if new_idle_limit is above -1, new
101 * idle memory limit is set before Cleanup. This allows to shrink
102 * memPool memory usage to specified minimum.
103 */
104void
105MemPools::clean(time_t maxage)
106{
107 flushMeters();
108 if (idleLimit() < 0) // no limit to enforce
109 return;
110
111 int shift = 1;
113 maxage = shift = 0;
114
115 for (const auto pool: pools) {
116 if (pool->idleTrigger(shift))
117 pool->clean(maxage);
118 }
119}
static CodeContext::Pointer & Instance()
guarantees the forever existence of the pointer, starting from the first use
Definition: CodeContext.cc:26
time_t squid_curtime
Definition: stub_libtime.cc:20
Mem::PoolMeter TheMeter
memory usage totals as of latest MemPools::flushMeters() event
Definition: Pool.cc:24
Definition: Pool.h:61
bool defaultIsChunked
Definition: Pool.h:113
void setDefaultPoolChunking(bool const &)
Definition: Pool.cc:63
MemPools()
Definition: Pool.cc:40
ssize_t idleLimit() const
Definition: Pool.h:80
std::list< Mem::Allocator * > pools
Definition: Pool.h:112
void flushMeters()
Definition: Pool.cc:72
Mem::Allocator * create(const char *, size_t)
Definition: Pool.cc:47
static MemPools & GetInstance()
Definition: Pool.cc:27
void clean(time_t maxage)
Definition: Pool.cc:105
ssize_t currentLevel() const
Definition: Meter.h:26
Meter idle
Definition: Meter.h:91
void flush()
flush counters back to 0, but leave historic peak records
Definition: Meter.h:79
mgb_t gb_allocated
Definition: Meter.h:94
Meter alloc
Definition: Meter.h:89
Meter inuse
Definition: Meter.h:90
mgb_t gb_saved
Definition: Meter.h:98
mgb_t gb_freed
Definition: Meter.h:101
code related to Squid Instance and PID file management
Definition: Instance.h:17

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors