]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/rock/RockSwapDir.h
Stop wasting 96 RAM bytes per slot for high-offset slots in large shared caches
[thirdparty/squid.git] / src / fs / rock / RockSwapDir.h
1 #ifndef SQUID_FS_ROCK_SWAP_DIR_H
2 #define SQUID_FS_ROCK_SWAP_DIR_H
3
4 #include "DiskIO/DiskFile.h"
5 #include "DiskIO/IORequestor.h"
6 #include "fs/rock/RockDbCell.h"
7 #include "fs/rock/RockForward.h"
8 #include "ipc/mem/Page.h"
9 #include "ipc/mem/PageStack.h"
10 #include "ipc/StoreMap.h"
11 #include "SwapDir.h"
12
13 class DiskIOStrategy;
14 class ReadRequest;
15 class WriteRequest;
16
17 namespace Rock
18 {
19
20 /// \ingroup Rock
21 class SwapDir: public ::SwapDir, public IORequestor, public Ipc::StoreMapCleaner
22 {
23 public:
24 typedef RefCount<SwapDir> Pointer;
25 typedef Ipc::StoreMap DirMap;
26
27 SwapDir();
28 virtual ~SwapDir();
29
30 /* public ::SwapDir API */
31 virtual void reconfigure();
32 virtual StoreSearch *search(String const url, HttpRequest *);
33 virtual StoreEntry *get(const cache_key *key);
34 virtual void get(String const, STOREGETCLIENT, void * cbdata);
35 virtual void markForUnlink(StoreEntry &e);
36 virtual void disconnect(StoreEntry &e);
37 virtual uint64_t currentSize() const;
38 virtual uint64_t currentCount() const;
39 virtual bool doReportStat() const;
40 virtual void swappedOut(const StoreEntry &e);
41 virtual void create();
42 virtual void parse(int index, char *path);
43
44 // temporary path to the shared memory map of first slots of cached entries
45 SBuf inodeMapPath() const;
46 // temporary path to the shared memory stack of free slots
47 const char *freeSlotsPath() const;
48
49 int64_t entryLimitAbsolute() const { return SwapFilenMax+1; } ///< Core limit
50 int64_t entryLimitActual() const; ///< max number of possible entries in db
51 int64_t slotLimitAbsolute() const; ///< Rock store implementation limit
52 int64_t slotLimitActual() const; ///< total number of slots in this db
53
54 /// removes a slot from a list of free slots or returns false
55 bool useFreeSlot(Ipc::Mem::PageId &pageId);
56 /// whether the given slot ID may point to a slot in this db
57 bool validSlotId(const SlotId slotId) const;
58 /// purges one or more entries to make full() false and free some slots
59 void purgeSome();
60
61 int64_t diskOffset(Ipc::Mem::PageId &pageId) const;
62 int64_t diskOffset(int filen) const;
63 void writeError(StoreEntry &e);
64
65 /* StoreMapCleaner API */
66 virtual void noteFreeMapSlice(const Ipc::StoreMapSliceId fileno);
67
68 uint64_t slotSize; ///< all db slots are of this size
69
70 protected:
71 /* Store API */
72 virtual bool anchorCollapsed(StoreEntry &collapsed, bool &inSync);
73 virtual bool updateCollapsed(StoreEntry &collapsed);
74
75 /* protected ::SwapDir API */
76 virtual bool needsDiskStrand() const;
77 virtual void init();
78 virtual ConfigOption *getOptionTree() const;
79 virtual bool allowOptionReconfigure(const char *const option) const;
80 virtual bool canStore(const StoreEntry &e, int64_t diskSpaceNeeded, int &load) const;
81 virtual StoreIOState::Pointer createStoreIO(StoreEntry &, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
82 virtual StoreIOState::Pointer openStoreIO(StoreEntry &, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
83 virtual void maintain();
84 virtual void diskFull();
85 virtual void reference(StoreEntry &e);
86 virtual bool dereference(StoreEntry &e, bool);
87 virtual bool unlinkdUseful() const;
88 virtual void unlink(StoreEntry &e);
89 virtual void statfs(StoreEntry &e) const;
90
91 /* IORequestor API */
92 virtual void ioCompletedNotification();
93 virtual void closeCompleted();
94 virtual void readCompleted(const char *buf, int len, int errflag, RefCount< ::ReadRequest>);
95 virtual void writeCompleted(int errflag, size_t len, RefCount< ::WriteRequest>);
96
97 void parseSize(const bool reconfiguring); ///< parses anonymous cache_dir size option
98 void validateOptions(); ///< warns of configuration problems; may quit
99 bool parseTimeOption(char const *option, const char *value, int reconfiguring);
100 void dumpTimeOption(StoreEntry * e) const;
101 bool parseRateOption(char const *option, const char *value, int reconfiguring);
102 void dumpRateOption(StoreEntry * e) const;
103 bool parseSizeOption(char const *option, const char *value, int reconfiguring);
104 void dumpSizeOption(StoreEntry * e) const;
105
106 void rebuild(); ///< starts loading and validating stored entry metadata
107
108 bool full() const; ///< no more entries can be stored without purging
109 void trackReferences(StoreEntry &e); ///< add to replacement policy scope
110 void ignoreReferences(StoreEntry &e); ///< delete from repl policy scope
111
112 int64_t diskOffsetLimit() const;
113
114 void anchorEntry(StoreEntry &e, const sfileno filen, const Ipc::StoreMapAnchor &anchor);
115 bool updateCollapsedWith(StoreEntry &collapsed, const Ipc::StoreMapAnchor &anchor);
116
117 friend class Rebuild;
118 friend class IoState;
119 const char *filePath; ///< location of cache storage file inside path/
120 DirMap *map; ///< entry key/sfileno to MaxExtras/inode mapping
121
122 private:
123 void createError(const char *const msg);
124
125 DiskIOStrategy *io;
126 RefCount<DiskFile> theFile; ///< cache storage for this cache_dir
127 Ipc::Mem::Pointer<Ipc::Mem::PageStack> freeSlots; ///< all unused slots
128 Ipc::Mem::PageId *waitingForPage; ///< one-page cache for a "hot" free slot
129
130 /* configurable options */
131 DiskFile::Config fileConfig; ///< file-level configuration options
132
133 static const int64_t HeaderSize; ///< on-disk db header size
134 };
135
136 /// initializes shared memory segments used by Rock::SwapDir
137 class SwapDirRr: public Ipc::Mem::RegisteredRunner
138 {
139 public:
140 /* ::RegisteredRunner API */
141 virtual ~SwapDirRr();
142
143 protected:
144 /* Ipc::Mem::RegisteredRunner API */
145 virtual void create();
146
147 private:
148 Vector<SwapDir::DirMap::Owner *> mapOwners;
149 Vector< Ipc::Mem::Owner<Ipc::Mem::PageStack> *> freeSlotsOwners;
150 };
151
152 } // namespace Rock
153
154 #endif /* SQUID_FS_ROCK_SWAP_DIR_H */