]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/Transients.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 20 Storage Manager */
12 #include "base/RunnersRegistry.h"
13 #include "CollapsedForwarding.h"
14 #include "HttpReply.h"
15 #include "ipc/mem/Page.h"
16 #include "ipc/mem/Pages.h"
17 #include "MemObject.h"
18 #include "mime_header.h"
19 #include "SquidConfig.h"
20 #include "SquidMath.h"
21 #include "StoreStats.h"
23 #include "Transients.h"
27 /// shared memory segment path to use for Transients map
28 static const SBuf
MapLabel("transients_map");
29 /// shared memory segment path to use for Transients map extras
30 static const char *ExtrasLabel
= "transients_ex";
32 Transients::Transients(): map(NULL
), locals(NULL
)
36 Transients::~Transients()
45 const int64_t entryLimit
= EntryLimit();
47 return; // no SMP support or a misconfiguration
50 map
= new TransientsMap(MapLabel
);
53 extras
= shm_old(TransientsMapExtras
)(ExtrasLabel
);
55 locals
= new Locals(entryLimit
, 0);
59 Transients::getStats(StoreInfoStats
&stats
) const
61 #if TRANSIENT_STATS_SUPPORTED
62 const size_t pageSize
= Ipc::Mem::PageSize();
64 stats
.mem
.shared
= true;
66 Ipc::Mem::PageLimit(Ipc::Mem::PageId::cachePage
) * pageSize
;
68 Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) * pageSize
;
69 stats
.mem
.count
= currentCount();
74 Transients::stat(StoreEntry
&e
) const
76 storeAppendPrintf(&e
, "\n\nTransient Objects\n");
78 storeAppendPrintf(&e
, "Maximum Size: %.0f KB\n", maxSize()/1024.0);
79 storeAppendPrintf(&e
, "Current Size: %.2f KB %.2f%%\n",
80 currentSize() / 1024.0,
81 Math::doublePercent(currentSize(), maxSize()));
84 const int limit
= map
->entryLimit();
85 storeAppendPrintf(&e
, "Maximum entries: %9d\n", limit
);
87 storeAppendPrintf(&e
, "Current entries: %" PRId64
" %.2f%%\n",
88 currentCount(), (100.0 * currentCount() / limit
));
94 Transients::maintain()
96 // no lazy garbage collection needed
100 Transients::minSize() const
102 return 0; // XXX: irrelevant, but Store parent forces us to implement this
106 Transients::maxSize() const
108 // Squid currently does not limit the total size of all transient objects
109 return std::numeric_limits
<uint64_t>::max();
113 Transients::currentSize() const
115 // TODO: we do not get enough information to calculate this
116 // StoreEntry should update associated stores when its size changes
121 Transients::currentCount() const
123 return map
? map
->entryCount() : 0;
127 Transients::maxObjectSize() const
129 // Squid currently does not limit the size of a transient object
130 return std::numeric_limits
<uint64_t>::max();
134 Transients::reference(StoreEntry
&)
136 // no replacement policy (but the cache(s) storing the entry may have one)
140 Transients::dereference(StoreEntry
&, bool)
142 // no need to keep e in the global store_table for us; we have our own map
147 Transients::callback()
153 Transients::search(String
const, HttpRequest
*)
155 fatal("not implemented");
160 Transients::get(const cache_key
*key
)
166 const Ipc::StoreMapAnchor
*anchor
= map
->openForReading(key
, index
);
170 // If we already have a local entry, the store_table should have found it.
171 // Since it did not, the local entry key must have changed from public to
172 // private. We still need to keep the private entry around for syncing as
173 // its clients depend on it, but we should not allow new clients to join.
174 if (StoreEntry
*oldE
= locals
->at(index
)) {
175 debugs(20, 3, "not joining private " << *oldE
);
176 assert(EBIT_TEST(oldE
->flags
, KEY_PRIVATE
));
177 } else if (StoreEntry
*newE
= copyFromShm(index
)) {
178 return newE
; // keep read lock to receive updates from others
181 // private entry or loading failure
182 map
->closeForReading(index
);
187 Transients::copyFromShm(const sfileno index
)
189 const TransientsMapExtras::Item
&extra
= extras
->items
[index
];
191 // create a brand new store entry and initialize it with stored info
192 StoreEntry
*e
= storeCreatePureEntry(extra
.url
, extra
.url
,
193 extra
.reqFlags
, extra
.reqMethod
);
196 e
->mem_obj
->method
= extra
.reqMethod
;
197 e
->mem_obj
->xitTable
.io
= MemObject::ioReading
;
198 e
->mem_obj
->xitTable
.index
= index
;
203 // How do we know its SMP- and not just locally-collapsed? A worker gets
204 // locally-collapsed entries from the local store_table, not Transients.
205 // TODO: Can we remove smpCollapsed by not syncing non-transient entries?
206 e
->mem_obj
->smpCollapsed
= true;
208 assert(!locals
->at(index
));
209 // We do not lock e because we do not want to prevent its destruction;
210 // e is tied to us via mem_obj so we will know when it is destructed.
211 locals
->at(index
) = e
;
216 Transients::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
218 // XXX: not needed but Store parent forces us to implement this
219 fatal("Transients::get(key,callback,data) should not be called");
223 Transients::findCollapsed(const sfileno index
)
228 if (StoreEntry
*oldE
= locals
->at(index
)) {
229 debugs(20, 5, "found " << *oldE
<< " at " << index
<< " in " << MapLabel
);
230 assert(oldE
->mem_obj
&& oldE
->mem_obj
->xitTable
.index
== index
);
234 debugs(20, 3, "no entry at " << index
<< " in " << MapLabel
);
239 Transients::startWriting(StoreEntry
*e
, const RequestFlags
&reqFlags
,
240 const HttpRequestMethod
&reqMethod
)
244 assert(e
->mem_obj
->xitTable
.index
< 0);
247 debugs(20, 5, "No map to add " << *e
);
252 Ipc::StoreMapAnchor
*slot
= map
->openForWriting(reinterpret_cast<const cache_key
*>(e
->key
), index
);
254 debugs(20, 5, "collision registering " << *e
);
259 if (copyToShm(*e
, index
, reqFlags
, reqMethod
)) {
261 e
->mem_obj
->xitTable
.io
= MemObject::ioWriting
;
262 e
->mem_obj
->xitTable
.index
= index
;
263 map
->startAppending(index
);
264 // keep write lock -- we will be supplying others with updates
267 // fall through to the error handling code
268 } catch (const std::exception
&x
) { // TODO: should we catch ... as well?
269 debugs(20, 2, "error keeping entry " << index
<<
270 ' ' << *e
<< ": " << x
.what());
271 // fall through to the error handling code
274 map
->abortWriting(index
);
277 /// copies all relevant local data to shared memory
279 Transients::copyToShm(const StoreEntry
&e
, const sfileno index
,
280 const RequestFlags
&reqFlags
,
281 const HttpRequestMethod
&reqMethod
)
283 TransientsMapExtras::Item
&extra
= extras
->items
[index
];
285 const char *url
= e
.url();
286 const size_t urlLen
= strlen(url
);
287 Must(urlLen
< sizeof(extra
.url
)); // we have space to store it all, plus 0
288 strncpy(extra
.url
, url
, sizeof(extra
.url
));
289 extra
.url
[urlLen
] = '\0';
291 extra
.reqFlags
= reqFlags
;
293 Must(reqMethod
!= Http::METHOD_OTHER
);
294 extra
.reqMethod
= reqMethod
.id();
300 Transients::noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId
)
302 // TODO: we should probably find the entry being deleted and abort it
306 Transients::abandon(const StoreEntry
&e
)
308 assert(e
.mem_obj
&& map
);
309 map
->freeEntry(e
.mem_obj
->xitTable
.index
); // just marks the locked entry
310 CollapsedForwarding::Broadcast(e
);
311 // We do not unlock the entry now because the problem is most likely with
312 // the server resource rather than a specific cache writer, so we want to
313 // prevent other readers from collapsing requests for that resource.
317 Transients::abandoned(const StoreEntry
&e
) const
320 return abandonedAt(e
.mem_obj
->xitTable
.index
);
323 /// whether an in-transit entry at the index is now abandoned by its writer
325 Transients::abandonedAt(const sfileno index
) const
328 return map
->readableEntry(index
).waitingToBeFreed
;
332 Transients::completeWriting(const StoreEntry
&e
)
334 if (e
.mem_obj
&& e
.mem_obj
->xitTable
.index
>= 0) {
335 assert(e
.mem_obj
->xitTable
.io
== MemObject::ioWriting
);
336 // there will be no more updates from us after this, so we must prevent
337 // future readers from joining
338 map
->freeEntry(e
.mem_obj
->xitTable
.index
); // just marks the locked entry
339 map
->closeForWriting(e
.mem_obj
->xitTable
.index
);
340 e
.mem_obj
->xitTable
.index
= -1;
341 e
.mem_obj
->xitTable
.io
= MemObject::ioDone
;
346 Transients::readers(const StoreEntry
&e
) const
348 if (e
.mem_obj
&& e
.mem_obj
->xitTable
.index
>= 0) {
350 return map
->peekAtEntry(e
.mem_obj
->xitTable
.index
).lock
.readers
;
356 Transients::markForUnlink(StoreEntry
&e
)
358 if (e
.mem_obj
&& e
.mem_obj
->xitTable
.io
== MemObject::ioWriting
)
363 Transients::disconnect(MemObject
&mem_obj
)
365 if (mem_obj
.xitTable
.index
>= 0) {
367 if (mem_obj
.xitTable
.io
== MemObject::ioWriting
) {
368 map
->abortWriting(mem_obj
.xitTable
.index
);
370 assert(mem_obj
.xitTable
.io
== MemObject::ioReading
);
371 map
->closeForReading(mem_obj
.xitTable
.index
);
373 locals
->at(mem_obj
.xitTable
.index
) = NULL
;
374 mem_obj
.xitTable
.index
= -1;
375 mem_obj
.xitTable
.io
= MemObject::ioDone
;
379 /// calculates maximum number of entries we need to store and map
381 Transients::EntryLimit()
383 // TODO: we should also check whether any SMP-aware caching is configured
384 if (!UsingSmp() || !Config
.onoff
.collapsed_forwarding
)
385 return 0; // no SMP collapsed forwarding possible or needed
387 return 16*1024; // TODO: make configurable?
390 /// initializes shared memory segment used by Transients
391 class TransientsRr
: public Ipc::Mem::RegisteredRunner
394 /* RegisteredRunner API */
395 TransientsRr(): mapOwner(NULL
), extrasOwner(NULL
) {}
396 virtual void useConfig();
397 virtual ~TransientsRr();
400 virtual void create();
403 TransientsMap::Owner
*mapOwner
;
404 Ipc::Mem::Owner
<TransientsMapExtras
> *extrasOwner
;
407 RunnerRegistrationEntry(TransientsRr
);
410 TransientsRr::useConfig()
412 assert(Config
.memShared
.configured());
413 Ipc::Mem::RegisteredRunner::useConfig();
417 TransientsRr::create()
419 if (!Config
.onoff
.collapsed_forwarding
)
422 const int64_t entryLimit
= Transients::EntryLimit();
424 return; // no SMP configured or a misconfiguration
427 mapOwner
= TransientsMap::Init(MapLabel
, entryLimit
);
429 extrasOwner
= shm_new(TransientsMapExtras
)(ExtrasLabel
, entryLimit
);
432 TransientsRr::~TransientsRr()