GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2024-04-28 02:33:07
Exec Total Coverage
Lines: 147 147 100.0%
Branches: 35 64 54.7%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "item.h"
6
7 #include <algorithm>
8 #include <cassert>
9 #include <cstdlib>
10 #include <cstring>
11
12 #include "ingestion/ingestion_source.h"
13 #include "item_mem.h"
14 #include "util/concurrency.h"
15 #include "util/smalloc.h"
16
17 253259 FileItem::FileItem(
18 IngestionSource* source,
19 uint64_t min_chunk_size,
20 uint64_t avg_chunk_size,
21 uint64_t max_chunk_size,
22 zlib::Algorithms compression_algorithm,
23 shash::Algorithms hash_algorithm,
24 shash::Suffix hash_suffix,
25 bool may_have_chunks,
26 253259 bool has_legacy_bulk_chunk)
27 253259 : source_(source)
28 253259 , compression_algorithm_(compression_algorithm)
29 253259 , hash_algorithm_(hash_algorithm)
30 253259 , hash_suffix_(hash_suffix)
31 253259 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
32 253259 , size_(kSizeUnknown)
33 253259 , may_have_chunks_(may_have_chunks)
34
1/2
✓ Branch 1 taken 253259 times.
✗ Branch 2 not taken.
253259 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
35
1/2
✓ Branch 1 taken 253259 times.
✗ Branch 2 not taken.
253259 , bulk_hash_(hash_algorithm)
36
1/2
✓ Branch 2 taken 253259 times.
✗ Branch 3 not taken.
506518 , chunks_(1)
37 {
38 253259 int retval = pthread_mutex_init(&lock_, NULL);
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253259 times.
253259 assert(retval == 0);
40 253259 atomic_init64(&nchunks_in_fly_);
41 253259 atomic_init32(&is_fully_chunked_);
42 253259 }
43
44 253211 FileItem::~FileItem() {
45 253211 pthread_mutex_destroy(&lock_);
46 253202 }
47
48 250522 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
49 250522 MutexLockGuard lock_guard(lock_);
50
51
2/2
✓ Branch 1 taken 441 times.
✓ Branch 2 taken 250081 times.
250522 switch (file_chunk.content_hash().suffix) {
52 441 case shash::kSuffixPartial:
53
1/2
✓ Branch 1 taken 441 times.
✗ Branch 2 not taken.
441 chunks_.PushBack(file_chunk);
54 441 break;
55
56 250081 default:
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250081 times.
250081 assert(file_chunk.offset() == 0);
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250081 times.
250081 assert(file_chunk.size() == size_);
59 250081 bulk_hash_ = file_chunk.content_hash();
60 250081 break;
61 }
62 250522 atomic_dec64(&nchunks_in_fly_);
63 250522 }
64
65
66 //------------------------------------------------------------------------------
67
68
69 255260 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
70 255139 : file_item_(file_item)
71 255139 , offset_(offset)
72 255139 , size_(0)
73 255139 , is_bulk_chunk_(false)
74 255139 , upload_handle_(NULL)
75
1/2
✓ Branch 4 taken 255119 times.
✗ Branch 5 not taken.
255260 , compressor_(NULL)
76 {
77 255119 hash_ctx_.algorithm = file_item->hash_algorithm();
78
1/2
✓ Branch 1 taken 255076 times.
✗ Branch 2 not taken.
255116 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
79 255076 hash_ctx_.buffer = hash_ctx_buffer_;
80
1/2
✓ Branch 1 taken 255142 times.
✗ Branch 2 not taken.
255076 shash::Init(hash_ctx_);
81 255142 hash_value_.algorithm = hash_ctx_.algorithm;
82 255142 hash_value_.suffix = shash::kSuffixPartial;
83 255142 file_item_->IncNchunksInFly();
84 255438 }
85
86
87 249702 void ChunkItem::MakeBulkChunk() {
88 249702 is_bulk_chunk_ = true;
89 249702 hash_value_.suffix = file_item_->hash_suffix();
90 249669 }
91
92
93 578103 zlib::Compressor *ChunkItem::GetCompressor() {
94
2/2
✓ Branch 1 taken 249748 times.
✓ Branch 2 taken 328541 times.
578103 if (!compressor_.IsValid()) {
95 compressor_ =
96
2/4
✓ Branch 2 taken 250136 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 250098 times.
✗ Branch 6 not taken.
249748 zlib::Compressor::Construct(file_item_->compression_algorithm());
97 }
98 578639 return compressor_.weak_ref();
99 }
100
101
102 249991 void ChunkItem::ReleaseCompressor() {
103 249991 compressor_.Destroy();
104 250017 }
105
106
107 //------------------------------------------------------------------------------
108
109 atomic_int64 BlockItem::managed_bytes_ = 0;
110
111
112 4440 BlockItem::BlockItem(ItemAllocator *allocator)
113 4440 : allocator_(allocator)
114 4440 , type_(kBlockHollow)
115 4440 , tag_(-1)
116 4440 , file_item_(NULL)
117 4440 , chunk_item_(NULL)
118 4440 , data_(NULL)
119 4440 , capacity_(0)
120 4440 , size_(0)
121 4440 { }
122
123
124 1820847 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
125 1811791 : allocator_(allocator)
126 1811791 , type_(kBlockHollow)
127 1811791 , tag_(tag)
128 1811791 , file_item_(NULL)
129 1811791 , chunk_item_(NULL)
130 1811791 , data_(NULL)
131 1811791 , capacity_(0)
132 1820847 , size_(0)
133 {
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1811791 times.
1811791 assert(tag_ >= 0);
135 1811791 }
136
137
138 1817332 BlockItem::~BlockItem() {
139
2/2
✓ Branch 0 taken 766385 times.
✓ Branch 1 taken 1050947 times.
1817332 if (data_)
140 766385 allocator_->Free(data_);
141 1818017 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
142 1829423 }
143
144
145 275443 void BlockItem::Discharge() {
146 275443 data_ = NULL;
147 275443 size_ = capacity_ = 0;
148 275443 }
149
150
151 751305 void BlockItem::MakeStop() {
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 751305 times.
751305 assert(type_ == kBlockHollow);
153 751305 type_ = kBlockStop;
154 751305 }
155
156
157 386750 void BlockItem::MakeData(uint32_t capacity) {
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386750 times.
386750 assert(type_ == kBlockHollow);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386750 times.
386750 assert(allocator_ != NULL);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 386750 times.
386750 assert(capacity > 0);
161
162 386750 type_ = kBlockData;
163 386750 capacity_ = capacity;
164 386750 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
165 387663 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
166 387766 }
167
168
169 /**
170 * Move data from one block to another.
171 */
172 275392 void BlockItem::MakeDataMove(BlockItem *other) {
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275392 times.
275392 assert(type_ == kBlockHollow);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275392 times.
275392 assert(other->type_ == kBlockData);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275392 times.
275392 assert(other->size_ > 0);
176
177 275392 type_ = kBlockData;
178 275392 capacity_ = size_ = other->size_;
179 275392 data_ = other->data_;
180 275392 allocator_ = other->allocator_;
181
182 275392 other->Discharge();
183 275459 }
184
185
186 /**
187 * Copy a piece of one block's data into a new block.
188 */
189 416439 void BlockItem::MakeDataCopy(
190 const unsigned char *data,
191 uint32_t size)
192 {
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416439 times.
416439 assert(type_ == kBlockHollow);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416439 times.
416439 assert(allocator_ != NULL);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416439 times.
416439 assert(size > 0);
196
197 416439 type_ = kBlockData;
198 416439 capacity_ = size_ = size;
199 416439 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
200 417115 memcpy(data_, data, size);
201 417115 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
202 417039 }
203
204
205 37385 void BlockItem::Reset() {
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37385 times.
37385 assert(type_ == kBlockData);
207
208 37385 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
209 37403 allocator_->Free(data_);
210 37406 data_ = NULL;
211 37406 size_ = capacity_ = 0;
212 37406 type_ = kBlockHollow;
213 37406 }
214
215
216 1259120 void BlockItem::SetChunkItem(ChunkItem *value) {
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1259120 times.
1259120 assert(value != NULL);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1259120 times.
1259120 assert(chunk_item_ == NULL);
219 1259120 chunk_item_ = value;
220 1259120 }
221
222
223 1807235 void BlockItem::SetFileItem(FileItem *value) {
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1807235 times.
1807235 assert(value != NULL);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1807235 times.
1807235 assert(file_item_ == NULL);
226 1807235 file_item_ = value;
227 1807235 }
228
229
230 512 uint32_t BlockItem::Write(void *buf, uint32_t count) {
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
512 assert(type_ == kBlockData);
232
233 512 uint32_t remaining = capacity_ - size_;
234 512 uint32_t nbytes = std::min(remaining, count);
235 512 memcpy(data_ + size_, buf, nbytes);
236 512 size_ += nbytes;
237 512 return nbytes;
238 }
239