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"
47 #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 maxobjsize to unlimited (-1) maxobjsize.
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
= swapDir
->get(key
)) {
758 // TODO: ignore and maybe handleIdleEntry() unlocked intransit entries
759 // because their backing store slot may be gone already.
760 debugs(20, 3, HERE
<< "got in-transit entry: " << *e
);
765 if (StoreEntry
*e
= memStore
->get(key
)) {
766 debugs(20, 3, HERE
<< "got mem-cached entry: " << *e
);
771 // TODO: this disk iteration is misplaced; move to StoreHashIndex when
772 // the global store_table is no longer used for in-transit objects.
773 if (const int cacheDirs
= Config
.cacheSwap
.n_configured
) {
774 // ask each cache_dir until the entry is found; use static starting
775 // point to avoid asking the same subset of disks more often
776 // TODO: coordinate with put() to be able to guess the right disk often
778 for (int n
= 0; n
< cacheDirs
; ++n
) {
779 idx
= (idx
+ 1) % cacheDirs
;
780 SwapDir
*sd
= dynamic_cast<SwapDir
*>(INDEXSD(idx
));
784 if (StoreEntry
*e
= sd
->get(key
)) {
785 debugs(20, 3, HERE
<< "cache_dir " << idx
<<
786 " got cached entry: " << *e
);
792 debugs(20, 4, HERE
<< "none of " << Config
.cacheSwap
.n_configured
<<
793 " cache_dirs have " << storeKeyText(key
));
795 // Last, check shared in-transit table if enabled.
796 // We speculate that collapsed forwarding hits are less frequent than
797 // proper cache hits checked above (the order does not matter for misses).
799 if (StoreEntry
*e
= transients
->get(key
)) {
800 debugs(20, 3, "got shared in-transit entry: " << *e
);
809 StoreController::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
811 fatal("not implemented");
814 // move this into [non-shared] memory cache class when we have one
815 /// whether e should be kept in local RAM for possible future caching
817 StoreController::keepForLocalMemoryCache(const StoreEntry
&e
) const
819 if (!e
.memoryCachable())
822 // does the current and expected size obey memory caching limits?
824 const int64_t loadedSize
= e
.mem_obj
->endOffset();
825 const int64_t expectedSize
= e
.mem_obj
->expectedReplySize(); // may be < 0
826 const int64_t ramSize
= max(loadedSize
, expectedSize
);
827 const int64_t ramLimit
= min(
828 static_cast<int64_t>(Config
.memMaxSize
),
829 static_cast<int64_t>(Config
.Store
.maxInMemObjSize
));
830 return ramSize
<= ramLimit
;
834 StoreController::maybeTrimMemory(StoreEntry
&e
, const bool preserveSwappable
)
836 bool keepInLocalMemory
= false;
838 keepInLocalMemory
= memStore
->keepInLocalMemory(e
);
840 keepInLocalMemory
= keepForLocalMemoryCache(e
);
842 debugs(20, 7, HERE
<< "keepInLocalMemory: " << keepInLocalMemory
);
844 if (!keepInLocalMemory
)
845 e
.trimMemory(preserveSwappable
);
849 StoreController::handleIdleEntry(StoreEntry
&e
)
851 bool keepInLocalMemory
= false;
853 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) {
854 // Icons (and cache digests?) should stay in store_table until we
855 // have a dedicated storage for them (that would not purge them).
856 // They are not managed [well] by any specific Store handled below.
857 keepInLocalMemory
= true;
858 } else if (memStore
) {
859 memStore
->considerKeeping(e
);
860 // leave keepInLocalMemory false; memStore maintains its own cache
862 keepInLocalMemory
= keepForLocalMemoryCache(e
) && // in good shape and
863 // the local memory cache is not overflowing
864 (mem_node::InUseCount() <= store_pages_max
);
867 // An idle, unlocked entry that only belongs to a SwapDir which controls
868 // its own index, should not stay in the global store_table.
869 if (!dereference(e
, keepInLocalMemory
)) {
870 debugs(20, 5, HERE
<< "destroying unlocked entry: " << &e
<< ' ' << e
);
871 destroyStoreEntry(static_cast<hash_link
*>(&e
));
875 debugs(20, 5, HERE
<< "keepInLocalMemory: " << keepInLocalMemory
);
877 // TODO: move this into [non-shared] memory cache class when we have one
878 if (keepInLocalMemory
) {
879 e
.setMemStatus(IN_MEMORY
);
880 e
.mem_obj
->unlinkRequest();
882 e
.purgeMem(); // may free e
887 StoreController::allowCollapsing(StoreEntry
*e
, const RequestFlags
&reqFlags
,
888 const HttpRequestMethod
&reqMethod
)
890 e
->makePublic(); // this is needed for both local and SMP collapsing
892 transients
->put(e
, reqFlags
, reqMethod
);
893 debugs(20, 3, "may " << (transients
? "SMP" : "") << " collapse " << *e
);
897 StoreHashIndex::StoreHashIndex()
901 assert (store_table
== NULL
);
904 StoreHashIndex::~StoreHashIndex()
907 hashFreeItems(store_table
, destroyStoreEntry
);
908 hashFreeMemory(store_table
);
914 StoreHashIndex::callback()
923 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
924 if (ndir
>= Config
.cacheSwap
.n_configured
)
925 ndir
= ndir
% Config
.cacheSwap
.n_configured
;
927 int temp_result
= store(ndir
)->callback();
933 result
+= temp_result
;
936 fatal ("too much io\n");
946 StoreHashIndex::create()
948 if (Config
.cacheSwap
.n_configured
== 0) {
949 debugs(0, DBG_PARSE_NOTE(DBG_CRITICAL
), "No cache_dir stores are configured.");
952 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
958 /* Lookup an object in the cache.
959 * return just a reference to object, don't start swapping in yet. */
961 StoreHashIndex::get(const cache_key
*key
)
963 PROF_start(storeGet
);
964 debugs(20, 3, "storeGet: looking up " << storeKeyText(key
));
965 StoreEntry
*p
= static_cast<StoreEntry
*>(hash_lookup(store_table
, key
));
971 StoreHashIndex::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
973 fatal("not implemented");
977 StoreHashIndex::init()
979 if (Config
.Store
.objectsPerBucket
<= 0)
980 fatal("'store_objects_per_bucket' should be larger than 0.");
982 if (Config
.Store
.avgObjectSize
<= 0)
983 fatal("'store_avg_object_size' should be larger than 0.");
985 /* Calculate size of hash table (maximum currently 64k buckets). */
986 /* this is very bogus, its specific to the any Store maintaining an
987 * in-core index, not global */
988 size_t buckets
= (Store::Root().maxSize() + Config
.memMaxSize
) / Config
.Store
.avgObjectSize
;
989 debugs(20, DBG_IMPORTANT
, "Swap maxSize " << (Store::Root().maxSize() >> 10) <<
990 " + " << ( Config
.memMaxSize
>> 10) << " KB, estimated " << buckets
<< " objects");
991 buckets
/= Config
.Store
.objectsPerBucket
;
992 debugs(20, DBG_IMPORTANT
, "Target number of buckets: " << buckets
);
993 /* ideally the full scan period should be configurable, for the
994 * moment it remains at approximately 24 hours. */
995 store_hash_buckets
= storeKeyHashBuckets(buckets
);
996 debugs(20, DBG_IMPORTANT
, "Using " << store_hash_buckets
<< " Store buckets");
997 debugs(20, DBG_IMPORTANT
, "Max Mem size: " << ( Config
.memMaxSize
>> 10) << " KB" <<
998 (Config
.memShared
? " [shared]" : ""));
999 debugs(20, DBG_IMPORTANT
, "Max Swap size: " << (Store::Root().maxSize() >> 10) << " KB");
1001 store_table
= hash_create(storeKeyHashCmp
,
1002 store_hash_buckets
, storeKeyHashHash
);
1004 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1005 /* this starts a search of the store dirs, loading their
1006 * index. under the new Store api this should be
1007 * driven by the StoreHashIndex, not by each store.
1009 * That is, the HashIndex should perform a search of each dir it is
1010 * indexing to do the hash insertions. The search is then able to
1011 * decide 'from-memory', or 'from-clean-log' or 'from-dirty-log' or
1014 * Step 1: make the store rebuilds use a search internally
1015 * Step 2: change the search logic to use the four modes described
1017 * Step 3: have the hash index walk the searches itself.
1019 if (dir(i
).active())
1025 StoreHashIndex::maxSize() const
1027 uint64_t result
= 0;
1029 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1030 if (dir(i
).doReportStat())
1031 result
+= store(i
)->maxSize();
1038 StoreHashIndex::minSize() const
1040 uint64_t result
= 0;
1042 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1043 if (dir(i
).doReportStat())
1044 result
+= store(i
)->minSize();
1051 StoreHashIndex::currentSize() const
1053 uint64_t result
= 0;
1055 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1056 if (dir(i
).doReportStat())
1057 result
+= store(i
)->currentSize();
1064 StoreHashIndex::currentCount() const
1066 uint64_t result
= 0;
1068 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1069 if (dir(i
).doReportStat())
1070 result
+= store(i
)->currentCount();
1077 StoreHashIndex::maxObjectSize() const
1079 int64_t result
= -1;
1081 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1082 if (dir(i
).active() && store(i
)->maxObjectSize() > result
)
1083 result
= store(i
)->maxObjectSize();
1090 StoreHashIndex::getStats(StoreInfoStats
&stats
) const
1092 // accumulate per-disk cache stats
1093 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1094 StoreInfoStats dirStats
;
1095 store(i
)->getStats(dirStats
);
1099 // common to all disks
1100 stats
.swap
.open_disk_fd
= store_open_disk_fd
;
1102 // memory cache stats are collected in StoreController::getStats(), for now
1106 StoreHashIndex::stat(StoreEntry
& output
) const
1110 /* Now go through each store, calling its stat routine */
1112 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1113 storeAppendPrintf(&output
, "\n");
1114 store(i
)->stat(output
);
1119 StoreHashIndex::reference(StoreEntry
&e
)
1121 e
.store()->reference(e
);
1125 StoreHashIndex::dereference(StoreEntry
&e
, bool wantsLocalMemory
)
1127 return e
.store()->dereference(e
, wantsLocalMemory
);
1131 StoreHashIndex::maintain()
1136 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1137 /* XXX FixMe: This should be done "in parallell" on the different
1138 * cache_dirs, not one at a time.
1140 /* call the maintain function .. */
1141 store(i
)->maintain();
1146 StoreHashIndex::sync()
1148 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
)
1153 StoreHashIndex::search(String
const url
, HttpRequest
*)
1156 fatal ("Cannot search by url yet\n");
1158 return new StoreSearchHashIndex (this);
1161 CBDATA_CLASS_INIT(StoreSearchHashIndex
);
1163 StoreSearchHashIndex::StoreSearchHashIndex(RefCount
<StoreHashIndex
> aSwapDir
) :
1172 StoreSearchHashIndex::StoreSearchHashIndex(StoreSearchHashIndex const &);
1175 StoreSearchHashIndex::~StoreSearchHashIndex()
1179 StoreSearchHashIndex::next(void (aCallback
)(void *), void *aCallbackData
)
1182 aCallback (aCallbackData
);
1186 StoreSearchHashIndex::next()
1191 while (!isDone() && !entries
.size())
1194 return currentItem() != NULL
;
1198 StoreSearchHashIndex::error() const
1204 StoreSearchHashIndex::isDone() const
1206 return bucket
>= store_hash_buckets
|| _done
;
1210 StoreSearchHashIndex::currentItem()
1212 if (!entries
.size())
1215 return entries
.back();
1219 StoreSearchHashIndex::copyBucket()
1221 /* probably need to lock the store entries...
1222 * we copy them all to prevent races on the links. */
1223 debugs(47, 3, "StoreSearchHashIndex::copyBucket #" << bucket
);
1224 assert (!entries
.size());
1225 hash_link
*link_ptr
= NULL
;
1226 hash_link
*link_next
= NULL
;
1227 link_next
= hash_get_bucket(store_table
, bucket
);
1229 while (NULL
!= (link_ptr
= link_next
)) {
1230 link_next
= link_ptr
->next
;
1231 StoreEntry
*e
= (StoreEntry
*) link_ptr
;
1233 entries
.push_back(e
);
1237 debugs(47,3, "got entries: " << entries
.size());