MemStore.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 #ifndef SQUID_SRC_MEMSTORE_H
10 #define SQUID_SRC_MEMSTORE_H
11 
12 #include "ipc/mem/Page.h"
13 #include "ipc/mem/PageStack.h"
14 #include "ipc/StoreMap.h"
15 #include "Store.h"
16 #include "store/Controlled.h"
17 
18 // StoreEntry restoration info not already stored by Ipc::StoreMap
21 };
24 
25 class ShmWriter;
26 
30 {
31 public:
32  MemStore();
33  ~MemStore() override;
34 
36  bool keepInLocalMemory(const StoreEntry &e) const;
37 
39  void write(StoreEntry &e);
40 
42  void completeWriting(StoreEntry &e);
43 
45  void disconnect(StoreEntry &e);
46 
47  /* Storage API */
48  void create() override {}
49  void init() override;
50  StoreEntry *get(const cache_key *) override;
51  uint64_t maxSize() const override;
52  uint64_t minSize() const override;
53  uint64_t currentSize() const override;
54  uint64_t currentCount() const override;
55  int64_t maxObjectSize() const override;
56  void getStats(StoreInfoStats &stats) const override;
57  void stat(StoreEntry &e) const override;
58  void reference(StoreEntry &e) override;
59  bool dereference(StoreEntry &e) override;
60  void updateHeaders(StoreEntry *e) override;
61  void maintain() override;
62  bool anchorToCache(StoreEntry &) override;
63  bool updateAnchored(StoreEntry &) override;
64  void evictCached(StoreEntry &) override;
65  void evictIfFound(const cache_key *) override;
66 
68  static bool Enabled() { return EntryLimit() > 0; }
69  static int64_t EntryLimit();
72  static bool Requested();
73 
74 protected:
75  friend ShmWriter;
76 
77  bool shouldCache(StoreEntry &e) const;
78  bool startCaching(StoreEntry &e);
79 
80  void copyToShm(StoreEntry &e);
82  bool copyFromShm(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
83  void copyFromShmSlice(StoreEntry &, const StoreIOBuffer &);
84 
86 
87  void anchorEntry(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
89 
91  Ipc::StoreMap::Slice &nextAppendableSlice(const sfileno entryIndex, sfileno &sliceOffset);
93 
94  // Ipc::StoreMapCleaner API
95  void noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId) override;
96 
97 private:
98  // TODO: move freeSlots into map
101 
104 
107 
110  {
111  public:
112  SlotAndPage(): slot(nullptr), page(nullptr) {}
113  bool operator !() const { return !slot && !page; }
116  };
118 };
119 
120 // Why use Store as a base? MemStore and SwapDir are both "caches".
121 
122 // Why not just use a SwapDir API? That would not help much because Store has
123 // to check/update memory cache separately from the disk cache. And same API
124 // would hurt because we can support synchronous get/put, unlike the disks.
125 
126 #endif /* SQUID_SRC_MEMSTORE_H */
127 
bool updateAnchored(StoreEntry &) override
Definition: MemStore.cc:423
void updateHeaders(StoreEntry *e) override
make stored metadata and HTTP headers the same as in the given entry
Definition: MemStore.cc:342
Ipc::StoreMap MemStoreMap
Definition: MemStore.h:23
bool startCaching(StoreEntry &e)
locks map anchor and preps to store the entry in shared memory
Definition: MemStore.cc:649
MemStore()
Definition: MemStore.cc:164
StoreEntry * get(const cache_key *) override
Definition: MemStore.cc:304
void create() override
create system resources needed for this store to operate in the future
Definition: MemStore.h:48
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 keepInLocalMemory(const StoreEntry &e) const
whether e should be kept in local RAM for possible future caching
Shared memory page identifier, address, or handler.
Definition: Page.h:23
friend ShmWriter
Definition: MemStore.h:75
void stat(StoreEntry &e) const override
Definition: MemStore.cc:218
void maintain() override
perform regular periodic maintenance; TODO: move to UFSSwapDir::Maintain
Definition: MemStore.cc:256
sfileno lastWritingSlice
the last allocate slice for writing a store entry (during copyToShm)
Definition: MemStore.h:106
bool anchorToCache(StoreEntry &) override
Definition: MemStore.cc:402
uint64_t currentSize() const override
current size
Definition: MemStore.cc:273
uint64_t minSize() const override
the minimum size the store will shrink to via normal housekeeping
Definition: MemStore.cc:261
void getStats(StoreInfoStats &stats) const override
collect statistics
Definition: MemStore.cc:205
static bool Requested()
Definition: MemStore.cc:951
uint64_t maxSize() const override
Definition: MemStore.cc:267
SlotAndPage waitingFor
a cache for a single "hot" free slot and page
Definition: MemStore.h:117
void copyToShmSlice(StoreEntry &e, Ipc::StoreMapAnchor &anchor, Ipc::StoreMap::Slice &slice)
copies at most one slice worth of local memory to shared memory
Definition: MemStore.cc:708
Ipc::Mem::PageId page
shared memory page with entry slice content
Definition: MemStore.h:20
void copyToShm(StoreEntry &e)
copies all local data to shared memory
Definition: MemStore.cc:672
void evictIfFound(const cache_key *) override
Definition: MemStore.cc:923
int32_t StoreMapSliceId
Definition: StoreMap.h:24
void completeWriting(StoreEntry &e)
all data has been received; there will be no more write() calls
Definition: MemStore.cc:886
API for adjusting external state when dirty map slice is being freed.
Definition: StoreMap.h:397
~MemStore() override
Definition: MemStore.cc:168
uint64_t currentCount() const override
the total number of objects stored right now
Definition: MemStore.cc:280
void evictCached(StoreEntry &) override
Definition: MemStore.cc:904
void noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId) override
adjust slice-linked state before a locked Readable slice is erased
Definition: MemStore.cc:825
Aggregates information required for updating entry metadata and headers.
Definition: StoreMap.h:181
void anchorEntry(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor)
anchors StoreEntry to an already locked map entry
Definition: MemStore.cc:446
Ipc::StoreMapItems< MemStoreMapExtraItem > MemStoreMapExtras
Definition: MemStore.h:22
signed_int32_t sfileno
Definition: forward.h:22
Ipc::Mem::Pointer< Ipc::Mem::PageStack > freeSlots
unused map slot IDs
Definition: MemStore.h:99
sfileno reserveSapForWriting(Ipc::Mem::PageId &page)
finds a slot and a free page to fill or throws
Definition: MemStore.cc:786
MemStoreMap * map
index of mem-cached entries
Definition: MemStore.h:100
void init() override
Definition: MemStore.cc:174
void updateHeadersOrThrow(Ipc::StoreMapUpdate &update)
Definition: MemStore.cc:362
bool operator!() const
Definition: MemStore.h:113
bool copyFromShm(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor)
copies the entire entry from shared to local memory
Definition: MemStore.cc:471
Ipc::Mem::PageId * slot
local slot variable, waiting to be filled
Definition: MemStore.h:114
void write(StoreEntry &e)
copy non-shared entry data of the being-cached entry to our cache
Definition: MemStore.cc:847
void copyFromShmSlice(StoreEntry &, const StoreIOBuffer &)
imports one shared memory slice into local memory
Definition: MemStore.cc:567
static int64_t EntryLimit()
calculates maximum number of entries we need to store and map
Definition: MemStore.cc:958
temporary storage for slot and page ID pointers; for the waiting cache
Definition: MemStore.h:109
bool dereference(StoreEntry &e) override
Definition: MemStore.cc:297
MemStoreMapExtras Extras
Definition: MemStore.h:102
Ipc::StoreMap::Slice & nextAppendableSlice(const sfileno entryIndex, sfileno &sliceOffset)
Definition: MemStore.cc:739
bool updateAnchoredWith(StoreEntry &, const sfileno, const Ipc::StoreMapAnchor &)
updates Transients entry after its anchor has been located
Definition: MemStore.cc:437
Ipc::Mem::PageId pageForSlice(Ipc::StoreMapSliceId sliceId)
safely returns a previously allocated memory page for the given entry slice
Definition: MemStore.cc:775
void disconnect(StoreEntry &e)
called when the entry is about to forget its association with mem cache
Definition: MemStore.cc:930
Ipc::Mem::PageId * page
local page variable, waiting to be filled
Definition: MemStore.h:115
Ipc::Mem::Pointer< Extras > extras
IDs of pages with slice data.
Definition: MemStore.h:103
void reference(StoreEntry &e) override
somebody needs this entry (many cache replacement policies need to know)
Definition: MemStore.cc:292
bool shouldCache(StoreEntry &e) const
whether we should cache the entry
Definition: MemStore.cc:582
int64_t maxObjectSize() const override
the maximum size of a storable object; -1 if unlimited
Definition: MemStore.cc:286
High-level store statistics used by mgr:info action. Used inside PODs!
Definition: StoreStats.h:13

 

Introduction

Documentation

Support

Miscellaneous