Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_mem_cache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_CACHE_H_
20 #define PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_CACHE_H_
21 
22 #include <cstddef>
23 #include <vector>
24 
31 #include "pagespeed/kernel/base/thread_annotations.h"
34 
35 namespace net_instaweb {
36 
37 class AbstractSharedMem;
38 class AbstractSharedMemSegment;
39 class Hasher;
40 class MessageHandler;
41 class SharedMemCacheDump;
42 class Timer;
43 
45 template<size_t kBlockSize>
47  public:
48  static const int kAssociativity = 4;
49 
59  SharedMemCache(AbstractSharedMem* shm_runtime, const GoogleString& filename,
60  Timer* timer, const Hasher* hasher, int sectors,
61  int entries_per_sector, int blocks_per_sector,
62  MessageHandler* handler);
63 
64  virtual ~SharedMemCache();
65 
72  bool Initialize();
73 
77  bool Attach();
78 
81  static void GlobalCleanup(AbstractSharedMem* shm_runtime,
82  const GoogleString& filename,
83  MessageHandler* message_handler);
84 
91  static void ComputeDimensions(int64 size_kb,
92  int block_entry_ratio,
93  int sectors,
94  int* entries_per_sector_out,
95  int* blocks_per_sector_out,
96  int64* size_cap_out);
97 
99  size_t MaxValueSize() const {
100  return (blocks_per_sector_ * kBlockSize) / 8;
101  }
102 
107 
116  bool AddSectorToSnapshot(int sector_num, int64 last_checkpoint_ms,
117  SharedMemCacheDump* dest);
118 
121  void RestoreSnapshot(const SharedMemCacheDump& dump);
122 
124  static void MarshalSnapshot(const SharedMemCacheDump& dump,
125  GoogleString* out);
126  static void DemarshalSnapshot(const StringPiece& marshaled,
127  SharedMemCacheDump* out);
128 
129  virtual void Get(const GoogleString& key, Callback* callback);
130  virtual void Put(const GoogleString& key, const SharedString& value);
131  virtual void Delete(const GoogleString& key);
132  static GoogleString FormatName();
133  virtual GoogleString Name() const { return FormatName();}
134 
135  virtual bool IsBlocking() const { return true; }
136  virtual bool IsHealthy() const { return true; }
137  virtual void ShutDown() {
139  }
140 
142  void SanityCheck();
143 
149  void RegisterSnapshotFileCache(FileCache* potential_file_cache,
150  int checkpoint_interval_sec);
151 
152  StringPiece snapshot_path() const { return snapshot_path_; }
153  FileCache* file_cache() const { return file_cache_; }
154 
155  int64 GetLastWriteMsForTesting(int sector_num);
156  void SetLastWriteMsForTesting(int sector_num, int64 last_checkpoint_ms);
157 
158  void WriteOutSnapshotForTesting(int sector_num, int64 last_checkpoint_ms) {
159  WriteOutSnapshotFromWorkerThread(sector_num, last_checkpoint_ms);
160  }
161 
162  private:
163  class WriteOutSnapshotFunction;
164 
166  struct Position {
167  int sector;
168  SharedMemCacheData::EntryNum keys[kAssociativity];
169  };
170 
171  bool InitCache(bool parent);
172 
179  void PutRawHash(const GoogleString& raw_hash, int64 last_use_timestamp_ms,
180  const SharedString& value, bool checkpoint_ok);
181 
184  CacheInterface::KeyState GetFromEntry(
185  const GoogleString& key,
186  SharedMemCacheData::Sector<kBlockSize>* sector,
187  SharedMemCacheData::EntryNum entry_num,
188  Callback* str) EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
189 
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());
197 
199  void DeleteEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
200  SharedMemCacheData::EntryNum entry_num)
201  EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
202 
209  bool TryAllocateBlocks(SharedMemCacheData::Sector<kBlockSize>* sector,
210  int goal, SharedMemCacheData::BlockVector* blocks)
211  EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
212 
215  void MarkEntryFree(SharedMemCacheData::Sector<kBlockSize>* sector,
216  SharedMemCacheData::EntryNum entry_num);
217 
219  void TouchEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
220  int64 last_use_timestamp_ms,
221  SharedMemCacheData::EntryNum entry_num);
222 
225  bool Writeable(const SharedMemCacheData::CacheEntry* entry);
226 
227  bool KeyMatch(SharedMemCacheData::CacheEntry* entry,
228  const GoogleString& raw_hash);
229 
230  GoogleString ToRawHash(const GoogleString& key);
231 
233  void ExtractPosition(const GoogleString& raw_hash, Position* out_pos);
234 
237  void EnsureReadyForWriting(SharedMemCacheData::Sector<kBlockSize>* sector,
238  SharedMemCacheData::CacheEntry* entry)
239  EXCLUSIVE_LOCKS_REQUIRED(sector->mutex());
240 
242  void RestoreFromDisk();
243 
246  void ScheduleSnapshotIfNecessary(bool checkpoint_ok,
247  int64 last_use_timestamp_ms,
248  int64 last_checkpoint_ms,
249  int sector_num);
250 
254  void ScheduleSnapshot(int sector_num, int64 last_checkpoint_ms);
255 
257  void WriteOutSnapshotFromWorkerThread(int sector_num,
258  int64 last_checkpoint_ms);
259 
263  GoogleString SnapshotCacheKey(int sector_num) const;
264 
265  AbstractSharedMem* shm_runtime_;
266  const Hasher* hasher_;
267  Timer* timer_;
268  GoogleString filename_;
269  int num_sectors_;
270  int entries_per_sector_;
271  int blocks_per_sector_;
272  int checkpoint_interval_sec_;
273  MessageHandler* handler_;
274  GoogleString snapshot_path_;
275  FileCache* file_cache_;
276 
277  scoped_ptr<AbstractSharedMemSegment> segment_;
278  std::vector<SharedMemCacheData::Sector<kBlockSize>*> sectors_;
279 
280  GoogleString name_;
281 
282 
283 };
284 
285 }
286 
287 #endif
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
Definition: hasher.h:30
bool AddSectorToSnapshot(int sector_num, int64 last_checkpoint_ms, SharedMemCacheDump *dest)
virtual GoogleString Name() const
Definition: shared_mem_cache.h:133