GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2025-07-06 02:35:01
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 7590811 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 7590811 bool has_legacy_bulk_chunk)
26 7590811 : source_(source)
27 7590811 , compression_algorithm_(compression_algorithm)
28 7590811 , hash_algorithm_(hash_algorithm)
29 7590811 , hash_suffix_(hash_suffix)
30 7590811 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
31 7590811 , size_(kSizeUnknown)
32 7590811 , may_have_chunks_(may_have_chunks)
33
1/2
✓ Branch 1 taken 7590811 times.
✗ Branch 2 not taken.
7590811 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
34
1/2
✓ Branch 1 taken 7590811 times.
✗ Branch 2 not taken.
7590811 , bulk_hash_(hash_algorithm)
35
1/2
✓ Branch 2 taken 7590811 times.
✗ Branch 3 not taken.
15181622 , chunks_(1) {
36 7590811 const int retval = pthread_mutex_init(&lock_, NULL);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7590811 times.
7590811 assert(retval == 0);
38 7590811 atomic_init64(&nchunks_in_fly_);
39 7590811 atomic_init32(&is_fully_chunked_);
40 7590811 }
41
42 7585700 FileItem::~FileItem() { pthread_mutex_destroy(&lock_); }
43
44 7514606 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
45 7514606 const MutexLockGuard lock_guard(lock_);
46
47
2/2
✓ Branch 1 taken 12430 times.
✓ Branch 2 taken 7502176 times.
7514606 switch (file_chunk.content_hash().suffix) {
48 12430 case shash::kSuffixPartial:
49
1/2
✓ Branch 1 taken 12430 times.
✗ Branch 2 not taken.
12430 chunks_.PushBack(file_chunk);
50 12430 break;
51
52 7502176 default:
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7502176 times.
7502176 assert(file_chunk.offset() == 0);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7502176 times.
7502176 assert(file_chunk.size() == size_);
55 7502176 bulk_hash_ = file_chunk.content_hash();
56 7502176 break;
57 }
58 7514606 atomic_dec64(&nchunks_in_fly_);
59 7514606 }
60
61
62 //------------------------------------------------------------------------------
63
64
65 7526971 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
66 7523281 : file_item_(file_item)
67 7523281 , offset_(offset)
68 7523281 , size_(0)
69 7523281 , is_bulk_chunk_(false)
70 7523281 , upload_handle_(NULL)
71
1/2
✓ Branch 4 taken 7525321 times.
✗ Branch 5 not taken.
7526971 , compressor_(NULL) {
72 7525321 hash_ctx_.algorithm = file_item->hash_algorithm();
73
1/2
✓ Branch 1 taken 7520791 times.
✗ Branch 2 not taken.
7523281 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
74 7520791 hash_ctx_.buffer = hash_ctx_buffer_;
75
1/2
✓ Branch 1 taken 7519831 times.
✗ Branch 2 not taken.
7520791 shash::Init(hash_ctx_);
76 7519831 hash_value_.algorithm = hash_ctx_.algorithm;
77 7519831 hash_value_.suffix = shash::kSuffixPartial;
78 7519831 file_item_->IncNchunksInFly();
79 7534591 }
80
81
82 7487161 void ChunkItem::MakeBulkChunk() {
83 7487161 is_bulk_chunk_ = true;
84 7487161 hash_value_.suffix = file_item_->hash_suffix();
85 7485601 }
86
87
88 17313014 zlib::Compressor *ChunkItem::GetCompressor() {
89
2/2
✓ Branch 1 taken 7485531 times.
✓ Branch 2 taken 9840413 times.
17313014 if (!compressor_.IsValid()) {
90 compressor_ = zlib::Compressor::Construct(
91
2/4
✓ Branch 2 taken 7503321 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7497171 times.
✗ Branch 6 not taken.
7485531 file_item_->compression_algorithm());
92 }
93 17337584 return compressor_.weak_ref();
94 }
95
96
97 7494591 void ChunkItem::ReleaseCompressor() { compressor_.Destroy(); }
98
99
100 //------------------------------------------------------------------------------
101
102 atomic_int64 BlockItem::managed_bytes_ = 0;
103
104
105 119800 BlockItem::BlockItem(ItemAllocator *allocator)
106 119800 : allocator_(allocator)
107 119800 , type_(kBlockHollow)
108 119800 , tag_(-1)
109 119800 , file_item_(NULL)
110 119800 , chunk_item_(NULL)
111 119800 , data_(NULL)
112 119800 , capacity_(0)
113 119800 , size_(0) { }
114
115
116 52977935 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
117 52706550 : allocator_(allocator)
118 52706550 , type_(kBlockHollow)
119 52706550 , tag_(tag)
120 52706550 , file_item_(NULL)
121 52706550 , chunk_item_(NULL)
122 52706550 , data_(NULL)
123 52706550 , capacity_(0)
124 52977935 , size_(0) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52706550 times.
52706550 assert(tag_ >= 0);
126 52706550 }
127
128
129 52839951 BlockItem::~BlockItem() {
130
2/2
✓ Branch 0 taken 21764874 times.
✓ Branch 1 taken 31075077 times.
52839951 if (data_)
131 21764874 allocator_->Free(data_);
132 52870071 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
133 53303970 }
134
135
136 8271583 void BlockItem::Discharge() {
137 8271583 data_ = NULL;
138 8271583 size_ = capacity_ = 0;
139 8271583 }
140
141
142 22415488 void BlockItem::MakeStop() {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22415488 times.
22415488 assert(type_ == kBlockHollow);
144 22415488 type_ = kBlockStop;
145 22415488 }
146
147
148 11586306 void BlockItem::MakeData(uint32_t capacity) {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11586306 times.
11586306 assert(type_ == kBlockHollow);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11586306 times.
11586306 assert(allocator_ != NULL);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11586306 times.
11586306 assert(capacity > 0);
152
153 11586306 type_ = kBlockData;
154 11586306 capacity_ = capacity;
155 11586306 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
156 11619366 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
157 11623416 }
158
159
160 /**
161 * Move data from one block to another.
162 */
163 8269063 void BlockItem::MakeDataMove(BlockItem *other) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8269063 times.
8269063 assert(type_ == kBlockHollow);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8269063 times.
8269063 assert(other->type_ == kBlockData);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8269063 times.
8269063 assert(other->size_ > 0);
167
168 8269063 type_ = kBlockData;
169 8269063 capacity_ = size_ = other->size_;
170 8269063 data_ = other->data_;
171 8269063 allocator_ = other->allocator_;
172
173 8269063 other->Discharge();
174 8266783 }
175
176
177 /**
178 * Copy a piece of one block's data into a new block.
179 */
180 11035348 void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11035348 times.
11035348 assert(type_ == kBlockHollow);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11035348 times.
11035348 assert(allocator_ != NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11035348 times.
11035348 assert(size > 0);
184
185 11035348 type_ = kBlockData;
186 11035348 capacity_ = size_ = size;
187 11035348 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
188 11054028 memcpy(data_, data, size);
189 11054028 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
190 11054958 }
191
192
193 871420 void BlockItem::Reset() {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 871420 times.
871420 assert(type_ == kBlockData);
195
196 871420 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
197 871780 allocator_->Free(data_);
198 871750 data_ = NULL;
199 871750 size_ = capacity_ = 0;
200 871750 type_ = kBlockHollow;
201 871750 }
202
203
204 36368011 void BlockItem::SetChunkItem(ChunkItem *value) {
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36368011 times.
36368011 assert(value != NULL);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36368011 times.
36368011 assert(chunk_item_ == NULL);
207 36368011 chunk_item_ = value;
208 36368011 }
209
210
211 52557890 void BlockItem::SetFileItem(FileItem *value) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52557890 times.
52557890 assert(value != NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52557890 times.
52557890 assert(file_item_ == NULL);
214 52557890 file_item_ = value;
215 52557890 }
216
217
218 2560 uint32_t BlockItem::Write(void *buf, uint32_t count) {
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2560 times.
2560 assert(type_ == kBlockData);
220
221 2560 const uint32_t remaining = capacity_ - size_;
222 2560 const uint32_t nbytes = std::min(remaining, count);
223 2560 memcpy(data_ + size_, buf, nbytes);
224 2560 size_ += nbytes;
225 2560 return nbytes;
226 }
227