]>
Commit | Line | Data |
---|---|---|
9a9954ba | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 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 |
28 | static const SBuf MapLabel("transients_map"); | |
9a9954ba | 29 | |
aee3523a | 30 | Transients::Transients(): map(nullptr), locals(nullptr) |
9a9954ba | 31 | { |
9a9954ba AR |
32 | } |
33 | ||
34 | Transients::~Transients() | |
35 | { | |
36 | delete map; | |
6919be24 | 37 | delete locals; |
9a9954ba AR |
38 | } |
39 | ||
40 | void | |
41 | Transients::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 | |
aee3523a | 52 | locals = new Locals(entryLimit, nullptr); |
9a9954ba AR |
53 | } |
54 | ||
55 | void | |
56 | Transients::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(); | |
8b082ed9 FC |
67 | #else |
68 | (void)stats; | |
9a9954ba AR |
69 | #endif |
70 | } | |
71 | ||
72 | void | |
73 | Transients::stat(StoreEntry &e) const | |
74 | { | |
75 | storeAppendPrintf(&e, "\n\nTransient Objects\n"); | |
76 | ||
77 | storeAppendPrintf(&e, "Maximum Size: %.0f KB\n", maxSize()/1024.0); | |
78 | storeAppendPrintf(&e, "Current Size: %.2f KB %.2f%%\n", | |
79 | currentSize() / 1024.0, | |
80 | Math::doublePercent(currentSize(), maxSize())); | |
81 | ||
82 | if (map) { | |
83 | const int limit = map->entryLimit(); | |
84 | storeAppendPrintf(&e, "Maximum entries: %9d\n", limit); | |
85 | if (limit > 0) { | |
86 | storeAppendPrintf(&e, "Current entries: %" PRId64 " %.2f%%\n", | |
87 | currentCount(), (100.0 * currentCount() / limit)); | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | void | |
93 | Transients::maintain() | |
94 | { | |
e4d13993 | 95 | // no lazy garbage collection needed |
9a9954ba AR |
96 | } |
97 | ||
98 | uint64_t | |
99 | Transients::minSize() const | |
100 | { | |
101 | return 0; // XXX: irrelevant, but Store parent forces us to implement this | |
102 | } | |
103 | ||
104 | uint64_t | |
105 | Transients::maxSize() const | |
106 | { | |
107 | // Squid currently does not limit the total size of all transient objects | |
108 | return std::numeric_limits<uint64_t>::max(); | |
109 | } | |
110 | ||
111 | uint64_t | |
112 | Transients::currentSize() const | |
113 | { | |
114 | // TODO: we do not get enough information to calculate this | |
115 | // StoreEntry should update associated stores when its size changes | |
116 | return 0; | |
117 | } | |
118 | ||
119 | uint64_t | |
120 | Transients::currentCount() const | |
121 | { | |
122 | return map ? map->entryCount() : 0; | |
123 | } | |
124 | ||
125 | int64_t | |
126 | Transients::maxObjectSize() const | |
127 | { | |
128 | // Squid currently does not limit the size of a transient object | |
129 | return std::numeric_limits<uint64_t>::max(); | |
130 | } | |
131 | ||
132 | void | |
133 | Transients::reference(StoreEntry &) | |
134 | { | |
e4d13993 | 135 | // no replacement policy (but the cache(s) storing the entry may have one) |
9a9954ba AR |
136 | } |
137 | ||
138 | bool | |
2745fea5 | 139 | Transients::dereference(StoreEntry &) |
9a9954ba AR |
140 | { |
141 | // no need to keep e in the global store_table for us; we have our own map | |
142 | return false; | |
143 | } | |
144 | ||
9a9954ba AR |
145 | StoreEntry * |
146 | Transients::get(const cache_key *key) | |
147 | { | |
148 | if (!map) | |
aee3523a | 149 | return nullptr; |
9a9954ba AR |
150 | |
151 | sfileno index; | |
1bfe9ade AR |
152 | const Ipc::StoreMapAnchor *anchor = map->openForReading(key, index); |
153 | if (!anchor) | |
aee3523a | 154 | return nullptr; |
9a9954ba | 155 | |
6919be24 AR |
156 | // If we already have a local entry, the store_table should have found it. |
157 | // Since it did not, the local entry key must have changed from public to | |
158 | // private. We still need to keep the private entry around for syncing as | |
159 | // its clients depend on it, but we should not allow new clients to join. | |
160 | if (StoreEntry *oldE = locals->at(index)) { | |
161 | debugs(20, 3, "not joining private " << *oldE); | |
162 | assert(EBIT_TEST(oldE->flags, KEY_PRIVATE)); | |
d1d3b4dc | 163 | map->closeForReadingAndFreeIdle(index); |
4310f8b0 | 164 | return nullptr; |
1bfe9ade | 165 | } |
4475555f | 166 | |
4310f8b0 EB |
167 | StoreEntry *e = new StoreEntry(); |
168 | e->createMemObject(); | |
dbedb5b9 | 169 | e->mem_obj->xitTable.open(index, Store::ioReading); |
6c8cbe63 | 170 | |
4310f8b0 | 171 | // keep read lock to receive updates from others |
9a9954ba AR |
172 | return e; |
173 | } | |
174 | ||
6919be24 AR |
175 | StoreEntry * |
176 | Transients::findCollapsed(const sfileno index) | |
177 | { | |
178 | if (!map) | |
aee3523a | 179 | return nullptr; |
6919be24 AR |
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); | |
aee3523a | 188 | return nullptr; |
6919be24 AR |
189 | } |
190 | ||
9a9954ba | 191 | void |
4310f8b0 | 192 | Transients::monitorIo(StoreEntry *e, const cache_key *key, const Store::IoStatus direction) |
9a9954ba | 193 | { |
4310f8b0 EB |
194 | if (!e->hasTransients()) { |
195 | addEntry(e, key, direction); | |
8253d451 | 196 | assert(e->hasTransients()); |
9d4e9cfb | 197 | } |
9a9954ba | 198 | |
4310f8b0 EB |
199 | const auto index = e->mem_obj->xitTable.index; |
200 | if (const auto old = locals->at(index)) { | |
201 | assert(old == e); | |
202 | } else { | |
203 | // We do not lock e because we do not want to prevent its destruction; | |
204 | // e is tied to us via mem_obj so we will know when it is destructed. | |
205 | locals->at(index) = e; | |
9d4e9cfb | 206 | } |
9a9954ba AR |
207 | } |
208 | ||
8253d451 | 209 | /// creates a new Transients entry |
4310f8b0 EB |
210 | void |
211 | Transients::addEntry(StoreEntry *e, const cache_key *key, const Store::IoStatus direction) | |
9a9954ba | 212 | { |
4310f8b0 EB |
213 | assert(e); |
214 | assert(e->mem_obj); | |
215 | assert(!e->hasTransients()); | |
9a9954ba | 216 | |
4310f8b0 | 217 | Must(map); // configured to track transients |
9a9954ba | 218 | |
5bd8ce13 AR |
219 | if (direction == Store::ioWriting) |
220 | return addWriterEntry(*e, key); | |
221 | ||
222 | assert(direction == Store::ioReading); | |
223 | addReaderEntry(*e, key); | |
224 | } | |
225 | ||
226 | /// addEntry() helper used for cache entry creators/writers | |
227 | void | |
228 | Transients::addWriterEntry(StoreEntry &e, const cache_key *key) | |
229 | { | |
4310f8b0 | 230 | sfileno index = 0; |
5bd8ce13 AR |
231 | const auto anchor = map->openForWriting(key, index); |
232 | if (!anchor) | |
233 | throw TextException("writer collision", Here()); | |
9a9954ba | 234 | |
8253d451 | 235 | // set ASAP in hope to unlock the slot if something throws |
5bd8ce13 | 236 | // and to provide index to such methods as hasWriter() |
dbedb5b9 | 237 | e.mem_obj->xitTable.open(index, Store::ioWriting); |
5bd8ce13 | 238 | |
dbedb5b9 | 239 | anchor->setKey(key); |
5bd8ce13 AR |
240 | // allow reading and receive remote DELETE events, but do not switch to |
241 | // the reading lock because transientReaders() callers want true readers | |
242 | map->startAppending(index); | |
243 | } | |
244 | ||
245 | /// addEntry() helper used for cache readers | |
246 | /// readers do not modify the cache, but they must create a Transients entry | |
247 | void | |
248 | Transients::addReaderEntry(StoreEntry &e, const cache_key *key) | |
249 | { | |
250 | sfileno index = 0; | |
dbedb5b9 | 251 | const auto anchor = map->openOrCreateForReading(key, index); |
5bd8ce13 AR |
252 | if (!anchor) |
253 | throw TextException("reader collision", Here()); | |
254 | ||
dbedb5b9 | 255 | e.mem_obj->xitTable.open(index, Store::ioReading); |
5bd8ce13 AR |
256 | // keep the entry locked (for reading) to receive remote DELETE events |
257 | } | |
258 | ||
d1d3b4dc EB |
259 | bool |
260 | Transients::hasWriter(const StoreEntry &e) | |
261 | { | |
262 | if (!e.hasTransients()) | |
263 | return false; | |
264 | return map->peekAtWriter(e.mem_obj->xitTable.index); | |
265 | } | |
266 | ||
9a9954ba | 267 | void |
ced8def3 | 268 | Transients::noteFreeMapSlice(const Ipc::StoreMapSliceId) |
9a9954ba AR |
269 | { |
270 | // TODO: we should probably find the entry being deleted and abort it | |
271 | } | |
272 | ||
4475555f | 273 | void |
d2a6dcba | 274 | Transients::status(const StoreEntry &entry, Transients::EntryStatus &entryStatus) const |
4475555f AR |
275 | { |
276 | assert(map); | |
4310f8b0 EB |
277 | assert(entry.hasTransients()); |
278 | const auto idx = entry.mem_obj->xitTable.index; | |
279 | const auto &anchor = isWriter(entry) ? | |
280 | map->writeableEntry(idx) : map->readableEntry(idx); | |
24c93780 | 281 | entryStatus.hasWriter = anchor.writing(); |
d2a6dcba | 282 | entryStatus.waitingToBeFreed = anchor.waitingToBeFreed; |
4475555f AR |
283 | } |
284 | ||
99921d9d AR |
285 | void |
286 | Transients::completeWriting(const StoreEntry &e) | |
287 | { | |
24c93780 | 288 | debugs(20, 5, e); |
4310f8b0 EB |
289 | assert(e.hasTransients()); |
290 | assert(isWriter(e)); | |
8253d451 | 291 | map->switchWritingToReading(e.mem_obj->xitTable.index); |
4310f8b0 | 292 | e.mem_obj->xitTable.io = Store::ioReading; |
24c93780 | 293 | CollapsedForwarding::Broadcast(e); |
99921d9d AR |
294 | } |
295 | ||
d366a7fa AR |
296 | int |
297 | Transients::readers(const StoreEntry &e) const | |
298 | { | |
4310f8b0 | 299 | if (e.hasTransients()) { |
d366a7fa AR |
300 | assert(map); |
301 | return map->peekAtEntry(e.mem_obj->xitTable.index).lock.readers; | |
302 | } | |
303 | return 0; | |
304 | } | |
305 | ||
1bfe9ade | 306 | void |
4310f8b0 EB |
307 | Transients::evictCached(StoreEntry &e) |
308 | { | |
309 | debugs(20, 5, e); | |
310 | if (e.hasTransients()) { | |
311 | const auto index = e.mem_obj->xitTable.index; | |
312 | if (map->freeEntry(index)) { | |
313 | // Delay syncCollapsed(index) which may end `e` wait for updates. | |
314 | // Calling it directly/here creates complex reentrant call chains. | |
315 | CollapsedForwarding::Broadcast(e, true); | |
316 | } | |
317 | } // else nothing to do because e must be private | |
2745fea5 AR |
318 | } |
319 | ||
320 | void | |
4310f8b0 | 321 | Transients::evictIfFound(const cache_key *key) |
1bfe9ade | 322 | { |
4310f8b0 EB |
323 | if (!map) |
324 | return; | |
325 | ||
326 | const sfileno index = map->fileNoByKey(key); | |
327 | if (map->freeEntry(index)) | |
328 | CollapsedForwarding::Broadcast(index, true); | |
1bfe9ade AR |
329 | } |
330 | ||
4475555f | 331 | void |
4310f8b0 | 332 | Transients::disconnect(StoreEntry &entry) |
4475555f | 333 | { |
4310f8b0 EB |
334 | debugs(20, 5, entry); |
335 | if (entry.hasTransients()) { | |
336 | auto &xitTable = entry.mem_obj->xitTable; | |
99921d9d | 337 | assert(map); |
4310f8b0 | 338 | if (isWriter(entry)) { |
24c93780 AR |
339 | // completeWriting() was not called, so there could be an active |
340 | // Store writer out there, but we should not abortWriting() here | |
341 | // because another writer may have succeeded, making readers happy. | |
342 | // If none succeeded, the readers will notice the lack of writers. | |
343 | map->closeForWriting(xitTable.index); | |
344 | CollapsedForwarding::Broadcast(entry); | |
99921d9d | 345 | } else { |
4310f8b0 | 346 | assert(isReader(entry)); |
d1d3b4dc | 347 | map->closeForReadingAndFreeIdle(xitTable.index); |
99921d9d | 348 | } |
4310f8b0 | 349 | locals->at(xitTable.index) = nullptr; |
dbedb5b9 | 350 | xitTable.close(); |
99921d9d | 351 | } |
4475555f AR |
352 | } |
353 | ||
9a9954ba AR |
354 | /// calculates maximum number of entries we need to store and map |
355 | int64_t | |
356 | Transients::EntryLimit() | |
357 | { | |
daed75a9 EB |
358 | return (UsingSmp() && Store::Controller::SmpAware()) ? |
359 | Config.shared_transient_entries_limit : 0; | |
9a9954ba AR |
360 | } |
361 | ||
4310f8b0 EB |
362 | bool |
363 | Transients::markedForDeletion(const cache_key *key) const | |
364 | { | |
365 | assert(map); | |
366 | return map->markedForDeletion(key); | |
367 | } | |
368 | ||
369 | bool | |
370 | Transients::isReader(const StoreEntry &e) const | |
371 | { | |
372 | return e.mem_obj && e.mem_obj->xitTable.io == Store::ioReading; | |
373 | } | |
374 | ||
375 | bool | |
376 | Transients::isWriter(const StoreEntry &e) const | |
377 | { | |
378 | return e.mem_obj && e.mem_obj->xitTable.io == Store::ioWriting; | |
379 | } | |
380 | ||
9a9954ba AR |
381 | /// initializes shared memory segment used by Transients |
382 | class TransientsRr: public Ipc::Mem::RegisteredRunner | |
383 | { | |
384 | public: | |
385 | /* RegisteredRunner API */ | |
337b9aa4 AR |
386 | void useConfig() override; |
387 | ~TransientsRr() override; | |
9a9954ba AR |
388 | |
389 | protected: | |
337b9aa4 | 390 | void create() override; |
9a9954ba AR |
391 | |
392 | private: | |
4310f8b0 | 393 | TransientsMap::Owner *mapOwner = nullptr; |
9a9954ba AR |
394 | }; |
395 | ||
21b7990f | 396 | RunnerRegistrationEntry(TransientsRr); |
9a9954ba | 397 | |
e4d13993 | 398 | void |
21b7990f | 399 | TransientsRr::useConfig() |
9a9954ba AR |
400 | { |
401 | assert(Config.memShared.configured()); | |
21b7990f | 402 | Ipc::Mem::RegisteredRunner::useConfig(); |
9a9954ba AR |
403 | } |
404 | ||
e4d13993 | 405 | void |
21b7990f | 406 | TransientsRr::create() |
9a9954ba | 407 | { |
9a9954ba | 408 | const int64_t entryLimit = Transients::EntryLimit(); |
9a9954ba AR |
409 | if (entryLimit <= 0) |
410 | return; // no SMP configured or a misconfiguration | |
411 | ||
412 | Must(!mapOwner); | |
413 | mapOwner = TransientsMap::Init(MapLabel, entryLimit); | |
414 | } | |
415 | ||
416 | TransientsRr::~TransientsRr() | |
417 | { | |
418 | delete mapOwner; | |
419 | } | |
f53969cc | 420 |