CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fd_refcount_mgr.cc
Go to the documentation of this file.
1 
5 #include "fd_refcount_mgr.h"
6 
7 #include <fcntl.h>
8 #include <unistd.h>
9 
10 #include <cassert>
11 #include <string>
12 
13 #include "util/mutex.h"
14 #include "util/smalloc.h"
15 
16 
17 int FdRefcountMgr::Open(const shash::Any id, const std::string &path) {
18  int result = -1;
20  if (!map_fd_.Lookup(id, &result)) {
21  result = open(path.c_str(), O_RDONLY);
22  if (result >= 0) {
23  map_fd_.Insert(id, result);
24  }
25  }
26  if (result >= 0) {
27  FdRefcountInfo refc_info;
28  if (map_refcount_.Lookup(result, &refc_info)) {
29  refc_info.refcount++;
30  } else {
31  refc_info.refcount = 1;
32  refc_info.id = id;
33  }
34  map_refcount_.Insert(result, refc_info);
35  }
36  return result;
37 }
38 
39 int FdRefcountMgr::Close(int fd) {
40  int retval = -1;
42  FdRefcountInfo refc_info;
43  if (map_refcount_.Lookup(fd, &refc_info)) {
44  if (refc_info.refcount > 1) {
45  refc_info.refcount -= 1;
46  map_refcount_.Insert(fd, refc_info);
47  retval = 0;
48  } else {
49  retval = close(fd);
50  map_fd_.Erase(refc_info.id);
51  map_refcount_.Erase(fd);
52  }
53  } else {
54  // fd not present in our table - this should only happen
55  // when reloading from the normal posix cache manager!
57  "WARNING: trying to close fd that "
58  " is not in refcount tables");
59  retval = close(fd);
60  }
61  return retval;
62 }
63 
64 int FdRefcountMgr::Dup(int fd) {
65  int retval = -1;
67  FdRefcountInfo refc_info;
68  if (map_refcount_.Lookup(fd, &refc_info)) {
69  refc_info.refcount += 1;
70  map_refcount_.Insert(fd, refc_info);
71  retval = fd;
72  } else {
73  // fd not present in our table - this should
74  // not happen in the current usage of Dup
76  "WARNING: trying to dup fd that "
77  " is not in refcount tables");
78  retval = dup(fd);
79  }
80  return retval;
81 }
82 
85  return clone;
86 }
87 
89  return &map_fd_;
90 }
91 
94  return &map_refcount_;
95 }
96 
98  map_fd_ = *other->GetFdMapPtr();
99  map_refcount_ = *other->GetRefcountMapPtr();
100 }
101 
103  pthread_mutex_destroy(lock_cache_refcount_);
104  free(lock_cache_refcount_);
105 }
106 
108  const shash::Any hash_null;
109  map_fd_.Init(16, hash_null, hasher_any);
110  map_refcount_.Init(16, -1, hasher_int);
111  lock_cache_refcount_ = reinterpret_cast<pthread_mutex_t *>(
112  smalloc(sizeof(pthread_mutex_t)));
113  int retval = pthread_mutex_init(lock_cache_refcount_, NULL);
114  assert(retval == 0);
115 }
116 
119  const SmallHashDynamic<shash::Any, int> &map_fd) {
120  const shash::Any hash_null;
121  map_fd_.Init(16, hash_null, hasher_any);
122  map_refcount_.Init(16, -1, hasher_int);
123  map_refcount_ = map_refcount;
124  map_fd_ = map_fd;
125  lock_cache_refcount_ = reinterpret_cast<pthread_mutex_t *>(
126  smalloc(sizeof(pthread_mutex_t)));
127  int retval = pthread_mutex_init(lock_cache_refcount_, NULL);
128  assert(retval == 0);
129 }
int Dup(int fd)
SmallHashDynamic< int, FdRefcountInfo > * GetRefcountMapPtr()
pthread_mutex_t * lock_cache_refcount_
FdRefcountMgr * Clone()
assert((mem||(size==0))&&"Out Of Memory")
static uint32_t hasher_int(const int &key)
static uint32_t hasher_any(const ComparableHash &key)
int Open(const shash::Any id, const std::string &path)
SmallHashDynamic< int, FdRefcountInfo > map_refcount_
SmallHashDynamic< shash::Any, int > map_fd_
void Insert(const Key &key, const Value &value)
Definition: smallhash.h:106
Definition: mutex.h:42
int Close(int fd)
bool Erase(const Key &key)
Definition: smallhash.h:112
SmallHashDynamic< shash::Any, int > * GetFdMapPtr()
bool Lookup(const Key &key, Value *value) const
Definition: smallhash.h:70
void Init(uint32_t expected_size, Key empty, uint32_t(*hasher)(const Key &key))
Definition: smallhash.h:58
void AssignFrom(FdRefcountMgr *other)
shash::Any id
refcount for the times the fd was opened in the cache
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545