00001 #include "artdaq-core/Core/SharedMemoryManager.hh"
00002 #include "artdaq-core/Utilities/TimeUtils.hh"
00003 #include "artdaq-core/Utilities/configureMessageFacility.hh"
00004
00005 #define BOOST_TEST_MODULE SharedMemoryManager_t
00006 #include "cetlib/quiet_unit_test.hpp"
00007 #include "cetlib_except/exception.h"
00008
00009 #include "tracemf.h"
00010
00011 BOOST_AUTO_TEST_SUITE(SharedMemoryManager_test)
00012
00013 BOOST_AUTO_TEST_CASE(Construct)
00014 {
00015 artdaq::configureMessageFacility("SharedMemoryManager_t", true, true);
00016 TLOG_DEBUG("SharedMemoryManager_t") << "BEGIN TEST Construct" << TLOG_ENDL;
00017 srand(artdaq::TimeUtils::gettimeofday_us());
00018 uint32_t key = 0x7357 + rand() % 0x10000000;
00019 artdaq::SharedMemoryManager man(key, 10, 0x1000,0x10000);
00020 BOOST_REQUIRE_EQUAL(man.IsValid(), true);
00021 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
00022 BOOST_REQUIRE_EQUAL(man.size(), 10);
00023 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 0);
00024 BOOST_REQUIRE_EQUAL(man.GetKey(), key);
00025 TLOG_DEBUG("SharedMemoryManager_t") << "END TEST Construct" << TLOG_ENDL;
00026 }
00027
00028 BOOST_AUTO_TEST_CASE(Attach)
00029 {
00030 TLOG_DEBUG("SharedMemoryManager_t") << "BEGIN TEST Attach" << TLOG_ENDL;
00031 srand(artdaq::TimeUtils::gettimeofday_us());
00032 uint32_t key = 0x7357 + rand() % 0x10000000;
00033 artdaq::SharedMemoryManager man(key, 10, 0x1000, 0x10000);
00034 artdaq::SharedMemoryManager man2(key, 10, 0x1000, 0x10000);
00035
00036 BOOST_REQUIRE_EQUAL(man.IsValid(), true);
00037 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
00038 BOOST_REQUIRE_EQUAL(man.size(), 10);
00039 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 1);
00040 BOOST_REQUIRE_EQUAL(man.GetKey(), key);
00041
00042 BOOST_REQUIRE_EQUAL(man2.IsValid(), true);
00043 BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1);
00044 BOOST_REQUIRE_EQUAL(man2.size(), 10);
00045 BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 1);
00046 BOOST_REQUIRE_EQUAL(man2.GetKey(), key);
00047
00048 TLOG_DEBUG("SharedMemoryManager_t") << "END TEST Attach" << TLOG_ENDL;
00049 }
00050
00051 BOOST_AUTO_TEST_CASE(DataFlow)
00052 {
00053 TLOG_DEBUG("SharedMemoryManager_t") << "BEGIN TEST DataFlow" << TLOG_ENDL;
00054 srand(artdaq::TimeUtils::gettimeofday_us());
00055 uint32_t key = 0x7357 + rand() % 0x10000000;
00056 artdaq::SharedMemoryManager man(key, 10, 0x1000);
00057 artdaq::SharedMemoryManager man2(key, 10, 0x1000);
00058
00059 BOOST_REQUIRE_EQUAL(man.ReadyForWrite(false), true);
00060 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(false), 10);
00061 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00062 BOOST_REQUIRE_EQUAL(man.ReadReadyCount(), 0);
00063
00064 int buf = man.GetBufferForWriting(false);
00065 BOOST_REQUIRE_EQUAL(man.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Writing), true);
00066 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
00067 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
00068 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(),0);
00069 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
00070
00071 uint8_t n = 0;
00072 uint8_t data[0x1000];
00073 std::generate_n(data, 0x1000, [&]() {return ++n; });
00074 man.Write(buf, data, 0x1000);
00075 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
00076 BOOST_REQUIRE_EQUAL(man2.BufferDataSize(buf), 0x1000);
00077 man.MarkBufferFull(buf, 1);
00078 BOOST_REQUIRE_EQUAL(man2.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Full), true);
00079 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
00080 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 1);
00081
00082 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00083 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(), true);
00084 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
00085
00086 auto readbuf = man2.GetBufferForReading();
00087 BOOST_REQUIRE_EQUAL(man2.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Reading), true);
00088 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00089 uint8_t byte;
00090 auto sts = man2.Read(readbuf, &byte, 1);
00091 BOOST_REQUIRE_EQUAL(sts, true);
00092 BOOST_REQUIRE_EQUAL(byte, 1);
00093 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00094 sts = man2.Read(readbuf, &byte, 1);
00095 BOOST_REQUIRE_EQUAL(sts, true);
00096 BOOST_REQUIRE_EQUAL(byte, 2);
00097 man2.IncrementReadPos(readbuf, 0x10);
00098 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00099 sts = man2.Read(readbuf, &byte, 1);
00100 BOOST_REQUIRE_EQUAL(sts, true);
00101 BOOST_REQUIRE_EQUAL(byte, 0x13);
00102 man2.ResetReadPos(readbuf);
00103 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00104 sts = man2.Read(readbuf, &byte, 1);
00105 BOOST_REQUIRE_EQUAL(sts, true);
00106 BOOST_REQUIRE_EQUAL(byte, 1);
00107 man2.IncrementReadPos(readbuf, 0xFFF);
00108 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), false);
00109 man2.MarkBufferEmpty(readbuf);
00110
00111 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 0);
00112 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
00113 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(false), 10);
00114 TLOG_DEBUG("SharedMemoryManager_t") << "END TEST DataFlow" << TLOG_ENDL;
00115 }
00116
00117 BOOST_AUTO_TEST_CASE(Exceptions)
00118 {
00119 artdaq::configureMessageFacility("SharedMemoryManager_t", true, true);
00120 TLOG_DEBUG("SharedMemoryManager_t") << "BEGIN TEST Exceptions" << TLOG_ENDL;
00121 srand(artdaq::TimeUtils::gettimeofday_us());
00122 uint32_t key = 0x7357 + rand() % 0x10000000;
00123 artdaq::SharedMemoryManager man(key, 10, 0x1000);
00124 artdaq::SharedMemoryManager man2(key, 10, 0x1000);
00125 BOOST_REQUIRE_EQUAL(man.ReadyForWrite(false), true);
00126 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(false), 10);
00127 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00128 BOOST_REQUIRE_EQUAL(man.ReadReadyCount(), 0);
00129
00130
00131 BOOST_REQUIRE_EXCEPTION(man.ResetReadPos(11), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
00132 BOOST_REQUIRE_EQUAL(man.IsValid(), false);
00133
00134 man.Attach();
00135
00136 BOOST_REQUIRE_EXCEPTION(man.MarkBufferEmpty(0),cet::exception, [&](cet::exception e) { return e.category() == "StateAccessViolation"; });
00137 BOOST_REQUIRE_EQUAL(man.IsValid(), false);
00138
00139 man.Attach();
00140
00141
00142 int buf = man.GetBufferForWriting(false);
00143 BOOST_REQUIRE_EQUAL(man.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Writing), true);
00144 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
00145 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
00146 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
00147
00148 uint8_t n = 0;
00149 uint8_t data[0x2000];
00150 std::generate_n(data, 0x2000, [&]() {return ++n; });
00151 BOOST_REQUIRE_EXCEPTION(man.Write(buf, data, 0x2000), cet::exception, [&](cet::exception e) { return e.category() == "SharedMemoryWrite"; });
00152 BOOST_REQUIRE_EQUAL(man.IsValid(), false);
00153
00154 man.Attach();
00155 man2.Attach();
00156 buf = man.GetBufferForWriting(false);
00157 BOOST_REQUIRE_EQUAL(man.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Writing), true);
00158 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
00159 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
00160 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
00161 man.Write(buf, data, 0x1000);
00162 man.MarkBufferFull(buf);
00163
00164
00165 BOOST_REQUIRE_EQUAL(man2.IsValid(), true);
00166 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(), true);
00167 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
00168
00169 int readbuf = man2.GetBufferForReading();
00170 BOOST_REQUIRE_EQUAL(readbuf, buf);
00171 BOOST_REQUIRE_EXCEPTION(man2.Read(readbuf, data, 0x1001), cet::exception, [&](cet::exception e) { return e.category() == "SharedMemoryRead"; });
00172 BOOST_REQUIRE_EQUAL(man2.IsValid(), false);
00173
00174 man.Attach();
00175 man2.Attach();
00176
00177 man.MarkBufferEmpty(readbuf, true);
00178 buf = man.GetBufferForWriting(false);
00179 BOOST_REQUIRE_EQUAL(man.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Writing), true);
00180 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
00181 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
00182 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
00183 man.Write(buf, data, 0x1000);
00184 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
00185 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
00186 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
00187 man.MarkBufferFull(buf);
00188 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
00189 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
00190
00191 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(), true);
00192 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
00193 readbuf = man2.GetBufferForReading();
00194 BOOST_REQUIRE_EQUAL(readbuf, buf);
00195 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 1);
00196 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager()[0], readbuf);
00197 BOOST_REQUIRE_EQUAL(man2.BufferDataSize(readbuf), 0x1000);
00198
00199 BOOST_REQUIRE_EXCEPTION(man.MarkBufferEmpty(readbuf), cet::exception, [&](cet::exception e) { return e.category() == "OwnerAccessViolation"; });
00200 BOOST_REQUIRE_EQUAL(man.IsValid(), false);
00201
00202 TLOG_DEBUG("SharedMemoryManager_t") << "END TEST Exceptions" << TLOG_ENDL;
00203 }
00204
00205 BOOST_AUTO_TEST_CASE(Broadcast)
00206 {
00207 TLOG_DEBUG("SharedMemoryManager_t") << "BEGIN TEST Broadcast" << TLOG_ENDL;
00208 srand(artdaq::TimeUtils::gettimeofday_us());
00209 uint32_t key = 0x7357 + rand() % 0x10000000;
00210 artdaq::SharedMemoryManager man(key, 10, 0x1000, 0x10000, false);
00211 artdaq::SharedMemoryManager man2(key, 10, 0x1000, 0x10000, false);
00212 artdaq::SharedMemoryManager man3(key, 10, 0x1000, 0x10000, false);
00213
00214 BOOST_REQUIRE_EQUAL(man.ReadyForWrite(false), true);
00215 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(false), 10);
00216 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00217 BOOST_REQUIRE_EQUAL(man.ReadReadyCount(), 0);
00218
00219 int buf = man.GetBufferForWriting(false);
00220 BOOST_REQUIRE_EQUAL(man.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Writing), true);
00221 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
00222 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
00223 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 0);
00224 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
00225
00226 uint8_t n = 0;
00227 uint8_t data[0x1000];
00228 std::generate_n(data, 0x1000, [&]() {return ++n; });
00229 man.Write(buf, data, 0x1000);
00230 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
00231 BOOST_REQUIRE_EQUAL(man2.BufferDataSize(buf), 0x1000);
00232 man.MarkBufferFull(buf, -1);
00233 BOOST_REQUIRE_EQUAL(man2.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Full), true);
00234 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
00235
00236 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00237 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(), true);
00238 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
00239
00240 auto readbuf = man2.GetBufferForReading();
00241 BOOST_REQUIRE_EQUAL(readbuf, buf);
00242 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 1);
00243 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00244 BOOST_REQUIRE_EQUAL(man3.ReadyForRead(), false);
00245 BOOST_REQUIRE_EQUAL(man2.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Reading), true);
00246 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00247 uint8_t byte;
00248 auto sts = man2.Read(readbuf, &byte, 1);
00249 BOOST_REQUIRE_EQUAL(sts, true);
00250 BOOST_REQUIRE_EQUAL(byte, 1);
00251 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00252 sts = man2.Read(readbuf, &byte, 1);
00253 BOOST_REQUIRE_EQUAL(sts, true);
00254 BOOST_REQUIRE_EQUAL(byte, 2);
00255 man2.IncrementReadPos(readbuf, 0x10);
00256 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00257 sts = man2.Read(readbuf, &byte, 1);
00258 BOOST_REQUIRE_EQUAL(sts, true);
00259 BOOST_REQUIRE_EQUAL(byte, 0x13);
00260 man2.ResetReadPos(readbuf);
00261 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), true);
00262 sts = man2.Read(readbuf, &byte, 1);
00263 BOOST_REQUIRE_EQUAL(sts, true);
00264 BOOST_REQUIRE_EQUAL(byte, 1);
00265 man2.IncrementReadPos(readbuf, 0xFFF);
00266 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf), false);
00267 man2.MarkBufferEmpty(readbuf);
00268 BOOST_REQUIRE_EQUAL(man3.ReadyForRead(), true);
00269 BOOST_REQUIRE_EQUAL(man3.ReadReadyCount(), 1);
00270 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(), false);
00271 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 0);
00272 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00273
00274
00275 readbuf = man3.GetBufferForReading();
00276 BOOST_REQUIRE_EQUAL(readbuf, buf);
00277 BOOST_REQUIRE_EQUAL(man3.GetBuffersOwnedByManager().size(), 1);
00278 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00279 BOOST_REQUIRE_EQUAL(man3.CheckBuffer(buf, artdaq::SharedMemoryManager::BufferSemaphoreFlags::Reading), true);
00280 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf), true);
00281 sts = man3.Read(readbuf, &byte, 1);
00282 BOOST_REQUIRE_EQUAL(sts, true);
00283 BOOST_REQUIRE_EQUAL(byte, 1);
00284 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf), true);
00285 sts = man3.Read(readbuf, &byte, 1);
00286 BOOST_REQUIRE_EQUAL(sts, true);
00287 BOOST_REQUIRE_EQUAL(byte, 2);
00288 man3.IncrementReadPos(readbuf, 0x10);
00289 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf), true);
00290 sts = man3.Read(readbuf, &byte, 1);
00291 BOOST_REQUIRE_EQUAL(sts, true);
00292 BOOST_REQUIRE_EQUAL(byte, 0x13);
00293 man3.ResetReadPos(readbuf);
00294 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf), true);
00295 sts = man3.Read(readbuf, &byte, 1);
00296 BOOST_REQUIRE_EQUAL(sts, true);
00297 BOOST_REQUIRE_EQUAL(byte, 1);
00298 man3.IncrementReadPos(readbuf, 0xFFF);
00299 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf), false);
00300 man3.MarkBufferEmpty(readbuf);
00301 BOOST_REQUIRE_EQUAL(man3.ReadyForRead(), false);
00302 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(), false);
00303 BOOST_REQUIRE_EQUAL(man.ReadyForRead(), false);
00304
00305 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 0);
00306 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
00307 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(false), 9);
00308 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(true), 10);
00309 sleep(1);
00310 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(false), 10);
00311 TLOG_DEBUG("SharedMemoryManager_t") << "END TEST Broadcast" << TLOG_ENDL;
00312 }
00313
00314 BOOST_AUTO_TEST_SUITE_END()