5 * DEBUG: section 47 Store Directory Routines
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "squid-old.h"
38 #include "MemObject.h"
41 #include "SquidMath.h"
42 #include "SquidTime.h"
44 #include "swap_log_op.h"
47 #if HAVE_SYS_STATVFS_H
48 #include <sys/statvfs.h>
50 #endif /* HAVE_STATVFS */
51 /* statfs() needs <sys/param.h> and <sys/mount.h> on BSD systems */
53 #include <sys/param.h>
56 #include <sys/mount.h>
58 /* Windows and Linux use sys/vfs.h */
63 #include "StoreHashIndex.h"
65 static STDIRSELECT storeDirSelectSwapDirRoundRobin
;
66 static STDIRSELECT storeDirSelectSwapDirLeastLoad
;
69 * store_dirs_rebuilding is initialized to _1_ as a hack so that
70 * storeDirWriteCleanLogs() doesn't try to do anything unless _all_
71 * cache_dirs have been read. For example, without this hack, Squid
72 * will try to write clean log files if -kparse fails (becasue it
75 int StoreController::store_dirs_rebuilding
= 1;
77 StoreController::StoreController() : swapDir (new StoreHashIndex())
81 StoreController::~StoreController()
87 * This function pointer is set according to 'store_dir_select_algorithm'
90 STDIRSELECT
*storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
93 StoreController::init()
95 if (Config
.memShared
&& IamWorkerProcess()) {
96 memStore
= new MemStore
;
102 if (0 == strcasecmp(Config
.store_dir_select_algorithm
, "round-robin")) {
103 storeDirSelectSwapDir
= storeDirSelectSwapDirRoundRobin
;
104 debugs(47, DBG_IMPORTANT
, "Using Round Robin store dir selection");
106 storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
107 debugs(47, DBG_IMPORTANT
, "Using Least Load store dir selection");
112 StoreController::createOneStore(Store
&aStore
)
115 * On Windows, fork() is not available.
116 * The following is a workaround for create store directories sequentially
117 * when running on native Windows port.
136 StoreController::create()
148 pid
= wait3(&status
, WNOHANG
, NULL
);
151 pid
= waitpid(-1, &status
, 0);
154 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
160 * Determine whether the given directory can handle this object
163 * Note: if the object size is -1, then the only swapdirs that
164 * will return true here are ones that have min and max unset,
165 * ie any-sized-object swapdirs. This is a good thing.
168 SwapDir::objectSizeIsAcceptable(int64_t objsize
) const
170 // If the swapdir has no range limits, then it definitely can
171 if (min_objsize
<= 0 && max_objsize
== -1)
175 * If the object size is -1 and the storedir has limits we
176 * can't store it there.
181 // Else, make sure that the object size will fit.
182 if (max_objsize
== -1 && min_objsize
<= objsize
)
185 return min_objsize
<= objsize
&& max_objsize
> objsize
;
190 * This new selection scheme simply does round-robin on all SwapDirs.
191 * A SwapDir is skipped if it is over the max_size (100%) limit, or
195 storeDirSelectSwapDirRoundRobin(const StoreEntry
* e
)
200 RefCount
<SwapDir
> sd
;
202 // e->objectLen() is negative at this point when we are still STORE_PENDING
203 ssize_t objsize
= e
->mem_obj
->expectedReplySize();
205 objsize
+= e
->mem_obj
->swap_hdr_sz
;
207 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
208 if (++dirn
>= Config
.cacheSwap
.n_configured
)
211 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
213 if (!sd
->canStore(*e
, objsize
, load
))
216 if (load
< 0 || load
> 1000) {
227 * Spread load across all of the store directories
229 * Note: We should modify this later on to prefer sticking objects
230 * in the *tightest fit* swapdir to conserve space, along with the
231 * actual swapdir usage. But for now, this hack will do while
232 * testing, so you should order your swapdirs in the config file
233 * from smallest maxobjsize to unlimited (-1) maxobjsize.
235 * We also have to choose nleast == nconf since we need to consider
236 * ALL swapdirs, regardless of state. Again, this is a hack while
237 * we sort out the real usefulness of this algorithm.
240 storeDirSelectSwapDirLeastLoad(const StoreEntry
* e
)
242 int64_t most_free
= 0;
243 ssize_t least_objsize
= -1;
244 int least_load
= INT_MAX
;
248 RefCount
<SwapDir
> SD
;
250 // e->objectLen() is negative at this point when we are still STORE_PENDING
251 ssize_t objsize
= e
->mem_obj
->expectedReplySize();
254 objsize
+= e
->mem_obj
->swap_hdr_sz
;
256 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
257 SD
= dynamic_cast<SwapDir
*>(INDEXSD(i
));
258 SD
->flags
.selected
= 0;
260 if (!SD
->canStore(*e
, objsize
, load
))
263 if (load
< 0 || load
> 1000)
266 if (load
> least_load
)
269 const int64_t cur_free
= SD
->maxSize() - SD
->currentSize();
271 /* If the load is equal, then look in more details */
272 if (load
== least_load
) {
273 /* closest max_objsize fit */
275 if (least_objsize
!= -1)
276 if (SD
->max_objsize
> least_objsize
|| SD
->max_objsize
== -1)
280 if (cur_free
< most_free
)
285 least_objsize
= SD
->max_objsize
;
286 most_free
= cur_free
;
291 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->flags
.selected
= 1;
297 * An entry written to the swap log MUST have the following
299 * 1. It MUST be a public key. It does no good to log
300 * a public ADD, change the key, then log a private
301 * DEL. So we need to log a DEL before we change a
302 * key from public to private.
303 * 2. It MUST have a valid (> -1) swap_filen.
306 storeDirSwapLog(const StoreEntry
* e
, int op
)
309 assert(!EBIT_TEST(e
->flags
, KEY_PRIVATE
));
310 assert(e
->swap_filen
>= 0);
312 * icons and such; don't write them to the swap log
315 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
318 assert(op
> SWAP_LOG_NOP
&& op
< SWAP_LOG_MAX
);
320 debugs(20, 3, "storeDirSwapLog: " <<
321 swap_log_op_str
[op
] << " " <<
322 e
->getMD5Text() << " " <<
323 e
->swap_dirn
<< " " <<
324 std::hex
<< std::uppercase
<< std::setfill('0') << std::setw(8) << e
->swap_filen
);
326 dynamic_cast<SwapDir
*>(INDEXSD(e
->swap_dirn
))->logEntry(*e
, op
);
330 StoreController::getStats(StoreInfoStats
&stats
) const
333 memStore
->getStats(stats
);
335 // move this code to a non-shared memory cache class when we have it
336 stats
.mem
.shared
= false;
337 stats
.mem
.capacity
= Config
.memMaxSize
;
338 stats
.mem
.size
= mem_node::StoreMemSize();
339 stats
.mem
.count
= hot_obj_count
;
342 swapDir
->getStats(stats
);
344 // low-level info not specific to memory or disk cache
345 stats
.store_entry_count
= StoreEntry::inUseCount();
346 stats
.mem_object_count
= MemObject::inUseCount();
350 StoreController::stat(StoreEntry
&output
) const
352 storeAppendPrintf(&output
, "Store Directory Statistics:\n");
353 storeAppendPrintf(&output
, "Store Entries : %lu\n",
354 (unsigned long int)StoreEntry::inUseCount());
355 storeAppendPrintf(&output
, "Maximum Swap Size : %" PRIu64
" KB\n",
357 storeAppendPrintf(&output
, "Current Store Swap Size: %.2f KB\n",
358 currentSize() / 1024.0);
359 storeAppendPrintf(&output
, "Current Capacity : %.2f%% used, %.2f%% free\n",
360 Math::doublePercent(currentSize(), maxSize()),
361 Math::doublePercent((maxSize() - currentSize()), maxSize()));
364 memStore
->stat(output
);
366 /* now the swapDir */
367 swapDir
->stat(output
);
370 /* if needed, this could be taught to cache the result */
372 StoreController::maxSize() const
374 /* TODO: include memory cache ? */
375 return swapDir
->maxSize();
379 StoreController::minSize() const
381 /* TODO: include memory cache ? */
382 return swapDir
->minSize();
386 StoreController::currentSize() const
388 return swapDir
->currentSize();
392 StoreController::currentCount() const
394 return swapDir
->currentCount();
398 StoreController::maxObjectSize() const
400 return swapDir
->maxObjectSize();
406 if (currentSize() >= maxSize())
409 max_size
= currentSize();
411 debugs(20, DBG_IMPORTANT
, "WARNING: Shrinking cache_dir #" << index
<< " to " << currentSize() / 1024.0 << " KB");
415 storeDirOpenSwapLogs(void)
417 for (int dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
418 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->openLog();
422 storeDirCloseSwapLogs(void)
424 for (int dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
425 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->closeLog();
429 * storeDirWriteCleanLogs
431 * Writes a "clean" swap log file from in-memory metadata.
432 * This is a rewrite of the original function to troll each
433 * StoreDir and write the logs, and flush at the end of
434 * the run. Thanks goes to Eric Stern, since this solution
435 * came out of his COSS code.
438 storeDirWriteCleanLogs(int reopen
)
440 const StoreEntry
*e
= NULL
;
443 struct timeval start
;
445 RefCount
<SwapDir
> sd
;
449 if (StoreController::store_dirs_rebuilding
) {
450 debugs(20, DBG_IMPORTANT
, "Not currently OK to rewrite swap log.");
451 debugs(20, DBG_IMPORTANT
, "storeDirWriteCleanLogs: Operation aborted.");
455 debugs(20, DBG_IMPORTANT
, "storeDirWriteCleanLogs: Starting...");
457 start
= current_time
;
459 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
) {
460 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
462 if (sd
->writeCleanStart() < 0) {
463 debugs(20, DBG_IMPORTANT
, "log.clean.start() failed for dir #" << sd
->index
);
469 * This may look inefficient as CPU wise it is more efficient to do this
470 * sequentially, but I/O wise the parallellism helps as it allows more
471 * hdd spindles to be active.
476 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
) {
477 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
479 if (NULL
== sd
->cleanLog
)
482 e
= sd
->cleanLog
->nextEntry();
492 sd
->cleanLog
->write(*e
);
494 if ((++n
& 0xFFFF) == 0) {
496 debugs(20, DBG_IMPORTANT
, " " << std::setw(7) << n
<<
497 " entries written so far.");
503 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
504 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->writeCleanDone();
507 storeDirOpenSwapLogs();
511 dt
= tvSubDsec(start
, current_time
);
513 debugs(20, DBG_IMPORTANT
, " Finished. Wrote " << n
<< " entries.");
514 debugs(20, DBG_IMPORTANT
, " Took "<< std::setw(3)<< std::setprecision(2) << dt
<<
515 " seconds ("<< std::setw(6) << ((double) n
/ (dt
> 0.0 ? dt
: 1.0)) << " entries/sec).");
522 StoreController::search(String
const url
, HttpRequest
*request
)
524 /* cheat, for now you can't search the memory hot cache */
525 return swapDir
->search(url
, request
);
529 StoreHashIndex::store(int const x
) const
535 StoreHashIndex::dir(const int i
) const
537 SwapDir
*sd
= dynamic_cast<SwapDir
*>(INDEXSD(i
));
543 StoreController::sync(void)
551 * handle callbacks all avaliable fs'es
554 StoreController::callback()
556 /* This will likely double count. Thats ok. */
557 PROF_start(storeDirCallback
);
559 /* mem cache callbacks ? */
560 int result
= swapDir
->callback();
562 PROF_stop(storeDirCallback
);
568 storeDirGetBlkSize(const char *path
, int *blksize
)
574 if (statvfs(path
, &sfs
)) {
575 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
580 *blksize
= (int) sfs
.f_frsize
;
585 if (statfs(path
, &sfs
)) {
586 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
591 *blksize
= (int) sfs
.f_bsize
;
594 * Sanity check; make sure we have a meaningful value.
603 #define fsbtoblk(num, fsbs, bs) \
604 (((fsbs) != 0 && (fsbs) < (bs)) ? \
605 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
607 storeDirGetUFSStats(const char *path
, int *totl_kb
, int *free_kb
, int *totl_in
, int *free_in
)
613 if (statvfs(path
, &sfs
)) {
614 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
618 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_frsize
, 1024);
619 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_frsize
, 1024);
620 *totl_in
= (int) sfs
.f_files
;
621 *free_in
= (int) sfs
.f_ffree
;
626 if (statfs(path
, &sfs
)) {
627 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
631 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_bsize
, 1024);
632 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_bsize
, 1024);
633 *totl_in
= (int) sfs
.f_files
;
634 *free_in
= (int) sfs
.f_ffree
;
641 allocate_new_swapdir(SquidConfig::_cacheSwap
* swap
)
643 if (swap
->swapDirs
== NULL
) {
644 swap
->n_allocated
= 4;
645 swap
->swapDirs
= static_cast<SwapDir::Pointer
*>(xcalloc(swap
->n_allocated
, sizeof(SwapDir::Pointer
)));
648 if (swap
->n_allocated
== swap
->n_configured
) {
649 swap
->n_allocated
<<= 1;
650 SwapDir::Pointer
*const tmp
= static_cast<SwapDir::Pointer
*>(xcalloc(swap
->n_allocated
, sizeof(SwapDir::Pointer
)));
651 memcpy(tmp
, swap
->swapDirs
, swap
->n_configured
* sizeof(SwapDir
*));
652 xfree(swap
->swapDirs
);
653 swap
->swapDirs
= tmp
;
658 free_cachedir(SquidConfig::_cacheSwap
* swap
)
661 /* DON'T FREE THESE FOR RECONFIGURE */
666 for (i
= 0; i
< swap
->n_configured
; ++i
) {
667 /* TODO XXX this lets the swapdir free resources asynchronously
668 * swap->swapDirs[i]->deactivate();
669 * but there may be such a means already.
672 swap
->swapDirs
[i
] = NULL
;
675 safe_free(swap
->swapDirs
);
676 swap
->swapDirs
= NULL
;
677 swap
->n_allocated
= 0;
678 swap
->n_configured
= 0;
681 /* this should be a virtual method on StoreEntry,
682 * i.e. e->referenced()
683 * so that the entry can notify the creating Store
686 StoreController::reference(StoreEntry
&e
)
688 // special entries do not belong to any specific Store, but are IN_MEMORY
689 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
))
692 /* Notify the fs that we're referencing this object again */
694 if (e
.swap_dirn
> -1)
695 swapDir
->reference(e
);
697 // Notify the memory cache that we're referencing this object again
698 if (memStore
&& e
.mem_status
== IN_MEMORY
)
699 memStore
->reference(e
);
701 // TODO: move this code to a non-shared memory cache class when we have it
703 if (mem_policy
->Referenced
)
704 mem_policy
->Referenced(mem_policy
, &e
, &e
.mem_obj
->repl
);
709 StoreController::dereference(StoreEntry
& e
)
711 bool keepInStoreTable
= true; // keep if there are no objections
713 // special entries do not belong to any specific Store, but are IN_MEMORY
714 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
))
715 return keepInStoreTable
;
717 /* Notify the fs that we're not referencing this object any more */
719 if (e
.swap_filen
> -1)
720 keepInStoreTable
= swapDir
->dereference(e
) && keepInStoreTable
;
722 // Notify the memory cache that we're not referencing this object any more
723 if (memStore
&& e
.mem_status
== IN_MEMORY
)
724 keepInStoreTable
= memStore
->dereference(e
) && keepInStoreTable
;
726 // TODO: move this code to a non-shared memory cache class when we have it
728 if (mem_policy
->Dereferenced
)
729 mem_policy
->Dereferenced(mem_policy
, &e
, &e
.mem_obj
->repl
);
732 return keepInStoreTable
;
736 StoreController::get(const cache_key
*key
)
738 if (StoreEntry
*e
= swapDir
->get(key
)) {
739 // TODO: ignore and maybe handleIdleEntry() unlocked intransit entries
740 // because their backing store slot may be gone already.
741 debugs(20, 3, HERE
<< "got in-transit entry: " << *e
);
746 if (StoreEntry
*e
= memStore
->get(key
)) {
747 debugs(20, 3, HERE
<< "got mem-cached entry: " << *e
);
752 // TODO: this disk iteration is misplaced; move to StoreHashIndex when
753 // the global store_table is no longer used for in-transit objects.
754 if (const int cacheDirs
= Config
.cacheSwap
.n_configured
) {
755 // ask each cache_dir until the entry is found; use static starting
756 // point to avoid asking the same subset of disks more often
757 // TODO: coordinate with put() to be able to guess the right disk often
759 for (int n
= 0; n
< cacheDirs
; ++n
) {
760 idx
= (idx
+ 1) % cacheDirs
;
761 SwapDir
*sd
= dynamic_cast<SwapDir
*>(INDEXSD(idx
));
765 if (StoreEntry
*e
= sd
->get(key
)) {
766 debugs(20, 3, HERE
<< "cache_dir " << idx
<<
767 " got cached entry: " << *e
);
773 debugs(20, 4, HERE
<< "none of " << Config
.cacheSwap
.n_configured
<<
774 " cache_dirs have " << storeKeyText(key
));
779 StoreController::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
781 fatal("not implemented");
784 // move this into [non-shared] memory cache class when we have one
785 /// whether e should be kept in local RAM for possible future caching
787 StoreController::keepForLocalMemoryCache(const StoreEntry
&e
) const
789 if (!e
.memoryCachable())
792 // does the current and expected size obey memory caching limits?
794 const int64_t loadedSize
= e
.mem_obj
->endOffset();
795 const int64_t expectedSize
= e
.mem_obj
->expectedReplySize(); // may be < 0
796 const int64_t ramSize
= max(loadedSize
, expectedSize
);
797 const int64_t ramLimit
= min(
798 static_cast<int64_t>(Config
.memMaxSize
),
799 static_cast<int64_t>(Config
.Store
.maxInMemObjSize
));
800 return ramSize
<= ramLimit
;
804 StoreController::maybeTrimMemory(StoreEntry
&e
, const bool preserveSwappable
)
806 bool keepInLocalMemory
= false;
808 keepInLocalMemory
= memStore
->keepInLocalMemory(e
);
810 keepInLocalMemory
= keepForLocalMemoryCache(e
);
812 debugs(20, 7, HERE
<< "keepInLocalMemory: " << keepInLocalMemory
);
814 if (!keepInLocalMemory
)
815 e
.trimMemory(preserveSwappable
);
819 StoreController::handleIdleEntry(StoreEntry
&e
)
821 bool keepInLocalMemory
= false;
823 if (EBIT_TEST(e
.flags
, ENTRY_SPECIAL
)) {
824 // Icons (and cache digests?) should stay in store_table until we
825 // have a dedicated storage for them (that would not purge them).
826 // They are not managed [well] by any specific Store handled below.
827 keepInLocalMemory
= true;
828 } else if (memStore
) {
829 memStore
->considerKeeping(e
);
830 // leave keepInLocalMemory false; memStore maintains its own cache
832 keepInLocalMemory
= keepForLocalMemoryCache(e
) && // in good shape and
833 // the local memory cache is not overflowing
834 (mem_node::InUseCount() <= store_pages_max
);
837 // An idle, unlocked entry that belongs to a SwapDir which controls
838 // its own index, should not stay in the global store_table.
839 if (!dereference(e
)) {
840 debugs(20, 5, HERE
<< "destroying unlocked entry: " << &e
<< ' ' << e
);
841 destroyStoreEntry(static_cast<hash_link
*>(&e
));
845 debugs(20, 5, HERE
<< "keepInLocalMemory: " << keepInLocalMemory
);
847 // TODO: move this into [non-shared] memory cache class when we have one
848 if (keepInLocalMemory
) {
849 e
.setMemStatus(IN_MEMORY
);
850 e
.mem_obj
->unlinkRequest();
852 e
.purgeMem(); // may free e
856 StoreHashIndex::StoreHashIndex()
860 assert (store_table
== NULL
);
863 StoreHashIndex::~StoreHashIndex()
866 hashFreeItems(store_table
, destroyStoreEntry
);
867 hashFreeMemory(store_table
);
873 StoreHashIndex::callback()
882 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
883 if (ndir
>= Config
.cacheSwap
.n_configured
)
884 ndir
= ndir
% Config
.cacheSwap
.n_configured
;
886 int temp_result
= store(ndir
)->callback();
892 result
+= temp_result
;
895 fatal ("too much io\n");
905 StoreHashIndex::create()
907 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
913 /* Lookup an object in the cache.
914 * return just a reference to object, don't start swapping in yet. */
916 StoreHashIndex::get(const cache_key
*key
)
918 PROF_start(storeGet
);
919 debugs(20, 3, "storeGet: looking up " << storeKeyText(key
));
920 StoreEntry
*p
= static_cast<StoreEntry
*>(hash_lookup(store_table
, key
));
926 StoreHashIndex::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
928 fatal("not implemented");
932 StoreHashIndex::init()
934 /* Calculate size of hash table (maximum currently 64k buckets). */
935 /* this is very bogus, its specific to the any Store maintaining an
936 * in-core index, not global */
937 size_t buckets
= (Store::Root().maxSize() + Config
.memMaxSize
) / Config
.Store
.avgObjectSize
;
938 debugs(20, DBG_IMPORTANT
, "Swap maxSize " << (Store::Root().maxSize() >> 10) <<
939 " + " << ( Config
.memMaxSize
>> 10) << " KB, estimated " << buckets
<< " objects");
940 buckets
/= Config
.Store
.objectsPerBucket
;
941 debugs(20, DBG_IMPORTANT
, "Target number of buckets: " << buckets
);
942 /* ideally the full scan period should be configurable, for the
943 * moment it remains at approximately 24 hours. */
944 store_hash_buckets
= storeKeyHashBuckets(buckets
);
945 debugs(20, DBG_IMPORTANT
, "Using " << store_hash_buckets
<< " Store buckets");
946 debugs(20, DBG_IMPORTANT
, "Max Mem size: " << ( Config
.memMaxSize
>> 10) << " KB" <<
947 (Config
.memShared
? " [shared]" : ""));
948 debugs(20, DBG_IMPORTANT
, "Max Swap size: " << (Store::Root().maxSize() >> 10) << " KB");
950 store_table
= hash_create(storeKeyHashCmp
,
951 store_hash_buckets
, storeKeyHashHash
);
953 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
954 /* this starts a search of the store dirs, loading their
955 * index. under the new Store api this should be
956 * driven by the StoreHashIndex, not by each store.
958 * That is, the HashIndex should perform a search of each dir it is
959 * indexing to do the hash insertions. The search is then able to
960 * decide 'from-memory', or 'from-clean-log' or 'from-dirty-log' or
963 * Step 1: make the store rebuilds use a search internally
964 * Step 2: change the search logic to use the four modes described
966 * Step 3: have the hash index walk the searches itself.
974 StoreHashIndex::maxSize() const
978 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
979 if (dir(i
).doReportStat())
980 result
+= store(i
)->maxSize();
987 StoreHashIndex::minSize() const
991 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
992 if (dir(i
).doReportStat())
993 result
+= store(i
)->minSize();
1000 StoreHashIndex::currentSize() const
1002 uint64_t result
= 0;
1004 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1005 if (dir(i
).doReportStat())
1006 result
+= store(i
)->currentSize();
1013 StoreHashIndex::currentCount() const
1015 uint64_t result
= 0;
1017 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1018 if (dir(i
).doReportStat())
1019 result
+= store(i
)->currentCount();
1026 StoreHashIndex::maxObjectSize() const
1028 int64_t result
= -1;
1030 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1031 if (dir(i
).active() && store(i
)->maxObjectSize() > result
)
1032 result
= store(i
)->maxObjectSize();
1039 StoreHashIndex::getStats(StoreInfoStats
&stats
) const
1041 // accumulate per-disk cache stats
1042 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1043 StoreInfoStats dirStats
;
1044 store(i
)->getStats(dirStats
);
1048 // common to all disks
1049 stats
.swap
.open_disk_fd
= store_open_disk_fd
;
1051 // memory cache stats are collected in StoreController::getStats(), for now
1055 StoreHashIndex::stat(StoreEntry
& output
) const
1059 /* Now go through each store, calling its stat routine */
1061 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1062 storeAppendPrintf(&output
, "\n");
1063 store(i
)->stat(output
);
1068 StoreHashIndex::reference(StoreEntry
&e
)
1070 e
.store()->reference(e
);
1074 StoreHashIndex::dereference(StoreEntry
&e
)
1076 return e
.store()->dereference(e
);
1080 StoreHashIndex::maintain()
1085 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1086 /* XXX FixMe: This should be done "in parallell" on the different
1087 * cache_dirs, not one at a time.
1089 /* call the maintain function .. */
1090 store(i
)->maintain();
1095 StoreHashIndex::sync()
1097 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
)
1102 StoreHashIndex::search(String
const url
, HttpRequest
*)
1105 fatal ("Cannot search by url yet\n");
1107 return new StoreSearchHashIndex (this);
1110 CBDATA_CLASS_INIT(StoreSearchHashIndex
);
1112 StoreSearchHashIndex::StoreSearchHashIndex(RefCount
<StoreHashIndex
> aSwapDir
) : sd(aSwapDir
), _done (false), bucket (0)
1116 StoreSearchHashIndex::StoreSearchHashIndex(StoreSearchHashIndex const &);
1119 StoreSearchHashIndex::~StoreSearchHashIndex()
1123 StoreSearchHashIndex::next(void (aCallback
)(void *), void *aCallbackData
)
1126 aCallback (aCallbackData
);
1130 StoreSearchHashIndex::next()
1135 while (!isDone() && !entries
.size())
1138 return currentItem() != NULL
;
1142 StoreSearchHashIndex::error() const
1148 StoreSearchHashIndex::isDone() const
1150 return bucket
>= store_hash_buckets
|| _done
;
1154 StoreSearchHashIndex::currentItem()
1156 if (!entries
.size())
1159 return entries
.back();
1163 StoreSearchHashIndex::copyBucket()
1165 /* probably need to lock the store entries...
1166 * we copy them all to prevent races on the links. */
1167 debugs(47, 3, "StoreSearchHashIndex::copyBucket #" << bucket
);
1168 assert (!entries
.size());
1169 hash_link
*link_ptr
= NULL
;
1170 hash_link
*link_next
= NULL
;
1171 link_next
= hash_get_bucket(store_table
, bucket
);
1173 while (NULL
!= (link_ptr
= link_next
)) {
1174 link_next
= link_ptr
->next
;
1175 StoreEntry
*e
= (StoreEntry
*) link_ptr
;
1177 entries
.push_back(e
);
1181 debugs(47,3, "got entries: " << entries
.size());