Page Speed Optimization Libraries  1.5.27.2
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   static GoogleString FormatName();
00107   virtual GoogleString Name() const { return FormatName();}
00108 
00109   virtual bool IsBlocking() const { return true; }
00110   virtual bool IsHealthy() const { return true; }
00111   virtual void ShutDown() {
00113   }
00114 
00116   void SanityCheck();
00117 
00118  private:
00120   struct Position {
00121     int sector;
00122     SharedMemCacheData::EntryNum keys[kAssociativity];
00123   };
00124 
00125   bool InitCache(bool parent);
00126 
00129   void GetFromEntry(const GoogleString& key,
00130                     SharedMemCacheData::Sector<kBlockSize>* sector,
00131                     SharedMemCacheData::EntryNum entry_num,
00132                     Callback* callback);
00133 
00137   void PutIntoEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
00138                     SharedMemCacheData::EntryNum entry_num,
00139                     SharedString* value);
00140 
00143   void DeleteEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
00144                    SharedMemCacheData::EntryNum entry_num);
00145 
00152   bool TryAllocateBlocks(SharedMemCacheData::Sector<kBlockSize>* sector,
00153                          int goal, SharedMemCacheData::BlockVector* blocks);
00154 
00157   void MarkEntryFree(SharedMemCacheData::Sector<kBlockSize>* sector,
00158                      SharedMemCacheData::EntryNum entry_num);
00159 
00161   void TouchEntry(SharedMemCacheData::Sector<kBlockSize>* sector,
00162                   SharedMemCacheData::EntryNum entry_num);
00163 
00166   bool Writeable(const SharedMemCacheData::CacheEntry* entry);
00167 
00168   bool KeyMatch(SharedMemCacheData::CacheEntry* entry,
00169                 const GoogleString& raw_hash);
00170 
00171   GoogleString ToRawHash(const GoogleString& key);
00172 
00174   void ExtractPosition(const GoogleString& raw_hash, Position* out_pos);
00175 
00178   void EnsureReadyForWriting(SharedMemCacheData::Sector<kBlockSize>* sector,
00179                              SharedMemCacheData::CacheEntry* entry);
00180 
00181   AbstractSharedMem* shm_runtime_;
00182   const Hasher* hasher_;
00183   Timer* timer_;
00184   GoogleString filename_;
00185   int num_sectors_;
00186   int entries_per_sector_;
00187   int blocks_per_sector_;
00188   MessageHandler* handler_;
00189 
00190   scoped_ptr<AbstractSharedMemSegment> segment_;
00191   std::vector<SharedMemCacheData::Sector<kBlockSize>*> sectors_;
00192 
00193   GoogleString name_;
00194 
00195   DISALLOW_COPY_AND_ASSIGN(SharedMemCache);
00196 };
00197 
00198 }  
00199 
00200 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_SHARED_MEM_CACHE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines