2 * DEBUG: section 20 Memory Cache
7 #include "base/RunnersRegistry.h"
8 #include "CollapsedForwarding.h"
10 #include "ipc/mem/Page.h"
11 #include "ipc/mem/Pages.h"
12 #include "MemObject.h"
14 #include "mime_header.h"
15 #include "SquidConfig.h"
16 #include "SquidMath.h"
17 #include "StoreStats.h"
20 /// shared memory segment path to use for MemStore maps
21 static const char *MapLabel
= "cache_mem_map";
22 /// shared memory segment path to use for the free slices index
23 static const char *SpaceLabel
= "cache_mem_space";
24 // TODO: sync with Rock::SwapDir::*Path()
26 // We store free slot IDs (i.e., "space") as Page objects so that we can use
27 // Ipc::Mem::PageStack. Pages require pool IDs. The value here is not really
28 // used except for a positivity test. A unique value is handy for debugging.
29 static const uint32_t SpacePoolId
= 510716;
32 MemStore::MemStore(): map(NULL
), lastWritingSlice(-1)
44 const int64_t entryLimit
= EntryLimit();
46 return; // no memory cache configured or a misconfiguration
48 // check compatibility with the disk cache, if any
49 if (Config
.cacheSwap
.n_configured
> 0) {
50 const int64_t diskMaxSize
= Store::Root().maxObjectSize();
51 const int64_t memMaxSize
= maxObjectSize();
52 if (diskMaxSize
== -1) {
53 debugs(20, DBG_IMPORTANT
, "WARNING: disk-cache maximum object size "
54 "is unlimited but mem-cache maximum object size is " <<
55 memMaxSize
/ 1024.0 << " KB");
56 } else if (diskMaxSize
> memMaxSize
) {
57 debugs(20, DBG_IMPORTANT
, "WARNING: disk-cache maximum object size "
58 "is too large for mem-cache: " <<
59 diskMaxSize
/ 1024.0 << " KB > " <<
60 memMaxSize
/ 1024.0 << " KB");
64 freeSlots
= shm_old(Ipc::Mem::PageStack
)(SpaceLabel
);
67 map
= new MemStoreMap(MapLabel
);
72 MemStore::getStats(StoreInfoStats
&stats
) const
74 const size_t pageSize
= Ipc::Mem::PageSize();
76 stats
.mem
.shared
= true;
78 Ipc::Mem::PageLimit(Ipc::Mem::PageId::cachePage
) * pageSize
;
80 Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) * pageSize
;
81 stats
.mem
.count
= currentCount();
85 MemStore::stat(StoreEntry
&e
) const
87 storeAppendPrintf(&e
, "\n\nShared Memory Cache\n");
89 storeAppendPrintf(&e
, "Maximum Size: %.0f KB\n", maxSize()/1024.0);
90 storeAppendPrintf(&e
, "Current Size: %.2f KB %.2f%%\n",
91 currentSize() / 1024.0,
92 Math::doublePercent(currentSize(), maxSize()));
95 const int limit
= map
->entryLimit();
96 storeAppendPrintf(&e
, "Maximum entries: %9d\n", limit
);
98 storeAppendPrintf(&e
, "Current entries: %" PRId64
" %.2f%%\n",
99 currentCount(), (100.0 * currentCount() / limit
));
101 const unsigned int slotsFree
=
102 Ipc::Mem::PagesAvailable(Ipc::Mem::PageId::cachePage
);
103 if (slotsFree
<= static_cast<const unsigned int>(limit
)) {
104 const int usedSlots
= limit
- static_cast<const int>(slotsFree
);
105 storeAppendPrintf(&e
, "Used slots: %9d %.2f%%\n",
106 usedSlots
, (100.0 * usedSlots
/ limit
));
109 if (limit
< 100) { // XXX: otherwise too expensive to count
110 Ipc::ReadWriteLockStats stats
;
111 map
->updateStats(stats
);
124 MemStore::minSize() const
126 return 0; // XXX: irrelevant, but Store parent forces us to implement this
130 MemStore::maxSize() const
132 return Config
.memMaxSize
;
136 MemStore::currentSize() const
138 return Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) *
139 Ipc::Mem::PageSize();
143 MemStore::currentCount() const
145 return map
? map
->entryCount() : 0;
149 MemStore::maxObjectSize() const
151 return min(Config
.Store
.maxInMemObjSize
, Config
.memMaxSize
);
155 MemStore::reference(StoreEntry
&)
160 MemStore::dereference(StoreEntry
&, bool)
162 // no need to keep e in the global store_table for us; we have our own map
173 MemStore::search(String
const, HttpRequest
*)
175 fatal("not implemented");
180 MemStore::get(const cache_key
*key
)
186 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(key
, index
);
190 // create a brand new store entry and initialize it with stored info
191 StoreEntry
*e
= new StoreEntry();
193 // XXX: We do not know the URLs yet, only the key, but we need to parse and
194 // store the response for the Root().get() callers to be happy because they
195 // expect IN_MEMORY entries to already have the response headers and body.
198 anchorEntry(*e
, index
, *slot
);
200 const bool copied
= copyFromShm(*e
, index
, *slot
);
207 debugs(20, 3, HERE
<< "mem-loading failed; freeing " << index
);
208 map
->freeEntry(index
); // do not let others into the same trap
213 MemStore::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
215 // XXX: not needed but Store parent forces us to implement this
216 fatal("MemStore::get(key,callback,data) should not be called");
220 MemStore::anchorCollapsed(StoreEntry
&collapsed
, bool &inSync
)
226 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(
227 reinterpret_cast<cache_key
*>(collapsed
.key
), index
);
231 anchorEntry(collapsed
, index
, *slot
);
232 inSync
= updateCollapsedWith(collapsed
, index
, *slot
);
233 return true; // even if inSync is false
237 MemStore::updateCollapsed(StoreEntry
&collapsed
)
239 assert(collapsed
.mem_obj
);
241 const sfileno index
= collapsed
.mem_obj
->memCache
.index
;
243 // already disconnected from the cache, no need to update
250 const Ipc::StoreMapAnchor
&anchor
= map
->readableEntry(index
);
251 return updateCollapsedWith(collapsed
, index
, anchor
);
254 /// updates collapsed entry after its anchor has been located
256 MemStore::updateCollapsedWith(StoreEntry
&collapsed
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
258 collapsed
.swap_file_sz
= anchor
.basics
.swap_file_sz
;
259 const bool copied
= copyFromShm(collapsed
, index
, anchor
);
263 /// anchors StoreEntry to an already locked map entry
265 MemStore::anchorEntry(StoreEntry
&e
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
267 const Ipc::StoreMapAnchor::Basics
&basics
= anchor
.basics
;
269 e
.swap_file_sz
= basics
.swap_file_sz
;
270 e
.lastref
= basics
.lastref
;
271 e
.timestamp
= basics
.timestamp
;
272 e
.expires
= basics
.expires
;
273 e
.lastmod
= basics
.lastmod
;
274 e
.refcount
= basics
.refcount
;
275 e
.flags
= basics
.flags
;
278 if (anchor
.complete()) {
279 e
.store_status
= STORE_OK
;
280 e
.mem_obj
->object_sz
= e
.swap_file_sz
;
281 e
.setMemStatus(IN_MEMORY
);
283 e
.store_status
= STORE_PENDING
;
284 assert(e
.mem_obj
->object_sz
< 0);
285 e
.setMemStatus(NOT_IN_MEMORY
);
287 assert(e
.swap_status
== SWAPOUT_NONE
); // set in StoreEntry constructor
288 e
.ping_status
= PING_NONE
;
290 EBIT_CLR(e
.flags
, RELEASE_REQUEST
);
291 EBIT_CLR(e
.flags
, KEY_PRIVATE
);
292 EBIT_SET(e
.flags
, ENTRY_VALIDATED
);
294 MemObject::MemCache
&mc
= e
.mem_obj
->memCache
;
296 mc
.io
= MemObject::ioReading
;
299 /// copies the entire entry from shared to local memory
301 MemStore::copyFromShm(StoreEntry
&e
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
303 debugs(20, 7, "mem-loading entry " << index
<< " from " << anchor
.start
);
306 // emulate the usual Store code but w/o inapplicable checks and callbacks:
308 Ipc::StoreMapSliceId sid
= anchor
.start
; // optimize: remember the last sid
309 bool wasEof
= anchor
.complete() && sid
< 0;
310 int64_t sliceOffset
= 0;
312 const Ipc::StoreMapSlice
&slice
= map
->readableSlice(index
, sid
);
313 // slice state may change during copying; take snapshots now
314 wasEof
= anchor
.complete() && slice
.next
< 0;
315 const Ipc::StoreMapSlice::Size wasSize
= slice
.size
;
317 debugs(20, 9, "entry " << index
<< " slice " << sid
<< " eof " <<
318 wasEof
<< " wasSize " << wasSize
<< " <= " <<
319 anchor
.basics
.swap_file_sz
<< " sliceOffset " << sliceOffset
<<
320 " mem.endOffset " << e
.mem_obj
->endOffset());
322 if (e
.mem_obj
->endOffset() < sliceOffset
+ wasSize
) {
323 // size of the slice data that we already copied
324 const size_t prefixSize
= e
.mem_obj
->endOffset() - sliceOffset
;
325 assert(prefixSize
<= wasSize
);
327 const MemStoreMap::Extras
&extras
= map
->extras(sid
);
328 char *page
= static_cast<char*>(PagePointer(extras
.page
));
329 const StoreIOBuffer
sliceBuf(wasSize
- prefixSize
,
330 e
.mem_obj
->endOffset(),
332 if (!copyFromShmSlice(e
, sliceBuf
, wasEof
))
334 debugs(20, 9, "entry " << index
<< " copied slice " << sid
<<
335 " from " << extras
.page
<< " +" << prefixSize
);
337 // else skip a [possibly incomplete] slice that we copied earlier
339 // careful: the slice may have grown _and_ gotten the next slice ID!
340 if (slice
.next
>= 0) {
342 // here we know that slice.size may not change any more
343 if (wasSize
>= slice
.size
) { // did not grow since we started copying
344 sliceOffset
+= wasSize
;
347 } else if (wasSize
>= slice
.size
) { // did not grow
353 debugs(20, 7, "mem-loaded " << e
.mem_obj
->endOffset() << '/' <<
354 anchor
.basics
.swap_file_sz
<< " bytes of " << e
);
358 debugs(20, 7, "mem-loaded all " << e
.mem_obj
->object_sz
<< '/' <<
359 anchor
.basics
.swap_file_sz
<< " bytes of " << e
);
361 // from StoreEntry::complete()
362 e
.mem_obj
->object_sz
= e
.mem_obj
->endOffset();
363 e
.store_status
= STORE_OK
;
364 e
.setMemStatus(IN_MEMORY
);
366 assert(e
.mem_obj
->object_sz
>= 0);
367 assert(static_cast<uint64_t>(e
.mem_obj
->object_sz
) == anchor
.basics
.swap_file_sz
);
368 // would be nice to call validLength() here, but it needs e.key
370 // we read the entire response into the local memory; no more need to lock
375 /// imports one shared memory slice into local memory
377 MemStore::copyFromShmSlice(StoreEntry
&e
, const StoreIOBuffer
&buf
, bool eof
)
379 debugs(20, 7, "buf: " << buf
.offset
<< " + " << buf
.length
);
381 // from store_client::readBody()
382 // parse headers if needed; they might span multiple slices!
383 HttpReply
*rep
= (HttpReply
*)e
.getReply();
384 if (rep
->pstate
< psParsed
) {
385 // XXX: have to copy because httpMsgParseStep() requires 0-termination
387 mb
.init(buf
.length
+1, buf
.length
+1);
388 mb
.append(buf
.data
, buf
.length
);
390 const int result
= rep
->httpMsgParseStep(mb
.buf
, buf
.length
, eof
);
392 assert(rep
->pstate
== psParsed
);
393 EBIT_CLR(e
.flags
, ENTRY_FWD_HDR_WAIT
);
394 } else if (result
< 0) {
395 debugs(20, DBG_IMPORTANT
, "Corrupted mem-cached headers: " << e
);
397 } else { // more slices are needed
401 debugs(20, 7, "rep pstate: " << rep
->pstate
);
403 // local memory stores both headers and body so copy regardless of pstate
404 const int64_t offBefore
= e
.mem_obj
->endOffset();
405 assert(e
.mem_obj
->data_hdr
.write(buf
)); // from MemObject::write()
406 const int64_t offAfter
= e
.mem_obj
->endOffset();
407 // expect to write the entire buf because StoreEntry::write() never fails
408 assert(offAfter
>= 0 && offBefore
<= offAfter
&&
409 static_cast<size_t>(offAfter
- offBefore
) == buf
.length
);
413 /// whether we should cache the entry
415 MemStore::shouldCache(const StoreEntry
&e
) const
417 if (e
.mem_status
== IN_MEMORY
) {
418 debugs(20, 5, "already loaded from mem-cache: " << e
);
422 if (e
.mem_obj
&& e
.mem_obj
->memCache
.offset
> 0) {
423 debugs(20, 5, "already written to mem-cache: " << e
);
427 if (!e
.memoryCachable()) {
428 debugs(20, 7, HERE
<< "Not memory cachable: " << e
);
429 return false; // will not cache due to entry state or properties
433 const int64_t expectedSize
= e
.mem_obj
->expectedReplySize(); // may be < 0
435 // objects of unknown size are not allowed into memory cache, for now
436 if (expectedSize
< 0) {
437 debugs(20, 5, HERE
<< "Unknown expected size: " << e
);
441 const int64_t loadedSize
= e
.mem_obj
->endOffset();
442 const int64_t ramSize
= max(loadedSize
, expectedSize
);
444 if (ramSize
> maxObjectSize()) {
445 debugs(20, 5, HERE
<< "Too big max(" <<
446 loadedSize
<< ", " << expectedSize
<< "): " << e
);
447 return false; // will not cache due to cachable entry size limits
451 debugs(20, 5, HERE
<< "No map to mem-cache " << e
);
455 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) {
456 debugs(20, 5, HERE
<< "Not mem-caching ENTRY_SPECIAL " << e
);
463 /// locks map anchor and preps to store the entry in shared memory
465 MemStore::startCaching(StoreEntry
&e
)
468 Ipc::StoreMapAnchor
*slot
= map
->openForWriting(reinterpret_cast<const cache_key
*>(e
.key
), index
);
470 debugs(20, 5, HERE
<< "No room in mem-cache map to index " << e
);
475 e
.mem_obj
->memCache
.index
= index
;
476 e
.mem_obj
->memCache
.io
= MemObject::ioWriting
;
478 map
->startAppending(index
);
482 /// copies all local data to shared memory
484 MemStore::copyToShm(StoreEntry
&e
)
486 // prevents remote readers from getting ENTRY_FWD_HDR_WAIT entries and
487 // not knowing when the wait is over
488 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) {
489 debugs(20, 5, "postponing copying " << e
<< " for ENTRY_FWD_HDR_WAIT");
496 const int32_t index
= e
.mem_obj
->memCache
.index
;
498 Ipc::StoreMapAnchor
&anchor
= map
->writeableEntry(index
);
500 const int64_t eSize
= e
.mem_obj
->endOffset();
501 if (e
.mem_obj
->memCache
.offset
>= eSize
) {
502 debugs(20, 5, "postponing copying " << e
<< " for lack of news: " <<
503 e
.mem_obj
->memCache
.offset
<< " >= " << eSize
);
504 return; // nothing to do (yet)
507 if (anchor
.start
< 0) { // must allocate the very first slot for e
508 Ipc::Mem::PageId page
;
509 anchor
.start
= reserveSapForWriting(page
); // throws
510 map
->extras(anchor
.start
).page
= page
;
513 lastWritingSlice
= anchor
.start
;
514 const size_t sliceCapacity
= Ipc::Mem::PageSize();
516 // fill, skip slices that are already full
517 // Optimize: remember lastWritingSlice in e.mem_obj
518 while (e
.mem_obj
->memCache
.offset
< eSize
) {
519 Ipc::StoreMap::Slice
&slice
=
520 map
->writeableSlice(e
.mem_obj
->memCache
.index
, lastWritingSlice
);
522 if (slice
.size
>= sliceCapacity
) {
523 if (slice
.next
>= 0) {
524 lastWritingSlice
= slice
.next
;
528 Ipc::Mem::PageId page
;
529 slice
.next
= lastWritingSlice
= reserveSapForWriting(page
);
530 map
->extras(lastWritingSlice
).page
= page
;
531 debugs(20, 7, "entry " << index
<< " new slice: " << lastWritingSlice
);
534 copyToShmSlice(e
, anchor
);
537 anchor
.basics
.swap_file_sz
= e
.mem_obj
->memCache
.offset
;
538 debugs(20, 7, "mem-cached available " << eSize
<< " bytes of " << e
);
541 /// copies at most one slice worth of local memory to shared memory
543 MemStore::copyToShmSlice(StoreEntry
&e
, Ipc::StoreMapAnchor
&anchor
)
545 Ipc::StoreMap::Slice
&slice
=
546 map
->writeableSlice(e
.mem_obj
->memCache
.index
, lastWritingSlice
);
548 Ipc::Mem::PageId page
= map
->extras(lastWritingSlice
).page
;
549 assert(lastWritingSlice
>= 0 && page
);
550 debugs(20, 7, "entry " << e
<< " slice " << lastWritingSlice
<< " has " <<
553 const int64_t bufSize
= Ipc::Mem::PageSize();
554 const int64_t sliceOffset
= e
.mem_obj
->memCache
.offset
% bufSize
;
555 StoreIOBuffer
sharedSpace(bufSize
- sliceOffset
, e
.mem_obj
->memCache
.offset
,
556 static_cast<char*>(PagePointer(page
)) + sliceOffset
);
558 // check that we kept everything or purge incomplete/sparse cached entry
559 const ssize_t copied
= e
.mem_obj
->data_hdr
.copy(sharedSpace
);
561 debugs(20, 2, "Failed to mem-cache " << (bufSize
- sliceOffset
) <<
562 " bytes of " << e
<< " from " << e
.mem_obj
->memCache
.offset
<<
564 throw TexcHere("data_hdr.copy failure");
567 debugs(20, 7, "mem-cached " << copied
<< " bytes of " << e
<<
568 " from " << e
.mem_obj
->memCache
.offset
<< " in " << page
);
570 slice
.size
+= copied
;
571 e
.mem_obj
->memCache
.offset
+= copied
;
574 /// finds a slot and a free page to fill or throws
576 MemStore::reserveSapForWriting(Ipc::Mem::PageId
&page
)
578 Ipc::Mem::PageId slot
;
579 if (freeSlots
->pop(slot
)) {
580 debugs(20, 5, "got a previously free slot: " << slot
);
582 if (Ipc::Mem::GetPage(Ipc::Mem::PageId::cachePage
, page
)) {
583 debugs(20, 5, "and got a previously free page: " << page
);
584 return slot
.number
- 1;
586 debugs(20, 3, "but there is no free page, returning " << slot
);
587 freeSlots
->push(slot
);
591 // catch free slots delivered to noteFreeMapSlice()
593 waitingFor
.slot
= &slot
;
594 waitingFor
.page
= &page
;
595 if (map
->purgeOne()) {
596 assert(!waitingFor
); // noteFreeMapSlice() should have cleared it
599 debugs(20, 5, "got previously busy " << slot
<< " and " << page
);
600 return slot
.number
- 1;
602 assert(waitingFor
.slot
== &slot
&& waitingFor
.page
== &page
);
603 waitingFor
.slot
= NULL
;
604 waitingFor
.page
= NULL
;
606 debugs(47, 3, "cannot get a slice; entries: " << map
->entryCount());
607 throw TexcHere("ran out of mem-cache slots");
611 MemStore::noteFreeMapSlice(const sfileno sliceId
)
613 Ipc::Mem::PageId
&pageId
= map
->extras(sliceId
).page
;
614 debugs(20, 9, "slice " << sliceId
<< " freed " << pageId
);
616 Ipc::Mem::PageId slotId
;
617 slotId
.pool
= SpacePoolId
;
618 slotId
.number
= sliceId
+ 1;
620 // must zero pageId before we give slice (and pageId extras!) to others
621 Ipc::Mem::PutPage(pageId
);
622 freeSlots
->push(slotId
);
624 *waitingFor
.slot
= slotId
;
625 *waitingFor
.page
= pageId
;
626 waitingFor
.slot
= NULL
;
627 waitingFor
.page
= NULL
;
628 pageId
= Ipc::Mem::PageId();
633 MemStore::write(StoreEntry
&e
)
637 debugs(20, 7, "entry " << e
);
639 switch (e
.mem_obj
->memCache
.io
) {
640 case MemObject::ioUndecided
:
641 if (!shouldCache(e
) || !startCaching(e
)) {
642 e
.mem_obj
->memCache
.io
= MemObject::ioDone
;
643 Store::Root().transientsAbandon(e
);
648 case MemObject::ioDone
:
649 case MemObject::ioReading
:
650 return; // we should not write in all of the above cases
652 case MemObject::ioWriting
:
653 break; // already decided to write and still writing
658 if (e
.store_status
== STORE_OK
) // done receiving new content
661 CollapsedForwarding::Broadcast(e
);
664 catch (const std::exception
&x
) { // TODO: should we catch ... as well?
665 debugs(20, 2, "mem-caching error writing entry " << e
<< ": " << x
.what());
666 // fall through to the error handling code
673 MemStore::completeWriting(StoreEntry
&e
)
676 const int32_t index
= e
.mem_obj
->memCache
.index
;
680 debugs(20, 5, "mem-cached all " << e
.mem_obj
->memCache
.offset
<< " bytes of " << e
);
682 e
.mem_obj
->memCache
.index
= -1;
683 e
.mem_obj
->memCache
.io
= MemObject::ioDone
;
684 map
->closeForWriting(index
, false);
686 CollapsedForwarding::Broadcast(e
); // before we close our transient entry!
687 Store::Root().transientsCompleteWriting(e
);
691 MemStore::markForUnlink(StoreEntry
&e
)
694 if (e
.mem_obj
->memCache
.index
>= 0)
695 map
->freeEntry(e
.mem_obj
->memCache
.index
);
699 MemStore::unlink(StoreEntry
&e
)
702 if (e
.mem_obj
->memCache
.index
>= 0) {
703 map
->freeEntry(e
.mem_obj
->memCache
.index
);
706 // the entry may have been loaded and then disconnected from the cache
707 map
->freeEntryByKey(reinterpret_cast<cache_key
*>(e
.key
));
710 e
.destroyMemObject(); // XXX: but it may contain useful info such as a client list. The old code used to do that though, right?
714 MemStore::disconnect(StoreEntry
&e
)
717 MemObject
&mem_obj
= *e
.mem_obj
;
718 if (mem_obj
.memCache
.index
>= 0) {
719 if (mem_obj
.memCache
.io
== MemObject::ioWriting
) {
720 map
->abortWriting(mem_obj
.memCache
.index
);
721 mem_obj
.memCache
.index
= -1;
722 mem_obj
.memCache
.io
= MemObject::ioDone
;
723 Store::Root().transientsAbandon(e
); // broadcasts after the change
725 assert(mem_obj
.memCache
.io
== MemObject::ioReading
);
726 map
->closeForReading(mem_obj
.memCache
.index
);
727 mem_obj
.memCache
.index
= -1;
728 mem_obj
.memCache
.io
= MemObject::ioDone
;
733 /// calculates maximum number of entries we need to store and map
735 MemStore::EntryLimit()
737 if (!Config
.memShared
|| !Config
.memMaxSize
)
738 return 0; // no memory cache configured
740 const int64_t minEntrySize
= Ipc::Mem::PageSize();
741 const int64_t entryLimit
= Config
.memMaxSize
/ minEntrySize
;
745 /// reports our needs for shared memory pages to Ipc::Mem::Pages
746 class MemStoreClaimMemoryNeedsRr
: public RegisteredRunner
749 /* RegisteredRunner API */
750 virtual void run(const RunnerRegistry
&r
);
753 RunnerRegistrationEntry(rrClaimMemoryNeeds
, MemStoreClaimMemoryNeedsRr
);
756 MemStoreClaimMemoryNeedsRr::run(const RunnerRegistry
&)
758 Ipc::Mem::NotePageNeed(Ipc::Mem::PageId::cachePage
, MemStore::EntryLimit());
761 /// decides whether to use a shared memory cache or checks its configuration
762 class MemStoreCfgRr
: public ::RegisteredRunner
765 /* RegisteredRunner API */
766 virtual void run(const RunnerRegistry
&);
769 RunnerRegistrationEntry(rrFinalizeConfig
, MemStoreCfgRr
);
771 void MemStoreCfgRr::run(const RunnerRegistry
&r
)
773 // decide whether to use a shared memory cache if the user did not specify
774 if (!Config
.memShared
.configured()) {
775 Config
.memShared
.configure(Ipc::Atomic::Enabled() &&
776 Ipc::Mem::Segment::Enabled() && UsingSmp() &&
777 Config
.memMaxSize
> 0);
778 } else if (Config
.memShared
&& !Ipc::Atomic::Enabled()) {
779 // bail if the user wants shared memory cache but we cannot support it
780 fatal("memory_cache_shared is on, but no support for atomic operations detected");
781 } else if (Config
.memShared
&& !Ipc::Mem::Segment::Enabled()) {
782 fatal("memory_cache_shared is on, but no support for shared memory detected");
783 } else if (Config
.memShared
&& !UsingSmp()) {
784 debugs(20, DBG_IMPORTANT
, "WARNING: memory_cache_shared is on, but only"
785 " a single worker is running");
789 /// initializes shared memory segments used by MemStore
790 class MemStoreRr
: public Ipc::Mem::RegisteredRunner
793 /* RegisteredRunner API */
794 MemStoreRr(): spaceOwner(NULL
), mapOwner(NULL
) {}
795 virtual void run(const RunnerRegistry
&);
796 virtual ~MemStoreRr();
799 virtual void create(const RunnerRegistry
&);
802 Ipc::Mem::Owner
<Ipc::Mem::PageStack
> *spaceOwner
; ///< free slices Owner
803 MemStoreMap::Owner
*mapOwner
; ///< primary map Owner
806 RunnerRegistrationEntry(rrAfterConfig
, MemStoreRr
);
808 void MemStoreRr::run(const RunnerRegistry
&r
)
810 assert(Config
.memShared
.configured());
811 Ipc::Mem::RegisteredRunner::run(r
);
814 void MemStoreRr::create(const RunnerRegistry
&)
816 if (!Config
.memShared
)
819 const int64_t entryLimit
= MemStore::EntryLimit();
820 if (entryLimit
<= 0) {
821 if (Config
.memMaxSize
> 0) {
822 debugs(20, DBG_IMPORTANT
, "WARNING: mem-cache size is too small ("
823 << (Config
.memMaxSize
/ 1024.0) << " KB), should be >= " <<
824 (Ipc::Mem::PageSize() / 1024.0) << " KB");
826 return; // no memory cache configured or a misconfiguration
830 spaceOwner
= shm_new(Ipc::Mem::PageStack
)(SpaceLabel
, SpacePoolId
,
832 sizeof(Ipc::Mem::PageId
));
834 mapOwner
= MemStoreMap::Init(MapLabel
, entryLimit
);
837 MemStoreRr::~MemStoreRr()