]>
Commit | Line | Data |
---|---|---|
44c95fcf AR |
1 | #ifndef SQUID_IPC_STORE_MAP_H |
2 | #define SQUID_IPC_STORE_MAP_H | |
3 | ||
4 | #include "ipc/ReadWriteLock.h" | |
3a8c5551 | 5 | #include "ipc/mem/FlexibleArray.h" |
68353d5a | 6 | #include "ipc/mem/Pointer.h" |
6d68a230 | 7 | #include "typedefs.h" |
44c95fcf | 8 | |
9199139f AR |
9 | namespace Ipc |
10 | { | |
44c95fcf | 11 | |
f13833e9 | 12 | typedef int32_t StoreMapSliceId; |
bc8b6522 AR |
13 | |
14 | /// a piece of Store entry, linked to other pieces, forming a chain | |
ce49546e | 15 | /// slices may be appended by writers while readers read the entry |
bc8b6522 AR |
16 | class StoreMapSlice |
17 | { | |
18 | public: | |
ce49546e | 19 | typedef uint32_t Size; |
bc8b6522 | 20 | |
ce49546e AR |
21 | StoreMapSlice(): size(0), next(-1) {} |
22 | ||
23 | Atomic::WordT<Size> size; ///< slice contents size | |
24 | Atomic::WordT<StoreMapSliceId> next; ///< ID of the next entry slice | |
bc8b6522 AR |
25 | }; |
26 | ||
27 | ||
50dc81ec AR |
28 | /// Maintains shareable information about a StoreEntry as a whole. |
29 | /// An anchor points to one or more StoreEntry slices. This is the | |
30 | /// only lockable part of shared StoreEntry information, providing | |
31 | /// protection for all StoreEntry slices. | |
32 | class StoreMapAnchor | |
9199139f | 33 | { |
44c95fcf | 34 | public: |
50dc81ec | 35 | StoreMapAnchor(); |
44c95fcf | 36 | |
50dc81ec | 37 | /// store StoreEntry key and basics for an inode slot |
44c95fcf AR |
38 | void set(const StoreEntry &anEntry); |
39 | ||
40 | void setKey(const cache_key *const aKey); | |
41 | bool sameKey(const cache_key *const aKey) const; | |
42 | ||
50dc81ec AR |
43 | /// undo the effects of set(), setKey(), etc., but keep locks and state |
44 | void rewind(); | |
45 | ||
ce49546e AR |
46 | /* entry state may change immediately after calling these methods unless |
47 | * the caller holds an appropriate lock */ | |
48 | bool empty() const { return !key[0] && !key[1]; } | |
49 | bool reading() const { return lock.readers; } | |
a3023c03 | 50 | bool writing() const { return lock.writing; } |
ce49546e AR |
51 | bool complete() const { return !empty() && !writing(); } |
52 | ||
44c95fcf AR |
53 | public: |
54 | mutable ReadWriteLock lock; ///< protects slot data below | |
794d4c0c | 55 | Atomic::WordT<uint8_t> waitingToBeFreed; ///< may be accessed w/o a lock |
44c95fcf | 56 | |
ce49546e AR |
57 | // fields marked with [app] can be modified when appending-while-reading |
58 | ||
44c95fcf AR |
59 | uint64_t key[2]; ///< StoreEntry key |
60 | ||
61 | // STORE_META_STD TLV field from StoreEntry | |
62 | struct Basics { | |
63 | time_t timestamp; | |
64 | time_t lastref; | |
65 | time_t expires; | |
66 | time_t lastmod; | |
ce49546e | 67 | uint64_t swap_file_sz; // [app]; XXX: make atomic |
89924985 AR |
68 | uint16_t refcount; |
69 | uint16_t flags; | |
9199139f | 70 | } basics; |
44c95fcf | 71 | |
ce49546e AR |
72 | /// where the chain of StoreEntry slices begins [app]; XXX: make atomic |
73 | StoreMapSliceId start; | |
50dc81ec | 74 | |
ce49546e | 75 | #if 0 |
44c95fcf AR |
76 | /// possible persistent states |
77 | typedef enum { | |
78 | Empty, ///< ready for writing, with nothing of value | |
79 | Writeable, ///< transitions from Empty to Readable | |
80 | Readable, ///< ready for reading | |
9199139f | 81 | } State; |
44c95fcf | 82 | State state; ///< current state |
ce49546e | 83 | #endif |
44c95fcf AR |
84 | }; |
85 | ||
6d68a230 AR |
86 | /// A hack to allocate one shared array for both anchors and slices. |
87 | /// Anchors are indexed by store entry ID and are independent from each other. | |
88 | /// Slices are indexed by slice IDs and form entry chains using slice.next. | |
50dc81ec AR |
89 | class StoreMapSlot { |
90 | public: | |
6d68a230 AR |
91 | StoreMapAnchor anchor; ///< information about store entry as a whole |
92 | StoreMapSlice slice; ///< information about one stored entry piece | |
50dc81ec AR |
93 | }; |
94 | ||
7f6748c8 AR |
95 | class StoreMapCleaner; |
96 | ||
50dc81ec | 97 | /// map of StoreMapSlots indexed by their keys, with read/write slice locking |
44c95fcf AR |
98 | /// kids extend to store custom data |
99 | class StoreMap | |
100 | { | |
101 | public: | |
50dc81ec AR |
102 | typedef StoreMapAnchor Anchor; |
103 | typedef sfileno AnchorId; | |
104 | typedef StoreMapSlice Slice; | |
105 | typedef StoreMapSliceId SliceId; | |
44c95fcf | 106 | |
f8f98441 | 107 | /// data shared across maps in different processes |
fc3921ec A |
108 | class Shared |
109 | { | |
f8f98441 | 110 | public: |
68353d5a DK |
111 | Shared(const int aLimit, const size_t anExtrasSize); |
112 | size_t sharedMemorySize() const; | |
113 | static size_t SharedMemorySize(const int limit, const size_t anExtrasSize); | |
114 | ||
50dc81ec AR |
115 | const int limit; ///< maximum number of store entries |
116 | const size_t extrasSize; ///< size of slice extra data | |
117 | Atomic::Word count; ///< current number of entries | |
118 | Atomic::WordT<sfileno> victim; ///< starting point for purge search | |
119 | Ipc::Mem::FlexibleArray<StoreMapSlot> slots; ///< storage | |
68353d5a DK |
120 | }; |
121 | ||
122 | public: | |
123 | typedef Mem::Owner<Shared> Owner; | |
124 | ||
125 | /// initialize shared memory | |
126 | static Owner *Init(const char *const path, const int limit); | |
127 | ||
128 | StoreMap(const char *const aPath); | |
44c95fcf | 129 | |
50dc81ec AR |
130 | /// computes map entry position for a given entry key |
131 | sfileno anchorIndexByKey(const cache_key *const key) const; | |
132 | ||
133 | /// Like strcmp(mapped, new), but for store entry versions/timestamps. | |
134 | /// Returns +2 if the mapped entry does not exist; -1/0/+1 otherwise. | |
135 | /// Comparison may be inaccurate unless the caller is a lock holder. | |
136 | int compareVersions(const sfileno oldFileno, time_t newVersion) const; | |
137 | ||
138 | /// finds, locks, and returns an anchor for an empty key position, | |
139 | /// erasing the old entry (if any) | |
140 | Anchor *openForWriting(const cache_key *const key, sfileno &fileno); | |
141 | /// locks and returns an anchor for the empty fileno position; if | |
142 | /// overwriteExisting is false and the position is not empty, returns nil | |
143 | Anchor *openForWritingAt(sfileno fileno, bool overwriteExisting = true); | |
ce49546e AR |
144 | /// restrict opened for writing entry to appending operations; allow reads |
145 | void startAppending(const sfileno fileno); | |
50dc81ec | 146 | /// successfully finish creating or updating the entry at fileno pos |
44c95fcf | 147 | void closeForWriting(const sfileno fileno, bool lockForReading = false); |
50dc81ec AR |
148 | /// unlock and "forget" openForWriting entry, making it Empty again |
149 | /// this call does not free entry slices so the caller has to do that | |
150 | void forgetWritingEntry(const sfileno fileno); | |
151 | ||
152 | /// only works on locked entries; returns nil unless the slice is readable | |
153 | const Anchor *peekAtReader(const sfileno fileno) const; | |
154 | ||
ce49546e | 155 | /// free the entry if possible or mark it as waiting to be freed if not |
50dc81ec | 156 | void freeEntry(const sfileno fileno); |
ce49546e AR |
157 | /// free the entry if possible or mark it as waiting to be freed if not |
158 | /// does nothing if we cannot check that the key matches the cached entry | |
159 | void freeEntryByKey(const cache_key *const key); | |
50dc81ec AR |
160 | |
161 | /// opens entry (identified by key) for reading, increments read level | |
162 | const Anchor *openForReading(const cache_key *const key, sfileno &fileno); | |
163 | /// opens entry (identified by sfileno) for reading, increments read level | |
164 | const Anchor *openForReadingAt(const sfileno fileno); | |
165 | /// closes open entry after reading, decrements read level | |
166 | void closeForReading(const sfileno fileno); | |
44c95fcf | 167 | |
50dc81ec AR |
168 | /// writeable slice within an entry chain created by openForWriting() |
169 | Slice &writeableSlice(const AnchorId anchorId, const SliceId sliceId); | |
170 | /// readable slice within an entry chain opened by openForReading() | |
171 | const Slice &readableSlice(const AnchorId anchorId, const SliceId sliceId) const; | |
172 | /// writeable anchor for the entry created by openForWriting() | |
173 | Anchor &writeableEntry(const AnchorId anchorId); | |
ce49546e AR |
174 | /// readable anchor for the entry created by openForReading() |
175 | const Anchor &readableEntry(const AnchorId anchorId) const; | |
44c95fcf | 176 | |
a3023c03 AR |
177 | /// stop writing the entry, freeing its slot for others to use if possible |
178 | void abortWriting(const sfileno fileno); | |
44c95fcf | 179 | |
50dc81ec AR |
180 | /// finds an unlocked entry and frees it or returns false |
181 | bool purgeOne(); | |
44c95fcf | 182 | |
50dc81ec AR |
183 | /// copies slice to its designated position |
184 | void importSlice(const SliceId sliceId, const Slice &slice); | |
44c95fcf | 185 | |
50dc81ec AR |
186 | bool full() const; ///< there are no empty slices left; XXX: remove as unused? |
187 | bool valid(const int n) const; ///< whether n is a valid slice coordinate | |
188 | int entryCount() const; ///< number of writeable and readable entries | |
189 | int entryLimit() const; ///< maximum entryCount() possible | |
44c95fcf AR |
190 | |
191 | /// adds approximate current stats to the supplied ones | |
192 | void updateStats(ReadWriteLockStats &stats) const; | |
193 | ||
7f6748c8 AR |
194 | StoreMapCleaner *cleaner; ///< notified before a readable entry is freed |
195 | ||
44c95fcf | 196 | protected: |
68353d5a | 197 | static Owner *Init(const char *const path, const int limit, const size_t extrasSize); |
44c95fcf | 198 | |
920378ba | 199 | const String path; ///< cache_dir path or similar cache name; for logging |
68353d5a | 200 | Mem::Pointer<Shared> shared; |
44c95fcf AR |
201 | |
202 | private: | |
50dc81ec | 203 | Anchor &anchorByKey(const cache_key *const key); |
44c95fcf | 204 | |
50dc81ec | 205 | Anchor *openForReading(Slice &s); |
50dc81ec AR |
206 | |
207 | void freeChain(const sfileno fileno, Anchor &inode, const bool keepLock); | |
68353d5a | 208 | }; |
44c95fcf | 209 | |
50dc81ec | 210 | /// StoreMap with extra slice data |
68353d5a DK |
211 | /// Note: ExtrasT must be POD, it is initialized with zeroes, no |
212 | /// constructors or destructors are called | |
213 | template <class ExtrasT> | |
214 | class StoreMapWithExtras: public StoreMap | |
215 | { | |
216 | public: | |
217 | typedef ExtrasT Extras; | |
218 | ||
219 | /// initialize shared memory | |
220 | static Owner *Init(const char *const path, const int limit); | |
221 | ||
222 | StoreMapWithExtras(const char *const path); | |
223 | ||
224 | /// write access to the extras; call openForWriting() first! | |
225 | ExtrasT &extras(const sfileno fileno); | |
226 | /// read-only access to the extras; call openForReading() first! | |
227 | const ExtrasT &extras(const sfileno fileno) const; | |
228 | ||
229 | protected: | |
230 | ||
231 | ExtrasT *sharedExtras; ///< pointer to extras in shared memory | |
44c95fcf AR |
232 | }; |
233 | ||
50dc81ec | 234 | /// API for adjusting external state when dirty map slice is being freed |
7f6748c8 AR |
235 | class StoreMapCleaner |
236 | { | |
237 | public: | |
238 | virtual ~StoreMapCleaner() {} | |
239 | ||
50dc81ec AR |
240 | /// adjust slice-linked state before a locked Readable slice is erased |
241 | virtual void noteFreeMapSlice(const sfileno sliceId) = 0; | |
7f6748c8 AR |
242 | }; |
243 | ||
68353d5a DK |
244 | // StoreMapWithExtras implementation |
245 | ||
246 | template <class ExtrasT> | |
247 | StoreMap::Owner * | |
248 | StoreMapWithExtras<ExtrasT>::Init(const char *const path, const int limit) | |
249 | { | |
250 | return StoreMap::Init(path, limit, sizeof(Extras)); | |
251 | } | |
252 | ||
253 | template <class ExtrasT> | |
5f621cd0 AJ |
254 | StoreMapWithExtras<ExtrasT>::StoreMapWithExtras(const char *const aPath): |
255 | StoreMap(aPath) | |
68353d5a DK |
256 | { |
257 | const size_t sharedSizeWithoutExtras = | |
258 | Shared::SharedMemorySize(entryLimit(), 0); | |
259 | sharedExtras = reinterpret_cast<Extras *>(reinterpret_cast<char *>(shared.getRaw()) + sharedSizeWithoutExtras); | |
260 | } | |
261 | ||
262 | template <class ExtrasT> | |
263 | ExtrasT & | |
264 | StoreMapWithExtras<ExtrasT>::extras(const sfileno fileno) | |
265 | { | |
266 | return const_cast<ExtrasT &>(const_cast<const StoreMapWithExtras *>(this)->extras(fileno)); | |
267 | } | |
268 | ||
269 | template <class ExtrasT> | |
270 | const ExtrasT & | |
271 | StoreMapWithExtras<ExtrasT>::extras(const sfileno fileno) const | |
272 | { | |
273 | assert(sharedExtras); | |
274 | assert(valid(fileno)); | |
275 | return sharedExtras[fileno]; | |
276 | } | |
277 | ||
44c95fcf AR |
278 | } // namespace Ipc |
279 | ||
75f8f9a2 | 280 | // We do not reuse FileMap because we cannot control its size, |
44c95fcf AR |
281 | // resulting in sfilenos that are pointing beyond the database. |
282 | ||
283 | #endif /* SQUID_IPC_STORE_MAP_H */ |