]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_dir.cc
6e564643d32a04f36fb0b2e1b0c061dd32ccb0ac
[thirdparty/squid.git] / src / store_dir.cc
1
2 /*
3 * DEBUG: section 47 Store Directory Routines
4 * AUTHOR: Duane Wessels
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
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.
17 *
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.
22 *
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.
27 *
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.
31 *
32 */
33
34 #include "squid.h"
35 #include "globals.h"
36 #include "mem_node.h"
37 #include "MemObject.h"
38 #include "MemStore.h"
39 #include "profiler/Profiler.h"
40 #include "SquidConfig.h"
41 #include "SquidMath.h"
42 #include "SquidTime.h"
43 #include "Store.h"
44 #include "store_key_md5.h"
45 #include "StoreHashIndex.h"
46 #include "SwapDir.h"
47 #include "swap_log_op.h"
48 #include "tools.h"
49 #include "Transients.h"
50
51 #if HAVE_STATVFS
52 #if HAVE_SYS_STATVFS_H
53 #include <sys/statvfs.h>
54 #endif
55 #endif /* HAVE_STATVFS */
56 /* statfs() needs <sys/param.h> and <sys/mount.h> on BSD systems */
57 #if HAVE_SYS_PARAM_H
58 #include <sys/param.h>
59 #endif
60 #if HAVE_LIMITS_H
61 #include <limits.h>
62 #endif
63 #if HAVE_SYS_MOUNT_H
64 #include <sys/mount.h>
65 #endif
66 /* Windows and Linux use sys/vfs.h */
67 #if HAVE_SYS_VFS_H
68 #include <sys/vfs.h>
69 #endif
70 #if HAVE_SYS_WAIT_H
71 #include <sys/wait.h>
72 #endif
73 #if HAVE_ERRNO_H
74 #include <errno.h>
75 #endif
76
77 static STDIRSELECT storeDirSelectSwapDirRoundRobin;
78 static STDIRSELECT storeDirSelectSwapDirLeastLoad;
79
80 /*
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
85 * calls fatal()).
86 */
87 int StoreController::store_dirs_rebuilding = 1;
88
89 StoreController::StoreController() : swapDir (new StoreHashIndex())
90 , memStore(NULL), transients(NULL)
91 {}
92
93 StoreController::~StoreController()
94 {
95 delete memStore;
96 delete transients;
97 }
98
99 /*
100 * This function pointer is set according to 'store_dir_select_algorithm'
101 * in squid.conf.
102 */
103 STDIRSELECT *storeDirSelectSwapDir = storeDirSelectSwapDirLeastLoad;
104
105 void
106 StoreController::init()
107 {
108 if (Config.memShared && IamWorkerProcess()) {
109 memStore = new MemStore;
110 memStore->init();
111 }
112
113 swapDir->init();
114
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");
118 } else {
119 storeDirSelectSwapDir = storeDirSelectSwapDirLeastLoad;
120 debugs(47, DBG_IMPORTANT, "Using Least Load store dir selection");
121 }
122
123 if (UsingSmp() && IamWorkerProcess() && Config.onoff.collapsed_forwarding) {
124 transients = new Transients;
125 transients->init();
126 }
127 }
128
129 void
130 StoreController::createOneStore(Store &aStore)
131 {
132 /*
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.
136 */
137 #if !_SQUID_WINDOWS_
138
139 if (fork())
140 return;
141
142 #endif
143
144 aStore.create();
145
146 #if !_SQUID_WINDOWS_
147
148 exit(0);
149
150 #endif
151 }
152
153 void
154 StoreController::create()
155 {
156 swapDir->create();
157
158 #if !_SQUID_WINDOWS_
159
160 pid_t pid;
161
162 do {
163 int status;
164 #if _SQUID_NEXT_
165
166 pid = wait3(&status, WNOHANG, NULL);
167 #else
168
169 pid = waitpid(-1, &status, 0);
170 #endif
171
172 } while (pid > 0 || (pid < 0 && errno == EINTR));
173
174 #endif
175 }
176
177 /**
178 * Determine whether the given directory can handle this object
179 * size
180 *
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.
184 */
185 bool
186 SwapDir::objectSizeIsAcceptable(int64_t objsize) const
187 {
188 // If the swapdir has no range limits, then it definitely can
189 if (min_objsize <= 0 && max_objsize == -1)
190 return true;
191
192 /*
193 * If the object size is -1 and the storedir has limits we
194 * can't store it there.
195 */
196 if (objsize == -1)
197 return false;
198
199 // Else, make sure that the object size will fit.
200 if (max_objsize == -1 && min_objsize <= objsize)
201 return true;
202 else
203 return min_objsize <= objsize && max_objsize > objsize;
204 }
205
206 /*
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
209 * overloaded.
210 */
211 static int
212 storeDirSelectSwapDirRoundRobin(const StoreEntry * e)
213 {
214 static int dirn = 0;
215 int i;
216 int load;
217 RefCount<SwapDir> sd;
218
219 // e->objectLen() is negative at this point when we are still STORE_PENDING
220 ssize_t objsize = e->mem_obj->expectedReplySize();
221 if (objsize != -1)
222 objsize += e->mem_obj->swap_hdr_sz;
223
224 for (i = 0; i < Config.cacheSwap.n_configured; ++i) {
225 if (++dirn >= Config.cacheSwap.n_configured)
226 dirn = 0;
227
228 sd = dynamic_cast<SwapDir *>(INDEXSD(dirn));
229
230 if (!sd->canStore(*e, objsize, load))
231 continue;
232
233 if (load < 0 || load > 1000) {
234 continue;
235 }
236
237 return dirn;
238 }
239
240 return -1;
241 }
242
243 /*
244 * Spread load across all of the store directories
245 *
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.
251 *
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.
255 */
256 static int
257 storeDirSelectSwapDirLeastLoad(const StoreEntry * e)
258 {
259 int64_t most_free = 0;
260 ssize_t least_objsize = -1;
261 int least_load = INT_MAX;
262 int load;
263 int dirn = -1;
264 int i;
265 RefCount<SwapDir> SD;
266
267 // e->objectLen() is negative at this point when we are still STORE_PENDING
268 ssize_t objsize = e->mem_obj->expectedReplySize();
269
270 if (objsize != -1)
271 objsize += e->mem_obj->swap_hdr_sz;
272
273 for (i = 0; i < Config.cacheSwap.n_configured; ++i) {
274 SD = dynamic_cast<SwapDir *>(INDEXSD(i));
275 SD->flags.selected = false;
276
277 if (!SD->canStore(*e, objsize, load))
278 continue;
279
280 if (load < 0 || load > 1000)
281 continue;
282
283 if (load > least_load)
284 continue;
285
286 const int64_t cur_free = SD->maxSize() - SD->currentSize();
287
288 /* If the load is equal, then look in more details */
289 if (load == least_load) {
290 /* closest max-size fit */
291
292 if (least_objsize != -1)
293 if (SD->maxObjectSize() > least_objsize)
294 continue;
295
296 /* most free */
297 if (cur_free < most_free)
298 continue;
299 }
300
301 least_load = load;
302 least_objsize = SD->maxObjectSize();
303 most_free = cur_free;
304 dirn = i;
305 }
306
307 if (dirn >= 0)
308 dynamic_cast<SwapDir *>(INDEXSD(dirn))->flags.selected = true;
309
310 return dirn;
311 }
312
313 /*
314 * An entry written to the swap log MUST have the following
315 * properties.
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.
321 */
322 void
323 storeDirSwapLog(const StoreEntry * e, int op)
324 {
325 assert (e);
326 assert(!EBIT_TEST(e->flags, KEY_PRIVATE));
327 assert(e->swap_filen >= 0);
328 /*
329 * icons and such; don't write them to the swap log
330 */
331
332 if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
333 return;
334
335 assert(op > SWAP_LOG_NOP && op < SWAP_LOG_MAX);
336
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);
342
343 dynamic_cast<SwapDir *>(INDEXSD(e->swap_dirn))->logEntry(*e, op);
344 }
345
346 void
347 StoreController::getStats(StoreInfoStats &stats) const
348 {
349 if (memStore)
350 memStore->getStats(stats);
351 else {
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;
357 }
358
359 swapDir->getStats(stats);
360
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();
364 }
365
366 void
367 StoreController::stat(StoreEntry &output) const
368 {
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",
373 maxSize() >> 10);
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()));
379
380 if (memStore)
381 memStore->stat(output);
382
383 /* now the swapDir */
384 swapDir->stat(output);
385 }
386
387 /* if needed, this could be taught to cache the result */
388 uint64_t
389 StoreController::maxSize() const
390 {
391 /* TODO: include memory cache ? */
392 return swapDir->maxSize();
393 }
394
395 uint64_t
396 StoreController::minSize() const
397 {
398 /* TODO: include memory cache ? */
399 return swapDir->minSize();
400 }
401
402 uint64_t
403 StoreController::currentSize() const
404 {
405 return swapDir->currentSize();
406 }
407
408 uint64_t
409 StoreController::currentCount() const
410 {
411 return swapDir->currentCount();
412 }
413
414 int64_t
415 StoreController::maxObjectSize() const
416 {
417 return swapDir->maxObjectSize();
418 }
419
420 void
421 SwapDir::diskFull()
422 {
423 if (currentSize() >= maxSize())
424 return;
425
426 max_size = currentSize();
427
428 debugs(20, DBG_IMPORTANT, "WARNING: Shrinking cache_dir #" << index << " to " << currentSize() / 1024.0 << " KB");
429 }
430
431 void
432 storeDirOpenSwapLogs(void)
433 {
434 for (int dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn)
435 dynamic_cast<SwapDir *>(INDEXSD(dirn))->openLog();
436 }
437
438 void
439 storeDirCloseSwapLogs(void)
440 {
441 for (int dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn)
442 dynamic_cast<SwapDir *>(INDEXSD(dirn))->closeLog();
443 }
444
445 /*
446 * storeDirWriteCleanLogs
447 *
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.
453 */
454 int
455 storeDirWriteCleanLogs(int reopen)
456 {
457 const StoreEntry *e = NULL;
458 int n = 0;
459
460 struct timeval start;
461 double dt;
462 RefCount<SwapDir> sd;
463 int dirn;
464 int notdone = 1;
465
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.");
469 return 0;
470 }
471
472 debugs(20, DBG_IMPORTANT, "storeDirWriteCleanLogs: Starting...");
473 getCurrentTime();
474 start = current_time;
475
476 for (dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn) {
477 sd = dynamic_cast<SwapDir *>(INDEXSD(dirn));
478
479 if (sd->writeCleanStart() < 0) {
480 debugs(20, DBG_IMPORTANT, "log.clean.start() failed for dir #" << sd->index);
481 continue;
482 }
483 }
484
485 /*
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.
489 */
490 while (notdone) {
491 notdone = 0;
492
493 for (dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn) {
494 sd = dynamic_cast<SwapDir *>(INDEXSD(dirn));
495
496 if (NULL == sd->cleanLog)
497 continue;
498
499 e = sd->cleanLog->nextEntry();
500
501 if (!e)
502 continue;
503
504 notdone = 1;
505
506 if (!sd->canLog(*e))
507 continue;
508
509 sd->cleanLog->write(*e);
510
511 if ((++n & 0xFFFF) == 0) {
512 getCurrentTime();
513 debugs(20, DBG_IMPORTANT, " " << std::setw(7) << n <<
514 " entries written so far.");
515 }
516 }
517 }
518
519 /* Flush */
520 for (dirn = 0; dirn < Config.cacheSwap.n_configured; ++dirn)
521 dynamic_cast<SwapDir *>(INDEXSD(dirn))->writeCleanDone();
522
523 if (reopen)
524 storeDirOpenSwapLogs();
525
526 getCurrentTime();
527
528 dt = tvSubDsec(start, current_time);
529
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).");
533
534 return n;
535 }
536
537 StoreSearch *
538 StoreController::search(String const url, HttpRequest *request)
539 {
540 /* cheat, for now you can't search the memory hot cache */
541 return swapDir->search(url, request);
542 }
543
544 StorePointer
545 StoreHashIndex::store(int const x) const
546 {
547 return INDEXSD(x);
548 }
549
550 SwapDir &
551 StoreHashIndex::dir(const int i) const
552 {
553 SwapDir *sd = dynamic_cast<SwapDir*>(INDEXSD(i));
554 assert(sd);
555 return *sd;
556 }
557
558 void
559 StoreController::sync(void)
560 {
561 if (memStore)
562 memStore->sync();
563 swapDir->sync();
564 }
565
566 /*
567 * handle callbacks all avaliable fs'es
568 */
569 int
570 StoreController::callback()
571 {
572 /* This will likely double count. Thats ok. */
573 PROF_start(storeDirCallback);
574
575 /* mem cache callbacks ? */
576 int result = swapDir->callback();
577
578 PROF_stop(storeDirCallback);
579
580 return result;
581 }
582
583 int
584 storeDirGetBlkSize(const char *path, int *blksize)
585 {
586 #if HAVE_STATVFS
587
588 struct statvfs sfs;
589
590 if (statvfs(path, &sfs)) {
591 debugs(50, DBG_IMPORTANT, "" << path << ": " << xstrerror());
592 *blksize = 2048;
593 return 1;
594 }
595
596 *blksize = (int) sfs.f_frsize;
597 #else
598
599 struct statfs sfs;
600
601 if (statfs(path, &sfs)) {
602 debugs(50, DBG_IMPORTANT, "" << path << ": " << xstrerror());
603 *blksize = 2048;
604 return 1;
605 }
606
607 *blksize = (int) sfs.f_bsize;
608 #endif
609 /*
610 * Sanity check; make sure we have a meaningful value.
611 */
612
613 if (*blksize < 512)
614 *blksize = 2048;
615
616 return 0;
617 }
618
619 #define fsbtoblk(num, fsbs, bs) \
620 (((fsbs) != 0 && (fsbs) < (bs)) ? \
621 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
622 int
623 storeDirGetUFSStats(const char *path, int *totl_kb, int *free_kb, int *totl_in, int *free_in)
624 {
625 #if HAVE_STATVFS
626
627 struct statvfs sfs;
628
629 if (statvfs(path, &sfs)) {
630 debugs(50, DBG_IMPORTANT, "" << path << ": " << xstrerror());
631 return 1;
632 }
633
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;
638 #else
639
640 struct statfs sfs;
641
642 if (statfs(path, &sfs)) {
643 debugs(50, DBG_IMPORTANT, "" << path << ": " << xstrerror());
644 return 1;
645 }
646
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;
651 #endif
652
653 return 0;
654 }
655
656 void
657 allocate_new_swapdir(SquidConfig::_cacheSwap * swap)
658 {
659 if (swap->swapDirs == NULL) {
660 swap->n_allocated = 4;
661 swap->swapDirs = static_cast<SwapDir::Pointer *>(xcalloc(swap->n_allocated, sizeof(SwapDir::Pointer)));
662 }
663
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;
670 }
671 }
672
673 void
674 free_cachedir(SquidConfig::_cacheSwap * swap)
675 {
676 int i;
677 /* DON'T FREE THESE FOR RECONFIGURE */
678
679 if (reconfiguring)
680 return;
681
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.
686 * RBC 20041225
687 */
688 swap->swapDirs[i] = NULL;
689 }
690
691 safe_free(swap->swapDirs);
692 swap->swapDirs = NULL;
693 swap->n_allocated = 0;
694 swap->n_configured = 0;
695 }
696
697 /* this should be a virtual method on StoreEntry,
698 * i.e. e->referenced()
699 * so that the entry can notify the creating Store
700 */
701 void
702 StoreController::reference(StoreEntry &e)
703 {
704 // special entries do not belong to any specific Store, but are IN_MEMORY
705 if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
706 return;
707
708 /* Notify the fs that we're referencing this object again */
709
710 if (e.swap_dirn > -1)
711 swapDir->reference(e);
712
713 // Notify the memory cache that we're referencing this object again
714 if (memStore && e.mem_status == IN_MEMORY)
715 memStore->reference(e);
716
717 // TODO: move this code to a non-shared memory cache class when we have it
718 if (e.mem_obj) {
719 if (mem_policy->Referenced)
720 mem_policy->Referenced(mem_policy, &e, &e.mem_obj->repl);
721 }
722 }
723
724 bool
725 StoreController::dereference(StoreEntry &e, bool wantsLocalMemory)
726 {
727 // special entries do not belong to any specific Store, but are IN_MEMORY
728 if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
729 return true;
730
731 bool keepInStoreTable = false; // keep only if somebody needs it there
732
733 /* Notify the fs that we're not referencing this object any more */
734
735 if (e.swap_filen > -1)
736 keepInStoreTable = swapDir->dereference(e, wantsLocalMemory) || keepInStoreTable;
737
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;
741
742 // TODO: move this code to a non-shared memory cache class when we have it
743 if (e.mem_obj) {
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
747 if (!memStore)
748 keepInStoreTable = wantsLocalMemory || keepInStoreTable;
749 }
750
751 return keepInStoreTable;
752 }
753
754 StoreEntry *
755 StoreController::get(const cache_key *key)
756 {
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);
761 return e;
762 }
763
764 if (memStore) {
765 if (StoreEntry *e = memStore->get(key)) {
766 debugs(20, 3, HERE << "got mem-cached entry: " << *e);
767 return e;
768 }
769 }
770
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
777 static int idx = 0;
778 for (int n = 0; n < cacheDirs; ++n) {
779 idx = (idx + 1) % cacheDirs;
780 SwapDir *sd = dynamic_cast<SwapDir*>(INDEXSD(idx));
781 if (!sd->active())
782 continue;
783
784 if (StoreEntry *e = sd->get(key)) {
785 debugs(20, 3, HERE << "cache_dir " << idx <<
786 " got cached entry: " << *e);
787 return e;
788 }
789 }
790 }
791
792 debugs(20, 4, HERE << "none of " << Config.cacheSwap.n_configured <<
793 " cache_dirs have " << storeKeyText(key));
794
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).
798 if (transients) {
799 if (StoreEntry *e = transients->get(key)) {
800 debugs(20, 3, "got shared in-transit entry: " << *e);
801 return e;
802 }
803 }
804
805 return NULL;
806 }
807
808 void
809 StoreController::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData)
810 {
811 fatal("not implemented");
812 }
813
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
816 bool
817 StoreController::keepForLocalMemoryCache(const StoreEntry &e) const
818 {
819 if (!e.memoryCachable())
820 return false;
821
822 // does the current and expected size obey memory caching limits?
823 assert(e.mem_obj);
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;
831 }
832
833 void
834 StoreController::maybeTrimMemory(StoreEntry &e, const bool preserveSwappable)
835 {
836 bool keepInLocalMemory = false;
837 if (memStore)
838 keepInLocalMemory = memStore->keepInLocalMemory(e);
839 else
840 keepInLocalMemory = keepForLocalMemoryCache(e);
841
842 debugs(20, 7, HERE << "keepInLocalMemory: " << keepInLocalMemory);
843
844 if (!keepInLocalMemory)
845 e.trimMemory(preserveSwappable);
846 }
847
848 void
849 StoreController::handleIdleEntry(StoreEntry &e)
850 {
851 bool keepInLocalMemory = false;
852
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
861 } else {
862 keepInLocalMemory = keepForLocalMemoryCache(e) && // in good shape and
863 // the local memory cache is not overflowing
864 (mem_node::InUseCount() <= store_pages_max);
865 }
866
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));
872 return;
873 }
874
875 debugs(20, 5, HERE << "keepInLocalMemory: " << keepInLocalMemory);
876
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();
881 } else {
882 e.purgeMem(); // may free e
883 }
884 }
885
886 void
887 StoreController::allowCollapsing(StoreEntry *e, const RequestFlags &reqFlags,
888 const HttpRequestMethod &reqMethod)
889 {
890 e->makePublic(); // this is needed for both local and SMP collapsing
891 if (transients)
892 transients->put(e, reqFlags, reqMethod);
893 debugs(20, 3, "may " << (transients ? "SMP" : "") << " collapse " << *e);
894 }
895
896
897 StoreHashIndex::StoreHashIndex()
898 {
899 if (store_table)
900 abort();
901 assert (store_table == NULL);
902 }
903
904 StoreHashIndex::~StoreHashIndex()
905 {
906 if (store_table) {
907 hashFreeItems(store_table, destroyStoreEntry);
908 hashFreeMemory(store_table);
909 store_table = NULL;
910 }
911 }
912
913 int
914 StoreHashIndex::callback()
915 {
916 int result = 0;
917 int j;
918 static int ndir = 0;
919
920 do {
921 j = 0;
922
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;
926
927 int temp_result = store(ndir)->callback();
928
929 ++ndir;
930
931 j += temp_result;
932
933 result += temp_result;
934
935 if (j > 100)
936 fatal ("too much io\n");
937 }
938 } while (j > 0);
939
940 ++ndir;
941
942 return result;
943 }
944
945 void
946 StoreHashIndex::create()
947 {
948 if (Config.cacheSwap.n_configured == 0) {
949 debugs(0, DBG_PARSE_NOTE(DBG_CRITICAL), "No cache_dir stores are configured.");
950 }
951
952 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
953 if (dir(i).active())
954 store(i)->create();
955 }
956 }
957
958 /* Lookup an object in the cache.
959 * return just a reference to object, don't start swapping in yet. */
960 StoreEntry *
961 StoreHashIndex::get(const cache_key *key)
962 {
963 PROF_start(storeGet);
964 debugs(20, 3, "storeGet: looking up " << storeKeyText(key));
965 StoreEntry *p = static_cast<StoreEntry *>(hash_lookup(store_table, key));
966 PROF_stop(storeGet);
967 return p;
968 }
969
970 void
971 StoreHashIndex::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData)
972 {
973 fatal("not implemented");
974 }
975
976 void
977 StoreHashIndex::init()
978 {
979 if (Config.Store.objectsPerBucket <= 0)
980 fatal("'store_objects_per_bucket' should be larger than 0.");
981
982 if (Config.Store.avgObjectSize <= 0)
983 fatal("'store_avg_object_size' should be larger than 0.");
984
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");
1000
1001 store_table = hash_create(storeKeyHashCmp,
1002 store_hash_buckets, storeKeyHashHash);
1003
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.
1008 *
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
1012 * 'from-no-log'.
1013 *
1014 * Step 1: make the store rebuilds use a search internally
1015 * Step 2: change the search logic to use the four modes described
1016 * above
1017 * Step 3: have the hash index walk the searches itself.
1018 */
1019 if (dir(i).active())
1020 store(i)->init();
1021 }
1022 }
1023
1024 uint64_t
1025 StoreHashIndex::maxSize() const
1026 {
1027 uint64_t result = 0;
1028
1029 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
1030 if (dir(i).doReportStat())
1031 result += store(i)->maxSize();
1032 }
1033
1034 return result;
1035 }
1036
1037 uint64_t
1038 StoreHashIndex::minSize() const
1039 {
1040 uint64_t result = 0;
1041
1042 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
1043 if (dir(i).doReportStat())
1044 result += store(i)->minSize();
1045 }
1046
1047 return result;
1048 }
1049
1050 uint64_t
1051 StoreHashIndex::currentSize() const
1052 {
1053 uint64_t result = 0;
1054
1055 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
1056 if (dir(i).doReportStat())
1057 result += store(i)->currentSize();
1058 }
1059
1060 return result;
1061 }
1062
1063 uint64_t
1064 StoreHashIndex::currentCount() const
1065 {
1066 uint64_t result = 0;
1067
1068 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
1069 if (dir(i).doReportStat())
1070 result += store(i)->currentCount();
1071 }
1072
1073 return result;
1074 }
1075
1076 int64_t
1077 StoreHashIndex::maxObjectSize() const
1078 {
1079 int64_t result = -1;
1080
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();
1084 }
1085
1086 return result;
1087 }
1088
1089 void
1090 StoreHashIndex::getStats(StoreInfoStats &stats) const
1091 {
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);
1096 stats += dirStats;
1097 }
1098
1099 // common to all disks
1100 stats.swap.open_disk_fd = store_open_disk_fd;
1101
1102 // memory cache stats are collected in StoreController::getStats(), for now
1103 }
1104
1105 void
1106 StoreHashIndex::stat(StoreEntry & output) const
1107 {
1108 int i;
1109
1110 /* Now go through each store, calling its stat routine */
1111
1112 for (i = 0; i < Config.cacheSwap.n_configured; ++i) {
1113 storeAppendPrintf(&output, "\n");
1114 store(i)->stat(output);
1115 }
1116 }
1117
1118 void
1119 StoreHashIndex::reference(StoreEntry &e)
1120 {
1121 e.store()->reference(e);
1122 }
1123
1124 bool
1125 StoreHashIndex::dereference(StoreEntry &e, bool wantsLocalMemory)
1126 {
1127 return e.store()->dereference(e, wantsLocalMemory);
1128 }
1129
1130 void
1131 StoreHashIndex::maintain()
1132 {
1133 int i;
1134 /* walk each fs */
1135
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.
1139 */
1140 /* call the maintain function .. */
1141 store(i)->maintain();
1142 }
1143 }
1144
1145 void
1146 StoreHashIndex::sync()
1147 {
1148 for (int i = 0; i < Config.cacheSwap.n_configured; ++i)
1149 store(i)->sync();
1150 }
1151
1152 StoreSearch *
1153 StoreHashIndex::search(String const url, HttpRequest *)
1154 {
1155 if (url.size())
1156 fatal ("Cannot search by url yet\n");
1157
1158 return new StoreSearchHashIndex (this);
1159 }
1160
1161 CBDATA_CLASS_INIT(StoreSearchHashIndex);
1162
1163 StoreSearchHashIndex::StoreSearchHashIndex(RefCount<StoreHashIndex> aSwapDir) :
1164 sd(aSwapDir),
1165 callback(NULL),
1166 cbdata(NULL),
1167 _done(false),
1168 bucket(0)
1169 {}
1170
1171 /* do not link
1172 StoreSearchHashIndex::StoreSearchHashIndex(StoreSearchHashIndex const &);
1173 */
1174
1175 StoreSearchHashIndex::~StoreSearchHashIndex()
1176 {}
1177
1178 void
1179 StoreSearchHashIndex::next(void (aCallback)(void *), void *aCallbackData)
1180 {
1181 next();
1182 aCallback (aCallbackData);
1183 }
1184
1185 bool
1186 StoreSearchHashIndex::next()
1187 {
1188 if (entries.size())
1189 entries.pop_back();
1190
1191 while (!isDone() && !entries.size())
1192 copyBucket();
1193
1194 return currentItem() != NULL;
1195 }
1196
1197 bool
1198 StoreSearchHashIndex::error() const
1199 {
1200 return false;
1201 }
1202
1203 bool
1204 StoreSearchHashIndex::isDone() const
1205 {
1206 return bucket >= store_hash_buckets || _done;
1207 }
1208
1209 StoreEntry *
1210 StoreSearchHashIndex::currentItem()
1211 {
1212 if (!entries.size())
1213 return NULL;
1214
1215 return entries.back();
1216 }
1217
1218 void
1219 StoreSearchHashIndex::copyBucket()
1220 {
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);
1228
1229 while (NULL != (link_ptr = link_next)) {
1230 link_next = link_ptr->next;
1231 StoreEntry *e = (StoreEntry *) link_ptr;
1232
1233 entries.push_back(e);
1234 }
1235
1236 ++bucket;
1237 debugs(47,3, "got entries: " << entries.size());
1238 }