summaryrefslogtreecommitdiffstats
path: root/test/vfd_swmr_zoo_writer.c
blob: 78ca06a7dea7fe0408a7bec6871bc6a6d580b627 (plain)
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by Akadio, Inc.                                                 *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define H5C_FRIEND /* suppress error about including H5Cpkg */
#define H5F_FRIEND /* suppress error about including H5Fpkg */

#include "hdf5.h"

#include "H5private.h"
#include "H5retry_private.h"
#include "H5Cpkg.h"
#include "H5Fpkg.h"
#include "H5HGprivate.h"
#include "H5VLprivate.h"

#include "testhdf5.h"
#include "genall5.h"
#include "vfd_swmr_common.h"

#define MAX_READ_LEN_IN_SECONDS 2
#define TICK_LEN                4

typedef struct _shared_ticks {
    uint64_t reader_tick;
} shared_ticks_t;

int                          fd_writer_to_reader = -1, fd_reader_to_writer = -1;
const char *                 fifo_writer_to_reader = "./fifo_writer_to_reader";
const char *                 fifo_reader_to_writer = "./fifo_reader_to_writer";
bool                         use_vfd_swmr          = true;
bool                         use_named_pipe        = true;
bool                         print_estack          = false;
static H5F_vfd_swmr_config_t swmr_config;
static bool                  writer;
struct timespec ival = {MAX_READ_LEN_IN_SECONDS, 0}; /* Expected maximal time for reader's validation */

zoo_config_t config = {.proc_num        = 0,
                       .skip_compact    = false,
                       .skip_varlen     = true,
                       .max_pause_msecs = 0,
                       .msgival         = {.tv_sec = 0, .tv_nsec = 0}};

static void
#ifndef H5C_COLLECT_CACHE_STATS
print_cache_hits(H5C_t *cache)
{
    int i;

    for (i = 0; i < H5AC_NTYPES; i++) {
        dbgf(3, "type-%d cache hits %" PRId64 "%s\n", i, cache->hits[i], (i == H5AC_GHEAP_ID) ? " *" : "");
    }
    dbgf(3, "\n");
}
#else
print_cache_hits(H5C_t H5_ATTR_UNUSED *cache)
{
    return;
}
#endif

void
zoo_create_hook(hid_t H5_ATTR_UNUSED fid)
{
    dbgf(3, "%s: enter\n", __func__);
    if (writer)
        decisleep(1);
}

/* Print out the menu for the command-line options */
static void
usage(const char *progname)
{
    HDfprintf(stderr, "usage: %s [-C] [-S] [-a] [-e] [-p] [-q] [-v]\n", progname);
    HDfprintf(stderr, "\n  -C: skip compact dataset tests\n");
    HDfprintf(stderr, "  -S: do not use VFD SWMR\n");
    HDfprintf(stderr, "  -a: run all tests, including variable-length data\n");
    HDfprintf(stderr, "  -e: print error stacks\n");
    HDfprintf(stderr, "  -l tick_num: expected maximal number of ticks from \n");
    HDfprintf(stderr,
              "     the writer's finishing zoo creation or deletion to the reader's finishing validation\n");
    HDfprintf(stderr, "  -N: do not use named pipes\n");
    HDfprintf(stderr, "  -q: be quiet: few/no progress messages\n");
    HDfprintf(stderr, "  -v: be verbose: most progress messages\n");
    HDexit(EXIT_FAILURE);
}

/* Private function to help parsing command-line options */
static int
parse_command_line_options(int argc, char **argv)
{
    int           ch;
    unsigned long tmpl;
    char *        end;

    while ((ch = getopt(argc, argv, "CSael:Nqv")) != -1) {
        switch (ch) {
            case 'C':
                config.skip_compact = true;
                break;
            case 'S':
                use_vfd_swmr = false;
                break;
            case 'a':
                config.skip_varlen = false;
                break;
            case 'e':
                print_estack = true;
                break;
            case 'l':
                /* Expected maximal number of ticks from the writer's finishing zoo creation or deletion
                 * to the reader's finishing validation of zoo creation or deletion */
                errno = 0;
                tmpl  = HDstrtoul(optarg, &end, 0);

                if (end == optarg || *end != '\0') {
                    HDprintf("couldn't parse `-l` argument `%s`", optarg);
                    goto error;
                }
                else if (errno != 0) {
                    HDprintf("couldn't parse `-l` argument `%s`", optarg);
                    goto error;
                }
                else if (tmpl > UINT_MAX) {
                    HDprintf("`-l` argument `%lu` too large", tmpl);
                    goto error;
                }

                {
                    /* Translate the tick number to time represented by the timespec struct */
                    float    time = (float)(((unsigned)tmpl * TICK_LEN) / 10.0);
                    unsigned sec  = (unsigned)time;
                    unsigned nsec = (unsigned)((time - sec) * 10 * 1000 * 1000);

                    ival.tv_sec  = sec;
                    ival.tv_nsec = nsec;
                }
                break;
            case 'N':
                /* Disable named pipes, mainly for running the writer and reader seperately */
                use_named_pipe = false;
                break;
            case 'q':
                verbosity = 1;
                break;
            case 'v':
                verbosity = 3;
                break;
            default:
                usage(argv[0]);
                break;
        }
    }
    argv += optind;
    argc -= optind;

    if (argc > 0) {
        H5_FAILED();
        AT();
        HDprintf("unexpected command-line arguments");
        goto error;
    }

    return 0;

error:
    return -1;
}

/* Writer creates two named pipes(FIFO) to coordinate two-way communication
 * between the writer and the reader.  Both the writer and reader open the named pipes */
static int
create_open_named_pipes(void)
{
    /* Writer creates two named pipes(FIFO) to coordinate two-way communication */
    if (writer) {
        if (HDmkfifo(fifo_writer_to_reader, 0600) < 0) {
            H5_FAILED();
            AT();
            HDprintf("HDmkfifo failed");
            goto error;
        }

        if (HDmkfifo(fifo_reader_to_writer, 0600) < 0) {
            H5_FAILED();
            AT();
            HDprintf("HDmkfifo failed");
            goto error;
        }
    }

    /* Both the writer and reader open the pipes */
    if ((fd_writer_to_reader = HDopen(fifo_writer_to_reader, O_RDWR)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("fifo_writer_to_reader open failed");
        goto error;
    }

    if ((fd_reader_to_writer = HDopen(fifo_reader_to_writer, O_RDWR)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("fifo_reader_to_writer open failed");
        goto error;
    }

    return 0;

error:
    return -1;
}

/* Notify the reader of finishing zoo creation by sending the timestamp
 * and wait for the reader to finish validation before proceeding */
static int
notify_and_wait_for_reader(hid_t fid, int verify)
{
    int             notify;
    unsigned int    i;
    struct timespec last = {0, 0};

    /* Get the time when finishing zoo creation */
    if (HDclock_gettime(CLOCK_MONOTONIC, &last) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDclock_gettime failed");
        goto error;
    }

    /* Notify the reader of finishing zoo creation by sending the timestamp */
    if (HDwrite(fd_writer_to_reader, &last, sizeof(last)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDwrite failed");
        goto error;
    }

    /* During the wait, writer makes repeated HDF5 API calls so as to trigger
     * EOT at approximately the correct time */
    for (i = 0; i < swmr_config.max_lag + 1; i++) {
        decisleep(swmr_config.tick_len);

        H5E_BEGIN_TRY
        {
            H5Aexists(fid, "nonexistent");
        }
        H5E_END_TRY;
    }

    /* Wait until the reader finishes validating zoo creation */
    if (HDread(fd_reader_to_writer, &notify, sizeof(int)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDread failed");
        goto error;
    }

    if (notify != verify) {
        H5_FAILED();
        AT();
        HDprintf("expected %d but read %d", verify, notify);
        goto error;
    }

    return 0;

error:
    return -1;
}

/* Notify the reader of finishing zoo deletion by sending the timestamp */
static int
notify_reader(void)
{
    struct timespec last = {0, 0};

    /* Get the time when finishing zoo deletion */
    if (HDclock_gettime(CLOCK_MONOTONIC, &last) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDclock_gettime failed");
        goto error;
    }

    /* Notify the reader about finishing zoo deletion by sending the timestamp */
    if (HDwrite(fd_writer_to_reader, &last, sizeof(last)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDwrite failed");
        goto error;
    }

    return 0;

error:
    return -1;
}

/* Wait for the writer's notice before starting to zoo validation */
static int
reader_verify(int verify)
{
    int notify;

    if (HDread(fd_writer_to_reader, &notify, sizeof(int)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDread failed");
        goto error;
    }

    if (notify != verify) {
        H5_FAILED();
        AT();
        HDprintf("expected %d but read %d", verify, notify);
        goto error;
    }

    return 0;

error:
    return -1;
}

/* Receive the notice of the writer finishing zoo creation (timestamp)
 * Make sure the zoo validation doesn't take longer than the expected time.
 * This time period is from the writer finishing zoo creation to the reader finishing
 * the validation of zoo creation */
static int
reader_check_time_and_notify_writer(int notify)
{
    struct timespec last = {0, 0};

    /* Receive the notice of the writer finishing zoo creation (timestamp) */
    if (HDread(fd_writer_to_reader, &last, sizeof(last)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDread failed");
        goto error;
    }

    /* Make sure the zoo validation doesn't take longer than the expected time.
     * This time period is from the writer finishing zoo creation to the reader finishing
     * the validation of zoo creation */
    if (below_speed_limit(&last, &ival)) {
        AT();
        HDfprintf(stderr, "validate_zoo took too long to finish");
    }

    /* Notify the writer that zoo validation is finished */
    if (HDwrite(fd_reader_to_writer, &notify, sizeof(int)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDwrite failed");
        goto error;
    }

    return 0;

error:
    return -1;
}

/* Receive the finish notice (timestamp) from the writer.
 * Make sure validation of zoo deletion doesn't take longer than the expected time.
 * This time period is from the writer finishing zoo deletion to the reader finishing
 * the validation of zoo deletion */
static int
reader_check_time_after_verify_deletion(void)
{
    struct timespec last = {0, 0};

    if (HDread(fd_writer_to_reader, &last, sizeof(last)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDread failed");
        goto error;
    }

    if (below_speed_limit(&last, &ival)) {
        AT();
        HDfprintf(stderr, "validate_deleted_zoo took too long to finish");
    }

    return 0;

error:
    return -1;
}

/* Close and remove the named pipes */
static int
close_named_pipes(void)
{
    /* Close the named pipes */
    if (HDclose(fd_writer_to_reader) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDclose failed\n");
        goto error;
    }

    if (HDclose(fd_reader_to_writer) < 0) {
        H5_FAILED();
        AT();
        HDprintf("HDclose failed\n");
        goto error;
    }

    /* Reader finishes last and deletes the named pipes */
    if (!writer) {
        if (HDremove(fifo_writer_to_reader) != 0) {
            H5_FAILED();
            AT();
            HDprintf("HDremove failed\n");
            goto error;
        }

        if (HDremove(fifo_reader_to_writer) != 0) {
            H5_FAILED();
            AT();
            HDprintf("HDremove failed\n");
            goto error;
        }
    }

    return 0;

error:
    return -1;
}

int
main(int argc, char **argv)
{
    hid_t                 fapl = H5I_INVALID_HID, fcpl = H5I_INVALID_HID, fid = H5I_INVALID_HID;
    H5F_t *               f;
    H5C_t *               cache;
    struct timespec       lastmsgtime = {.tv_sec = 0, .tv_nsec = 0};
    char *                progname    = NULL;
    char *                personality;
    estack_state_t        es;
    H5F_vfd_swmr_config_t vfd_swmr_config;
    int                   notify = 0, verify = 0;

    if (H5_basename(argv[0], &progname) < 0) {
        H5_FAILED();
        AT();
        HDprintf("H5_basename failed\n");
        goto error;
    }

    personality = HDstrstr(progname, "vfd_swmr_zoo_");

    if (personality != NULL && HDstrcmp(personality, "vfd_swmr_zoo_writer") == 0)
        writer = true;
    else if (personality != NULL && HDstrcmp(personality, "vfd_swmr_zoo_reader") == 0)
        writer = false;
    else {
        H5_FAILED();
        AT();
        HDprintf("unknown personality, expected vfd_swmr_zoo_{reader,writer}");
        goto error;
    }

    parse_command_line_options(argc, argv);

    /* config, tick_len, max_lag, writer, maintain_metadata_file, generate_updater_files,
     * flush_raw_data, md_pages_reserved, md_file_path, updater_file_path */
    init_vfd_swmr_config(&vfd_swmr_config, TICK_LEN, 7, writer, TRUE, FALSE, TRUE, 128, "./zoo-shadow", NULL);

    /* ? turn off use latest format argument via 1st argument? since later on it reset to early format */
    /* use_latest_format, use_vfd_swmr, only_meta_page, page_buf_size, config */
    if ((fapl = vfd_swmr_create_fapl(true, use_vfd_swmr, true, 4096, &vfd_swmr_config)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("vfd_swmr_create_fapl");
        goto error;
    }

    if (use_vfd_swmr && H5Pget_vfd_swmr_config(fapl, &swmr_config) < 0) {
        H5_FAILED();
        AT();
        HDprintf("H5Pget_vfd_swmr_config failed");
        goto error;
    }

    if (H5Pset_libver_bounds(fapl, H5F_LIBVER_EARLIEST, H5F_LIBVER_LATEST) < 0) {
        H5_FAILED();
        AT();
        HDprintf("H5Pset_libver_bounds failed");
        goto error;
    }

    if ((fcpl = vfd_swmr_create_fcpl(H5F_FSPACE_STRATEGY_PAGE, 4096)) < 0) {
        H5_FAILED();
        AT();
        HDprintf("vfd_swmr_create_fcpl() failed");
        goto error;
    }

    if (writer)
        fid = H5Fcreate("vfd_swmr_zoo.h5", H5F_ACC_TRUNC, fcpl, fapl);
    else
        fid = H5Fopen("vfd_swmr_zoo.h5", H5F_ACC_RDONLY, fapl);

    if (fid < 0) {
        H5_FAILED();
        AT();
        HDprintf(writer ? "H5Fcreate failed" : "H5Fopen failed");
        goto error;
    }

    if ((f = H5VL_object_verify(fid, H5I_FILE)) == NULL) {
        H5_FAILED();
        AT();
        HDprintf("H5VL_object_verify failed");
        goto error;
    }

    cache = f->shared->cache;

    /* Writer creates two named pipes(FIFO) to coordinate two-way communication
     * between the writer and the reader.  Both the writer and reader open the named pipes */
    if (use_named_pipe && create_open_named_pipes() < 0) {
        H5_FAILED();
        AT();
        HDprintf("create_open_named_pipes failed");
        goto error;
    }

    print_cache_hits(cache);

    es = print_estack ? estack_get_state() : disable_estack();
    if (writer) {
        dbgf(2, "Writing zoo...\n");

        /* Writer tells reader to start */
        notify = 1;
        if (use_named_pipe && HDwrite(fd_writer_to_reader, &notify, sizeof(int)) < 0) {
            H5_FAILED();
            AT();
            HDprintf("HDwrite failed");
            goto error;
        }

        /* Start to create the zoo */
        if (!create_zoo(fid, ".", &lastmsgtime, config)) {
            H5_FAILED();
            AT();
            HDprintf("create_zoo failed");
            goto error;
        }

        /* Notify the reader of finishing zoo creation by sending the timestamp
         * and wait for the reader to finish validation before proceeding */
        verify = 2;
        if (use_named_pipe && notify_and_wait_for_reader(fid, verify) < 0) {
            H5_FAILED();
            AT();
            HDprintf("notify_and_wait_for_reader failed");
            goto error;
        }

        /* Start to delete the zoo */
        if (!delete_zoo(fid, ".", &lastmsgtime, config)) {
            H5_FAILED();
            AT();
            HDprintf("delete_zoo failed");
            goto error;
        }

        /* Notify the reader of finishing zoo deletion by sending the timestamp */
        if (use_named_pipe && notify_reader() < 0) {
            H5_FAILED();
            AT();
            HDprintf("notify_reader failed");
            goto error;
        }
    }
    else {
        dbgf(2, "Reading zoo...\n");

        /* Wait for the writer's notice before starting to zoo validation */
        verify = 1;
        if (use_named_pipe && reader_verify(verify) < 0) {
            H5_FAILED();
            AT();
            HDprintf("reader_verify failed");
            goto error;
        }

        /* Validate the zoo creation */
        while (!validate_zoo(fid, ".", &lastmsgtime, config))
            ;

        /* Receive the notice of the writer finishing zoo creation (timestamp)
         * Make sure the zoo validation doesn't take longer than the expected time.
         * This time period is from the writer finishing zoo creation to the reader finishing
         * the validation of zoo creation */
        notify = 2;
        if (use_named_pipe && reader_check_time_and_notify_writer(notify) < 0) {
            H5_FAILED();
            AT();
            HDprintf("reader_check_time_and_notify_writer failed");
            goto error;
        }

        /* Start to validate the zoo deletion */
        while (!validate_deleted_zoo(fid, ".", &lastmsgtime, config))
            ;

        /* Receive the finish notice (timestamp) from the writer.
         * Make sure validation of zoo deletion doesn't take longer than the expected time.
         * This time period is from the writer finishing zoo deletion to the reader finishing
         * the validation of zoo deletion */
        if (use_named_pipe && reader_check_time_after_verify_deletion() < 0) {
            H5_FAILED();
            AT();
            HDprintf("reader_check_time_and_notify_writer failed");
            goto error;
        }
    }
    restore_estack(es);

    if (H5Pclose(fapl) < 0) {
        H5_FAILED();
        AT();
        HDprintf("H5Pclose failed");
        goto error;
    }

    if (H5Pclose(fcpl) < 0) {
        H5_FAILED();
        AT();
        HDprintf("H5Pclose failed");
        goto error;
    }

    if (H5Fclose(fid) < 0) {
        H5_FAILED();
        AT();
        HDprintf("H5Fclose failed");
        goto error;
    }

    if (progname)
        HDfree(progname);

    if (use_named_pipe && close_named_pipes() < 0) {
        H5_FAILED();
        AT();
        HDprintf("close_named_pipes failed");
        goto error;
    }

    return EXIT_SUCCESS;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl);
        H5Pclose(fcpl);
        H5Fclose(fid);
    }
    H5E_END_TRY;

    if (use_named_pipe && fd_writer_to_reader >= 0)
        HDclose(fd_writer_to_reader);

    if (use_named_pipe && fd_reader_to_writer >= 0)
        HDclose(fd_reader_to_writer);

    if (use_named_pipe && !writer) {
        HDremove(fifo_writer_to_reader);
        HDremove(fifo_reader_to_writer);
    }

    return EXIT_FAILURE;
}