summaryrefslogtreecommitdiffstats
path: root/tools/h5recover/trecover_verifier.c
blob: f88be8ec53bdae1597779590fc2805dbe5b7162d (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
689
690
691
692
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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 files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* 
 * This file contains a set of routines intended to verify that the crashed
 * file has been recovered correctly.
 *
 * We say that a crashed file is correctly recovered iff:
 *
 * 1) The file is readable.
 *
 * 2) If the crash file contains a dataset of the same name as a dataset
 *    that appears in the architype, 
 *
 *    a) it is of the same type and configuration,
 *
 *    b) it is the same size or smaller,
 *
 *    c) While metadata journaling does nothing about raw data, we should
 *       look at the raw data and comment if it is either not all garbage
 *       or filler value, or matching data for a while followed by garbage
 *       or filler falue
 *
 * 3) The crash file contains no dataset that are not in the architype 
 *    file.
 *
 * If the crash file is clearly not correctly recoverd, write a message 
 * to this effect to stderr, and return non-zero.
 *
 * If the raw data looks odd, write a message to stdout, and return zero.
 *
 * If all is OK, return 0
 *
 * Note that this code make a lot of assumptions about the structure
 * of the control and crash files.  I expect that this file will have 
 * to be reworked whenever the test file generation code is changed.
 *
 * 					JRM -- 12/8/08
 *
 * WARNING: The above is only partially implemented.  Specifically,
 * at present we only check for the chunked integer dataset type, and 
 * do not check for extraneous dataset.
 */

#include "trecover.h"

#define FALSE 0
#define TRUE  1

int
open_files(const char *filename, const char *ctl_filename)
{
    int ret_val = 0;

    ctl_file = H5Fopen(ctl_filename, H5F_ACC_RDONLY, H5P_DEFAULT);

    if ( ctl_file < 0 ) {

	ret_val = 1;
	fprintf(stderr, "Can't open control file \"%s\".\n", ctl_filename);
    }

    if ( ret_val == 0 ) {

        datafile = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);

        if ( datafile < 0 ) {

            ret_val = 1;
	    fprintf(stderr, "Can't open crash file \"%s\".\n", ctl_filename);
	    H5Fclose(ctl_file);
	    ctl_file = -1;
        
        }
    }

    return(ret_val);

}

int 
verify_chunked_dset(void)
{
    hbool_t done = FALSE;
    hbool_t dump_ctl_dset = FALSE;
    hbool_t dump_crash_dset = FALSE;
    hbool_t good_data;
    hbool_t odd_streak;
    hbool_t prev_entry_matched;
    int streak_len;
    int transitions;
    int ret_val = 0;
    int ctl_rank = -1;
    int crash_rank = -1;
    int ctl_data[NX * 4][NY];
    int crash_data[NX * 4][NY];
    unsigned int i, j;
    herr_t ctl_result;
    herr_t crash_result;
    hid_t ctl_dset = -1;
    hid_t crash_dset = -1;
    hid_t ctl_dspace = -1;
    hid_t crash_dspace = -1;
    hid_t ctl_dtype = -1;
    hid_t crash_dtype = -1;
    hid_t ctl_plist = -1;
    hid_t crash_plist = -1;
    hid_t ctl_memspace = -1;
    hid_t crash_memspace = -1;
    hsize_t zero_offset[RANK] = {0, 0};
    hsize_t ctl_dims[RANK];
    hsize_t crash_dims[RANK];
    hsize_t ctl_maxdims[RANK];
    hsize_t crash_maxdims[RANK];
    hsize_t ctl_chnkdims[RANK];
    hsize_t crash_chnkdims[RANK];
    hsize_t ctl_memspace_dims[RANK];
    hsize_t crash_memspace_dims[RANK];

    /* Check to see if the dataset appears in both the ctl and crash 
     * files.
     */
    if ( ret_val == 0 )
    {
        ctl_dset = H5Dopen2(ctl_file, CHUNKDATASETNAME, H5P_DEFAULT);

	if ( ctl_dset >= 0 ) {

	    crash_dset = H5Dopen2(datafile, CHUNKDATASETNAME, H5P_DEFAULT);

	    if ( crash_dset < 0 ) {

	        /* Dataset doesn't appear in the crash file, so we are
                 * done.
		 */

                done = TRUE;

	    }
	} else {

	    crash_dset = H5Dopen2(datafile, DATASETNAME, H5P_DEFAULT);

	    if ( crash_dset >= 0 ) {

	        ret_val = 1;
		fprintf(stderr, 
		    "dataset %s appears in crash file but not in ctl file.\n",
		    DATASETNAME);
	    }

	}
    }


    /* Get the dataspaces of the data sets */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

	ctl_dspace = H5Dget_space(ctl_dset);
	crash_dspace = H5Dget_space(crash_dset);

	if ( ( ctl_dspace < 0 ) || ( crash_dspace < 0 ) ) {

	    ret_val = 1;
	    fprintf(stderr, "Can't get one or more data spaces.\n");

	}
    }


    /* verify that ranks are as expected */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_rank = H5Sget_simple_extent_ndims(ctl_dspace);
        crash_rank = H5Sget_simple_extent_ndims(crash_dspace);

        if ( ( ctl_rank < 0 ) || ( crash_rank < 0 ) ) {

            ret_val = 1;
            fprintf(stderr, "Can't get one or more data space ranks.\n");

        } else if ( ctl_rank != crash_rank ) {

	    ret_val = 1;
            fprintf(stderr, "Contig dspace rank mismatch.\n");

        } else if ( ctl_rank != RANK ) {

	    ret_val = 1;
            fprintf(stderr, "Unexpected chunk dspace rank(1).\n");

	}
    }


    /* get the extents */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_rank = H5Sget_simple_extent_dims(ctl_dspace, 
                                             ctl_dims, 
                                             ctl_maxdims );
        if ( ctl_rank != RANK ) {

	    ret_val = 1;
            fprintf(stderr, "Unexpected chunk ctl dspace rank.\n");

	}

        crash_rank =  H5Sget_simple_extent_dims(crash_dspace, 
                                                crash_dims, 
                                                crash_maxdims);
        if ( ctl_rank != RANK ) {

	    ret_val = 1;
            fprintf(stderr, "Unexpected chunk crash dspace rank.\n");

	}
    }


    /* verify that the dims contain the expected data */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        if ( crash_dims[0] > ctl_dims[0] ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace crash_dims[0] > ctl_dims[0].\n");
            fprintf(stderr, "ctl_dims[0] = %d, crash_dims[0] = %d.\n",
                    (int)(ctl_dims[0]), (int)(crash_dims[0]));

        } else if ( (ctl_dims[0] % NX) != 0 ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace (ctl_dims[0] mod NX) != 0.\n");

        } else if ( (crash_dims[0] % NX) != 0 ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace (crash_dims[0] mod NX) != 0.\n");

        } else if ( crash_dims[1] != ctl_dims[1] ) {

	    ret_val = 1;
            fprintf(stderr, "crash_dims[1] != ctl_dims[1].\n");

        } else if ( (ctl_dims[1] % NY) != 0 ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace (ctl_dims[1] mod NY) != 0.\n");

        } else if ( (crash_dims[1] % NY) != 0 ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace (crash_dims[1] mod NY) != 0.\n");

        } else if ( ( ctl_dims[0] > 4 * NX ) || ( ctl_dims[1] > NY ) ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace ctl dims too big.\n");

        } else if ( ( crash_dims[0] > 4 * NX ) || ( crash_dims[1] > NY ) ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace crash dims too big.\n");

        }
    }


    /* verify that the maxdims contain the expected data */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        if ( ctl_maxdims[0] != H5S_UNLIMITED ) {

	    ret_val = 1;
            fprintf(stderr, 
                      "chunk dspace ctl_maxdims[0] != H5S_UNLIMITED.\n");

	} else if ( ctl_maxdims[1] != NY ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace ctl_maxdims[1] != NY.\n");

        } else if ( crash_maxdims[0] != H5S_UNLIMITED ) {

	    ret_val = 1;
            fprintf(stderr, 
                      "chunk dspace crash_maxdims[0] != H5S_UNLIMITED.\n");

	} else if ( crash_maxdims[1] != NY ) {

	    ret_val = 1;
            fprintf(stderr, "chunk dspace crash_maxdims[1] != NY.\n");
        }
    }


    /* get the underlying type of the data sets, and verify that it 
     * is as expected 
     */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_dtype = H5Dget_type(ctl_dset);
        crash_dtype = H5Dget_type(crash_dset);

        if ( H5Tequal(ctl_dtype, H5T_STD_I32LE) <= 0 ) {

	    ret_val = 1;
            fprintf(stderr, "unexpected chunked ctl_dset dtype.\n");

        } else if ( H5Tequal( crash_dtype, H5T_STD_I32LE ) <= 0 ) {

	    ret_val = 1;
            fprintf(stderr, "unexpected chunked crash_dset dtype.\n");

        }
    }


    /* get the creation property lists of the datasets, and verify
     * that they contain the expected data.
     */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_plist = H5Dget_create_plist(ctl_dset);
        crash_plist = H5Dget_create_plist(crash_dset);

        if ( ( ctl_plist < 0 ) || ( crash_plist < 0 ) ) {

	    ret_val = 1;
            fprintf(stderr, "Failure obtaining  chnked dset plist(s).\n");

        }
    }


    /* verify that the data sets are chunked as expected */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        if ( ( H5Pget_layout(ctl_plist) != H5D_CHUNKED ) ||
             ( H5Pget_layout(crash_plist) != H5D_CHUNKED ) ) {

	    ret_val = 1;
            fprintf(stderr, "One or more chnked dsets not chnked?.\n");

        }
    }


    /* verify that the data sets have the expected chunk sizes */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        if ( ( H5Pget_chunk(ctl_plist, ctl_rank, ctl_chnkdims) < 0 ) ||
             ( H5Pget_chunk(crash_plist, crash_rank, crash_chnkdims) < 0 ) ) {

            ret_val = 1;
            fprintf(stderr, "Failure obtaining chnk dimss).\n");
        }
        else if ( ( ctl_chnkdims[0] != ChunkX ) ||
                  ( ctl_chnkdims[1] != ChunkY ) ||
                  ( crash_chnkdims[0] != ChunkX ) ||
                  ( crash_chnkdims[1] != ChunkY ) ) {

            ret_val = 1;
            fprintf(stderr, "unexpected chnk dimss.\n");
        }
    }

    /* finally, read in the contents of the dsets and compare them.
     *
     * Since we are not journaling raw data, it is hard to say if 
     * any particular set of value is right or wrong, but we will
     * try to spot anything odd and comment.
     */

    /* start by setting up the memory spaces and mem space selections */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_memspace_dims[0] = ctl_dims[0];
        ctl_memspace_dims[1] = ctl_dims[1];
    
	ctl_memspace = H5Screate_simple(RANK, ctl_memspace_dims, NULL);

        crash_memspace_dims[0] = crash_dims[0];
        crash_memspace_dims[1] = crash_dims[1];
    
	crash_memspace = H5Screate_simple(RANK, crash_memspace_dims, NULL);

	if ( ( ctl_memspace < 0 ) || ( crash_memspace < 0 ) ) {

            ret_val = 1;
            fprintf(stderr, "memspace allocation failed.\n");

        } else {

	    ctl_result = H5Sselect_hyperslab(ctl_memspace, H5S_SELECT_SET,
			                     zero_offset, NULL, 
					     ctl_memspace_dims, NULL);

	    crash_result = H5Sselect_hyperslab(crash_memspace, H5S_SELECT_SET,
			                       zero_offset, NULL, 
					       crash_memspace_dims, NULL);

	    if ( ( ctl_result < 0 ) || ( crash_result < 0 ) ) {

                ret_val = 1;
                fprintf(stderr, "memspace hyperslab selection(s) failed.\n");

	    }
	}
    }


    /* set up the file space selections */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_result = H5Sselect_hyperslab(ctl_dspace, H5S_SELECT_SET, 
			                 zero_offset, NULL, ctl_dims, NULL);

        crash_result = H5Sselect_hyperslab(crash_dspace, H5S_SELECT_SET, 
			                   zero_offset, NULL, crash_dims, NULL);

        if ( ( ctl_result < 0 ) || ( crash_result < 0 ) ) {

            ret_val = 1;
            fprintf(stderr, "file hyperslab selection(s) failed.\n");
        }
    }


    /* read in the contents of the data sets */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        ctl_result = H5Dread(ctl_dset, H5T_NATIVE_INT, ctl_memspace,
			     ctl_dspace, H5P_DEFAULT, ctl_data);

        crash_result = H5Dread(crash_dset, H5T_NATIVE_INT, crash_memspace,
			       crash_dspace, H5P_DEFAULT, crash_data);


        if ( ( ctl_result < 0 ) || ( crash_result < 0 ) ) {

            ret_val = 1;
            fprintf(stderr, "chunked dset read(s) failed.\n");
        }
    }

    /* scan the data:
     *
     * Since we are only doing metadata journaling, not raw data journaling,
     * it is hard to say that any particular raw data is wrong.  However,
     * typically, we should see one of the following cases:
     *
     * 1) All good data.
     *
     * 2) All bad data.
     *
     * 3) Good data at the beginning, turning to gargbage somewhere along 
     *    the way.  This should be the typical case.
     *    Further, since we this is a chunked data set, we would expect
     *    each chunk to be all good or all bad.
     *
     * The following code is an attempt to check this.  We will not flag
     * any errors, but if we see anything odd, we will dump the contents 
     * of the data set.
     */

    if ( ( ret_val == 0 ) && ( ! done ) ) {

        /* do a raster scan of the dataset and cont the number of 
         * transitions from good data to bad data and vise versa.
         * 
         * If there are "too many" transitions, make note and request
         * a dump of the data sets.
         *
         * Similarly, if any streak of good or bad data is not a 
         * multiple of the chunk height, make note and request a 
         * dump of the data sets.
         */

        good_data = TRUE;
        odd_streak = FALSE;
        prev_entry_matched = FALSE;
        streak_len = 0;
        transitions = 0;

        for ( i = 0; i < crash_dims[0]; i++ )
        {
	    for ( j = 0; j < crash_dims[1]; j++ )
            {
                if ( good_data ) {

                    if ( ctl_data[i][j] != crash_data[i][j] ) {

                        good_data = FALSE;
                        prev_entry_matched = FALSE;
                        transitions++;

                        if ( (streak_len % ChunkY) != 0 ) {

                            odd_streak = TRUE;
                        }
                        streak_len = 0;

                    } else {

                        streak_len++;

                    }
                } else {

                    if ( ctl_data[i][j] == crash_data[i][j] ) {

                        /* don't begin a good data streak unless we have 
                         * two good values in a row.
                         */
                        if ( prev_entry_matched ) {

                            good_data = TRUE;
                            transitions++;

                            if ( ((streak_len - 1) % ChunkY) != 0 ) {

                                odd_streak = TRUE;
                            }
                            streak_len = 1;
                        } else {
                            prev_entry_matched = TRUE;
                        }
                    } else {

                        prev_entry_matched = FALSE;
                        streak_len++;

                    }
                }
            }
        }

        if ( odd_streak ) {

            fprintf(stdout, "\nFound odd length streak of good or bad data.\n");
            fprintf(stdout, "Dumping the ctl and crash chunked data sets.\n");
            dump_ctl_dset = TRUE;
            dump_crash_dset = TRUE;
        }

        if ( transitions > (2 * ChunkX) ) {

            fprintf(stdout, 
                    "\nToo many transitions between good and bad data.\n");
            fprintf(stdout, "Dumping the ctl and crash chunked data sets.\n");
            dump_ctl_dset = TRUE;
            dump_crash_dset = TRUE;
        }
    }


    /* dump ctl and/or crash data set if requested */
    if ( ( ret_val == 0 ) && ( ! done ) ) {

        if ( dump_ctl_dset ) {

	    fprintf(stdout, "\nctl data:\n");

	    for ( i = 0; i < ctl_dims[0]; i++ )
	    {
		fprintf(stdout, "(%d,0): ", i);

                for ( j = 0; j < ctl_dims[1]; j++ )
		{
                    fprintf(stdout, "%d, ", ctl_data[i][j]);
		}
		fprintf(stdout, "\n");
	    }

        }

        if ( dump_crash_dset ) {

	    fprintf(stdout, "\ncrash data:\n");

	    for ( i = 0; i < crash_dims[0]; i++ )
	    {
		fprintf(stdout, "(%d,0): ", i);

                for ( j = 0; j < crash_dims[1]; j++ )
		{
                    fprintf(stdout, "%d, ", crash_data[i][j]);
		}
		fprintf(stdout, "\n");
	    }
        }
    }

    /* tidy up */

    if ( ctl_memspace >= 0 ) {
	H5Sclose(ctl_memspace);
	ctl_memspace = -1;
    }

    if ( crash_memspace >= 0 ) {
	H5Sclose(crash_memspace);
	crash_memspace = -1;
    }


    if ( ctl_dspace >= 0 ) {
	H5Sclose(ctl_dspace);
	ctl_dspace = -1;
    }

    if ( crash_dspace >= 0 ) {
	H5Sclose(crash_dspace);
	crash_dspace = -1;
    }

    if ( ctl_plist >= 0 ) {
	H5Pclose(ctl_plist);
	ctl_plist = -1;
    }

    if ( crash_plist >= 0 ) {
	H5Pclose(crash_plist);
	crash_plist = -1;
    }

    if ( ctl_dtype >= 0 ) {
        H5Tclose(ctl_dtype);
	ctl_dtype = -1;
    }

    if ( crash_dtype >= 0 ) {
        H5Tclose(crash_dtype);
	crash_dtype = -1;
    }

    if ( ctl_dset >= 0 ) {
	H5Dclose(ctl_dset);
	ctl_dset = -1;
    }

    if ( crash_dset >= 0 ) {
	H5Dclose(crash_dset);
	crash_dset = -1;
    }

    return(ret_val);

} /* verify_chunked_dset() */


int 
verify_recovery(void)
{
    int ret_val = 0;

    ret_val = open_files(H5FILE_NAME, CTL_H5FILE_NAME);

    if ( ret_val == 0 ) {

        ret_val = verify_chunked_dset();
    }

    if ( ctl_file >= 0 ) {

	H5Fclose(ctl_file);
	ctl_file = -1;
    }

    if ( datafile >= 0 ) {

	H5Fclose(datafile);
	datafile = -1;
    }

    return(ret_val);
}