CernVM-FS  2.12.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 LabeledObject &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 Label &label, const int flags, void *txn);
194 
202  virtual int64_t Write(const void *buf, uint64_t size, void *txn);
203 
208  virtual int Reset(void *txn);
209 
218  virtual int OpenFromTxn(void *txn);
219 
224  virtual int AbortTxn(void *txn);
225 
236  virtual int CommitTxn(void *txn);
237 
238  virtual void Spawn() { }
239 
240  private:
241  // The null hash (hashed output is all null bytes) serves as a marker for
242  // an invalid handle
244 
245  struct ReadOnlyHandle {
248  , is_volatile(false)
249  { }
250  ReadOnlyHandle(const shash::Any &h, bool v)
251  : handle(h)
252  , is_volatile(v)
253  { }
254  bool operator ==(const ReadOnlyHandle &other) const {
255  return this->handle == other.handle;
256  }
257  bool operator !=(const ReadOnlyHandle &other) const {
258  return this->handle != other.handle;
259  }
260 
263  };
264 
265  struct Transaction {
267  : buffer()
268  , expected_size(0)
269  , pos(0) { }
271  uint64_t expected_size;
272  uint64_t pos;
273  std::string description;
274  };
275 
276  inline MemoryKvStore *GetStore(const ReadOnlyHandle &fd) {
277  if (fd.is_volatile) {
278  return &volatile_entries_;
279  } else {
280  return &regular_entries_;
281  }
282  }
283 
286  return &volatile_entries_;
287  } else {
288  return &regular_entries_;
289  }
290  }
291 
292  int AddFd(const ReadOnlyHandle &handle);
293  int64_t CommitToKvStore(Transaction *transaction);
294  virtual int DoOpen(const shash::Any &id);
295 
296  uint64_t max_size_;
298  pthread_rwlock_t rwlock_;
302 }; // class RamCacheManager
303 
304 #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:291
virtual int Open(const LabeledObject &object)
Definition: cache_ram.cc:81
perf::Counter * n_pread
Definition: cache_ram.h:49
static const shash::Any kInvalidHandle
Definition: cache_ram.h:243
perf::Counter * n_write
Definition: cache_ram.h:53
bool operator==(const ReadOnlyHandle &other) const
Definition: cache_ram.h:254
perf::Counter * n_full
Definition: cache_ram.h:62
virtual int Close(int fd)
Definition: cache_ram.cc:136
perf::Counter * n_committxn
Definition: cache_ram.h:56
virtual int CommitTxn(void *txn)
Definition: cache_ram.cc:318
MemoryKvStore * GetStore(const ReadOnlyHandle &fd)
Definition: cache_ram.h:276
Counters(perf::StatisticsTemplate statistics)
Definition: cache_ram.h:65
virtual int AbortTxn(void *txn)
Definition: cache_ram.cc:308
perf::Counter * n_starttxn
Definition: cache_ram.h:52
MemoryKvStore * GetTransactionStore(Transaction *txn)
Definition: cache_ram.h:284
virtual int Dup(int fd)
Definition: cache_ram.cc:174
Counter * RegisterTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:111
virtual void CtrlTxn(const Label &label, const int flags, void *txn)
Definition: cache_ram.cc:229
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)
Definition: cache_ram.cc:209
void Transaction()
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)
Definition: cache_ram.cc:156
virtual int Readahead(int fd)
Definition: cache_ram.cc:196
FdTable< ReadOnlyHandle > fd_table_
Definition: cache_ram.h:297
perf::Counter * n_readahead
Definition: cache_ram.h:51
perf::Counter * n_overrun
Definition: cache_ram.h:61
CacheManagerIds
Definition: cache.h:24
perf::Counter * n_realloc
Definition: cache_ram.h:63
int AddFd(const ReadOnlyHandle &handle)
Definition: cache_ram.cc:63
int64_t CommitToKvStore(Transaction *transaction)
Definition: cache_ram.cc:329
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:257
ReadOnlyHandle(const shash::Any &h, bool v)
Definition: cache_ram.h:250
perf::Counter * n_dup
Definition: cache_ram.h:50
virtual int DoOpen(const shash::Any &id)
Definition: cache_ram.cc:87
int object_flags
Definition: kvstore.h:50
perf::Counter * n_close
Definition: cache_ram.h:48
perf::Counter * n_enfile
Definition: cache_ram.h:57
static const int kLabelVolatile
Definition: cache.h:82
perf::Counter * n_openvolatile
Definition: cache_ram.h:59
virtual std::string Describe()
Definition: cache_ram.cc:23
Counters counters_
Definition: cache_ram.h:301
QuotaManager * quota_mgr()
Definition: cache.h:193
uint64_t max_size_
Definition: cache_ram.h:296
pthread_rwlock_t rwlock_
Definition: cache_ram.h:298
virtual void Spawn()
Definition: cache_ram.h:238
virtual int64_t GetSize(int fd)
Definition: cache_ram.cc:124
virtual int Reset(void *txn)
Definition: cache_ram.cc:281
virtual ~RamCacheManager()
Definition: cache_ram.cc:58
MemoryKvStore regular_entries_
Definition: cache_ram.h:299
MemoryKvStore volatile_entries_
Definition: cache_ram.h:300
virtual int64_t Write(const void *buf, uint64_t size, void *txn)
Definition: cache_ram.cc:240
static void size_t size
Definition: smalloc.h:54
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr)
Definition: cache_ram.cc:73
virtual uint32_t SizeOfTxn()
Definition: cache_ram.h:173