Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
TestDadaDB.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/psrdada/test_utils/TestDadaDB.h"
25 #include "panda/Error.h"
26 #include "panda/Log.h"
27 #include <sys/shm.h>
28 #include <time.h>
29 #include <cstdint>
30 #include <stdexcept>
31 #include <iostream>
32 #include <thread>
33 #include <stdlib.h>
34 #include <unistd.h>
35 
36 namespace ska {
37 namespace cheetah {
38 namespace psrdada {
39 namespace test_utils {
40 namespace detail {
52  key_t random_dada_key()
53  {
54  int max_attempts = 20, attempts = 0;
55  srand (time(NULL) + ::getpid());
56  while (attempts < max_attempts)
57  {
58  key_t key = 0xdada0000 + 2*((rand() % (1<<16))/2);
59  if ((shmget(key, 0, 0)==-1) && (shmget(key+1, 0, 0)==-1))
60  {
61  return key;
62  }
63  ++attempts;
64  }
65  throw std::runtime_error("Unable to find free dada key");
66  }
67 
68 } // namespace detail
69 
70 TestDadaDB::TestDadaDB(uint64_t nbufs, uint64_t bufsz, uint64_t nhdrs, uint64_t hdrsz)
71  : _nbufs(nbufs)
72  , _bufsz(bufsz)
73  , _nhdrs(nhdrs)
74  , _hdrsz(hdrsz)
75  , _dada_key(0)
76  , _data_block(IPCBUF_INIT)
77  , _header(IPCBUF_INIT)
78  , _data_blocks_created(false)
79  , _header_blocks_created(false)
80 {
81 }
82 
83 TestDadaDB::~TestDadaDB()
84 {
85  destroy();
86 }
87 
89 {
90  std::lock_guard<std::mutex> lock(_lock);
91  if (_data_blocks_created)
92  throw std::runtime_error("DADA data blocks already created");
93 
94  _dada_key = detail::random_dada_key();
95  PANDA_LOG << "Using DADA key: " << std::hex << _dada_key << std::dec << "\n";
96 
97  //Here we always assume that the number of readers will be 1.
98  if (ipcbuf_create (&_data_block, _dada_key, _nbufs, _bufsz, 1) < 0) {
99  do_destroy();
100  throw std::runtime_error("Could not create DADA data block");
101  }
102  _data_blocks_created = true;
103  PANDA_LOG_DEBUG << "Create DADA data block with nbufs=" << _nbufs << " bufsz=" << _bufsz << "\n";
104 
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) {
108  do_destroy();
109  throw std::runtime_error("Could not create DADA header block");
110  }
111  _header_blocks_created = true;
112  PANDA_LOG_DEBUG << "Create DADA header block with nhdrs=" << _nhdrs << " hdrsz=" << _hdrsz << "\n";
113 }
114 
116 {
117  std::lock_guard<std::mutex> lock(_lock);
118  PANDA_LOG_DEBUG << "Destroying the buffers";
119  do_destroy();
120 }
121 
122 void TestDadaDB::do_destroy()
123 {
124  if (_data_blocks_created)
125  {
126  ipcbuf_connect(&_data_block, _dada_key);
127  ipcbuf_destroy(&_data_block);
128  _data_blocks_created = false;
129  }
130  if (_header_blocks_created)
131  {
132  ipcbuf_connect(&_header, _dada_key + 1);
133  ipcbuf_destroy(&_header);
134  _header_blocks_created = false;
135  }
136 }
137 
139 {
140  return _nbufs;
141 }
142 
144 {
145  return _bufsz;
146 }
147 
149 {
150  return _nhdrs;
151 }
152 
154 {
155  return _hdrsz;
156 }
157 
158 key_t TestDadaDB::key() const
159 {
160  return _dada_key;
161 }
162 
163 } // namespace test_utils
164 } // namespace psrdada
165 } // namespace cheetah
166 } // namespace ska
uint64_t num_data_buffers() const
Return the number of data buffers.
Definition: TestDadaDB.cpp:138
void destroy()
Destroy the allocated shared memory blocks.
Definition: TestDadaDB.cpp:115
uint64_t num_header_buffers() const
Return the number of header buffers.
Definition: TestDadaDB.cpp:148
uint64_t header_buffer_size() const
Return the size of each header buffer.
Definition: TestDadaDB.cpp:153
Some limits and constants for FLDO.
Definition: Brdz.h:35
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.
Definition: TestDadaDB.cpp:70
void create()
Create the data and header blocks in shared memory.
Definition: TestDadaDB.cpp:88
uint64_t data_buffer_size() const
Return the size of each data buffer.
Definition: TestDadaDB.cpp:143
key_t key() const
Return the hexidecimal shared memory key.
Definition: TestDadaDB.cpp:158