CernVM-FS  2.13.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", "Number of Dup calls");
73  n_readahead = statistics.RegisterTemplated("n_readahead",
74  "Number of ReadAhead calls");
75  n_starttxn = statistics.RegisterTemplated("n_starttxn",
76  "Number of StartTxn calls");
77  n_write = statistics.RegisterTemplated("n_write",
78  "Number of Write calls");
79  n_reset = statistics.RegisterTemplated("n_reset",
80  "Number of Reset calls");
81  n_aborttxn = statistics.RegisterTemplated("n_aborttxn",
82  "Number of AbortTxn calls");
83  n_committxn = statistics.RegisterTemplated("n_committxn",
84  "Number of Commit calls");
85  n_enfile = statistics.RegisterTemplated(
86  "n_enfile", "Number of times the limit on handles was reached");
87  n_openregular = statistics.RegisterTemplated(
88  "n_openregular", "Number of opens from the regular cache");
89  n_openvolatile = statistics.RegisterTemplated(
90  "n_openvolatile", "Number of opens from the volatile cache");
91  n_openmiss = statistics.RegisterTemplated("n_openmiss",
92  "Number of missed opens");
93  n_realloc = statistics.RegisterTemplated("n_realloc",
94  "Number of reallocs");
95  n_overrun = statistics.RegisterTemplated(
96  "n_overrun", "Number of cache limit overruns");
97  n_full = statistics.RegisterTemplated(
98  "n_full", "Number of overruns that could not be resolved");
99  }
100  };
101 
102  virtual CacheManagerIds id() { return kRamCacheManager; }
103  virtual std::string Describe();
104 
105  RamCacheManager(uint64_t max_size,
106  unsigned max_entries,
108  perf::StatisticsTemplate statistics);
109 
110  virtual ~RamCacheManager();
111 
113 
123  virtual int Open(const LabeledObject &object);
124 
131  virtual int64_t GetSize(int fd);
132 
139  virtual int Close(int fd);
140 
148  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
149 
158  virtual int Dup(int fd);
159 
165  virtual int Readahead(int fd);
166 
167 
171  virtual uint32_t SizeOfTxn() { return sizeof(Transaction); }
172 
173 
181  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn);
182 
191  virtual void CtrlTxn(const Label &label, const int flags, void *txn);
192 
200  virtual int64_t Write(const void *buf, uint64_t size, void *txn);
201 
206  virtual int Reset(void *txn);
207 
216  virtual int OpenFromTxn(void *txn);
217 
222  virtual int AbortTxn(void *txn);
223 
234  virtual int CommitTxn(void *txn);
235 
236  virtual void Spawn() { }
237 
238  private:
239  // The null hash (hashed output is all null bytes) serves as a marker for
240  // an invalid handle
242 
243  struct ReadOnlyHandle {
245  ReadOnlyHandle(const shash::Any &h, bool v) : handle(h), is_volatile(v) { }
246  bool operator==(const ReadOnlyHandle &other) const {
247  return this->handle == other.handle;
248  }
249  bool operator!=(const ReadOnlyHandle &other) const {
250  return this->handle != other.handle;
251  }
252 
255  };
256 
257  struct Transaction {
260  uint64_t expected_size;
261  uint64_t pos;
262  std::string description;
263  };
264 
265  inline MemoryKvStore *GetStore(const ReadOnlyHandle &fd) {
266  if (fd.is_volatile) {
267  return &volatile_entries_;
268  } else {
269  return &regular_entries_;
270  }
271  }
272 
275  return &volatile_entries_;
276  } else {
277  return &regular_entries_;
278  }
279  }
280 
281  int AddFd(const ReadOnlyHandle &handle);
282  int64_t CommitToKvStore(Transaction *transaction);
283  virtual int DoOpen(const shash::Any &id);
284 
285  uint64_t max_size_;
287  pthread_rwlock_t rwlock_;
291 }; // class RamCacheManager
292 
293 #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:102
RamCacheManager(uint64_t max_size, unsigned max_entries, MemoryKvStore::MemoryAllocator alloc, perf::StatisticsTemplate statistics)
Definition: cache_ram.cc:30
perf::Counter * n_getsize
Definition: cache_ram.h:47
virtual int OpenFromTxn(void *txn)
Definition: cache_ram.cc:283
virtual int Open(const LabeledObject &object)
Definition: cache_ram.cc:78
perf::Counter * n_pread
Definition: cache_ram.h:49
static const shash::Any kInvalidHandle
Definition: cache_ram.h:241
perf::Counter * n_write
Definition: cache_ram.h:53
bool operator==(const ReadOnlyHandle &other) const
Definition: cache_ram.h:246
perf::Counter * n_full
Definition: cache_ram.h:62
virtual int Close(int fd)
Definition: cache_ram.cc:132
perf::Counter * n_committxn
Definition: cache_ram.h:56
virtual int CommitTxn(void *txn)
Definition: cache_ram.cc:310
MemoryKvStore * GetStore(const ReadOnlyHandle &fd)
Definition: cache_ram.h:265
Counters(perf::StatisticsTemplate statistics)
Definition: cache_ram.h:65
virtual int AbortTxn(void *txn)
Definition: cache_ram.cc:300
perf::Counter * n_starttxn
Definition: cache_ram.h:52
MemoryKvStore * GetTransactionStore(Transaction *txn)
Definition: cache_ram.h:273
virtual int Dup(int fd)
Definition: cache_ram.cc:168
Counter * RegisterTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:109
virtual void CtrlTxn(const Label &label, const int flags, void *txn)
Definition: cache_ram.cc:223
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)
Definition: cache_ram.cc:204
void Transaction()
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)
Definition: cache_ram.cc:152
virtual int Readahead(int fd)
Definition: cache_ram.cc:191
FdTable< ReadOnlyHandle > fd_table_
Definition: cache_ram.h:286
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:60
int64_t CommitToKvStore(Transaction *transaction)
Definition: cache_ram.cc:322
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:249
ReadOnlyHandle(const shash::Any &h, bool v)
Definition: cache_ram.h:245
perf::Counter * n_dup
Definition: cache_ram.h:50
virtual int DoOpen(const shash::Any &id)
Definition: cache_ram.cc:84
int object_flags
Definition: kvstore.h:46
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:24
Counters counters_
Definition: cache_ram.h:290
QuotaManager * quota_mgr()
Definition: cache.h:191
uint64_t max_size_
Definition: cache_ram.h:285
pthread_rwlock_t rwlock_
Definition: cache_ram.h:287
virtual void Spawn()
Definition: cache_ram.h:236
virtual int64_t GetSize(int fd)
Definition: cache_ram.cc:120
virtual int Reset(void *txn)
Definition: cache_ram.cc:273
virtual ~RamCacheManager()
Definition: cache_ram.cc:57
MemoryKvStore regular_entries_
Definition: cache_ram.h:288
MemoryKvStore volatile_entries_
Definition: cache_ram.h:289
virtual int64_t Write(const void *buf, uint64_t size, void *txn)
Definition: cache_ram.cc:233
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:171