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