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.
38 #include "MemObject.h"
39 #include "SquidMath.h"
40 #include "SquidTime.h"
42 #include "swap_log_op.h"
45 #if HAVE_SYS_STATVFS_H
46 #include <sys/statvfs.h>
48 #endif /* HAVE_STATVFS */
49 /* statfs() needs <sys/param.h> and <sys/mount.h> on BSD systems */
51 #include <sys/param.h>
54 #include <sys/mount.h>
56 /* Windows and Linux use sys/vfs.h */
61 #include "StoreHashIndex.h"
63 static STDIRSELECT storeDirSelectSwapDirRoundRobin
;
64 static STDIRSELECT storeDirSelectSwapDirLeastLoad
;
67 * store_dirs_rebuilding is initialized to _1_ as a hack so that
68 * storeDirWriteCleanLogs() doesn't try to do anything unless _all_
69 * cache_dirs have been read. For example, without this hack, Squid
70 * will try to write clean log files if -kparse fails (becasue it
73 int StoreController::store_dirs_rebuilding
= 1;
75 StoreController::StoreController() : swapDir (new StoreHashIndex())
78 StoreController::~StoreController()
82 * This function pointer is set according to 'store_dir_select_algorithm'
85 STDIRSELECT
*storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
88 StoreController::init()
92 if (0 == strcasecmp(Config
.store_dir_select_algorithm
, "round-robin")) {
93 storeDirSelectSwapDir
= storeDirSelectSwapDirRoundRobin
;
94 debugs(47, 1, "Using Round Robin store dir selection");
96 storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
97 debugs(47, 1, "Using Least Load store dir selection");
102 StoreController::createOneStore(Store
&aStore
)
105 * On Windows, fork() is not available.
106 * The following is a workaround for create store directories sequentially
107 * when running on native Windows port.
126 StoreController::create()
138 pid
= wait3(&status
, WNOHANG
, NULL
);
141 pid
= waitpid(-1, &status
, 0);
144 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
150 * Determine whether the given directory can handle this object
153 * Note: if the object size is -1, then the only swapdirs that
154 * will return true here are ones that have min and max unset,
155 * ie any-sized-object swapdirs. This is a good thing.
158 SwapDir::objectSizeIsAcceptable(int64_t objsize
) const
160 // If the swapdir has no range limits, then it definitely can
161 if (min_objsize
<= 0 && max_objsize
== -1)
165 * If the object size is -1 and the storedir has limits we
166 * can't store it there.
171 // Else, make sure that the object size will fit.
172 if (max_objsize
== -1 && min_objsize
<= objsize
)
175 return min_objsize
<= objsize
&& max_objsize
> objsize
;
180 * This new selection scheme simply does round-robin on all SwapDirs.
181 * A SwapDir is skipped if it is over the max_size (100%) limit, or
185 storeDirSelectSwapDirRoundRobin(const StoreEntry
* e
)
190 RefCount
<SwapDir
> sd
;
192 ssize_t objsize
= e
->objectLen();
194 objsize
+= e
->mem_obj
->swap_hdr_sz
;
196 for (i
= 0; i
<= Config
.cacheSwap
.n_configured
; i
++) {
197 if (++dirn
>= Config
.cacheSwap
.n_configured
)
200 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
202 if (sd
->flags
.read_only
)
205 if (sd
->cur_size
> sd
->max_size
)
208 if (!sd
->objectSizeIsAcceptable(objsize
))
211 /* check for error or overload condition */
212 load
= sd
->canStore(*e
);
214 if (load
< 0 || load
> 1000) {
225 * Spread load across all of the store directories
227 * Note: We should modify this later on to prefer sticking objects
228 * in the *tightest fit* swapdir to conserve space, along with the
229 * actual swapdir usage. But for now, this hack will do while
230 * testing, so you should order your swapdirs in the config file
231 * from smallest maxobjsize to unlimited (-1) maxobjsize.
233 * We also have to choose nleast == nconf since we need to consider
234 * ALL swapdirs, regardless of state. Again, this is a hack while
235 * we sort out the real usefulness of this algorithm.
238 storeDirSelectSwapDirLeastLoad(const StoreEntry
* e
)
241 ssize_t most_free
= 0, cur_free
;
242 ssize_t least_objsize
= -1;
243 int least_load
= INT_MAX
;
247 RefCount
<SwapDir
> SD
;
249 /* Calculate the object size */
250 objsize
= e
->objectLen();
253 objsize
+= e
->mem_obj
->swap_hdr_sz
;
255 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
256 SD
= dynamic_cast<SwapDir
*>(INDEXSD(i
));
257 SD
->flags
.selected
= 0;
258 load
= SD
->canStore(*e
);
260 if (load
< 0 || load
> 1000) {
264 if (!SD
->objectSizeIsAcceptable(objsize
))
267 if (SD
->flags
.read_only
)
270 if (SD
->cur_size
> SD
->max_size
)
273 if (load
> least_load
)
276 cur_free
= SD
->max_size
- SD
->cur_size
;
278 /* If the load is equal, then look in more details */
279 if (load
== least_load
) {
280 /* closest max_objsize fit */
282 if (least_objsize
!= -1)
283 if (SD
->max_objsize
> least_objsize
|| SD
->max_objsize
== -1)
287 if (cur_free
< most_free
)
292 least_objsize
= SD
->max_objsize
;
293 most_free
= cur_free
;
298 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->flags
.selected
= 1;
304 * An entry written to the swap log MUST have the following
306 * 1. It MUST be a public key. It does no good to log
307 * a public ADD, change the key, then log a private
308 * DEL. So we need to log a DEL before we change a
309 * key from public to private.
310 * 2. It MUST have a valid (> -1) swap_filen.
313 storeDirSwapLog(const StoreEntry
* e
, int op
)
316 assert(!EBIT_TEST(e
->flags
, KEY_PRIVATE
));
317 assert(e
->swap_filen
>= 0);
319 * icons and such; don't write them to the swap log
322 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
325 assert(op
> SWAP_LOG_NOP
&& op
< SWAP_LOG_MAX
);
327 debugs(20, 3, "storeDirSwapLog: " <<
328 swap_log_op_str
[op
] << " " <<
329 e
->getMD5Text() << " " <<
330 e
->swap_dirn
<< " " <<
331 std::hex
<< std::uppercase
<< std::setfill('0') << std::setw(8) << e
->swap_filen
);
333 dynamic_cast<SwapDir
*>(INDEXSD(e
->swap_dirn
))->logEntry(*e
, op
);
337 StoreController::updateSize(int64_t size
, int sign
)
339 fatal("StoreController has no independent size\n");
343 SwapDir::updateSize(int64_t size
, int sign
)
345 int64_t blks
= (size
+ fs
.blksize
- 1) / fs
.blksize
;
346 int64_t k
= ((blks
* fs
.blksize
) >> 10) * sign
;
348 store_swap_size
+= k
;
357 StoreController::stat(StoreEntry
&output
) const
359 storeAppendPrintf(&output
, "Store Directory Statistics:\n");
360 storeAppendPrintf(&output
, "Store Entries : %lu\n",
361 (unsigned long int)StoreEntry::inUseCount());
362 storeAppendPrintf(&output
, "Maximum Swap Size : %"PRIu64
" KB\n",
364 storeAppendPrintf(&output
, "Current Store Swap Size: %8lu KB\n",
366 storeAppendPrintf(&output
, "Current Capacity : %"PRId64
"%% used, %"PRId64
"%% free\n",
367 Math::int64Percent(store_swap_size
, maxSize()),
368 Math::int64Percent((maxSize() - store_swap_size
), maxSize()));
369 /* FIXME Here we should output memory statistics */
371 /* now the swapDir */
372 swapDir
->stat(output
);
375 /* if needed, this could be taught to cache the result */
377 StoreController::maxSize() const
379 /* TODO: include memory cache ? */
380 return swapDir
->maxSize();
384 StoreController::minSize() const
386 /* TODO: include memory cache ? */
387 return swapDir
->minSize();
393 if (cur_size
>= max_size
)
398 debugs(20, 1, "WARNING: Shrinking cache_dir #" << index
<< " to " << cur_size
<< " KB");
402 storeDirOpenSwapLogs(void)
404 for (int dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
405 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->openLog();
409 storeDirCloseSwapLogs(void)
411 for (int dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; ++dirn
)
412 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->closeLog();
416 * storeDirWriteCleanLogs
418 * Writes a "clean" swap log file from in-memory metadata.
419 * This is a rewrite of the original function to troll each
420 * StoreDir and write the logs, and flush at the end of
421 * the run. Thanks goes to Eric Stern, since this solution
422 * came out of his COSS code.
425 storeDirWriteCleanLogs(int reopen
)
427 const StoreEntry
*e
= NULL
;
430 struct timeval start
;
432 RefCount
<SwapDir
> sd
;
436 if (StoreController::store_dirs_rebuilding
) {
437 debugs(20, 1, "Not currently OK to rewrite swap log.");
438 debugs(20, 1, "storeDirWriteCleanLogs: Operation aborted.");
442 debugs(20, 1, "storeDirWriteCleanLogs: Starting...");
444 start
= current_time
;
446 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
447 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
449 if (sd
->writeCleanStart() < 0) {
450 debugs(20, 1, "log.clean.start() failed for dir #" << sd
->index
);
456 * This may look inefficient as CPU wise it is more efficient to do this
457 * sequentially, but I/O wise the parallellism helps as it allows more
458 * hdd spindles to be active.
463 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
464 sd
= dynamic_cast<SwapDir
*>(INDEXSD(dirn
));
466 if (NULL
== sd
->cleanLog
)
469 e
= sd
->cleanLog
->nextEntry();
479 sd
->cleanLog
->write(*e
);
481 if ((++n
& 0xFFFF) == 0) {
483 debugs(20, 1, " " << std::setw(7) << n
<<
484 " entries written so far.");
490 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++)
491 dynamic_cast<SwapDir
*>(INDEXSD(dirn
))->writeCleanDone();
494 storeDirOpenSwapLogs();
498 dt
= tvSubDsec(start
, current_time
);
500 debugs(20, 1, " Finished. Wrote " << n
<< " entries.");
501 debugs(20, 1, " Took "<< std::setw(3)<< std::setprecision(2) << dt
<<
502 " seconds ("<< std::setw(6) << ((double) n
/ (dt
> 0.0 ? dt
: 1.0)) << " entries/sec).");
509 StoreController::search(String
const url
, HttpRequest
*request
)
511 /* cheat, for now you can't search the memory hot cache */
512 return swapDir
->search(url
, request
);
516 StoreHashIndex::store(int const x
) const
522 StoreController::sync(void)
524 /* sync mem cache? */
529 * handle callbacks all avaliable fs'es
532 StoreController::callback()
534 /* This will likely double count. Thats ok. */
535 PROF_start(storeDirCallback
);
537 /* mem cache callbacks ? */
538 int result
= swapDir
->callback();
540 PROF_stop(storeDirCallback
);
546 storeDirGetBlkSize(const char *path
, int *blksize
)
552 if (statvfs(path
, &sfs
)) {
553 debugs(50, 1, "" << path
<< ": " << xstrerror());
558 *blksize
= (int) sfs
.f_frsize
;
563 if (statfs(path
, &sfs
)) {
564 debugs(50, 1, "" << path
<< ": " << xstrerror());
569 *blksize
= (int) sfs
.f_bsize
;
572 * Sanity check; make sure we have a meaningful value.
581 #define fsbtoblk(num, fsbs, bs) \
582 (((fsbs) != 0 && (fsbs) < (bs)) ? \
583 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
585 storeDirGetUFSStats(const char *path
, int *totl_kb
, int *free_kb
, int *totl_in
, int *free_in
)
591 if (statvfs(path
, &sfs
)) {
592 debugs(50, 1, "" << path
<< ": " << xstrerror());
596 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_frsize
, 1024);
597 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_frsize
, 1024);
598 *totl_in
= (int) sfs
.f_files
;
599 *free_in
= (int) sfs
.f_ffree
;
604 if (statfs(path
, &sfs
)) {
605 debugs(50, 1, "" << path
<< ": " << xstrerror());
609 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_bsize
, 1024);
610 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_bsize
, 1024);
611 *totl_in
= (int) sfs
.f_files
;
612 *free_in
= (int) sfs
.f_ffree
;
619 allocate_new_swapdir(SquidConfig::_cacheSwap
* swap
)
621 if (swap
->swapDirs
== NULL
) {
622 swap
->n_allocated
= 4;
623 swap
->swapDirs
= static_cast<StorePointer
*>(xcalloc(swap
->n_allocated
, sizeof(StorePointer
)));
626 if (swap
->n_allocated
== swap
->n_configured
) {
628 swap
->n_allocated
<<= 1;
629 tmp
= static_cast<StorePointer
*>(xcalloc(swap
->n_allocated
, sizeof(StorePointer
)));
630 memcpy(tmp
, swap
->swapDirs
, swap
->n_configured
* sizeof(SwapDir
*));
631 xfree(swap
->swapDirs
);
632 swap
->swapDirs
= tmp
;
637 free_cachedir(SquidConfig::_cacheSwap
* swap
)
640 /* DON'T FREE THESE FOR RECONFIGURE */
645 for (i
= 0; i
< swap
->n_configured
; i
++) {
646 /* TODO XXX this lets the swapdir free resources asynchronously
647 * swap->swapDirs[i]->deactivate();
648 * but there may be such a means already.
651 swap
->swapDirs
[i
] = NULL
;
654 safe_free(swap
->swapDirs
);
655 swap
->swapDirs
= NULL
;
656 swap
->n_allocated
= 0;
657 swap
->n_configured
= 0;
660 /* this should be a virtual method on StoreEntry,
661 * i.e. e->referenced()
662 * so that the entry can notify the creating Store
665 StoreController::reference(StoreEntry
&e
)
667 /* Notify the fs that we're referencing this object again */
669 if (e
.swap_dirn
> -1)
670 e
.store()->reference(e
);
672 /* Notify the memory cache that we're referencing this object again */
674 if (mem_policy
->Referenced
)
675 mem_policy
->Referenced(mem_policy
, &e
, &e
.mem_obj
->repl
);
680 StoreController::dereference(StoreEntry
& e
)
682 /* Notify the fs that we're not referencing this object any more */
684 if (e
.swap_filen
> -1)
685 e
.store()->dereference(e
);
687 /* Notify the memory cache that we're not referencing this object any more */
689 if (mem_policy
->Dereferenced
)
690 mem_policy
->Dereferenced(mem_policy
, &e
, &e
.mem_obj
->repl
);
695 StoreController::get(const cache_key
*key
)
698 return swapDir
->get(key
);
702 StoreController::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
704 fatal("not implemented");
707 StoreHashIndex::StoreHashIndex()
711 assert (store_table
== NULL
);
714 StoreHashIndex::~StoreHashIndex()
717 hashFreeItems(store_table
, destroyStoreEntry
);
718 hashFreeMemory(store_table
);
724 StoreHashIndex::callback()
733 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
734 if (ndir
>= Config
.cacheSwap
.n_configured
)
735 ndir
= ndir
% Config
.cacheSwap
.n_configured
;
737 int temp_result
= store(ndir
)->callback();
743 result
+= temp_result
;
746 fatal ("too much io\n");
756 StoreHashIndex::create()
758 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++)
762 /* Lookup an object in the cache.
763 * return just a reference to object, don't start swapping in yet. */
765 StoreHashIndex::get(const cache_key
*key
)
767 PROF_start(storeGet
);
768 debugs(20, 3, "storeGet: looking up " << storeKeyText(key
));
769 StoreEntry
*p
= static_cast<StoreEntry
*>(hash_lookup(store_table
, key
));
775 StoreHashIndex::get(String
const key
, STOREGETCLIENT aCallback
, void *aCallbackData
)
777 fatal("not implemented");
781 StoreHashIndex::init()
783 /* Calculate size of hash table (maximum currently 64k buckets). */
784 /* this is very bogus, its specific to the any Store maintaining an
785 * in-core index, not global */
786 size_t buckets
= (Store::Root().maxSize() + ( Config
.memMaxSize
>> 10)) / Config
.Store
.avgObjectSize
;
787 debugs(20, 1, "Swap maxSize " << Store::Root().maxSize() <<
788 " + " << ( Config
.memMaxSize
>> 10) << " KB, estimated " << buckets
<< " objects");
789 buckets
/= Config
.Store
.objectsPerBucket
;
790 debugs(20, 1, "Target number of buckets: " << buckets
);
791 /* ideally the full scan period should be configurable, for the
792 * moment it remains at approximately 24 hours. */
793 store_hash_buckets
= storeKeyHashBuckets(buckets
);
794 debugs(20, 1, "Using " << store_hash_buckets
<< " Store buckets");
795 debugs(20, 1, "Max Mem size: " << ( Config
.memMaxSize
>> 10) << " KB");
796 debugs(20, 1, "Max Swap size: " << Store::Root().maxSize() << " KB");
798 store_table
= hash_create(storeKeyHashCmp
,
799 store_hash_buckets
, storeKeyHashHash
);
801 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
802 /* this starts a search of the store dirs, loading their
803 * index. under the new Store api this should be
804 * driven by the StoreHashIndex, not by each store.
806 * That is, the HashIndex should perform a search of each dir it is
807 * indexing to do the hash insertions. The search is then able to
808 * decide 'from-memory', or 'from-clean-log' or 'from-dirty-log' or
811 * Step 1: make the store rebuilds use a search internally
812 * Step 2: change the search logic to use the four modes described
814 * Step 3: have the hash index walk the searches itself.
821 StoreHashIndex::maxSize() const
825 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++)
826 result
+= store(i
)->maxSize();
832 StoreHashIndex::minSize() const
836 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++)
837 result
+= store(i
)->minSize();
843 StoreHashIndex::stat(StoreEntry
& output
) const
847 /* Now go through each store, calling its stat routine */
849 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
850 storeAppendPrintf(&output
, "\n");
851 store(i
)->stat(output
);
856 StoreHashIndex::reference(StoreEntry
&)
860 StoreHashIndex::dereference(StoreEntry
&)
864 StoreHashIndex::maintain()
869 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
870 /* XXX FixMe: This should be done "in parallell" on the different
871 * cache_dirs, not one at a time.
873 /* call the maintain function .. */
874 store(i
)->maintain();
879 StoreHashIndex::updateSize(int64_t, int)
883 StoreHashIndex::sync()
885 for (int i
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
)
890 StoreHashIndex::search(String
const url
, HttpRequest
*)
893 fatal ("Cannot search by url yet\n");
895 return new StoreSearchHashIndex (this);
898 CBDATA_CLASS_INIT(StoreSearchHashIndex
);
900 StoreSearchHashIndex::StoreSearchHashIndex(RefCount
<StoreHashIndex
> aSwapDir
) : sd(aSwapDir
), _done (false), bucket (0)
904 StoreSearchHashIndex::StoreSearchHashIndex(StoreSearchHashIndex const &);
907 StoreSearchHashIndex::~StoreSearchHashIndex()
911 StoreSearchHashIndex::next(void (aCallback
)(void *), void *aCallbackData
)
914 aCallback (aCallbackData
);
918 StoreSearchHashIndex::next()
923 while (!isDone() && !entries
.size())
926 return currentItem() != NULL
;
930 StoreSearchHashIndex::error() const
936 StoreSearchHashIndex::isDone() const
938 return bucket
>= store_hash_buckets
|| _done
;
942 StoreSearchHashIndex::currentItem()
947 return entries
.back();
951 StoreSearchHashIndex::copyBucket()
953 /* probably need to lock the store entries...
954 * we copy them all to prevent races on the links. */
955 debugs(47, 3, "StoreSearchHashIndex::copyBucket #" << bucket
);
956 assert (!entries
.size());
957 hash_link
*link_ptr
= NULL
;
958 hash_link
*link_next
= NULL
;
959 link_next
= hash_get_bucket(store_table
, bucket
);
961 while (NULL
!= (link_ptr
= link_next
)) {
962 link_next
= link_ptr
->next
;
963 StoreEntry
*e
= (StoreEntry
*) link_ptr
;
965 entries
.push_back(e
);
969 debugs(47,3, "got entries: " << entries
.size());