2 * DEBUG: section 47 Store Directory Routines
7 #include "CollapsedForwarding.h"
8 #include "ConfigOption.h"
9 #include "DiskIO/DiskIOModule.h"
10 #include "DiskIO/DiskIOStrategy.h"
11 #include "DiskIO/ReadRequest.h"
12 #include "DiskIO/WriteRequest.h"
13 #include "fs/rock/RockSwapDir.h"
14 #include "fs/rock/RockIoState.h"
15 #include "fs/rock/RockIoRequests.h"
16 #include "fs/rock/RockRebuild.h"
18 #include "ipc/mem/Pages.h"
19 #include "MemObject.h"
21 #include "SquidConfig.h"
22 #include "SquidMath.h"
32 const int64_t Rock::SwapDir::HeaderSize
= 16*1024;
34 Rock::SwapDir::SwapDir(): ::SwapDir("rock"),
35 slotSize(HeaderSize
), filePath(NULL
), map(NULL
), io(NULL
),
40 Rock::SwapDir::~SwapDir()
48 Rock::SwapDir::search(String
const url
, HttpRequest
*)
51 return NULL
; // XXX: implement
55 Rock::SwapDir::get(String
const key
, STOREGETCLIENT cb
, void *data
)
57 ::SwapDir::get(key
, cb
, data
);
60 // called when Squid core needs a StoreEntry with a given key
62 Rock::SwapDir::get(const cache_key
*key
)
64 if (!map
|| !theFile
|| !theFile
->canRead())
68 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(key
, filen
);
72 // create a brand new store entry and initialize it with stored basics
73 StoreEntry
*e
= new StoreEntry();
74 anchorEntry(*e
, filen
, *slot
);
80 // the disk entry remains open for reading, protected from modifications
84 Rock::SwapDir::anchorCollapsed(StoreEntry
&collapsed
, bool &inSync
)
86 if (!map
|| !theFile
|| !theFile
->canRead())
90 const Ipc::StoreMapAnchor
*const slot
= map
->openForReading(
91 reinterpret_cast<cache_key
*>(collapsed
.key
), filen
);
95 anchorEntry(collapsed
, filen
, *slot
);
96 inSync
= updateCollapsedWith(collapsed
, *slot
);
101 Rock::SwapDir::updateCollapsed(StoreEntry
&collapsed
)
103 if (!map
|| !theFile
|| !theFile
->canRead())
106 if (collapsed
.swap_filen
< 0) // no longer using a disk cache
108 assert(collapsed
.swap_dirn
== index
);
110 const Ipc::StoreMapAnchor
&s
= map
->readableEntry(collapsed
.swap_filen
);
111 return updateCollapsedWith(collapsed
, s
);
115 Rock::SwapDir::updateCollapsedWith(StoreEntry
&collapsed
, const Ipc::StoreMapAnchor
&anchor
)
117 collapsed
.swap_file_sz
= anchor
.basics
.swap_file_sz
; // XXX: make atomic
122 Rock::SwapDir::anchorEntry(StoreEntry
&e
, const sfileno filen
, const Ipc::StoreMapAnchor
&anchor
)
124 const Ipc::StoreMapAnchor::Basics
&basics
= anchor
.basics
;
126 e
.swap_file_sz
= basics
.swap_file_sz
;
128 e
.swap_filen
= filen
;
129 e
.lastref
= basics
.lastref
;
130 e
.timestamp
= basics
.timestamp
;
131 e
.expires
= basics
.expires
;
132 e
.lastmod
= basics
.lastmod
;
133 e
.refcount
= basics
.refcount
;
134 e
.flags
= basics
.flags
;
136 e
.store_status
= STORE_OK
;
137 e
.setMemStatus(NOT_IN_MEMORY
);
138 e
.swap_status
= SWAPOUT_DONE
;
139 e
.ping_status
= PING_NONE
;
141 EBIT_SET(e
.flags
, ENTRY_CACHABLE
);
142 EBIT_CLR(e
.flags
, RELEASE_REQUEST
);
143 EBIT_CLR(e
.flags
, KEY_PRIVATE
);
144 EBIT_SET(e
.flags
, ENTRY_VALIDATED
);
148 void Rock::SwapDir::disconnect(StoreEntry
&e
)
150 assert(e
.swap_dirn
== index
);
151 assert(e
.swap_filen
>= 0);
152 // cannot have SWAPOUT_NONE entry with swap_filen >= 0
153 assert(e
.swap_status
!= SWAPOUT_NONE
);
155 // do not rely on e.swap_status here because there is an async delay
156 // before it switches from SWAPOUT_WRITING to SWAPOUT_DONE.
158 // since e has swap_filen, its slot is locked for reading and/or writing
159 // but it is difficult to know whether THIS worker is reading or writing e
160 if (e
.swap_status
== SWAPOUT_WRITING
||
161 (e
.mem_obj
&& e
.mem_obj
->swapout
.sio
!= NULL
))
162 map
->abortWriting(e
.swap_filen
);
164 map
->closeForReading(e
.swap_filen
);
167 e
.swap_status
= SWAPOUT_NONE
;
171 Rock::SwapDir::currentSize() const
173 const uint64_t spaceSize
= !freeSlots
?
174 maxSize() : (slotSize
* freeSlots
->size());
175 // everything that is not free is in use
176 return maxSize() - spaceSize
;
180 Rock::SwapDir::currentCount() const
182 return map
? map
->entryCount() : 0;
185 /// In SMP mode only the disker process reports stats to avoid
186 /// counting the same stats by multiple processes.
188 Rock::SwapDir::doReportStat() const
190 return ::SwapDir::doReportStat() && (!UsingSmp() || IamDiskProcess());
194 Rock::SwapDir::swappedOut(const StoreEntry
&)
196 // stats are not stored but computed when needed
200 Rock::SwapDir::entryLimitAllowed() const
202 const int64_t eLimitLo
= map
? map
->entryLimit() : 0; // dynamic shrinking unsupported
203 const int64_t eWanted
= (maxSize() - HeaderSize
)/slotSize
;
204 return min(max(eLimitLo
, eWanted
), entryLimitHigh());
207 // TODO: encapsulate as a tool; identical to CossSwapDir::create()
209 Rock::SwapDir::create()
214 if (UsingSmp() && !IamDiskProcess()) {
215 debugs (47,3, HERE
<< "disker will create in " << path
);
219 debugs (47,3, HERE
<< "creating in " << path
);
222 if (::stat(path
, &dir_sb
) == 0) {
224 if (::stat(filePath
, &file_sb
) == 0) {
225 debugs (47, DBG_IMPORTANT
, "Skipping existing Rock db: " << filePath
);
228 // else the db file is not there or is not accessible, and we will try
229 // to create it later below, generating a detailed error on failures.
230 } else { // path does not exist or is inaccessible
231 // If path exists but is not accessible, mkdir() below will fail, and
232 // the admin should see the error and act accordingly, so there is
233 // no need to distinguish ENOENT from other possible stat() errors.
234 debugs (47, DBG_IMPORTANT
, "Creating Rock db directory: " << path
);
235 const int res
= mkdir(path
, 0700);
237 createError("mkdir");
240 debugs (47, DBG_IMPORTANT
, "Creating Rock db: " << filePath
);
241 const int swap
= open(filePath
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_BINARY
, 0600);
243 createError("create");
245 #if SLOWLY_FILL_WITH_ZEROS
247 Must(maxSize() % sizeof(block
) == 0);
248 memset(block
, '\0', sizeof(block
));
250 for (off_t offset
= 0; offset
< maxSize(); offset
+= sizeof(block
)) {
251 if (write(swap
, block
, sizeof(block
)) != sizeof(block
))
252 createError("write");
255 if (ftruncate(swap
, maxSize()) != 0)
256 createError("truncate");
258 char header
[HeaderSize
];
259 memset(header
, '\0', sizeof(header
));
260 if (write(swap
, header
, sizeof(header
)) != sizeof(header
))
261 createError("write");
267 // report Rock DB creation error and exit
269 Rock::SwapDir::createError(const char *const msg
) {
270 debugs(47, DBG_CRITICAL
, "ERROR: Failed to initialize Rock Store db in " <<
271 filePath
<< "; " << msg
<< " error: " << xstrerror());
272 fatal("Rock Store db creation error");
276 Rock::SwapDir::init()
280 // XXX: SwapDirs aren't refcounted. We make IORequestor calls, which
281 // are refcounted. We up our count once to avoid implicit delete's.
284 freeSlots
= shm_old(Ipc::Mem::PageStack
)(freeSlotsPath());
287 map
= new DirMap(inodeMapPath());
290 const char *ioModule
= needsDiskStrand() ? "IpcIo" : "Blocking";
291 if (DiskIOModule
*m
= DiskIOModule::Find(ioModule
)) {
292 debugs(47,2, HERE
<< "Using DiskIO module: " << ioModule
);
293 io
= m
->createStrategy();
296 debugs(47, DBG_CRITICAL
, "FATAL: Rock store is missing DiskIO module: " <<
298 fatal("Rock Store missing a required DiskIO module");
301 theFile
= io
->newFile(filePath
);
302 theFile
->configure(fileConfig
);
303 theFile
->open(O_RDWR
, 0644, this);
305 // Increment early. Otherwise, if one SwapDir finishes rebuild before
306 // others start, storeRebuildComplete() will think the rebuild is over!
307 // TODO: move store_dirs_rebuilding hack to store modules that need it.
308 ++StoreController::store_dirs_rebuilding
;
312 Rock::SwapDir::needsDiskStrand() const
314 const bool wontEvenWorkWithoutDisker
= Config
.workers
> 1;
315 const bool wouldWorkBetterWithDisker
= DiskIOModule::Find("IpcIo");
316 return InDaemonMode() && (wontEvenWorkWithoutDisker
||
317 wouldWorkBetterWithDisker
);
321 Rock::SwapDir::parse(int anIndex
, char *aPath
)
325 path
= xstrdup(aPath
);
327 // cache store is located at path/db
329 fname
.append("/rock");
330 filePath
= xstrdup(fname
.termedBuf());
335 // Current openForWriting() code overwrites the old slot if needed
336 // and possible, so proactively removing old slots is probably useless.
337 assert(!repl
); // repl = createRemovalPolicy(Config.replPolicy);
343 Rock::SwapDir::reconfigure()
347 // TODO: can we reconfigure the replacement policy (repl)?
351 /// parse maximum db disk size
353 Rock::SwapDir::parseSize(const bool reconfig
)
355 const int i
= GetInteger();
357 fatal("negative Rock cache_dir size value");
358 const uint64_t new_max_size
=
359 static_cast<uint64_t>(i
) << 20; // MBytes to Bytes
361 max_size
= new_max_size
;
362 else if (new_max_size
!= max_size
) {
363 debugs(3, DBG_IMPORTANT
, "WARNING: cache_dir '" << path
<< "' size "
364 "cannot be changed dynamically, value left unchanged (" <<
365 (max_size
>> 20) << " MB)");
370 Rock::SwapDir::getOptionTree() const
372 ConfigOptionVector
*vector
= dynamic_cast<ConfigOptionVector
*>(::SwapDir::getOptionTree());
374 vector
->options
.push_back(new ConfigOptionAdapter
<SwapDir
>(*const_cast<SwapDir
*>(this), &SwapDir::parseSizeOption
, &SwapDir::dumpSizeOption
));
375 vector
->options
.push_back(new ConfigOptionAdapter
<SwapDir
>(*const_cast<SwapDir
*>(this), &SwapDir::parseTimeOption
, &SwapDir::dumpTimeOption
));
376 vector
->options
.push_back(new ConfigOptionAdapter
<SwapDir
>(*const_cast<SwapDir
*>(this), &SwapDir::parseRateOption
, &SwapDir::dumpRateOption
));
381 Rock::SwapDir::allowOptionReconfigure(const char *const option
) const
383 return strcmp(option
, "slot-size") != 0 &&
384 ::SwapDir::allowOptionReconfigure(option
);
387 /// parses time-specific options; mimics ::SwapDir::optionObjectSizeParse()
389 Rock::SwapDir::parseTimeOption(char const *option
, const char *value
, int reconfig
)
391 // TODO: ::SwapDir or, better, Config should provide time-parsing routines,
392 // including time unit handling. Same for size and rate.
394 time_msec_t
*storedTime
;
395 if (strcmp(option
, "swap-timeout") == 0)
396 storedTime
= &fileConfig
.ioTimeout
;
403 // TODO: handle time units and detect parsing errors better
404 const int64_t parsedValue
= strtoll(value
, NULL
, 10);
405 if (parsedValue
< 0) {
406 debugs(3, DBG_CRITICAL
, "FATAL: cache_dir " << path
<< ' ' << option
<< " must not be negative but is: " << parsedValue
);
410 const time_msec_t newTime
= static_cast<time_msec_t
>(parsedValue
);
413 *storedTime
= newTime
;
414 else if (*storedTime
!= newTime
) {
415 debugs(3, DBG_IMPORTANT
, "WARNING: cache_dir " << path
<< ' ' << option
416 << " cannot be changed dynamically, value left unchanged: " <<
423 /// reports time-specific options; mimics ::SwapDir::optionObjectSizeDump()
425 Rock::SwapDir::dumpTimeOption(StoreEntry
* e
) const
427 if (fileConfig
.ioTimeout
)
428 storeAppendPrintf(e
, " swap-timeout=%" PRId64
,
429 static_cast<int64_t>(fileConfig
.ioTimeout
));
432 /// parses rate-specific options; mimics ::SwapDir::optionObjectSizeParse()
434 Rock::SwapDir::parseRateOption(char const *option
, const char *value
, int isaReconfig
)
437 if (strcmp(option
, "max-swap-rate") == 0)
438 storedRate
= &fileConfig
.ioRate
;
445 // TODO: handle time units and detect parsing errors better
446 const int64_t parsedValue
= strtoll(value
, NULL
, 10);
447 if (parsedValue
< 0) {
448 debugs(3, DBG_CRITICAL
, "FATAL: cache_dir " << path
<< ' ' << option
<< " must not be negative but is: " << parsedValue
);
452 const int newRate
= static_cast<int>(parsedValue
);
455 debugs(3, DBG_CRITICAL
, "FATAL: cache_dir " << path
<< ' ' << option
<< " must not be negative but is: " << newRate
);
460 *storedRate
= newRate
;
461 else if (*storedRate
!= newRate
) {
462 debugs(3, DBG_IMPORTANT
, "WARNING: cache_dir " << path
<< ' ' << option
463 << " cannot be changed dynamically, value left unchanged: " <<
470 /// reports rate-specific options; mimics ::SwapDir::optionObjectSizeDump()
472 Rock::SwapDir::dumpRateOption(StoreEntry
* e
) const
474 if (fileConfig
.ioRate
>= 0)
475 storeAppendPrintf(e
, " max-swap-rate=%d", fileConfig
.ioRate
);
478 /// parses size-specific options; mimics ::SwapDir::optionObjectSizeParse()
480 Rock::SwapDir::parseSizeOption(char const *option
, const char *value
, int reconfig
)
482 uint64_t *storedSize
;
483 if (strcmp(option
, "slot-size") == 0)
484 storedSize
= &slotSize
;
491 // TODO: handle size units and detect parsing errors better
492 const uint64_t newSize
= strtoll(value
, NULL
, 10);
494 debugs(3, DBG_CRITICAL
, "FATAL: cache_dir " << path
<< ' ' << option
<< " must be positive; got: " << newSize
);
498 if (newSize
<= sizeof(DbCellHeader
)) {
499 debugs(3, DBG_CRITICAL
, "FATAL: cache_dir " << path
<< ' ' << option
<< " must exceed " << sizeof(DbCellHeader
) << "; got: " << newSize
);
504 *storedSize
= newSize
;
505 else if (*storedSize
!= newSize
) {
506 debugs(3, DBG_IMPORTANT
, "WARNING: cache_dir " << path
<< ' ' << option
507 << " cannot be changed dynamically, value left unchanged: " <<
514 /// reports size-specific options; mimics ::SwapDir::optionObjectSizeDump()
516 Rock::SwapDir::dumpSizeOption(StoreEntry
* e
) const
518 storeAppendPrintf(e
, " slot-size=%" PRId64
, slotSize
);
521 /// check the results of the configuration; only level-0 debugging works here
523 Rock::SwapDir::validateOptions()
526 fatal("Rock store requires a positive slot-size");
528 const int64_t maxSizeRoundingWaste
= 1024 * 1024; // size is configured in MB
529 const int64_t slotSizeRoundingWaste
= slotSize
;
530 const int64_t maxRoundingWaste
=
531 max(maxSizeRoundingWaste
, slotSizeRoundingWaste
);
532 const int64_t usableDiskSize
= diskOffset(entryLimitAllowed());
533 const int64_t diskWasteSize
= maxSize() - usableDiskSize
;
534 Must(diskWasteSize
>= 0);
536 // warn if maximum db size is not reachable due to sfileno limit
537 if (entryLimitAllowed() == entryLimitHigh() &&
538 diskWasteSize
>= maxRoundingWaste
) {
539 debugs(47, DBG_CRITICAL
, "Rock store cache_dir[" << index
<< "] '" << path
<< "':");
540 debugs(47, DBG_CRITICAL
, "\tmaximum number of entries: " << entryLimitAllowed());
541 debugs(47, DBG_CRITICAL
, "\tdb slot size: " << slotSize
<< " Bytes");
542 debugs(47, DBG_CRITICAL
, "\tmaximum db size: " << maxSize() << " Bytes");
543 debugs(47, DBG_CRITICAL
, "\tusable db size: " << usableDiskSize
<< " Bytes");
544 debugs(47, DBG_CRITICAL
, "\tdisk space waste: " << diskWasteSize
<< " Bytes");
545 debugs(47, DBG_CRITICAL
, "WARNING: Rock store config wastes space.");
550 Rock::SwapDir::rebuild()
552 //++StoreController::store_dirs_rebuilding; // see Rock::SwapDir::init()
553 AsyncJob::Start(new Rebuild(this));
557 Rock::SwapDir::canStore(const StoreEntry
&e
, int64_t diskSpaceNeeded
, int &load
) const
559 if (!::SwapDir::canStore(e
, sizeof(DbCellHeader
)+diskSpaceNeeded
, load
))
562 if (!theFile
|| !theFile
->canWrite())
568 // Do not start I/O transaction if there are less than 10% free pages left.
569 // TODO: reserve page instead
570 if (needsDiskStrand() &&
571 Ipc::Mem::PageLevel(Ipc::Mem::PageId::ioPage
) >= 0.9 * Ipc::Mem::PageLimit(Ipc::Mem::PageId::ioPage
)) {
572 debugs(47, 5, HERE
<< "too few shared pages for IPC I/O left");
583 StoreIOState::Pointer
584 Rock::SwapDir::createStoreIO(StoreEntry
&e
, StoreIOState::STFNCB
*cbFile
, StoreIOState::STIOCB
*cbIo
, void *data
)
586 if (!theFile
|| theFile
->error()) {
587 debugs(47,4, HERE
<< theFile
);
592 Ipc::StoreMapAnchor
*const slot
=
593 map
->openForWriting(reinterpret_cast<const cache_key
*>(e
.key
), filen
);
595 debugs(47, 5, HERE
<< "map->add failed");
602 // XXX: We rely on our caller, storeSwapOutStart(), to set e.fileno.
603 // If that does not happen, the entry will not decrement the read level!
605 Rock::SwapDir::Pointer
self(this);
606 IoState
*sio
= new IoState(self
, &e
, cbFile
, cbIo
, data
);
608 sio
->swap_dirn
= index
;
609 sio
->swap_filen
= filen
;
610 sio
->writeableAnchor_
= slot
;
612 debugs(47,5, HERE
<< "dir " << index
<< " created new filen " <<
613 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) <<
614 sio
->swap_filen
<< std::dec
<< " starting at " <<
615 diskOffset(sio
->swap_filen
));
624 Rock::SwapDir::diskOffset(int filen
) const
627 return HeaderSize
+ slotSize
*filen
;
631 Rock::SwapDir::diskOffset(Ipc::Mem::PageId
&pageId
) const
634 return diskOffset(pageId
.number
- 1);
638 Rock::SwapDir::diskOffsetLimit() const
641 return diskOffset(map
->entryLimit());
645 Rock::SwapDir::entryMaxPayloadSize() const
647 return slotSize
- sizeof(DbCellHeader
);
651 Rock::SwapDir::entriesNeeded(const int64_t objSize
) const
653 return (objSize
+ entryMaxPayloadSize() - 1) / entryMaxPayloadSize();
657 Rock::SwapDir::useFreeSlot(Ipc::Mem::PageId
&pageId
)
659 if (freeSlots
->pop(pageId
)) {
660 debugs(47, 5, "got a previously free slot: " << pageId
);
664 // catch free slots delivered to noteFreeMapSlice()
665 assert(!waitingForPage
);
666 waitingForPage
= &pageId
;
667 if (map
->purgeOne()) {
668 assert(!waitingForPage
); // noteFreeMapSlice() should have cleared it
669 assert(pageId
.set());
670 debugs(47, 5, "got a previously busy slot: " << pageId
);
673 assert(waitingForPage
== &pageId
);
674 waitingForPage
= NULL
;
676 debugs(47, 3, "cannot get a slot; entries: " << map
->entryCount());
681 Rock::SwapDir::validSlotId(const SlotId slotId
) const
683 return 0 <= slotId
&& slotId
< entryLimitAllowed();
687 Rock::SwapDir::noteFreeMapSlice(const sfileno sliceId
)
689 Ipc::Mem::PageId pageId
;
690 pageId
.pool
= index
+1;
691 pageId
.number
= sliceId
+1;
692 if (waitingForPage
) {
693 *waitingForPage
= pageId
;
694 waitingForPage
= NULL
;
696 freeSlots
->push(pageId
);
700 // tries to open an old entry with swap_filen for reading
701 StoreIOState::Pointer
702 Rock::SwapDir::openStoreIO(StoreEntry
&e
, StoreIOState::STFNCB
*cbFile
, StoreIOState::STIOCB
*cbIo
, void *data
)
704 if (!theFile
|| theFile
->error()) {
705 debugs(47,4, HERE
<< theFile
);
709 if (e
.swap_filen
< 0) {
710 debugs(47,4, HERE
<< e
);
714 // Do not start I/O transaction if there are less than 10% free pages left.
715 // TODO: reserve page instead
716 if (needsDiskStrand() &&
717 Ipc::Mem::PageLevel(Ipc::Mem::PageId::ioPage
) >= 0.9 * Ipc::Mem::PageLimit(Ipc::Mem::PageId::ioPage
)) {
718 debugs(47, 5, HERE
<< "too few shared pages for IPC I/O left");
722 // The are two ways an entry can get swap_filen: our get() locked it for
723 // reading or our storeSwapOutStart() locked it for writing. Peeking at our
724 // locked entry is safe, but no support for reading a filling entry.
725 const Ipc::StoreMapAnchor
*slot
= map
->peekAtReader(e
.swap_filen
);
727 return NULL
; // we were writing afterall
729 Rock::SwapDir::Pointer
self(this);
730 IoState
*sio
= new IoState(self
, &e
, cbFile
, cbIo
, data
);
732 sio
->swap_dirn
= index
;
733 sio
->swap_filen
= e
.swap_filen
;
734 sio
->readableAnchor_
= slot
;
737 debugs(47,5, HERE
<< "dir " << index
<< " has old filen: " <<
738 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) <<
741 assert(slot
->sameKey(static_cast<const cache_key
*>(e
.key
)));
742 assert(slot
->basics
.swap_file_sz
> 0);
743 assert(slot
->basics
.swap_file_sz
== e
.swap_file_sz
);
749 Rock::SwapDir::ioCompletedNotification()
752 fatalf("Rock cache_dir failed to initialize db file: %s", filePath
);
754 if (theFile
->error())
755 fatalf("Rock cache_dir at %s failed to open db file: %s", filePath
,
758 debugs(47, 2, "Rock cache_dir[" << index
<< "] limits: " <<
759 std::setw(12) << maxSize() << " disk bytes and " <<
760 std::setw(7) << map
->entryLimit() << " entries");
766 Rock::SwapDir::closeCompleted()
772 Rock::SwapDir::readCompleted(const char *buf
, int rlen
, int errflag
, RefCount
< ::ReadRequest
> r
)
774 ReadRequest
*request
= dynamic_cast<Rock::ReadRequest
*>(r
.getRaw());
776 IoState::Pointer sio
= request
->sio
;
778 if (errflag
== DISK_OK
&& rlen
> 0)
779 sio
->offset_
+= rlen
;
781 StoreIOState::STRCB
*callb
= sio
->read
.callback
;
783 sio
->read
.callback
= NULL
;
785 if (cbdataReferenceValidDone(sio
->read
.callback_data
, &cbdata
))
786 callb(cbdata
, r
->buf
, rlen
, sio
.getRaw());
790 Rock::SwapDir::writeCompleted(int errflag
, size_t rlen
, RefCount
< ::WriteRequest
> r
)
792 Rock::WriteRequest
*request
= dynamic_cast<Rock::WriteRequest
*>(r
.getRaw());
794 assert(request
->sio
!= NULL
);
795 IoState
&sio
= *request
->sio
;
797 // quit if somebody called IoState::close() while we were waiting
798 if (!sio
.stillWaiting()) {
799 debugs(79, 3, "ignoring closed entry " << sio
.swap_filen
);
803 if (errflag
== DISK_OK
) {
804 // do not increment sio.offset_ because we do it in sio->write()
806 // finalize the shared slice info after writing slice contents to disk
807 Ipc::StoreMap::Slice
&slice
=
808 map
->writeableSlice(sio
.swap_filen
, request
->sidCurrent
);
809 slice
.size
= request
->len
- sizeof(DbCellHeader
);
810 slice
.next
= request
->sidNext
;
812 if (request
->sidNext
< 0) {
813 // close, the entry gets the read lock
814 map
->closeForWriting(sio
.swap_filen
, true);
815 sio
.finishedWriting(errflag
);
819 sio
.finishedWriting(errflag
);
820 // and hope that Core will call disconnect() to close the map entry
823 CollapsedForwarding::Broadcast(*sio
.e
);
827 Rock::SwapDir::writeError(StoreEntry
&e
)
829 // Do not abortWriting here. The entry should keep the write lock
830 // instead of losing association with the store and confusing core.
831 map
->freeEntry(e
.swap_filen
); // will mark as unusable, just in case
833 Store::Root().transientsAbandon(e
);
835 // All callers must also call IoState callback, to propagate the error.
839 Rock::SwapDir::full() const
841 return freeSlots
!= NULL
&& !freeSlots
->size();
844 // storeSwapOutFileClosed calls this nethod on DISK_NO_SPACE_LEFT,
845 // but it should not happen for us
847 Rock::SwapDir::diskFull()
849 debugs(20, DBG_IMPORTANT
, "BUG: No space left with rock cache_dir: " <<
853 /// purge while full(); it should be sufficient to purge just one
855 Rock::SwapDir::maintain()
857 // The Store calls this to free some db space, but there is nothing wrong
858 // with a full() db, except when db has to shrink after reconfigure, and
859 // we do not support shrinking yet (it would have to purge specific slots).
860 // TODO: Disable maintain() requests when they are pointless.
864 Rock::SwapDir::reference(StoreEntry
&e
)
866 debugs(47, 5, HERE
<< &e
<< ' ' << e
.swap_dirn
<< ' ' << e
.swap_filen
);
867 if (repl
&& repl
->Referenced
)
868 repl
->Referenced(repl
, &e
, &e
.repl
);
872 Rock::SwapDir::dereference(StoreEntry
&e
, bool)
874 debugs(47, 5, HERE
<< &e
<< ' ' << e
.swap_dirn
<< ' ' << e
.swap_filen
);
875 if (repl
&& repl
->Dereferenced
)
876 repl
->Dereferenced(repl
, &e
, &e
.repl
);
878 // no need to keep e in the global store_table for us; we have our own map
883 Rock::SwapDir::unlinkdUseful() const
885 // no entry-specific files to unlink
890 Rock::SwapDir::unlink(StoreEntry
&e
)
892 debugs(47, 5, HERE
<< e
);
894 map
->freeEntry(e
.swap_filen
);
899 Rock::SwapDir::markForUnlink(StoreEntry
&e
)
902 map
->freeEntry(e
.swap_filen
);
906 Rock::SwapDir::trackReferences(StoreEntry
&e
)
908 debugs(47, 5, HERE
<< e
);
910 repl
->Add(repl
, &e
, &e
.repl
);
914 Rock::SwapDir::ignoreReferences(StoreEntry
&e
)
916 debugs(47, 5, HERE
<< e
);
918 repl
->Remove(repl
, &e
, &e
.repl
);
922 Rock::SwapDir::statfs(StoreEntry
&e
) const
924 storeAppendPrintf(&e
, "\n");
925 storeAppendPrintf(&e
, "Maximum Size: %" PRIu64
" KB\n", maxSize() >> 10);
926 storeAppendPrintf(&e
, "Current Size: %.2f KB %.2f%%\n",
927 currentSize() / 1024.0,
928 Math::doublePercent(currentSize(), maxSize()));
931 const int limit
= map
->entryLimit();
932 storeAppendPrintf(&e
, "Maximum entries: %9d\n", limit
);
934 const int entryCount
= map
->entryCount();
935 storeAppendPrintf(&e
, "Current entries: %9d %.2f%%\n",
936 entryCount
, (100.0 * entryCount
/ limit
));
938 const unsigned int slotsFree
= !freeSlots
? 0 : freeSlots
->size();
939 if (slotsFree
<= static_cast<const unsigned int>(limit
)) {
940 const int usedSlots
= limit
- static_cast<const int>(slotsFree
);
941 storeAppendPrintf(&e
, "Used slots: %9d %.2f%%\n",
942 usedSlots
, (100.0 * usedSlots
/ limit
));
944 if (limit
< 100) { // XXX: otherwise too expensive to count
945 Ipc::ReadWriteLockStats stats
;
946 map
->updateStats(stats
);
952 storeAppendPrintf(&e
, "Pending operations: %d out of %d\n",
953 store_open_disk_fd
, Config
.max_open_disk_fds
);
955 storeAppendPrintf(&e
, "Flags:");
958 storeAppendPrintf(&e
, " SELECTED");
961 storeAppendPrintf(&e
, " READ-ONLY");
963 storeAppendPrintf(&e
, "\n");
968 Rock::SwapDir::inodeMapPath() const {
969 static String inodesPath
;
971 inodesPath
.append("_inodes");
972 return inodesPath
.termedBuf();
976 Rock::SwapDir::freeSlotsPath() const {
977 static String spacesPath
;
979 spacesPath
.append("_spaces");
980 return spacesPath
.termedBuf();
985 RunnerRegistrationEntry(rrAfterConfig
, SwapDirRr
);
988 void Rock::SwapDirRr::create(const RunnerRegistry
&)
990 Must(mapOwners
.empty() && freeSlotsOwners
.empty());
991 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
992 if (const Rock::SwapDir
*const sd
= dynamic_cast<Rock::SwapDir
*>(INDEXSD(i
))) {
993 const int64_t capacity
= sd
->entryLimitAllowed();
995 SwapDir::DirMap::Owner
*const mapOwner
=
996 SwapDir::DirMap::Init(sd
->inodeMapPath(), capacity
);
997 mapOwners
.push_back(mapOwner
);
999 // XXX: remove pool id and counters from PageStack
1000 Ipc::Mem::Owner
<Ipc::Mem::PageStack
> *const freeSlotsOwner
=
1001 shm_new(Ipc::Mem::PageStack
)(sd
->freeSlotsPath(),
1003 sizeof(DbCellHeader
));
1004 freeSlotsOwners
.push_back(freeSlotsOwner
);
1006 // XXX: add method to initialize PageStack with no free pages
1008 Ipc::Mem::PageId pageId
;
1009 if (!freeSlotsOwner
->object()->pop(pageId
))
1016 Rock::SwapDirRr::~SwapDirRr()
1018 for (size_t i
= 0; i
< mapOwners
.size(); ++i
) {
1019 delete mapOwners
[i
];
1020 delete freeSlotsOwners
[i
];