CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
kvstore.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_KVSTORE_H_
6 #define CVMFS_KVSTORE_H_
7 
8 #include <pthread.h>
9 #include <unistd.h>
10 
11 #include <string>
12 #include <vector>
13 
14 #include "cache.h"
15 #include "lru.h"
16 #include "malloc_heap.h"
17 #include "statistics.h"
18 #include "util/async.h"
19 #include "util/single_copy.h"
20 
21 using namespace std; // NOLINT
22 
23 
29 struct AllocHeader {
30  AllocHeader() : version(0), id() { }
31  uint8_t version;
33 };
34 
35 
40 struct MemoryBuffer {
42  : address(NULL)
43  , size(0)
44  , refcount(0)
45  , object_type(CacheManager::kTypeRegular)
46  , id() {}
47  void *address;
48  size_t size;
49  unsigned int refcount;
52 };
53 
54 
66 class MemoryKvStore : SingleCopy, public Callbackable<MallocHeap::BlockPtr> {
67  public:
71  };
72 
73  struct Counters {
87 
88  explicit Counters(perf::StatisticsTemplate statistics) {
89  sz_size = statistics.RegisterTemplated("sz_size", "Total size");
90  n_getsize = statistics.RegisterTemplated("n_getsize",
91  "Number of GetSize calls");
92  n_getrefcount = statistics.RegisterTemplated("n_getrefcount",
93  "Number of GetRefcount calls");
94  n_incref = statistics.RegisterTemplated("n_incref",
95  "Number of IncRef calls");
96  n_unref = statistics.RegisterTemplated("n_unref",
97  "Number of Unref calls");
98  n_read = statistics.RegisterTemplated("n_read", "Number of Read calls");
99  n_commit = statistics.RegisterTemplated("n_commit",
100  "Number of Commit calls");
101  n_delete = statistics.RegisterTemplated("n_delete",
102  "Number of Delete calls");
103  n_shrinkto = statistics.RegisterTemplated("n_shrinkto",
104  "Number of ShrinkTo calls");
105  sz_read = statistics.RegisterTemplated("sz_read", "Bytes read");
106  sz_committed = statistics.RegisterTemplated("sz_committed",
107  "Bytes committed");
108  sz_deleted = statistics.RegisterTemplated("sz_deleted", "Bytes deleted");
109  sz_shrunk = statistics.RegisterTemplated("sz_shrunk", "Bytes shrunk");
110  }
111  };
112 
114  unsigned int cache_entries,
115  MemoryAllocator alloc,
116  unsigned alloc_size,
117  perf::StatisticsTemplate statistics);
118 
119  ~MemoryKvStore();
120 
126  bool Contains(const shash::Any &id);
127 
134  int64_t GetSize(const shash::Any &id);
135 
142  int64_t GetRefcount(const shash::Any &id);
143 
149  bool IncRef(const shash::Any &id);
150 
156  bool Unref(const shash::Any &id);
157 
167  int64_t Read(
168  const shash::Any &id,
169  void *buf,
170  size_t size,
171  size_t offset);
172 
180  int Commit(const MemoryBuffer &buf);
181 
187  bool Delete(const shash::Any &id);
188 
195  bool ShrinkTo(size_t size);
196 
200  size_t GetUsed() { return used_bytes_; }
201 
202  private:
203  // Compact memory once utilization falls below the threshold
204  static const double kCompactThreshold; // = 0.8
205 
206  bool DoDelete(const shash::Any &id);
207  int DoMalloc(MemoryBuffer *buf);
208  void DoFree(MemoryBuffer *buf);
209  int DoCommit(const MemoryBuffer &buf);
210  void OnBlockMove(const MallocHeap::BlockPtr &ptr);
211  bool CompactMemory();
212 
214  size_t used_bytes_;
215  unsigned int entry_count_;
216  unsigned int max_entries_;
219  pthread_rwlock_t rwlock_;
221 };
222 
223 #endif // CVMFS_KVSTORE_H_
unsigned int max_entries_
Definition: kvstore.h:216
perf::Counter * n_delete
Definition: kvstore.h:81
static const double kCompactThreshold
Definition: kvstore.h:204
Counters(perf::StatisticsTemplate statistics)
Definition: kvstore.h:88
perf::Counter * n_read
Definition: kvstore.h:79
perf::Counter * n_getrefcount
Definition: kvstore.h:76
uint8_t version
Definition: kvstore.h:31
perf::Counter * n_unref
Definition: kvstore.h:78
static void Read(void *buf, size_t nbyte)
Definition: helper_util.cc:94
unsigned int entry_count_
Definition: kvstore.h:215
size_t used_bytes_
Definition: kvstore.h:214
unsigned int refcount
Definition: kvstore.h:49
perf::Counter * n_getsize
Definition: kvstore.h:75
perf::Counter * sz_deleted
Definition: kvstore.h:85
MemoryAllocator allocator_
Definition: kvstore.h:213
Counter * RegisterTemplated(const std::string &name_minor, const std::string &desc)
Definition: statistics.h:109
perf::Counter * sz_size
Definition: kvstore.h:74
AllocHeader()
Definition: kvstore.h:30
perf::Counter * n_commit
Definition: kvstore.h:80
size_t GetUsed()
Definition: kvstore.h:200
perf::Counter * sz_shrunk
Definition: kvstore.h:86
MemoryBuffer()
Definition: kvstore.h:41
shash::Any id
Definition: kvstore.h:51
pthread_rwlock_t rwlock_
Definition: kvstore.h:219
MallocHeap * heap_
Definition: kvstore.h:218
CacheManager::ObjectType object_type
Definition: kvstore.h:50
lru::LruCache< shash::Any, MemoryBuffer > entries_
Definition: kvstore.h:217
void * address
Definition: kvstore.h:47
size_t size
Definition: kvstore.h:48
shash::Any id
Definition: kvstore.h:32
perf::Counter * n_incref
Definition: kvstore.h:77
perf::Counter * sz_read
Definition: kvstore.h:83
perf::Counter * n_shrinkto
Definition: kvstore.h:82
Counters counters_
Definition: kvstore.h:220
static void size_t size
Definition: smalloc.h:47
perf::Counter * sz_committed
Definition: kvstore.h:84