]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemStore.cc
2 * Copyright (C) 1996-2016 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 Memory Cache */
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"
19 #include "mime_header.h"
20 #include "SquidConfig.h"
21 #include "SquidMath.h"
22 #include "StoreStats.h"
25 /// shared memory segment path to use for MemStore maps
26 static const SBuf
MapLabel("cache_mem_map");
27 /// shared memory segment path to use for the free slices index
28 static const char *SpaceLabel
= "cache_mem_space";
29 /// shared memory segment path to use for IDs of shared pages with slice data
30 static const char *ExtrasLabel
= "cache_mem_ex";
31 // TODO: sync with Rock::SwapDir::*Path()
33 // We store free slot IDs (i.e., "space") as Page objects so that we can use
34 // Ipc::Mem::PageStack. Pages require pool IDs. The value here is not really
35 // used except for a positivity test. A unique value is handy for debugging.
36 static const uint32_t SpacePoolId
= 510716;
38 MemStore::MemStore(): map(NULL
), lastWritingSlice(-1)
50 const int64_t entryLimit
= EntryLimit();
52 return; // no memory cache configured or a misconfiguration
54 // check compatibility with the disk cache, if any
55 if (Config
.cacheSwap
.n_configured
> 0) {
56 const int64_t diskMaxSize
= Store::Root().maxObjectSize();
57 const int64_t memMaxSize
= maxObjectSize();
58 if (diskMaxSize
== -1) {
59 debugs(20, DBG_IMPORTANT
, "WARNING: disk-cache maximum object size "
60 "is unlimited but mem-cache maximum object size is " <<
61 memMaxSize
/ 1024.0 << " KB");
62 } else if (diskMaxSize
> memMaxSize
) {
63 debugs(20, DBG_IMPORTANT
, "WARNING: disk-cache maximum object size "
64 "is too large for mem-cache: " <<
65 diskMaxSize
/ 1024.0 << " KB > " <<
66 memMaxSize
/ 1024.0 << " KB");
70 freeSlots
= shm_old(Ipc::Mem::PageStack
)(SpaceLabel
);
71 extras
= shm_old(Extras
)(ExtrasLabel
);
74 map
= new MemStoreMap(MapLabel
);
79 MemStore::getStats(StoreInfoStats
&stats
) const
81 const size_t pageSize
= Ipc::Mem::PageSize();
83 stats
.mem
.shared
= true;
85 Ipc::Mem::PageLimit(Ipc::Mem::PageId::cachePage
) * pageSize
;
87 Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) * pageSize
;
88 stats
.mem
.count
= currentCount();
92 MemStore::stat(StoreEntry
&e
) const
94 storeAppendPrintf(&e
, "\n\nShared Memory Cache\n");
96 storeAppendPrintf(&e
, "Maximum Size: %.0f KB\n", maxSize()/1024.0);
97 storeAppendPrintf(&e
, "Current Size: %.2f KB %.2f%%\n",
98 currentSize() / 1024.0,
99 Math::doublePercent(currentSize(), maxSize()));
102 const int entryLimit
= map
->entryLimit();
103 const int slotLimit
= map
->sliceLimit();
104 storeAppendPrintf(&e
, "Maximum entries: %9d\n", entryLimit
);
105 if (entryLimit
> 0) {
106 storeAppendPrintf(&e
, "Current entries: %" PRId64
" %.2f%%\n",
107 currentCount(), (100.0 * currentCount() / entryLimit
));
110 storeAppendPrintf(&e
, "Maximum slots: %9d\n", slotLimit
);
112 const unsigned int slotsFree
=
113 Ipc::Mem::PagesAvailable(Ipc::Mem::PageId::cachePage
);
114 if (slotsFree
<= static_cast<const unsigned int>(slotLimit
)) {
115 const int usedSlots
= slotLimit
- static_cast<const int>(slotsFree
);
116 storeAppendPrintf(&e
, "Used slots: %9d %.2f%%\n",
117 usedSlots
, (100.0 * usedSlots
/ slotLimit
));
120 if (slotLimit
< 100) { // XXX: otherwise too expensive to count
121 Ipc::ReadWriteLockStats stats
;
122 map
->updateStats(stats
);
135 MemStore::minSize() const
137 return 0; // XXX: irrelevant, but Store parent forces us to implement this
141 MemStore::maxSize() const
143 return Config
.memMaxSize
;
147 MemStore::currentSize() const
149 return Ipc::Mem::PageLevel(Ipc::Mem::PageId::cachePage
) *
150 Ipc::Mem::PageSize();
154 MemStore::currentCount() const
156 return map
? map
->entryCount() : 0;
160 MemStore::maxObjectSize() const
162 return min(Config
.Store
.maxInMemObjSize
, Config
.memMaxSize
);
166 MemStore::reference(StoreEntry
&)
171 MemStore::dereference(StoreEntry
&)
173 // no need to keep e in the global store_table for us; we have our own map
178 MemStore::get(const cache_key
*key
)
184 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(key
, index
);
188 // create a brand new store entry and initialize it with stored info
189 StoreEntry
*e
= new StoreEntry();
191 // XXX: We do not know the URLs yet, only the key, but we need to parse and
192 // store the response for the Root().get() callers to be happy because they
193 // expect IN_MEMORY entries to already have the response headers and body.
196 anchorEntry(*e
, index
, *slot
);
198 const bool copied
= copyFromShm(*e
, index
, *slot
);
205 debugs(20, 3, HERE
<< "mem-loading failed; freeing " << index
);
206 map
->freeEntry(index
); // do not let others into the same trap
211 MemStore::anchorCollapsed(StoreEntry
&collapsed
, bool &inSync
)
217 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(
218 reinterpret_cast<cache_key
*>(collapsed
.key
), index
);
222 anchorEntry(collapsed
, index
, *slot
);
223 inSync
= updateCollapsedWith(collapsed
, index
, *slot
);
224 return true; // even if inSync is false
228 MemStore::updateCollapsed(StoreEntry
&collapsed
)
230 assert(collapsed
.mem_obj
);
232 const sfileno index
= collapsed
.mem_obj
->memCache
.index
;
234 // already disconnected from the cache, no need to update
241 const Ipc::StoreMapAnchor
&anchor
= map
->readableEntry(index
);
242 return updateCollapsedWith(collapsed
, index
, anchor
);
245 /// updates collapsed entry after its anchor has been located
247 MemStore::updateCollapsedWith(StoreEntry
&collapsed
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
249 collapsed
.swap_file_sz
= anchor
.basics
.swap_file_sz
;
250 const bool copied
= copyFromShm(collapsed
, index
, anchor
);
254 /// anchors StoreEntry to an already locked map entry
256 MemStore::anchorEntry(StoreEntry
&e
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
258 const Ipc::StoreMapAnchor::Basics
&basics
= anchor
.basics
;
260 e
.swap_file_sz
= basics
.swap_file_sz
;
261 e
.lastref
= basics
.lastref
;
262 e
.timestamp
= basics
.timestamp
;
263 e
.expires
= basics
.expires
;
264 e
.lastmod
= basics
.lastmod
;
265 e
.refcount
= basics
.refcount
;
266 e
.flags
= basics
.flags
;
269 if (anchor
.complete()) {
270 e
.store_status
= STORE_OK
;
271 e
.mem_obj
->object_sz
= e
.swap_file_sz
;
272 e
.setMemStatus(IN_MEMORY
);
274 e
.store_status
= STORE_PENDING
;
275 assert(e
.mem_obj
->object_sz
< 0);
276 e
.setMemStatus(NOT_IN_MEMORY
);
278 assert(e
.swap_status
== SWAPOUT_NONE
); // set in StoreEntry constructor
279 e
.ping_status
= PING_NONE
;
281 EBIT_CLR(e
.flags
, RELEASE_REQUEST
);
282 EBIT_CLR(e
.flags
, KEY_PRIVATE
);
283 EBIT_SET(e
.flags
, ENTRY_VALIDATED
);
285 MemObject::MemCache
&mc
= e
.mem_obj
->memCache
;
287 mc
.io
= MemObject::ioReading
;
290 /// copies the entire entry from shared to local memory
292 MemStore::copyFromShm(StoreEntry
&e
, const sfileno index
, const Ipc::StoreMapAnchor
&anchor
)
294 debugs(20, 7, "mem-loading entry " << index
<< " from " << anchor
.start
);
297 // emulate the usual Store code but w/o inapplicable checks and callbacks:
299 Ipc::StoreMapSliceId sid
= anchor
.start
; // optimize: remember the last sid
300 bool wasEof
= anchor
.complete() && sid
< 0;
301 int64_t sliceOffset
= 0;
303 const Ipc::StoreMapSlice
&slice
= map
->readableSlice(index
, sid
);
304 // slice state may change during copying; take snapshots now
305 wasEof
= anchor
.complete() && slice
.next
< 0;
306 const Ipc::StoreMapSlice::Size wasSize
= slice
.size
;
308 debugs(20, 9, "entry " << index
<< " slice " << sid
<< " eof " <<
309 wasEof
<< " wasSize " << wasSize
<< " <= " <<
310 anchor
.basics
.swap_file_sz
<< " sliceOffset " << sliceOffset
<<
311 " mem.endOffset " << e
.mem_obj
->endOffset());
313 if (e
.mem_obj
->endOffset() < sliceOffset
+ wasSize
) {
314 // size of the slice data that we already copied
315 const size_t prefixSize
= e
.mem_obj
->endOffset() - sliceOffset
;
316 assert(prefixSize
<= wasSize
);
318 const MemStoreMapExtras::Item
&extra
= extras
->items
[sid
];
320 char *page
= static_cast<char*>(PagePointer(extra
.page
));
321 const StoreIOBuffer
sliceBuf(wasSize
- prefixSize
,
322 e
.mem_obj
->endOffset(),
324 if (!copyFromShmSlice(e
, sliceBuf
, wasEof
))
326 debugs(20, 9, "entry " << index
<< " copied slice " << sid
<<
327 " from " << extra
.page
<< '+' << prefixSize
);
329 // else skip a [possibly incomplete] slice that we copied earlier
331 // careful: the slice may have grown _and_ gotten the next slice ID!
332 if (slice
.next
>= 0) {
334 // here we know that slice.size may not change any more
335 if (wasSize
>= slice
.size
) { // did not grow since we started copying
336 sliceOffset
+= wasSize
;
339 } else if (wasSize
>= slice
.size
) { // did not grow
345 debugs(20, 7, "mem-loaded " << e
.mem_obj
->endOffset() << '/' <<
346 anchor
.basics
.swap_file_sz
<< " bytes of " << e
);
350 debugs(20, 7, "mem-loaded all " << e
.mem_obj
->object_sz
<< '/' <<
351 anchor
.basics
.swap_file_sz
<< " bytes of " << e
);
353 // from StoreEntry::complete()
354 e
.mem_obj
->object_sz
= e
.mem_obj
->endOffset();
355 e
.store_status
= STORE_OK
;
356 e
.setMemStatus(IN_MEMORY
);
358 assert(e
.mem_obj
->object_sz
>= 0);
359 assert(static_cast<uint64_t>(e
.mem_obj
->object_sz
) == anchor
.basics
.swap_file_sz
);
360 // would be nice to call validLength() here, but it needs e.key
362 // we read the entire response into the local memory; no more need to lock
367 /// imports one shared memory slice into local memory
369 MemStore::copyFromShmSlice(StoreEntry
&e
, const StoreIOBuffer
&buf
, bool eof
)
371 debugs(20, 7, "buf: " << buf
.offset
<< " + " << buf
.length
);
373 // from store_client::readBody()
374 // parse headers if needed; they might span multiple slices!
375 HttpReply
*rep
= (HttpReply
*)e
.getReply();
376 if (rep
->pstate
< psParsed
) {
377 // XXX: have to copy because httpMsgParseStep() requires 0-termination
379 mb
.init(buf
.length
+1, buf
.length
+1);
380 mb
.append(buf
.data
, buf
.length
);
382 const int result
= rep
->httpMsgParseStep(mb
.buf
, buf
.length
, eof
);
384 assert(rep
->pstate
== psParsed
);
385 EBIT_CLR(e
.flags
, ENTRY_FWD_HDR_WAIT
);
386 } else if (result
< 0) {
387 debugs(20, DBG_IMPORTANT
, "Corrupted mem-cached headers: " << e
);
389 } else { // more slices are needed
393 debugs(20, 7, "rep pstate: " << rep
->pstate
);
395 // local memory stores both headers and body so copy regardless of pstate
396 const int64_t offBefore
= e
.mem_obj
->endOffset();
397 assert(e
.mem_obj
->data_hdr
.write(buf
)); // from MemObject::write()
398 const int64_t offAfter
= e
.mem_obj
->endOffset();
399 // expect to write the entire buf because StoreEntry::write() never fails
400 assert(offAfter
>= 0 && offBefore
<= offAfter
&&
401 static_cast<size_t>(offAfter
- offBefore
) == buf
.length
);
405 /// whether we should cache the entry
407 MemStore::shouldCache(StoreEntry
&e
) const
409 if (e
.mem_status
== IN_MEMORY
) {
410 debugs(20, 5, "already loaded from mem-cache: " << e
);
414 if (e
.mem_obj
&& e
.mem_obj
->memCache
.offset
> 0) {
415 debugs(20, 5, "already written to mem-cache: " << e
);
419 if (!e
.memoryCachable()) {
420 debugs(20, 7, HERE
<< "Not memory cachable: " << e
);
421 return false; // will not cache due to entry state or properties
426 if (e
.mem_obj
->vary_headers
) {
427 // XXX: We must store/load SerialisedMetaData to cache Vary in RAM
428 debugs(20, 5, "Vary not yet supported: " << e
.mem_obj
->vary_headers
);
432 const int64_t expectedSize
= e
.mem_obj
->expectedReplySize(); // may be < 0
434 // objects of unknown size are not allowed into memory cache, for now
435 if (expectedSize
< 0) {
436 debugs(20, 5, "Unknown expected size: " << e
);
440 const int64_t loadedSize
= e
.mem_obj
->endOffset();
441 const int64_t ramSize
= max(loadedSize
, expectedSize
);
443 if (ramSize
> maxObjectSize()) {
444 debugs(20, 5, HERE
<< "Too big max(" <<
445 loadedSize
<< ", " << expectedSize
<< "): " << e
);
446 return false; // will not cache due to cachable entry size limits
449 if (!e
.mem_obj
->isContiguous()) {
450 debugs(20, 5, "not contiguous");
455 debugs(20, 5, HERE
<< "No map to mem-cache " << e
);
459 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) {
460 debugs(20, 5, "Not mem-caching ENTRY_SPECIAL " << e
);
467 /// locks map anchor and preps to store the entry in shared memory
469 MemStore::startCaching(StoreEntry
&e
)
472 Ipc::StoreMapAnchor
*slot
= map
->openForWriting(reinterpret_cast<const cache_key
*>(e
.key
), index
);
474 debugs(20, 5, HERE
<< "No room in mem-cache map to index " << e
);
479 e
.mem_obj
->memCache
.index
= index
;
480 e
.mem_obj
->memCache
.io
= MemObject::ioWriting
;
482 map
->startAppending(index
);
483 e
.memOutDecision(true);
487 /// copies all local data to shared memory
489 MemStore::copyToShm(StoreEntry
&e
)
491 // prevents remote readers from getting ENTRY_FWD_HDR_WAIT entries and
492 // not knowing when the wait is over
493 if (EBIT_TEST(e
.flags
, ENTRY_FWD_HDR_WAIT
)) {
494 debugs(20, 5, "postponing copying " << e
<< " for ENTRY_FWD_HDR_WAIT");
501 const int32_t index
= e
.mem_obj
->memCache
.index
;
503 Ipc::StoreMapAnchor
&anchor
= map
->writeableEntry(index
);
505 const int64_t eSize
= e
.mem_obj
->endOffset();
506 if (e
.mem_obj
->memCache
.offset
>= eSize
) {
507 debugs(20, 5, "postponing copying " << e
<< " for lack of news: " <<
508 e
.mem_obj
->memCache
.offset
<< " >= " << eSize
);
509 return; // nothing to do (yet)
512 if (anchor
.start
< 0) { // must allocate the very first slot for e
513 Ipc::Mem::PageId page
;
514 anchor
.start
= reserveSapForWriting(page
); // throws
515 extras
->items
[anchor
.start
].page
= page
;
518 lastWritingSlice
= anchor
.start
;
519 const size_t sliceCapacity
= Ipc::Mem::PageSize();
521 // fill, skip slices that are already full
522 // Optimize: remember lastWritingSlice in e.mem_obj
523 while (e
.mem_obj
->memCache
.offset
< eSize
) {
524 Ipc::StoreMap::Slice
&slice
=
525 map
->writeableSlice(e
.mem_obj
->memCache
.index
, lastWritingSlice
);
527 if (slice
.size
>= sliceCapacity
) {
528 if (slice
.next
>= 0) {
529 lastWritingSlice
= slice
.next
;
533 Ipc::Mem::PageId page
;
534 slice
.next
= lastWritingSlice
= reserveSapForWriting(page
);
535 extras
->items
[lastWritingSlice
].page
= page
;
536 debugs(20, 7, "entry " << index
<< " new slice: " << lastWritingSlice
);
539 copyToShmSlice(e
, anchor
);
542 debugs(20, 7, "mem-cached available " << eSize
<< " bytes of " << e
);
545 /// copies at most one slice worth of local memory to shared memory
547 MemStore::copyToShmSlice(StoreEntry
&e
, Ipc::StoreMapAnchor
&anchor
)
549 Ipc::StoreMap::Slice
&slice
=
550 map
->writeableSlice(e
.mem_obj
->memCache
.index
, lastWritingSlice
);
552 Ipc::Mem::PageId page
= extras
->items
[lastWritingSlice
].page
;
553 assert(lastWritingSlice
>= 0 && page
);
554 debugs(20, 7, "entry " << e
<< " slice " << lastWritingSlice
<< " has " <<
557 const int64_t bufSize
= Ipc::Mem::PageSize();
558 const int64_t sliceOffset
= e
.mem_obj
->memCache
.offset
% bufSize
;
559 StoreIOBuffer
sharedSpace(bufSize
- sliceOffset
, e
.mem_obj
->memCache
.offset
,
560 static_cast<char*>(PagePointer(page
)) + sliceOffset
);
562 // check that we kept everything or purge incomplete/sparse cached entry
563 const ssize_t copied
= e
.mem_obj
->data_hdr
.copy(sharedSpace
);
565 debugs(20, 2, "Failed to mem-cache " << (bufSize
- sliceOffset
) <<
566 " bytes of " << e
<< " from " << e
.mem_obj
->memCache
.offset
<<
568 throw TexcHere("data_hdr.copy failure");
571 debugs(20, 7, "mem-cached " << copied
<< " bytes of " << e
<<
572 " from " << e
.mem_obj
->memCache
.offset
<< " in " << page
);
574 slice
.size
+= copied
;
575 e
.mem_obj
->memCache
.offset
+= copied
;
576 anchor
.basics
.swap_file_sz
= e
.mem_obj
->memCache
.offset
;
579 /// finds a slot and a free page to fill or throws
581 MemStore::reserveSapForWriting(Ipc::Mem::PageId
&page
)
583 Ipc::Mem::PageId slot
;
584 if (freeSlots
->pop(slot
)) {
585 debugs(20, 5, "got a previously free slot: " << slot
);
587 if (Ipc::Mem::GetPage(Ipc::Mem::PageId::cachePage
, page
)) {
588 debugs(20, 5, "and got a previously free page: " << page
);
589 return slot
.number
- 1;
591 debugs(20, 3, "but there is no free page, returning " << slot
);
592 freeSlots
->push(slot
);
596 // catch free slots delivered to noteFreeMapSlice()
598 waitingFor
.slot
= &slot
;
599 waitingFor
.page
= &page
;
600 if (map
->purgeOne()) {
601 assert(!waitingFor
); // noteFreeMapSlice() should have cleared it
604 debugs(20, 5, "got previously busy " << slot
<< " and " << page
);
605 return slot
.number
- 1;
607 assert(waitingFor
.slot
== &slot
&& waitingFor
.page
== &page
);
608 waitingFor
.slot
= NULL
;
609 waitingFor
.page
= NULL
;
611 debugs(47, 3, "cannot get a slice; entries: " << map
->entryCount());
612 throw TexcHere("ran out of mem-cache slots");
616 MemStore::noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId
)
618 Ipc::Mem::PageId
&pageId
= extras
->items
[sliceId
].page
;
619 debugs(20, 9, "slice " << sliceId
<< " freed " << pageId
);
621 Ipc::Mem::PageId slotId
;
622 slotId
.pool
= SpacePoolId
;
623 slotId
.number
= sliceId
+ 1;
625 // must zero pageId before we give slice (and pageId extras!) to others
626 Ipc::Mem::PutPage(pageId
);
627 freeSlots
->push(slotId
);
629 *waitingFor
.slot
= slotId
;
630 *waitingFor
.page
= pageId
;
631 waitingFor
.slot
= NULL
;
632 waitingFor
.page
= NULL
;
633 pageId
= Ipc::Mem::PageId();
638 MemStore::write(StoreEntry
&e
)
642 debugs(20, 7, "entry " << e
);
644 switch (e
.mem_obj
->memCache
.io
) {
645 case MemObject::ioUndecided
:
646 if (!shouldCache(e
) || !startCaching(e
)) {
647 e
.mem_obj
->memCache
.io
= MemObject::ioDone
;
648 e
.memOutDecision(false);
653 case MemObject::ioDone
:
654 case MemObject::ioReading
:
655 return; // we should not write in all of the above cases
657 case MemObject::ioWriting
:
658 break; // already decided to write and still writing
663 if (e
.store_status
== STORE_OK
) // done receiving new content
666 CollapsedForwarding::Broadcast(e
);
668 } catch (const std::exception
&x
) { // TODO: should we catch ... as well?
669 debugs(20, 2, "mem-caching error writing entry " << e
<< ": " << x
.what());
670 // fall through to the error handling code
677 MemStore::completeWriting(StoreEntry
&e
)
680 const int32_t index
= e
.mem_obj
->memCache
.index
;
684 debugs(20, 5, "mem-cached all " << e
.mem_obj
->memCache
.offset
<< " bytes of " << e
);
686 e
.mem_obj
->memCache
.index
= -1;
687 e
.mem_obj
->memCache
.io
= MemObject::ioDone
;
688 map
->closeForWriting(index
, false);
690 CollapsedForwarding::Broadcast(e
); // before we close our transient entry!
691 Store::Root().transientsCompleteWriting(e
);
695 MemStore::markForUnlink(StoreEntry
&e
)
698 if (e
.mem_obj
->memCache
.index
>= 0)
699 map
->freeEntry(e
.mem_obj
->memCache
.index
);
703 MemStore::unlink(StoreEntry
&e
)
705 if (e
.mem_obj
&& e
.mem_obj
->memCache
.index
>= 0) {
706 map
->freeEntry(e
.mem_obj
->memCache
.index
);
709 // the entry may have been loaded and then disconnected from the cache
710 map
->freeEntryByKey(reinterpret_cast<cache_key
*>(e
.key
));
713 e
.destroyMemObject(); // XXX: but it may contain useful info such as a client list. The old code used to do that though, right?
717 MemStore::disconnect(StoreEntry
&e
)
720 MemObject
&mem_obj
= *e
.mem_obj
;
721 if (mem_obj
.memCache
.index
>= 0) {
722 if (mem_obj
.memCache
.io
== MemObject::ioWriting
) {
723 map
->abortWriting(mem_obj
.memCache
.index
);
724 mem_obj
.memCache
.index
= -1;
725 mem_obj
.memCache
.io
= MemObject::ioDone
;
726 Store::Root().transientsAbandon(e
); // broadcasts after the change
728 assert(mem_obj
.memCache
.io
== MemObject::ioReading
);
729 map
->closeForReading(mem_obj
.memCache
.index
);
730 mem_obj
.memCache
.index
= -1;
731 mem_obj
.memCache
.io
= MemObject::ioDone
;
736 /// calculates maximum number of entries we need to store and map
738 MemStore::EntryLimit()
740 if (!Config
.memShared
|| !Config
.memMaxSize
)
741 return 0; // no memory cache configured
743 const int64_t minEntrySize
= Ipc::Mem::PageSize();
744 const int64_t entryLimit
= Config
.memMaxSize
/ minEntrySize
;
748 /// reports our needs for shared memory pages to Ipc::Mem::Pages;
749 /// decides whether to use a shared memory cache or checks its configuration;
750 /// and initializes shared memory segments used by MemStore
751 class MemStoreRr
: public Ipc::Mem::RegisteredRunner
754 /* RegisteredRunner API */
755 MemStoreRr(): spaceOwner(NULL
), mapOwner(NULL
), extrasOwner(NULL
) {}
756 virtual void finalizeConfig();
757 virtual void claimMemoryNeeds();
758 virtual void useConfig();
759 virtual ~MemStoreRr();
762 /* Ipc::Mem::RegisteredRunner API */
763 virtual void create();
766 Ipc::Mem::Owner
<Ipc::Mem::PageStack
> *spaceOwner
; ///< free slices Owner
767 MemStoreMap::Owner
*mapOwner
; ///< primary map Owner
768 Ipc::Mem::Owner
<MemStoreMapExtras
> *extrasOwner
; ///< PageIds 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::Mem::Segment::Enabled() && UsingSmp() &&
785 Config
.memMaxSize
> 0);
786 } else if (Config
.memShared
&& !Ipc::Mem::Segment::Enabled()) {
787 fatal("memory_cache_shared is on, but no support for shared memory detected");
788 } else if (Config
.memShared
&& !UsingSmp()) {
789 debugs(20, DBG_IMPORTANT
, "WARNING: memory_cache_shared is on, but only"
790 " a single worker is running");
795 MemStoreRr::useConfig()
797 assert(Config
.memShared
.configured());
798 Ipc::Mem::RegisteredRunner::useConfig();
804 if (!Config
.memShared
)
807 const int64_t entryLimit
= MemStore::EntryLimit();
808 if (entryLimit
<= 0) {
809 if (Config
.memMaxSize
> 0) {
810 debugs(20, DBG_IMPORTANT
, "WARNING: mem-cache size is too small ("
811 << (Config
.memMaxSize
/ 1024.0) << " KB), should be >= " <<
812 (Ipc::Mem::PageSize() / 1024.0) << " KB");
814 return; // no memory cache configured or a misconfiguration
818 spaceOwner
= shm_new(Ipc::Mem::PageStack
)(SpaceLabel
, SpacePoolId
,
821 mapOwner
= MemStoreMap::Init(MapLabel
, entryLimit
);
823 extrasOwner
= shm_new(MemStoreMapExtras
)(ExtrasLabel
, entryLimit
);
826 MemStoreRr::~MemStoreRr()