Page Speed Optimization Libraries  1.3.25.1
net/instaweb/util/public/shared_mem_cache.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_SHARED_MEM_CACHE_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_SHARED_MEM_CACHE_H_
00021 
00022 #include <cstddef>
00023 #include <vector>
00024 
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/util/public/cache_interface.h"
00027 #include "net/instaweb/util/public/scoped_ptr.h"
00028 #include "net/instaweb/util/public/string.h"
00029 
00030 namespace net_instaweb {
00031 
00032 class AbstractSharedMem;
00033 class AbstractSharedMemSegment;
00034 class Hasher;
00035 class MessageHandler;
00036 class SharedString;
00037 class Timer;
00038 
00039 namespace SharedMemCacheData {
00040 
00041 typedef int32 EntryNum;
00042 typedef int32 BlockNum;
00043 typedef std::vector<BlockNum> BlockVector;
00044 struct CacheEntry;
00045 template<size_t kBlockSize> class Sector;
00046 
00047 }  
00048 
00050 template<size_t kBlockSize>
00051 class SharedMemCache : public CacheInterface {
00052  public:
00053   static const int kAssociativity = 4; 
00054 
00055 
00062   SharedMemCache(AbstractSharedMem* shm_runtime, const GoogleString& filename,
00063                  Timer* timer, const Hasher* hasher, int sectors,
00064                  int entries_per_sector, int blocks_per_sector,
00065                  MessageHandler* handler);
00066 
00067   virtual ~SharedMemCache();
00068 
00072   bool Initialize();
00073 
00077   bool Attach();
00078 
00081   static void GlobalCleanup(AbstractSharedMem* shm_runtime,
00082                             const GoogleString& filename,
00083                             MessageHandler* message_handler);
00084 
00091   static void ComputeDimensions(int64 size_kb,
00092                                 int block_entry_ratio,
00093                                 int sectors,
00094                                 int* entries_per_sector_out,
00095                                 int* blocks_per_sector_out,
00096                                 int64* size_cap_out);
00097 
00101   GoogleString DumpStats();
00102 
00103   virtual void Get(const GoogleString& key, Callback* callback);
00104   virtual void Put(const GoogleString& key, SharedString* value);
00105   virtual void Delete(const GoogleString& key);
00106   virtual const char* Name() const;
00107 
00108   virtual bool IsBlocking() const { return true; }
00109   virtual bool IsHealthy() const { return true; }
00110   virtual void ShutDown() {
00112   }
00113 
00115   void SanityCheck();
00116 
00117  private:
00119   struct Position {
00120     int sector;
00121     SharedMemCacheData::EntryNum keys[kAssociativity];
00122   };
00123 
00124   bool InitCache(bool parent);
00125 
00128   void GetFromEntry(const GoogleString& key,
00129                     SharedMemCacheData::Sector<kBlockSize>* sector,
00130                     SharedMemCacheData::EntryNum entry_num,
00131                     Callback* callback);
00132 
00136   void PutIntoEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
00137                     SharedMemCacheData::EntryNum entry_num,
00138                     SharedString* value);
00139 
00142   void DeleteEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
00143                    SharedMemCacheData::EntryNum entry_num);
00144 
00151   bool TryAllocateBlocks(SharedMemCacheData::Sector<kBlockSize>* sector,
00152                          int goal, SharedMemCacheData::BlockVector* blocks);
00153 
00156   void MarkEntryFree(SharedMemCacheData::Sector<kBlockSize>* sector,
00157                      SharedMemCacheData::EntryNum entry_num);
00158 
00160   void TouchEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
00161                   SharedMemCacheData::EntryNum entry_num);
00162 
00165   bool Writeable(const SharedMemCacheData::CacheEntry* entry);
00166 
00167   bool KeyMatch(SharedMemCacheData::CacheEntry* entry,
00168                 const GoogleString& raw_hash);
00169 
00170   GoogleString ToRawHash(const GoogleString& key);
00171 
00173   void ExtractPosition(const GoogleString& raw_hash, Position* out_pos);
00174 
00177   void EnsureReadyForWriting(SharedMemCacheData::Sector<kBlockSize>* sector,
00178                              SharedMemCacheData::CacheEntry* entry);
00179 
00180   AbstractSharedMem* shm_runtime_;
00181   const Hasher* hasher_;
00182   Timer* timer_;
00183   GoogleString filename_;
00184   int num_sectors_;
00185   int entries_per_sector_;
00186   int blocks_per_sector_;
00187   MessageHandler* handler_;
00188 
00189   scoped_ptr<AbstractSharedMemSegment> segment_;
00190   std::vector<SharedMemCacheData::Sector<kBlockSize>*> sectors_;
00191 
00192   GoogleString name_;
00193 
00194   DISALLOW_COPY_AND_ASSIGN(SharedMemCache);
00195 };
00196 
00197 }  
00198 
00199 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_SHARED_MEM_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines