]>
Commit | Line | Data |
---|---|---|
9a9954ba AR |
1 | /* |
2 | * DEBUG: section 20 Storage Manager | |
3 | * | |
4 | */ | |
5 | ||
6 | #include "squid.h" | |
7 | #include "base/RunnersRegistry.h" | |
8 | #include "HttpReply.h" | |
9 | #include "ipc/mem/Page.h" | |
10 | #include "ipc/mem/Pages.h" | |
11 | #include "MemObject.h" | |
12 | #include "Transients.h" | |
13 | #include "mime_header.h" | |
14 | #include "SquidConfig.h" | |
15 | #include "SquidMath.h" | |
16 | #include "StoreStats.h" | |
17 | #include "tools.h" | |
18 | ||
19 | #if HAVE_LIMITS_H | |
20 | #include <limits> | |
21 | #endif | |
22 | ||
23 | ||
24 | /// shared memory segment path to use for Transients maps | |
25 | static const char *MapLabel = "transients_map"; | |
26 | ||
27 | ||
28 | Transients::Transients(): map(NULL) | |
29 | { | |
9a9954ba AR |
30 | } |
31 | ||
32 | Transients::~Transients() | |
33 | { | |
34 | delete map; | |
35 | } | |
36 | ||
37 | void | |
38 | Transients::init() | |
39 | { | |
40 | const int64_t entryLimit = EntryLimit(); | |
41 | if (entryLimit <= 0) | |
42 | return; // no SMP support or a misconfiguration | |
43 | ||
44 | Must(!map); | |
45 | map = new TransientsMap(MapLabel); | |
46 | map->cleaner = this; | |
47 | } | |
48 | ||
49 | void | |
50 | Transients::getStats(StoreInfoStats &stats) const | |
51 | { | |
52 | #if TRANSIENT_STATS_SUPPORTED | |
53 | const size_t pageSize = Ipc::Mem::PageSize(); | |
54 | ||
55 | stats.mem.shared = true; | |
56 | stats.mem.capacity = | |
57 | Ipc::Mem::PageLimit(Ipc::Mem::PageId::cachePage) * pageSize; | |
58 | stats.mem.size = | |
59 | Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage) * pageSize; | |
60 | stats.mem.count = currentCount(); | |
61 | #endif | |
62 | } | |
63 | ||
64 | void | |
65 | Transients::stat(StoreEntry &e) const | |
66 | { | |
67 | storeAppendPrintf(&e, "\n\nTransient Objects\n"); | |
68 | ||
69 | storeAppendPrintf(&e, "Maximum Size: %.0f KB\n", maxSize()/1024.0); | |
70 | storeAppendPrintf(&e, "Current Size: %.2f KB %.2f%%\n", | |
71 | currentSize() / 1024.0, | |
72 | Math::doublePercent(currentSize(), maxSize())); | |
73 | ||
74 | if (map) { | |
75 | const int limit = map->entryLimit(); | |
76 | storeAppendPrintf(&e, "Maximum entries: %9d\n", limit); | |
77 | if (limit > 0) { | |
78 | storeAppendPrintf(&e, "Current entries: %" PRId64 " %.2f%%\n", | |
79 | currentCount(), (100.0 * currentCount() / limit)); | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | void | |
85 | Transients::maintain() | |
86 | { | |
87 | } | |
88 | ||
89 | uint64_t | |
90 | Transients::minSize() const | |
91 | { | |
92 | return 0; // XXX: irrelevant, but Store parent forces us to implement this | |
93 | } | |
94 | ||
95 | uint64_t | |
96 | Transients::maxSize() const | |
97 | { | |
98 | // Squid currently does not limit the total size of all transient objects | |
99 | return std::numeric_limits<uint64_t>::max(); | |
100 | } | |
101 | ||
102 | uint64_t | |
103 | Transients::currentSize() const | |
104 | { | |
105 | // TODO: we do not get enough information to calculate this | |
106 | // StoreEntry should update associated stores when its size changes | |
107 | return 0; | |
108 | } | |
109 | ||
110 | uint64_t | |
111 | Transients::currentCount() const | |
112 | { | |
113 | return map ? map->entryCount() : 0; | |
114 | } | |
115 | ||
116 | int64_t | |
117 | Transients::maxObjectSize() const | |
118 | { | |
119 | // Squid currently does not limit the size of a transient object | |
120 | return std::numeric_limits<uint64_t>::max(); | |
121 | } | |
122 | ||
123 | void | |
124 | Transients::reference(StoreEntry &) | |
125 | { | |
126 | } | |
127 | ||
128 | bool | |
129 | Transients::dereference(StoreEntry &, bool) | |
130 | { | |
131 | // no need to keep e in the global store_table for us; we have our own map | |
132 | return false; | |
133 | } | |
134 | ||
135 | int | |
136 | Transients::callback() | |
137 | { | |
138 | return 0; | |
139 | } | |
140 | ||
141 | StoreSearch * | |
142 | Transients::search(String const, HttpRequest *) | |
143 | { | |
144 | fatal("not implemented"); | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | StoreEntry * | |
149 | Transients::get(const cache_key *key) | |
150 | { | |
151 | if (!map) | |
152 | return NULL; | |
153 | ||
154 | sfileno index; | |
155 | if (!map->openForReading(key, index)) | |
156 | return NULL; | |
157 | ||
4475555f AR |
158 | if (StoreEntry *e = copyFromShm(index)) |
159 | return e; // keep read lock to receive updates from others | |
160 | ||
161 | // loading failure | |
162 | map->closeForReading(index); | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | StoreEntry * | |
167 | Transients::copyFromShm(const sfileno index) | |
168 | { | |
9a9954ba AR |
169 | const TransientsMap::Extras &extras = map->extras(index); |
170 | ||
171 | // create a brand new store entry and initialize it with stored info | |
172 | StoreEntry *e = storeCreateEntry(extras.url, extras.url, | |
173 | extras.reqFlags, extras.reqMethod); | |
174 | // XXX: overwriting storeCreateEntry() because we are expected to return an unlocked entry | |
175 | // TODO: move locking from storeCreateEntry to callers as a mid-term solution | |
176 | e->lock_count = 0; | |
177 | ||
178 | assert(e->mem_obj); | |
179 | e->mem_obj->method = extras.reqMethod; | |
99921d9d | 180 | e->mem_obj->xitTable.io = MemObject::ioReading; |
4475555f | 181 | e->mem_obj->xitTable.index = index; |
9a9954ba AR |
182 | |
183 | // XXX: overwriting storeCreateEntry() which calls setPrivateKey() if | |
184 | // neighbors_do_private_keys (which is true in most cases and by default). | |
185 | // This is nothing but waste of CPU cycles. Need a better API to avoid it. | |
186 | e->setPublicKey(); | |
ce49546e | 187 | assert(e->key); |
9a9954ba | 188 | |
4475555f AR |
189 | // How do we know its SMP- and not just locally-collapsed? A worker gets |
190 | // locally-collapsed entries from the local store_table, not Transients. | |
191 | // TODO: Can we remove smpCollapsed by not syncing non-transient entries? | |
192 | e->mem_obj->smpCollapsed = true; | |
193 | ||
9a9954ba AR |
194 | return e; |
195 | } | |
196 | ||
197 | void | |
198 | Transients::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData) | |
199 | { | |
200 | // XXX: not needed but Store parent forces us to implement this | |
201 | fatal("Transients::get(key,callback,data) should not be called"); | |
202 | } | |
203 | ||
204 | void | |
99921d9d | 205 | Transients::startWriting(StoreEntry *e, const RequestFlags &reqFlags, |
9a9954ba AR |
206 | const HttpRequestMethod &reqMethod) |
207 | { | |
208 | assert(e); | |
4475555f AR |
209 | assert(e->mem_obj); |
210 | assert(e->mem_obj->xitTable.index < 0); | |
9a9954ba AR |
211 | |
212 | if (!map) { | |
213 | debugs(20, 5, "No map to add " << *e); | |
214 | return; | |
215 | } | |
216 | ||
217 | sfileno index = 0; | |
218 | Ipc::StoreMapAnchor *slot = map->openForWriting(reinterpret_cast<const cache_key *>(e->key), index); | |
219 | if (!slot) { | |
4475555f | 220 | debugs(20, 5, "collision registering " << *e); |
9a9954ba AR |
221 | return; |
222 | } | |
223 | ||
224 | try { | |
225 | if (copyToShm(*e, index, reqFlags, reqMethod)) { | |
226 | slot->set(*e); | |
99921d9d | 227 | e->mem_obj->xitTable.io = MemObject::ioWriting; |
4475555f AR |
228 | e->mem_obj->xitTable.index = index; |
229 | map->startAppending(index); | |
230 | // keep write lock -- we will be supplying others with updates | |
9a9954ba AR |
231 | return; |
232 | } | |
233 | // fall through to the error handling code | |
234 | } | |
235 | catch (const std::exception &x) { // TODO: should we catch ... as well? | |
236 | debugs(20, 2, "error keeping entry " << index << | |
237 | ' ' << *e << ": " << x.what()); | |
238 | // fall through to the error handling code | |
239 | } | |
240 | ||
4475555f | 241 | map->abortWriting(index); |
9a9954ba AR |
242 | } |
243 | ||
244 | ||
245 | /// copies all relevant local data to shared memory | |
246 | bool | |
247 | Transients::copyToShm(const StoreEntry &e, const sfileno index, | |
248 | const RequestFlags &reqFlags, | |
249 | const HttpRequestMethod &reqMethod) | |
250 | { | |
251 | TransientsMap::Extras &extras = map->extras(index); | |
252 | ||
253 | const char *url = e.url(); | |
254 | const size_t urlLen = strlen(url); | |
255 | Must(urlLen < sizeof(extras.url)); // we have space to store it all, plus 0 | |
256 | strncpy(extras.url, url, sizeof(extras.url)); | |
257 | extras.url[urlLen] = '\0'; | |
258 | ||
259 | extras.reqFlags = reqFlags; | |
9a9954ba AR |
260 | |
261 | Must(reqMethod != Http::METHOD_OTHER); | |
262 | extras.reqMethod = reqMethod.id(); | |
263 | ||
264 | return true; | |
265 | } | |
266 | ||
267 | void | |
268 | Transients::noteFreeMapSlice(const sfileno sliceId) | |
269 | { | |
270 | // TODO: we should probably find the entry being deleted and abort it | |
271 | } | |
272 | ||
4475555f AR |
273 | void |
274 | Transients::abandon(const StoreEntry &e) | |
275 | { | |
276 | assert(e.mem_obj && map); | |
277 | map->freeEntry(e.mem_obj->xitTable.index); // just marks the locked entry | |
278 | // We do not unlock the entry now because the problem is most likely with | |
279 | // the server resource rather than a specific cache writer, so we want to | |
280 | // prevent other readers from collapsing requests for that resource. | |
281 | } | |
282 | ||
283 | bool | |
284 | Transients::abandoned(const StoreEntry &e) const | |
285 | { | |
286 | assert(e.mem_obj); | |
287 | return abandonedAt(e.mem_obj->xitTable.index); | |
288 | } | |
289 | ||
290 | /// whether an in-transit entry at the index is now abandoned by its writer | |
291 | bool | |
292 | Transients::abandonedAt(const sfileno index) const | |
293 | { | |
294 | assert(map); | |
295 | return map->readableEntry(index).waitingToBeFreed; | |
296 | } | |
297 | ||
99921d9d AR |
298 | void |
299 | Transients::completeWriting(const StoreEntry &e) | |
300 | { | |
301 | if (e.mem_obj && e.mem_obj->xitTable.index >= 0) { | |
302 | assert(e.mem_obj->xitTable.io == MemObject::ioWriting); | |
303 | map->closeForWriting(e.mem_obj->xitTable.index); | |
304 | e.mem_obj->xitTable.index = -1; | |
305 | e.mem_obj->xitTable.io = MemObject::ioDone; | |
306 | } | |
307 | } | |
308 | ||
d366a7fa AR |
309 | int |
310 | Transients::readers(const StoreEntry &e) const | |
311 | { | |
312 | if (e.mem_obj && e.mem_obj->xitTable.index >= 0) { | |
313 | assert(map); | |
314 | return map->peekAtEntry(e.mem_obj->xitTable.index).lock.readers; | |
315 | } | |
316 | return 0; | |
317 | } | |
318 | ||
4475555f AR |
319 | void |
320 | Transients::disconnect(MemObject &mem_obj) | |
321 | { | |
99921d9d AR |
322 | if (mem_obj.xitTable.index >= 0) { |
323 | assert(map); | |
324 | if (mem_obj.xitTable.io == MemObject::ioWriting) { | |
325 | map->abortWriting(mem_obj.xitTable.index); | |
326 | } else { | |
327 | assert(mem_obj.xitTable.io == MemObject::ioReading); | |
328 | map->closeForReading(mem_obj.xitTable.index); | |
329 | } | |
330 | mem_obj.xitTable.index = -1; | |
331 | mem_obj.xitTable.io = MemObject::ioDone; | |
332 | } | |
4475555f AR |
333 | } |
334 | ||
9a9954ba AR |
335 | /// calculates maximum number of entries we need to store and map |
336 | int64_t | |
337 | Transients::EntryLimit() | |
338 | { | |
339 | // TODO: we should also check whether any SMP-aware caching is configured | |
340 | if (!UsingSmp() || !Config.onoff.collapsed_forwarding) | |
341 | return 0; // no SMP collapsed forwarding possible or needed | |
342 | ||
343 | return 16*1024; // XXX: make configurable | |
344 | } | |
345 | ||
346 | /// initializes shared memory segment used by Transients | |
347 | class TransientsRr: public Ipc::Mem::RegisteredRunner | |
348 | { | |
349 | public: | |
350 | /* RegisteredRunner API */ | |
351 | TransientsRr(): mapOwner(NULL) {} | |
352 | virtual void run(const RunnerRegistry &); | |
353 | virtual ~TransientsRr(); | |
354 | ||
355 | protected: | |
356 | virtual void create(const RunnerRegistry &); | |
357 | ||
358 | private: | |
359 | TransientsMap::Owner *mapOwner; | |
360 | }; | |
361 | ||
362 | RunnerRegistrationEntry(rrAfterConfig, TransientsRr); | |
363 | ||
364 | void TransientsRr::run(const RunnerRegistry &r) | |
365 | { | |
366 | assert(Config.memShared.configured()); | |
367 | Ipc::Mem::RegisteredRunner::run(r); | |
368 | } | |
369 | ||
370 | void TransientsRr::create(const RunnerRegistry &) | |
371 | { | |
9a9954ba AR |
372 | if (!Config.onoff.collapsed_forwarding) |
373 | return; | |
374 | ||
375 | const int64_t entryLimit = Transients::EntryLimit(); | |
9a9954ba AR |
376 | if (entryLimit <= 0) |
377 | return; // no SMP configured or a misconfiguration | |
378 | ||
379 | Must(!mapOwner); | |
380 | mapOwner = TransientsMap::Init(MapLabel, entryLimit); | |
381 | } | |
382 | ||
383 | TransientsRr::~TransientsRr() | |
384 | { | |
385 | delete mapOwner; | |
386 | } |