24 #include "cheetah/psrdada/test_utils/TestDadaDB.h" 25 #include "panda/Error.h" 26 #include "panda/Log.h" 39 namespace test_utils {
52 key_t random_dada_key()
54 int max_attempts = 20, attempts = 0;
55 srand (time(NULL) + ::getpid());
56 while (attempts < max_attempts)
58 key_t key = 0xdada0000 + 2*((rand() % (1<<16))/2);
59 if ((shmget(key, 0, 0)==-1) && (shmget(key+1, 0, 0)==-1))
65 throw std::runtime_error(
"Unable to find free dada key");
76 , _data_block(IPCBUF_INIT)
77 , _header(IPCBUF_INIT)
78 , _data_blocks_created(false)
79 , _header_blocks_created(false)
83 TestDadaDB::~TestDadaDB()
90 std::lock_guard<std::mutex> lock(_lock);
91 if (_data_blocks_created)
92 throw std::runtime_error(
"DADA data blocks already created");
94 _dada_key = detail::random_dada_key();
95 PANDA_LOG <<
"Using DADA key: " << std::hex << _dada_key << std::dec <<
"\n";
98 if (ipcbuf_create (&_data_block, _dada_key, _nbufs, _bufsz, 1) < 0) {
100 throw std::runtime_error(
"Could not create DADA data block");
102 _data_blocks_created =
true;
103 PANDA_LOG_DEBUG <<
"Create DADA data block with nbufs=" << _nbufs <<
" bufsz=" << _bufsz <<
"\n";
105 if (_header_blocks_created)
106 throw std::runtime_error(
"DADA header blocks already created");
107 if (ipcbuf_create (&_header, _dada_key + 1, _nhdrs, _hdrsz, 1) < 0) {
109 throw std::runtime_error(
"Could not create DADA header block");
111 _header_blocks_created =
true;
112 PANDA_LOG_DEBUG <<
"Create DADA header block with nhdrs=" << _nhdrs <<
" hdrsz=" << _hdrsz <<
"\n";
117 std::lock_guard<std::mutex> lock(_lock);
118 PANDA_LOG_DEBUG <<
"Destroying the buffers";
122 void TestDadaDB::do_destroy()
124 if (_data_blocks_created)
126 ipcbuf_connect(&_data_block, _dada_key);
127 ipcbuf_destroy(&_data_block);
128 _data_blocks_created =
false;
130 if (_header_blocks_created)
132 ipcbuf_connect(&_header, _dada_key + 1);
133 ipcbuf_destroy(&_header);
134 _header_blocks_created =
false;
uint64_t num_data_buffers() const
Return the number of data buffers.
void destroy()
Destroy the allocated shared memory blocks.
uint64_t num_header_buffers() const
Return the number of header buffers.
uint64_t header_buffer_size() const
Return the size of each header buffer.
Some limits and constants for FLDO.
TestDadaDB(uint64_t nbufs=DADA_DEFAULT_BLOCK_NUM, uint64_t bufsz=DADA_DEFAULT_BLOCK_SIZE, uint64_t nhdrs=IPCBUF_XFERS, uint64_t hdrsz=DADA_DEFAULT_HEADER_SIZE)
Constructa new TestDadaDB instance.
void create()
Create the data and header blocks in shared memory.
uint64_t data_buffer_size() const
Return the size of each data buffer.
key_t key() const
Return the hexidecimal shared memory key.