artdaq_core  v3_01_04
SharedMemoryFragmentManager_t.cc
1 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
2 #include "artdaq-core/Utilities/configureMessageFacility.hh"
3 #include "tracemf.h"
4 
5 #define BOOST_TEST_MODULE(SharedMemoryFragmentManager_t)
6 #include "cetlib/quiet_unit_test.hpp"
7 #include "cetlib_except/exception.h"
8 
9 
10 BOOST_AUTO_TEST_SUITE(SharedMemoryFragmentManager_test)
11 
12 BOOST_AUTO_TEST_CASE(Construct)
13 {
14  artdaq::configureMessageFacility("SharedMemoryFragmentManager_t", true, true);
15  TLOG_DEBUG("SharedMemoryManager_t") << "BEGIN TEST Construct" << TLOG_ENDL;
16  srand(time(0));
17  artdaq::SharedMemoryFragmentManager man(0x7357 + rand() % 0x10000000, 10, 0x1000);
18  BOOST_REQUIRE_EQUAL(man.IsValid(), true);
19  BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
20  BOOST_REQUIRE_EQUAL(man.size(), 10);
21  BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 0);
22 }
23 
24 BOOST_AUTO_TEST_CASE(Attach)
25 {
26  srand(time(0));
27  uint32_t key = 0x7357 + rand() % 0x10000000;
28  artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
29  artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
30 
31  BOOST_REQUIRE_EQUAL(man.IsValid(), true);
32  BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
33  BOOST_REQUIRE_EQUAL(man.size(), 10);
34  BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 1);
35 
36  BOOST_REQUIRE_EQUAL(man2.IsValid(), true);
37  BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1);
38  BOOST_REQUIRE_EQUAL(man2.size(), 10);
39  BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 1);
40 
41 }
42 
43 BOOST_AUTO_TEST_CASE(DataFlow)
44 {
45  srand(time(0));
46  std::cout << "Initializing SharedMemoryFragmentManagers for DataFlow test" << std::endl;
47  uint32_t key = 0x7357 + rand() % 0x10000000;
48  artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
49  artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
50 
51  auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
52 
53  std::cout << "Creating test Fragment" << std::endl;
54  artdaq::Fragment frag(fragSizeWords);
55  frag.setSequenceID(0x10);
56  frag.setFragmentID(0x20);
58  frag.setSystemType(type);
59  frag.setTimestamp(0x30);
60  for (size_t ii = 0; ii < fragSizeWords; ++ii)
61  {
62  *(frag.dataBegin() + ii) = ii;
63  }
64 
65  std::cout << "Writing Test Fragment to Shared Memory" << std::endl;
66  man.WriteFragment(std::move(frag), false);
67 
68  std::cout << "Reading Test Fragment Header" << std::endl;
70  auto sts = man2.ReadFragmentHeader(header);
71 
72  std::cout << "Checking Test Fragment Header Contents" << std::endl;
73  BOOST_REQUIRE_EQUAL(sts, 0);
74  BOOST_REQUIRE_EQUAL(header.word_count, frag.size());
75  BOOST_REQUIRE_EQUAL(header.sequence_id, 0x10);
76  BOOST_REQUIRE_EQUAL(header.fragment_id, 0x20);
77  BOOST_REQUIRE_EQUAL(header.type, type);
78  BOOST_REQUIRE_EQUAL(header.timestamp, 0x30);
79 
80  std::cout << "Reading Test Fragment data" << std::endl;
81  artdaq::Fragment frag2(header.word_count);
82  sts = man2.ReadFragmentData(frag2.dataBegin(), header.word_count - header.num_words());
83 
84  std::cout << "Checking Test Fragment contents" << std::endl;
85  BOOST_REQUIRE_EQUAL(sts, 0);
86  for(size_t ii = 0; ii < fragSizeWords; ++ii)
87  {
88  BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(frag2.dataBegin() + ii));
89  }
90  std::cout << "SharedMemoryFragmentManager test complete" << std::endl;
91 }
92 
93 BOOST_AUTO_TEST_CASE(WholeFragment)
94 {
95  std::cout << "Initializing SharedMemoryFragmentManagers for WholeFragment Test" << std::endl;
96  srand(time(0));
97  uint32_t key = 0x7357 + rand() % 0x10000000;
98  artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
99  artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
100 
101  auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
102 
103  std::cout << "Creating test Fragment" << std::endl;
104  artdaq::Fragment frag(fragSizeWords);
105  frag.setSequenceID(0x10);
106  frag.setFragmentID(0x20);
108  frag.setSystemType(type);
109  frag.setTimestamp(0x30);
110  for (size_t ii = 0; ii < fragSizeWords; ++ii)
111  {
112  *(frag.dataBegin() + ii) = ii;
113  }
114 
115  std::cout << "Writing Test Fragment to Shared Memory" << std::endl;
116  man.WriteFragment(std::move(frag), false);
117 
118  std::cout << "Reading Test Fragment Header" << std::endl;
119  artdaq::Fragment recvdFrag;
120  auto sts = man2.ReadFragment(recvdFrag);
121 
122  std::cout << "Checking Test Fragment Header Contents" << std::endl;
123  BOOST_REQUIRE_EQUAL(sts, 0);
124  BOOST_REQUIRE_EQUAL(recvdFrag.size(), frag.size());
125  BOOST_REQUIRE_EQUAL(recvdFrag.sequenceID(), 0x10);
126  BOOST_REQUIRE_EQUAL(recvdFrag.fragmentID(), 0x20);
127  BOOST_REQUIRE_EQUAL(recvdFrag.type(), type);
128  BOOST_REQUIRE_EQUAL(recvdFrag.timestamp(), 0x30);
129 
130  std::cout << "Checking Test Fragment Data Contents" << std::endl;
131  for (size_t ii = 0; ii < fragSizeWords; ++ii)
132  {
133  //std::cout << std::to_string(*(frag.dataBegin() + ii)) << " =?= " << *(recvdFrag.dataBegin() + ii) << std::endl;
134  BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(recvdFrag.dataBegin() + ii));
135  }
136  std::cout << "SharedMemoryFragmentManager test complete" << std::endl;
137 }
138 
139 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
void configureMessageFacility(char const *progname, bool useConsole=true, bool printDebug=false)
Configure and start the message facility. Provide the program name so that messages will be appropria...
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