$treeview $search $mathjax $extrastylesheet
artdaq_core
v3_05_05
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #ifndef artdaq_core_Data_ContainerFragment_hh 00002 #define artdaq_core_Data_ContainerFragment_hh 00003 00004 #include "artdaq-core/Data/Fragment.hh" 00005 #include "cetlib_except/exception.h" 00006 00007 //#include <ostream> 00008 //#include <vector> 00009 00010 // Implementation of "ContainerFragment", an artdaq::Fragment overlay class 00011 00012 namespace artdaq { 00013 class ContainerFragment; 00014 } 00015 00019 class artdaq::ContainerFragment 00020 { 00021 public: 00023 static constexpr uint8_t CURRENT_VERSION = 1; 00025 static constexpr size_t CONTAINER_MAGIC = 0x00BADDEED5B1BEE5; 00026 00030 struct MetadataV0 00031 { 00035 static constexpr int CONTAINER_FRAGMENT_COUNT_MAX = 100; 00036 00037 typedef uint8_t data_t; 00038 typedef uint64_t count_t; 00039 00040 count_t block_count : 55; 00041 count_t fragment_type : 8; 00042 count_t missing_data : 1; 00043 00045 size_t index[CONTAINER_FRAGMENT_COUNT_MAX]; 00046 00048 static size_t const size_words = 8ul + CONTAINER_FRAGMENT_COUNT_MAX * sizeof(size_t) / sizeof(data_t); // Units of Header::data_t 00049 }; 00050 static_assert(sizeof(MetadataV0) == MetadataV0::size_words * sizeof(MetadataV0::data_t), "ContainerFragment::MetadataV0 size changed"); 00051 00055 struct Metadata 00056 { 00057 typedef uint8_t data_t; 00058 typedef uint64_t count_t; 00059 00060 count_t block_count : 16; 00061 count_t fragment_type : 8; 00062 count_t version : 4; 00063 count_t missing_data : 1; 00064 count_t has_index : 1; 00065 count_t unused_flag1 : 1; 00066 count_t unused_flag2 : 1; 00067 count_t unused : 32; 00068 00069 uint64_t index_offset; 00070 00072 static size_t const size_words = 16ul; // Units of Header::data_t 00073 }; 00074 static_assert(sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t), "ContainerFragment::Metadata size changed"); 00075 00081 Metadata const* UpgradeMetadata(MetadataV0 const* in) const 00082 { 00083 TLOG(TLVL_DEBUG, "ContainerFragment") << "Upgrading ContainerFragment::MetadataV0 into new ContainerFragment::Metadata"; 00084 assert(in->block_count < std::numeric_limits<Metadata::count_t>::max()); 00085 metadata_alloc_ = true; 00086 Metadata md; 00087 md.block_count = in->block_count; 00088 md.fragment_type = in->fragment_type; 00089 md.has_index = 0; 00090 md.missing_data = in->missing_data; 00091 md.version = 0; 00092 index_ptr_ = in->index; 00093 metadata_ = new Metadata(md); 00094 return metadata_; 00095 } 00096 00103 explicit ContainerFragment(Fragment const& f) 00104 : artdaq_Fragment_(f), index_ptr_(nullptr), index_alloc_(false), metadata_(nullptr), metadata_alloc_(false) {} 00105 00106 virtual ~ContainerFragment() 00107 { 00108 if (index_alloc_) 00109 { 00110 delete[] index_ptr_; 00111 } 00112 if (metadata_alloc_) 00113 { 00114 delete metadata_; 00115 } 00116 } 00117 00122 Metadata const* metadata() const 00123 { 00124 if (metadata_alloc_) return metadata_; 00125 00126 if (artdaq_Fragment_.sizeBytes() - artdaq_Fragment_.dataSizeBytes() - artdaq_Fragment_.headerSizeBytes() == sizeof(MetadataV0)) 00127 { 00128 return UpgradeMetadata(artdaq_Fragment_.metadata<MetadataV0>()); 00129 } 00130 00131 return artdaq_Fragment_.metadata<Metadata>(); 00132 } 00133 00138 Metadata::count_t block_count() const { return metadata()->block_count; } 00143 Fragment::type_t fragment_type() const { return static_cast<Fragment::type_t>(metadata()->fragment_type); } 00148 bool missing_data() const { return static_cast<bool>(metadata()->missing_data); } 00149 00154 void const* dataBegin() const 00155 { 00156 return reinterpret_cast<void const*>(&*artdaq_Fragment_.dataBegin()); 00157 } 00158 00163 void const* dataEnd() const 00164 { 00165 return reinterpret_cast<void const*>(reinterpret_cast<uint8_t const*>(dataBegin()) + lastFragmentIndex()); 00166 } 00167 00174 FragmentPtr at(size_t index) const 00175 { 00176 if (index >= block_count() || block_count() == 0) 00177 { 00178 throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::at was asked for a non-existent Fragment!"; 00179 } 00180 // Subtract RawFragmentHeader::num_words here as Fragment consturctor will allocate n + detail::RawFragmentHeader::num_words(), and we want fragSize to be allocated. 00181 FragmentPtr frag(new Fragment((fragSize(index)) / sizeof(RawDataType) - detail::RawFragmentHeader::num_words())); 00182 memcpy(frag->headerAddress(), reinterpret_cast<uint8_t const*>(dataBegin()) + fragmentIndex(index), fragSize(index)); 00183 return frag; 00184 } 00185 00192 size_t fragSize(size_t index) const 00193 { 00194 if (index >= block_count() || block_count() == 0) 00195 { 00196 throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::fragSize was asked for a non-existent Fragment!"; 00197 } 00198 auto end = fragmentIndex(index + 1); 00199 if (index == 0) return end; 00200 return end - fragmentIndex(index); 00201 } 00202 00209 FragmentPtr operator[](size_t index) const 00210 { 00211 return this->at(index); 00212 } 00213 00220 size_t fragmentIndex(size_t index) const 00221 { 00222 if (index > block_count()) 00223 { 00224 throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::fragmentIndex was asked for a non-existent Fragment!"; 00225 } 00226 if (index == 0) { return 0; } 00227 00228 auto index_ptr = get_index_(); 00229 00230 return index_ptr[index - 1]; 00231 } 00232 00237 size_t lastFragmentIndex() const 00238 { 00239 return fragmentIndex(block_count()); 00240 } 00241 00242 protected: 00247 static constexpr size_t words_per_frag_word_() 00248 { 00249 return sizeof(Fragment::value_type) / sizeof(Metadata::data_t); 00250 } 00251 00256 const size_t* create_index_() const 00257 { 00258 TLOG(TLVL_TRACE, "ContainerFragment") << "Creating new index for ContainerFragment"; 00259 auto tmp = new size_t[metadata()->block_count + 1]; 00260 00261 auto current = reinterpret_cast<uint8_t const*>(artdaq_Fragment_.dataBegin()); 00262 size_t offset = 0; 00263 for (int ii = 0; ii < metadata()->block_count; ++ii) 00264 { 00265 auto this_size = reinterpret_cast<const detail::RawFragmentHeader*>(current)->word_count * sizeof(RawDataType); 00266 offset += this_size; 00267 tmp[ii] = offset; 00268 current += this_size; 00269 } 00270 tmp[metadata()->block_count] = CONTAINER_MAGIC; 00271 return tmp; 00272 } 00273 00278 void reset_index_ptr_() const 00279 { 00280 TLOG(TLVL_TRACE, "ContainerFragment") << "Request to reset index_ptr recieved. has_index=" << metadata()->has_index << ", Check word = " << std::hex 00281 << *(reinterpret_cast<size_t const*>(artdaq_Fragment_.dataBeginBytes() + metadata()->index_offset) + metadata()->block_count); 00282 if (metadata()->has_index && *(reinterpret_cast<size_t const*>(artdaq_Fragment_.dataBeginBytes() + metadata()->index_offset) + metadata()->block_count) == CONTAINER_MAGIC) 00283 { 00284 TLOG(TLVL_TRACE, "ContainerFragment") << "Setting index_ptr to found valid index"; 00285 index_ptr_ = reinterpret_cast<size_t const*>(artdaq_Fragment_.dataBeginBytes() + metadata()->index_offset); 00286 } 00287 else 00288 { 00289 TLOG(TLVL_TRACE, "ContainerFragment") << "Index invalid or not found, allocating new index"; 00290 if (index_alloc_) 00291 { 00292 delete[] index_ptr_; 00293 } 00294 00295 index_alloc_ = true; 00296 index_ptr_ = create_index_(); 00297 } 00298 } 00299 00304 const size_t* get_index_() const 00305 { 00306 if (index_ptr_ != nullptr) return index_ptr_; 00307 00308 reset_index_ptr_(); 00309 00310 return index_ptr_; 00311 } 00312 00313 private: 00314 Fragment const& artdaq_Fragment_; 00315 00316 mutable const size_t* index_ptr_; 00317 mutable bool index_alloc_; 00318 mutable const Metadata* metadata_; 00319 mutable bool metadata_alloc_; 00320 }; 00321 00322 #endif /* artdaq_core_Data_ContainerFragment_hh */