Directory: | cvmfs/ |
---|---|
File: | cvmfs/fd_refcount_mgr.cc |
Date: | 2025-03-09 02:34:28 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 80 | 80 | 100.0% |
Branches: | 45 | 76 | 59.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
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 | 186 | int FdRefcountMgr::Open(const shash::Any id, const std::string& path) { | |
17 | 186 | int result = -1; | |
18 | 186 | MutexLockGuard lock_guard(lock_cache_refcount_); | |
19 |
3/4✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 179 times.
✓ Branch 4 taken 7 times.
|
186 | if (!map_fd_.Lookup(id, &result)) { |
20 |
1/2✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
|
179 | result = open(path.c_str(), O_RDONLY); |
21 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 104 times.
|
179 | if (result >= 0) { |
22 |
1/2✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
|
75 | map_fd_.Insert(id, result); |
23 | } | ||
24 | } | ||
25 |
2/2✓ Branch 0 taken 82 times.
✓ Branch 1 taken 104 times.
|
186 | if (result >= 0) { |
26 |
1/2✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
|
82 | FdRefcountInfo refc_info; |
27 |
3/4✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 75 times.
|
82 | if (map_refcount_.Lookup(result, &refc_info)) { |
28 | 7 | refc_info.refcount++; | |
29 | } else { | ||
30 | 75 | refc_info.refcount = 1; | |
31 | 75 | refc_info.id = id; | |
32 | } | ||
33 |
1/2✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
|
82 | map_refcount_.Insert(result, refc_info); |
34 | } | ||
35 | 186 | return result; | |
36 | 186 | } | |
37 | |||
38 | 78 | int FdRefcountMgr::Close(int fd) { | |
39 | 78 | int retval = -1; | |
40 | 78 | MutexLockGuard lock_guard(lock_cache_refcount_); | |
41 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | FdRefcountInfo refc_info; |
42 |
3/4✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 77 times.
✓ Branch 4 taken 1 times.
|
78 | if (map_refcount_.Lookup(fd, &refc_info)) { |
43 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 71 times.
|
77 | if (refc_info.refcount > 1) { |
44 | 6 | refc_info.refcount -= 1; | |
45 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | map_refcount_.Insert(fd, refc_info); |
46 | 6 | retval = 0; | |
47 | } else { | ||
48 |
1/2✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
|
71 | retval = close(fd); |
49 |
1/2✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
|
71 | map_fd_.Erase(refc_info.id); |
50 |
1/2✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
|
71 | 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! | ||
55 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | LogCvmfs(kLogCache, kLogDebug, |
56 | "WARNING: trying to close fd that " | ||
57 | " is not in refcount tables"); | ||
58 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | retval = close(fd); |
59 | } | ||
60 | 78 | return retval; | |
61 | 78 | } | |
62 | |||
63 | 4 | int FdRefcountMgr::Dup(int fd) { | |
64 | 4 | int retval = -1; | |
65 | 4 | MutexLockGuard lock_guard(lock_cache_refcount_); | |
66 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | FdRefcountInfo refc_info; |
67 |
3/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | if (map_refcount_.Lookup(fd, &refc_info)) { |
68 | 2 | refc_info.refcount += 1; | |
69 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | map_refcount_.Insert(fd, refc_info); |
70 | 2 | retval = fd; | |
71 | } else { | ||
72 | // fd not present in our table - this should | ||
73 | // not happen in the current usage of Dup | ||
74 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | LogCvmfs(kLogCache, kLogDebug, |
75 | "WARNING: trying to dup fd that " | ||
76 | " is not in refcount tables"); | ||
77 | 2 | retval = dup(fd); | |
78 | } | ||
79 | 4 | return retval; | |
80 | 4 | } | |
81 | |||
82 | 1 | FdRefcountMgr* FdRefcountMgr::Clone() { | |
83 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | FdRefcountMgr* clone = new FdRefcountMgr(map_refcount_, map_fd_); |
84 | 1 | return clone; | |
85 | } | ||
86 | |||
87 | 1 | SmallHashDynamic<shash::Any, int> * FdRefcountMgr::GetFdMapPtr() { | |
88 | 1 | return &map_fd_; | |
89 | } | ||
90 | |||
91 | SmallHashDynamic<int, FdRefcountMgr::FdRefcountInfo>* | ||
92 | 1 | FdRefcountMgr::GetRefcountMapPtr() { | |
93 | 1 | return &map_refcount_; | |
94 | } | ||
95 | |||
96 | 1 | void FdRefcountMgr::AssignFrom(FdRefcountMgr* other) { | |
97 | 1 | map_fd_ = *other->GetFdMapPtr(); | |
98 | 1 | map_refcount_ = *other->GetRefcountMapPtr(); | |
99 | 1 | } | |
100 | |||
101 | 170 | FdRefcountMgr::~FdRefcountMgr() { | |
102 | 170 | pthread_mutex_destroy(lock_cache_refcount_); | |
103 | 170 | free(lock_cache_refcount_); | |
104 | 170 | } | |
105 | |||
106 |
1/2✓ Branch 2 taken 171 times.
✗ Branch 3 not taken.
|
171 | FdRefcountMgr::FdRefcountMgr() { |
107 |
1/2✓ Branch 1 taken 171 times.
✗ Branch 2 not taken.
|
171 | const shash::Any hash_null; |
108 |
1/2✓ Branch 1 taken 171 times.
✗ Branch 2 not taken.
|
171 | map_fd_.Init(16, hash_null, hasher_any); |
109 |
1/2✓ Branch 1 taken 171 times.
✗ Branch 2 not taken.
|
171 | map_refcount_.Init(16, -1, hasher_int); |
110 | 171 | lock_cache_refcount_ = | |
111 | 171 | reinterpret_cast<pthread_mutex_t*>(smalloc(sizeof(pthread_mutex_t))); | |
112 | 171 | int retval = pthread_mutex_init(lock_cache_refcount_, NULL); | |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
|
171 | assert(retval == 0); |
114 | 171 | } | |
115 | |||
116 | 1 | FdRefcountMgr::FdRefcountMgr( | |
117 | const SmallHashDynamic<int, FdRefcountMgr::FdRefcountInfo> &map_refcount, | ||
118 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | const SmallHashDynamic<shash::Any, int> &map_fd) |
119 | { | ||
120 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | const shash::Any hash_null; |
121 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | map_fd_.Init(16, hash_null, hasher_any); |
122 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | map_refcount_.Init(16, -1, hasher_int); |
123 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | map_refcount_ = map_refcount; |
124 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | map_fd_ = map_fd; |
125 | 1 | lock_cache_refcount_ = | |
126 | 1 | reinterpret_cast<pthread_mutex_t*>(smalloc(sizeof(pthread_mutex_t))); | |
127 | 1 | int retval = pthread_mutex_init(lock_cache_refcount_, NULL); | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert(retval == 0); |
129 | 1 | } | |
130 |