-
Notifications
You must be signed in to change notification settings - Fork 3
/
lofar_cli_extractor.c
525 lines (420 loc) · 18.2 KB
/
lofar_cli_extractor.c
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "lofar_cli_meta.h"
// Constant to define the length of the timing variable array
#define TIMEARRLEN 4
void helpMessages() {
printf("LOFAR UDP Data extractor (CLI v%s, lib v%s)\n\n", UPM_CLI_VERSION, UPM_VERSION);
printf("Usage: lofar_cli_extractor <flags>");
printf("\n\n");
sharedFlags();
//printf();
printf("-p: <mode> Processing mode, options listed below (default: 0)\n");
processingModes();
}
static void CLICleanup(lofar_udp_config *config, lofar_udp_io_write_config *outConfig, int8_t *headerBuffer) {
FREE_NOT_NULL(config);
FREE_NOT_NULL(outConfig);
FREE_NOT_NULL(headerBuffer);
}
int main(int argc, char *argv[]) {
// Set up input local variables
int32_t inputOpt, input = 0;
float seconds = 0.0f;
char inputTime[256] = "", stringBuff[128] = "", inputFormat[DEF_STR_LEN] = "";
int8_t silent = 0, inputProvided = 0, outputProvided = 0;
int64_t maxPackets = LONG_MAX, startingPacket = -1, splitEvery = LONG_MAX;
int8_t clock200MHz = 1;
lofar_udp_config *config = lofar_udp_config_alloc();
lofar_udp_io_write_config *outConfig = lofar_udp_io_write_alloc();
int8_t *headerBuffer = NULL;
if (config == NULL || outConfig == NULL) {
fprintf(stderr, "ERROR: Failed to allocate memory for configuration structs (something has gone very wrong...), exiting.\n");
FREE_NOT_NULL(config); FREE_NOT_NULL(outConfig);
return 1;
}
// Set up reader loop variables
int64_t loops = 0, localLoops = 0, returnValMeta = 0;
int64_t returnVal;
int64_t packetsProcessed = 0, packetsWritten = 0, packetsToWrite;
// Timing variables
double timing[TIMEARRLEN] = { 0. }, totalReadTime = 0., totalOpsTime = 0., totalWriteTime = 0., totalMetadataTime = 0.;
struct timespec tick, tick0, tick1, tock, tock0, tock1;
// strtol / option checks
char *endPtr;
int8_t flagged = 0;
// Standard ugly input flags parser
while ((inputOpt = getopt(argc, argv, "hzrqfvVi:o:m:M:I:u:t:s:S:e:p:a:n:b:ck:T:")) != -1) {
input = 1;
switch (inputOpt) {
case 'i':
strncpy(inputFormat, optarg, DEF_STR_LEN - 1);
inputProvided = 1;
break;
case 'o':
if (lofar_udp_io_write_parse_optarg(outConfig, optarg) < 0) {
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
// If the metadata is not yet set, see if we can parse a requested type from the output filename
if (config->metadata_config.metadataType == NO_META) config->metadata_config.metadataType = lofar_udp_metadata_parse_type_output(optarg);
outputProvided = 1;
break;
case 'm':
config->packetsPerIteration = strtol(optarg, &endPtr, 10);
if (checkOpt(inputOpt, optarg, endPtr)) { flagged = 1; }
break;
case 'M':
config->metadata_config.metadataType = lofar_udp_metadata_string_to_meta(optarg);
break;
case 'I':
if (strncpy(config->metadata_config.metadataLocation, optarg, DEF_STR_LEN) != config->metadata_config.metadataLocation) {
fprintf(stderr, "ERROR: Failed to copy metadata file location to config, exiting.\n");
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
break;
case 'u':
config->numPorts = internal_strtoc(optarg, &endPtr);
if (checkOpt(inputOpt, optarg, endPtr)) { flagged = 1; }
break;
case 't':
strncpy(inputTime, optarg, 255);
break;
case 's':
seconds = strtof(optarg, &endPtr);
if (checkOpt(inputOpt, optarg, endPtr)) { flagged = 1; }
break;
case 'S':
splitEvery = internal_strtoi(optarg, &endPtr);
if (checkOpt(inputOpt, optarg, endPtr)) { flagged = 1; }
break;
case 'p':
config->processingMode = internal_strtoi(optarg, &endPtr);
if (checkOpt(inputOpt, optarg, endPtr)) { flagged = 1; }
break;
case 'b':
sscanf(optarg, "%hd,%hd", &(config->beamletLimits[0]), &(config->beamletLimits[1]));
break;
case 'r':
config->replayDroppedPackets = 1;
break;
case 'c':
config->calibrateData = APPLY_CALIBRATION;
break;
case 'z':
clock200MHz = 0;
break;
case 'q':
silent = 1;
break;
case 'f':
outConfig->progressWithExisting = 1;
break;
case 'v':
VERBOSE(config->verbose = 1;);
break;
case 'V':
VERBOSE(config->verbose = 2;);
break;
case 'T':
config->ompThreads = internal_strtoi(optarg, &endPtr);
if (checkOpt(inputOpt, optarg, endPtr)) { flagged = 1; }
break;
// Silence GCC warnings, fall-through is the desired behaviour
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#pragma GCC diagnostic push
// Handle edge/error cases
case '?':
if ((optopt == 'i') || (optopt == 'o') || (optopt == 'm') || (optopt == 'u') || (optopt == 't') ||
(optopt == 's') || (optopt == 'e') || (optopt == 'p') || (optopt == 'a') || (optopt == 'c') ||
(optopt == 'd')) {
fprintf(stderr, "Option '%c' requires an argument.\n", optopt);
} else {
fprintf(stderr, "Option '%c' is unknown or encountered an error.\n", optopt);
}
case 'h':
default:
#pragma GCC diagnostic pop
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
}
if (flagged) {
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (!input) {
fprintf(stderr, "ERROR: No inputs provided, exiting.\n");
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (!inputProvided) {
fprintf(stderr, "ERROR: An input was not provided, exiting.\n");
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (lofar_udp_io_read_parse_optarg(config, inputFormat) < 0) {
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (!outputProvided) {
fprintf(stderr, "ERROR: An output was not provided, exiting.\n");
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (config->calibrateData != NO_CALIBRATION && !strnlen(config->metadata_config.metadataLocation, DEF_STR_LEN)) {
fprintf(stderr, "ERROR: Data calibration was enabled, but metadata was not provided. Exiting.\n");
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
// Sanity check a few inputs
if ((config->numPorts <= 0 || config->numPorts > MAX_NUM_PORTS) || // We are processing a sane number of ports
(config->packetsPerIteration < 2) || // We are processing a sane number of packets
(config->replayDroppedPackets > 1 || config->replayDroppedPackets < 0) || // Replay key was not malformed
(config->processingMode > 1000 || config->processingMode < 0) ||
// Processing mode is sane (may still fail later)
(seconds < 0) || // Time is sane
(config->ompThreads < 1)) // Number of threads will allow execution to continue
{
fprintf(stderr, "One or more inputs invalid or not fully initialised, exiting.\n");
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (silent == 0) {
printf("LOFAR UDP Data extractor (v%s, lib v%s)\n\n", UPM_CLI_VERSION, UPM_VERSION);
printf("=========== Given configuration ===========\n");
printf("Input:\t%s\n", inputFormat);
for (int i = 0; i < config->numPorts; i++) printf("\t\t%s\n", config->inputLocations[i]);
printf("Output File: %s\n\n", outConfig->outputFormat);
printf("Packets/Gulp:\t%ld\t\t\tPorts:\t%d\n\n", config->packetsPerIteration, config->numPorts);
VERBOSE(printf("Verbose:\t%d\n", config->verbose););
printf("Proc Mode:\t%03d\t\t\tReader:\t%d\n\n", config->processingMode, config->readerType);
printf("Beamlet limits:\t%d, %d\n\n", config->beamletLimits[0], config->beamletLimits[1]);
}
headerBuffer = calloc(DEF_HDR_LEN, sizeof(char));
if (strnlen(inputTime, 256)) {
startingPacket = lofar_udp_time_get_packet_from_isot(inputTime, clock200MHz);
if (startingPacket == 1) {
helpMessages();
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
}
if (seconds != 0.0) {
maxPackets = lofar_udp_time_get_packets_from_seconds(seconds, clock200MHz);
}
if (silent == 0) { printf("Start Time:\t%s\t200MHz Clock:\t%d\n", inputTime, clock200MHz); }
if (silent == 0) {
printf("Initial Packet:\t%ld\t\tFinal Packet:\t%ld\n", startingPacket, startingPacket + maxPackets);
}
if (silent == 0) { printf("============ End configuration ============\n\n"); }
// If the largest requested data block is less than the packetsPerIteration input, lower the figure so we aren't doing unnecessary reads/writes
if (maxPackets > 0 && config->packetsPerIteration > maxPackets) {
if (silent == 0) {
printf("Packet/Gulp is greater than the maximum packets requested, reducing from %ld to %ld.\n",
config->packetsPerIteration, maxPackets);
}
config->packetsPerIteration = maxPackets;
}
if (silent == 0) { printf("Starting data read/reform operations...\n"); }
// Start our timers
CLICK(tick);
CLICK(tick0);
// Generate the lofar_udp_reader, this also performs I/O to seeks to the required packet and gulps the first input
config->startingPacket = startingPacket;
config->packetsReadMax = maxPackets;
lofar_udp_reader *reader = lofar_udp_reader_setup(config);
// Returns null on error, check
if (reader == NULL) {
fprintf(stderr, "Failed to generate reader. Exiting.\n");
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
// Sanity check that we were passed the correct clock bit
if (((lofar_source_bytes *) &(reader->meta->inputData[0][1]))->clockBit != (uint32_t) clock200MHz) {
fprintf(stderr,
"ERROR: The clock bit of the first packet does not match the clock state given when starting the CLI. Add or remove -c from your command. Exiting.\n");
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
if (silent == 0) {
lofar_udp_time_get_current_isot(reader, stringBuff, sizeof(stringBuff) / sizeof(stringBuff[0]));
printf("\n\n=========== Reader Information ===========\n");
printf("Total Beamlets:\t%d/%d\t\t\t\t\tFirst Packet:\t%ld\n", reader->meta->totalProcBeamlets,
reader->meta->totalRawBeamlets, reader->meta->lastPacket);
printf("Start time:\t%s\t\tMJD Time:\t%lf\n", stringBuff,
lofar_udp_time_get_packet_time_mjd(reader->meta->inputData[0]));
for (int port = 0; port < reader->meta->numPorts; port++) {
printf("------------------ Port %d -----------------\n", port);
printf("Port Beamlets:\t%d/%d\t\tPort Bitmode:\t%d\t\tInput Pkt Len:\t%d\n",
reader->meta->upperBeamlets[port] - reader->meta->baseBeamlets[port],
reader->meta->portRawBeamlets[port], reader->meta->inputBitMode,
reader->meta->portPacketLength[port]);
}
for (int out = 0; out < reader->meta->numOutputs; out++)
printf("Output Pkt Len (%d):\t%d\t\t", out, reader->meta->packetOutputLength[out]);
printf("\n");
printf("============= End Information =============\n\n");
}
// Get the starting packet for output file names, fix the packets per iteration if we dropped packets on the last iter
startingPacket = reader->meta->leadingPacket;
if ((returnVal = _lofar_udp_io_write_internal_lib_setup_helper(outConfig, reader, 0)) < 0) {
fprintf(stderr, "ERROR: Failed to open an output file (%ld, errno %d: %s), exiting.\n", returnVal, errno, strerror(errno));
CLICleanup(config, outConfig, headerBuffer);
return 1;
}
VERBOSE(if (config->verbose) { printf("Beginning data extraction loop\n"); });
// While we receive new data for the current event,
localLoops = 0;
while ((returnVal = lofar_udp_reader_step_timed(reader, timing)) < 1) {
if (returnVal < -1) {
returnValMeta = returnVal;
}
CLICK(tock0);
if (localLoops == 0) {
timing[0] = TICKTOCK(tick0, tock0) -
timing[1];
} // _file_reader_step or _reader_reuse does first I/O operation; approximate the time here
if (silent == 0) {
printf("Read complete for operation %ld after %f seconds (I/O: %lf, MemOps: %lf), return value: %ld\n",
loops, TICKTOCK(tick0, tock0), timing[0], timing[1], returnVal);
}
totalReadTime += timing[0];
totalOpsTime += timing[1];
// Write out the desired amount of packets; cap if needed.
packetsToWrite = reader->meta->packetsPerIteration;
if (splitEvery == LONG_MAX && maxPackets < packetsToWrite) {
packetsToWrite = maxPackets;
}
for (int8_t out = 0; out < reader->meta->numOutputs; out++) {
CLICK(tick1);
if ((returnVal = lofar_udp_metadata_write_file(reader, outConfig, out, reader->metadata, headerBuffer, 4096 * 8, localLoops == 0)) < 0) {
fprintf(stderr, "ERROR: Failed to write header to output (%ld, errno %d: %s), breaking.\n", returnVal, errno, strerror(errno));
returnValMeta = (returnValMeta < 0 && returnValMeta > -4) ? returnValMeta : -4;
break;
}
CLICK(tock1);
timing[2] += TICKTOCK(tick1, tock1);
CLICK(tick0);
VERBOSE(printf("Writing %ld bytes (%ld packets) to disk for output %d...\n",
packetsToWrite * reader->meta->packetOutputLength[out], packetsToWrite, out));
size_t outputLength = packetsToWrite * reader->meta->packetOutputLength[out];
size_t outputWritten;
if ((outputWritten = lofar_udp_io_write(outConfig, out, reader->meta->outputData[out],
outputLength)) != outputLength) {
fprintf(stderr, "ERROR: Failed to write data to output (%ld bytes/%ld bytes writen, errno %d: %s)), breaking.\n", outputWritten, outputLength, errno, strerror(errno));
returnValMeta = (returnValMeta < 0 && returnValMeta > -5) ? returnValMeta : -5;
break;
}
CLICK(tock0);
timing[3] += TICKTOCK(tick0, tock0);
}
if (splitEvery != LONG_MAX && returnValMeta > -2) {
if (!((localLoops + 1) % splitEvery)) {
// Close existing files
lofar_udp_io_write_cleanup(outConfig, 0);
// Open new files
if ((returnVal = _lofar_udp_io_write_internal_lib_setup_helper(outConfig, reader, (int32_t) (localLoops / splitEvery))) < 0) {
fprintf(stderr, "ERROR: Failed to open new file are breakpoint reached (%ld, errno %d: %s), breaking.\n", returnVal, errno, strerror(errno));
returnValMeta = (returnValMeta < 0 && returnValMeta > -6) ? returnValMeta : -6;
break;
}
}
}
totalMetadataTime += timing[2];
totalWriteTime += timing[3];
packetsWritten += packetsToWrite;
packetsProcessed += reader->meta->packetsPerIteration;
if (silent == 0) {
printf("Metadata processing for operation %ld after %f seconds.\n", loops, timing[2]);
printf("Disk writes completed for operation %ld after %f seconds.\n", loops, timing[3]);
for (int idx = 0; idx < TIMEARRLEN; idx++) {
timing[idx] = 0.;
}
if (returnVal == -1) {
for (int port = 0; port < reader->meta->numPorts; port++)
if (reader->meta->portLastDroppedPackets[port] != 0) {
printf("During this iteration there were %ld dropped packets on port %d.\n",
reader->meta->portLastDroppedPackets[port], port);
}
}
printf("\n");
}
loops++;
localLoops++;
#ifdef __SLOWDOWN
sleep(1);
#endif
CLICK(tick0);
// returnVal below -1 indicates we will not be given data on the next iteration, so gracefully exit with the known reason
if (returnValMeta < -1) {
break;
}
// returnVal below -1 indicates we will not be given data on the next iteration, so gracefully exit with the known reason
if (returnValMeta < -1) {
printf("We've hit a termination return value (%ld, %s), exiting.\n", returnValMeta,
exitReasons[abs((int32_t) returnValMeta)]);
break;
}
}
CLICK(tock);
int64_t droppedPackets = 0, totalPacketLength = 0, totalOutLength = 0;
// Print out a summary of the operations performed, this does not contain data read for seek operations
if (silent == 0) {
for (int8_t port = 0; port < reader->meta->numPorts; port++)
totalPacketLength += reader->meta->portPacketLength[port];
for (int8_t out = 0; out < reader->meta->numOutputs; out++)
totalOutLength += reader->meta->packetOutputLength[out];
for (int8_t port = 0; port < reader->meta->numPorts; port++)
droppedPackets += reader->meta->portTotalDroppedPackets[port];
printf("Reader loop exited (%ld); overall process took %f seconds.\n", returnVal, TICKTOCK(tick, tock));
printf("We processed %ld packets, representing %.03lf seconds of data", packetsProcessed,
(float) (reader->meta->numPorts * packetsProcessed * UDPNTIMESLICE) * 5.12e-6f);
if (reader->meta->numPorts > 1) {
printf(" (%.03lf per port)\n", (float) (packetsProcessed * UDPNTIMESLICE) * 5.12e-6f);
} else { printf(".\n"); }
printf("Total Read Time:\t%3.02lf s\t\t\tTotal CPU Ops Time:\t%3.02lf s\nTotal Write Time:\t%3.02lf s\t\t\tTotal MetaD Time:\t%3.02lf s\n", totalReadTime,
totalOpsTime, totalWriteTime, totalMetadataTime);
printf("Total Data Read:\t%3.03lf GB\t\tTotal Data Written:\t%3.03lf GB\n",
(double) (packetsProcessed * totalPacketLength) / 1e+9,
(double) (packetsWritten * totalOutLength) / 1e+9);
printf("Effective Read Speed:\t%3.01lf MB/s\t\tEffective Write Speed:\t%3.01lf MB/s\n", (double) (packetsProcessed * totalPacketLength) / 1e+6 / totalReadTime,
(double) (packetsWritten * totalOutLength) / 1e+6 / totalWriteTime);
printf("Approximate Throughput:\t%3.01lf GB/s\n", (double) (reader->meta->numPorts * packetsProcessed * (totalPacketLength + totalOutLength)) / 1e+9 / totalOpsTime);
printf("A total of %ld packets were missed during the observation.\n", droppedPackets);
printf("\n\nData processing finished. Cleaning up file and memory objects...\n");
}
// Clean-up the reader object, also closes the input files for us
lofar_udp_reader_cleanup(reader);
if (silent == 0) { printf("Reader cleanup performed successfully.\n"); }
// Cleanup the writer and close outputs
lofar_udp_io_write_cleanup(outConfig, 1);
outConfig = NULL;
// Free our malloc'd objects
CLICleanup(config, outConfig, headerBuffer);
if (silent == 0) { printf("CLI memory cleaned up successfully. Exiting.\n"); }
return 0;
}
/**
* Copyright (C) 2023 David McKenna
* This file is part of udpPacketManager <https://github.com/David-McKenna/udpPacketManager>.
*
* udpPacketManager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* udpPacketManager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with udpPacketManager. If not, see <http://www.gnu.org/licenses/>.
**/