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