Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_mem_cache_data.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 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 
21 
22 #ifndef PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_CACHE_DATA_H_
23 #define PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_CACHE_DATA_H_
24 
25 #include <cstddef>
26 #include <vector>
27 
28 #include "base/logging.h"
32 #include "pagespeed/kernel/base/thread_annotations.h"
33 
34 namespace net_instaweb {
35 
36 class AbstractMutex;
37 class AbstractSharedMem;
38 class AbstractSharedMemSegment;
39 class MessageHandler;
40 
41 namespace SharedMemCacheData {
42 
43 typedef int32 EntryNum;
44 typedef int32 BlockNum;
45 typedef std::vector<BlockNum> BlockVector;
46 
47 const BlockNum kInvalidBlock = -1;
48 const EntryNum kInvalidEntry = -1;
49 const size_t kHashSize = 16;
50 
51 struct SectorStats {
52  SectorStats();
53 
61  int64 num_put;
64  int64 num_put_concurrent_create;
65  int64 num_put_concurrent_full_set;
66  int64 num_put_spins;
67  int64 num_get;
68  int64 num_get_hit;
70 
72  int64 used_entries;
73  int64 used_blocks;
74 
76  void Add(const SectorStats& other);
77 
79  GoogleString Dump(size_t total_entries, size_t total_blocks) const;
80 };
81 
82 struct SectorHeader {
83  BlockNum free_list_front;
84  EntryNum lru_list_front;
85  EntryNum lru_list_rear;
86  int32 padding;
87 
88  SectorStats stats;
89 
91 };
92 
93 struct CacheEntry {
94  char hash_bytes[kHashSize];
95  int64 last_use_timestamp_ms;
96  int32 byte_size;
97 
101  EntryNum lru_prev;
102  EntryNum lru_next;
103 
104  BlockNum first_block;
105 
107  bool creating : 1;
108 
110  uint32 open_count : 31;
111 
112  uint32 padding;
113 };
114 
120 template<size_t kBlockSize>
121 class Sector {
122  public:
132  Sector(AbstractSharedMemSegment* segment, size_t sector_offset,
133  size_t cache_entries, size_t data_blocks);
134  ~Sector();
135 
142  bool Attach(MessageHandler* handler);
143 
147  bool Initialize(MessageHandler* handler);
148 
151  static size_t RequiredSize(AbstractSharedMem* shmem_runtime,
152  size_t cache_entries, size_t data_blocks);
153 
155 
157  AbstractMutex* mutex() const LOCK_RETURNED(mutex_) { return mutex_.get(); }
158 
161  BlockNum GetBlockSuccessor(BlockNum block) EXCLUSIVE_LOCKS_REQUIRED(mutex()) {
162  DCHECK_GE(block, 0);
163  DCHECK_LT(block, static_cast<BlockNum>(data_blocks_));
164  return block_successors_[block];
165  }
166 
167  void SetBlockSuccessor(BlockNum block, BlockNum next)
168  EXCLUSIVE_LOCKS_REQUIRED(mutex()) {
169  DCHECK_GE(block, 0);
170  DCHECK_LT(block, static_cast<BlockNum>(data_blocks_));
171 
172  DCHECK_GE(next, kInvalidBlock);
173  DCHECK_LT(next, static_cast<BlockNum>(data_blocks_));
174 
175  block_successors_[block] = next;
176  }
177 
180  void LinkBlockSuccessors(const BlockVector& blocks)
181  EXCLUSIVE_LOCKS_REQUIRED(mutex()) {
182  for (size_t pos = 0; pos < blocks.size(); ++pos) {
183  if (pos == (blocks.size() - 1)) {
184  SetBlockSuccessor(blocks[pos], kInvalidBlock);
185  } else {
186  SetBlockSuccessor(blocks[pos], blocks[pos + 1]);
187  }
188  }
189  }
190 
193 
200  int AllocBlocksFromFreeList(int goal, BlockVector* blocks)
201  EXCLUSIVE_LOCKS_REQUIRED(mutex());
202 
206  void ReturnBlocksToFreeList(const BlockVector& blocks)
207  EXCLUSIVE_LOCKS_REQUIRED(mutex());
208 
211 
213  CacheEntry* EntryAt(EntryNum slot) {
214  return reinterpret_cast<CacheEntry*>(directory_base_) + slot;
215  }
216 
219  void InsertEntryIntoLRU(EntryNum entry_num);
220 
222  void UnlinkEntryFromLRU(EntryNum entry_num);
223 
224  EntryNum OldestEntryNum() {
225  return sector_header_->lru_list_rear;
226  }
227 
230 
231  char* BlockBytes(BlockNum block_num) {
232  return blocks_base_ + kBlockSize * block_num;
233  }
234 
238 
240  static size_t DataBlocksForSize(size_t size) {
241  return NeededPieces(size, kBlockSize);
242  }
243 
247  static size_t BytesInPortion(size_t total_bytes, size_t b, size_t total);
248 
251  int BlockListForEntry(CacheEntry* entry, BlockVector* out_blocks)
252  EXCLUSIVE_LOCKS_REQUIRED(mutex());
253 
256 
257  SectorStats* sector_stats() { return &sector_header_->stats; }
258 
261  void DumpStats(MessageHandler* handler);
262 
263  private:
265  struct MemLayout;
266 
268  static size_t NeededPieces(size_t total, size_t piece_size) {
269  return (total + piece_size - 1) / piece_size;
270  }
271 
273  size_t cache_entries_;
274  size_t data_blocks_;
275 
277  AbstractSharedMemSegment* segment_;
279  SectorHeader* sector_header_;
280  BlockNum* block_successors_ PT_GUARDED_BY(mutex());
281  char* directory_base_;
282  char* blocks_base_;
283  size_t sector_offset_;
284 
285 
286 };
287 
288 }
289 
290 }
291 
292 #endif
void DumpStats(MessageHandler *handler)
static size_t RequiredSize(AbstractSharedMem *shmem_runtime, size_t cache_entries, size_t data_blocks)
Definition: shared_mem_cache_data.h:121
int64 used_entries
Current state stats — updated by SharedMemCacheData.
Definition: shared_mem_cache_data.h:72
int64 num_put
Definition: shared_mem_cache_data.h:61
Sector(AbstractSharedMemSegment *segment, size_t sector_offset, size_t cache_entries, size_t data_blocks)
void InsertEntryIntoLRU(EntryNum entry_num)
static size_t BytesInPortion(size_t total_bytes, size_t b, size_t total)
void Add(const SectorStats &other)
Adds number to this object's. No concurrency control is done.
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
CacheEntry * EntryAt(EntryNum slot)
Returns the given # entry.
Definition: shared_mem_cache_data.h:213
int64 num_put_replace
replacement of different key
Definition: shared_mem_cache_data.h:63
uint32 open_count
Number of readers currently accessing the data.
Definition: shared_mem_cache_data.h:110
bool Attach(MessageHandler *handler)
bool Initialize(MessageHandler *handler)
Definition: shared_mem_cache_data.h:93
int64 num_put_update
update of the same key
Definition: shared_mem_cache_data.h:62
Definition: scoped_ptr.h:30
static size_t DataBlocksForSize(size_t size)
Number of blocks of data needed for size blocks.
Definition: shared_mem_cache_data.h:240
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void ReturnBlocksToFreeList(const BlockVector &blocks) EXCLUSIVE_LOCKS_REQUIRED(mutex())
int64 last_checkpoint_ms
When this sector was last checkpointed to disk.
Definition: shared_mem_cache_data.h:69
int64 num_put_spins
of times writers had to sleep behind readers
Definition: shared_mem_cache_data.h:66
Definition: abstract_shared_mem.h:86
Definition: shared_mem_cache_data.h:82
int AllocBlocksFromFreeList(int goal, BlockVector *blocks) EXCLUSIVE_LOCKS_REQUIRED(mutex())
uint32 padding
ensures we're 8-aligned.
Definition: shared_mem_cache_data.h:112
void UnlinkEntryFromLRU(EntryNum entry_num)
Removes from the LRU. Safe to call if not in the LRU already.
void LinkBlockSuccessors(const BlockVector &blocks) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Definition: shared_mem_cache_data.h:180
EntryNum lru_prev
Definition: shared_mem_cache_data.h:101
Definition: message_handler.h:39
int BlockListForEntry(CacheEntry *entry, BlockVector *out_blocks) EXCLUSIVE_LOCKS_REQUIRED(mutex())
BlockNum GetBlockSuccessor(BlockNum block) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Definition: shared_mem_cache_data.h:161
Definition: abstract_shared_mem.h:31
Definition: shared_mem_cache_data.h:51
GoogleString Dump(size_t total_entries, size_t total_blocks) const
Text dump of the statistics. No concurrency control is done.
AbstractMutex * mutex() const LOCK_RETURNED(mutex_)
Mutex ops.
Definition: shared_mem_cache_data.h:157
char * BlockBytes(BlockNum block_num)
Definition: shared_mem_cache_data.h:231
bool creating
When this is true, someone is trying to overwrite this entry.
Definition: shared_mem_cache_data.h:107
int64 num_get
of calls to get
Definition: shared_mem_cache_data.h:67
SectorStats * sector_stats()
Definition: shared_mem_cache_data.h:257