CernVM-FS  2.11.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_ram.h
Go to the documentation of this file.
1 
4 #ifndef CVMFS_CACHE_RAM_H_
5 #define CVMFS_CACHE_RAM_H_
6 
7 #include <pthread.h>
8 
9 #include <cassert>
10 #include <cstdlib>
11 #include <string>
12 #include <vector>
13 
14 #include "cache.h"
15 #include "crypto/hash.h"
16 #include "fd_table.h"
17 #include "kvstore.h"
18 #include "statistics.h"
19 #include "util/pointer.h"
20 
21 
44 class RamCacheManager : public CacheManager {
45  public:
46  struct Counters {
64 
65  explicit Counters(perf::StatisticsTemplate statistics) {
66  n_getsize = statistics.RegisterTemplated("n_getsize",
67  "Number of GetSize calls");
68  n_close = statistics.RegisterTemplated("n_close",
69  "Number of Close calls");
70  n_pread = statistics.RegisterTemplated("n_pread",
71  "Number of Pread calls");
72  n_dup = statistics.RegisterTemplated("n_dup",
73  "Number of Dup calls");
74  n_readahead = statistics.RegisterTemplated("n_readahead",
75  "Number of ReadAhead calls");
76  n_starttxn = statistics.RegisterTemplated("n_starttxn",
77  "Number of StartTxn calls");
78  n_write = statistics.RegisterTemplated("n_write",
79  "Number of Write calls");
80  n_reset = statistics.RegisterTemplated("n_reset",
81  "Number of Reset calls");
82  n_aborttxn = statistics.RegisterTemplated("n_aborttxn",
83  "Number of AbortTxn calls");
84  n_committxn = statistics.RegisterTemplated("n_committxn",
85  "Number of Commit calls");
86  n_enfile = statistics.RegisterTemplated("n_enfile",
87  "Number of times the limit on handles was reached");
88  n_openregular = statistics.RegisterTemplated("n_openregular",
89  "Number of opens from the regular cache");
90  n_openvolatile = statistics.RegisterTemplated("n_openvolatile",
91  "Number of opens from the volatile cache");
92  n_openmiss = statistics.RegisterTemplated("n_openmiss",
93  "Number of missed opens");
94  n_realloc = statistics.RegisterTemplated("n_realloc",
95  "Number of reallocs");
96  n_overrun = statistics.RegisterTemplated("n_overrun",
97  "Number of cache limit overruns");
98  n_full = statistics.RegisterTemplated("n_full",
99  "Number of overruns that could not be resolved");
100  }
101  };
102 
103  virtual CacheManagerIds id() { return kRamCacheManager; }
104  virtual std::string Describe();
105 
107  uint64_t max_size,
108  unsigned max_entries,
110  perf::StatisticsTemplate statistics);
111 
112  virtual ~RamCacheManager();
113 
115 
125  virtual int Open(const BlessedObject &object);
126 
133  virtual int64_t GetSize(int fd);
134 
141  virtual int Close(int fd);
142 
150  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
151 
160  virtual int Dup(int fd);
161 
167  virtual int Readahead(int fd);
168 
169 
173  virtual uint32_t SizeOfTxn() { return sizeof(Transaction); }
174 
175 
183  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn);
184 
193  virtual void CtrlTxn(const ObjectInfo &object_info,
194  const int flags,
195  void *txn);
196 
204  virtual int64_t Write(const void *buf, uint64_t size, void *txn);
205 
210  virtual int Reset(void *txn);
211 
220  virtual int OpenFromTxn(void *txn);
221 
226  virtual int AbortTxn(void *txn);
227 
238  virtual int CommitTxn(void *txn);
239 
240  virtual void Spawn() { }
241 
242  private:
243  // The null hash (hashed output is all null bytes) serves as a marker for
244  // an invalid handle
246 
247  struct ReadOnlyHandle {
250  , is_volatile(false)
251  { }
252  ReadOnlyHandle(const shash::Any &h, bool v)
253  : handle(h)
254  , is_volatile(v)
255  { }
256  bool operator ==(const ReadOnlyHandle &other) const {
257  return this->handle == other.handle;
258  }
259  bool operator !=(const ReadOnlyHandle &other) const {
260  return this->handle != other.handle;
261  }
262 
265  };
266 
267  struct Transaction {
269  : buffer()
270  , expected_size(0)
271  , pos(0) { }
273  uint64_t expected_size;
274  uint64_t pos;
275  std::string description;
276  };
277 
278  inline MemoryKvStore *GetStore(const ReadOnlyHandle &fd) {
279  if (fd.is_volatile) {
280  return &volatile_entries_;
281  } else {
282  return &regular_entries_;
283  }
284  }
285 
287  if (txn->buffer.object_type == kTypeVolatile) {
288  return &volatile_entries_;
289  } else {
290  return &regular_entries_;
291  }
292  }
293 
294  int AddFd(const ReadOnlyHandle &handle);
295  int64_t CommitToKvStore(Transaction *transaction);
296  virtual int DoOpen(const shash::Any &id);
297 
298  uint64_t max_size_;
300  pthread_rwlock_t rwlock_;
304 }; // class RamCacheManager
305 
306 #endif // CVMFS_CACHE_RAM_H_
perf::Counter * n_aborttxn
Definition: cache_ram.h:55
perf::Counter * n_openmiss
Definition: cache_ram.h:60
virtual CacheManagerIds id()
Definition: cache_ram.h:103
RamCacheManager(uint64_t max_size, unsigned max_entries, MemoryKvStore::MemoryAllocator alloc, perf::StatisticsTemplate statistics)
Definition: cache_ram.cc:29
perf::Counter * n_getsize
Definition: cache_ram.h:47
virtual int OpenFromTxn(void *txn)
Definition: cache_ram.cc:290
virtual void CtrlTxn(const ObjectInfo &object_info, const int flags, void *txn)
Definition: cache_ram.cc:226
perf::Counter * n_pread
Definition: cache_ram.h:49
static const shash::Any kInvalidHandle
Definition: cache_ram.h:245
perf::Counter * n_write
Definition: cache_ram.h:53
bool operator==(const ReadOnlyHandle &other) const
Definition: cache_ram.h:256
perf::Counter * n_full
Definition: cache_ram.h:62
virtual int Close(int fd)
Definition: cache_ram.cc:133
perf::Counter * n_committxn
Definition: cache_ram.h:56
virtual int CommitTxn(void *txn)
Definition: cache_ram.cc:317
MemoryKvStore * GetStore(const ReadOnlyHandle &fd)
Definition: cache_ram.h:278
Counters(perf::StatisticsTemplate statistics)
Definition: cache_ram.h:65
virtual int AbortTxn(void *txn)
Definition: cache_ram.cc:307
perf::Counter * n_starttxn
Definition: cache_ram.h:52
MemoryKvStore * GetTransactionStore(Transaction *txn)
Definition: cache_ram.h:286
virtual int Dup(int fd)
Definition: cache_ram.cc:171
Counter * RegisterTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:111
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)
Definition: cache_ram.cc:206
void Transaction()
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)
Definition: cache_ram.cc:153
virtual int Readahead(int fd)
Definition: cache_ram.cc:193
FdTable< ReadOnlyHandle > fd_table_
Definition: cache_ram.h:299
perf::Counter * n_readahead
Definition: cache_ram.h:51
perf::Counter * n_overrun
Definition: cache_ram.h:61
CacheManagerIds
Definition: cache.h:23
perf::Counter * n_realloc
Definition: cache_ram.h:63
int AddFd(const ReadOnlyHandle &handle)
Definition: cache_ram.cc:60
int64_t CommitToKvStore(Transaction *transaction)
Definition: cache_ram.cc:328
perf::Counter * n_openregular
Definition: cache_ram.h:58
perf::Counter * n_reset
Definition: cache_ram.h:54
bool operator!=(const ReadOnlyHandle &other) const
Definition: cache_ram.h:259
ReadOnlyHandle(const shash::Any &h, bool v)
Definition: cache_ram.h:252
perf::Counter * n_dup
Definition: cache_ram.h:50
virtual int DoOpen(const shash::Any &id)
Definition: cache_ram.cc:84
perf::Counter * n_close
Definition: cache_ram.h:48
perf::Counter * n_enfile
Definition: cache_ram.h:57
perf::Counter * n_openvolatile
Definition: cache_ram.h:59
virtual std::string Describe()
Definition: cache_ram.cc:23
CacheManager::ObjectType object_type
Definition: kvstore.h:50
Counters counters_
Definition: cache_ram.h:303
QuotaManager * quota_mgr()
Definition: cache.h:198
uint64_t max_size_
Definition: cache_ram.h:298
virtual int Open(const BlessedObject &object)
Definition: cache_ram.cc:78
pthread_rwlock_t rwlock_
Definition: cache_ram.h:300
virtual void Spawn()
Definition: cache_ram.h:240
virtual int64_t GetSize(int fd)
Definition: cache_ram.cc:121
virtual int Reset(void *txn)
Definition: cache_ram.cc:280
virtual ~RamCacheManager()
Definition: cache_ram.cc:55
MemoryKvStore regular_entries_
Definition: cache_ram.h:301
MemoryKvStore volatile_entries_
Definition: cache_ram.h:302
virtual int64_t Write(const void *buf, uint64_t size, void *txn)
Definition: cache_ram.cc:239
static void size_t size
Definition: smalloc.h:54
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr)
Definition: cache_ram.cc:70
virtual uint32_t SizeOfTxn()
Definition: cache_ram.h:173