summaryrefslogtreecommitdiffstats
path: root/test/vfd_swmr_sparse_reader.c
blob: 3ac8935bbebd6397540f99cbcca2f3e1b777bc24 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * 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://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:     vfd_swmr_sparse_reader.c
 *              (copied and modified from swmr_sparse_reader.c)
 *
 * Purpose:     Reads data from a randomly selected subset of the datasets
 *              in the VFD SWMR test file.  Unlike the regular reader, these
 *              datasets will be shrinking.
 *
 *              This program is intended to run concurrently with the
 *              vfd_swmr_sparse_writer program.
 *
 *-------------------------------------------------------------------------
 */

/***********/
/* Headers */
/***********/

#include <inttypes.h>   /* for PRIu64 */

#include "h5test.h"
#include "vfd_swmr_common.h"

/****************/
/* Local Macros */
/****************/

#define TIMEOUT 30

/*******************/
/* Local Variables */
/*******************/

static hid_t symbol_tid = (-1);

/********************/
/* Local Prototypes */
/********************/

static int check_dataset(hid_t fid, unsigned verbose, const symbol_info_t *symbol,
    symbol_t *record, hid_t rec_sid);
static int read_records(const char *filename, unsigned verbose, unsigned long nrecords,
    unsigned poll_time, unsigned reopen_count);
static void usage(void);


/*-------------------------------------------------------------------------
 * Function:    check_dataset
 *
 * Purpose:     For a given dataset, checks to make sure that the stated
 *              and actual sizes are the same.  If they are not, then
 *              we have an inconsistent dataset due to a SWMR error.
 *
 * Parameters:  hid_t fid
 *              The SWMR test file's ID.
 *
 *              unsigned verbose
 *              Whether verbose console output is desired.
 *
 *              const symbol_info_t *symbol
 *              The dataset from which to read (the ID is in the struct).
 *              Must be pre-allocated.
 *
 *              symbol_t *record
 *              Memory for the record.  Must be pre-allocated.
 *
 *              hid_t rec_sid
 *              The memory dataspace for access.  It's always the same so
 *              there is no need to re-create it every time this function
 *              is called.
 *
 * Return:      Success:    0
 *              Failure:    -1
 *
 *-------------------------------------------------------------------------
 */
static int
check_dataset(hid_t fid, unsigned verbose, const symbol_info_t *symbol, symbol_t *record,
    hid_t rec_sid)
{
    hid_t dsid = -1;            /* Dataset ID */
    hid_t file_sid = -1;        /* Dataset's space ID */
    hsize_t start[2] = {0, 0};  /* Hyperslab selection values */
    hsize_t count[2] = {1, 1};  /* Hyperslab selection values */

    HDassert(fid >= 0);
    HDassert(symbol);
    HDassert(record);
    HDassert(rec_sid >= 0);

    /* Open dataset for symbol */
    if((dsid = H5Dopen2(fid, symbol->name, H5P_DEFAULT)) < 0)
        goto error;

    /* Get the dataset's dataspace */
    if((file_sid = H5Dget_space(dsid)) < 0)
        goto error;

    /* Choose the random record in the dataset (will be the same as chosen by
     * the writer) */
    start[1] = (hsize_t)HDrandom() % symbol->nrecords;
    if(H5Sselect_hyperslab(file_sid, H5S_SELECT_SET, start, NULL, count, NULL) < 0)
        goto error;

    /* Emit informational message */
    if(verbose)
        HDfprintf(stderr, "READER: Symbol = '%s', nrecords = %Hu, name = %s, location = %Hu, %Hu\n", \
                  symbol->name, symbol->nrecords, symbol->name, start[0], start[1]);

    /* Read record from dataset */
    record->rec_id = UINT64_MAX;
    if(H5Dread(dsid, symbol_tid, rec_sid, file_sid, H5P_DEFAULT, record) < 0)
        goto error;

    /* Verify record value */
    if(record->rec_id != start[1]) {
        HDfprintf(stderr, "*** READER: ERROR ***\n");
        HDfprintf(stderr, "Incorrect record value!\n");
        HDfprintf(stderr, "Symbol = '%s', location = %Hu, %Hu, record->rec_id = %" PRIu64 "\n", symbol->name, start[0], start[1], record->rec_id);
        goto error;
    } /* end if */

    /* Close the dataset's dataspace */
    if(H5Sclose(file_sid) < 0)
        goto error;

    /* Close dataset for symbol */
    if(H5Dclose(dsid) < 0)
        goto error;

    return 0;

error:

    H5E_BEGIN_TRY {
        H5Sclose(file_sid);
        H5Dclose(dsid);
    } H5E_END_TRY;

    return -1;
} /* end check_dataset() */


/*-------------------------------------------------------------------------
 * Function:    read_records
 *
 * Purpose:     For a given dataset, checks to make sure that the stated
 *              and actual sizes are the same.  If they are not, then
 *              we have an inconsistent dataset due to a SWMR error.
 *
 * Parameters:  const char *filename
 *              The SWMR test file's name.
 *
 *              unsigned verbose
 *              Whether verbose console output is desired.
 *
 *              unsigned long nrecords
 *              The total number of records to read.
 *
 *              unsigned poll_time
 *              The amount of time to sleep (s).
 *
 *              unsigned reopen_count
 *              
 *
 * Return:      Success:    0
 *              Failure:    -1
 *
 *-------------------------------------------------------------------------
 */
static int
read_records(const char *filename, unsigned verbose, unsigned long nrecords,
    unsigned poll_time, unsigned reopen_count)
{
    hid_t fid = H5I_INVALID_HID;
    hid_t aid = H5I_INVALID_HID;
    time_t start_time;          /* Starting time */
    hid_t mem_sid = H5I_INVALID_HID;
    symbol_t record;            /* The record to add to the dataset */
    unsigned seed;              /* Seed for random number generator */
    unsigned iter_to_reopen = reopen_count; /* # of iterations until reopen */
    unsigned long u;            /* Local index variable */
    hid_t fapl = H5I_INVALID_HID;
    H5F_vfd_swmr_config_t *config = NULL;   /* Configuration for VFD SWMR */

    HDassert(filename);
    HDassert(poll_time != 0);
    
    /* Create file access property list */
    if((fapl = h5_fileaccess()) < 0)
        goto error;

    if(H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI) < 0)
        goto error;

    /*
     * Set up to open the file with VFD SWMR configured.
     */
     /* Enable page buffering */
    if(H5Pset_page_buffer_size(fapl, 4096, 0, 0) < 0)
        goto error;

    /* Allocate memory for the configuration structure */
    if((config = HDcalloc(1, sizeof(H5F_vfd_swmr_config_t))) == NULL)
        goto error;

    config->version = H5F__CURR_VFD_SWMR_CONFIG_VERSION;
    config->tick_len = 4;
    config->max_lag = 5;
    config->writer = FALSE;
    config->md_pages_reserved = 128;
    HDstrcpy(config->md_file_path, "./my_md_file");

    /* Enable VFD SWMR configuration */
    if(H5Pset_vfd_swmr_config(fapl, config) < 0)
        goto error;

    /* Emit informational message */
    if(verbose)
        HDfprintf(stderr, "READER: Opening file: %s\n", filename);

    /* Open the file */
    /* Remove H5E_BEGIN_TRY/END_TRY to see the error stack if error */
    H5E_BEGIN_TRY {
        fid = H5Fopen(filename, H5F_ACC_RDONLY, fapl);
    } H5E_END_TRY;
    if(fid < 0) {
        HDfprintf(stderr, "READER: Error in opening the file: %s\n", filename);
        goto error;
    } else
        HDfprintf(stderr, "READER: SUCCESS in opening the file: %s\n", filename);

    /* Seed the random number generator with the attribute in the file */
    if((aid = H5Aopen(fid, "seed", H5P_DEFAULT)) < 0)
        goto error;
    if(H5Aread(aid, H5T_NATIVE_UINT, &seed) < 0)
        goto error;
    if(H5Aclose(aid) < 0)
        goto error;
    HDsrandom(seed);

    /* Reset the record */
    /* (record's 'info' field might need to change for each record written, also) */
    HDmemset(&record, 0, sizeof(record));

    /* Create a dataspace for the record to read */
    if((mem_sid = H5Screate(H5S_SCALAR)) < 0)
        goto error;

    /* Emit informational message */
    if(verbose)
        HDfprintf(stderr, "READER: Reading records\n");

    /* Read records */
    for(u = 0; u < nrecords; u++) {
        unsigned level, offset;
        symbol_info_t *symbol = NULL;   /* Symbol (dataset) */
        htri_t attr_exists;             /* Whether the sequence number attribute exists */
        unsigned long file_u;           /* Attribute sequence number (writer's "u") */

        /* Get a random dataset, according to the symbol distribution */
        symbol = choose_dataset(&level, &offset);

        /* Fill in "nrecords" field.  Note that this depends on the writer
         * using the same algorithm and "nrecords" */
        symbol->nrecords = nrecords / 5;

        /* Get the starting time */
        if ((start_time = HDtime(NULL)) == (time_t)-1) {
            fprintf(stderr, "READER: could not read time.\n");
            goto error;
        }

        /* Wait until we can read the dataset */
        for (;;) {
            /* Check if sequence attribute exists */
            if((attr_exists = H5Aexists_by_name(fid, symbol->name, "seq", H5P_DEFAULT)) < 0)
                goto error;

            if(attr_exists) {
                /* Read sequence number attribute */
                if((aid = H5Aopen_by_name(fid, symbol->name, "seq", H5P_DEFAULT, H5P_DEFAULT)) < 0)
                    goto error;
                if(H5Aread(aid, H5T_NATIVE_ULONG, &file_u) < 0)
                    goto error;
                if(H5Aclose(aid) < 0)
                    goto error;

                /* Check if sequence number is at least u - if so, this should
                 * guarantee that this record has been written */
                if(file_u >= u)
                    break;
            } /* end if */

            /* Check for timeout */
            if(HDtime(NULL) >= (time_t)(start_time + (time_t)TIMEOUT)) {
                HDfprintf(stderr,
                    "READER: Reader timed at record %lu level %u offset %u",
                        u, level, offset);
                if (attr_exists) {
                    HDfprintf(stderr, ", read sequence %lu\n", file_u);
                } else {
                    HDfprintf(stderr, ", read no sequence\n");
                    HDfprintf(stderr, ", read no sequence\n");
                }
                goto error;
            } /* end if */

            /* Pause */
            HDsleep(poll_time);

            /* Emit informational message */
            if(verbose)
                HDfprintf(stderr, "READER: Reopening file (do while loop): %s\n", filename);

            /* Retrieve and print the collection of metadata read retries */
            if(print_metadata_retries_info(fid) < 0)
                HDfprintf(stderr, "READER: Warning: could not obtain metadata retries info\n");

            /* Reopen the file */
            if(H5Fclose(fid) < 0)
                goto error;

            /* Remove H5E_BEGIN_TRY/END_TRY if you want to see the error stack */
            H5E_BEGIN_TRY {
                fid = H5Fopen(filename, H5F_ACC_RDONLY, fapl);
            } H5E_END_TRY;
            if(fid < 0) {
                HDfprintf(stderr, "READER: Error in reopening the file (do while loop): %s\n", filename);
                goto error;
            } 
            iter_to_reopen = reopen_count;
        }

        /* Emit informational message */
        if(verbose)
            HDfprintf(stderr, "READER: Checking dataset %lu\n", u);

        /* Check dataset */
        if(check_dataset(fid, verbose, symbol, &record, mem_sid) < 0)
            goto error;
        HDmemset(&record, 0, sizeof(record));

        /* Check for reopen */
        iter_to_reopen--;
        if(iter_to_reopen == 0) {
            /* Emit informational message */
            if(verbose)
                HDfprintf(stderr, "READER: Reopening file (iter_to_reopen): %s\n", filename);

            /* Retrieve and print the collection of metadata read retries */
            if(print_metadata_retries_info(fid) < 0)
                HDfprintf(stderr, "READER: Warning: could not obtain metadata retries info\n");

            /* Reopen the file */
            if(H5Fclose(fid) < 0)
                goto error;

            /* Remove H5E_BEGIN_TRY/END_TRY to see the error stack if error */
//            H5E_BEGIN_TRY {
                fid = H5Fopen(filename, H5F_ACC_RDONLY, fapl);
 //           } H5E_END_TRY;
            if(fid < 0) {
                HDfprintf(stderr, "READER: Error in reopening the file (iter_to_reopen): %s\n", filename);
                goto error;
            } 

            iter_to_reopen = reopen_count;
        } /* end if */
    } /* end while */

    /* Retrieve and print the collection of metadata read retries */
    if(print_metadata_retries_info(fid) < 0)
        HDfprintf(stderr, "READER: Warning: could not obtain metadata retries info\n");

    /* Close file */
    if(H5Fclose(fid) < 0)
        goto error;

    /* Close the memory dataspace */
    if(H5Sclose(mem_sid) < 0)
        goto error;

    /* Close the file access property list */
    if(H5Pclose(fapl) < 0)
        goto error;

    if(config)
        HDfree(config);

    return 0;

error:
    if(config)
        HDfree(config);

    H5E_BEGIN_TRY {
        H5Aclose(aid);
        H5Sclose(mem_sid);
        H5Fclose(fid);
        H5Pclose(fapl);
    } H5E_END_TRY;

    return -1;
} /* end read_records() */

static void
usage(void)
{
    printf("\n");
    printf("Usage error!\n");
    printf("\n");
    printf("Usage: vfd_swmr_sparse_reader [-q] [-s <# of seconds to wait for writer>]\n");
    printf("    [-n <# of reads between reopens>] <# of records>\n");
    printf("\n");
    printf("Defaults to verbose (no '-q' given), 1 second wait ('-s 1') and 1 read\n");
    printf("between reopens ('-r 1')\n");
    printf("\n");
    printf("Note that the # of records *must* be the same as that supplied to\n");
    printf("vfd_swmr_sparse_writer\n");
    printf("\n");
    HDexit(1);
} /* end usage() */

int main(int argc, const char *argv[])
{
    long nrecords = 0;      /* # of records to read */
    int poll_time = 1;      /* # of seconds to sleep when waiting for writer */
    int reopen_count = 1;   /* # of reads between reopens */
    unsigned verbose = 1;   /* Whether to emit some informational messages */
    unsigned u;             /* Local index variables */

    /* Parse command line options */
    if(argc < 2)
        usage();
    if(argc > 1) {
        u = 1;
        while(u < (unsigned)argc) {
            if(argv[u][0] == '-') {
                switch(argv[u][1]) {
                    /* # of reads between reopens */
                    case 'n':
                        reopen_count = HDatoi(argv[u + 1]);
                        if(reopen_count < 0)
                            usage();
                        u += 2;
                        break;

                    /* Be quiet */
                    case 'q':
                        verbose = 0;
                        u++;
                        break;

                    /* # of seconds between polling */
                    case 's':
                        poll_time = HDatoi(argv[u + 1]);
                        if(poll_time < 0)
                            usage();
                        u += 2;
                        break;

                    default:
                        usage();
                        break;
                } /* end switch */
            } /* end if */
            else {
                /* Get the number of records to read */
                nrecords = HDatol(argv[u]);
                if(nrecords <= 0)
                    usage();

                u++;
            } /* end else */
        } /* end while */
    } /* end if */

    /* Emit informational message */
    if(verbose) {
        HDfprintf(stderr, "READER: Parameters:\n");
        HDfprintf(stderr, "\t# of seconds between polling = %d\n", poll_time);
        HDfprintf(stderr, "\t# of reads between reopens = %d\n", reopen_count);
        HDfprintf(stderr, "\t# of records to read = %ld\n", nrecords);
    } /* end if */

    /* Emit informational message */
    if(verbose)
        HDfprintf(stderr, "READER: Generating symbol names\n");

    /* Generate dataset names */
    if(generate_symbols() < 0) {
        HDfprintf(stderr, "READER: Error generating symbol names!\n");
        HDexit(1);
    } /* end if */

    /* Create datatype for creating datasets */
    if((symbol_tid = create_symbol_datatype()) < 0) {
        HDfprintf(stderr, "READER: Error creating symbol datatype!\n");
        HDexit(1);
    }

    /* Reading records from datasets */
    if(read_records(COMMON_FILENAME, verbose, (unsigned long) nrecords, (unsigned)poll_time, (unsigned)reopen_count) < 0) {
        HDfprintf(stderr, "READER: Error reading records from datasets!\n");
        HDexit(1);
    } /* end if */

    /* Emit informational message */
    if(verbose)
        HDfprintf(stderr, "READER: Releasing symbols\n");

    /* Clean up the symbols */
    if(shutdown_symbols() < 0) {
        HDfprintf(stderr, "READER: Error releasing symbols!\n");
        HDexit(1);
    } /* end if */

    /* Emit informational message */
    if(verbose)
        HDfprintf(stderr, "READER: Closing objects\n");

    /* Close objects created */
    if(H5Tclose(symbol_tid) < 0) {
        HDfprintf(stderr, "READER: Error closing symbol datatype!\n");
        HDexit(1);
    } /* end if */

    return 0;
} /* main() */