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