3 * DEBUG: section 47 Store Directory Routines
4 * AUTHOR: Duane Wessels
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "MemObject.h"
39 #include "profiler/Profiler.h"
40 #include "SquidConfig.h"
41 #include "SquidMath.h"
42 #include "SquidTime.h"
44 #include "store_key_md5.h"
45 #include "StoreHashIndex.h"
46 #include "swap_log_op.h"
49 #include "Transients.h"
52 #if HAVE_SYS_STATVFS_H
53 #include <sys/statvfs.h>
55 #endif /* HAVE_STATVFS */
56 /* statfs() needs <sys/param.h> and <sys/mount.h> on BSD systems */
58 #include <sys/param.h>
64 #include <sys/mount.h>
66 /* Windows and Linux use sys/vfs.h */
77 static STDIRSELECT storeDirSelectSwapDirRoundRobin
;
78 static STDIRSELECT storeDirSelectSwapDirLeastLoad
;
81 * store_dirs_rebuilding is initialized to _1_ as a hack so that
82 * storeDirWriteCleanLogs() doesn't try to do anything unless _all_
83 * cache_dirs have been read. For example, without this hack, Squid
84 * will try to write clean log files if -kparse fails (becasue it
87 int StoreController::store_dirs_rebuilding
= 1;
89 StoreController::StoreController() : swapDir (new StoreHashIndex())
90 , memStore(NULL
), transients(NULL
)
93 StoreController::~StoreController()
100 * This function pointer is set according to 'store_dir_select_algorithm'
103 STDIRSELECT
*storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
106 StoreController::init()
108 if (Config
.memShared
&& IamWorkerProcess()) {
109 memStore
= new MemStore
;
115 if (0 == strcasecmp(Config
.store_dir_select_algorithm
, "round-robin")) {
116 storeDirSelectSwapDir
= storeDirSelectSwapDirRoundRobin
;
117 debugs(47, DBG_IMPORTANT
, "Using Round Robin store dir selection");
119 storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
120 debugs(47, DBG_IMPORTANT
, "Using Least Load store dir selection");
123 if (UsingSmp() && IamWorkerProcess() && Config
.onoff
.collapsed_forwarding
) {
124 transients
= new Transients
;
130 StoreController::createOneStore(Store
&aStore
)
133 * On Windows, fork() is not available.
134 * The following is a workaround for create store directories sequentially
135 * when running on native Windows port.
154 StoreController::create()
166 pid
= wait3(&status
, WNOHANG
, NULL
);
169 pid
= waitpid(-1, &status
, 0);
172 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
178 * Determine whether the given directory can handle this object
181 * Note: if the object size is -1, then the only swapdirs that
182 * will return true here are ones that have min and max unset,
183 * ie any-sized-object swapdirs. This is a good thing.
186 SwapDir::objectSizeIsAcceptable(int64_t objsize
) const
188 // If the swapdir has no range limits, then it definitely can
189 if (min_objsize
<= 0 && max_objsize
== -1)
193 * If the object size is -1 and the storedir has limits we
194 * can't store it there.
199 // Else, make sure that the object size will fit.
200 if (max_objsize
== -1 && min_objsize
<= objsize
)
203 return min_objsize
<= objsize
&& max_objsize
> objsize
;
207 * This new selection scheme simply does round-robin on all SwapDirs.
208 * A SwapDir is skipped if it is over the max_size (100%) limit, or
212 storeDirSelectSwapDirRoundRobin(const StoreEntry
* e
)
217 RefCount
<SwapDir
> sd
;
219 // e->objectLen() is negative at this point when we are still STORE_PENDING
220 ssize_t objsize
= e
->mem_obj
->expectedReplySize();
222 objsize
+= e
->mem_obj
->swap_hdr_sz
;
224 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
225 if (++dirn
>= Config
.cacheSwap
.n_configured
)
228 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
230 if (!sd
->canStore(*e
, objsize
, load
))
233 if (load
< 0 || load
> 1000) {
244 * Spread load across all of the store directories
246 * Note: We should modify this later on to prefer sticking objects
247 * in the *tightest fit* swapdir to conserve space, along with the
248 * actual swapdir usage. But for now, this hack will do while
249 * testing, so you should order your swapdirs in the config file
250 * from smallest max-size= to largest max-size=.
252 * We also have to choose nleast == nconf since we need to consider
253 * ALL swapdirs, regardless of state. Again, this is a hack while
254 * we sort out the real usefulness of this algorithm.
257 storeDirSelectSwapDirLeastLoad(const StoreEntry
* e
)
259 int64_t most_free
= 0;
260 ssize_t least_objsize
= -1;
261 int least_load
= INT_MAX
;
265 RefCount
<SwapDir
> SD
;
267 // e->objectLen() is negative at this point when we are still STORE_PENDING
268 ssize_t objsize
= e
->mem_obj
->expectedReplySize();
271 objsize
+= e
->mem_obj
->swap_hdr_sz
;
273 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
274 SD
= dynamic_cast<SwapDir
*>(INDEXSD(i
));
275 SD
->flags
.selected
= false;
277 if (!SD
->canStore(*e
, objsize
, load
))
280 if (load
< 0 || load
> 1000)
283 if (load
> least_load
)
286 const int64_t cur_free
= SD
->maxSize() - SD
->currentSize();
288 /* If the load is equal, then look in more details */
289 if (load
== least_load
) {
290 /* closest max-size fit */
292 if (least_objsize
!= -1)
293 if (SD
->maxObjectSize() > least_objsize
)
297 if (cur_free
< most_free
)
302 least_objsize
= SD
->maxObjectSize();
303 most_free
= cur_free
;
308 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->flags
.selected
= true;
314 * An entry written to the swap log MUST have the following
316 * 1. It MUST be a public key. It does no good to log
317 * a public ADD, change the key, then log a private
318 * DEL. So we need to log a DEL before we change a
319 * key from public to private.
320 * 2. It MUST have a valid (> -1) swap_filen.
323 storeDirSwapLog(const StoreEntry
* e
, int op
)
326 assert(!EBIT_TEST(e
->flags
, KEY_PRIVATE
));
327 assert(e
->swap_filen
>= 0);
329 * icons and such; don't write them to the swap log
332 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
335 assert(op
> SWAP_LOG_NOP
&& op
< SWAP_LOG_MAX
);
337 debugs(20, 3, "storeDirSwapLog: " <<
338 swap_log_op_str
[op
] << " " <<
339 e
->getMD5Text() << " " <<
340 e
->swap_dirn
<< " " <<
341 std::hex
<< std::uppercase
<< std::setfill('0') << std::setw(8) << e
->swap_filen
);
343 dynamic_cast<SwapDir
*>(INDEXSD(e
->swap_dirn
))->logEntry(*e
, op
);
347 StoreController::getStats(StoreInfoStats
&stats
) const
350 memStore
->getStats(stats
);
352 // move this code to a non-shared memory cache class when we have it
353 stats
.mem
.shared
= false;
354 stats
.mem
.capacity
= Config
.memMaxSize
;
355 stats
.mem
.size
= mem_node::StoreMemSize();
356 stats
.mem
.count
= hot_obj_count
;
359 swapDir
->getStats(stats
);
361 // low-level info not specific to memory or disk cache
362 stats
.store_entry_count
= StoreEntry::inUseCount();
363 stats
.mem_object_count
= MemObject::inUseCount();
367 StoreController::stat(StoreEntry
&output
) const
369 storeAppendPrintf(&output
, "Store Directory Statistics:\n");
370 storeAppendPrintf(&output
, "Store Entries : %lu\n",
371 (unsigned long int)StoreEntry::inUseCount());
372 storeAppendPrintf(&output
, "Maximum Swap Size : %" PRIu64
" KB\n",
374 storeAppendPrintf(&output
, "Current Store Swap Size: %.2f KB\n",
375 currentSize() / 1024.0);
376 storeAppendPrintf(&output
, "Current Capacity : %.2f%% used, %.2f%% free\n",
377 Math::doublePercent(currentSize(), maxSize()),
378 Math::doublePercent((maxSize() - currentSize()), maxSize()));
381 memStore
->stat(output
);
383 /* now the swapDir */
384 swapDir
->stat(output
);
387 /* if needed, this could be taught to cache the result */
389 StoreController::maxSize() const
391 /* TODO: include memory cache ? */
392 return swapDir
->maxSize();
396 StoreController::minSize() const
398 /* TODO: include memory cache ? */
399 return swapDir
->minSize();
403 StoreController::currentSize() const
405 return swapDir
->currentSize();
409 StoreController::currentCount() const
411 return swapDir
->currentCount();
415 StoreController::maxObjectSize() const
417 return swapDir
->maxObjectSize();
423 if (currentSize() >= maxSize())
426 max_size
= currentSize();
428 debugs(20, DBG_IMPORTANT
, "WARNING: Shrinking cache_dir #" << index
<< " to " << currentSize() / 1024.0 << " KB");
432 storeDirOpenSwapLogs(void)
434 for (int dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
435 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->openLog();
439 storeDirCloseSwapLogs(void)
441 for (int dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
442 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->closeLog();
446 * storeDirWriteCleanLogs
448 * Writes a "clean" swap log file from in-memory metadata.
449 * This is a rewrite of the original function to troll each
450 * StoreDir and write the logs, and flush at the end of
451 * the run. Thanks goes to Eric Stern, since this solution
452 * came out of his COSS code.
455 storeDirWriteCleanLogs(int reopen
)
457 const StoreEntry
*e
= NULL
;
460 struct timeval start
;
462 RefCount
<SwapDir
> sd
;
466 if (StoreController::store_dirs_rebuilding
) {
467 debugs(20, DBG_IMPORTANT
, "Not currently OK to rewrite swap log.");
468 debugs(20, DBG_IMPORTANT
, "storeDirWriteCleanLogs: Operation aborted.");
472 debugs(20, DBG_IMPORTANT
, "storeDirWriteCleanLogs: Starting...");
474 start
= current_time
;
476 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
) {
477 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
479 if (sd
->writeCleanStart() < 0) {
480 debugs(20, DBG_IMPORTANT
, "log.clean.start() failed for dir #" << sd
->index
);
486 * This may look inefficient as CPU wise it is more efficient to do this
487 * sequentially, but I/O wise the parallellism helps as it allows more
488 * hdd spindles to be active.
493 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
) {
494 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
496 if (NULL
== sd
->cleanLog
)
499 e
= sd
->cleanLog
->nextEntry();
509 sd
->cleanLog
->write(*e
);
511 if ((++n
& 0xFFFF) == 0) {
513 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << n
<<
514 " entries written so far.");
520 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
521 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->writeCleanDone();
524 storeDirOpenSwapLogs();
528 dt
= tvSubDsec(start
, current_time
);
530 debugs(20, DBG_IMPORTANT
, " Finished. Wrote " << n
<< " entries.");
531 debugs(20, DBG_IMPORTANT
, " Took "<< std::setw(3)<< std::setprecision(2) << dt
<<
532 " seconds ("<< std::setw(6) << ((double) n
/ (dt
> 0.0 ? dt
: 1.0)) << " entries/sec).");
538 StoreController::search(String
const url
, HttpRequest
*request
)
540 /* cheat, for now you can't search the memory hot cache */
541 return swapDir
->search(url
, request
);
545 StoreHashIndex::store(int const x
) const
551 StoreHashIndex::dir(const int i
) const
553 SwapDir
*sd
= dynamic_cast<SwapDir
*>(INDEXSD(i
));
559 StoreController::sync(void)
567 * handle callbacks all avaliable fs'es
570 StoreController::callback()
572 /* This will likely double count. Thats ok. */
573 PROF_start(storeDirCallback
);
575 /* mem cache callbacks ? */
576 int result
= swapDir
->callback();
578 PROF_stop(storeDirCallback
);
584 storeDirGetBlkSize(const char *path
, int *blksize
)
590 if (statvfs(path
, &sfs
)) {
591 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
596 *blksize
= (int) sfs
.f_frsize
;
601 if (statfs(path
, &sfs
)) {
602 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
607 *blksize
= (int) sfs
.f_bsize
;
610 * Sanity check; make sure we have a meaningful value.
619 #define fsbtoblk(num, fsbs, bs) \
620 (((fsbs) != 0 && (fsbs) < (bs)) ? \
621 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
623 storeDirGetUFSStats(const char *path
, int *totl_kb
, int *free_kb
, int *totl_in
, int *free_in
)
629 if (statvfs(path
, &sfs
)) {
630 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
634 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_frsize
, 1024);
635 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_frsize
, 1024);
636 *totl_in
= (int) sfs
.f_files
;
637 *free_in
= (int) sfs
.f_ffree
;
642 if (statfs(path
, &sfs
)) {
643 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
647 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_bsize
, 1024);
648 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_bsize
, 1024);
649 *totl_in
= (int) sfs
.f_files
;
650 *free_in
= (int) sfs
.f_ffree
;
657 allocate_new_swapdir(SquidConfig::_cacheSwap
* swap
)
659 if (swap
->swapDirs
== NULL
) {
660 swap
->n_allocated
= 4;
661 swap
->swapDirs
= static_cast<SwapDir::Pointer
*>(xcalloc(swap
->n_allocated
, sizeof(SwapDir::Pointer
)));
664 if (swap
->n_allocated
== swap
->n_configured
) {
665 swap
->n_allocated
<<= 1;
666 SwapDir::Pointer
*const tmp
= static_cast<SwapDir::Pointer
*>(xcalloc(swap
->n_allocated
, sizeof(SwapDir::Pointer
)));
667 memcpy(tmp
, swap
->swapDirs
, swap
->n_configured
* sizeof(SwapDir
*));
668 xfree(swap
->swapDirs
);
669 swap
->swapDirs
= tmp
;
674 free_cachedir(SquidConfig::_cacheSwap
* swap
)
677 /* DON'T FREE THESE FOR RECONFIGURE */
682 for (i
= 0; i
< swap
->n_configured
; ++i
) {
683 /* TODO XXX this lets the swapdir free resources asynchronously
684 * swap->swapDirs[i]->deactivate();
685 * but there may be such a means already.
688 swap
->swapDirs
[i
] = NULL
;
691 safe_free(swap
->swapDirs
);
692 swap
->swapDirs
= NULL
;
693 swap
->n_allocated
= 0;
694 swap
->n_configured
= 0;
697 /* this should be a virtual method on StoreEntry,
698 * i.e. e->referenced()
699 * so that the entry can notify the creating Store
702 StoreController::reference(StoreEntry
&e
)
704 // special entries do not belong to any specific Store, but are IN_MEMORY
705 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
))
708 /* Notify the fs that we're referencing this object again */
710 if (e
.swap_dirn
> -1)
711 swapDir
->reference(e
);
713 // Notify the memory cache that we're referencing this object again
714 if (memStore
&& e
.mem_status
== IN_MEMORY
)
715 memStore
->reference(e
);
717 // TODO: move this code to a non-shared memory cache class when we have it
719 if (mem_policy
->Referenced
)
720 mem_policy
->Referenced(mem_policy
, &e
, &e
.mem_obj
->repl
);
725 StoreController::dereference(StoreEntry
&e
, bool wantsLocalMemory
)
727 // special entries do not belong to any specific Store, but are IN_MEMORY
728 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
))
731 bool keepInStoreTable
= false; // keep only if somebody needs it there
733 /* Notify the fs that we're not referencing this object any more */
735 if (e
.swap_filen
> -1)
736 keepInStoreTable
= swapDir
->dereference(e
, wantsLocalMemory
) || keepInStoreTable
;
738 // Notify the memory cache that we're not referencing this object any more
739 if (memStore
&& e
.mem_status
== IN_MEMORY
)
740 keepInStoreTable
= memStore
->dereference(e
, wantsLocalMemory
) || keepInStoreTable
;
742 // TODO: move this code to a non-shared memory cache class when we have it
744 if (mem_policy
->Dereferenced
)
745 mem_policy
->Dereferenced(mem_policy
, &e
, &e
.mem_obj
->repl
);
746 // non-shared memory cache relies on store_table
748 keepInStoreTable
= wantsLocalMemory
|| keepInStoreTable
;
751 return keepInStoreTable
;
755 StoreController::get(const cache_key
*key
)
757 if (StoreEntry
*e
= find(key
)) {
758 // this is not very precise: some get()s are not initiated by clients
765 /// Internal method to implements the guts of the Store::get() API:
766 /// returns an in-transit or cached object with a given key, if any.
768 StoreController::find(const cache_key
*key
)
770 if (StoreEntry
*e
= swapDir
->get(key
)) {
771 // TODO: ignore and maybe handleIdleEntry() unlocked intransit entries
772 // because their backing store slot may be gone already.
773 debugs(20, 3, HERE
<< "got in-transit entry: " << *e
);
777 // Must search transients before caches because we must sync those we find.
779 if (StoreEntry
*e
= transients
->get(key
)) {
780 debugs(20, 3, "got shared in-transit entry: " << *e
);
782 const bool found
= anchorCollapsed(*e
, inSync
);
783 if (!found
|| inSync
)
785 assert(!e
->locked()); // ensure release will destroyStoreEntry()
786 e
->release(); // do not let others into the same trap
792 if (StoreEntry
*e
= memStore
->get(key
)) {
793 debugs(20, 3, HERE
<< "got mem-cached entry: " << *e
);
798 // TODO: this disk iteration is misplaced; move to StoreHashIndex when
799 // the global store_table is no longer used for in-transit objects.
800 if (const int cacheDirs
= Config
.cacheSwap
.n_configured
) {
801 // ask each cache_dir until the entry is found; use static starting
802 // point to avoid asking the same subset of disks more often
803 // TODO: coordinate with put() to be able to guess the right disk often
805 for (int n
= 0; n
< cacheDirs
; ++n
) {
806 idx
= (idx
+ 1) % cacheDirs
;
807 SwapDir
*sd
= dynamic_cast<SwapDir
*>(INDEXSD(idx
));
811 if (StoreEntry
*e
= sd
->get(key
)) {
812 debugs(20, 3, HERE
<< "cache_dir " << idx
<<
813 " got cached entry: " << *e
);
819 debugs(20, 4, HERE
<< "none of " << Config
.cacheSwap
.n_configured
<<
820 " cache_dirs have " << storeKeyText(key
));
826 StoreController::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
828 fatal("not implemented");
831 /// updates the collapsed entry with the corresponding on-disk entry, if any
832 /// In other words, the SwapDir::anchorCollapsed() API applied to all disks.
834 StoreController::anchorCollapsedOnDisk(StoreEntry
&collapsed
, bool &inSync
)
836 // TODO: move this loop to StoreHashIndex, just like the one in get().
837 if (const int cacheDirs
= Config
.cacheSwap
.n_configured
) {
838 // ask each cache_dir until the entry is found; use static starting
839 // point to avoid asking the same subset of disks more often
840 // TODO: coordinate with put() to be able to guess the right disk often
842 for (int n
= 0; n
< cacheDirs
; ++n
) {
843 idx
= (idx
+ 1) % cacheDirs
;
844 SwapDir
*sd
= dynamic_cast<SwapDir
*>(INDEXSD(idx
));
848 if (sd
->anchorCollapsed(collapsed
, inSync
)) {
849 debugs(20, 3, "cache_dir " << idx
<< " anchors " << collapsed
);
855 debugs(20, 4, "none of " << Config
.cacheSwap
.n_configured
<<
856 " cache_dirs have " << collapsed
);
860 void StoreController::markForUnlink(StoreEntry
&e
)
862 if (transients
&& e
.mem_obj
&& e
.mem_obj
->xitTable
.index
>= 0)
863 transients
->markForUnlink(e
);
864 if (memStore
&& e
.mem_obj
&& e
.mem_obj
->memCache
.index
>= 0)
865 memStore
->markForUnlink(e
);
866 if (e
.swap_filen
>= 0)
867 e
.store()->markForUnlink(e
);
870 // move this into [non-shared] memory cache class when we have one
871 /// whether e should be kept in local RAM for possible future caching
873 StoreController::keepForLocalMemoryCache(const StoreEntry
&e
) const
875 if (!e
.memoryCachable())
878 // does the current and expected size obey memory caching limits?
880 const int64_t loadedSize
= e
.mem_obj
->endOffset();
881 const int64_t expectedSize
= e
.mem_obj
->expectedReplySize(); // may be < 0
882 const int64_t ramSize
= max(loadedSize
, expectedSize
);
883 const int64_t ramLimit
= min(
884 static_cast<int64_t>(Config
.memMaxSize
),
885 static_cast<int64_t>(Config
.Store
.maxInMemObjSize
));
886 return ramSize
<= ramLimit
;
890 StoreController::memoryOut(StoreEntry
&e
, const bool preserveSwappable
)
892 bool keepInLocalMemory
= false;
894 memStore
->write(e
); // leave keepInLocalMemory false
896 keepInLocalMemory
= keepForLocalMemoryCache(e
);
898 debugs(20, 7, HERE
<< "keepInLocalMemory: " << keepInLocalMemory
);
900 if (!keepInLocalMemory
)
901 e
.trimMemory(preserveSwappable
);
905 StoreController::memoryUnlink(StoreEntry
&e
)
909 else // TODO: move into [non-shared] memory cache class when we have one
910 e
.destroyMemObject();
914 StoreController::memoryDisconnect(StoreEntry
&e
)
917 memStore
->disconnect(e
);
918 // else nothing to do for non-shared memory cache
922 StoreController::transientsAbandon(StoreEntry
&e
)
926 if (e
.mem_obj
->xitTable
.index
>= 0)
927 transients
->abandon(e
);
932 StoreController::transientsCompleteWriting(StoreEntry
&e
)
936 if (e
.mem_obj
->xitTable
.index
>= 0)
937 transients
->completeWriting(e
);
942 StoreController::transientReaders(const StoreEntry
&e
) const
944 return (transients
&& e
.mem_obj
&& e
.mem_obj
->xitTable
.index
>= 0) ?
945 transients
->readers(e
) : 0;
949 StoreController::transientsDisconnect(MemObject
&mem_obj
)
952 transients
->disconnect(mem_obj
);
956 StoreController::handleIdleEntry(StoreEntry
&e
)
958 bool keepInLocalMemory
= false;
960 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) {
961 // Icons (and cache digests?) should stay in store_table until we
962 // have a dedicated storage for them (that would not purge them).
963 // They are not managed [well] by any specific Store handled below.
964 keepInLocalMemory
= true;
965 } else if (memStore
) {
966 // leave keepInLocalMemory false; memStore maintains its own cache
968 keepInLocalMemory
= keepForLocalMemoryCache(e
) && // in good shape and
969 // the local memory cache is not overflowing
970 (mem_node::InUseCount() <= store_pages_max
);
973 // An idle, unlocked entry that only belongs to a SwapDir which controls
974 // its own index, should not stay in the global store_table.
975 if (!dereference(e
, keepInLocalMemory
)) {
976 debugs(20, 5, HERE
<< "destroying unlocked entry: " << &e
<< ' ' << e
);
977 destroyStoreEntry(static_cast<hash_link
*>(&e
));
981 debugs(20, 5, HERE
<< "keepInLocalMemory: " << keepInLocalMemory
);
983 // TODO: move this into [non-shared] memory cache class when we have one
984 if (keepInLocalMemory
) {
985 e
.setMemStatus(IN_MEMORY
);
986 e
.mem_obj
->unlinkRequest();
988 e
.purgeMem(); // may free e
993 StoreController::allowCollapsing(StoreEntry
*e
, const RequestFlags
&reqFlags
,
994 const HttpRequestMethod
&reqMethod
)
996 e
->makePublic(); // this is needed for both local and SMP collapsing
998 transients
->startWriting(e
, reqFlags
, reqMethod
);
999 debugs(20, 3, "may " << (transients
&& e
->mem_obj
->xitTable
.index
>= 0 ?
1000 "SMP-" : "locally-") << "collapse " << *e
);
1004 StoreController::syncCollapsed(const sfileno xitIndex
)
1008 StoreEntry
*collapsed
= transients
->findCollapsed(xitIndex
);
1009 if (!collapsed
) { // the entry is no longer locally active, ignore update
1010 debugs(20, 7, "not SMP-syncing not-transient " << xitIndex
);
1013 assert(collapsed
->mem_obj
);
1014 assert(collapsed
->mem_obj
->smpCollapsed
);
1016 debugs(20, 7, "syncing " << *collapsed
);
1018 bool abandoned
= transients
->abandoned(*collapsed
);
1020 bool inSync
= false;
1021 if (memStore
&& collapsed
->mem_obj
->memCache
.io
== MemObject::ioDone
) {
1024 debugs(20, 7, "fully mem-loaded " << *collapsed
);
1025 } else if (memStore
&& collapsed
->mem_obj
->memCache
.index
>= 0) {
1027 inSync
= memStore
->updateCollapsed(*collapsed
);
1028 } else if (collapsed
->swap_filen
>= 0) {
1030 inSync
= collapsed
->store()->updateCollapsed(*collapsed
);
1032 found
= anchorCollapsed(*collapsed
, inSync
);
1035 if (abandoned
&& collapsed
->store_status
== STORE_PENDING
) {
1036 debugs(20, 3, "aborting abandoned but STORE_PENDING " << *collapsed
);
1042 debugs(20, 5, "synced " << *collapsed
);
1043 collapsed
->invokeHandlers();
1044 } else if (found
) { // unrecoverable problem syncing this entry
1045 debugs(20, 3, "aborting unsyncable " << *collapsed
);
1047 } else { // the entry is still not in one of the caches
1048 debugs(20, 7, "waiting " << *collapsed
);
1052 /// Called for in-transit entries that are not yet anchored to a cache.
1053 /// For cached entries, return true after synchronizing them with their cache
1054 /// (making inSync true on success). For not-yet-cached entries, return false.
1056 StoreController::anchorCollapsed(StoreEntry
&collapsed
, bool &inSync
)
1058 // this method is designed to work with collapsed transients only
1059 assert(collapsed
.mem_obj
);
1060 assert(collapsed
.mem_obj
->xitTable
.index
>= 0);
1061 assert(collapsed
.mem_obj
->smpCollapsed
);
1063 debugs(20, 7, "anchoring " << collapsed
);
1067 found
= memStore
->anchorCollapsed(collapsed
, inSync
);
1068 else if (Config
.cacheSwap
.n_configured
)
1069 found
= anchorCollapsedOnDisk(collapsed
, inSync
);
1073 debugs(20, 7, "anchored " << collapsed
);
1075 debugs(20, 5, "failed to anchor " << collapsed
);
1077 debugs(20, 7, "skipping not yet cached " << collapsed
);
1083 StoreHashIndex::StoreHashIndex()
1087 assert (store_table
== NULL
);
1090 StoreHashIndex::~StoreHashIndex()
1093 hashFreeItems(store_table
, destroyStoreEntry
);
1094 hashFreeMemory(store_table
);
1100 StoreHashIndex::callback()
1104 static int ndir
= 0;
1109 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1110 if (ndir
>= Config
.cacheSwap
.n_configured
)
1111 ndir
= ndir
% Config
.cacheSwap
.n_configured
;
1113 int temp_result
= store(ndir
)->callback();
1119 result
+= temp_result
;
1122 fatal ("too much io\n");
1132 StoreHashIndex::create()
1134 if (Config
.cacheSwap
.n_configured
== 0) {
1135 debugs(0, DBG_PARSE_NOTE(DBG_CRITICAL
), "No cache_dir stores are configured.");
1138 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1139 if (dir(i
).active())
1144 /* Lookup an object in the cache.
1145 * return just a reference to object, don't start swapping in yet. */
1147 StoreHashIndex::get(const cache_key
*key
)
1149 PROF_start(storeGet
);
1150 debugs(20, 3, "storeGet: looking up " << storeKeyText(key
));
1151 StoreEntry
*p
= static_cast<StoreEntry
*>(hash_lookup(store_table
, key
));
1152 PROF_stop(storeGet
);
1157 StoreHashIndex::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
1159 fatal("not implemented");
1163 StoreHashIndex::init()
1165 if (Config
.Store
.objectsPerBucket
<= 0)
1166 fatal("'store_objects_per_bucket' should be larger than 0.");
1168 if (Config
.Store
.avgObjectSize
<= 0)
1169 fatal("'store_avg_object_size' should be larger than 0.");
1171 /* Calculate size of hash table (maximum currently 64k buckets). */
1172 /* this is very bogus, its specific to the any Store maintaining an
1173 * in-core index, not global */
1174 size_t buckets
= (Store::Root().maxSize() + Config
.memMaxSize
) / Config
.Store
.avgObjectSize
;
1175 debugs(20, DBG_IMPORTANT
, "Swap maxSize " << (Store::Root().maxSize() >> 10) <<
1176 " + " << ( Config
.memMaxSize
>> 10) << " KB, estimated " << buckets
<< " objects");
1177 buckets
/= Config
.Store
.objectsPerBucket
;
1178 debugs(20, DBG_IMPORTANT
, "Target number of buckets: " << buckets
);
1179 /* ideally the full scan period should be configurable, for the
1180 * moment it remains at approximately 24 hours. */
1181 store_hash_buckets
= storeKeyHashBuckets(buckets
);
1182 debugs(20, DBG_IMPORTANT
, "Using " << store_hash_buckets
<< " Store buckets");
1183 debugs(20, DBG_IMPORTANT
, "Max Mem size: " << ( Config
.memMaxSize
>> 10) << " KB" <<
1184 (Config
.memShared
? " [shared]" : ""));
1185 debugs(20, DBG_IMPORTANT
, "Max Swap size: " << (Store::Root().maxSize() >> 10) << " KB");
1187 store_table
= hash_create(storeKeyHashCmp
,
1188 store_hash_buckets
, storeKeyHashHash
);
1190 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1191 /* this starts a search of the store dirs, loading their
1192 * index. under the new Store api this should be
1193 * driven by the StoreHashIndex, not by each store.
1195 * That is, the HashIndex should perform a search of each dir it is
1196 * indexing to do the hash insertions. The search is then able to
1197 * decide 'from-memory', or 'from-clean-log' or 'from-dirty-log' or
1200 * Step 1: make the store rebuilds use a search internally
1201 * Step 2: change the search logic to use the four modes described
1203 * Step 3: have the hash index walk the searches itself.
1205 if (dir(i
).active())
1211 StoreHashIndex::maxSize() const
1213 uint64_t result
= 0;
1215 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1216 if (dir(i
).doReportStat())
1217 result
+= store(i
)->maxSize();
1224 StoreHashIndex::minSize() const
1226 uint64_t result
= 0;
1228 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1229 if (dir(i
).doReportStat())
1230 result
+= store(i
)->minSize();
1237 StoreHashIndex::currentSize() const
1239 uint64_t result
= 0;
1241 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1242 if (dir(i
).doReportStat())
1243 result
+= store(i
)->currentSize();
1250 StoreHashIndex::currentCount() const
1252 uint64_t result
= 0;
1254 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1255 if (dir(i
).doReportStat())
1256 result
+= store(i
)->currentCount();
1263 StoreHashIndex::maxObjectSize() const
1265 int64_t result
= -1;
1267 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1268 if (dir(i
).active() && store(i
)->maxObjectSize() > result
)
1269 result
= store(i
)->maxObjectSize();
1276 StoreHashIndex::getStats(StoreInfoStats
&stats
) const
1278 // accumulate per-disk cache stats
1279 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1280 StoreInfoStats dirStats
;
1281 store(i
)->getStats(dirStats
);
1285 // common to all disks
1286 stats
.swap
.open_disk_fd
= store_open_disk_fd
;
1288 // memory cache stats are collected in StoreController::getStats(), for now
1292 StoreHashIndex::stat(StoreEntry
& output
) const
1296 /* Now go through each store, calling its stat routine */
1298 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1299 storeAppendPrintf(&output
, "\n");
1300 store(i
)->stat(output
);
1305 StoreHashIndex::reference(StoreEntry
&e
)
1307 e
.store()->reference(e
);
1311 StoreHashIndex::dereference(StoreEntry
&e
, bool wantsLocalMemory
)
1313 return e
.store()->dereference(e
, wantsLocalMemory
);
1317 StoreHashIndex::maintain()
1322 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1323 /* XXX FixMe: This should be done "in parallell" on the different
1324 * cache_dirs, not one at a time.
1326 /* call the maintain function .. */
1327 store(i
)->maintain();
1332 StoreHashIndex::sync()
1334 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
)
1339 StoreHashIndex::search(String
const url
, HttpRequest
*)
1342 fatal ("Cannot search by url yet\n");
1344 return new StoreSearchHashIndex (this);
1347 CBDATA_CLASS_INIT(StoreSearchHashIndex
);
1349 StoreSearchHashIndex::StoreSearchHashIndex(RefCount
<StoreHashIndex
> aSwapDir
) :
1358 StoreSearchHashIndex::StoreSearchHashIndex(StoreSearchHashIndex const &);
1361 StoreSearchHashIndex::~StoreSearchHashIndex()
1365 StoreSearchHashIndex::next(void (aCallback
)(void *), void *aCallbackData
)
1368 aCallback (aCallbackData
);
1372 StoreSearchHashIndex::next()
1377 while (!isDone() && !entries
.size())
1380 return currentItem() != NULL
;
1384 StoreSearchHashIndex::error() const
1390 StoreSearchHashIndex::isDone() const
1392 return bucket
>= store_hash_buckets
|| _done
;
1396 StoreSearchHashIndex::currentItem()
1398 if (!entries
.size())
1401 return entries
.back();
1405 StoreSearchHashIndex::copyBucket()
1407 /* probably need to lock the store entries...
1408 * we copy them all to prevent races on the links. */
1409 debugs(47, 3, "StoreSearchHashIndex::copyBucket #" << bucket
);
1410 assert (!entries
.size());
1411 hash_link
*link_ptr
= NULL
;
1412 hash_link
*link_next
= NULL
;
1413 link_next
= hash_get_bucket(store_table
, bucket
);
1415 while (NULL
!= (link_ptr
= link_next
)) {
1416 link_next
= link_ptr
->next
;
1417 StoreEntry
*e
= (StoreEntry
*) link_ptr
;
1419 entries
.push_back(e
);
1423 debugs(47,3, "got entries: " << entries
.size());