]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Transients.cc
Fix "make check" with GCC-10 (#677)
[thirdparty/squid.git] / src / Transients.cc
CommitLineData
9a9954ba 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
9a9954ba 3 *
bbc27441
AJ
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.
9a9954ba
AR
7 */
8
bbc27441
AJ
9/* DEBUG: section 20 Storage Manager */
10
9a9954ba
AR
11#include "squid.h"
12#include "base/RunnersRegistry.h"
e4d13993 13#include "CollapsedForwarding.h"
9a9954ba
AR
14#include "HttpReply.h"
15#include "ipc/mem/Page.h"
16#include "ipc/mem/Pages.h"
17#include "MemObject.h"
9a9954ba
AR
18#include "mime_header.h"
19#include "SquidConfig.h"
20#include "SquidMath.h"
21#include "StoreStats.h"
22#include "tools.h"
e4d13993 23#include "Transients.h"
9a9954ba 24
9a9954ba 25#include <limits>
9a9954ba 26
1860fbac
AR
27/// shared memory segment path to use for Transients map
28static const SBuf MapLabel("transients_map");
9a9954ba 29
6919be24 30Transients::Transients(): map(NULL), locals(NULL)
9a9954ba 31{
9a9954ba
AR
32}
33
34Transients::~Transients()
35{
36 delete map;
6919be24 37 delete locals;
9a9954ba
AR
38}
39
40void
41Transients::init()
42{
daed75a9 43 assert(Enabled());
9a9954ba 44 const int64_t entryLimit = EntryLimit();
daed75a9 45 assert(entryLimit > 0);
9a9954ba
AR
46
47 Must(!map);
48 map = new TransientsMap(MapLabel);
49 map->cleaner = this;
b2aca62a 50 map->disableHitValidation(); // Transients lacks slices to validate
6919be24 51
8bcca0f8 52 locals = new Locals(entryLimit, 0);
9a9954ba
AR
53}
54
55void
56Transients::getStats(StoreInfoStats &stats) const
57{
58#if TRANSIENT_STATS_SUPPORTED
59 const size_t pageSize = Ipc::Mem::PageSize();
60
61 stats.mem.shared = true;
62 stats.mem.capacity =
63 Ipc::Mem::PageLimit(Ipc::Mem::PageId::cachePage) * pageSize;
64 stats.mem.size =
65 Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage) * pageSize;
66 stats.mem.count = currentCount();
67#endif
68}
69
70void
71Transients::stat(StoreEntry &e) const
72{
73 storeAppendPrintf(&e, "\n\nTransient Objects\n");
74
75 storeAppendPrintf(&e, "Maximum Size: %.0f KB\n", maxSize()/1024.0);
76 storeAppendPrintf(&e, "Current Size: %.2f KB %.2f%%\n",
77 currentSize() / 1024.0,
78 Math::doublePercent(currentSize(), maxSize()));
79
80 if (map) {
81 const int limit = map->entryLimit();
82 storeAppendPrintf(&e, "Maximum entries: %9d\n", limit);
83 if (limit > 0) {
84 storeAppendPrintf(&e, "Current entries: %" PRId64 " %.2f%%\n",
85 currentCount(), (100.0 * currentCount() / limit));
86 }
87 }
88}
89
90void
91Transients::maintain()
92{
e4d13993 93 // no lazy garbage collection needed
9a9954ba
AR
94}
95
96uint64_t
97Transients::minSize() const
98{
99 return 0; // XXX: irrelevant, but Store parent forces us to implement this
100}
101
102uint64_t
103Transients::maxSize() const
104{
105 // Squid currently does not limit the total size of all transient objects
106 return std::numeric_limits<uint64_t>::max();
107}
108
109uint64_t
110Transients::currentSize() const
111{
112 // TODO: we do not get enough information to calculate this
113 // StoreEntry should update associated stores when its size changes
114 return 0;
115}
116
117uint64_t
118Transients::currentCount() const
119{
120 return map ? map->entryCount() : 0;
121}
122
123int64_t
124Transients::maxObjectSize() const
125{
126 // Squid currently does not limit the size of a transient object
127 return std::numeric_limits<uint64_t>::max();
128}
129
130void
131Transients::reference(StoreEntry &)
132{
e4d13993 133 // no replacement policy (but the cache(s) storing the entry may have one)
9a9954ba
AR
134}
135
136bool
2745fea5 137Transients::dereference(StoreEntry &)
9a9954ba
AR
138{
139 // no need to keep e in the global store_table for us; we have our own map
140 return false;
141}
142
9a9954ba
AR
143StoreEntry *
144Transients::get(const cache_key *key)
145{
146 if (!map)
147 return NULL;
148
149 sfileno index;
1bfe9ade
AR
150 const Ipc::StoreMapAnchor *anchor = map->openForReading(key, index);
151 if (!anchor)
9a9954ba
AR
152 return NULL;
153
6919be24
AR
154 // If we already have a local entry, the store_table should have found it.
155 // Since it did not, the local entry key must have changed from public to
156 // private. We still need to keep the private entry around for syncing as
157 // its clients depend on it, but we should not allow new clients to join.
158 if (StoreEntry *oldE = locals->at(index)) {
159 debugs(20, 3, "not joining private " << *oldE);
160 assert(EBIT_TEST(oldE->flags, KEY_PRIVATE));
d1d3b4dc 161 map->closeForReadingAndFreeIdle(index);
4310f8b0 162 return nullptr;
1bfe9ade 163 }
4475555f 164
4310f8b0
EB
165 StoreEntry *e = new StoreEntry();
166 e->createMemObject();
4475555f 167 e->mem_obj->xitTable.index = index;
4310f8b0
EB
168 e->mem_obj->xitTable.io = Store::ioReading;
169 anchor->exportInto(*e);
d2a6dcba
EB
170 const bool collapsingRequired = EBIT_TEST(anchor->basics.flags, ENTRY_REQUIRES_COLLAPSING);
171 e->setCollapsingRequirement(collapsingRequired);
4310f8b0 172 // keep read lock to receive updates from others
9a9954ba
AR
173 return e;
174}
175
6919be24
AR
176StoreEntry *
177Transients::findCollapsed(const sfileno index)
178{
179 if (!map)
180 return NULL;
181
182 if (StoreEntry *oldE = locals->at(index)) {
183 debugs(20, 5, "found " << *oldE << " at " << index << " in " << MapLabel);
184 assert(oldE->mem_obj && oldE->mem_obj->xitTable.index == index);
185 return oldE;
186 }
187
188 debugs(20, 3, "no entry at " << index << " in " << MapLabel);
189 return NULL;
190}
191
d2a6dcba
EB
192void
193Transients::clearCollapsingRequirement(const StoreEntry &e)
194{
195 assert(map);
196 assert(e.hasTransients());
197 assert(isWriter(e));
198 const auto idx = e.mem_obj->xitTable.index;
199 auto &anchor = map->writeableEntry(idx);
200 if (EBIT_TEST(anchor.basics.flags, ENTRY_REQUIRES_COLLAPSING)) {
201 EBIT_CLR(anchor.basics.flags, ENTRY_REQUIRES_COLLAPSING);
202 CollapsedForwarding::Broadcast(e);
203 }
204}
205
9a9954ba 206void
4310f8b0 207Transients::monitorIo(StoreEntry *e, const cache_key *key, const Store::IoStatus direction)
9a9954ba 208{
4310f8b0
EB
209 if (!e->hasTransients()) {
210 addEntry(e, key, direction);
8253d451 211 assert(e->hasTransients());
9d4e9cfb 212 }
9a9954ba 213
4310f8b0
EB
214 const auto index = e->mem_obj->xitTable.index;
215 if (const auto old = locals->at(index)) {
216 assert(old == e);
217 } else {
218 // We do not lock e because we do not want to prevent its destruction;
219 // e is tied to us via mem_obj so we will know when it is destructed.
220 locals->at(index) = e;
9d4e9cfb 221 }
9a9954ba
AR
222}
223
8253d451 224/// creates a new Transients entry
4310f8b0
EB
225void
226Transients::addEntry(StoreEntry *e, const cache_key *key, const Store::IoStatus direction)
9a9954ba 227{
4310f8b0
EB
228 assert(e);
229 assert(e->mem_obj);
230 assert(!e->hasTransients());
9a9954ba 231
4310f8b0 232 Must(map); // configured to track transients
9a9954ba 233
4310f8b0
EB
234 sfileno index = 0;
235 Ipc::StoreMapAnchor *slot = map->openForWriting(key, index);
236 Must(slot); // no writer collisions
9a9954ba 237
8253d451 238 // set ASAP in hope to unlock the slot if something throws
4310f8b0 239 e->mem_obj->xitTable.index = index;
8253d451
AR
240 e->mem_obj->xitTable.io = Store::ioWriting;
241
242 slot->set(*e, key);
4310f8b0 243 if (direction == Store::ioWriting) {
8253d451
AR
244 // allow reading and receive remote DELETE events, but do not switch to
245 // the reading lock because transientReaders() callers want true readers
246 map->startAppending(index);
4310f8b0 247 } else {
8253d451 248 assert(direction == Store::ioReading);
4310f8b0 249 // keep the entry locked (for reading) to receive remote DELETE events
8253d451
AR
250 map->switchWritingToReading(index);
251 e->mem_obj->xitTable.io = Store::ioReading;
4310f8b0 252 }
9a9954ba
AR
253}
254
d1d3b4dc
EB
255bool
256Transients::hasWriter(const StoreEntry &e)
257{
258 if (!e.hasTransients())
259 return false;
260 return map->peekAtWriter(e.mem_obj->xitTable.index);
261}
262
9a9954ba 263void
ced8def3 264Transients::noteFreeMapSlice(const Ipc::StoreMapSliceId)
9a9954ba
AR
265{
266 // TODO: we should probably find the entry being deleted and abort it
267}
268
4475555f 269void
d2a6dcba 270Transients::status(const StoreEntry &entry, Transients::EntryStatus &entryStatus) const
4475555f
AR
271{
272 assert(map);
4310f8b0
EB
273 assert(entry.hasTransients());
274 const auto idx = entry.mem_obj->xitTable.index;
275 const auto &anchor = isWriter(entry) ?
276 map->writeableEntry(idx) : map->readableEntry(idx);
d2a6dcba
EB
277 entryStatus.abortedByWriter = anchor.writerHalted;
278 entryStatus.waitingToBeFreed = anchor.waitingToBeFreed;
279 entryStatus.collapsed = EBIT_TEST(anchor.basics.flags, ENTRY_REQUIRES_COLLAPSING);
4475555f
AR
280}
281
99921d9d
AR
282void
283Transients::completeWriting(const StoreEntry &e)
284{
4310f8b0
EB
285 assert(e.hasTransients());
286 assert(isWriter(e));
8253d451 287 map->switchWritingToReading(e.mem_obj->xitTable.index);
4310f8b0 288 e.mem_obj->xitTable.io = Store::ioReading;
99921d9d
AR
289}
290
d366a7fa
AR
291int
292Transients::readers(const StoreEntry &e) const
293{
4310f8b0 294 if (e.hasTransients()) {
d366a7fa
AR
295 assert(map);
296 return map->peekAtEntry(e.mem_obj->xitTable.index).lock.readers;
297 }
298 return 0;
299}
300
1bfe9ade 301void
4310f8b0
EB
302Transients::evictCached(StoreEntry &e)
303{
304 debugs(20, 5, e);
305 if (e.hasTransients()) {
306 const auto index = e.mem_obj->xitTable.index;
307 if (map->freeEntry(index)) {
308 // Delay syncCollapsed(index) which may end `e` wait for updates.
309 // Calling it directly/here creates complex reentrant call chains.
310 CollapsedForwarding::Broadcast(e, true);
311 }
312 } // else nothing to do because e must be private
2745fea5
AR
313}
314
315void
4310f8b0 316Transients::evictIfFound(const cache_key *key)
1bfe9ade 317{
4310f8b0
EB
318 if (!map)
319 return;
320
321 const sfileno index = map->fileNoByKey(key);
322 if (map->freeEntry(index))
323 CollapsedForwarding::Broadcast(index, true);
1bfe9ade
AR
324}
325
4475555f 326void
4310f8b0 327Transients::disconnect(StoreEntry &entry)
4475555f 328{
4310f8b0
EB
329 debugs(20, 5, entry);
330 if (entry.hasTransients()) {
331 auto &xitTable = entry.mem_obj->xitTable;
99921d9d 332 assert(map);
4310f8b0
EB
333 if (isWriter(entry)) {
334 map->abortWriting(xitTable.index);
99921d9d 335 } else {
4310f8b0 336 assert(isReader(entry));
d1d3b4dc 337 map->closeForReadingAndFreeIdle(xitTable.index);
99921d9d 338 }
4310f8b0
EB
339 locals->at(xitTable.index) = nullptr;
340 xitTable.index = -1;
341 xitTable.io = Store::ioDone;
99921d9d 342 }
4475555f
AR
343}
344
9a9954ba
AR
345/// calculates maximum number of entries we need to store and map
346int64_t
347Transients::EntryLimit()
348{
daed75a9
EB
349 return (UsingSmp() && Store::Controller::SmpAware()) ?
350 Config.shared_transient_entries_limit : 0;
9a9954ba
AR
351}
352
4310f8b0
EB
353bool
354Transients::markedForDeletion(const cache_key *key) const
355{
356 assert(map);
357 return map->markedForDeletion(key);
358}
359
360bool
361Transients::isReader(const StoreEntry &e) const
362{
363 return e.mem_obj && e.mem_obj->xitTable.io == Store::ioReading;
364}
365
366bool
367Transients::isWriter(const StoreEntry &e) const
368{
369 return e.mem_obj && e.mem_obj->xitTable.io == Store::ioWriting;
370}
371
9a9954ba
AR
372/// initializes shared memory segment used by Transients
373class TransientsRr: public Ipc::Mem::RegisteredRunner
374{
375public:
376 /* RegisteredRunner API */
21b7990f 377 virtual void useConfig();
9a9954ba
AR
378 virtual ~TransientsRr();
379
380protected:
21b7990f 381 virtual void create();
9a9954ba
AR
382
383private:
4310f8b0 384 TransientsMap::Owner *mapOwner = nullptr;
9a9954ba
AR
385};
386
21b7990f 387RunnerRegistrationEntry(TransientsRr);
9a9954ba 388
e4d13993 389void
21b7990f 390TransientsRr::useConfig()
9a9954ba
AR
391{
392 assert(Config.memShared.configured());
21b7990f 393 Ipc::Mem::RegisteredRunner::useConfig();
9a9954ba
AR
394}
395
e4d13993 396void
21b7990f 397TransientsRr::create()
9a9954ba 398{
9a9954ba 399 const int64_t entryLimit = Transients::EntryLimit();
9a9954ba
AR
400 if (entryLimit <= 0)
401 return; // no SMP configured or a misconfiguration
402
403 Must(!mapOwner);
404 mapOwner = TransientsMap::Init(MapLabel, entryLimit);
405}
406
407TransientsRr::~TransientsRr()
408{
409 delete mapOwner;
410}
f53969cc 411