artdaq_core  v3_04_09
ContainerFragment.hh
1 #ifndef artdaq_core_Data_ContainerFragment_hh
2 #define artdaq_core_Data_ContainerFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "cetlib_except/exception.h"
6 
7 //#include <ostream>
8 //#include <vector>
9 
10 // Implementation of "ContainerFragment", an artdaq::Fragment overlay class
11 
12 namespace artdaq
13 {
14  class ContainerFragment;
15 }
16 
21 {
22 public:
23 
24  static constexpr uint8_t CURRENT_VERSION = 1;
25  static constexpr size_t CONTAINER_MAGIC = 0x00BADDEED5B1BEE5;
26 
30  struct MetadataV0
31  {
35  static constexpr int CONTAINER_FRAGMENT_COUNT_MAX = 100;
36 
37  typedef uint8_t data_t;
38  typedef uint64_t count_t;
39 
43 
46 
48  static size_t const size_words = 8ul + CONTAINER_FRAGMENT_COUNT_MAX * sizeof(size_t) / sizeof(data_t); // Units of Header::data_t
49  };
50  static_assert (sizeof(MetadataV0) == MetadataV0::size_words * sizeof(MetadataV0::data_t), "ContainerFragment::MetadataV0 size changed");
51 
55  struct Metadata
56  {
57  typedef uint8_t data_t;
58  typedef uint64_t count_t;
59 
62  count_t version : 4;
64  count_t has_index : 1;
65  count_t unused_flag1 : 1;
66  count_t unused_flag2 : 1;
67  count_t unused : 32;
68 
69  uint64_t index_offset;
70 
72  static size_t const size_words = 16ul; // Units of Header::data_t
73  };
74  static_assert (sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t), "ContainerFragment::Metadata size changed");
75 
76  Metadata const* UpgradeMetadata(MetadataV0 const* in) const
77  {
78  TLOG(TLVL_DEBUG, "ContainerFragment") << "Upgrading ContainerFragment::MetadataV0 into new ContainerFragment::Metadata";
79  assert(in->block_count < std::numeric_limits<Metadata::count_t>::max());
80  metadata_alloc_ = true;
81  Metadata md;
82  md.block_count = in->block_count;
84  md.has_index = 0;
85  md.missing_data = in->missing_data;
86  md.version = 0;
87  index_ptr_ = in->index;
88  metadata_ = new Metadata(md);
89  return metadata_;
90  }
91 
98  explicit ContainerFragment(Fragment const& f) : artdaq_Fragment_(f), index_ptr_(nullptr), index_alloc_(false),metadata_(nullptr), metadata_alloc_(false) { }
99 
100  virtual ~ContainerFragment()
101  {
102  if (index_alloc_)
103  {
104  delete[] index_ptr_;
105  }
106  if (metadata_alloc_)
107  {
108  delete metadata_;
109  }
110  }
111 
116  Metadata const* metadata() const
117  {
118  if (metadata_alloc_) return metadata_;
119 
120  if (artdaq_Fragment_.sizeBytes() - artdaq_Fragment_.dataSizeBytes() - sizeof(detail::RawFragmentHeader) == sizeof(MetadataV0))
121  {
122  return UpgradeMetadata(artdaq_Fragment_.metadata<MetadataV0>());
123  }
124 
125  return artdaq_Fragment_.metadata<Metadata>();
126  }
127 
142  bool missing_data() const { return static_cast<bool>(metadata()->missing_data); }
143 
148  void const* dataBegin() const
149  {
150  return reinterpret_cast<void const *>(&*artdaq_Fragment_.dataBegin());
151  }
152 
157  void const* dataEnd() const
158  {
159  return reinterpret_cast<void const *>(reinterpret_cast<uint8_t const *>(dataBegin()) + lastFragmentIndex());
160  }
161 
168  FragmentPtr at(size_t index) const
169  {
170  if (index >= block_count() || block_count() == 0)
171  {
172  throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::at was asked for a non-existent Fragment!";
173  }
175  memcpy(frag->headerAddress(), reinterpret_cast<uint8_t const *>(dataBegin()) + fragmentIndex(index), fragSize(index));
176  return frag;
177  }
178 
185  size_t fragSize(size_t index) const
186  {
187  if (index >= block_count() || block_count() == 0)
188  {
189  throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::fragSize was asked for a non-existent Fragment!";
190  }
191  auto end = fragmentIndex(index + 1);
192  if (index == 0) return end;
193  return end - fragmentIndex(index);
194  }
195 
202  FragmentPtr operator[](size_t index) const
203  {
204  return this->at(index);
205  }
206 
213  size_t fragmentIndex(size_t index) const
214  {
215  if (index > block_count())
216  {
217  throw cet::exception("ArgumentOutOfRange") << "Buffer overrun detected! ContainerFragment::fragmentIndex was asked for a non-existent Fragment!";
218  }
219  if (index == 0) { return 0; }
220 
221  auto index_ptr = get_index_();
222 
223  return index_ptr[index - 1];
224  }
225 
230  size_t lastFragmentIndex() const
231  {
232  return fragmentIndex(block_count());
233  }
234 
235 protected:
236 
241  static constexpr size_t words_per_frag_word_()
242  {
243  return sizeof(Fragment::value_type) / sizeof(Metadata::data_t);
244  }
245 
246  const size_t* create_index_() const
247  {
248  TLOG(TLVL_TRACE, "ContainerFragment") << "Creating new index for ContainerFragment";
249  auto tmp = new size_t[metadata()->block_count + 1];
250 
251  auto current = reinterpret_cast<uint8_t const*>(artdaq_Fragment_.dataBegin());
252  size_t offset = 0;
253  for (int ii = 0; ii < metadata()->block_count; ++ii)
254  {
255  auto this_size = reinterpret_cast<const detail::RawFragmentHeader*>(current)->word_count * sizeof(RawDataType);
256  offset += this_size;
257  tmp[ii] = offset;
258  current += this_size;
259  }
260  tmp[metadata()->block_count] = CONTAINER_MAGIC;
261  return tmp;
262  }
263 
264  void reset_index_ptr_() const
265  {
266  TLOG(TLVL_TRACE, "ContainerFragment") << "Request to reset index_ptr recieved. has_index=" << metadata()->has_index << ", Check word = " << std::hex
267  << *(reinterpret_cast<size_t const*>(artdaq_Fragment_.dataBeginBytes() + metadata()->index_offset) + metadata()->block_count);
268  if (metadata()->has_index && *(reinterpret_cast<size_t const*>(artdaq_Fragment_.dataBeginBytes() + metadata()->index_offset) + metadata()->block_count) == CONTAINER_MAGIC)
269  {
270  TLOG(TLVL_TRACE, "ContainerFragment") << "Setting index_ptr to found valid index";
271  index_ptr_ = reinterpret_cast<size_t const*>(artdaq_Fragment_.dataBeginBytes() + metadata()->index_offset);
272  }
273  else
274  {
275  TLOG(TLVL_TRACE, "ContainerFragment") << "Index invalid or not found, allocating new index";
276  if (index_alloc_)
277  {
278  delete[] index_ptr_;
279  }
280 
281  index_alloc_ = true;
282  index_ptr_ = create_index_();
283  }
284  }
285 
286  const size_t* get_index_() const
287  {
288  if (index_ptr_ != nullptr) return index_ptr_;
289 
290  reset_index_ptr_();
291 
292  return index_ptr_;
293  }
294 
295 private:
296  Fragment const& artdaq_Fragment_;
297 
298  mutable const size_t* index_ptr_;
299  mutable bool index_alloc_;
300  mutable const Metadata* metadata_;
301  mutable bool metadata_alloc_;
302 };
303 
304 #endif /* artdaq_core_Data_ContainerFragment_hh */
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:53
static constexpr size_t words_per_frag_word_()
Gets the ratio between the fundamental data storage type and the representation within the Fragment...
uint8_t data_t
Basic unit of data-retrieval.
static size_t const size_words
Size of the Metadata object.
size_t fragmentIndex(size_t index) const
Get the offset of a Fragment within the ContainerFragment.
The artdaq::ContainerFragment class represents a Fragment which contains other Fragments.
std::size_t dataSizeBytes() const
Return the number of bytes in the data payload. This does not include the number of bytes in the head...
Definition: Fragment.hh:356
uint64_t count_t
Size of block_count variables.
std::size_t sizeBytes() const
Size of vals_ vector ( header + (optional) metadata + payload) in bytes.
Definition: Fragment.hh:341
byte_t * dataBeginBytes()
Return Fragment::byte_t* pointing at the beginning of the payload.
Definition: Fragment.hh:533
static constexpr std::size_t num_words()
Returns the number of RawDataType words present in the header.
The RawFragmentHeader class contains the basic fields used by artdaq for routing Fragment objects thr...
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
Definition: Fragment.hh:136
bool missing_data() const
Gets the flag if the ContainerFragment knows that it is missing data.
Fragment::type_t fragment_type() const
Get the Fragment::type_t of stored Fragment objects.
size_t index[CONTAINER_FRAGMENT_COUNT_MAX]
Offset of each Fragment within the ContainerFragment.
count_t fragment_type
The Fragment::type_t of stored Fragment objects.
count_t fragment_type
The Fragment::type_t of stored Fragment objects.
size_t lastFragmentIndex() const
Returns the offset of the last Fragment in the ContainerFragment.
count_t block_count
The number of Fragment objects stored in the ContainerFragment.
iterator dataBegin()
Return an iterator to the beginning of the data payload (after header and metadata) ...
Definition: Fragment.hh:1045
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec&lt;RawDataType&gt;
Definition: Fragment.hh:184
void const * dataBegin() const
Gets the start of the data.
Contains the information necessary for retrieving Fragment objects from the ContainerFragment.
void const * dataEnd() const
Gets the last Fragment in the ContainerFragment.
count_t missing_data
Flag if the ContainerFragment knows that it is missing data.
ContainerFragment(Fragment const &f)
Contains the information necessary for retrieving Fragment objects from the ContainerFragment.
count_t missing_data
Flag if the ContainerFragment knows that it is missing data.
Metadata::count_t block_count() const
Gets the number of fragments stored in the ContainerFragment.
T * metadata()
Return a pointer to the metadata. This throws an exception if the Fragment contains no metadata...
Definition: Fragment.hh:920
Metadata const * metadata() const
const getter function for the Metadata
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
Definition: Fragment.hh:39
count_t block_count
The number of Fragment objects stored in the ContainerFragment.
FragmentPtr operator[](size_t index) const
Alias to ContainerFragment::at()
uint64_t count_t
Size of block_count variables.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:84
FragmentPtr at(size_t index) const
Gets a specific Fragment from the ContainerFragment.
static size_t const size_words
Size of the Metadata object.
static constexpr int CONTAINER_FRAGMENT_COUNT_MAX
The maximum capacity of the ContainerFragment (in fragments)
size_t fragSize(size_t index) const
Gets the size of the Fragment at the specified location in the ContainerFragment, in bytes...
uint8_t data_t
Basic unit of data-retrieval.