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