psrdada-sys 0.2.2

Bindgen wrappers for psrdada
Documentation
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
/* To enable the use of O_DIRECT */
#define _GNU_SOURCE

#include "dada_client.h"
#include "dada_hdu.h"
#include "dada_def.h"
#include "disk_array.h"
#include "ascii_header.h"
#include "daemon.h"
#include "dada_affinity.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>

/* #define _DEBUG 1 */

void usage()
{
  fprintf (stdout,
           "dada_dbdisk [options]\n"
           " -b <core>  bind process to CPU core\n"
           " -k         hexadecimal shared memory key  [default: %x]\n"
           " -D <path>  add a disk to which data will be written\n"
           " -o         use O_DIRECT flag to bypass kernel buffering\n"
           " -W         over-write exisiting files\n"
           " -s         single transfer only\n"
           " -t bytes   set optimal bytes\n"
           " -z         use zero copy transfers\n"
           " -d         run as daemon\n", DADA_DEFAULT_BLOCK_KEY);
}

typedef struct {

  /* the set of disks to which data may be written */
  disk_array_t* array;

  /* current utc start, as defined by UTC_START attribute */
  char utc_start[64];

  uint64_t obs_offset;

  /* current filename */
  char file_name [FILENAME_MAX];

  /* file offset from start of data, as defined by FILE_NUMBER attribute */
  unsigned file_number;

  char o_direct;

  uint64_t optimal_bytes;

  unsigned verbose;

} dada_dbdisk_t;

#define DADA_DBDISK_INIT { 0, "", 0, "", 0, 0, 0, 0 }

/*! Function that opens the data transfer target */
int file_open_function (dada_client_t* client)
{
  /* the dada_dbdisk specific data */
  dada_dbdisk_t* dbdisk = 0;
  
  /* status and error logging facility */
  multilog_t* log;

  /* the header */
  char* header = 0;

  /* utc start, as defined by UTC_START attribute */
  char utc_start[64] = "";

  /* size of each file to be written in bytes, subject to % 512*RESOLUTION */
  /* use  FILE_SIZE is defined, else 10*BYTES_PER_SECOND */
  uint64_t file_size = 0;

  /* BYTES_PER_SECOND from the header */
  uint64_t bytes_ps= 0;

  /* the optimal buffer size for writing to file */
  uint64_t optimal_bytes = 0;

  /* the open file descriptor */
  int fd = -1;

  /* observation offset from the UTC_START */
  uint64_t obs_offset = 0;

  /* resolution of data [from packet size] defined in RESOLUTION */
  uint64_t resolution = 0;

  assert (client != 0);

  dbdisk = (dada_dbdisk_t*) client->context;
  assert (dbdisk != 0);
  assert (dbdisk->array != 0);

  log = client->log;
  assert (log != 0);

  header = client->header;
  assert (header != 0);

  // get the UTC_START 
  if (ascii_header_get (client->header, "UTC_START", "%s", utc_start) != 1)
  {
    multilog (log, LOG_WARNING, "Header with no UTC_START\n");
    strcpy (utc_start, "UNKNOWN");
  }

  // get the OBS_OFFSET
  if (ascii_header_get (client->header, "OBS_OFFSET", "%"PRIu64, &obs_offset) != 1)
  {
    multilog (log, LOG_WARNING, "Header with no OBS_OFFSET\n");
    obs_offset = 0;
  }

  // check to see if we are still working with the same observation
  if ((strcmp (utc_start, dbdisk->utc_start) != 0) || (obs_offset != dbdisk->obs_offset))
  {
    dbdisk->file_number = 0;
    if (dbdisk->verbose)
      multilog (log, LOG_INFO, "New UTC_START=%s, OBS_OFFSET=%"PRIu64" -> "
               "file number=0\n", utc_start, obs_offset);
  }
  else
  {
    dbdisk->file_number++;
    if (dbdisk->verbose)
      multilog (log, LOG_INFO, "Continue UTC_START=%s, OBS_OFFSET=%"PRIu64" -> "
                "file number=%lu\n", utc_start, obs_offset, dbdisk->file_number);
  }

  // Set the file number to be written to the header
  if (ascii_header_set (header, "FILE_NUMBER", "%u", dbdisk->file_number) < 0)
  {
    multilog (log, LOG_ERR, "Error writing FILE_NUMBER\n");
    return -1;
  }

  // set the current observation id
  strcpy (dbdisk->utc_start, utc_start);
  dbdisk->obs_offset = obs_offset;

  // create the current file name
  snprintf (dbdisk->file_name, FILENAME_MAX, "%s_%016"PRIu64".%06u.dada", 
            utc_start, obs_offset, dbdisk->file_number);

  // check if the file size is defined in the header
  if (ascii_header_get (header, "FILE_SIZE", "%"PRIu64, &file_size) != 1)
    file_size = 0;

  if (dbdisk->verbose)
    multilog (log, LOG_INFO, "open: advertised FILE_SIZE=%"PRIu64"\n", file_size);

  // get the bytes per second from the header
  if (ascii_header_get (header, "BYTES_PER_SECOND", "%"PRIu64, &bytes_ps) != 1) 
  {
    multilog (log, LOG_WARNING, "Header with no BYTES_PER_SECOND\n");
    bytes_ps = 64000000;
  }

  // get the resolution
  if (ascii_header_get (header, "RESOLUTION", "%"PRIu64, &resolution) != 1)
  {
    multilog (log, LOG_INFO, "Header with no RESOLUTION\n");
    resolution = 0;
  }

  // if file_size is not defined, then use 10 * bytes_ps
  if (file_size == 0) 
    file_size = bytes_ps * 10;

  uint64_t byte_multiple = 1;

  // If case we use O_DIRECT, must be aligned to 512 bytes
#ifdef O_DIRECT
  if (dbdisk->o_direct)
    byte_multiple = 512;
#endif

  if (dbdisk->verbose)
    multilog (log, LOG_INFO, "open: RESOULTION=%"PRIu64"\n", resolution);

  // If we have RESOLUTION, include this in byte_multiple
  if (resolution > 0)
    byte_multiple *= resolution;

  uint64_t gap = (obs_offset + file_size) % byte_multiple;

  file_size -= gap;

  if (gap > byte_multiple / 2) 
    file_size += byte_multiple;

  if (dbdisk->verbose) 
    multilog (log, LOG_INFO, "OBS_OFFSET=%"PRIu64", FILE_SIZE=%"PRIu64","
              " BYTE_MULTIPLE=%"PRIu64"\n", obs_offset, file_size, 
              byte_multiple);

  /* update header with actual FILE_SIZE */
  if (ascii_header_set (header, "FILE_SIZE", "%"PRIu64"", file_size) < 0) {
    multilog (log, LOG_WARNING, "Could not write FILE_SIZE to header\n");
  }

#ifdef _DEBUG
  fprintf (stderr, "dbdisk: open the file\n");
#endif

  /* Open the file */
  int flags = 0;

#ifdef O_DIRECT
  if (dbdisk->o_direct)
    flags = O_DIRECT;
#endif

  if (dbdisk->verbose)
    multilog (log, LOG_INFO, "open: dbdisk->file_name=%s file_size=%"
              PRIu64"\n", dbdisk->file_name, file_size);

  fd = disk_array_open (dbdisk->array, dbdisk->file_name,
        		file_size, &optimal_bytes, flags);

  if (fd < 0)
  {
    multilog (log, LOG_ERR, "Error opening %s: %s\n", dbdisk->file_name,
              strerror (errno));
    return -1;
  }

#ifndef O_DIRECT
  if (dbdisk->o_direct)
    fcntl (fd, F_NOCACHE, 1);
#endif

  client->fd = fd;
  client->transfer_bytes = file_size;
  client->optimal_bytes = 512 * optimal_bytes;
  
  multilog (log, LOG_INFO, "%s opened for writing %"PRIu64" bytes in %"PRIu64" chunks\n",
            dbdisk->file_name, client->transfer_bytes, client->optimal_bytes);


  return 0;
}

/*! Function that closes the data file */
int file_close_function (dada_client_t* client, uint64_t bytes_written)
{
  /* the dada_dbdisk specific data */
  dada_dbdisk_t* dbdisk = 0;

  /* status and error logging facility */
  multilog_t* log;

  assert (client != 0);

  dbdisk = (dada_dbdisk_t*) client->context;
  assert (dbdisk != 0);

  log = client->log;
  assert (log != 0);

  if (dbdisk->verbose)
    multilog (log, LOG_INFO, "close: wrote %"PRIu64" bytes\n", bytes_written);

  if (close (client->fd) < 0)
    multilog (log, LOG_ERR, "Error closing %s: %s\n", 
              dbdisk->file_name, strerror(errno));

  if (!bytes_written)
  {
    multilog (log, LOG_ERR, "Removing empty file: %s\n", dbdisk->file_name);

    if (chmod (dbdisk->file_name, S_IRWXU) < 0)
      multilog (log, LOG_ERR, "Error chmod (%s, rwx): %s\n", 
        	dbdisk->file_name, strerror(errno));
    
    if (remove (dbdisk->file_name) < 0)
      multilog (log, LOG_ERR, "Error remove (%s): %s\n", 
        	dbdisk->file_name, strerror(errno));
  }

  return 0;
}

/*! Pointer to the function that transfers data to/from the target */
int64_t file_write_function (dada_client_t* client, 
        		     void* data, uint64_t data_size)
{
  dada_dbdisk_t * dbdisk = (dada_dbdisk_t*) client->context;

  if (dbdisk->verbose)
    multilog (client->log, LOG_INFO, "write: %"PRIu64" bytes\n", data_size);

  uint64_t written  = 0;
  uint64_t to_write = 0;
  uint64_t wrote    = 0;
  

  while (written < data_size)
  {
    if (dbdisk->optimal_bytes)
    {
      to_write = dbdisk->optimal_bytes;
      if (written + to_write > data_size)
        to_write = data_size - written;
    }
    else
      to_write = data_size;

    // Need to check we that to_write is a 512 byte multiple if using o_direct
    if (dbdisk->o_direct && (to_write % 512 != 0)) 
    {

      multilog (client->log, LOG_WARNING, "Using O_DIRECT and data_size not mod 512\n");

      // write all except the mod 512 bytes;
      //uint64_t end = (data_size / 512) * 512;
      uint64_t end = (to_write / 512) * 512;
      wrote = write (client->fd, data + written, end);
      written += wrote;

      client->fd = disk_array_reopen(dbdisk->array, client->fd, dbdisk->file_name);

      if (client->fd < 0) 
      {
        multilog (client->log, LOG_ERR, "Could not reopen file: fd=%d\n",client->fd);
        return written;
      } 
      else 
      {
        // ensure O_DIRECT is now off 
        dbdisk->o_direct = 0;
        wrote = write (client->fd, data+written, to_write-wrote);
        written += wrote;
        return written;
      }
    }
    wrote = write (client->fd, data + written, to_write);
    if (wrote != to_write)
      multilog (client->log, LOG_ERR, "write: wrote[%"PRIu64"] != to_write[%"PRIu64"]\n", wrote, to_write);
    written += wrote;
  }

  return written;
}

/*! Pointer to the function that transfers data to/from the target */
int64_t file_write_block_function (dada_client_t* client, void* data, uint64_t data_size, uint64_t block_id)
{
  return file_write_function(client, data, data_size);
}


int main (int argc, char **argv)
{
  /* DADA Data Block to Disk configuration */
  dada_dbdisk_t dbdisk = DADA_DBDISK_INIT;

  /* DADA Header plus Data Unit */
  dada_hdu_t* hdu = 0;

  /* DADA Primary Read Client main loop */
  dada_client_t* client = 0;

  /* DADA Logger */
  multilog_t* log = 0;

  /* Flag set in daemon mode */
  char daemon = 0;

  /* Quit flag */
  char quit = 0;

  /* Zero copy flag */
  char zero_copy = 0;

  /* hexadecimal shared memory key */
  key_t dada_key = DADA_DEFAULT_BLOCK_KEY;

  int arg = 0;

  uint64_t optimal_bytes = 0;

  int cpu_core = -1;

  dbdisk.array = disk_array_create ();

  while ((arg=getopt(argc,argv,"b:k:dD:ot:vWsz")) != -1)
    switch (arg) {

    case 'b':
      cpu_core = atoi (optarg);
      break;

    case 'k':
      if (sscanf (optarg, "%x", &dada_key) != 1) {
        fprintf (stderr,"dada_dbdisk: could not parse key from %s\n",optarg);
        return -1;
      }
      break;

    case 'd':
      daemon=1;
      break;
      
    case 'D':
      if (disk_array_add (dbdisk.array, optarg) < 0) {
        fprintf (stderr, "Could not add '%s' to disk array\n", optarg);
        return EXIT_FAILURE;
      }
      break;
      
    case 'o':
      dbdisk.o_direct = 1;
      disk_array_set_overwrite (dbdisk.array, 1);
      break;

    case 't':
      optimal_bytes = atoi(optarg);
      break;

    case 'v':
      dbdisk.verbose = 1;

      break;
      
    case 'W':
      disk_array_set_overwrite (dbdisk.array, 1);
      break;

    case 's':
      quit = 1;
      break;

    case 'z':
      zero_copy = 1;
      break;

    default:
      usage ();
      return 0;
      
    }

  if (cpu_core >= 0)
    dada_bind_thread_to_core(cpu_core);

  log = multilog_open ("dada_dbdisk", daemon);

  if (daemon) {
    be_a_daemon ();
    multilog_serve (log, DADA_DEFAULT_DBDISK_LOG);
  }
  else
    multilog_add (log, stderr);

  hdu = dada_hdu_create (log);

  dada_hdu_set_key(hdu, dada_key);

  if (dada_hdu_connect (hdu) < 0)
    return EXIT_FAILURE;

  if (dada_hdu_lock_read (hdu) < 0)
    return EXIT_FAILURE;

  client = dada_client_create ();

  client->log = log;

  client->data_block = hdu->data_block;
  client->header_block = hdu->header_block;

  client->open_function  = file_open_function;

  if (zero_copy)
    client->io_block_function    = file_write_block_function;

  client->io_function    = file_write_function;
  client->close_function = file_close_function;
  client->direction      = dada_client_reader;

  dbdisk.optimal_bytes   = optimal_bytes;

  client->context = &dbdisk;

  while (!client->quit) {

    if (dada_client_read (client) < 0)
    {
      multilog (log, LOG_ERR, "dada_client_read failed\n");
      quit = 1;
    }

    if (quit) {
      client->quit = 1;
    }

  }

  if (dada_hdu_unlock_read (hdu) < 0)
    return EXIT_FAILURE;

  if (dada_hdu_disconnect (hdu) < 0)
    return EXIT_FAILURE;

  return EXIT_SUCCESS;
}