forked from kmsmith137/ch_vdif_assembler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit_testing_thread.cpp
323 lines (241 loc) · 8.01 KB
/
unit_testing_thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// A stream/processor pair which unit tests the assembler by comparing with a slow reference implementation.
//
#include <cstring>
#include "ch_vdif_assembler_internals.hpp"
using namespace std;
namespace ch_vdif_assembler {
#if 0
}; // pacify emacs c-mode!
#endif
// -------------------------------------------------------------------------------------------------
//
// unit_test_buffer
unit_test_buffer::unit_test_buffer(int capacity_)
: capacity(capacity_)
{
xassert(capacity > 0);
pthread_mutex_init(&this->lock, NULL);
pthread_cond_init(&this->cond_produced, NULL);
this->ix0 = 0;
this->ix1 = 0;
this->buf.resize(capacity);
this->producer_exit_flag = false;
}
unit_test_buffer::~unit_test_buffer()
{
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond_produced);
}
int unit_test_buffer::get_size() const
{
pthread_mutex_lock(&lock);
int ret = ix1-ix0;
pthread_mutex_unlock(&lock);
return ret;
}
shared_ptr<assembled_chunk> unit_test_buffer::get_chunk()
{
pthread_mutex_lock(&lock);
for (;;) {
if (ix0 < ix1) {
shared_ptr<assembled_chunk> ret = buf[ix0 % capacity];
buf[ix0 % capacity] = shared_ptr<assembled_chunk> (); // drop reference to allocated memory
ix0++;
pthread_mutex_unlock(&lock);
xassert(ret);
return ret;
}
if (producer_exit_flag) {
pthread_mutex_unlock(&lock);
return shared_ptr<assembled_chunk> ();
}
pthread_cond_wait(&cond_produced, &lock);
}
}
void unit_test_buffer::put_chunk(const shared_ptr<assembled_chunk> &chunk)
{
xassert(chunk);
pthread_mutex_lock(&lock);
if (producer_exit_flag) {
pthread_mutex_unlock(&lock);
throw runtime_error("unit_test_buffer::put_chunk() called after producer_exit()");
}
if (ix1 >= ix0 + capacity) {
pthread_mutex_unlock(&lock);
throw runtime_error("unit_test_buffer capacity exceeded");
}
buf[ix1 % capacity] = chunk;
ix1++;
pthread_cond_broadcast(&cond_produced);
pthread_mutex_unlock(&lock);
}
void unit_test_buffer::producer_exit()
{
pthread_mutex_lock(&lock);
producer_exit_flag = true;
pthread_mutex_unlock(&lock);
}
// -------------------------------------------------------------------------------------------------
//
// unit_test_stream
struct unit_test_stream_thread : public thread_base
{
shared_ptr<assembler_nerve_center> nc;
shared_ptr<unit_test_buffer> ubuf;
int nchunks;
int asm_nt;
unit_test_stream_thread(const shared_ptr<assembler_nerve_center> &nc_, const shared_ptr<unit_test_buffer> &ubuf_, int nchunks_, int assembler_nt_)
: thread_base("unit_test stream thread"),
nc(nc_), ubuf(ubuf_), nchunks(nchunks_), asm_nt(assembler_nt_)
{
xassert(nc);
xassert(asm_nt >= 8192);
xassert(ubuf);
xassert(nchunks > 0);
}
virtual ~unit_test_stream_thread() { }
// stream_start() gets called prior to spawning thread, but thread is responsible for calling stream_end()
virtual void thread_body()
{
static const int packets_per_chunk = 50000;
// kill assembler if we throw an exception somewhere
assembler_killer killer(nc, "unit_test simulation thread threw exception");
// starting time (in fpga counts) of the stream
const uint64_t tstart = asm_nt;
// max size of random offset (in fpga counts)
const uint64_t tjitter = (int)(0.9 * asm_nt) - constants::timestamps_per_packet;
// fpga counts per packet
const double dt_dpacket = 625./256.;
// memory pools
shared_ptr<vdif_chunk_pool> vpool = make_shared<vdif_chunk_pool> (packets_per_chunk, true); // set_zero=true
shared_ptr<assembled_chunk_pool> apool = make_shared<assembled_chunk_pool> (asm_nt);
// reference assembler state
uint64_t asm_t0 = tstart;
shared_ptr<assembled_chunk> asm_chunk0 = make_shared<assembled_chunk> (apool, asm_t0);
shared_ptr<assembled_chunk> asm_chunk1 = make_shared<assembled_chunk> (apool, asm_t0 + asm_nt);
for (int ichunk = 0; ichunk < nchunks; ichunk++) {
shared_ptr<vdif_chunk> chunk = make_shared<vdif_chunk> (vpool, ichunk);
chunk->size = chunk->capacity;
uint8_t *packet0 = chunk->buf;
for (int ipacket = 0; ipacket < packets_per_chunk; ipacket++) {
uint32_t *header = reinterpret_cast<uint32_t *> (packet0 + ipacket*constants::packet_nbytes);
uint8_t *data = packet0 + ipacket*constants::packet_nbytes + constants::header_nbytes;
// randomly generate frequency and polarization
int freq_major = randint(0, 128);
int pol = randint(0, 2);
// randomly generate starting time
uint64_t tpacket = tstart;
tpacket += (uint64_t)((ichunk*packets_per_chunk+ipacket) * dt_dpacket);
tpacket += randint(0, tjitter);
// write vdif header
int link_id = freq_major / 16;
int slot_id = freq_major % 16;
header[3] = (link_id << 16) | (slot_id << 20) | pol;
header[5] = (uint32_t) tpacket;
// randomly generate data, and put it in both the vdif_chunk and the assembled_chunks
for (int i = 0; i < constants::timestamps_per_packet; i++) {
// advance assembler
if (tpacket + i >= asm_t0 + 2*asm_nt) {
ubuf->put_chunk(asm_chunk0);
asm_chunk0 = asm_chunk1;
asm_chunk1 = make_shared<assembled_chunk> (apool, asm_t0 + 2*asm_nt);
asm_t0 += asm_nt;
}
xassert(tpacket + i >= asm_t0);
xassert(tpacket + i < asm_t0 + 2*asm_nt);
// locate timestamp (tpacket + i) in assembler buffer
int s = (2*freq_major + pol) * asm_nt + (tpacket + i - asm_t0);
const uint8_t *pp = ((tpacket + i) >= (asm_t0 + asm_nt)) ? (asm_chunk1->buf + s - asm_nt) : (asm_chunk0->buf + s);
uint8_t *p = const_cast<uint8_t *> (pp);
for (int j = 0; j < 8; j++) {
uint8_t d = randint(0, 256);
data[8*i + j] = d;
p[256*asm_nt*j] = d;
}
}
}
stringstream ss;
ss << "unit test: chunk=" << ichunk << "/" << nchunks << "\n";
cout << ss.str() << flush;
nc->check_alive();
nc->stream_put_chunk(chunk, timer);
}
ubuf->put_chunk(asm_chunk0);
ubuf->put_chunk(asm_chunk1);
ubuf->producer_exit();
nc->stream_end();
killer.let_live();
}
};
struct unit_test_stream : public vdif_stream
{
shared_ptr<unit_test_buffer> ubuf;
int nchunks;
int assembler_nt;
unit_test_stream(const shared_ptr<unit_test_buffer> &ubuf_, int nchunks_, int assembler_nt_)
: vdif_stream(false), // is_realtime=false
ubuf(ubuf_), nchunks(nchunks_), assembler_nt(assembler_nt_)
{
xassert(ubuf);
xassert(nchunks > 0);
xassert(assembler_nt > 0);
}
virtual ~unit_test_stream() { }
virtual void spawn_threads(const shared_ptr<assembler_nerve_center> &nc)
{
xassert(nc);
nc->check_alive();
spawn_thread<unit_test_stream_thread> (nc, ubuf, nchunks, assembler_nt);
}
};
shared_ptr<vdif_stream> make_unit_test_stream(const shared_ptr<unit_test_buffer> &ubuf, int nchunks, int assembler_nt)
{
return make_shared<unit_test_stream> (ubuf, nchunks, assembler_nt);
}
// -------------------------------------------------------------------------------------------------
//
// unit_test_processor
struct unit_test_processor : public vdif_processor
{
shared_ptr<unit_test_buffer> ubuf;
unit_test_processor(const shared_ptr<unit_test_buffer> &ubuf_)
: vdif_processor("unit_test processor", true), // is_critical=true
ubuf(ubuf_)
{
xassert(ubuf);
}
virtual ~unit_test_processor() { }
virtual void process_chunk(const shared_ptr<assembled_chunk> &a)
{
if (a->is_zero())
return;
shared_ptr<assembled_chunk> b;
for (;;) {
b = ubuf->get_chunk();
xassert(b);
xassert(b->nt == a->nt);
if (b->t0 == a->t0) {
xassert(b->is_equal(*a));
return;
}
xassert(b->t0 < a->t0);
xassert(b->is_zero());
}
}
virtual void finalize()
{
for (;;) {
shared_ptr<assembled_chunk> b = ubuf->get_chunk();
if (!b)
return;
xassert(b->is_zero());
}
cout << "unit test: pass\n" << flush;
}
};
shared_ptr<vdif_processor> make_unit_test_processor(const shared_ptr<unit_test_buffer> &ubuf)
{
return make_shared<unit_test_processor> (ubuf);
}
} // namespace ch_vdif_assembler