GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2026-02-01 02:35:56
Exec Total Coverage
Lines: 142 142 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 9858315 FileItem::FileItem(IngestionSource *source,
18 uint64_t min_chunk_size,
19 uint64_t avg_chunk_size,
20 uint64_t max_chunk_size,
21 zlib::Algorithms compression_algorithm,
22 shash::Algorithms hash_algorithm,
23 shash::Suffix hash_suffix,
24 bool may_have_chunks,
25 9858315 bool has_legacy_bulk_chunk)
26 9858315 : source_(source)
27 9858315 , compression_algorithm_(compression_algorithm)
28 9858315 , hash_algorithm_(hash_algorithm)
29 9858315 , hash_suffix_(hash_suffix)
30 9858315 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
31 9858315 , size_(kSizeUnknown)
32 9858315 , may_have_chunks_(may_have_chunks)
33
1/2
✓ Branch 1 taken 9858315 times.
✗ Branch 2 not taken.
9858315 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
34
1/2
✓ Branch 1 taken 9858315 times.
✗ Branch 2 not taken.
9858315 , bulk_hash_(hash_algorithm)
35
1/2
✓ Branch 2 taken 9858315 times.
✗ Branch 3 not taken.
19716630 , chunks_(1) {
36 9858315 const int retval = pthread_mutex_init(&lock_, NULL);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9858315 times.
9858315 assert(retval == 0);
38 9858315 atomic_init64(&nchunks_in_fly_);
39 9858315 atomic_init32(&is_fully_chunked_);
40 9858315 }
41
42 9854007 FileItem::~FileItem() { pthread_mutex_destroy(&lock_); }
43
44 9770306 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
45 9770306 const MutexLockGuard lock_guard(lock_);
46
47
2/2
✓ Branch 1 taken 17519 times.
✓ Branch 2 taken 9752787 times.
9770306 switch (file_chunk.content_hash().suffix) {
48 17519 case shash::kSuffixPartial:
49
1/2
✓ Branch 1 taken 17519 times.
✗ Branch 2 not taken.
17519 chunks_.PushBack(file_chunk);
50 17519 break;
51
52 9752787 default:
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9752787 times.
9752787 assert(file_chunk.offset() == 0);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9752787 times.
9752787 assert(file_chunk.size() == size_);
55 9752787 bulk_hash_ = file_chunk.content_hash();
56 9752787 break;
57 }
58 9770306 atomic_dec64(&nchunks_in_fly_);
59 9770306 }
60
61
62 //------------------------------------------------------------------------------
63
64
65 9998724 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
66 9993303 : file_item_(file_item)
67 9993303 , offset_(offset)
68 9993303 , size_(0)
69 9993303 , is_bulk_chunk_(false)
70 9993303 , upload_handle_(NULL)
71
1/2
✓ Branch 4 taken 9993927 times.
✗ Branch 5 not taken.
9998724 , compressor_(NULL) {
72 9993927 hash_ctx_.algorithm = file_item->hash_algorithm();
73
1/2
✓ Branch 1 taken 9993966 times.
✗ Branch 2 not taken.
9994278 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
74 9993966 hash_ctx_.buffer = hash_ctx_buffer_;
75
1/2
✓ Branch 1 taken 9989832 times.
✗ Branch 2 not taken.
9993966 shash::Init(hash_ctx_);
76 9989832 hash_value_.algorithm = hash_ctx_.algorithm;
77 9989832 hash_value_.suffix = shash::kSuffixPartial;
78 9989832 file_item_->IncNchunksInFly();
79 10007967 }
80
81
82 9729534 void ChunkItem::MakeBulkChunk() {
83 9729534 is_bulk_chunk_ = true;
84 9729534 hash_value_.suffix = file_item_->hash_suffix();
85 9727701 }
86
87
88 22522216 zlib::Compressor *ChunkItem::GetCompressor() {
89
2/2
✓ Branch 1 taken 9724756 times.
✓ Branch 2 taken 12801672 times.
22522216 if (!compressor_.IsValid()) {
90 compressor_ = zlib::Compressor::Construct(
91
2/4
✓ Branch 2 taken 9758218 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9746050 times.
✗ Branch 6 not taken.
9724756 file_item_->compression_algorithm());
92 }
93 22547722 return compressor_.weak_ref();
94 }
95
96
97 9738952 void ChunkItem::ReleaseCompressor() { compressor_.Destroy(); }
98
99
100 //------------------------------------------------------------------------------
101
102 atomic_int64 BlockItem::managed_bytes_ = 0;
103
104
105 135048 BlockItem::BlockItem(ItemAllocator *allocator)
106 135048 : allocator_(allocator)
107 135048 , type_(kBlockHollow)
108 135048 , tag_(-1)
109 135048 , file_item_(NULL)
110 135048 , chunk_item_(NULL)
111 135048 , data_(NULL)
112 135048 , capacity_(0)
113 135048 , size_(0) { }
114
115
116 71541102 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
117 71078098 : allocator_(allocator)
118 71078098 , type_(kBlockHollow)
119 71078098 , tag_(tag)
120 71078098 , file_item_(NULL)
121 71078098 , chunk_item_(NULL)
122 71078098 , data_(NULL)
123 71078098 , capacity_(0)
124 71541102 , size_(0) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71078098 times.
71078098 assert(tag_ >= 0);
126 71078098 }
127
128
129 71134901 BlockItem::~BlockItem() {
130
2/2
✓ Branch 0 taken 30315870 times.
✓ Branch 1 taken 40819031 times.
71134901 if (data_)
131 30315870 allocator_->Free(data_);
132 71192309 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
133 71937487 }
134
135
136 10742968 void BlockItem::Discharge() {
137 10742968 data_ = NULL;
138 10742968 size_ = capacity_ = 0;
139 10742968 }
140
141
142 29271461 void BlockItem::MakeStop() {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29271461 times.
29271461 assert(type_ == kBlockHollow);
144 29271461 type_ = kBlockStop;
145 29271461 }
146
147
148 15047485 void BlockItem::MakeData(uint32_t capacity) {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15047485 times.
15047485 assert(type_ == kBlockHollow);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15047485 times.
15047485 assert(allocator_ != NULL);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15047485 times.
15047485 assert(capacity > 0);
152
153 15047485 type_ = kBlockData;
154 15047485 capacity_ = capacity;
155 15047485 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
156 15094402 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
157 15099706 }
158
159
160 /**
161 * Move data from one block to another.
162 */
163 10740823 void BlockItem::MakeDataMove(BlockItem *other) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740823 times.
10740823 assert(type_ == kBlockHollow);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740823 times.
10740823 assert(other->type_ == kBlockData);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740823 times.
10740823 assert(other->size_ > 0);
167
168 10740823 type_ = kBlockData;
169 10740823 capacity_ = size_ = other->size_;
170 10740823 data_ = other->data_;
171 10740823 allocator_ = other->allocator_;
172
173 10740823 other->Discharge();
174 10743943 }
175
176
177 /**
178 * Copy a piece of one block's data into a new block.
179 */
180 16820947 void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16820947 times.
16820947 assert(type_ == kBlockHollow);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16820947 times.
16820947 assert(allocator_ != NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16820947 times.
16820947 assert(size > 0);
184
185 16820947 type_ = kBlockData;
186 16820947 capacity_ = size_ = size;
187 16820947 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
188 16847343 memcpy(data_, data, size);
189 16847343 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
190 16844565 }
191
192
193 1558643 void BlockItem::Reset() {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1558643 times.
1558643 assert(type_ == kBlockData);
195
196 1558643 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
197 1559072 allocator_->Free(data_);
198 1558682 data_ = NULL;
199 1558682 size_ = capacity_ = 0;
200 1558682 type_ = kBlockHollow;
201 1558682 }
202
203
204 49506805 void BlockItem::SetChunkItem(ChunkItem *value) {
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49506805 times.
49506805 assert(value != NULL);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49506805 times.
49506805 assert(chunk_item_ == NULL);
207 49506805 chunk_item_ = value;
208 49506805 }
209
210
211 70837994 void BlockItem::SetFileItem(FileItem *value) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70837994 times.
70837994 assert(value != NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70837994 times.
70837994 assert(file_item_ == NULL);
214 70837994 file_item_ = value;
215 70837994 }
216
217
218 25088 uint32_t BlockItem::Write(void *buf, uint32_t count) {
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25088 times.
25088 assert(type_ == kBlockData);
220
221 25088 const uint32_t remaining = capacity_ - size_;
222 25088 const uint32_t nbytes = std::min(remaining, count);
223 25088 memcpy(data_ + size_, buf, nbytes);
224 25088 size_ += nbytes;
225 25088 return nbytes;
226 }
227