| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/file_chunk.cc |
| Date: | 2026-04-19 02:41:37 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 108 | 136 | 79.4% |
| Branches: | 63 | 110 | 57.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | |||
| 6 | #include "file_chunk.h" | ||
| 7 | |||
| 8 | #include <cassert> | ||
| 9 | |||
| 10 | #include "util/murmur.hxx" | ||
| 11 | |||
| 12 | using namespace std; // NOLINT | ||
| 13 | |||
| 14 | ✗ | static inline uint32_t hasher_uint64t(const uint64_t &value) { | |
| 15 | ✗ | return MurmurHash2(&value, sizeof(value), 0x07387a4f); | |
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | //------------------------------------------------------------------------------ | ||
| 20 | |||
| 21 | |||
| 22 | 40 | unsigned FileChunkReflist::FindChunkIdx(const uint64_t off) { | |
| 23 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
|
40 | assert(list && (list->size() > 0)); |
| 24 | 40 | unsigned idx_low = 0; | |
| 25 | 40 | unsigned idx_high = list->size() - 1; | |
| 26 | 40 | unsigned chunk_idx = idx_high / 2; | |
| 27 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 32 times.
|
100 | while (idx_low < idx_high) { |
| 28 |
2/2✓ Branch 2 taken 12 times.
✓ Branch 3 taken 56 times.
|
68 | if (static_cast<uint64_t>(list->AtPtr(chunk_idx)->offset()) > off) { |
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | assert(idx_high > 0); |
| 30 | 12 | idx_high = chunk_idx - 1; | |
| 31 | } else { | ||
| 32 | 56 | if ((chunk_idx == list->size() - 1) | |
| 33 |
5/6✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 48 times.
|
56 | || (static_cast<uint64_t>(list->AtPtr(chunk_idx + 1)->offset()) |
| 34 | > off)) { | ||
| 35 | 8 | break; | |
| 36 | } | ||
| 37 | 48 | idx_low = chunk_idx + 1; | |
| 38 | } | ||
| 39 | 60 | chunk_idx = idx_low + (idx_high - idx_low) / 2; | |
| 40 | } | ||
| 41 | 40 | return chunk_idx; | |
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | /** | ||
| 46 | * Returns a consistent hash over hashes of the chunks. Used by libcvmfs. | ||
| 47 | */ | ||
| 48 | 5 | shash::Any FileChunkReflist::HashChunkList() { | |
| 49 | 5 | const shash::Algorithms algo = list->AtPtr(0)->content_hash().algorithm; | |
| 50 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | shash::ContextPtr ctx(algo); |
| 51 | 5 | ctx.buffer = alloca(ctx.size); | |
| 52 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | shash::Init(ctx); |
| 53 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 5 times.
|
15 | for (unsigned i = 0; i < list->size(); ++i) { |
| 54 | 10 | shash::Update( | |
| 55 |
1/2✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
10 | list->AtPtr(i)->content_hash().digest, shash::kDigestSizes[algo], ctx); |
| 56 | } | ||
| 57 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | shash::Any result(algo); |
| 58 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | shash::Final(ctx, &result); |
| 59 | 10 | return result; | |
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | //------------------------------------------------------------------------------ | ||
| 64 | |||
| 65 | |||
| 66 | 178 | void ChunkTables::InitLocks() { | |
| 67 | 178 | lock = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t))); | |
| 68 | 178 | const int retval = pthread_mutex_init(lock, NULL); | |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
|
178 | assert(retval == 0); |
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 22784 times.
✓ Branch 1 taken 178 times.
|
22962 | for (unsigned i = 0; i < kNumHandleLocks; ++i) { |
| 72 | pthread_mutex_t *m = reinterpret_cast<pthread_mutex_t *>( | ||
| 73 | 22784 | smalloc(sizeof(pthread_mutex_t))); | |
| 74 | 22784 | const int retval = pthread_mutex_init(m, NULL); | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22784 times.
|
22784 | assert(retval == 0); |
| 76 |
1/2✓ Branch 1 taken 22784 times.
✗ Branch 2 not taken.
|
22784 | handle_locks.PushBack(m); |
| 77 | } | ||
| 78 | 178 | } | |
| 79 | |||
| 80 | |||
| 81 | 178 | void ChunkTables::InitHashmaps() { | |
| 82 | 178 | handle2uniqino.Init(16, 0, hasher_uint64t); | |
| 83 | 178 | handle2fd.Init(16, 0, hasher_uint64t); | |
| 84 | 178 | inode2chunks.Init(16, 0, hasher_uint64t); | |
| 85 | 178 | inode2references.Init(16, 0, hasher_uint64t); | |
| 86 | 178 | } | |
| 87 | |||
| 88 | |||
| 89 |
4/8✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 178 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 178 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 178 times.
✗ Branch 12 not taken.
|
178 | ChunkTables::ChunkTables() { |
| 90 | 178 | next_handle = 2; | |
| 91 | 178 | version = kVersion; | |
| 92 |
1/2✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
|
178 | InitLocks(); |
| 93 |
1/2✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
|
178 | InitHashmaps(); |
| 94 | 178 | } | |
| 95 | |||
| 96 | |||
| 97 | 178 | ChunkTables::~ChunkTables() { | |
| 98 | 178 | pthread_mutex_destroy(lock); | |
| 99 | 178 | free(lock); | |
| 100 |
2/2✓ Branch 0 taken 22784 times.
✓ Branch 1 taken 178 times.
|
22962 | for (unsigned i = 0; i < kNumHandleLocks; ++i) { |
| 101 | 22784 | pthread_mutex_destroy(handle_locks.At(i)); | |
| 102 | 22784 | free(handle_locks.At(i)); | |
| 103 | } | ||
| 104 | 178 | } | |
| 105 | |||
| 106 | |||
| 107 | ✗ | ChunkTables::ChunkTables(const ChunkTables &other) { | |
| 108 | ✗ | version = kVersion; | |
| 109 | ✗ | InitLocks(); | |
| 110 | ✗ | InitHashmaps(); | |
| 111 | ✗ | CopyFrom(other); | |
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | ✗ | ChunkTables &ChunkTables::operator=(const ChunkTables &other) { | |
| 116 | ✗ | if (&other == this) | |
| 117 | ✗ | return *this; | |
| 118 | |||
| 119 | ✗ | handle2uniqino.Clear(); | |
| 120 | ✗ | handle2fd.Clear(); | |
| 121 | ✗ | inode2chunks.Clear(); | |
| 122 | ✗ | inode2references.Clear(); | |
| 123 | ✗ | CopyFrom(other); | |
| 124 | ✗ | return *this; | |
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | ✗ | void ChunkTables::CopyFrom(const ChunkTables &other) { | |
| 129 | ✗ | assert(version == other.version); | |
| 130 | ✗ | next_handle = other.next_handle; | |
| 131 | ✗ | inode2references = other.inode2references; | |
| 132 | ✗ | inode2chunks = other.inode2chunks; | |
| 133 | ✗ | handle2fd = other.handle2fd; | |
| 134 | ✗ | handle2uniqino = other.handle2uniqino; | |
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | ✗ | pthread_mutex_t *ChunkTables::Handle2Lock(const uint64_t handle) const { | |
| 139 | ✗ | const uint32_t hash = hasher_uint64t(handle); | |
| 140 | ✗ | const double bucket = static_cast<double>(hash) | |
| 141 | ✗ | * static_cast<double>(kNumHandleLocks) | |
| 142 | / static_cast<double>((uint32_t)(-1)); | ||
| 143 | ✗ | return handle_locks.At((uint32_t)bucket % kNumHandleLocks); | |
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | //------------------------------------------------------------------------------ | ||
| 148 | |||
| 149 | |||
| 150 | 220 | SimpleChunkTables::SimpleChunkTables() { | |
| 151 | 220 | lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t))); | |
| 152 | 220 | const int retval = pthread_mutex_init(lock_, NULL); | |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
|
220 | assert(retval == 0); |
| 154 | 220 | } | |
| 155 | |||
| 156 | |||
| 157 | 218 | SimpleChunkTables::~SimpleChunkTables() { | |
| 158 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 218 times.
|
221 | for (unsigned i = 0; i < fd_table_.size(); ++i) { |
| 159 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | delete fd_table_[i].chunk_reflist.list; |
| 160 | } | ||
| 161 | 218 | pthread_mutex_destroy(lock_); | |
| 162 | 218 | free(lock_); | |
| 163 | 218 | } | |
| 164 | |||
| 165 | |||
| 166 | 24 | int SimpleChunkTables::Add(FileChunkReflist chunks) { | |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | assert(chunks.list != NULL); |
| 168 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | OpenChunks new_entry; |
| 169 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | new_entry.chunk_reflist = chunks; |
| 170 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | new_entry.chunk_fd = new ChunkFd(); |
| 171 | 24 | unsigned i = 0; | |
| 172 | 24 | Lock(); | |
| 173 |
2/2✓ Branch 1 taken 45 times.
✓ Branch 2 taken 21 times.
|
66 | for (; i < fd_table_.size(); ++i) { |
| 174 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 42 times.
|
45 | if (fd_table_[i].chunk_reflist.list == NULL) { |
| 175 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | fd_table_[i] = new_entry; |
| 176 | 3 | Unlock(); | |
| 177 | 3 | return i; | |
| 178 | } | ||
| 179 | } | ||
| 180 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | fd_table_.push_back(new_entry); |
| 181 | 21 | Unlock(); | |
| 182 | 21 | return i; | |
| 183 | 24 | } | |
| 184 | |||
| 185 | |||
| 186 | 9 | SimpleChunkTables::OpenChunks SimpleChunkTables::Get(int fd) { | |
| 187 | 9 | OpenChunks result; | |
| 188 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if (fd < 0) |
| 189 | 3 | return result; | |
| 190 | |||
| 191 | 6 | const unsigned idx = static_cast<unsigned>(fd); | |
| 192 | 6 | Lock(); | |
| 193 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
|
6 | if (idx < fd_table_.size()) |
| 194 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | result = fd_table_[idx]; |
| 195 | 6 | Unlock(); | |
| 196 | 6 | return result; | |
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | 27 | void SimpleChunkTables::Release(int fd) { | |
| 201 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 24 times.
|
27 | if (fd < 0) |
| 202 | 3 | return; | |
| 203 | |||
| 204 | 24 | Lock(); | |
| 205 | 24 | const unsigned idx = static_cast<unsigned>(fd); | |
| 206 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 21 times.
|
24 | if (idx >= fd_table_.size()) { |
| 207 | 3 | Unlock(); | |
| 208 | 3 | return; | |
| 209 | } | ||
| 210 | |||
| 211 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | delete fd_table_[idx].chunk_reflist.list; |
| 212 | 21 | fd_table_[idx].chunk_reflist.list = NULL; | |
| 213 | 21 | fd_table_[idx].chunk_reflist.path.Assign("", 0); | |
| 214 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | delete fd_table_[idx].chunk_fd; |
| 215 | 21 | fd_table_[idx].chunk_fd = NULL; | |
| 216 |
6/6✓ Branch 1 taken 36 times.
✓ Branch 2 taken 3 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 21 times.
|
39 | while (!fd_table_.empty() && (fd_table_.back().chunk_reflist.list == NULL)) { |
| 217 | 18 | fd_table_.pop_back(); | |
| 218 | } | ||
| 219 | 21 | Unlock(); | |
| 220 | } | ||
| 221 |