]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemStore.cc
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;
31 MemStore::MemStore(): map(NULL
), lastWritingSlice(-1)
43 const int64_t entryLimit
= EntryLimit();
45 return; // no memory cache configured or a misconfiguration
47 // check compatibility with the disk cache, if any
48 if (Config
.cacheSwap
.n_configured
> 0) {
49 const int64_t diskMaxSize
= Store::Root().maxObjectSize();
50 const int64_t memMaxSize
= maxObjectSize();
51 if (diskMaxSize
== -1) {
52 debugs(20, DBG_IMPORTANT
, "WARNING: disk-cache maximum object size "
53 "is unlimited but mem-cache maximum object size is " <<
54 memMaxSize
/ 1024.0 << " KB");
55 } else if (diskMaxSize
> memMaxSize
) {
56 debugs(20, DBG_IMPORTANT
, "WARNING: disk-cache maximum object size "
57 "is too large for mem-cache: " <<
58 diskMaxSize
/ 1024.0 << " KB > " <<
59 memMaxSize
/ 1024.0 << " KB");
63 freeSlots
= shm_old(Ipc::Mem::PageStack
)(SpaceLabel
);
66 map
= new MemStoreMap(MapLabel
);
71 MemStore::getStats(StoreInfoStats
&stats
) const
73 const size_t pageSize
= Ipc::Mem::PageSize();
75 stats
.mem
.shared
= true;
77 Ipc::Mem::PageLimit(Ipc::Mem::PageId::cachePage
) * pageSize
;
79 Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) * pageSize
;
80 stats
.mem
.count
= currentCount();
84 MemStore::stat(StoreEntry
&e
) const
86 storeAppendPrintf(&e
, "\n\nShared Memory Cache\n");
88 storeAppendPrintf(&e
, "Maximum Size: %.0f KB\n", maxSize()/1024.0);
89 storeAppendPrintf(&e
, "Current Size: %.2f KB %.2f%%\n",
90 currentSize() / 1024.0,
91 Math::doublePercent(currentSize(), maxSize()));
94 const int limit
= map
->entryLimit();
95 storeAppendPrintf(&e
, "Maximum entries: %9d\n", limit
);
97 storeAppendPrintf(&e
, "Current entries: %" PRId64
" %.2f%%\n",
98 currentCount(), (100.0 * currentCount() / limit
));
100 const unsigned int slotsFree
=
101 Ipc::Mem::PagesAvailable(Ipc::Mem::PageId::cachePage
);
102 if (slotsFree
<= static_cast<const unsigned int>(limit
)) {
103 const int usedSlots
= limit
- static_cast<const int>(slotsFree
);
104 storeAppendPrintf(&e
, "Used slots: %9d %.2f%%\n",
105 usedSlots
, (100.0 * usedSlots
/ limit
));
108 if (limit
< 100) { // XXX: otherwise too expensive to count
109 Ipc::ReadWriteLockStats stats
;
110 map
->updateStats(stats
);
123 MemStore::minSize() const
125 return 0; // XXX: irrelevant, but Store parent forces us to implement this
129 MemStore::maxSize() const
131 return Config
.memMaxSize
;
135 MemStore::currentSize() const
137 return Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) *
138 Ipc::Mem::PageSize();
142 MemStore::currentCount() const
144 return map
? map
->entryCount() : 0;
148 MemStore::maxObjectSize() const
150 return min(Config
.Store
.maxInMemObjSize
, Config
.memMaxSize
);
154 MemStore::reference(StoreEntry
&)
159 MemStore::dereference(StoreEntry
&, bool)
161 // no need to keep e in the global store_table for us; we have our own map
172 MemStore::search(String
const, HttpRequest
*)
174 fatal("not implemented");
179 MemStore::get(const cache_key
*key
)
185 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(key
, index
);
189 // create a brand new store entry and initialize it with stored info
190 StoreEntry
*e
= new StoreEntry();
192 // XXX: We do not know the URLs yet, only the key, but we need to parse and
193 // store the response for the Root().get() callers to be happy because they
194 // expect IN_MEMORY entries to already have the response headers and body.
197 anchorEntry(*e
, index
, *slot
);
199 const bool copied
= copyFromShm(*e
, index
, *slot
);
206 debugs(20, 3, HERE
<< "mem-loading failed; freeing " << index
);
207 map
->freeEntry(index
); // do not let others into the same trap
212 MemStore::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
214 // XXX: not needed but Store parent forces us to implement this
215 fatal("MemStore::get(key,callback,data) should not be called");
219 MemStore::anchorCollapsed(StoreEntry
&collapsed
, bool &inSync
)
225 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(
226 reinterpret_cast<cache_key
*>(collapsed
.key
), index
);
230 anchorEntry(collapsed
, index
, *slot
);
231 inSync
= updateCollapsedWith(collapsed
, index
, *slot
);
232 return true; // even if inSync is false
236 MemStore::updateCollapsed(StoreEntry
&collapsed
)
238 assert(collapsed
.mem_obj
);
240 const sfileno index
= collapsed
.mem_obj
->memCache
.index
;
242 // already disconnected from the cache, no need to update
249 const Ipc::StoreMapAnchor
&anchor
= map
->readableEntry(index
);
250 return updateCollapsedWith(collapsed
, index
, anchor
);
253 /// updates collapsed entry after its anchor has been located
255 MemStore::updateCollapsedWith(StoreEntry
&collapsed
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
257 collapsed
.swap_file_sz
= anchor
.basics
.swap_file_sz
;
258 const bool copied
= copyFromShm(collapsed
, index
, anchor
);
262 /// anchors StoreEntry to an already locked map entry
264 MemStore::anchorEntry(StoreEntry
&e
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
266 const Ipc::StoreMapAnchor::Basics
&basics
= anchor
.basics
;
268 e
.swap_file_sz
= basics
.swap_file_sz
;
269 e
.lastref
= basics
.lastref
;
270 e
.timestamp
= basics
.timestamp
;
271 e
.expires
= basics
.expires
;
272 e
.lastmod
= basics
.lastmod
;
273 e
.refcount
= basics
.refcount
;
274 e
.flags
= basics
.flags
;
277 if (anchor
.complete()) {
278 e
.store_status
= STORE_OK
;
279 e
.mem_obj
->object_sz
= e
.swap_file_sz
;
280 e
.setMemStatus(IN_MEMORY
);
282 e
.store_status
= STORE_PENDING
;
283 assert(e
.mem_obj
->object_sz
< 0);
284 e
.setMemStatus(NOT_IN_MEMORY
);
286 assert(e
.swap_status
== SWAPOUT_NONE
); // set in StoreEntry constructor
287 e
.ping_status
= PING_NONE
;
289 EBIT_CLR(e
.flags
, RELEASE_REQUEST
);
290 EBIT_CLR(e
.flags
, KEY_PRIVATE
);
291 EBIT_SET(e
.flags
, ENTRY_VALIDATED
);
293 MemObject::MemCache
&mc
= e
.mem_obj
->memCache
;
295 mc
.io
= MemObject::ioReading
;
298 /// copies the entire entry from shared to local memory
300 MemStore::copyFromShm(StoreEntry
&e
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
302 debugs(20, 7, "mem-loading entry " << index
<< " from " << anchor
.start
);
305 // emulate the usual Store code but w/o inapplicable checks and callbacks:
307 Ipc::StoreMapSliceId sid
= anchor
.start
; // optimize: remember the last sid
308 bool wasEof
= anchor
.complete() && sid
< 0;
309 int64_t sliceOffset
= 0;
311 const Ipc::StoreMapSlice
&slice
= map
->readableSlice(index
, sid
);
312 // slice state may change during copying; take snapshots now
313 wasEof
= anchor
.complete() && slice
.next
< 0;
314 const Ipc::StoreMapSlice::Size wasSize
= slice
.size
;
316 debugs(20, 9, "entry " << index
<< " slice " << sid
<< " eof " <<
317 wasEof
<< " wasSize " << wasSize
<< " <= " <<
318 anchor
.basics
.swap_file_sz
<< " sliceOffset " << sliceOffset
<<
319 " mem.endOffset " << e
.mem_obj
->endOffset());
321 if (e
.mem_obj
->endOffset() < sliceOffset
+ wasSize
) {
322 // size of the slice data that we already copied
323 const size_t prefixSize
= e
.mem_obj
->endOffset() - sliceOffset
;
324 assert(prefixSize
<= wasSize
);
326 const MemStoreMap::Extras
&extras
= map
->extras(sid
);
327 char *page
= static_cast<char*>(PagePointer(extras
.page
));
328 const StoreIOBuffer
sliceBuf(wasSize
- prefixSize
,
329 e
.mem_obj
->endOffset(),
331 if (!copyFromShmSlice(e
, sliceBuf
, wasEof
))
333 debugs(20, 9, "entry " << index
<< " copied slice " << sid
<<
334 " from " << extras
.page
<< " +" << prefixSize
);
336 // else skip a [possibly incomplete] slice that we copied earlier
338 // careful: the slice may have grown _and_ gotten the next slice ID!
339 if (slice
.next
>= 0) {
341 // here we know that slice.size may not change any more
342 if (wasSize
>= slice
.size
) { // did not grow since we started copying
343 sliceOffset
+= wasSize
;
346 } else if (wasSize
>= slice
.size
) { // did not grow
352 debugs(20, 7, "mem-loaded " << e
.mem_obj
->endOffset() << '/' <<
353 anchor
.basics
.swap_file_sz
<< " bytes of " << e
);
357 debugs(20, 7, "mem-loaded all " << e
.mem_obj
->object_sz
<< '/' <<
358 anchor
.basics
.swap_file_sz
<< " bytes of " << e
);
360 // from StoreEntry::complete()
361 e
.mem_obj
->object_sz
= e
.mem_obj
->endOffset();
362 e
.store_status
= STORE_OK
;
363 e
.setMemStatus(IN_MEMORY
);
365 assert(e
.mem_obj
->object_sz
>= 0);
366 assert(static_cast<uint64_t>(e
.mem_obj
->object_sz
) == anchor
.basics
.swap_file_sz
);
367 // would be nice to call validLength() here, but it needs e.key
369 // we read the entire response into the local memory; no more need to lock
374 /// imports one shared memory slice into local memory
376 MemStore::copyFromShmSlice(StoreEntry
&e
, const StoreIOBuffer
&buf
, bool eof
)
378 debugs(20, 7, "buf: " << buf
.offset
<< " + " << buf
.length
);
380 // from store_client::readBody()
381 // parse headers if needed; they might span multiple slices!
382 HttpReply
*rep
= (HttpReply
*)e
.getReply();
383 if (rep
->pstate
< psParsed
) {
384 // XXX: have to copy because httpMsgParseStep() requires 0-termination
386 mb
.init(buf
.length
+1, buf
.length
+1);
387 mb
.append(buf
.data
, buf
.length
);
389 const int result
= rep
->httpMsgParseStep(mb
.buf
, buf
.length
, eof
);
391 assert(rep
->pstate
== psParsed
);
392 EBIT_CLR(e
.flags
, ENTRY_FWD_HDR_WAIT
);
393 } else if (result
< 0) {
394 debugs(20, DBG_IMPORTANT
, "Corrupted mem-cached headers: " << e
);
396 } else { // more slices are needed
400 debugs(20, 7, "rep pstate: " << rep
->pstate
);
402 // local memory stores both headers and body so copy regardless of pstate
403 const int64_t offBefore
= e
.mem_obj
->endOffset();
404 assert(e
.mem_obj
->data_hdr
.write(buf
)); // from MemObject::write()
405 const int64_t offAfter
= e
.mem_obj
->endOffset();
406 // expect to write the entire buf because StoreEntry::write() never fails
407 assert(offAfter
>= 0 && offBefore
<= offAfter
&&
408 static_cast<size_t>(offAfter
- offBefore
) == buf
.length
);
412 /// whether we should cache the entry
414 MemStore::shouldCache(const StoreEntry
&e
) const
416 if (e
.mem_status
== IN_MEMORY
) {
417 debugs(20, 5, "already loaded from mem-cache: " << e
);
421 if (e
.mem_obj
&& e
.mem_obj
->memCache
.offset
> 0) {
422 debugs(20, 5, "already written to mem-cache: " << e
);
426 if (!e
.memoryCachable()) {
427 debugs(20, 7, HERE
<< "Not memory cachable: " << e
);
428 return false; // will not cache due to entry state or properties
433 if (e
.mem_obj
->vary_headers
) {
434 // XXX: We must store/load SerialisedMetaData to cache Vary in RAM
435 debugs(20, 5, "Vary not yet supported: " << e
.mem_obj
->vary_headers
);
439 const int64_t expectedSize
= e
.mem_obj
->expectedReplySize(); // may be < 0
441 // objects of unknown size are not allowed into memory cache, for now
442 if (expectedSize
< 0) {
443 debugs(20, 5, "Unknown expected size: " << e
);
447 const int64_t loadedSize
= e
.mem_obj
->endOffset();
448 const int64_t ramSize
= max(loadedSize
, expectedSize
);
450 if (ramSize
> maxObjectSize()) {
451 debugs(20, 5, HERE
<< "Too big max(" <<
452 loadedSize
<< ", " << expectedSize
<< "): " << e
);
453 return false; // will not cache due to cachable entry size limits
457 debugs(20, 5, HERE
<< "No map to mem-cache " << e
);
461 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) {
462 debugs(20, 5, "Not mem-caching ENTRY_SPECIAL " << e
);
469 /// locks map anchor and preps to store the entry in shared memory
471 MemStore::startCaching(StoreEntry
&e
)
474 Ipc::StoreMapAnchor
*slot
= map
->openForWriting(reinterpret_cast<const cache_key
*>(e
.key
), index
);
476 debugs(20, 5, HERE
<< "No room in mem-cache map to index " << e
);
481 e
.mem_obj
->memCache
.index
= index
;
482 e
.mem_obj
->memCache
.io
= MemObject::ioWriting
;
484 map
->startAppending(index
);
488 /// copies all local data to shared memory
490 MemStore::copyToShm(StoreEntry
&e
)
492 // prevents remote readers from getting ENTRY_FWD_HDR_WAIT entries and
493 // not knowing when the wait is over
494 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) {
495 debugs(20, 5, "postponing copying " << e
<< " for ENTRY_FWD_HDR_WAIT");
502 const int32_t index
= e
.mem_obj
->memCache
.index
;
504 Ipc::StoreMapAnchor
&anchor
= map
->writeableEntry(index
);
506 const int64_t eSize
= e
.mem_obj
->endOffset();
507 if (e
.mem_obj
->memCache
.offset
>= eSize
) {
508 debugs(20, 5, "postponing copying " << e
<< " for lack of news: " <<
509 e
.mem_obj
->memCache
.offset
<< " >= " << eSize
);
510 return; // nothing to do (yet)
513 if (anchor
.start
< 0) { // must allocate the very first slot for e
514 Ipc::Mem::PageId page
;
515 anchor
.start
= reserveSapForWriting(page
); // throws
516 map
->extras(anchor
.start
).page
= page
;
519 lastWritingSlice
= anchor
.start
;
520 const size_t sliceCapacity
= Ipc::Mem::PageSize();
522 // fill, skip slices that are already full
523 // Optimize: remember lastWritingSlice in e.mem_obj
524 while (e
.mem_obj
->memCache
.offset
< eSize
) {
525 Ipc::StoreMap::Slice
&slice
=
526 map
->writeableSlice(e
.mem_obj
->memCache
.index
, lastWritingSlice
);
528 if (slice
.size
>= sliceCapacity
) {
529 if (slice
.next
>= 0) {
530 lastWritingSlice
= slice
.next
;
534 Ipc::Mem::PageId page
;
535 slice
.next
= lastWritingSlice
= reserveSapForWriting(page
);
536 map
->extras(lastWritingSlice
).page
= page
;
537 debugs(20, 7, "entry " << index
<< " new slice: " << lastWritingSlice
);
540 copyToShmSlice(e
, anchor
);
543 debugs(20, 7, "mem-cached available " << eSize
<< " bytes of " << e
);
546 /// copies at most one slice worth of local memory to shared memory
548 MemStore::copyToShmSlice(StoreEntry
&e
, Ipc::StoreMapAnchor
&anchor
)
550 Ipc::StoreMap::Slice
&slice
=
551 map
->writeableSlice(e
.mem_obj
->memCache
.index
, lastWritingSlice
);
553 Ipc::Mem::PageId page
= map
->extras(lastWritingSlice
).page
;
554 assert(lastWritingSlice
>= 0 && page
);
555 debugs(20, 7, "entry " << e
<< " slice " << lastWritingSlice
<< " has " <<
558 const int64_t bufSize
= Ipc::Mem::PageSize();
559 const int64_t sliceOffset
= e
.mem_obj
->memCache
.offset
% bufSize
;
560 StoreIOBuffer
sharedSpace(bufSize
- sliceOffset
, e
.mem_obj
->memCache
.offset
,
561 static_cast<char*>(PagePointer(page
)) + sliceOffset
);
563 // check that we kept everything or purge incomplete/sparse cached entry
564 const ssize_t copied
= e
.mem_obj
->data_hdr
.copy(sharedSpace
);
566 debugs(20, 2, "Failed to mem-cache " << (bufSize
- sliceOffset
) <<
567 " bytes of " << e
<< " from " << e
.mem_obj
->memCache
.offset
<<
569 throw TexcHere("data_hdr.copy failure");
572 debugs(20, 7, "mem-cached " << copied
<< " bytes of " << e
<<
573 " from " << e
.mem_obj
->memCache
.offset
<< " in " << page
);
575 slice
.size
+= copied
;
576 e
.mem_obj
->memCache
.offset
+= copied
;
577 anchor
.basics
.swap_file_sz
= e
.mem_obj
->memCache
.offset
;
580 /// finds a slot and a free page to fill or throws
582 MemStore::reserveSapForWriting(Ipc::Mem::PageId
&page
)
584 Ipc::Mem::PageId slot
;
585 if (freeSlots
->pop(slot
)) {
586 debugs(20, 5, "got a previously free slot: " << slot
);
588 if (Ipc::Mem::GetPage(Ipc::Mem::PageId::cachePage
, page
)) {
589 debugs(20, 5, "and got a previously free page: " << page
);
590 return slot
.number
- 1;
592 debugs(20, 3, "but there is no free page, returning " << slot
);
593 freeSlots
->push(slot
);
597 // catch free slots delivered to noteFreeMapSlice()
599 waitingFor
.slot
= &slot
;
600 waitingFor
.page
= &page
;
601 if (map
->purgeOne()) {
602 assert(!waitingFor
); // noteFreeMapSlice() should have cleared it
605 debugs(20, 5, "got previously busy " << slot
<< " and " << page
);
606 return slot
.number
- 1;
608 assert(waitingFor
.slot
== &slot
&& waitingFor
.page
== &page
);
609 waitingFor
.slot
= NULL
;
610 waitingFor
.page
= NULL
;
612 debugs(47, 3, "cannot get a slice; entries: " << map
->entryCount());
613 throw TexcHere("ran out of mem-cache slots");
617 MemStore::noteFreeMapSlice(const sfileno sliceId
)
619 Ipc::Mem::PageId
&pageId
= map
->extras(sliceId
).page
;
620 debugs(20, 9, "slice " << sliceId
<< " freed " << pageId
);
622 Ipc::Mem::PageId slotId
;
623 slotId
.pool
= SpacePoolId
;
624 slotId
.number
= sliceId
+ 1;
626 // must zero pageId before we give slice (and pageId extras!) to others
627 Ipc::Mem::PutPage(pageId
);
628 freeSlots
->push(slotId
);
630 *waitingFor
.slot
= slotId
;
631 *waitingFor
.page
= pageId
;
632 waitingFor
.slot
= NULL
;
633 waitingFor
.page
= NULL
;
634 pageId
= Ipc::Mem::PageId();
639 MemStore::write(StoreEntry
&e
)
643 debugs(20, 7, "entry " << e
);
645 switch (e
.mem_obj
->memCache
.io
) {
646 case MemObject::ioUndecided
:
647 if (!shouldCache(e
) || !startCaching(e
)) {
648 e
.mem_obj
->memCache
.io
= MemObject::ioDone
;
649 Store::Root().transientsAbandon(e
);
654 case MemObject::ioDone
:
655 case MemObject::ioReading
:
656 return; // we should not write in all of the above cases
658 case MemObject::ioWriting
:
659 break; // already decided to write and still writing
664 if (e
.store_status
== STORE_OK
) // done receiving new content
667 CollapsedForwarding::Broadcast(e
);
669 } catch (const std::exception
&x
) { // TODO: should we catch ... as well?
670 debugs(20, 2, "mem-caching error writing entry " << e
<< ": " << x
.what());
671 // fall through to the error handling code
678 MemStore::completeWriting(StoreEntry
&e
)
681 const int32_t index
= e
.mem_obj
->memCache
.index
;
685 debugs(20, 5, "mem-cached all " << e
.mem_obj
->memCache
.offset
<< " bytes of " << e
);
687 e
.mem_obj
->memCache
.index
= -1;
688 e
.mem_obj
->memCache
.io
= MemObject::ioDone
;
689 map
->closeForWriting(index
, false);
691 CollapsedForwarding::Broadcast(e
); // before we close our transient entry!
692 Store::Root().transientsCompleteWriting(e
);
696 MemStore::markForUnlink(StoreEntry
&e
)
699 if (e
.mem_obj
->memCache
.index
>= 0)
700 map
->freeEntry(e
.mem_obj
->memCache
.index
);
704 MemStore::unlink(StoreEntry
&e
)
706 if (e
.mem_obj
&& e
.mem_obj
->memCache
.index
>= 0) {
707 map
->freeEntry(e
.mem_obj
->memCache
.index
);
710 // the entry may have been loaded and then disconnected from the cache
711 map
->freeEntryByKey(reinterpret_cast<cache_key
*>(e
.key
));
714 e
.destroyMemObject(); // XXX: but it may contain useful info such as a client list. The old code used to do that though, right?
718 MemStore::disconnect(StoreEntry
&e
)
721 MemObject
&mem_obj
= *e
.mem_obj
;
722 if (mem_obj
.memCache
.index
>= 0) {
723 if (mem_obj
.memCache
.io
== MemObject::ioWriting
) {
724 map
->abortWriting(mem_obj
.memCache
.index
);
725 mem_obj
.memCache
.index
= -1;
726 mem_obj
.memCache
.io
= MemObject::ioDone
;
727 Store::Root().transientsAbandon(e
); // broadcasts after the change
729 assert(mem_obj
.memCache
.io
== MemObject::ioReading
);
730 map
->closeForReading(mem_obj
.memCache
.index
);
731 mem_obj
.memCache
.index
= -1;
732 mem_obj
.memCache
.io
= MemObject::ioDone
;
737 /// calculates maximum number of entries we need to store and map
739 MemStore::EntryLimit()
741 if (!Config
.memShared
|| !Config
.memMaxSize
)
742 return 0; // no memory cache configured
744 const int64_t minEntrySize
= Ipc::Mem::PageSize();
745 const int64_t entryLimit
= Config
.memMaxSize
/ minEntrySize
;
749 /// reports our needs for shared memory pages to Ipc::Mem::Pages;
750 /// decides whether to use a shared memory cache or checks its configuration;
751 /// and initializes shared memory segments used by MemStore
752 class MemStoreRr
: public Ipc::Mem::RegisteredRunner
755 /* RegisteredRunner API */
756 MemStoreRr(): spaceOwner(NULL
), mapOwner(NULL
) {}
757 virtual void finalizeConfig();
758 virtual void claimMemoryNeeds();
759 virtual void useConfig();
760 virtual ~MemStoreRr();
763 /* Ipc::Mem::RegisteredRunner API */
764 virtual void create();
767 Ipc::Mem::Owner
<Ipc::Mem::PageStack
> *spaceOwner
; ///< free slices Owner
768 MemStoreMap::Owner
*mapOwner
; ///< primary map Owner
771 RunnerRegistrationEntry(MemStoreRr
);
774 MemStoreRr::claimMemoryNeeds()
776 Ipc::Mem::NotePageNeed(Ipc::Mem::PageId::cachePage
, MemStore::EntryLimit());
780 MemStoreRr::finalizeConfig()
782 // decide whether to use a shared memory cache if the user did not specify
783 if (!Config
.memShared
.configured()) {
784 Config
.memShared
.configure(Ipc::Atomic::Enabled() &&
785 Ipc::Mem::Segment::Enabled() && UsingSmp() &&
786 Config
.memMaxSize
> 0);
787 } else if (Config
.memShared
&& !Ipc::Atomic::Enabled()) {
788 // bail if the user wants shared memory cache but we cannot support it
789 fatal("memory_cache_shared is on, but no support for atomic operations detected");
790 } else if (Config
.memShared
&& !Ipc::Mem::Segment::Enabled()) {
791 fatal("memory_cache_shared is on, but no support for shared memory detected");
792 } else if (Config
.memShared
&& !UsingSmp()) {
793 debugs(20, DBG_IMPORTANT
, "WARNING: memory_cache_shared is on, but only"
794 " a single worker is running");
799 MemStoreRr::useConfig()
801 assert(Config
.memShared
.configured());
802 Ipc::Mem::RegisteredRunner::useConfig();
808 if (!Config
.memShared
)
811 const int64_t entryLimit
= MemStore::EntryLimit();
812 if (entryLimit
<= 0) {
813 if (Config
.memMaxSize
> 0) {
814 debugs(20, DBG_IMPORTANT
, "WARNING: mem-cache size is too small ("
815 << (Config
.memMaxSize
/ 1024.0) << " KB), should be >= " <<
816 (Ipc::Mem::PageSize() / 1024.0) << " KB");
818 return; // no memory cache configured or a misconfiguration
822 spaceOwner
= shm_new(Ipc::Mem::PageStack
)(SpaceLabel
, SpacePoolId
,
824 sizeof(Ipc::Mem::PageId
));
826 mapOwner
= MemStoreMap::Init(MapLabel
, entryLimit
);
829 MemStoreRr::~MemStoreRr()