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