]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/StoreMap.h
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_IPC_STORE_MAP_H
10 #define SQUID_IPC_STORE_MAP_H
12 #include "ipc/mem/FlexibleArray.h"
13 #include "ipc/mem/Pointer.h"
14 #include "ipc/ReadWriteLock.h"
21 typedef int32_t StoreMapSliceId
;
23 /// a piece of Store entry, linked to other pieces, forming a chain
24 /// slices may be appended by writers while readers read the entry
28 typedef uint32_t Size
;
30 StoreMapSlice(): size(0), next(-1) {}
32 Atomic::WordT
<Size
> size
; ///< slice contents size
33 Atomic::WordT
<StoreMapSliceId
> next
; ///< ID of the next entry slice
36 /// Maintains shareable information about a StoreEntry as a whole.
37 /// An anchor points to one or more StoreEntry slices. This is the
38 /// only lockable part of shared StoreEntry information, providing
39 /// protection for all StoreEntry slices.
45 /// store StoreEntry key and basics for an inode slot
46 void set(const StoreEntry
&anEntry
);
48 void setKey(const cache_key
*const aKey
);
49 bool sameKey(const cache_key
*const aKey
) const;
51 /// undo the effects of set(), setKey(), etc., but keep locks and state
54 /* entry state may change immediately after calling these methods unless
55 * the caller holds an appropriate lock */
56 bool empty() const { return !key
[0] && !key
[1]; }
57 bool reading() const { return lock
.readers
; }
58 bool writing() const { return lock
.writing
; }
59 bool complete() const { return !empty() && !writing(); }
62 mutable ReadWriteLock lock
; ///< protects slot data below
63 Atomic::WordT
<uint8_t> waitingToBeFreed
; ///< may be accessed w/o a lock
65 // fields marked with [app] can be modified when appending-while-reading
67 uint64_t key
[2]; ///< StoreEntry key
69 // STORE_META_STD TLV field from StoreEntry
75 Atomic::WordT
<uint64_t> swap_file_sz
; // [app]
80 /// where the chain of StoreEntry slices begins [app]
81 Atomic::WordT
<StoreMapSliceId
> start
;
84 /// an array of shareable Items
85 /// must be the last data member or, if used as a parent class, the last parent
91 typedef Ipc::Mem::Owner
< StoreMapItems
<Item
> > Owner
;
93 explicit StoreMapItems(const int aCapacity
): capacity(aCapacity
), items(aCapacity
) {}
95 size_t sharedMemorySize() const { return SharedMemorySize(capacity
); }
96 static size_t SharedMemorySize(const int aCapacity
) { return sizeof(StoreMapItems
<Item
>) + aCapacity
*sizeof(Item
); }
98 const int capacity
; ///< total number of items
99 Ipc::Mem::FlexibleArray
<Item
> items
; ///< storage
102 /// StoreMapSlices indexed by their slice ID.
103 typedef StoreMapItems
<StoreMapSlice
> StoreMapSlices
;
105 /// StoreMapAnchors indexed by entry fileno plus
106 /// sharing-safe basic housekeeping info about Store entries
107 class StoreMapAnchors
110 typedef Ipc::Mem::Owner
< StoreMapAnchors
> Owner
;
112 explicit StoreMapAnchors(const int aCapacity
);
114 size_t sharedMemorySize() const;
115 static size_t SharedMemorySize(const int anAnchorLimit
);
117 Atomic::Word count
; ///< current number of entries
118 Atomic::WordT
<uint32_t> victim
; ///< starting point for purge search
119 const int capacity
; ///< total number of anchors
120 Ipc::Mem::FlexibleArray
<StoreMapAnchor
> items
; ///< anchors storage
122 // TODO: Find an elegant way to use StoreMapItems in StoreMapAnchors
124 class StoreMapCleaner
;
126 /// Manages shared Store index (e.g., locking/unlocking/freeing entries) using
127 /// StoreMapAnchors indexed by their keys and
128 /// StoreMapSlices indexed by their slide ID.
132 typedef StoreMapAnchor Anchor
;
133 typedef StoreMapAnchors Anchors
;
134 typedef sfileno AnchorId
;
135 typedef StoreMapSlice Slice
;
136 typedef StoreMapSlices Slices
;
137 typedef StoreMapSliceId SliceId
;
140 /// aggregates anchor and slice owners for Init() caller convenience
146 Anchors::Owner
*anchors
;
147 Slices::Owner
*slices
;
149 Owner(const Owner
&); // not implemented
150 Owner
&operator =(const Owner
&); // not implemented
153 /// initialize shared memory
154 static Owner
*Init(const SBuf
&path
, const int slotLimit
);
156 StoreMap(const SBuf
&aPath
);
158 /// computes map entry position for a given entry key
159 sfileno
anchorIndexByKey(const cache_key
*const key
) const;
161 /// Like strcmp(mapped, new), but for store entry versions/timestamps.
162 /// Returns +2 if the mapped entry does not exist; -1/0/+1 otherwise.
163 /// Comparison may be inaccurate unless the caller is a lock holder.
164 int compareVersions(const sfileno oldFileno
, time_t newVersion
) const;
166 /// finds, locks, and returns an anchor for an empty key position,
167 /// erasing the old entry (if any)
168 Anchor
*openForWriting(const cache_key
*const key
, sfileno
&fileno
);
169 /// locks and returns an anchor for the empty fileno position; if
170 /// overwriteExisting is false and the position is not empty, returns nil
171 Anchor
*openForWritingAt(sfileno fileno
, bool overwriteExisting
= true);
172 /// restrict opened for writing entry to appending operations; allow reads
173 void startAppending(const sfileno fileno
);
174 /// successfully finish creating or updating the entry at fileno pos
175 void closeForWriting(const sfileno fileno
, bool lockForReading
= false);
176 /// unlock and "forget" openForWriting entry, making it Empty again
177 /// this call does not free entry slices so the caller has to do that
178 void forgetWritingEntry(const sfileno fileno
);
180 /// only works on locked entries; returns nil unless the slice is readable
181 const Anchor
*peekAtReader(const sfileno fileno
) const;
183 /// only works on locked entries; returns the corresponding Anchor
184 const Anchor
&peekAtEntry(const sfileno fileno
) const;
186 /// free the entry if possible or mark it as waiting to be freed if not
187 void freeEntry(const sfileno fileno
);
188 /// free the entry if possible or mark it as waiting to be freed if not
189 /// does nothing if we cannot check that the key matches the cached entry
190 void freeEntryByKey(const cache_key
*const key
);
192 /// opens entry (identified by key) for reading, increments read level
193 const Anchor
*openForReading(const cache_key
*const key
, sfileno
&fileno
);
194 /// opens entry (identified by sfileno) for reading, increments read level
195 const Anchor
*openForReadingAt(const sfileno fileno
);
196 /// closes open entry after reading, decrements read level
197 void closeForReading(const sfileno fileno
);
199 /// writeable slice within an entry chain created by openForWriting()
200 Slice
&writeableSlice(const AnchorId anchorId
, const SliceId sliceId
);
201 /// readable slice within an entry chain opened by openForReading()
202 const Slice
&readableSlice(const AnchorId anchorId
, const SliceId sliceId
) const;
203 /// writeable anchor for the entry created by openForWriting()
204 Anchor
&writeableEntry(const AnchorId anchorId
);
205 /// readable anchor for the entry created by openForReading()
206 const Anchor
&readableEntry(const AnchorId anchorId
) const;
208 /// stop writing the entry, freeing its slot for others to use if possible
209 void abortWriting(const sfileno fileno
);
211 /// either finds and frees an entry with at least 1 slice or returns false
214 /// copies slice to its designated position
215 void importSlice(const SliceId sliceId
, const Slice
&slice
);
217 /* SwapFilenMax limits the number of entries, but not slices or slots */
218 bool validEntry(const int n
) const; ///< whether n is a valid slice coordinate
219 bool validSlice(const int n
) const; ///< whether n is a valid slice coordinate
220 int entryCount() const; ///< number of writeable and readable entries
221 int entryLimit() const; ///< maximum entryCount() possible
222 int sliceLimit() const; ///< maximum number of slices possible
224 /// adds approximate current stats to the supplied ones
225 void updateStats(ReadWriteLockStats
&stats
) const;
227 StoreMapCleaner
*cleaner
; ///< notified before a readable entry is freed
230 const SBuf path
; ///< cache_dir path or similar cache name; for logging
231 Mem::Pointer
<StoreMapAnchors
> anchors
; ///< entry inodes (starting blocks)
232 Mem::Pointer
<StoreMapSlices
> slices
; ///< chained entry pieces positions
235 Anchor
&anchorAt(const sfileno fileno
);
236 const Anchor
&anchorAt(const sfileno fileno
) const;
237 Anchor
&anchorByKey(const cache_key
*const key
);
239 Slice
&sliceAt(const SliceId sliceId
);
240 const Slice
&sliceAt(const SliceId sliceId
) const;
241 Anchor
*openForReading(Slice
&s
);
243 void freeChain(const sfileno fileno
, Anchor
&inode
, const bool keepLock
);
246 /// API for adjusting external state when dirty map slice is being freed
247 class StoreMapCleaner
250 virtual ~StoreMapCleaner() {}
252 /// adjust slice-linked state before a locked Readable slice is erased
253 virtual void noteFreeMapSlice(const StoreMapSliceId sliceId
) = 0;
258 // We do not reuse FileMap because we cannot control its size,
259 // resulting in sfilenos that are pointing beyond the database.
261 #endif /* SQUID_IPC_STORE_MAP_H */