]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_dir.cc
Merge 3p2-rock.
[thirdparty/squid.git] / src / store_dir.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 47 Store Directory Routines
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
19 *
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.
24 *
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.
29 *
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.
33 *
34 */
35
36 #include "squid.h"
37 #include "Store.h"
38 #include "MemObject.h"
39 #include "SquidMath.h"
40 #include "SquidTime.h"
41 #include "SwapDir.h"
42 #include "swap_log_op.h"
43
44 #if HAVE_STATVFS
45 #if HAVE_SYS_STATVFS_H
46 #include <sys/statvfs.h>
47 #endif
48 #endif /* HAVE_STATVFS */
49 /* statfs() needs <sys/param.h> and <sys/mount.h> on BSD systems */
50 #if HAVE_SYS_PARAM_H
51 #include <sys/param.h>
52 #endif
53 #if HAVE_SYS_MOUNT_H
54 #include <sys/mount.h>
55 #endif
56 /* Windows and Linux use sys/vfs.h */
57 #if HAVE_SYS_VFS_H
58 #include <sys/vfs.h>
59 #endif
60
61 #include "StoreHashIndex.h"
62
63 static STDIRSELECT storeDirSelectSwapDirRoundRobin;
64 static STDIRSELECT storeDirSelectSwapDirLeastLoad;
65
66 /*
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
71 * calls fatal()).
72 */
73 int StoreController::store_dirs_rebuilding = 1;
74
75 StoreController::StoreController() : swapDir (new StoreHashIndex())
76 {}
77
78 StoreController::~StoreController()
79 {}
80
81 /*
82 * This function pointer is set according to 'store_dir_select_algorithm'
83 * in squid.conf.
84 */
85 STDIRSELECT *storeDirSelectSwapDir = storeDirSelectSwapDirLeastLoad;
86
87 void
88 StoreController::init()
89 {
90 swapDir->init();
91
92 if (0 == strcasecmp(Config.store_dir_select_algorithm, "round-robin")) {
93 storeDirSelectSwapDir = storeDirSelectSwapDirRoundRobin;
94 debugs(47, 1, "Using Round Robin store dir selection");
95 } else {
96 storeDirSelectSwapDir = storeDirSelectSwapDirLeastLoad;
97 debugs(47, 1, "Using Least Load store dir selection");
98 }
99 }
100
101 void
102 StoreController::createOneStore(Store &aStore)
103 {
104 /*
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.
108 */
109 #ifndef _SQUID_MSWIN_
110
111 if (fork())
112 return;
113
114 #endif
115
116 aStore.create();
117
118 #ifndef _SQUID_MSWIN_
119
120 exit(0);
121
122 #endif
123 }
124
125 void
126 StoreController::create()
127 {
128 swapDir->create();
129
130 #ifndef _SQUID_MSWIN_
131
132 pid_t pid;
133
134 do {
135 int status;
136 #ifdef _SQUID_NEXT_
137
138 pid = wait3(&status, WNOHANG, NULL);
139 #else
140
141 pid = waitpid(-1, &status, 0);
142 #endif
143
144 } while (pid > 0 || (pid < 0 && errno == EINTR));
145
146 #endif
147 }
148
149 /**
150 * Determine whether the given directory can handle this object
151 * size
152 *
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.
156 */
157 bool
158 SwapDir::objectSizeIsAcceptable(int64_t objsize) const
159 {
160 // If the swapdir has no range limits, then it definitely can
161 if (min_objsize <= 0 && max_objsize == -1)
162 return true;
163
164 /*
165 * If the object size is -1 and the storedir has limits we
166 * can't store it there.
167 */
168 if (objsize == -1)
169 return false;
170
171 // Else, make sure that the object size will fit.
172 return min_objsize <= objsize && max_objsize > objsize;
173 }
174
175
176 /*
177 * This new selection scheme simply does round-robin on all SwapDirs.
178 * A SwapDir is skipped if it is over the max_size (100%) limit, or
179 * overloaded.
180 */
181 static int
182 storeDirSelectSwapDirRoundRobin(const StoreEntry * e)
183 {
184 static int dirn = 0;
185 int i;
186 int load;
187 RefCount<SwapDir> sd;
188
189 ssize_t objsize = e->objectLen();
190 if (objsize != -1)
191 objsize += e->mem_obj->swap_hdr_sz;
192
193 for (i = 0; i <= Config.cacheSwap.n_configured; i++) {
194 if (++dirn >= Config.cacheSwap.n_configured)
195 dirn = 0;
196
197 sd = dynamic_cast<SwapDir *>(INDEXSD(dirn));
198
199 if (sd->flags.read_only)
200 continue;
201
202 if (sd->cur_size > sd->max_size)
203 continue;
204
205 if (!sd->objectSizeIsAcceptable(objsize))
206 continue;
207
208 /* check for error or overload condition */
209 load = sd->canStore(*e);
210
211 if (load < 0 || load > 1000) {
212 continue;
213 }
214
215 return dirn;
216 }
217
218 return -1;
219 }
220
221 /*
222 * Spread load across all of the store directories
223 *
224 * Note: We should modify this later on to prefer sticking objects
225 * in the *tightest fit* swapdir to conserve space, along with the
226 * actual swapdir usage. But for now, this hack will do while
227 * testing, so you should order your swapdirs in the config file
228 * from smallest maxobjsize to unlimited (-1) maxobjsize.
229 *
230 * We also have to choose nleast == nconf since we need to consider
231 * ALL swapdirs, regardless of state. Again, this is a hack while
232 * we sort out the real usefulness of this algorithm.
233 */
234 static int
235 storeDirSelectSwapDirLeastLoad(const StoreEntry * e)
236 {
237 ssize_t objsize;
238 ssize_t most_free = 0, cur_free;
239 ssize_t least_objsize = -1;
240 int least_load = INT_MAX;
241 int load;
242 int dirn = -1;
243 int i;
244 RefCount<SwapDir> SD;
245
246 /* Calculate the object size */
247 objsize = e->objectLen();
248
249 if (objsize != -1)
250 objsize += e->mem_obj->swap_hdr_sz;
251
252 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
253 SD = dynamic_cast<SwapDir *>(INDEXSD(i));
254 SD->flags.selected = 0;
255 load = SD->canStore(*e);
256
257 if (load < 0 || load > 1000) {
258 continue;
259 }
260
261 if (!SD->objectSizeIsAcceptable(objsize))
262 continue;
263
264 if (SD->flags.read_only)
265 continue;
266
267 if (SD->cur_size > SD->max_size)
268 continue;
269
270 if (load > least_load)
271 continue;
272
273 cur_free = SD->max_size - SD->cur_size;
274
275 /* If the load is equal, then look in more details */
276 if (load == least_load) {
277 /* closest max_objsize fit */
278
279 if (least_objsize != -1)
280 if (SD->max_objsize > least_objsize || SD->max_objsize == -1)
281 continue;
282
283 /* most free */
284 if (cur_free < most_free)
285 continue;
286 }
287
288 least_load = load;
289 least_objsize = SD->max_objsize;
290 most_free = cur_free;
291 dirn = i;
292 }
293
294 if (dirn >= 0)
295 dynamic_cast<SwapDir *>(INDEXSD(dirn))->flags.selected = 1;
296
297 return dirn;
298 }
299
300 /*
301 * An entry written to the swap log MUST have the following
302 * properties.
303 * 1. It MUST be a public key. It does no good to log
304 * a public ADD, change the key, then log a private
305 * DEL. So we need to log a DEL before we change a
306 * key from public to private.
307 * 2. It MUST have a valid (> -1) swap_filen.
308 */
309 void
310 storeDirSwapLog(const StoreEntry * e, int op)
311 {
312 assert (e);
313 assert(!EBIT_TEST(e->flags, KEY_PRIVATE));
314 assert(e->swap_filen >= 0);
315 /*
316 * icons and such; don't write them to the swap log
317 */
318
319 if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
320 return;
321
322 assert(op > SWAP_LOG_NOP && op < SWAP_LOG_MAX);
323
324 debugs(20, 3, "storeDirSwapLog: " <<
325 swap_log_op_str[op] << " " <<
326 e->getMD5Text() << " " <<
327 e->swap_dirn << " " <<
328 std::hex << std::uppercase << std::setfill('0') << std::setw(8) << e->swap_filen);
329
330 dynamic_cast<SwapDir *>(INDEXSD(e->swap_dirn))->logEntry(*e, op);
331 }
332
333 void
334 StoreController::updateSize(int64_t size, int sign)
335 {
336 fatal("StoreController has no independent size\n");
337 }
338
339 void
340 SwapDir::updateSize(int64_t size, int sign)
341 {
342 int64_t blks = (size + fs.blksize - 1) / fs.blksize;
343 int64_t k = ((blks * fs.blksize) >> 10) * sign;
344 cur_size += k;
345 store_swap_size += k;
346
347 if (sign > 0)
348 n_disk_objects++;
349 else if (sign < 0)
350 n_disk_objects--;
351 }
352
353 void
354 StoreController::stat(StoreEntry &output) const
355 {
356 storeAppendPrintf(&output, "Store Directory Statistics:\n");
357 storeAppendPrintf(&output, "Store Entries : %lu\n",
358 (unsigned long int)StoreEntry::inUseCount());
359 storeAppendPrintf(&output, "Maximum Swap Size : %"PRIu64" KB\n",
360 maxSize());
361 storeAppendPrintf(&output, "Current Store Swap Size: %8lu KB\n",
362 store_swap_size);
363 storeAppendPrintf(&output, "Current Capacity : %"PRId64"%% used, %"PRId64"%% free\n",
364 Math::int64Percent(store_swap_size, maxSize()),
365 Math::int64Percent((maxSize() - store_swap_size), maxSize()));
366 /* FIXME Here we should output memory statistics */
367
368 /* now the swapDir */
369 swapDir->stat(output);
370 }
371
372 /* if needed, this could be taught to cache the result */
373 uint64_t
374 StoreController::maxSize() const
375 {
376 /* TODO: include memory cache ? */
377 return swapDir->maxSize();
378 }
379
380 uint64_t
381 StoreController::minSize() const
382 {
383 /* TODO: include memory cache ? */
384 return swapDir->minSize();
385 }
386
387 void
388 SwapDir::diskFull()
389 {
390 if (cur_size >= max_size)
391 return;
392
393 max_size = cur_size;
394
395 debugs(20, 1, "WARNING: Shrinking cache_dir #" << index << " to " << cur_size << " KB");
396 }
397
398 void
399 storeDirOpenSwapLogs(void)
400 {
401 for (int dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn)
402 dynamic_cast<SwapDir *>(INDEXSD(dirn))->openLog();
403 }
404
405 void
406 storeDirCloseSwapLogs(void)
407 {
408 for (int dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn)
409 dynamic_cast<SwapDir *>(INDEXSD(dirn))->closeLog();
410 }
411
412 /*
413 * storeDirWriteCleanLogs
414 *
415 * Writes a "clean" swap log file from in-memory metadata.
416 * This is a rewrite of the original function to troll each
417 * StoreDir and write the logs, and flush at the end of
418 * the run. Thanks goes to Eric Stern, since this solution
419 * came out of his COSS code.
420 */
421 int
422 storeDirWriteCleanLogs(int reopen)
423 {
424 const StoreEntry *e = NULL;
425 int n = 0;
426
427 struct timeval start;
428 double dt;
429 RefCount<SwapDir> sd;
430 int dirn;
431 int notdone = 1;
432
433 if (StoreController::store_dirs_rebuilding) {
434 debugs(20, 1, "Not currently OK to rewrite swap log.");
435 debugs(20, 1, "storeDirWriteCleanLogs: Operation aborted.");
436 return 0;
437 }
438
439 debugs(20, 1, "storeDirWriteCleanLogs: Starting...");
440 getCurrentTime();
441 start = current_time;
442
443 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
444 sd = dynamic_cast<SwapDir *>(INDEXSD(dirn));
445
446 if (sd->writeCleanStart() < 0) {
447 debugs(20, 1, "log.clean.start() failed for dir #" << sd->index);
448 continue;
449 }
450 }
451
452 /*
453 * This may look inefficient as CPU wise it is more efficient to do this
454 * sequentially, but I/O wise the parallellism helps as it allows more
455 * hdd spindles to be active.
456 */
457 while (notdone) {
458 notdone = 0;
459
460 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
461 sd = dynamic_cast<SwapDir *>(INDEXSD(dirn));
462
463 if (NULL == sd->cleanLog)
464 continue;
465
466 e = sd->cleanLog->nextEntry();
467
468 if (!e)
469 continue;
470
471 notdone = 1;
472
473 if (!sd->canLog(*e))
474 continue;
475
476 sd->cleanLog->write(*e);
477
478 if ((++n & 0xFFFF) == 0) {
479 getCurrentTime();
480 debugs(20, 1, " " << std::setw(7) << n <<
481 " entries written so far.");
482 }
483 }
484 }
485
486 /* Flush */
487 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++)
488 dynamic_cast<SwapDir *>(INDEXSD(dirn))->writeCleanDone();
489
490 if (reopen)
491 storeDirOpenSwapLogs();
492
493 getCurrentTime();
494
495 dt = tvSubDsec(start, current_time);
496
497 debugs(20, 1, " Finished. Wrote " << n << " entries.");
498 debugs(20, 1, " Took "<< std::setw(3)<< std::setprecision(2) << dt <<
499 " seconds ("<< std::setw(6) << ((double) n / (dt > 0.0 ? dt : 1.0)) << " entries/sec).");
500
501
502 return n;
503 }
504
505 StoreSearch *
506 StoreController::search(String const url, HttpRequest *request)
507 {
508 /* cheat, for now you can't search the memory hot cache */
509 return swapDir->search(url, request);
510 }
511
512 StorePointer
513 StoreHashIndex::store(int const x) const
514 {
515 return INDEXSD(x);
516 }
517
518 void
519 StoreController::sync(void)
520 {
521 /* sync mem cache? */
522 swapDir->sync();
523 }
524
525 /*
526 * handle callbacks all avaliable fs'es
527 */
528 int
529 StoreController::callback()
530 {
531 /* This will likely double count. Thats ok. */
532 PROF_start(storeDirCallback);
533
534 /* mem cache callbacks ? */
535 int result = swapDir->callback();
536
537 PROF_stop(storeDirCallback);
538
539 return result;
540 }
541
542 int
543 storeDirGetBlkSize(const char *path, int *blksize)
544 {
545 #if HAVE_STATVFS
546
547 struct statvfs sfs;
548
549 if (statvfs(path, &sfs)) {
550 debugs(50, 1, "" << path << ": " << xstrerror());
551 *blksize = 2048;
552 return 1;
553 }
554
555 *blksize = (int) sfs.f_frsize;
556 #else
557
558 struct statfs sfs;
559
560 if (statfs(path, &sfs)) {
561 debugs(50, 1, "" << path << ": " << xstrerror());
562 *blksize = 2048;
563 return 1;
564 }
565
566 *blksize = (int) sfs.f_bsize;
567 #endif
568 /*
569 * Sanity check; make sure we have a meaningful value.
570 */
571
572 if (*blksize < 512)
573 *blksize = 2048;
574
575 return 0;
576 }
577
578 #define fsbtoblk(num, fsbs, bs) \
579 (((fsbs) != 0 && (fsbs) < (bs)) ? \
580 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
581 int
582 storeDirGetUFSStats(const char *path, int *totl_kb, int *free_kb, int *totl_in, int *free_in)
583 {
584 #if HAVE_STATVFS
585
586 struct statvfs sfs;
587
588 if (statvfs(path, &sfs)) {
589 debugs(50, 1, "" << path << ": " << xstrerror());
590 return 1;
591 }
592
593 *totl_kb = (int) fsbtoblk(sfs.f_blocks, sfs.f_frsize, 1024);
594 *free_kb = (int) fsbtoblk(sfs.f_bfree, sfs.f_frsize, 1024);
595 *totl_in = (int) sfs.f_files;
596 *free_in = (int) sfs.f_ffree;
597 #else
598
599 struct statfs sfs;
600
601 if (statfs(path, &sfs)) {
602 debugs(50, 1, "" << path << ": " << xstrerror());
603 return 1;
604 }
605
606 *totl_kb = (int) fsbtoblk(sfs.f_blocks, sfs.f_bsize, 1024);
607 *free_kb = (int) fsbtoblk(sfs.f_bfree, sfs.f_bsize, 1024);
608 *totl_in = (int) sfs.f_files;
609 *free_in = (int) sfs.f_ffree;
610 #endif
611
612 return 0;
613 }
614
615 void
616 allocate_new_swapdir(SquidConfig::_cacheSwap * swap)
617 {
618 if (swap->swapDirs == NULL) {
619 swap->n_allocated = 4;
620 swap->swapDirs = static_cast<StorePointer *>(xcalloc(swap->n_allocated, sizeof(StorePointer)));
621 }
622
623 if (swap->n_allocated == swap->n_configured) {
624 StorePointer *tmp;
625 swap->n_allocated <<= 1;
626 tmp = static_cast<StorePointer *>(xcalloc(swap->n_allocated, sizeof(StorePointer)));
627 memcpy(tmp, swap->swapDirs, swap->n_configured * sizeof(SwapDir *));
628 xfree(swap->swapDirs);
629 swap->swapDirs = tmp;
630 }
631 }
632
633 void
634 free_cachedir(SquidConfig::_cacheSwap * swap)
635 {
636 int i;
637 /* DON'T FREE THESE FOR RECONFIGURE */
638
639 if (reconfiguring)
640 return;
641
642 for (i = 0; i < swap->n_configured; i++) {
643 /* TODO XXX this lets the swapdir free resources asynchronously
644 * swap->swapDirs[i]->deactivate();
645 * but there may be such a means already.
646 * RBC 20041225
647 */
648 swap->swapDirs[i] = NULL;
649 }
650
651 safe_free(swap->swapDirs);
652 swap->swapDirs = NULL;
653 swap->n_allocated = 0;
654 swap->n_configured = 0;
655 }
656
657 /* this should be a virtual method on StoreEntry,
658 * i.e. e->referenced()
659 * so that the entry can notify the creating Store
660 */
661 void
662 StoreController::reference(StoreEntry &e)
663 {
664 /* Notify the fs that we're referencing this object again */
665
666 if (e.swap_dirn > -1)
667 e.store()->reference(e);
668
669 /* Notify the memory cache that we're referencing this object again */
670 if (e.mem_obj) {
671 if (mem_policy->Referenced)
672 mem_policy->Referenced(mem_policy, &e, &e.mem_obj->repl);
673 }
674 }
675
676 void
677 StoreController::dereference(StoreEntry & e)
678 {
679 /* Notify the fs that we're not referencing this object any more */
680
681 if (e.swap_filen > -1)
682 e.store()->dereference(e);
683
684 /* Notify the memory cache that we're not referencing this object any more */
685 if (e.mem_obj) {
686 if (mem_policy->Dereferenced)
687 mem_policy->Dereferenced(mem_policy, &e, &e.mem_obj->repl);
688 }
689 }
690
691 StoreEntry *
692 StoreController::get(const cache_key *key)
693 {
694 if (StoreEntry *e = swapDir->get(key)) {
695 debugs(20, 1, HERE << "got in-transit entry: " << *e);
696 return e;
697 }
698
699 if (const int cacheDirs = Config.cacheSwap.n_configured) {
700 // ask each cache_dir until the entry is found; use static starting
701 // point to avoid asking the same subset of disks more often
702 // TODO: coordinate with put() to be able to guess the right disk often
703 for (int n = 0; n < cacheDirs; ++n) {
704 static int idx = 0;
705 SwapDir *sd = dynamic_cast<SwapDir*>(INDEXSD(idx));
706 if (StoreEntry *e = sd->get(key)) {
707 debugs(20, 1, HERE << "cache_dir " << idx <<
708 " got cached entry: " << *e);
709 return e;
710 }
711 idx = (idx + 1) % cacheDirs;
712 }
713 }
714
715 debugs(20, 1, HERE << "none of " << Config.cacheSwap.n_configured <<
716 " cache_dirs have " << storeKeyText(key));
717 return NULL;
718 }
719
720 void
721 StoreController::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData)
722 {
723 fatal("not implemented");
724 }
725
726 StoreHashIndex::StoreHashIndex()
727 {
728 if (store_table)
729 abort();
730 assert (store_table == NULL);
731 }
732
733 StoreHashIndex::~StoreHashIndex()
734 {
735 if (store_table) {
736 hashFreeItems(store_table, destroyStoreEntry);
737 hashFreeMemory(store_table);
738 store_table = NULL;
739 }
740 }
741
742 int
743 StoreHashIndex::callback()
744 {
745 int result = 0;
746 int j;
747 static int ndir = 0;
748
749 do {
750 j = 0;
751
752 for (int i = 0; i < Config.cacheSwap.n_configured; i++) {
753 if (ndir >= Config.cacheSwap.n_configured)
754 ndir = ndir % Config.cacheSwap.n_configured;
755
756 int temp_result = store(ndir)->callback();
757
758 ++ndir;
759
760 j += temp_result;
761
762 result += temp_result;
763
764 if (j > 100)
765 fatal ("too much io\n");
766 }
767 } while (j > 0);
768
769 ndir++;
770
771 return result;
772 }
773
774 void
775 StoreHashIndex::create()
776 {
777 for (int i = 0; i < Config.cacheSwap.n_configured; i++)
778 store(i)->create();
779 }
780
781 /* Lookup an object in the cache.
782 * return just a reference to object, don't start swapping in yet. */
783 StoreEntry *
784 StoreHashIndex::get(const cache_key *key)
785 {
786 PROF_start(storeGet);
787 debugs(20, 3, "storeGet: looking up " << storeKeyText(key));
788 StoreEntry *p = static_cast<StoreEntry *>(hash_lookup(store_table, key));
789 PROF_stop(storeGet);
790 return p;
791 }
792
793 void
794 StoreHashIndex::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData)
795 {
796 fatal("not implemented");
797 }
798
799 void
800 StoreHashIndex::init()
801 {
802 /* Calculate size of hash table (maximum currently 64k buckets). */
803 /* this is very bogus, its specific to the any Store maintaining an
804 * in-core index, not global */
805 size_t buckets = (Store::Root().maxSize() + ( Config.memMaxSize >> 10)) / Config.Store.avgObjectSize;
806 debugs(20, 1, "Swap maxSize " << Store::Root().maxSize() <<
807 " + " << ( Config.memMaxSize >> 10) << " KB, estimated " << buckets << " objects");
808 buckets /= Config.Store.objectsPerBucket;
809 debugs(20, 1, "Target number of buckets: " << buckets);
810 /* ideally the full scan period should be configurable, for the
811 * moment it remains at approximately 24 hours. */
812 store_hash_buckets = storeKeyHashBuckets(buckets);
813 debugs(20, 1, "Using " << store_hash_buckets << " Store buckets");
814 debugs(20, 1, "Max Mem size: " << ( Config.memMaxSize >> 10) << " KB");
815 debugs(20, 1, "Max Swap size: " << Store::Root().maxSize() << " KB");
816
817 store_table = hash_create(storeKeyHashCmp,
818 store_hash_buckets, storeKeyHashHash);
819
820 for (int i = 0; i < Config.cacheSwap.n_configured; i++) {
821 /* this starts a search of the store dirs, loading their
822 * index. under the new Store api this should be
823 * driven by the StoreHashIndex, not by each store.
824 *
825 * That is, the HashIndex should perform a search of each dir it is
826 * indexing to do the hash insertions. The search is then able to
827 * decide 'from-memory', or 'from-clean-log' or 'from-dirty-log' or
828 * 'from-no-log'.
829 *
830 * Step 1: make the store rebuilds use a search internally
831 * Step 2: change the search logic to use the four modes described
832 * above
833 * Step 3: have the hash index walk the searches itself.
834 */
835 store(i)->init();
836 }
837 }
838
839 uint64_t
840 StoreHashIndex::maxSize() const
841 {
842 uint64_t result = 0;
843
844 for (int i = 0; i < Config.cacheSwap.n_configured; i++)
845 result += store(i)->maxSize();
846
847 return result;
848 }
849
850 uint64_t
851 StoreHashIndex::minSize() const
852 {
853 uint64_t result = 0;
854
855 for (int i = 0; i < Config.cacheSwap.n_configured; i++)
856 result += store(i)->minSize();
857
858 return result;
859 }
860
861 void
862 StoreHashIndex::stat(StoreEntry & output) const
863 {
864 int i;
865
866 /* Now go through each store, calling its stat routine */
867
868 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
869 storeAppendPrintf(&output, "\n");
870 store(i)->stat(output);
871 }
872 }
873
874 void
875 StoreHashIndex::reference(StoreEntry&)
876 {}
877
878 void
879 StoreHashIndex::dereference(StoreEntry&)
880 {}
881
882 void
883 StoreHashIndex::maintain()
884 {
885 int i;
886 /* walk each fs */
887
888 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
889 /* XXX FixMe: This should be done "in parallell" on the different
890 * cache_dirs, not one at a time.
891 */
892 /* call the maintain function .. */
893 store(i)->maintain();
894 }
895 }
896
897 void
898 StoreHashIndex::updateSize(int64_t, int)
899 {}
900
901 void
902 StoreHashIndex::sync()
903 {
904 for (int i = 0; i < Config.cacheSwap.n_configured; ++i)
905 store(i)->sync();
906 }
907
908 StoreSearch *
909 StoreHashIndex::search(String const url, HttpRequest *)
910 {
911 if (url.size())
912 fatal ("Cannot search by url yet\n");
913
914 return new StoreSearchHashIndex (this);
915 }
916
917 CBDATA_CLASS_INIT(StoreSearchHashIndex);
918
919 StoreSearchHashIndex::StoreSearchHashIndex(RefCount<StoreHashIndex> aSwapDir) : sd(aSwapDir), _done (false), bucket (0)
920 {}
921
922 /* do not link
923 StoreSearchHashIndex::StoreSearchHashIndex(StoreSearchHashIndex const &);
924 */
925
926 StoreSearchHashIndex::~StoreSearchHashIndex()
927 {}
928
929 void
930 StoreSearchHashIndex::next(void (aCallback)(void *), void *aCallbackData)
931 {
932 next();
933 aCallback (aCallbackData);
934 }
935
936 bool
937 StoreSearchHashIndex::next()
938 {
939 if (entries.size())
940 entries.pop_back();
941
942 while (!isDone() && !entries.size())
943 copyBucket();
944
945 return currentItem() != NULL;
946 }
947
948 bool
949 StoreSearchHashIndex::error() const
950 {
951 return false;
952 }
953
954 bool
955 StoreSearchHashIndex::isDone() const
956 {
957 return bucket >= store_hash_buckets || _done;
958 }
959
960 StoreEntry *
961 StoreSearchHashIndex::currentItem()
962 {
963 if (!entries.size())
964 return NULL;
965
966 return entries.back();
967 }
968
969 void
970 StoreSearchHashIndex::copyBucket()
971 {
972 /* probably need to lock the store entries...
973 * we copy them all to prevent races on the links. */
974 debugs(47, 3, "StoreSearchHashIndex::copyBucket #" << bucket);
975 assert (!entries.size());
976 hash_link *link_ptr = NULL;
977 hash_link *link_next = NULL;
978 link_next = hash_get_bucket(store_table, bucket);
979
980 while (NULL != (link_ptr = link_next)) {
981 link_next = link_ptr->next;
982 StoreEntry *e = (StoreEntry *) link_ptr;
983
984 entries.push_back(e);
985 }
986
987 bucket++;
988 debugs(47,3, "got entries: " << entries.size());
989 }