$treeview $search $mathjax $extrastylesheet
artdaq_core
v3_05_08a
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #define TRACE_NAME "SharedMemoryFragmentManager_t" 00002 00003 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh" 00004 #include "artdaq-core/Utilities/configureMessageFacility.hh" 00005 #include "tracemf.h" 00006 00007 #define BOOST_TEST_MODULE(SharedMemoryFragmentManager_t) 00008 #include "SharedMemoryTestShims.hh" 00009 #include "cetlib/quiet_unit_test.hpp" 00010 #include "cetlib_except/exception.h" 00011 00012 BOOST_AUTO_TEST_SUITE(SharedMemoryFragmentManager_test) 00013 00014 BOOST_AUTO_TEST_CASE(Construct) 00015 { 00016 artdaq::configureMessageFacility("SharedMemoryFragmentManager_t", true, true); 00017 TLOG(TLVL_INFO) << "BEGIN TEST Construct"; 00018 artdaq::SharedMemoryFragmentManager man(GetRandomKey(0xF4A6), 10, 0x1000); 00019 BOOST_REQUIRE_EQUAL(man.IsValid(), true); 00020 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0); 00021 BOOST_REQUIRE_EQUAL(man.size(), 10); 00022 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 1); 00023 TLOG(TLVL_INFO) << "END TEST Construct"; 00024 } 00025 00026 BOOST_AUTO_TEST_CASE(Attach) 00027 { 00028 TLOG(TLVL_INFO) << "BEGIN TEST Attach"; 00029 uint32_t key = GetRandomKey(0xF4A6); 00030 artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000); 00031 artdaq::SharedMemoryFragmentManager man2(key); 00032 00033 BOOST_REQUIRE_EQUAL(man.IsValid(), true); 00034 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0); 00035 BOOST_REQUIRE_EQUAL(man.size(), 10); 00036 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 2); 00037 00038 BOOST_REQUIRE_EQUAL(man2.IsValid(), true); 00039 BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1); 00040 BOOST_REQUIRE_EQUAL(man2.size(), 10); 00041 BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 2); 00042 TLOG(TLVL_INFO) << "END TEST Attach"; 00043 } 00044 00045 BOOST_AUTO_TEST_CASE(Reattach) 00046 { 00047 TLOG(TLVL_INFO) << "BEGIN TEST Reattach"; 00048 uint32_t key = GetRandomKey(0xF4A6); 00049 std::unique_ptr<artdaq::SharedMemoryFragmentManager> man(new artdaq::SharedMemoryFragmentManager(key, 10, 0x1000)); 00050 std::unique_ptr<artdaq::SharedMemoryFragmentManager> man2(new artdaq::SharedMemoryFragmentManager(key)); 00051 00052 BOOST_REQUIRE_EQUAL(man->IsValid(), true); 00053 BOOST_REQUIRE_EQUAL(man->GetMyId(), 0); 00054 BOOST_REQUIRE_EQUAL(man->size(), 10); 00055 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 2); 00056 00057 BOOST_REQUIRE_EQUAL(man2->IsValid(), true); 00058 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 1); 00059 BOOST_REQUIRE_EQUAL(man2->size(), 10); 00060 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 2); 00061 00062 man2.reset(nullptr); 00063 BOOST_REQUIRE_EQUAL(man->IsValid(), true); 00064 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 1); 00065 00066 man2.reset(new artdaq::SharedMemoryFragmentManager(key)); 00067 BOOST_REQUIRE_EQUAL(man->IsValid(), true); 00068 BOOST_REQUIRE_EQUAL(man->GetMyId(), 0); 00069 BOOST_REQUIRE_EQUAL(man->size(), 10); 00070 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 2); 00071 00072 BOOST_REQUIRE_EQUAL(man2->IsValid(), true); 00073 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 2); 00074 BOOST_REQUIRE_EQUAL(man2->size(), 10); 00075 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 2); 00076 00077 man.reset(nullptr); 00078 BOOST_REQUIRE_EQUAL(man2->IsValid(), true); 00079 BOOST_REQUIRE_EQUAL(man2->IsEndOfData(), true); 00080 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 2); 00081 BOOST_REQUIRE_EQUAL(man2->size(), 10); 00082 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 1); 00083 00084 man2->Attach(); 00085 BOOST_REQUIRE_EQUAL(man2->IsValid(), false); 00086 00087 man.reset(new artdaq::SharedMemoryFragmentManager(key, 10, 0x1000)); 00088 BOOST_REQUIRE_EQUAL(man->IsValid(), true); 00089 BOOST_REQUIRE_EQUAL(man->GetMyId(), 0); 00090 BOOST_REQUIRE_EQUAL(man->size(), 10); 00091 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 1); 00092 00093 man2->Attach(); 00094 BOOST_REQUIRE_EQUAL(man2->IsValid(), true); 00095 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 1); 00096 BOOST_REQUIRE_EQUAL(man2->size(), 10); 00097 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 2); 00098 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 2); 00099 00100 TLOG(TLVL_INFO) << "END TEST Reattach"; 00101 } 00102 00103 BOOST_AUTO_TEST_CASE(DataFlow) 00104 { 00105 TLOG(TLVL_INFO) << "BEGIN TEST DataFlow"; 00106 TLOG(TLVL_DEBUG) << "Initializing SharedMemoryFragmentManagers for DataFlow test"; 00107 uint32_t key = GetRandomKey(0xF4A6); 00108 artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000); 00109 artdaq::SharedMemoryFragmentManager man2(key); 00110 00111 auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1; 00112 00113 TLOG(TLVL_DEBUG) << "Creating test Fragment"; 00114 artdaq::Fragment frag(fragSizeWords); 00115 frag.setSequenceID(0x10); 00116 frag.setFragmentID(0x20); 00117 auto type = artdaq::Fragment::DataFragmentType; 00118 frag.setSystemType(type); 00119 frag.setTimestamp(0x30); 00120 for (size_t ii = 0; ii < fragSizeWords; ++ii) 00121 { 00122 *(frag.dataBegin() + ii) = ii; 00123 } 00124 00125 TLOG(TLVL_DEBUG) << "Writing Test Fragment to Shared Memory"; 00126 man.WriteFragment(std::move(frag), false, 0); 00127 00128 TLOG(TLVL_DEBUG) << "Reading Test Fragment Header"; 00129 artdaq::detail::RawFragmentHeader header; 00130 auto sts = man2.ReadFragmentHeader(header); 00131 00132 TLOG(TLVL_DEBUG) << "Checking Test Fragment Header Contents"; 00133 BOOST_REQUIRE_EQUAL(sts, 0); 00134 BOOST_REQUIRE_EQUAL(header.word_count, frag.size()); 00135 BOOST_REQUIRE_EQUAL(header.sequence_id, 0x10); 00136 BOOST_REQUIRE_EQUAL(header.fragment_id, 0x20); 00137 BOOST_REQUIRE_EQUAL(header.type, type); 00138 BOOST_REQUIRE_EQUAL(header.timestamp, 0x30); 00139 00140 TLOG(TLVL_DEBUG) << "Reading Test Fragment data"; 00141 artdaq::Fragment frag2(header.word_count); 00142 sts = man2.ReadFragmentData(frag2.dataBegin(), header.word_count - header.num_words()); 00143 00144 TLOG(TLVL_DEBUG) << "Checking Test Fragment contents"; 00145 BOOST_REQUIRE_EQUAL(sts, 0); 00146 for (size_t ii = 0; ii < fragSizeWords; ++ii) 00147 { 00148 BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(frag2.dataBegin() + ii)); 00149 } 00150 TLOG(TLVL_DEBUG) << "SharedMemoryFragmentManager DataFlow test complete"; 00151 TLOG(TLVL_INFO) << "END TEST DataFlow"; 00152 } 00153 00154 BOOST_AUTO_TEST_CASE(WholeFragment) 00155 { 00156 TLOG(TLVL_INFO) << "BEGIN TEST WholeFragment"; 00157 TLOG(TLVL_DEBUG) << "Initializing SharedMemoryFragmentManagers for WholeFragment Test"; 00158 uint32_t key = GetRandomKey(0xF4A6); 00159 artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000); 00160 artdaq::SharedMemoryFragmentManager man2(key); 00161 00162 auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1; 00163 00164 TLOG(TLVL_DEBUG) << "Creating test Fragment"; 00165 artdaq::Fragment frag(fragSizeWords); 00166 frag.setSequenceID(0x10); 00167 frag.setFragmentID(0x20); 00168 auto type = artdaq::Fragment::DataFragmentType; 00169 frag.setSystemType(type); 00170 frag.setTimestamp(0x30); 00171 for (size_t ii = 0; ii < fragSizeWords; ++ii) 00172 { 00173 *(frag.dataBegin() + ii) = ii; 00174 } 00175 00176 TLOG(TLVL_DEBUG) << "Writing Test Fragment to Shared Memory"; 00177 man.WriteFragment(std::move(frag), false, 0); 00178 00179 TLOG(TLVL_DEBUG) << "Reading Test Fragment Header"; 00180 artdaq::Fragment recvdFrag; 00181 auto sts = man2.ReadFragment(recvdFrag); 00182 00183 TLOG(TLVL_DEBUG) << "Checking Test Fragment Header Contents"; 00184 BOOST_REQUIRE_EQUAL(sts, 0); 00185 BOOST_REQUIRE_EQUAL(recvdFrag.size(), frag.size()); 00186 BOOST_REQUIRE_EQUAL(recvdFrag.sequenceID(), 0x10); 00187 BOOST_REQUIRE_EQUAL(recvdFrag.fragmentID(), 0x20); 00188 BOOST_REQUIRE_EQUAL(recvdFrag.type(), type); 00189 BOOST_REQUIRE_EQUAL(recvdFrag.timestamp(), 0x30); 00190 00191 TLOG(TLVL_DEBUG) << "Checking Test Fragment Data Contents"; 00192 for (size_t ii = 0; ii < fragSizeWords; ++ii) 00193 { 00194 //TLOG(TLVL_DEBUG) << *(frag.dataBegin() + ii) << " =?= " << *(recvdFrag.dataBegin() + ii) ; 00195 BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(recvdFrag.dataBegin() + ii)); 00196 } 00197 TLOG(TLVL_DEBUG) << "SharedMemoryFragmentManager WholeFragment test complete"; 00198 TLOG(TLVL_INFO) << "END TEST WholeFragment"; 00199 } 00200 00201 BOOST_AUTO_TEST_CASE(Timeout) 00202 { 00203 TLOG(TLVL_INFO) << "BEGIN TEST Timeout"; 00204 TLOG(TLVL_DEBUG) << "Initializing SharedMemoryFragmentManagers for Timeout Test"; 00205 uint32_t key = GetRandomKey(0xF4A6); 00206 artdaq::SharedMemoryFragmentManager man(key, 1, 0x1000); 00207 00208 auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1; 00209 00210 TLOG(TLVL_DEBUG) << "Creating test Fragment"; 00211 artdaq::Fragment frag(fragSizeWords); 00212 frag.setSequenceID(0x10); 00213 frag.setFragmentID(0x20); 00214 auto type = artdaq::Fragment::DataFragmentType; 00215 frag.setSystemType(type); 00216 frag.setTimestamp(0x30); 00217 for (size_t ii = 0; ii < fragSizeWords; ++ii) 00218 { 00219 *(frag.dataBegin() + ii) = ii; 00220 } 00221 00222 TLOG(TLVL_DEBUG) << "Reserving buffer to cause timeout to happen"; 00223 auto ret = man.GetBufferForWriting(true); 00224 BOOST_REQUIRE_EQUAL(ret, 0); 00225 00226 TLOG(TLVL_DEBUG) << "Attempting to write Fragment to Shared Memory. This should time out."; 00227 auto start_time = std::chrono::steady_clock::now(); 00228 ret = man.WriteFragment(std::move(frag), true, 100000); 00229 auto duration = artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time); 00230 00231 BOOST_REQUIRE_EQUAL(ret, -3); 00232 BOOST_REQUIRE_GE(duration, 100000); 00233 00234 TLOG(TLVL_DEBUG) << "SharedMemoryFragmentManager Timeout test complete"; 00235 TLOG(TLVL_INFO) << "END TEST Timeout"; 00236 } 00237 00238 BOOST_AUTO_TEST_SUITE_END()