Controller.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 20 Store Controller */
10 
11 #include "squid.h"
12 #include "mem_node.h"
13 #include "MemStore.h"
14 #include "profiler/Profiler.h"
15 #include "SquidConfig.h"
16 #include "SquidMath.h"
17 #include "store/Controller.h"
18 #include "store/Disks.h"
19 #include "store/LocalSearch.h"
20 #include "tools.h"
21 #include "Transients.h"
22 
23 #if HAVE_SYS_WAIT_H
24 #include <sys/wait.h>
25 #endif
26 
27 /*
28  * store_dirs_rebuilding is initialized to _1_ as a hack so that
29  * storeDirWriteCleanLogs() doesn't try to do anything unless _all_
30  * cache_dirs have been read. For example, without this hack, Squid
31  * will try to write clean log files if -kparse fails (becasue it
32  * calls fatal()).
33  */
35 
37  swapDir(new Disks),
38  sharedMemStore(nullptr),
39  localMemStore(false),
40  transients(NULL)
41 {
43 }
44 
46 {
47  delete sharedMemStore;
48  delete transients;
49  delete swapDir;
50 
51  if (store_table) {
54  store_table = nullptr;
55  }
56 }
57 
58 void
60 {
61  if (IamWorkerProcess()) {
62  if (MemStore::Enabled()) {
63  sharedMemStore = new MemStore;
64  sharedMemStore->init();
65  } else if (Config.memMaxSize > 0) {
66  localMemStore = true;
67  }
68  }
69 
70  swapDir->init();
71 
73  transients = new Transients;
74  transients->init();
75  }
76 }
77 
78 void
80 {
81  swapDir->create();
82 
83 #if !_SQUID_WINDOWS_
84  pid_t pid;
85  do {
86  PidStatus status;
87  pid = WaitForAnyPid(status, WNOHANG);
88  } while (pid > 0 || (pid < 0 && errno == EINTR));
89 #endif
90 }
91 
92 void
94 {
95  static time_t last_warn_time = 0;
96 
97  PROF_start(storeMaintainSwapSpace);
98  swapDir->maintain();
99 
100  /* this should be emitted by the oversize dir, not globally */
101 
102  if (Root().currentSize() > Store::Root().maxSize()) {
103  if (squid_curtime - last_warn_time > 10) {
104  debugs(20, DBG_CRITICAL, "WARNING: Disk space over limit: "
105  << Store::Root().currentSize() / 1024.0 << " KB > "
106  << (Store::Root().maxSize() >> 10) << " KB");
107  last_warn_time = squid_curtime;
108  }
109  }
110 
111  PROF_stop(storeMaintainSwapSpace);
112 }
113 
114 void
116 {
117  if (sharedMemStore)
118  sharedMemStore->getStats(stats);
119  else {
120  // move this code to a non-shared memory cache class when we have it
121  stats.mem.shared = false;
122  stats.mem.capacity = Config.memMaxSize;
123  stats.mem.size = mem_node::StoreMemSize();
124  if (localMemStore) {
125  // XXX: also count internal/in-transit objects
126  stats.mem.count = hot_obj_count;
127  } else {
128  // XXX: count internal/in-transit objects instead
129  stats.mem.count = hot_obj_count;
130  }
131  }
132 
133  swapDir->getStats(stats);
134 
135  // low-level info not specific to memory or disk cache
136  stats.store_entry_count = StoreEntry::inUseCount();
137  stats.mem_object_count = MemObject::inUseCount();
138 }
139 
140 void
142 {
143  storeAppendPrintf(&output, "Store Directory Statistics:\n");
144  storeAppendPrintf(&output, "Store Entries : %lu\n",
145  (unsigned long int)StoreEntry::inUseCount());
146  storeAppendPrintf(&output, "Maximum Swap Size : %" PRIu64 " KB\n",
147  maxSize() >> 10);
148  storeAppendPrintf(&output, "Current Store Swap Size: %.2f KB\n",
149  currentSize() / 1024.0);
150  storeAppendPrintf(&output, "Current Capacity : %.2f%% used, %.2f%% free\n",
151  Math::doublePercent(currentSize(), maxSize()),
152  Math::doublePercent((maxSize() - currentSize()), maxSize()));
153 
154  if (sharedMemStore)
155  sharedMemStore->stat(output);
156 
157  /* now the swapDir */
158  swapDir->stat(output);
159 }
160 
161 /* if needed, this could be taught to cache the result */
162 uint64_t
164 {
165  /* TODO: include memory cache ? */
166  return swapDir->maxSize();
167 }
168 
169 uint64_t
171 {
172  /* TODO: include memory cache ? */
173  return swapDir->minSize();
174 }
175 
176 uint64_t
178 {
179  /* TODO: include memory cache ? */
180  return swapDir->currentSize();
181 }
182 
183 uint64_t
185 {
186  /* TODO: include memory cache ? */
187  return swapDir->currentCount();
188 }
189 
190 int64_t
192 {
193  /* TODO: include memory cache ? */
194  return swapDir->maxObjectSize();
195 }
196 
197 void
199 {
200  swapDir->updateLimits();
201 
202  store_swap_high = (long) (((float) maxSize() *
203  (float) Config.Swap.highWaterMark) / (float) 100);
204  store_swap_low = (long) (((float) maxSize() *
205  (float) Config.Swap.lowWaterMark) / (float) 100);
207 
208  // TODO: move this into a memory cache class when we have one
209  const int64_t memMax = static_cast<int64_t>(min(Config.Store.maxInMemObjSize, Config.memMaxSize));
210  const int64_t disksMax = swapDir ? swapDir->maxObjectSize() : 0;
211  store_maxobjsize = std::max(disksMax, memMax);
212 }
213 
214 StoreSearch *
216 {
217  // this is the only kind of search we currently support
218  return NewLocalSearch();
219 }
220 
221 void
223 {
224  if (sharedMemStore)
225  sharedMemStore->sync();
226  swapDir->sync();
227 }
228 
229 /*
230  * handle callbacks all avaliable fs'es
231  */
232 int
234 {
235  /* This will likely double count. Thats ok. */
236  PROF_start(storeDirCallback);
237 
238  /* mem cache callbacks ? */
239  int result = swapDir->callback();
240 
241  PROF_stop(storeDirCallback);
242 
243  return result;
244 }
245 
247 void
249 {
250  // special entries do not belong to any specific Store, but are IN_MEMORY
251  if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
252  return;
253 
254  /* Notify the fs that we're referencing this object again */
255 
256  if (e.hasDisk())
257  swapDir->reference(e);
258 
259  // Notify the memory cache that we're referencing this object again
260  if (sharedMemStore && e.mem_status == IN_MEMORY)
261  sharedMemStore->reference(e);
262 
263  // TODO: move this code to a non-shared memory cache class when we have it
264  if (e.mem_obj) {
265  if (mem_policy->Referenced)
267  }
268 }
269 
272 bool
274 {
275  // special entries do not belong to any specific Store, but are IN_MEMORY
276  if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
277  return true;
278 
279  bool keepInStoreTable = false; // keep only if somebody needs it there
280 
281  /* Notify the fs that we're not referencing this object any more */
282 
283  if (e.hasDisk())
284  keepInStoreTable = swapDir->dereference(e) || keepInStoreTable;
285 
286  // Notify the memory cache that we're not referencing this object any more
287  if (sharedMemStore && e.mem_status == IN_MEMORY)
288  keepInStoreTable = sharedMemStore->dereference(e) || keepInStoreTable;
289 
290  // TODO: move this code to a non-shared memory cache class when we have it
291  if (e.mem_obj) {
294  // non-shared memory cache relies on store_table
295  if (localMemStore)
296  keepInStoreTable = wantsLocalMemory || keepInStoreTable;
297  }
298 
299  return keepInStoreTable;
300 }
301 
302 bool
304 {
305  // assuming a public key, checking Transients should cover all cases.
306  return transients && transients->markedForDeletion(key);
307 }
308 
309 bool
311 {
312  // The opposite check order could miss a reader that has arrived after the
313  // !readers() and before the markedForDeletion() check.
314  return markedForDeletion(reinterpret_cast<const cache_key*>(e.key)) &&
315  transients && !transients->readers(e);
316 }
317 
318 bool
320 {
321  return swapDir->hasReadableEntry(e);
322 }
323 
324 StoreEntry *
326 {
327  if (const auto entry = peek(key)) {
328  try {
329  if (!entry->key)
330  allowSharing(*entry, key);
331  checkTransients(*entry);
332  entry->touch();
333  referenceBusy(*entry);
334  return entry;
335  } catch (const std::exception &ex) {
336  debugs(20, 2, "failed with " << *entry << ": " << ex.what());
337  entry->release();
338  // fall through
339  }
340  }
341  return NULL;
342 }
343 
345 void
347 {
348  // TODO: refactor to throw on anchorToCache() inSync errors!
349 
350  // anchorToCache() below and many find() callers expect a registered entry
351  addReading(&entry, key);
352 
353  if (entry.hasTransients()) {
354  bool inSync = false;
355  const bool found = anchorToCache(entry, inSync);
356  if (found && !inSync)
357  throw TexcHere("cannot sync");
358  if (!found) {
359  // !found should imply hittingRequiresCollapsing() regardless of writer presence
360  if (!entry.hittingRequiresCollapsing()) {
361  debugs(20, DBG_IMPORTANT, "BUG: missing ENTRY_REQUIRES_COLLAPSING for " << entry);
362  throw TextException("transients entry missing ENTRY_REQUIRES_COLLAPSING", Here());
363  }
364 
365  if (!transients->hasWriter(entry)) {
366  // prevent others from falling into the same trap
367  throw TextException("unattached transients entry missing writer", Here());
368  }
369  }
370  }
371 }
372 
373 StoreEntry *
375 {
376  // We could check for mem_obj presence (and more), moving and merging some
377  // of the duplicated neighborsUdpAck() and neighborsHtcpReply() code here,
378  // but that would mean polluting Store with HTCP/ICP code. Instead, we
379  // should encapsulate callback-related data in a protocol-neutral MemObject
380  // member or use an HTCP/ICP-specific index rather than store_table.
381 
382  // cannot reuse peekAtLocal() because HTCP/ICP callbacks may use private keys
383  return static_cast<StoreEntry*>(hash_lookup(store_table, key));
384 }
385 
389 StoreEntry *
391 {
392  if (StoreEntry *e = static_cast<StoreEntry*>(hash_lookup(store_table, key))) {
393  // callers must only search for public entries
394  assert(!EBIT_TEST(e->flags, KEY_PRIVATE));
395  assert(e->publicKey());
396  checkTransients(*e);
397 
398  // TODO: ignore and maybe handleIdleEntry() unlocked intransit entries
399  // because their backing store slot may be gone already.
400  return e;
401  }
402  return nullptr;
403 }
404 
405 StoreEntry *
407 {
408  debugs(20, 3, storeKeyText(key));
409 
410  if (markedForDeletion(key)) {
411  debugs(20, 3, "ignoring marked in-transit " << storeKeyText(key));
412  return nullptr;
413  }
414 
415  if (StoreEntry *e = peekAtLocal(key)) {
416  debugs(20, 3, "got local in-transit entry: " << *e);
417  return e;
418  }
419 
420  // Must search transients before caches because we must sync those we find.
421  if (transients) {
422  if (StoreEntry *e = transients->get(key)) {
423  debugs(20, 3, "got shared in-transit entry: " << *e);
424  return e;
425  }
426  }
427 
428  if (sharedMemStore) {
429  if (StoreEntry *e = sharedMemStore->get(key)) {
430  debugs(20, 3, HERE << "got mem-cached entry: " << *e);
431  return e;
432  }
433  }
434 
435  if (swapDir) {
436  if (StoreEntry *e = swapDir->get(key)) {
437  debugs(20, 3, "got disk-cached entry: " << *e);
438  return e;
439  }
440  }
441 
442  debugs(20, 4, "cannot locate " << storeKeyText(key));
443  return nullptr;
444 }
445 
446 bool
448 {
449  return transients && e.hasTransients() && transients->isReader(e);
450 }
451 
452 bool
454 {
455  return transients && e.hasTransients() && transients->isWriter(e);
456 }
457 
458 int64_t
460 {
461  return swapDir ? swapDir->accumulateMore(entry) : 0;
462  // The memory cache should not influence for-swapout accumulation decision.
463 }
464 
465 // Must be called from StoreEntry::release() or releaseRequest() because
466 // those methods currently manage local indexing of StoreEntry objects.
467 // TODO: Replace StoreEntry::release*() with Root().evictCached().
468 void
470 {
471  debugs(20, 7, e);
472  if (transients)
473  transients->evictCached(e);
474  memoryEvictCached(e);
475  if (swapDir)
476  swapDir->evictCached(e);
477 }
478 
479 void
481 {
482  debugs(20, 7, storeKeyText(key));
483 
484  if (StoreEntry *entry = peekAtLocal(key)) {
485  debugs(20, 5, "marking local in-transit " << *entry);
486  entry->release(true);
487  return;
488  }
489 
490  if (sharedMemStore)
491  sharedMemStore->evictIfFound(key);
492  if (swapDir)
493  swapDir->evictIfFound(key);
494  if (transients)
495  transients->evictIfFound(key);
496 }
497 
499 bool
500 Store::Controller::memoryCacheHasSpaceFor(const int pagesRequired) const
501 {
502  // XXX: We count mem_nodes but may free shared memory pages instead.
503  const auto fits = mem_node::InUseCount() + pagesRequired <= store_pages_max;
504  debugs(20, 7, fits << ": " << mem_node::InUseCount() << '+' << pagesRequired << '?' << store_pages_max);
505  return fits;
506 }
507 
508 void
509 Store::Controller::freeMemorySpace(const int bytesRequired)
510 {
511  const auto pagesRequired = (bytesRequired + SM_PAGE_SIZE-1) / SM_PAGE_SIZE;
512 
513  if (memoryCacheHasSpaceFor(pagesRequired))
514  return;
515 
516  // XXX: When store_pages_max is smaller than pagesRequired, we should not
517  // look for more space (but we do because we want to abandon idle entries?).
518 
519  // limit our performance impact to one walk per second
520  static time_t lastWalk = 0;
521  if (lastWalk == squid_curtime)
522  return;
523  lastWalk = squid_curtime;
524 
525  debugs(20, 2, "need " << pagesRequired << " pages");
526 
527  // let abandon()/handleIdleEntry() know about the impeding memory shortage
528  memoryPagesDebt_ = pagesRequired;
529 
530  // XXX: SMP-unaware: Walkers should iterate memory cache, not store_table.
531  // XXX: Limit iterations by time, not arbitrary count.
532  const auto walker = mem_policy->PurgeInit(mem_policy, 100000);
533  int removed = 0;
534  while (const auto entry = walker->Next(walker)) {
535  // Abandoned memory cache entries are purged during memory shortage.
536  entry->abandon(__FUNCTION__); // may delete entry
537  ++removed;
538 
539  if (memoryCacheHasSpaceFor(pagesRequired))
540  break;
541  }
542  // TODO: Move to RemovalPolicyWalker::Done() that has more/better details.
543  debugs(20, 3, "removed " << removed << " out of " << hot_obj_count << " memory-cached entries");
544  walker->Done(walker);
545  memoryPagesDebt_ = 0;
546 }
547 
548 // move this into [non-shared] memory cache class when we have one
550 bool
552 {
553  if (!e.memoryCachable())
554  return false;
555 
556  // does the current and expected size obey memory caching limits?
557  assert(e.mem_obj);
558  const int64_t loadedSize = e.mem_obj->endOffset();
559  const int64_t expectedSize = e.mem_obj->expectedReplySize(); // may be < 0
560  const int64_t ramSize = max(loadedSize, expectedSize);
561  const int64_t ramLimit = min(
562  static_cast<int64_t>(Config.memMaxSize),
563  static_cast<int64_t>(Config.Store.maxInMemObjSize));
564  return ramSize <= ramLimit;
565 }
566 
567 void
568 Store::Controller::memoryOut(StoreEntry &e, const bool preserveSwappable)
569 {
570  bool keepInLocalMemory = false;
571  if (sharedMemStore)
572  sharedMemStore->write(e); // leave keepInLocalMemory false
573  else if (localMemStore)
574  keepInLocalMemory = keepForLocalMemoryCache(e);
575 
576  debugs(20, 7, HERE << "keepInLocalMemory: " << keepInLocalMemory);
577 
578  if (!keepInLocalMemory)
579  e.trimMemory(preserveSwappable);
580 }
581 
584 void
586 {
587  // TODO: Untangle memory caching from mem_obj.
588  if (sharedMemStore)
589  sharedMemStore->evictCached(e);
590  else // TODO: move into [non-shared] memory cache class when we have one
591  if (!e.locked())
592  e.destroyMemObject();
593 }
594 
595 void
597 {
598  if (sharedMemStore)
599  sharedMemStore->disconnect(e);
600  // else nothing to do for non-shared memory cache
601 }
602 
603 void
605 {
606  // Marking the transients entry is sufficient to prevent new readers from
607  // starting to wait for `e` updates and to inform the current readers (and,
608  // hence, Broadcast() recipients) about the underlying Store problems.
609  if (transients && e.hasTransients())
610  transients->evictCached(e);
611 }
612 
613 void
615 {
616  // transients->isWriter(e) is false if `e` is writing to its second store
617  // after finishing writing to its first store: At the end of the first swap
618  // out, the transients writer becomes a reader and (XXX) we never switch
619  // back to writing, even if we start swapping out again (to another store).
620  if (transients && e.hasTransients() && transients->isWriter(e))
621  transients->completeWriting(e);
622 }
623 
624 int
626 {
627  return (transients && e.hasTransients()) ?
628  transients->readers(e) : 0;
629 }
630 
631 void
633 {
634  if (transients)
635  transients->disconnect(e);
636 }
637 
638 void
640 {
641  if (transients)
642  transients->clearCollapsingRequirement(e);
643 }
644 
645 void
647 {
648  bool keepInLocalMemory = false;
649 
650  if (EBIT_TEST(e.flags, ENTRY_SPECIAL)) {
651  // Icons (and cache digests?) should stay in store_table until we
652  // have a dedicated storage for them (that would not purge them).
653  // They are not managed [well] by any specific Store handled below.
654  keepInLocalMemory = true;
655  } else if (sharedMemStore) {
656  // leave keepInLocalMemory false; sharedMemStore maintains its own cache
657  } else if (localMemStore) {
658  keepInLocalMemory = keepForLocalMemoryCache(e) && // in good shape and
659  // the local memory cache is not overflowing
660  memoryCacheHasSpaceFor(memoryPagesDebt_);
661  }
662 
663  // An idle, unlocked entry that only belongs to a SwapDir which controls
664  // its own index, should not stay in the global store_table.
665  if (!dereferenceIdle(e, keepInLocalMemory)) {
666  debugs(20, 5, HERE << "destroying unlocked entry: " << &e << ' ' << e);
667  destroyStoreEntry(static_cast<hash_link*>(&e));
668  return;
669  }
670 
671  debugs(20, 5, HERE << "keepInLocalMemory: " << keepInLocalMemory);
672 
673  // TODO: move this into [non-shared] memory cache class when we have one
674  if (keepInLocalMemory) {
676  e.mem_obj->unlinkRequest();
677  return;
678  }
679 
680  // We know the in-memory data will be gone. Get rid of the entire entry if
681  // it has nothing worth preserving on disk either.
682  if (!e.swappedOut()) {
683  e.release(); // deletes e
684  return;
685  }
686 
687  memoryEvictCached(e); // may already be gone
688  // and keep the entry in store_table for its on-disk data
689 }
690 
691 void
693 {
694  Must(old);
695  Must(old->mem_obj);
696  Must(e304.mem_obj);
697 
698  // updateOnNotModified() may be called many times for the same old entry.
699  // e304.mem_obj->appliedUpdates value distinguishes two cases:
700  // false: Independent store clients revalidating the same old StoreEntry.
701  // Each such update uses its own e304. The old StoreEntry
702  // accumulates such independent updates.
703  // true: Store clients feeding off the same 304 response. Each such update
704  // uses the same e304. For timestamps correctness and performance
705  // sake, it is best to detect and skip such repeated update calls.
706  if (e304.mem_obj->appliedUpdates) {
707  debugs(20, 5, "ignored repeated update of " << *old << " with " << e304);
708  return;
709  }
710  e304.mem_obj->appliedUpdates = true;
711 
712  if (!old->updateOnNotModified(e304)) {
713  debugs(20, 5, "updated nothing in " << *old << " with " << e304);
714  return;
715  }
716 
717  if (sharedMemStore && old->mem_status == IN_MEMORY && !EBIT_TEST(old->flags, ENTRY_SPECIAL))
718  sharedMemStore->updateHeaders(old);
719 
720  if (old->swap_dirn > -1)
721  swapDir->updateHeaders(old);
722 }
723 
724 bool
726  const HttpRequestMethod &reqMethod)
727 {
728  const KeyScope keyScope = reqFlags.refresh ? ksRevalidation : ksDefault;
729  // set the flag now so that it gets copied into the Transients entry
730  e->setCollapsingRequirement(true);
731  if (e->makePublic(keyScope)) { // this is needed for both local and SMP collapsing
732  debugs(20, 3, "may " << (transients && e->hasTransients() ?
733  "SMP-" : "locally-") << "collapse " << *e);
734  return true;
735  }
736  // paranoid cleanup; the flag is meaningless for private entries
737  e->setCollapsingRequirement(false);
738  return false;
739 }
740 
741 void
743 {
744  if (transients)
745  transients->monitorIo(e, key, Store::ioReading);
746  e->hashInsert(key);
747 }
748 
749 void
751 {
752  assert(e);
753  if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
754  return; // constant memory-resident entries do not need transients
755 
756  if (transients)
757  transients->monitorIo(e, key, Store::ioWriting);
758  // else: non-SMP configurations do not need transients
759 }
760 
761 void
763 {
764  assert(transients);
765 
766  StoreEntry *collapsed = transients->findCollapsed(xitIndex);
767  if (!collapsed) { // the entry is no longer active, ignore update
768  debugs(20, 7, "not SMP-syncing not-transient " << xitIndex);
769  return;
770  }
771 
772  if (!collapsed->locked()) {
773  debugs(20, 3, "skipping (and may destroy) unlocked " << *collapsed);
774  handleIdleEntry(*collapsed);
775  return;
776  }
777 
778  assert(collapsed->mem_obj);
779 
780  if (EBIT_TEST(collapsed->flags, ENTRY_ABORTED)) {
781  debugs(20, 3, "skipping already aborted " << *collapsed);
782  return;
783  }
784 
785  debugs(20, 7, "syncing " << *collapsed);
786 
787  Transients::EntryStatus entryStatus;
788  transients->status(*collapsed, entryStatus);
789 
790  if (!entryStatus.collapsed) {
791  debugs(20, 5, "removing collapsing requirement for " << *collapsed << " since remote writer probably got headers");
792  collapsed->setCollapsingRequirement(false);
793  }
794 
795  if (entryStatus.waitingToBeFreed) {
796  debugs(20, 3, "will release " << *collapsed << " due to waitingToBeFreed");
797  collapsed->release(true); // may already be marked
798  }
799 
800  if (transients->isWriter(*collapsed))
801  return; // readers can only change our waitingToBeFreed flag
802 
803  assert(transients->isReader(*collapsed));
804 
805  if (entryStatus.abortedByWriter) {
806  debugs(20, 3, "aborting " << *collapsed << " because its writer has aborted");
807  collapsed->abort();
808  return;
809  }
810 
811  if (entryStatus.collapsed && !collapsed->hittingRequiresCollapsing()) {
812  debugs(20, 3, "aborting " << *collapsed << " due to writer/reader collapsing state mismatch");
813  collapsed->abort();
814  return;
815  }
816 
817  bool found = false;
818  bool inSync = false;
819  if (sharedMemStore && collapsed->mem_obj->memCache.io == MemObject::ioDone) {
820  found = true;
821  inSync = true;
822  debugs(20, 7, "fully mem-loaded " << *collapsed);
823  } else if (sharedMemStore && collapsed->hasMemStore()) {
824  found = true;
825  inSync = sharedMemStore->updateAnchored(*collapsed);
826  // TODO: handle entries attached to both memory and disk
827  } else if (swapDir && collapsed->hasDisk()) {
828  found = true;
829  inSync = swapDir->updateAnchored(*collapsed);
830  } else {
831  found = anchorToCache(*collapsed, inSync);
832  }
833 
834  if (entryStatus.waitingToBeFreed && !found) {
835  debugs(20, 3, "aborting unattached " << *collapsed <<
836  " because it was marked for deletion before we could attach it");
837  collapsed->abort();
838  return;
839  }
840 
841  if (inSync) {
842  debugs(20, 5, "synced " << *collapsed);
843  collapsed->invokeHandlers();
844  return;
845  }
846 
847  if (found) { // unrecoverable problem syncing this entry
848  debugs(20, 3, "aborting unsyncable " << *collapsed);
849  collapsed->abort();
850  return;
851  }
852 
853  // the entry is still not in one of the caches
854  debugs(20, 7, "waiting " << *collapsed);
855 }
856 
860 bool
862 {
863  assert(entry.hasTransients());
864  assert(transientsReader(entry));
865 
866  debugs(20, 7, "anchoring " << entry);
867 
868  bool found = false;
869  if (sharedMemStore)
870  found = sharedMemStore->anchorToCache(entry, inSync);
871  if (!found && swapDir)
872  found = swapDir->anchorToCache(entry, inSync);
873 
874  if (found) {
875  if (inSync)
876  debugs(20, 7, "anchored " << entry);
877  else
878  debugs(20, 5, "failed to anchor " << entry);
879  } else {
880  debugs(20, 7, "skipping not yet cached " << entry);
881  }
882 
883  return found;
884 }
885 
886 bool
888 {
889  return MemStore::Enabled() || Disks::SmpAware();
890 }
891 
892 void
894 {
895  if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
896  return;
897  assert(!transients || e.hasTransients());
898 }
899 
900 namespace Store {
902 }
903 
906 {
907  assert(TheRoot);
908  return *TheRoot;
909 }
910 
911 void
913 {
914  TheRoot = root ? root : new Controller;
915 }
916 
917 void
919 {
920  TheRoot = nullptr;
921 }
922 
int transientReaders(const StoreEntry &) const
number of the transient entry readers some time ago
Definition: Controller.cc:625
static size_t StoreMemSize()
Definition: mem_node.cc:61
void memoryDisconnect(StoreEntry &)
disassociates the entry from the memory cache, preserving cached data
Definition: Controller.cc:596
virtual uint64_t minSize() const override
the minimum size the store will shrink to via normal housekeeping
Definition: Controller.cc:170
#define Here()
source code location of the caller
Definition: Here.h:15
#define SM_PAGE_SIZE
Definition: defines.h:102
void memoryOut(StoreEntry &, const bool preserveSwappable)
called to get rid of no longer needed entry data in RAM, if any
Definition: Controller.cc:568
bool updateOnNotModified(const StoreEntry &e304)
Definition: store.cc:1505
virtual void create() override
create system resources needed for this store to operate in the future
Definition: Controller.cc:79
unsigned char cache_key
Store key.
Definition: forward.h:29
static bool Enabled()
whether Squid is correctly configured to use a shared memory cache
Definition: MemStore.h:68
bool makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:145
bool hasReadableDiskEntry(const StoreEntry &) const
whether there is a disk entry with e.key
Definition: Controller.cc:319
virtual void sync() override
prepare for shutdown
Definition: Controller.cc:222
MemObject * mem_obj
Definition: Store.h:209
StoreEntry * peekAtLocal(const cache_key *)
Definition: Controller.cc:390
bool waitingToBeFreed
whether the entry was marked for deletion
Definition: Transients.h:34
size_t memMaxSize
Definition: SquidConfig.h:85
static bool SmpAware()
whether there are any SMP-aware storages
Definition: Controller.cc:887
void addReading(StoreEntry *, const cache_key *)
Definition: Controller.cc:742
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:881
void FreeMemory()
undo Init()
Definition: Controller.cc:918
void checkTransients(const StoreEntry &) const
Definition: Controller.cc:893
MemCache memCache
current [shared] memory caching state for the entry
Definition: MemObject.h:187
static bool SmpAware()
whether any disk cache is SMP-aware
Definition: Disks.cc:548
SQUIDCEXTERN void hashFreeItems(hash_table *, HASHFREE *)
Definition: hash.cc:256
virtual void init() override
Definition: Controller.cc:59
RemovalPurgeWalker *(* PurgeInit)(RemovalPolicy *policy, int max_scan)
Definition: RemovalPolicy.h:51
int64_t expectedReplySize() const
Definition: MemObject.cc:259
void referenceBusy(StoreEntry &e)
update reference counters of the recently touched entry
Definition: Controller.cc:248
const A & max(A const &lhs, A const &rhs)
SQUIDCEXTERN hash_link * hash_lookup(hash_table *, const void *)
Definition: hash.cc:147
bool transientsReader(const StoreEntry &) const
whether the entry is in "reading from Transients" I/O state
Definition: Controller.cc:447
#define PRIu64
Definition: types.h:120
virtual int callback() override
called once every main loop iteration; TODO: Move to UFS code.
Definition: Controller.cc:233
bool memoryCacheHasSpaceFor(const int pagesRequired) const
whether the memory cache is allowed to store that many additional pages
Definition: Controller.cc:500
::Transients Transients
Definition: forward.h:52
uint16_t flags
Definition: Store.h:220
int hot_obj_count
void memoryEvictCached(StoreEntry &)
Definition: Controller.cc:585
bool IamWorkerProcess()
whether the current process handles HTTP transactions and such
Definition: stub_tools.cc:49
Io io
current I/O state
Definition: MemObject.h:185
int64_t endOffset() const
Definition: MemObject.cc:235
#define DBG_CRITICAL
Definition: Debug.h:45
int store_swap_low
RemovalPolicy * mem_policy
Definition: MemObject.cc:45
#define PROF_stop(probename)
Definition: Profiler.h:63
#define DBG_IMPORTANT
Definition: Debug.h:46
void transientsCompleteWriting(StoreEntry &)
marks the entry completed for collapsed requests
Definition: Controller.cc:614
virtual void evictCached(StoreEntry &) override
Definition: Controller.cc:469
void destroyMemObject()
Definition: store.cc:378
bool allowCollapsing(StoreEntry *, const RequestFlags &, const HttpRequestMethod &)
tries to make the entry available for collapsing future requests
Definition: Controller.cc:725
int64_t accumulateMore(StoreEntry &) const
Definition: Controller.cc:459
int highWaterMark
Definition: SquidConfig.h:79
void(* Referenced)(RemovalPolicy *policy, const StoreEntry *entry, RemovalPolicyNode *node)
Definition: RemovalPolicy.h:48
bool abortedByWriter
whether the entry was aborted
Definition: Transients.h:33
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
Definition: TextException.h:55
void invokeHandlers()
static pid_t pid
Definition: IcmpSquid.cc:35
void addWriting(StoreEntry *, const cache_key *)
Definition: Controller.cc:750
shared entry metadata, used for synchronization
Definition: Transients.h:30
void updateLimits()
slowly calculate (and cache) hi/lo watermarks and similar limits
Definition: Controller.cc:198
virtual void init() override
Definition: Transients.cc:41
double doublePercent(const double, const double)
Definition: SquidMath.cc:25
void transientsDisconnect(StoreEntry &)
disassociates the entry from the intransit table
Definition: Controller.cc:632
StoreEntry * find(const cache_key *)
Definition: Controller.cc:325
static bool Enabled()
Can we create and initialize Transients?
Definition: Transients.h:94
#define NULL
Definition: types.h:166
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
void abort()
Definition: store.cc:1109
void stopSharing(StoreEntry &)
stop any current (and prevent any future) SMP sharing of the given entry
Definition: Controller.cc:604
bool hasDisk(const sdirno dirn=-1, const sfileno filen=-1) const
Definition: store.cc:1982
sdirno swap_dirn
Definition: Store.h:226
struct SquidConfig::@96 Swap
#define EBIT_TEST(flag, bit)
Definition: defines.h:107
void setCollapsingRequirement(const bool required)
allow or forbid collapsed requests feeding
Definition: store.cc:2066
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:157
virtual void evictIfFound(const cache_key *) override
Definition: Controller.cc:480
bool hasMemStore() const
whether there is a corresponding locked shared memory table entry
Definition: Store.h:201
summary view of all disk caches (cache_dirs) combined
Definition: Disks.h:18
bool markedForDeletion(const cache_key *key) const
Definition: Controller.cc:303
SQUIDCEXTERN void hashFreeMemory(hash_table *)
Definition: hash.cc:272
static size_t inUseCount()
Definition: store.cc:177
#define assert(EX)
Definition: assert.h:19
virtual void getStats(StoreInfoStats &stats) const override
collect statistics
Definition: Controller.cc:115
FREE destroyStoreEntry
virtual void stat(StoreEntry &) const override
Definition: Controller.cc:141
size_t store_pages_max
StoreSearch * NewLocalSearch()
Definition: LocalSearch.cc:44
static size_t inUseCount()
Definition: MemObject.cc:48
void syncCollapsed(const sfileno)
Update local intransit entry after changes made by appending worker.
Definition: Controller.cc:762
hash_table * store_table
pid_t WaitForAnyPid(PidStatus &status, int flags)
Definition: tools.h:111
mem_status_t mem_status
Definition: Store.h:228
StoreSearch * search()
Definition: Controller.cc:215
static int store_dirs_rebuilding
the number of cache_dirs being rebuilt; TODO: move to Disks::Rebuilding
Definition: Controller.h:139
time_t squid_curtime
Definition: stub_time.cc:17
void transientsClearCollapsingRequirement(StoreEntry &e)
removes collapsing requirement (for future hits)
Definition: Controller.cc:639
bool anchorToCache(StoreEntry &e, bool &inSync)
Definition: Controller.cc:861
size_t maxInMemObjSize
Definition: SquidConfig.h:268
signed_int32_t sfileno
Definition: forward.h:22
int64_t store_maxobjsize
void(* Dereferenced)(RemovalPolicy *policy, const StoreEntry *entry, RemovalPolicyNode *node)
Definition: RemovalPolicy.h:49
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:912
int lowWaterMark
Definition: SquidConfig.h:80
virtual uint64_t currentSize() const override
current size
Definition: Controller.cc:177
void setMemStatus(mem_status_t)
Definition: store.cc:1572
static constexpr Io ioDone
Definition: MemObject.h:167
virtual uint64_t maxSize() const override
Definition: Controller.cc:163
static size_t InUseCount()
Definition: mem_node.cc:55
bool appliedUpdates
Definition: MemObject.h:90
virtual void init() override
Definition: MemStore.cc:171
bool keepForLocalMemoryCache(StoreEntry &e) const
whether e should be kept in local RAM for possible future caching
Definition: Controller.cc:551
KeyScope
Definition: store_key_md5.h:18
struct SquidConfig::@110 Store
virtual int64_t maxObjectSize() const override
the maximum size of a storable object; -1 if unlimited
Definition: Controller.cc:191
StoreEntry * findCallbackXXX(const cache_key *)
Definition: Controller.cc:374
void updateOnNotModified(StoreEntry *old, StoreEntry &e304)
using a 304 response, update the old entry (metadata and reply headers)
Definition: Controller.cc:692
an std::runtime_error with thrower location info
Definition: TextException.h:19
int PidStatus
Definition: tools.h:94
static RefCount< Controller > TheRoot
Definition: Controller.cc:901
virtual void maintain() override
perform regular periodic maintenance; TODO: move to UFSSwapDir::Maintain
Definition: Controller.cc:93
void handleIdleEntry(StoreEntry &)
called when the entry is no longer needed by any transaction
Definition: Controller.cc:646
void unlinkRequest()
Definition: MemObject.h:56
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
StoreEntry * peek(const cache_key *)
Definition: Controller.cc:406
const char * storeKeyText(const cache_key *key)
void allowSharing(StoreEntry &, const cache_key *)
indexes and adds SMP-tracking for an ephemeral peek() result
Definition: Controller.cc:346
bool transientsWriter(const StoreEntry &) const
whether the entry is in "writing to Transients" I/O state
Definition: Controller.cc:453
bool hasTransients() const
whether there is a corresponding locked transients table entry
Definition: Store.h:199
bool collapsed
whether the entry allows collapsing
Definition: Transients.h:35
void release(const bool shareable=false)
Definition: store.cc:1191
void hashInsert(const cache_key *)
Definition: store.cc:418
bool memoryCachable()
checkCachable() and can be cached in memory
Definition: store.cc:1325
void freeMemorySpace(const int spaceRequired)
Definition: Controller.cc:509
virtual uint64_t currentCount() const override
the total number of objects stored right now
Definition: Controller.cc:184
void trimMemory(const bool preserveSwappable)
Definition: store.cc:1867
class Ping::pingStats_ stats
bool markedForDeletionAndAbandoned(const StoreEntry &) const
Definition: Controller.cc:310
bool hittingRequiresCollapsing() const
whether this entry can feed collapsed requests and only them
Definition: Store.h:204
int locked() const
Definition: Store.h:136
RemovalPolicyNode repl
Definition: MemObject.h:200
bool swappedOut() const
whether the entire entry is now on disk (possibly marked for deletion)
Definition: Store.h:126
#define false
Definition: GnuRegex.c:233
int store_swap_high
const A & min(A const &lhs, A const &rhs)
High-level store statistics used by mgr:info action. Used inside PODs!
Definition: StoreStats.h:13
#define PROF_start(probename)
Definition: Profiler.h:62
bool dereferenceIdle(StoreEntry &, bool wantsLocalMemory)
Definition: Controller.cc:273
class SquidConfig Config
Definition: SquidConfig.cc:12
virtual ~Controller() override
Definition: Controller.cc:45
Controller & Root()
safely access controller singleton
Definition: Controller.cc:905

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors