forked from HDFGroup/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH5Cimage.c
2818 lines (2429 loc) · 109 KB
/
H5Cimage.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 LICENSE 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 *
* [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------
*
* Created: H5Cimage.c
*
* Purpose: Functions in this file are specific to the implementation
* of the metadata cache image feature.
*
*-------------------------------------------------------------------------
*/
/****************/
/* Module Setup */
/****************/
#include "H5Cmodule.h" /* This source code file is part of the H5C module */
#define H5F_FRIEND /*suppress error about including H5Fpkg */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#ifdef H5_HAVE_PARALLEL
#define H5AC_FRIEND /*suppress error about including H5ACpkg */
#include "H5ACpkg.h" /* Metadata cache */
#endif /* H5_HAVE_PARALLEL */
#include "H5Cpkg.h" /* Cache */
#include "H5Eprivate.h" /* Error handling */
#include "H5Fpkg.h" /* Files */
#include "H5FDprivate.h" /* File drivers */
#include "H5FLprivate.h" /* Free Lists */
#include "H5MMprivate.h" /* Memory management */
/****************/
/* Local Macros */
/****************/
#if H5C_DO_MEMORY_SANITY_CHECKS
#define H5C_IMAGE_EXTRA_SPACE 8
#define H5C_IMAGE_SANITY_VALUE "DeadBeef"
#else /* H5C_DO_MEMORY_SANITY_CHECKS */
#define H5C_IMAGE_EXTRA_SPACE 0
#endif /* H5C_DO_MEMORY_SANITY_CHECKS */
/* Cache image buffer components, on disk */
#define H5C__MDCI_BLOCK_SIGNATURE "MDCI"
#define H5C__MDCI_BLOCK_SIGNATURE_LEN 4
#define H5C__MDCI_BLOCK_VERSION_0 0
/* Metadata cache image header flags -- max 8 bits */
#define H5C__MDCI_HEADER_HAVE_RESIZE_STATUS 0x01
/* Metadata cache image entry flags -- max 8 bits */
#define H5C__MDCI_ENTRY_DIRTY_FLAG 0x01
#define H5C__MDCI_ENTRY_IN_LRU_FLAG 0x02
#define H5C__MDCI_ENTRY_IS_FD_PARENT_FLAG 0x04
#define H5C__MDCI_ENTRY_IS_FD_CHILD_FLAG 0x08
/* Limits on flush dependency values, stored in 16-bit values on disk */
#define H5C__MDCI_MAX_FD_CHILDREN USHRT_MAX
#define H5C__MDCI_MAX_FD_PARENTS USHRT_MAX
/* Maximum ring allowed in image */
#define H5C_MAX_RING_IN_IMAGE H5C_RING_MDFSM
/***********************************************************************
*
* Stats collection macros
*
* The following macros must handle stats collection when collection
* is enabled, and evaluate to the empty string when it is not.
*
***********************************************************************/
#if H5C_COLLECT_CACHE_STATS
#define H5C__UPDATE_STATS_FOR_CACHE_IMAGE_CREATE(cache_ptr) \
do { \
(cache_ptr)->images_created++; \
} while (0)
#define H5C__UPDATE_STATS_FOR_CACHE_IMAGE_READ(cache_ptr) \
do { \
/* make sure image len is still good */ \
assert((cache_ptr)->image_len > 0); \
(cache_ptr)->images_read++; \
} while (0)
#define H5C__UPDATE_STATS_FOR_CACHE_IMAGE_LOAD(cache_ptr) \
do { \
/* make sure image len is still good */ \
assert((cache_ptr)->image_len > 0); \
(cache_ptr)->images_loaded++; \
(cache_ptr)->last_image_size = (cache_ptr)->image_len; \
} while (0)
#else /* H5C_COLLECT_CACHE_STATS */
#define H5C__UPDATE_STATS_FOR_CACHE_IMAGE_CREATE(cache_ptr)
#define H5C__UPDATE_STATS_FOR_CACHE_IMAGE_READ(cache_ptr)
#define H5C__UPDATE_STATS_FOR_CACHE_IMAGE_LOAD(cache_ptr)
#endif /* H5C_COLLECT_CACHE_STATS */
/******************/
/* Local Typedefs */
/******************/
/********************/
/* Local Prototypes */
/********************/
/* Helper routines */
static size_t H5C__cache_image_block_entry_header_size(const H5F_t *f);
static size_t H5C__cache_image_block_header_size(const H5F_t *f);
static herr_t H5C__decode_cache_image_header(const H5F_t *f, H5C_t *cache_ptr, const uint8_t **buf);
#ifndef NDEBUG /* only used in assertions */
static herr_t H5C__decode_cache_image_entry(const H5F_t *f, const H5C_t *cache_ptr, const uint8_t **buf,
unsigned entry_num);
#endif
static herr_t H5C__encode_cache_image_header(const H5F_t *f, const H5C_t *cache_ptr, uint8_t **buf);
static herr_t H5C__encode_cache_image_entry(H5F_t *f, H5C_t *cache_ptr, uint8_t **buf, unsigned entry_num);
static herr_t H5C__prep_for_file_close__compute_fd_heights(const H5C_t *cache_ptr);
static void H5C__prep_for_file_close__compute_fd_heights_real(H5C_cache_entry_t *entry_ptr,
uint32_t fd_height);
static herr_t H5C__prep_for_file_close__setup_image_entries_array(H5C_t *cache_ptr);
static herr_t H5C__prep_for_file_close__scan_entries(const H5F_t *f, H5C_t *cache_ptr);
static herr_t H5C__reconstruct_cache_contents(H5F_t *f, H5C_t *cache_ptr);
static H5C_cache_entry_t *H5C__reconstruct_cache_entry(const H5F_t *f, H5C_t *cache_ptr, const uint8_t **buf);
static herr_t H5C__write_cache_image_superblock_msg(H5F_t *f, bool create);
static herr_t H5C__read_cache_image(H5F_t *f, H5C_t *cache_ptr);
static herr_t H5C__write_cache_image(H5F_t *f, const H5C_t *cache_ptr);
static herr_t H5C__construct_cache_image_buffer(H5F_t *f, H5C_t *cache_ptr);
static herr_t H5C__free_image_entries_array(H5C_t *cache_ptr);
/*********************/
/* Package Variables */
/*********************/
/* Declare a free list to manage H5C_cache_entry_t objects */
H5FL_DEFINE(H5C_cache_entry_t);
/*****************************/
/* Library Private Variables */
/*****************************/
/*******************/
/* Local Variables */
/*******************/
/*-------------------------------------------------------------------------
* Function: H5C_cache_image_pending()
*
* Purpose: Tests to see if the load of a metadata cache image
* load is pending (i.e. will be executed on the next
* protect or insert)
*
* Returns true if a cache image load is pending, and false
* if not. Throws an assertion failure on error.
*
* Return: true if a cache image load is pending, and false otherwise.
*
*-------------------------------------------------------------------------
*/
bool
H5C_cache_image_pending(const H5C_t *cache_ptr)
{
bool ret_value = true; /* Return value */
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* Sanity checks */
assert(cache_ptr);
ret_value = (cache_ptr->load_image && !cache_ptr->image_loaded);
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_cache_image_pending() */
/*-------------------------------------------------------------------------
* Function: H5C_cache_image_status()
*
* Purpose: Examine the metadata cache associated with the supplied
* instance of H5F_t to determine whether the load of a
* cache image has either been queued or executed, and if
* construction of a cache image has been requested.
*
* This done, it set *load_ci_ptr to true if a cache image
* has either been loaded or a load has been requested, and
* to false otherwise.
*
* Similarly, set *write_ci_ptr to true if construction of
* a cache image has been requested, and to false otherwise.
*
* Return: SUCCEED on success, and FAIL on failure.
*
*-------------------------------------------------------------------------
*/
herr_t
H5C_cache_image_status(H5F_t *f, bool *load_ci_ptr, bool *write_ci_ptr)
{
H5C_t *cache_ptr;
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* Sanity checks */
assert(f);
assert(f->shared);
cache_ptr = f->shared->cache;
assert(cache_ptr);
assert(load_ci_ptr);
assert(write_ci_ptr);
*load_ci_ptr = cache_ptr->load_image || cache_ptr->image_loaded;
*write_ci_ptr = cache_ptr->image_ctl.generate_image;
FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5C_cache_image_status() */
/*-------------------------------------------------------------------------
* Function: H5C__construct_cache_image_buffer()
*
* Purpose: Allocate a buffer of size cache_ptr->image_len, and
* load it with an image of the metadata cache image block.
*
* Note that by the time this function is called, the cache
* should have removed all entries from its data structures.
*
* Return: SUCCEED on success, and FAIL on failure.
*
*-------------------------------------------------------------------------
*/
static herr_t
H5C__construct_cache_image_buffer(H5F_t *f, H5C_t *cache_ptr)
{
uint8_t *p; /* Pointer into image buffer */
uint32_t chksum;
unsigned u; /* Local index variable */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity checks */
assert(f);
assert(f->shared);
assert(cache_ptr == f->shared->cache);
assert(cache_ptr);
assert(cache_ptr->close_warning_received);
assert(cache_ptr->image_ctl.generate_image);
assert(cache_ptr->num_entries_in_image > 0);
assert(cache_ptr->index_len == 0);
assert(cache_ptr->image_data_len > 0);
assert(cache_ptr->image_data_len <= cache_ptr->image_len);
/* Allocate the buffer in which to construct the cache image block */
if (NULL == (cache_ptr->image_buffer = H5MM_malloc(cache_ptr->image_len + 1)))
HGOTO_ERROR(H5E_CACHE, H5E_CANTALLOC, FAIL, "memory allocation failed for cache image buffer");
/* Construct the cache image block header image */
p = (uint8_t *)cache_ptr->image_buffer;
if (H5C__encode_cache_image_header(f, cache_ptr, &p) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTENCODE, FAIL, "header image construction failed");
assert((size_t)(p - (uint8_t *)cache_ptr->image_buffer) < cache_ptr->image_data_len);
/* Construct the cache entry images */
for (u = 0; u < cache_ptr->num_entries_in_image; u++)
if (H5C__encode_cache_image_entry(f, cache_ptr, &p, u) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTENCODE, FAIL, "entry image construction failed");
assert((size_t)(p - (uint8_t *)cache_ptr->image_buffer) < cache_ptr->image_data_len);
/* Construct the adaptive resize status image -- not yet */
/* Compute the checksum and encode */
chksum = H5_checksum_metadata(cache_ptr->image_buffer,
(size_t)(cache_ptr->image_data_len - H5F_SIZEOF_CHKSUM), 0);
UINT32ENCODE(p, chksum);
assert((size_t)(p - (uint8_t *)cache_ptr->image_buffer) == cache_ptr->image_data_len);
assert((size_t)(p - (uint8_t *)cache_ptr->image_buffer) <= cache_ptr->image_len);
#ifndef NDEBUG
/* validate the metadata cache image we just constructed by decoding it
* and comparing the result with the original data.
*/
{
uint32_t old_chksum;
const uint8_t *q;
H5C_t *fake_cache_ptr = NULL;
unsigned v;
herr_t status; /* Status from decoding */
fake_cache_ptr = (H5C_t *)H5MM_malloc(sizeof(H5C_t));
assert(fake_cache_ptr);
/* needed for sanity checks */
fake_cache_ptr->image_len = cache_ptr->image_len;
q = (const uint8_t *)cache_ptr->image_buffer;
status = H5C__decode_cache_image_header(f, fake_cache_ptr, &q);
assert(status >= 0);
assert(NULL != p);
assert(fake_cache_ptr->num_entries_in_image == cache_ptr->num_entries_in_image);
fake_cache_ptr->image_entries = (H5C_image_entry_t *)H5MM_malloc(
sizeof(H5C_image_entry_t) * (size_t)(fake_cache_ptr->num_entries_in_image + 1));
assert(fake_cache_ptr->image_entries);
for (u = 0; u < fake_cache_ptr->num_entries_in_image; u++) {
fake_cache_ptr->image_entries[u].image_ptr = NULL;
/* touch up f->shared->cache to satisfy sanity checks... */
f->shared->cache = fake_cache_ptr;
status = H5C__decode_cache_image_entry(f, fake_cache_ptr, &q, u);
assert(status >= 0);
/* ...and then return f->shared->cache to its correct value */
f->shared->cache = cache_ptr;
/* verify expected contents */
assert(cache_ptr->image_entries[u].addr == fake_cache_ptr->image_entries[u].addr);
assert(cache_ptr->image_entries[u].size == fake_cache_ptr->image_entries[u].size);
assert(cache_ptr->image_entries[u].type_id == fake_cache_ptr->image_entries[u].type_id);
assert(cache_ptr->image_entries[u].lru_rank == fake_cache_ptr->image_entries[u].lru_rank);
assert(cache_ptr->image_entries[u].is_dirty == fake_cache_ptr->image_entries[u].is_dirty);
/* don't check image_fd_height as it is not stored in
* the metadata cache image block.
*/
assert(cache_ptr->image_entries[u].fd_child_count ==
fake_cache_ptr->image_entries[u].fd_child_count);
assert(cache_ptr->image_entries[u].fd_dirty_child_count ==
fake_cache_ptr->image_entries[u].fd_dirty_child_count);
assert(cache_ptr->image_entries[u].fd_parent_count ==
fake_cache_ptr->image_entries[u].fd_parent_count);
for (v = 0; v < cache_ptr->image_entries[u].fd_parent_count; v++)
assert(cache_ptr->image_entries[u].fd_parent_addrs[v] ==
fake_cache_ptr->image_entries[u].fd_parent_addrs[v]);
/* free the fd_parent_addrs array if it exists */
if (fake_cache_ptr->image_entries[u].fd_parent_addrs) {
assert(fake_cache_ptr->image_entries[u].fd_parent_count > 0);
fake_cache_ptr->image_entries[u].fd_parent_addrs =
(haddr_t *)H5MM_xfree(fake_cache_ptr->image_entries[u].fd_parent_addrs);
fake_cache_ptr->image_entries[u].fd_parent_count = 0;
} /* end if */
else
assert(fake_cache_ptr->image_entries[u].fd_parent_count == 0);
assert(cache_ptr->image_entries[u].image_ptr);
assert(fake_cache_ptr->image_entries[u].image_ptr);
assert(!memcmp(cache_ptr->image_entries[u].image_ptr, fake_cache_ptr->image_entries[u].image_ptr,
cache_ptr->image_entries[u].size));
fake_cache_ptr->image_entries[u].image_ptr =
H5MM_xfree(fake_cache_ptr->image_entries[u].image_ptr);
} /* end for */
assert((size_t)(q - (const uint8_t *)cache_ptr->image_buffer) ==
cache_ptr->image_data_len - H5F_SIZEOF_CHKSUM);
/* compute the checksum */
old_chksum = chksum;
chksum = H5_checksum_metadata(cache_ptr->image_buffer,
(size_t)(cache_ptr->image_data_len - H5F_SIZEOF_CHKSUM), 0);
assert(chksum == old_chksum);
fake_cache_ptr->image_entries = (H5C_image_entry_t *)H5MM_xfree(fake_cache_ptr->image_entries);
fake_cache_ptr = (H5C_t *)H5MM_xfree(fake_cache_ptr);
} /* end block */
#endif
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C__construct_cache_image_buffer() */
/*-------------------------------------------------------------------------
* Function: H5C__generate_cache_image()
*
* Purpose: Generate the cache image and write it to the file, if
* directed.
*
* Return: SUCCEED on success, and FAIL on failure.
*
*-------------------------------------------------------------------------
*/
herr_t
H5C__generate_cache_image(H5F_t *f, H5C_t *cache_ptr)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity checks */
assert(f);
assert(f->shared);
assert(cache_ptr == f->shared->cache);
assert(cache_ptr);
/* Construct cache image */
if (H5C__construct_cache_image_buffer(f, cache_ptr) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "Can't create metadata cache image");
/* Free image entries array */
if (H5C__free_image_entries_array(cache_ptr) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "Can't free image entries array");
/* Write cache image block if so configured */
if (cache_ptr->image_ctl.flags & H5C_CI__GEN_MDC_IMAGE_BLK) {
if (H5C__write_cache_image(f, cache_ptr) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "Can't write metadata cache image block to file");
H5C__UPDATE_STATS_FOR_CACHE_IMAGE_CREATE(cache_ptr);
} /* end if */
/* Free cache image buffer */
assert(cache_ptr->image_buffer);
cache_ptr->image_buffer = H5MM_xfree(cache_ptr->image_buffer);
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C__generate_cache_image() */
/*-------------------------------------------------------------------------
* Function: H5C__free_image_entries_array
*
* Purpose: If the image entries array exists, free the image
* associated with each entry, and then free the image
* entries array proper.
*
* Note that by the time this function is called, the cache
* should have removed all entries from its data structures.
*
* Return: SUCCEED on success, and FAIL on failure.
*
*-------------------------------------------------------------------------
*/
static herr_t
H5C__free_image_entries_array(H5C_t *cache_ptr)
{
FUNC_ENTER_PACKAGE_NOERR
/* Sanity checks */
assert(cache_ptr);
assert(cache_ptr->close_warning_received);
assert(cache_ptr->image_ctl.generate_image);
assert(cache_ptr->index_len == 0);
/* Check for entries to free */
if (cache_ptr->image_entries != NULL) {
unsigned u; /* Local index variable */
for (u = 0; u < cache_ptr->num_entries_in_image; u++) {
H5C_image_entry_t *ie_ptr; /* Image entry to release */
/* Get pointer to image entry */
ie_ptr = &(cache_ptr->image_entries[u]);
/* Sanity checks */
assert(ie_ptr);
assert(ie_ptr->image_ptr);
/* Free the parent addrs array if appropriate */
if (ie_ptr->fd_parent_addrs) {
assert(ie_ptr->fd_parent_count > 0);
ie_ptr->fd_parent_addrs = (haddr_t *)H5MM_xfree(ie_ptr->fd_parent_addrs);
} /* end if */
else
assert(ie_ptr->fd_parent_count == 0);
/* Free the image */
ie_ptr->image_ptr = H5MM_xfree(ie_ptr->image_ptr);
} /* end for */
/* Free the image entries array */
cache_ptr->image_entries = (H5C_image_entry_t *)H5MM_xfree(cache_ptr->image_entries);
} /* end if */
FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5C__free_image_entries_array() */
/*-------------------------------------------------------------------------
* Function: H5C__get_cache_image_config
*
* Purpose: Copy the current configuration for cache image generation
* on file close into the instance of H5C_cache_image_ctl_t
* pointed to by config_ptr.
*
* Return: SUCCEED on success, and FAIL on failure.
*
*-------------------------------------------------------------------------
*/
herr_t
H5C__get_cache_image_config(const H5C_t *cache_ptr, H5C_cache_image_ctl_t *config_ptr)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
if (cache_ptr == NULL)
HGOTO_ERROR(H5E_CACHE, H5E_BADVALUE, FAIL, "Bad cache_ptr on entry");
if (config_ptr == NULL)
HGOTO_ERROR(H5E_CACHE, H5E_BADVALUE, FAIL, "Bad config_ptr on entry");
*config_ptr = cache_ptr->image_ctl;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C__get_cache_image_config() */
/*-------------------------------------------------------------------------
* Function: H5C__read_cache_image
*
* Purpose: Load the metadata cache image from the specified location
* in the file, and return it in the supplied buffer.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5C__read_cache_image(H5F_t *f, H5C_t *cache_ptr)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity checks */
assert(f);
assert(cache_ptr);
assert(H5_addr_defined(cache_ptr->image_addr));
assert(cache_ptr->image_len > 0);
assert(cache_ptr->image_buffer);
#ifdef H5_HAVE_PARALLEL
{
H5AC_aux_t *aux_ptr = (H5AC_aux_t *)cache_ptr->aux_ptr;
int mpi_result;
if (NULL == aux_ptr || aux_ptr->mpi_rank == 0) {
#endif /* H5_HAVE_PARALLEL */
/* Read the buffer (if serial access, or rank 0 of parallel access) */
/* NOTE: if this block read is being performed on rank 0 only, throwing
* an error here will cause other ranks to hang in the following MPI_Bcast.
*/
if (H5F_block_read(f, H5FD_MEM_SUPER, cache_ptr->image_addr, cache_ptr->image_len,
cache_ptr->image_buffer) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_READERROR, FAIL, "Can't read metadata cache image block");
H5C__UPDATE_STATS_FOR_CACHE_IMAGE_READ(cache_ptr);
#ifdef H5_HAVE_PARALLEL
if (aux_ptr) {
/* Broadcast cache image */
if (MPI_SUCCESS != (mpi_result = MPI_Bcast(cache_ptr->image_buffer, (int)cache_ptr->image_len,
MPI_BYTE, 0, aux_ptr->mpi_comm)))
HMPI_GOTO_ERROR(FAIL, "MPI_Bcast failed", mpi_result)
} /* end if */
} /* end if */
else if (aux_ptr) {
/* Retrieve the contents of the metadata cache image from process 0 */
if (MPI_SUCCESS != (mpi_result = MPI_Bcast(cache_ptr->image_buffer, (int)cache_ptr->image_len,
MPI_BYTE, 0, aux_ptr->mpi_comm)))
HMPI_GOTO_ERROR(FAIL, "can't receive cache image MPI_Bcast", mpi_result)
} /* end else-if */
} /* end block */
#endif /* H5_HAVE_PARALLEL */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C__read_cache_image() */
/*-------------------------------------------------------------------------
* Function: H5C__load_cache_image
*
* Purpose: Read the cache image superblock extension message and
* delete it if so directed.
*
* Then load the cache image block at the specified location,
* decode it, and insert its contents into the metadata
* cache.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5C__load_cache_image(H5F_t *f)
{
H5C_t *cache_ptr;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity checks */
assert(f);
assert(f->shared);
cache_ptr = f->shared->cache;
assert(cache_ptr);
/* If the image address is defined, load the image, decode it,
* and insert its contents into the metadata cache.
*
* Note that under normal operating conditions, it is an error if the
* image address is HADDR_UNDEF. However, to facilitate testing,
* we allow this special value of the image address which means that
* no image exists, and that the load operation should be skipped
* silently.
*/
if (H5_addr_defined(cache_ptr->image_addr)) {
/* Sanity checks */
assert(cache_ptr->image_len > 0);
assert(cache_ptr->image_buffer == NULL);
/* Allocate space for the image */
if (NULL == (cache_ptr->image_buffer = H5MM_malloc(cache_ptr->image_len + 1)))
HGOTO_ERROR(H5E_CACHE, H5E_CANTALLOC, FAIL, "memory allocation failed for cache image buffer");
/* Load the image from file */
if (H5C__read_cache_image(f, cache_ptr) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_READERROR, FAIL, "Can't read metadata cache image block");
/* Reconstruct cache contents, from image */
if (H5C__reconstruct_cache_contents(f, cache_ptr) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTDECODE, FAIL, "Can't reconstruct cache contents from image block");
/* Free the image buffer */
cache_ptr->image_buffer = H5MM_xfree(cache_ptr->image_buffer);
/* Update stats -- must do this now, as we are about
* to discard the size of the cache image.
*/
H5C__UPDATE_STATS_FOR_CACHE_IMAGE_LOAD(cache_ptr);
cache_ptr->image_loaded = true;
} /* end if */
/* If directed, free the on disk metadata cache image */
if (cache_ptr->delete_image) {
if (H5F__super_ext_remove_msg(f, H5O_MDCI_MSG_ID) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTREMOVE, FAIL,
"can't remove metadata cache image message from superblock extension");
/* Reset image block values */
cache_ptr->image_len = 0;
cache_ptr->image_data_len = 0;
cache_ptr->image_addr = HADDR_UNDEF;
} /* end if */
done:
if (ret_value < 0) {
if (H5_addr_defined(cache_ptr->image_addr))
cache_ptr->image_buffer = H5MM_xfree(cache_ptr->image_buffer);
}
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C__load_cache_image() */
/*-------------------------------------------------------------------------
* Function: H5C_load_cache_image_on_next_protect()
*
* Purpose: Note the fact that a metadata cache image superblock
* extension message exists, along with the base address
* and length of the metadata cache image block.
*
* Once this notification is received the metadata cache
* image block must be read, decoded, and loaded into the
* cache on the next call to H5C_protect().
*
* Further, if the file is opened R/W, the metadata cache
* image superblock extension message must be deleted from
* the superblock extension and the image block freed
*
* Contrawise, if the file is opened R/O, the metadata
* cache image superblock extension message and image block
* must be left as is. Further, any dirty entries in the
* cache image block must be marked as clean to avoid
* attempts to write them on file close.
*
* Return: SUCCEED
*
*-------------------------------------------------------------------------
*/
herr_t
H5C_load_cache_image_on_next_protect(H5F_t *f, haddr_t addr, hsize_t len, bool rw)
{
H5C_t *cache_ptr;
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* Sanity checks */
assert(f);
assert(f->shared);
cache_ptr = f->shared->cache;
assert(cache_ptr);
/* Set information needed to load cache image */
cache_ptr->image_addr = addr;
cache_ptr->image_len = len;
cache_ptr->load_image = true;
cache_ptr->delete_image = rw;
FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5C_load_cache_image_on_next_protect() */
/*-------------------------------------------------------------------------
* Function: H5C__image_entry_cmp
*
* Purpose: Comparison callback for qsort(3) on image entries.
* Entries are sorted first by flush dependency height,
* and then by LRU rank.
*
* Note: Entries with a _greater_ flush dependency height should
* be sorted earlier than entries with lower heights, since
* leafs in the flush dependency graph are at height 0, and their
* parents need to be earlier in the image, so that they can
* construct their flush dependencies when decoded.
*
* Return: An integer less than, equal to, or greater than zero if the
* first entry is considered to be respectively less than,
* equal to, or greater than the second.
*
*-------------------------------------------------------------------------
*/
static int
H5C__image_entry_cmp(const void *_entry1, const void *_entry2)
{
const H5C_image_entry_t *entry1 =
(const H5C_image_entry_t *)_entry1; /* Pointer to first image entry to compare */
const H5C_image_entry_t *entry2 =
(const H5C_image_entry_t *)_entry2; /* Pointer to second image entry to compare */
int ret_value = 0; /* Return value */
FUNC_ENTER_PACKAGE_NOERR
/* Sanity checks */
assert(entry1);
assert(entry2);
if (entry1->image_fd_height > entry2->image_fd_height)
ret_value = -1;
else if (entry1->image_fd_height < entry2->image_fd_height)
ret_value = 1;
else {
/* Sanity check */
assert(entry1->lru_rank >= -1);
assert(entry2->lru_rank >= -1);
if (entry1->lru_rank < entry2->lru_rank)
ret_value = -1;
else if (entry1->lru_rank > entry2->lru_rank)
ret_value = 1;
} /* end else */
FUNC_LEAVE_NOAPI(ret_value)
} /* H5C__image_entry_cmp() */
/*-------------------------------------------------------------------------
* Function: H5C__prep_image_for_file_close
*
* Purpose: The objective of the call is to allow the metadata cache
* to do any preparatory work prior to generation of a
* cache image.
*
* In particular, the cache must
*
* 1) serialize all its entries,
*
* 2) compute the size of the metadata cache image,
*
* 3) allocate space for the metadata cache image, and
*
* 4) setup the metadata cache image superblock extension
* message with the address and size of the metadata
* cache image.
*
* The parallel case is complicated by the fact that
* while all metadata caches must contain the same set of
* dirty entries, there is no such requirement for clean
* entries or the order that entries appear in the LRU.
*
* Thus, there is no requirement that different processes
* will construct cache images of the same size.
*
* This is not a major issue as long as all processes include
* the same set of dirty entries in the cache -- as they
* currently do (note that this will change when we implement
* the ageout feature). Since only the process zero cache
* writes the cache image, all that is necessary is to
* broadcast the process zero cache size for use in the
* superblock extension messages and cache image block
* allocations.
*
* Note: At present, cache image is disabled in the
* parallel case as the new collective metadata write
* code must be modified to support cache image.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5C__prep_image_for_file_close(H5F_t *f, bool *image_generated)
{
H5C_t *cache_ptr = NULL;
haddr_t eoa_frag_addr = HADDR_UNDEF;
hsize_t eoa_frag_size = 0;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity checks */
assert(f);
assert(f->shared);
assert(f->shared->cache);
cache_ptr = f->shared->cache;
assert(cache_ptr);
assert(image_generated);
/* If the file is opened and closed without any access to
* any group or data set, it is possible that the cache image (if
* it exists) has not been read yet. Do this now if required.
*/
if (cache_ptr->load_image) {
cache_ptr->load_image = false;
if (H5C__load_cache_image(f) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_CANTLOAD, FAIL, "can't load cache image");
} /* end if */
/* Before we start to generate the cache image (if requested), verify
* that the superblock supports superblock extension messages, and
* silently cancel any request for a cache image if it does not.
*
* Ideally, we would do this when the cache image is requested,
* but the necessary information is not necessary available at that
* time -- hence this last minute check.
*
* Note that under some error conditions, the superblock will be
* undefined in this case as well -- if so, assume that the
* superblock does not support superblock extension messages.
* Also verify that the file's high_bound is at least release
* 1.10.x, otherwise cancel the request for a cache image
*/
if ((NULL == f->shared->sblock) || (f->shared->sblock->super_vers < HDF5_SUPERBLOCK_VERSION_2) ||
(f->shared->high_bound < H5F_LIBVER_V110)) {
H5C_cache_image_ctl_t default_image_ctl = H5C__DEFAULT_CACHE_IMAGE_CTL;
cache_ptr->image_ctl = default_image_ctl;
assert(!(cache_ptr->image_ctl.generate_image));
} /* end if */
/* Generate the cache image, if requested */
if (cache_ptr->image_ctl.generate_image) {
/* Create the cache image super block extension message.
*
* Note that the base address and length of the metadata cache
* image are undefined at this point, and thus will have to be
* updated later.
*
* Create the super block extension message now so that space
* is allocated for it (if necessary) before we allocate space
* for the cache image block.
*
* To simplify testing, do this only if the
* H5C_CI__GEN_MDCI_SBE_MESG bit is set in
* cache_ptr->image_ctl.flags.
*/
if (cache_ptr->image_ctl.flags & H5C_CI__GEN_MDCI_SBE_MESG)
if (H5C__write_cache_image_superblock_msg(f, true) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "creation of cache image SB mesg failed.");
/* Serialize the cache */
if (H5C__serialize_cache(f) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "serialization of the cache failed");
/* Scan the cache and record data needed to construct the
* cache image. In particular, for each entry we must record:
*
* 1) rank in LRU (if entry is in LRU)
*
* 2) Whether the entry is dirty prior to flush of
* cache just prior to close.
*
* 3) Addresses of flush dependency parents (if any).
*
* 4) Number of flush dependency children (if any).
*
* In passing, also compute the size of the metadata cache
* image. With the recent modifications of the free space
* manager code, this size should be correct.
*/
if (H5C__prep_for_file_close__scan_entries(f, cache_ptr) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "H5C__prep_for_file_close__scan_entries failed");
assert(HADDR_UNDEF == cache_ptr->image_addr);
#ifdef H5_HAVE_PARALLEL
/* In the parallel case, overwrite the image_len with the
* value computed by process 0.
*/
if (cache_ptr->aux_ptr) { /* we have multiple processes */
int mpi_result;
unsigned p0_image_len;
H5AC_aux_t *aux_ptr;
aux_ptr = (H5AC_aux_t *)cache_ptr->aux_ptr;
if (aux_ptr->mpi_rank == 0) {
aux_ptr->p0_image_len = (unsigned)cache_ptr->image_data_len;
p0_image_len = aux_ptr->p0_image_len;
if (MPI_SUCCESS !=
(mpi_result = MPI_Bcast(&p0_image_len, 1, MPI_UNSIGNED, 0, aux_ptr->mpi_comm)))
HMPI_GOTO_ERROR(FAIL, "MPI_Bcast failed", mpi_result)
assert(p0_image_len == aux_ptr->p0_image_len);
} /* end if */
else {
if (MPI_SUCCESS !=
(mpi_result = MPI_Bcast(&p0_image_len, 1, MPI_UNSIGNED, 0, aux_ptr->mpi_comm)))
HMPI_GOTO_ERROR(FAIL, "MPI_Bcast failed", mpi_result)
aux_ptr->p0_image_len = p0_image_len;
} /* end else */
/* Allocate space for a cache image of size equal to that
* computed by the process 0. This may be different from
* cache_ptr->image_data_len if mpi_rank != 0. However, since
* cache image write is suppressed on all processes other than
* process 0, this doesn't matter.
*
* Note that we allocate the cache image directly from the file
* driver so as to avoid unsettling the free space managers.
*/
if (HADDR_UNDEF ==
(cache_ptr->image_addr = H5FD_alloc(f->shared->lf, H5FD_MEM_SUPER, f, (hsize_t)p0_image_len,
&eoa_frag_addr, &eoa_frag_size)))
HGOTO_ERROR(H5E_CACHE, H5E_NOSPACE, FAIL,
"can't allocate file space for metadata cache image");
} /* end if */
else
#endif /* H5_HAVE_PARALLEL */
/* Allocate the cache image block. Note that we allocate this
* this space directly from the file driver so as to avoid
* unsettling the free space managers.
*/
if (HADDR_UNDEF == (cache_ptr->image_addr = H5FD_alloc(f->shared->lf, H5FD_MEM_SUPER, f,
(hsize_t)(cache_ptr->image_data_len),
&eoa_frag_addr, &eoa_frag_size)))
HGOTO_ERROR(H5E_CACHE, H5E_NOSPACE, FAIL,
"can't allocate file space for metadata cache image");
/* Make note of the eoa after allocation of the cache image
* block. This value is used for sanity checking when we
* shutdown the self referential free space managers after
* we destroy the metadata cache.
*/
assert(HADDR_UNDEF == f->shared->eoa_post_mdci_fsalloc);
if (HADDR_UNDEF == (f->shared->eoa_post_mdci_fsalloc = H5FD_get_eoa(f->shared->lf, H5FD_MEM_DEFAULT)))
HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get file size");
/* For now, drop any fragment left over from the allocation of the
* image block on the ground. A fragment should only be returned
* if the underlying file alignment is greater than 1.
*
* Clean this up eventually by extending the size of the cache
* image block to the next alignment boundary, and then setting
* the image_data_len to the actual size of the cache_image.
*
* On the off chance that there is some other way to get a
* a fragment on a cache image allocation, leave the following
* assertion in the code so we will find out.
*/
assert((eoa_frag_size == 0) || (f->shared->alignment != 1));
/* Eventually it will be possible for the length of the cache image
* block on file to be greater than the size of the data it
* contains. However, for now they must be the same. Set
* cache_ptr->image_len accordingly.
*/
cache_ptr->image_len = cache_ptr->image_data_len;
/* update the metadata cache image superblock extension
* message with the new cache image block base address and
* length.
*
* to simplify testing, do this only if the
* H5C_CI__GEN_MDC_IMAGE_BLK bit is set in
* cache_ptr->image_ctl.flags.
*/
if (cache_ptr->image_ctl.flags & H5C_CI__GEN_MDC_IMAGE_BLK)
if (H5C__write_cache_image_superblock_msg(f, false) < 0)
HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "update of cache image SB mesg failed");
/* At this point:
*
* 1) space in the file for the metadata cache image
* is allocated,
*