19 #ifndef PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_CACHE_H_
20 #define PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_CACHE_H_
31 #include "pagespeed/kernel/base/thread_annotations.h"
35 namespace net_instaweb {
37 class AbstractSharedMem;
38 class AbstractSharedMemSegment;
41 class SharedMemCacheDump;
45 template<
size_t kBlockSize>
61 int entries_per_sector,
int blocks_per_sector,
92 int block_entry_ratio,
94 int* entries_per_sector_out,
95 int* blocks_per_sector_out,
100 return (blocks_per_sector_ * kBlockSize) / 8;
117 SharedMemCacheDump* dest);
126 static void DemarshalSnapshot(
const StringPiece& marshaled,
127 SharedMemCacheDump* out);
150 int checkpoint_interval_sec);
152 StringPiece snapshot_path()
const {
return snapshot_path_; }
153 FileCache* file_cache()
const {
return file_cache_; }
155 int64 GetLastWriteMsForTesting(
int sector_num);
156 void SetLastWriteMsForTesting(
int sector_num, int64 last_checkpoint_ms);
158 void WriteOutSnapshotForTesting(
int sector_num, int64 last_checkpoint_ms) {
159 WriteOutSnapshotFromWorkerThread(sector_num, last_checkpoint_ms);
163 class WriteOutSnapshotFunction;
171 bool InitCache(
bool parent);
179 void PutRawHash(
const GoogleString& raw_hash, int64 last_use_timestamp_ms,
180 const SharedString& value,
bool checkpoint_ok);
186 SharedMemCacheData::Sector<kBlockSize>* sector,
187 SharedMemCacheData::EntryNum entry_num,
188 Callback* str) EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
193 void PutIntoEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
194 SharedMemCacheData::EntryNum entry_num,
195 int64 last_use_timestamp_ms,
const SharedString& value)
196 EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
199 void DeleteEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
200 SharedMemCacheData::EntryNum entry_num)
201 EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
209 bool TryAllocateBlocks(SharedMemCacheData::Sector<kBlockSize>* sector,
210 int goal, SharedMemCacheData::BlockVector* blocks)
211 EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
215 void MarkEntryFree(SharedMemCacheData::Sector<kBlockSize>* sector,
216 SharedMemCacheData::EntryNum entry_num);
219 void TouchEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
220 int64 last_use_timestamp_ms,
221 SharedMemCacheData::EntryNum entry_num);
225 bool Writeable(
const SharedMemCacheData::CacheEntry* entry);
227 bool KeyMatch(SharedMemCacheData::CacheEntry* entry,
233 void ExtractPosition(
const GoogleString& raw_hash, Position* out_pos);
237 void EnsureReadyForWriting(SharedMemCacheData::Sector<kBlockSize>* sector,
238 SharedMemCacheData::CacheEntry* entry)
239 EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
242 void RestoreFromDisk();
246 void ScheduleSnapshotIfNecessary(
bool checkpoint_ok,
247 int64 last_use_timestamp_ms,
248 int64 last_checkpoint_ms,
254 void ScheduleSnapshot(
int sector_num, int64 last_checkpoint_ms);
257 void WriteOutSnapshotFromWorkerThread(
int sector_num,
258 int64 last_checkpoint_ms);
265 AbstractSharedMem* shm_runtime_;
266 const Hasher* hasher_;
270 int entries_per_sector_;
271 int blocks_per_sector_;
272 int checkpoint_interval_sec_;
273 MessageHandler* handler_;
275 FileCache* file_cache_;
277 scoped_ptr<AbstractSharedMemSegment> segment_;
278 std::vector<SharedMemCacheData::Sector<kBlockSize>*> sectors_;
virtual bool IsHealthy() const
Definition: shared_mem_cache.h:136
Abstract interface for a cache.
Definition: cache_interface.h:32
static void MarshalSnapshot(const SharedMemCacheDump &dump, GoogleString *out)
Encode/Decode SharedMemCacheDump objects.
virtual void Get(const GoogleString &key, Callback *callback)
virtual bool IsBlocking() const
Definition: shared_mem_cache.h:135
static void ComputeDimensions(int64 size_kb, int block_entry_ratio, int sectors, int *entries_per_sector_out, int *blocks_per_sector_out, int64 *size_cap_out)
KeyState
Definition: cache_interface.h:34
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void SanityCheck()
Sanity check the cache data structures.
Definition: abstract_shared_mem.h:86
static const int kAssociativity
Definition: shared_mem_cache.h:48
virtual void Put(const GoogleString &key, const SharedString &value)
void RegisterSnapshotFileCache(FileCache *potential_file_cache, int checkpoint_interval_sec)
Definition: shared_string.h:40
void RestoreSnapshot(const SharedMemCacheDump &dump)
SharedMemCache(AbstractSharedMem *shm_runtime, const GoogleString &filename, Timer *timer, const Hasher *hasher, int sectors, int entries_per_sector, int blocks_per_sector, MessageHandler *handler)
code of ExtractPosition as well.
static void GlobalCleanup(AbstractSharedMem *shm_runtime, const GoogleString &filename, MessageHandler *message_handler)
virtual void ShutDown()
Definition: shared_mem_cache.h:137
Abstract interface for a cache.
Definition: shared_mem_cache.h:46
Definition: message_handler.h:39
size_t MaxValueSize() const
Returns the largest size of an object this cache can store.
Definition: shared_mem_cache.h:99
Simple C++ implementation of file cache.
Definition: file_cache.h:42
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
bool AddSectorToSnapshot(int sector_num, int64 last_checkpoint_ms, SharedMemCacheDump *dest)
virtual GoogleString Name() const
Definition: shared_mem_cache.h:133