artdaq_core  v3_00_03
SharedMemoryFragmentManager_t.cc
1 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
2 
3 #define BOOST_TEST_MODULE(SharedMemoryFragmentManager_t)
4 #include "cetlib/quiet_unit_test.hpp"
5 #include "cetlib_except/exception.h"
6 
7 
8 BOOST_AUTO_TEST_SUITE(SharedMemoryFragmentManager_test)
9 
10 BOOST_AUTO_TEST_CASE(Construct)
11 {
12  srand(time(0));
13  artdaq::SharedMemoryFragmentManager man(0x7357 + rand() % 0x10000000, 10, 0x1000);
14  BOOST_REQUIRE_EQUAL(man.IsValid(), true);
15  BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
16  BOOST_REQUIRE_EQUAL(man.size(), 10);
17  BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 0);
18 }
19 
20 BOOST_AUTO_TEST_CASE(Attach)
21 {
22  srand(time(0));
23  uint32_t key = 0x7357 + rand() % 0x10000000;
24  artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
25  artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
26 
27  BOOST_REQUIRE_EQUAL(man.IsValid(), true);
28  BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
29  BOOST_REQUIRE_EQUAL(man.size(), 10);
30  BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 1);
31 
32  BOOST_REQUIRE_EQUAL(man2.IsValid(), true);
33  BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1);
34  BOOST_REQUIRE_EQUAL(man2.size(), 10);
35  BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 1);
36 
37 }
38 
39 BOOST_AUTO_TEST_CASE(DataFlow)
40 {
41  srand(time(0));
42  std::cout << "Initializing SharedMemoryFragmentManagers for DataFlow test" << std::endl;
43  uint32_t key = 0x7357 + rand() % 0x10000000;
44  artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
45  artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
46 
47  auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
48 
49  std::cout << "Creating test Fragment" << std::endl;
50  artdaq::Fragment frag(fragSizeWords);
51  frag.setSequenceID(0x10);
52  frag.setFragmentID(0x20);
54  frag.setSystemType(type);
55  frag.setTimestamp(0x30);
56  for (size_t ii = 0; ii < fragSizeWords; ++ii)
57  {
58  *(frag.dataBegin() + ii) = ii;
59  }
60 
61  std::cout << "Writing Test Fragment to Shared Memory" << std::endl;
62  man.WriteFragment(std::move(frag), false);
63 
64  std::cout << "Reading Test Fragment Header" << std::endl;
66  auto sts = man2.ReadFragmentHeader(header);
67 
68  std::cout << "Checking Test Fragment Header Contents" << std::endl;
69  BOOST_REQUIRE_EQUAL(sts, 0);
70  BOOST_REQUIRE_EQUAL(header.word_count, frag.size());
71  BOOST_REQUIRE_EQUAL(header.sequence_id, 0x10);
72  BOOST_REQUIRE_EQUAL(header.fragment_id, 0x20);
73  BOOST_REQUIRE_EQUAL(header.type, type);
74  BOOST_REQUIRE_EQUAL(header.timestamp, 0x30);
75 
76  std::cout << "Reading Test Fragment data" << std::endl;
77  artdaq::Fragment frag2(header.word_count);
78  sts = man2.ReadFragmentData(frag2.dataBegin(), header.word_count - header.num_words());
79 
80  std::cout << "Checking Test Fragment contents" << std::endl;
81  BOOST_REQUIRE_EQUAL(sts, 0);
82  for(size_t ii = 0; ii < fragSizeWords; ++ii)
83  {
84  BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(frag2.dataBegin() + ii));
85  }
86  std::cout << "SharedMemoryFragmentManager test complete" << std::endl;
87 }
88 
89 BOOST_AUTO_TEST_CASE(WholeFragment)
90 {
91  std::cout << "Initializing SharedMemoryFragmentManagers for WholeFragment Test" << std::endl;
92  srand(time(0));
93  uint32_t key = 0x7357 + rand() % 0x10000000;
94  artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
95  artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
96 
97  auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
98 
99  std::cout << "Creating test Fragment" << std::endl;
100  artdaq::Fragment frag(fragSizeWords);
101  frag.setSequenceID(0x10);
102  frag.setFragmentID(0x20);
104  frag.setSystemType(type);
105  frag.setTimestamp(0x30);
106  for (size_t ii = 0; ii < fragSizeWords; ++ii)
107  {
108  *(frag.dataBegin() + ii) = ii;
109  }
110 
111  std::cout << "Writing Test Fragment to Shared Memory" << std::endl;
112  man.WriteFragment(std::move(frag), false);
113 
114  std::cout << "Reading Test Fragment Header" << std::endl;
115  artdaq::Fragment recvdFrag;
116  auto sts = man2.ReadFragment(recvdFrag);
117 
118  std::cout << "Checking Test Fragment Header Contents" << std::endl;
119  BOOST_REQUIRE_EQUAL(sts, 0);
120  BOOST_REQUIRE_EQUAL(recvdFrag.size(), frag.size());
121  BOOST_REQUIRE_EQUAL(recvdFrag.sequenceID(), 0x10);
122  BOOST_REQUIRE_EQUAL(recvdFrag.fragmentID(), 0x20);
123  BOOST_REQUIRE_EQUAL(recvdFrag.type(), type);
124  BOOST_REQUIRE_EQUAL(recvdFrag.timestamp(), 0x30);
125 
126  std::cout << "Checking Test Fragment Data Contents" << std::endl;
127  for (size_t ii = 0; ii < fragSizeWords; ++ii)
128  {
129  //std::cout << std::to_string(*(frag.dataBegin() + ii)) << " =?= " << *(recvdFrag.dataBegin() + ii) << std::endl;
130  BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(recvdFrag.dataBegin() + ii));
131  }
132  std::cout << "SharedMemoryFragmentManager test complete" << std::endl;
133 }
134 
135 BOOST_AUTO_TEST_SUITE_END()
RawDataType word_count
number of RawDataType words in this Fragment
RawDataType type
The type of the fragment, either system or user-defined.
std::size_t size() const
Gets the size of the Fragment, from the Fragment header.
Definition: Fragment.hh:798
The SharedMemoryFragmentManager is a SharedMemoryManager that deals with Fragment transfers using a S...
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...
RawDataType timestamp
The 64-bit timestamp field is the output of a user-defined clock used for building time-correlated ev...
RawDataType sequence_id
The 48-bit sequence_id uniquely identifies events within the artdaq system.
sequence_id_t sequenceID() const
Sequence ID of the Fragment, from the Fragment header.
Definition: Fragment.hh:826
timestamp_t timestamp() const
Timestamp of the Fragment, from the Fragment header.
Definition: Fragment.hh:840
static constexpr type_t DataFragmentType
Copy DataFragmentType from RawFragmentHeader.
Definition: Fragment.hh:148
RawDataType fragment_id
The fragment_id uniquely identifies a particular piece of hardware within the artdaq system...
iterator dataBegin()
Return an iterator to the beginning of the data payload (after header and metadata) ...
Definition: Fragment.hh:1025
type_t type() const
Type of the Fragment, from the Fragment header.
Definition: Fragment.hh:812
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
Definition: Fragment.hh:39
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:84
fragment_id_t fragmentID() const
Fragment ID of the Fragment, from the Fragment header.
Definition: Fragment.hh:833