]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Transients.cc
Cleanup unit tests (#688)
[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
6c8cbe63
D
165 // store hadWriter before checking ENTRY_REQUIRES_COLLAPSING to avoid racing
166 // the writer that clears that flag and then leaves
167 const auto hadWriter = map->peekAtWriter(index);
168 if (!hadWriter && EBIT_TEST(anchor->basics.flags, ENTRY_REQUIRES_COLLAPSING)) {
169 debugs(20, 3, "not joining abandoned entry " << index);
170 map->closeForReadingAndFreeIdle(index);
171 return nullptr;
172 }
173
4310f8b0
EB
174 StoreEntry *e = new StoreEntry();
175 e->createMemObject();
4475555f 176 e->mem_obj->xitTable.index = index;
4310f8b0
EB
177 e->mem_obj->xitTable.io = Store::ioReading;
178 anchor->exportInto(*e);
6c8cbe63
D
179
180 if (EBIT_TEST(anchor->basics.flags, ENTRY_REQUIRES_COLLAPSING)) {
181 assert(hadWriter);
182 e->setCollapsingRequirement(true);
183 }
184
4310f8b0 185 // keep read lock to receive updates from others
9a9954ba
AR
186 return e;
187}
188
6919be24
AR
189StoreEntry *
190Transients::findCollapsed(const sfileno index)
191{
192 if (!map)
193 return NULL;
194
195 if (StoreEntry *oldE = locals->at(index)) {
196 debugs(20, 5, "found " << *oldE << " at " << index << " in " << MapLabel);
197 assert(oldE->mem_obj && oldE->mem_obj->xitTable.index == index);
198 return oldE;
199 }
200
201 debugs(20, 3, "no entry at " << index << " in " << MapLabel);
202 return NULL;
203}
204
d2a6dcba
EB
205void
206Transients::clearCollapsingRequirement(const StoreEntry &e)
207{
208 assert(map);
209 assert(e.hasTransients());
210 assert(isWriter(e));
211 const auto idx = e.mem_obj->xitTable.index;
212 auto &anchor = map->writeableEntry(idx);
213 if (EBIT_TEST(anchor.basics.flags, ENTRY_REQUIRES_COLLAPSING)) {
214 EBIT_CLR(anchor.basics.flags, ENTRY_REQUIRES_COLLAPSING);
215 CollapsedForwarding::Broadcast(e);
216 }
217}
218
9a9954ba 219void
4310f8b0 220Transients::monitorIo(StoreEntry *e, const cache_key *key, const Store::IoStatus direction)
9a9954ba 221{
4310f8b0
EB
222 if (!e->hasTransients()) {
223 addEntry(e, key, direction);
8253d451 224 assert(e->hasTransients());
9d4e9cfb 225 }
9a9954ba 226
4310f8b0
EB
227 const auto index = e->mem_obj->xitTable.index;
228 if (const auto old = locals->at(index)) {
229 assert(old == e);
230 } else {
231 // We do not lock e because we do not want to prevent its destruction;
232 // e is tied to us via mem_obj so we will know when it is destructed.
233 locals->at(index) = e;
9d4e9cfb 234 }
9a9954ba
AR
235}
236
8253d451 237/// creates a new Transients entry
4310f8b0
EB
238void
239Transients::addEntry(StoreEntry *e, const cache_key *key, const Store::IoStatus direction)
9a9954ba 240{
4310f8b0
EB
241 assert(e);
242 assert(e->mem_obj);
243 assert(!e->hasTransients());
9a9954ba 244
4310f8b0 245 Must(map); // configured to track transients
9a9954ba 246
4310f8b0
EB
247 sfileno index = 0;
248 Ipc::StoreMapAnchor *slot = map->openForWriting(key, index);
249 Must(slot); // no writer collisions
9a9954ba 250
8253d451 251 // set ASAP in hope to unlock the slot if something throws
4310f8b0 252 e->mem_obj->xitTable.index = index;
8253d451
AR
253 e->mem_obj->xitTable.io = Store::ioWriting;
254
255 slot->set(*e, key);
4310f8b0 256 if (direction == Store::ioWriting) {
8253d451
AR
257 // allow reading and receive remote DELETE events, but do not switch to
258 // the reading lock because transientReaders() callers want true readers
259 map->startAppending(index);
4310f8b0 260 } else {
8253d451 261 assert(direction == Store::ioReading);
4310f8b0 262 // keep the entry locked (for reading) to receive remote DELETE events
8253d451
AR
263 map->switchWritingToReading(index);
264 e->mem_obj->xitTable.io = Store::ioReading;
4310f8b0 265 }
9a9954ba
AR
266}
267
d1d3b4dc
EB
268bool
269Transients::hasWriter(const StoreEntry &e)
270{
271 if (!e.hasTransients())
272 return false;
273 return map->peekAtWriter(e.mem_obj->xitTable.index);
274}
275
9a9954ba 276void
ced8def3 277Transients::noteFreeMapSlice(const Ipc::StoreMapSliceId)
9a9954ba
AR
278{
279 // TODO: we should probably find the entry being deleted and abort it
280}
281
4475555f 282void
d2a6dcba 283Transients::status(const StoreEntry &entry, Transients::EntryStatus &entryStatus) const
4475555f
AR
284{
285 assert(map);
4310f8b0
EB
286 assert(entry.hasTransients());
287 const auto idx = entry.mem_obj->xitTable.index;
288 const auto &anchor = isWriter(entry) ?
289 map->writeableEntry(idx) : map->readableEntry(idx);
d2a6dcba
EB
290 entryStatus.abortedByWriter = anchor.writerHalted;
291 entryStatus.waitingToBeFreed = anchor.waitingToBeFreed;
292 entryStatus.collapsed = EBIT_TEST(anchor.basics.flags, ENTRY_REQUIRES_COLLAPSING);
4475555f
AR
293}
294
99921d9d
AR
295void
296Transients::completeWriting(const StoreEntry &e)
297{
4310f8b0
EB
298 assert(e.hasTransients());
299 assert(isWriter(e));
8253d451 300 map->switchWritingToReading(e.mem_obj->xitTable.index);
4310f8b0 301 e.mem_obj->xitTable.io = Store::ioReading;
99921d9d
AR
302}
303
d366a7fa
AR
304int
305Transients::readers(const StoreEntry &e) const
306{
4310f8b0 307 if (e.hasTransients()) {
d366a7fa
AR
308 assert(map);
309 return map->peekAtEntry(e.mem_obj->xitTable.index).lock.readers;
310 }
311 return 0;
312}
313
1bfe9ade 314void
4310f8b0
EB
315Transients::evictCached(StoreEntry &e)
316{
317 debugs(20, 5, e);
318 if (e.hasTransients()) {
319 const auto index = e.mem_obj->xitTable.index;
320 if (map->freeEntry(index)) {
321 // Delay syncCollapsed(index) which may end `e` wait for updates.
322 // Calling it directly/here creates complex reentrant call chains.
323 CollapsedForwarding::Broadcast(e, true);
324 }
325 } // else nothing to do because e must be private
2745fea5
AR
326}
327
328void
4310f8b0 329Transients::evictIfFound(const cache_key *key)
1bfe9ade 330{
4310f8b0
EB
331 if (!map)
332 return;
333
334 const sfileno index = map->fileNoByKey(key);
335 if (map->freeEntry(index))
336 CollapsedForwarding::Broadcast(index, true);
1bfe9ade
AR
337}
338
4475555f 339void
4310f8b0 340Transients::disconnect(StoreEntry &entry)
4475555f 341{
4310f8b0
EB
342 debugs(20, 5, entry);
343 if (entry.hasTransients()) {
344 auto &xitTable = entry.mem_obj->xitTable;
99921d9d 345 assert(map);
4310f8b0
EB
346 if (isWriter(entry)) {
347 map->abortWriting(xitTable.index);
99921d9d 348 } else {
4310f8b0 349 assert(isReader(entry));
d1d3b4dc 350 map->closeForReadingAndFreeIdle(xitTable.index);
99921d9d 351 }
4310f8b0
EB
352 locals->at(xitTable.index) = nullptr;
353 xitTable.index = -1;
354 xitTable.io = Store::ioDone;
99921d9d 355 }
4475555f
AR
356}
357
9a9954ba
AR
358/// calculates maximum number of entries we need to store and map
359int64_t
360Transients::EntryLimit()
361{
daed75a9
EB
362 return (UsingSmp() && Store::Controller::SmpAware()) ?
363 Config.shared_transient_entries_limit : 0;
9a9954ba
AR
364}
365
4310f8b0
EB
366bool
367Transients::markedForDeletion(const cache_key *key) const
368{
369 assert(map);
370 return map->markedForDeletion(key);
371}
372
373bool
374Transients::isReader(const StoreEntry &e) const
375{
376 return e.mem_obj && e.mem_obj->xitTable.io == Store::ioReading;
377}
378
379bool
380Transients::isWriter(const StoreEntry &e) const
381{
382 return e.mem_obj && e.mem_obj->xitTable.io == Store::ioWriting;
383}
384
9a9954ba
AR
385/// initializes shared memory segment used by Transients
386class TransientsRr: public Ipc::Mem::RegisteredRunner
387{
388public:
389 /* RegisteredRunner API */
21b7990f 390 virtual void useConfig();
9a9954ba
AR
391 virtual ~TransientsRr();
392
393protected:
21b7990f 394 virtual void create();
9a9954ba
AR
395
396private:
4310f8b0 397 TransientsMap::Owner *mapOwner = nullptr;
9a9954ba
AR
398};
399
21b7990f 400RunnerRegistrationEntry(TransientsRr);
9a9954ba 401
e4d13993 402void
21b7990f 403TransientsRr::useConfig()
9a9954ba
AR
404{
405 assert(Config.memShared.configured());
21b7990f 406 Ipc::Mem::RegisteredRunner::useConfig();
9a9954ba
AR
407}
408
e4d13993 409void
21b7990f 410TransientsRr::create()
9a9954ba 411{
9a9954ba 412 const int64_t entryLimit = Transients::EntryLimit();
9a9954ba
AR
413 if (entryLimit <= 0)
414 return; // no SMP configured or a misconfiguration
415
416 Must(!mapOwner);
417 mapOwner = TransientsMap::Init(MapLabel, entryLimit);
418}
419
420TransientsRr::~TransientsRr()
421{
422 delete mapOwner;
423}
f53969cc 424