CernVM-FS  2.12.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 <fcntl.h>
6 #include <unistd.h>
7 
8 #include <cassert>
9 #include <string>
10 
11 #include "fd_refcount_mgr.h"
12 #include "util/mutex.h"
13 #include "util/smalloc.h"
14 
15 
16 int FdRefcountMgr::Open(const shash::Any id, const std::string& path) {
17  int result = -1;
19  if (!map_fd_.Lookup(id, &result)) {
20  result = open(path.c_str(), O_RDONLY);
21  if (result >= 0) {
22  map_fd_.Insert(id, result);
23  }
24  }
25  if (result >= 0) {
26  FdRefcountInfo refc_info;
27  if (map_refcount_.Lookup(result, &refc_info)) {
28  refc_info.refcount++;
29  } else {
30  refc_info.refcount = 1;
31  refc_info.id = id;
32  }
33  map_refcount_.Insert(result, refc_info);
34  }
35  return result;
36 }
37 
38 int FdRefcountMgr::Close(int fd) {
39  int retval = -1;
41  FdRefcountInfo refc_info;
42  if (map_refcount_.Lookup(fd, &refc_info)) {
43  if (refc_info.refcount > 1) {
44  refc_info.refcount -= 1;
45  map_refcount_.Insert(fd, refc_info);
46  retval = 0;
47  } else {
48  retval = close(fd);
49  map_fd_.Erase(refc_info.id);
50  map_refcount_.Erase(fd);
51  }
52  } else {
53  // fd not present in our table - this should only happen
54  // when reloading from the normal posix cache manager!
56  "WARNING: trying to close fd that "
57  " is not in refcount tables");
58  retval = close(fd);
59  }
60  return retval;
61 }
62 
63 int FdRefcountMgr::Dup(int fd) {
64  int retval = -1;
66  FdRefcountInfo refc_info;
67  if (map_refcount_.Lookup(fd, &refc_info)) {
68  refc_info.refcount += 1;
69  map_refcount_.Insert(fd, refc_info);
70  retval = fd;
71  } else {
72  // fd not present in our table - this should
73  // not happen in the current usage of Dup
75  "WARNING: trying to dup fd that "
76  " is not in refcount tables");
77  retval = dup(fd);
78  }
79  return retval;
80 }
81 
84  return clone;
85 }
86 
88  return &map_fd_;
89 }
90 
93  return &map_refcount_;
94 }
95 
97  map_fd_ = *other->GetFdMapPtr();
98  map_refcount_ = *other->GetRefcountMapPtr();
99 }
100 
102  pthread_mutex_destroy(lock_cache_refcount_);
103  free(lock_cache_refcount_);
104 }
105 
107  const shash::Any hash_null;
108  map_fd_.Init(16, hash_null, hasher_any);
109  map_refcount_.Init(16, -1, hasher_int);
111  reinterpret_cast<pthread_mutex_t*>(smalloc(sizeof(pthread_mutex_t)));
112  int retval = pthread_mutex_init(lock_cache_refcount_, NULL);
113  assert(retval == 0);
114 }
115 
118  const SmallHashDynamic<shash::Any, int> &map_fd)
119 {
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;
126  reinterpret_cast<pthread_mutex_t*>(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:109
Definition: mutex.h:42
int Close(int fd)
bool Erase(const Key &key)
Definition: smallhash.h:115
SmallHashDynamic< shash::Any, int > * GetFdMapPtr()
bool Lookup(const Key &key, Value *value) const
Definition: smallhash.h:73
void Init(uint32_t expected_size, Key empty, uint32_t(*hasher)(const Key &key))
Definition: smallhash.h:60
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:528