]>
Commit | Line | Data |
---|---|---|
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 |
28 | static const SBuf MapLabel("transients_map"); | |
9a9954ba | 29 | |
6919be24 | 30 | Transients::Transients(): map(NULL), locals(NULL) |
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 | { | |
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 | ||
54 | void | |
55 | Transients::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 | ||
69 | void | |
70 | Transients::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 | ||
89 | void | |
90 | Transients::maintain() | |
91 | { | |
e4d13993 | 92 | // no lazy garbage collection needed |
9a9954ba AR |
93 | } |
94 | ||
95 | uint64_t | |
96 | Transients::minSize() const | |
97 | { | |
98 | return 0; // XXX: irrelevant, but Store parent forces us to implement this | |
99 | } | |
100 | ||
101 | uint64_t | |
102 | Transients::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 | ||
108 | uint64_t | |
109 | Transients::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 | ||
116 | uint64_t | |
117 | Transients::currentCount() const | |
118 | { | |
119 | return map ? map->entryCount() : 0; | |
120 | } | |
121 | ||
122 | int64_t | |
123 | Transients::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 | ||
129 | void | |
130 | Transients::reference(StoreEntry &) | |
131 | { | |
e4d13993 | 132 | // no replacement policy (but the cache(s) storing the entry may have one) |
9a9954ba AR |
133 | } |
134 | ||
135 | bool | |
2745fea5 | 136 | Transients::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 |
142 | StoreEntry * |
143 | Transients::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); | |
169 | // keep read lock to receive updates from others | |
9a9954ba AR |
170 | return e; |
171 | } | |
172 | ||
6919be24 AR |
173 | StoreEntry * |
174 | Transients::findCollapsed(const sfileno index) | |
175 | { | |
176 | if (!map) | |
177 | return NULL; | |
178 | ||
179 | if (StoreEntry *oldE = locals->at(index)) { | |
180 | debugs(20, 5, "found " << *oldE << " at " << index << " in " << MapLabel); | |
181 | assert(oldE->mem_obj && oldE->mem_obj->xitTable.index == index); | |
182 | return oldE; | |
183 | } | |
184 | ||
185 | debugs(20, 3, "no entry at " << index << " in " << MapLabel); | |
186 | return NULL; | |
187 | } | |
188 | ||
9a9954ba | 189 | void |
4310f8b0 | 190 | Transients::monitorIo(StoreEntry *e, const cache_key *key, const Store::IoStatus direction) |
9a9954ba | 191 | { |
4310f8b0 EB |
192 | if (!e->hasTransients()) { |
193 | addEntry(e, key, direction); | |
8253d451 | 194 | assert(e->hasTransients()); |
9d4e9cfb | 195 | } |
9a9954ba | 196 | |
4310f8b0 EB |
197 | const auto index = e->mem_obj->xitTable.index; |
198 | if (const auto old = locals->at(index)) { | |
199 | assert(old == e); | |
200 | } else { | |
201 | // We do not lock e because we do not want to prevent its destruction; | |
202 | // e is tied to us via mem_obj so we will know when it is destructed. | |
203 | locals->at(index) = e; | |
9d4e9cfb | 204 | } |
9a9954ba AR |
205 | } |
206 | ||
8253d451 | 207 | /// creates a new Transients entry |
4310f8b0 EB |
208 | void |
209 | Transients::addEntry(StoreEntry *e, const cache_key *key, const Store::IoStatus direction) | |
9a9954ba | 210 | { |
4310f8b0 EB |
211 | assert(e); |
212 | assert(e->mem_obj); | |
213 | assert(!e->hasTransients()); | |
9a9954ba | 214 | |
4310f8b0 | 215 | Must(map); // configured to track transients |
9a9954ba | 216 | |
4310f8b0 EB |
217 | sfileno index = 0; |
218 | Ipc::StoreMapAnchor *slot = map->openForWriting(key, index); | |
219 | Must(slot); // no writer collisions | |
9a9954ba | 220 | |
8253d451 | 221 | // set ASAP in hope to unlock the slot if something throws |
4310f8b0 | 222 | e->mem_obj->xitTable.index = index; |
8253d451 AR |
223 | e->mem_obj->xitTable.io = Store::ioWriting; |
224 | ||
225 | slot->set(*e, key); | |
4310f8b0 | 226 | if (direction == Store::ioWriting) { |
8253d451 AR |
227 | // allow reading and receive remote DELETE events, but do not switch to |
228 | // the reading lock because transientReaders() callers want true readers | |
229 | map->startAppending(index); | |
4310f8b0 | 230 | } else { |
8253d451 | 231 | assert(direction == Store::ioReading); |
4310f8b0 | 232 | // keep the entry locked (for reading) to receive remote DELETE events |
8253d451 AR |
233 | map->switchWritingToReading(index); |
234 | e->mem_obj->xitTable.io = Store::ioReading; | |
4310f8b0 | 235 | } |
9a9954ba AR |
236 | } |
237 | ||
238 | void | |
ced8def3 | 239 | Transients::noteFreeMapSlice(const Ipc::StoreMapSliceId) |
9a9954ba AR |
240 | { |
241 | // TODO: we should probably find the entry being deleted and abort it | |
242 | } | |
243 | ||
4475555f | 244 | void |
4310f8b0 | 245 | Transients::status(const StoreEntry &entry, bool &aborted, bool &waitingToBeFreed) const |
4475555f AR |
246 | { |
247 | assert(map); | |
4310f8b0 EB |
248 | assert(entry.hasTransients()); |
249 | const auto idx = entry.mem_obj->xitTable.index; | |
250 | const auto &anchor = isWriter(entry) ? | |
251 | map->writeableEntry(idx) : map->readableEntry(idx); | |
252 | aborted = anchor.writerHalted; | |
253 | waitingToBeFreed = anchor.waitingToBeFreed; | |
4475555f AR |
254 | } |
255 | ||
99921d9d AR |
256 | void |
257 | Transients::completeWriting(const StoreEntry &e) | |
258 | { | |
4310f8b0 EB |
259 | assert(e.hasTransients()); |
260 | assert(isWriter(e)); | |
8253d451 | 261 | map->switchWritingToReading(e.mem_obj->xitTable.index); |
4310f8b0 | 262 | e.mem_obj->xitTable.io = Store::ioReading; |
99921d9d AR |
263 | } |
264 | ||
d366a7fa AR |
265 | int |
266 | Transients::readers(const StoreEntry &e) const | |
267 | { | |
4310f8b0 | 268 | if (e.hasTransients()) { |
d366a7fa AR |
269 | assert(map); |
270 | return map->peekAtEntry(e.mem_obj->xitTable.index).lock.readers; | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
1bfe9ade | 275 | void |
4310f8b0 EB |
276 | Transients::evictCached(StoreEntry &e) |
277 | { | |
278 | debugs(20, 5, e); | |
279 | if (e.hasTransients()) { | |
280 | const auto index = e.mem_obj->xitTable.index; | |
281 | if (map->freeEntry(index)) { | |
282 | // Delay syncCollapsed(index) which may end `e` wait for updates. | |
283 | // Calling it directly/here creates complex reentrant call chains. | |
284 | CollapsedForwarding::Broadcast(e, true); | |
285 | } | |
286 | } // else nothing to do because e must be private | |
2745fea5 AR |
287 | } |
288 | ||
289 | void | |
4310f8b0 | 290 | Transients::evictIfFound(const cache_key *key) |
1bfe9ade | 291 | { |
4310f8b0 EB |
292 | if (!map) |
293 | return; | |
294 | ||
295 | const sfileno index = map->fileNoByKey(key); | |
296 | if (map->freeEntry(index)) | |
297 | CollapsedForwarding::Broadcast(index, true); | |
1bfe9ade AR |
298 | } |
299 | ||
4475555f | 300 | void |
4310f8b0 | 301 | Transients::disconnect(StoreEntry &entry) |
4475555f | 302 | { |
4310f8b0 EB |
303 | debugs(20, 5, entry); |
304 | if (entry.hasTransients()) { | |
305 | auto &xitTable = entry.mem_obj->xitTable; | |
99921d9d | 306 | assert(map); |
4310f8b0 EB |
307 | if (isWriter(entry)) { |
308 | map->abortWriting(xitTable.index); | |
99921d9d | 309 | } else { |
4310f8b0 EB |
310 | assert(isReader(entry)); |
311 | map->closeForReading(xitTable.index); | |
99921d9d | 312 | } |
4310f8b0 EB |
313 | locals->at(xitTable.index) = nullptr; |
314 | xitTable.index = -1; | |
315 | xitTable.io = Store::ioDone; | |
99921d9d | 316 | } |
4475555f AR |
317 | } |
318 | ||
9a9954ba AR |
319 | /// calculates maximum number of entries we need to store and map |
320 | int64_t | |
321 | Transients::EntryLimit() | |
322 | { | |
323 | // TODO: we should also check whether any SMP-aware caching is configured | |
324 | if (!UsingSmp() || !Config.onoff.collapsed_forwarding) | |
325 | return 0; // no SMP collapsed forwarding possible or needed | |
326 | ||
8f7dbf74 | 327 | return Config.collapsed_forwarding_shared_entries_limit; |
9a9954ba AR |
328 | } |
329 | ||
4310f8b0 EB |
330 | bool |
331 | Transients::markedForDeletion(const cache_key *key) const | |
332 | { | |
333 | assert(map); | |
334 | return map->markedForDeletion(key); | |
335 | } | |
336 | ||
337 | bool | |
338 | Transients::isReader(const StoreEntry &e) const | |
339 | { | |
340 | return e.mem_obj && e.mem_obj->xitTable.io == Store::ioReading; | |
341 | } | |
342 | ||
343 | bool | |
344 | Transients::isWriter(const StoreEntry &e) const | |
345 | { | |
346 | return e.mem_obj && e.mem_obj->xitTable.io == Store::ioWriting; | |
347 | } | |
348 | ||
9a9954ba AR |
349 | /// initializes shared memory segment used by Transients |
350 | class TransientsRr: public Ipc::Mem::RegisteredRunner | |
351 | { | |
352 | public: | |
353 | /* RegisteredRunner API */ | |
21b7990f | 354 | virtual void useConfig(); |
9a9954ba AR |
355 | virtual ~TransientsRr(); |
356 | ||
357 | protected: | |
21b7990f | 358 | virtual void create(); |
9a9954ba AR |
359 | |
360 | private: | |
4310f8b0 | 361 | TransientsMap::Owner *mapOwner = nullptr; |
9a9954ba AR |
362 | }; |
363 | ||
21b7990f | 364 | RunnerRegistrationEntry(TransientsRr); |
9a9954ba | 365 | |
e4d13993 | 366 | void |
21b7990f | 367 | TransientsRr::useConfig() |
9a9954ba AR |
368 | { |
369 | assert(Config.memShared.configured()); | |
21b7990f | 370 | Ipc::Mem::RegisteredRunner::useConfig(); |
9a9954ba AR |
371 | } |
372 | ||
e4d13993 | 373 | void |
21b7990f | 374 | TransientsRr::create() |
9a9954ba | 375 | { |
9a9954ba AR |
376 | if (!Config.onoff.collapsed_forwarding) |
377 | return; | |
378 | ||
379 | const int64_t entryLimit = Transients::EntryLimit(); | |
9a9954ba AR |
380 | if (entryLimit <= 0) |
381 | return; // no SMP configured or a misconfiguration | |
382 | ||
383 | Must(!mapOwner); | |
384 | mapOwner = TransientsMap::Init(MapLabel, entryLimit); | |
385 | } | |
386 | ||
387 | TransientsRr::~TransientsRr() | |
388 | { | |
389 | delete mapOwner; | |
390 | } | |
f53969cc | 391 |