-
Notifications
You must be signed in to change notification settings - Fork 7
/
H5VL_log_filei.cpp
1097 lines (938 loc) · 32.8 KB
/
H5VL_log_filei.cpp
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 (C) 2022, Northwestern University and Argonne National Laboratory
* See COPYRIGHT notice in top-level directory.
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
// Std hdrs
#include <libgen.h>
#include <array>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
// Sys hdrs
#include <dirent.h>
#include <mpi.h>
#include <sys/errno.h> /* errno */
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
// Logvol hdrs
#include "H5VL_log.h"
#include "H5VL_log_dataset.hpp"
#include "H5VL_log_file.hpp"
#include "H5VL_log_filei.hpp"
#include "H5VL_logi.hpp"
#include "H5VL_logi_util.hpp"
#include "H5VL_logi_wrapper.hpp"
#include "H5VL_logi_zip.hpp"
//#define DEFAULT_SIZE 1073741824 // 1 GiB
#define DEFAULT_SIZE 209715200 // 200 MiB
//#define DEFAULT_SIZE 10485760 // 10 MiB
#define CHECK_LOG_INTERNAL_EXIST(EXISTS) \
do { \
if (EXISTS == 0) { \
fp->is_log_based_file = false; \
return; \
} \
} while (0)
std::map<decltype (stcrtstat::st_ino), H5VL_log_file_t *> files;
H5VL_log_file_t *H5VL_log_filei_search (const char *path) {
int err;
struct stat s;
err = stat (path, &s);
if (err == 0) {
auto f = files.find (s.st_ino);
if (f != files.end ()) return f->second;
}
return NULL;
}
void H5VL_log_filei_register (H5VL_log_file_t *fp) {
int err;
struct stat s;
err = stat (fp->name.c_str (), &s);
if (err == 0) {
fp->ino = s.st_ino;
fp->has_ino = true;
files[s.st_ino] = fp;
} else {
fp->has_ino = false;
if (!fp->rank) {
std::cout << "Warning: inode info not found, can't detect duplicate file open"
<< std::endl;
}
}
}
void H5VL_log_filei_rm (H5VL_log_file_t *fp) {
if (fp->has_ino) { files.erase (fp->ino); }
}
void H5VL_log_filei_balloc (H5VL_log_file_t *fp, size_t size, void **buf) {
size_t *bp;
// printf("Balloc %llu\n", size);
if (fp->bsize != LOG_VOL_BSIZE_UNLIMITED) {
if (fp->bused + size > (size_t)(fp->bsize)) {
*buf = NULL;
ERR_OUT ("Out of buffer")
}
}
// The first sizeof (size_t) bytes are used to store the size of the buffer allocated so we can
// keep track of the total amount of buffer allocated
bp = (size_t *)malloc (size + sizeof (size_t));
if (bp == NULL) {
*buf = NULL;
ERR_OUT ("OOM")
}
*bp = size;
*buf = bp + 1;
fp->bused += size;
}
void H5VL_log_filei_post_open (H5VL_log_file_t *fp) {
herr_t err = 0;
int mpierr;
H5VL_loc_params_t loc;
H5VL_object_specific_args_t args;
hbool_t exists;
int attbuf[H5VL_LOG_FILEI_NATTR];
void *lib_state = NULL;
H5VL_logi_err_finally finally (
[&lib_state] () -> void { H5VL_logi_restore_lib_stat (lib_state); });
H5VL_LOGI_PROFILING_TIMER_START;
// check for exisitence of __int_att;
// if inexists, mark as regular file and return directly.
exists = H5VL_logi_exists_att (fp, H5VL_LOG_FILEI_ATTR, fp->dxplid);
CHECK_LOG_INTERNAL_EXIST (exists);
// Att
H5VL_logi_get_att (fp, H5VL_LOG_FILEI_ATTR, H5T_NATIVE_INT32, attbuf, fp->dxplid);
fp->ndset = attbuf[0];
fp->nldset = attbuf[1];
fp->nmdset = attbuf[2];
fp->config = attbuf[3];
fp->ngroup = attbuf[3];
fp->mreqs.resize (fp->ndset, NULL); // Merge write reqeusts
fp->dsets_info.resize (fp->ndset, NULL); // Dataset info
fp->group_rank = fp->rank;
fp->group_comm = fp->comm;
fp->group_id = 0;
H5VL_log_filei_init_idx (fp);
fp->idx->reserve (fp->ndset);
H5VL_LOGI_PROFILING_TIMER_START;
if (fp->config & H5VL_FILEI_CONFIG_SUBFILING) {
H5VL_log_filei_calc_node_rank (fp);
} else {
fp->group_rank = fp->rank;
fp->group_np = fp->np;
fp->group_comm = fp->comm;
fp->group_id = 0;
fp->ngroup = 1;
}
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_OPEN_GROUP_RANK);
H5VL_LOGI_PROFILING_TIMER_START;
if (fp->config & H5VL_FILEI_CONFIG_SUBFILING) {
// Aligned write not supported in subfiles
fp->config &= ~H5VL_FILEI_CONFIG_DATA_ALIGN;
H5VL_log_filei_open_subfile (fp, fp->flag, fp->ufaplid, fp->dxplid);
} else {
fp->sfp = fp->uo;
fp->subname = std::string (fp->name);
}
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_OPEN_SUBFILE);
// Reset hdf5 context to allow group operations within a file operation
H5VL_logi_reset_lib_stat (lib_state);
// Open the LOG group
loc.obj_type = H5I_FILE;
loc.type = H5VL_OBJECT_BY_SELF;
H5VL_LOGI_PROFILING_TIMER_START
fp->lgp = H5VLgroup_open (fp->sfp, &loc, fp->uvlid, H5VL_LOG_FILEI_GROUP_LOG,
H5P_GROUP_ACCESS_DEFAULT, fp->dxplid, NULL);
CHECK_PTR (fp->lgp)
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VLGROUP_OPEN);
// Open the file with MPI
H5VL_LOGI_PROFILING_TIMER_START;
mpierr =
MPI_File_open (fp->group_comm, fp->subname.c_str (), MPI_MODE_RDWR, fp->info, &(fp->fh));
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE_FH);
CHECK_MPIERR
// Visit all dataasets for info
args.op_type = H5VL_OBJECT_VISIT;
args.args.visit.idx_type = H5_INDEX_CRT_ORDER;
args.args.visit.order = H5_ITER_INC;
args.args.visit.op = H5VL_log_filei_dset_visit;
args.args.visit.op_data = NULL;
args.args.visit.fields = H5O_INFO_ALL;
err = H5VLobject_specific (fp->uo, &loc, fp->uvlid, &args, fp->dxplid, NULL);
CHECK_ERR
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_OPEN);
}
void H5VL_log_filei_post_create (H5VL_log_file_t *fp) {
int mpierr;
H5VL_loc_params_t loc;
int attbuf[H5VL_LOG_FILEI_NATTR];
void *lib_state = NULL;
H5VL_logi_err_finally finally (
[&lib_state] () -> void { H5VL_logi_restore_lib_stat (lib_state); });
H5VL_LOGI_PROFILING_TIMER_START;
// Reset hdf5 context to allow group and attr operations within a file operation
H5VL_logi_reset_lib_stat (lib_state);
// Figure out lustre configuration
H5VL_LOGI_PROFILING_TIMER_START;
if (fp->config & H5VL_FILEI_CONFIG_DATA_ALIGN) {
H5VL_log_filei_parse_strip_info (fp);
// Dummy stripe setting for debugging without lustre
// fp->scount=2;
// fp->ssize=8388608;
if ((fp->scount <= 0) || (fp->ssize <= 0)) {
fp->config &= ~H5VL_FILEI_CONFIG_DATA_ALIGN;
if (fp->rank == 0) {
printf (
"Warning: Cannot retrive stripping info, disable aligned data layout\n");
}
}
}
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE_STRIPE);
H5VL_LOGI_PROFILING_TIMER_START;
if ((fp->config & H5VL_FILEI_CONFIG_DATA_ALIGN) ||
(fp->config & H5VL_FILEI_CONFIG_SUBFILING)) {
H5VL_log_filei_calc_node_rank (fp);
} else {
fp->group_rank = fp->rank;
fp->group_np = fp->np;
fp->group_comm = fp->comm;
fp->group_id = 0;
fp->ngroup = 1;
}
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE_GROUP_RANK);
H5VL_LOGI_PROFILING_TIMER_START;
if (fp->config & H5VL_FILEI_CONFIG_SUBFILING) {
// Aligned write not supported in subfiles
fp->config &= ~H5VL_FILEI_CONFIG_DATA_ALIGN;
H5VL_log_filei_create_subfile (fp, fp->flag, fp->ufaplid, fp->dxplid);
} else {
fp->sfp = fp->uo;
fp->subname = fp->name;
}
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE_SUBFILE);
// Create the LOG group
H5VL_LOGI_PROFILING_TIMER_START;
loc.obj_type = H5I_FILE;
loc.type = H5VL_OBJECT_BY_SELF;
fp->lgp = H5VLgroup_create (fp->sfp, &loc, fp->uvlid, H5VL_LOG_FILEI_GROUP_LOG,
H5P_LINK_CREATE_DEFAULT, H5P_GROUP_CREATE_DEFAULT,
H5P_GROUP_CREATE_DEFAULT, fp->dxplid, NULL);
CHECK_PTR (fp->lgp)
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE_GROUP);
if (fp->config & H5VL_FILEI_CONFIG_DATA_ALIGN) {
fp->fd = open (fp->name.c_str(), O_RDWR);
if (fp->fd < 0) { ERR_OUT ("open fail") }
} else {
fp->fd = -1;
}
// Open the file with MPI
H5VL_LOGI_PROFILING_TIMER_START;
mpierr =
MPI_File_open (fp->group_comm, fp->subname.c_str (), MPI_MODE_RDWR, fp->info, &(fp->fh));
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE_FH);
CHECK_MPIERR
// Att
attbuf[0] = fp->ndset;
attbuf[1] = fp->nldset;
attbuf[2] = fp->nmdset;
attbuf[3] = fp->config;
attbuf[4] = fp->ngroup;
H5VL_logi_add_att (fp, H5VL_LOG_FILEI_ATTR, H5T_STD_I32LE, H5T_NATIVE_INT32,
H5VL_LOG_FILEI_NATTR, attbuf, fp->dxplid, NULL);
H5VL_log_filei_register (fp);
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CREATE);
}
herr_t H5VL_log_filei_dset_visit (hid_t o_id,
const char *name,
const H5O_info_t *object_info,
void *op_data) {
herr_t err = 0;
hid_t did = -1; // Target dataset ID
// Skip unnamed and hidden object
if ((name == NULL) || (name[0] == '_' && name[1] != '_') ||
(name[0] == '/' || (name[0] == '.'))) {
goto err_out;
}
if (name[0] == '_') name++;
try {
// Open the dataset so that the dset metadata is cached in the file
if (object_info->type == H5O_TYPE_DATASET) {
did = H5Dopen2 (o_id, name, H5P_DEFAULT);
CHECK_ID (did)
H5Dclose (did);
}
}
H5VL_LOGI_EXP_CATCH_ERR
err_out:;
return err;
}
void H5VL_log_filei_bfree (H5VL_log_file_t *fp, void *buf) {
size_t *bp;
if (buf) {
// The first sizeof (size_t) bytes are used to store the size of the buffer allocated, so
// the real buffer to free is sizeof (size_t) bytes before buf
bp = ((size_t *)buf) - 1;
fp->bused -= bp[0];
free (bp);
return;
}
ERR_OUT ("Buffer cannot be NULL")
}
void H5VL_log_filei_parse_fapl (H5VL_log_file_t *fp, hid_t faplid) {
herr_t err = 0;
hbool_t ret;
H5VL_log_sel_encoding_t encoding;
char *env;
err = H5Pget_meta_merge (faplid, &ret);
CHECK_ERR
if (ret) { fp->config |= H5VL_FILEI_CONFIG_METADATA_MERGE; }
env = getenv ("H5VL_LOG_METADATA_MERGE");
if (env) {
if (strcmp (env, "1") == 0) {
fp->config |= H5VL_FILEI_CONFIG_METADATA_MERGE;
} else {
fp->config &= ~H5VL_FILEI_CONFIG_METADATA_MERGE;
}
}
err = H5Pget_meta_share (faplid, &ret);
CHECK_ERR
if (ret) { fp->config |= H5VL_FILEI_CONFIG_METADATA_SHARE; }
env = getenv ("H5VL_LOG_METADATA_SHARE");
if (env) {
if (strcmp (env, "1") == 0) {
fp->config |= H5VL_FILEI_CONFIG_METADATA_SHARE;
} else {
fp->config &= ~H5VL_FILEI_CONFIG_METADATA_SHARE;
}
}
err = H5Pget_meta_zip (faplid, &ret);
CHECK_ERR
if (ret) { fp->config |= H5VL_FILEI_CONFIG_SEL_DEFLATE; }
env = getenv ("H5VL_LOG_METADATA_ZIP");
if (env) {
if (strcmp (env, "1") == 0) {
fp->config |= H5VL_FILEI_CONFIG_SEL_DEFLATE;
} else {
fp->config &= ~H5VL_FILEI_CONFIG_SEL_DEFLATE;
}
}
err = H5Pget_sel_encoding (faplid, &encoding);
CHECK_ERR
if (encoding == H5VL_LOG_ENCODING_OFFSET) { fp->config |= H5VL_FILEI_CONFIG_SEL_ENCODE; }
env = getenv ("H5VL_LOG_SEL_ENCODING");
if (env) {
if (strcmp (env, "canonical") == 0) {
fp->config &= ~H5VL_FILEI_CONFIG_SEL_ENCODE;
} else {
fp->config |= H5VL_FILEI_CONFIG_SEL_ENCODE;
}
}
err = H5Pget_idx_buffer_size (faplid, &(fp->mbuf_size));
CHECK_ERR
env = getenv ("H5VL_LOG_IDX_BSIZE");
if (env) { fp->mbuf_size = (MPI_Offset)(atoll (env)); }
/*
err = H5Pget_sel_encoding (faplid, &encoding);
CHECK_ERR
if (encoding == H5VL_LOG_ENCODING_OFFSET) { fp->config |= H5VL_FILEI_CONFIG_SEL_ENCODE; }
*/
fp->index_type = list;
env = getenv ("H5VL_LOG_INDEX_TYPE");
if (env) {
if (strcmp (env, "compact") == 0) {
fp->index_type = compact;
} else {
fp->index_type = list;
}
}
err = H5Pget_single_subfile_read (faplid, &ret);
CHECK_ERR
if (ret) { fp->config |= H5VL_FILEI_CONFIG_SINGLE_SUBFILE_READ; }
env = getenv ("H5VL_LOG_SINGLE_SUBFILE_READ");
if (env) {
if (strcmp (env, "1") == 0) {
fp->config |= H5VL_FILEI_CONFIG_SINGLE_SUBFILE_READ;
} else {
fp->config &= ~H5VL_FILEI_CONFIG_SINGLE_SUBFILE_READ;
}
}
err = H5Pget_passthru (faplid, &ret);
CHECK_ERR
if (ret) { fp->config |= H5VL_FILEI_CONFIG_PASSTHRU; }
env = getenv ("H5VL_LOG_PASSTHRU");
if (env) {
if (strcmp (env, "1") == 0) {
fp->config |= H5VL_FILEI_CONFIG_PASSTHRU;
} else {
fp->config &= ~H5VL_FILEI_CONFIG_PASSTHRU;
}
}
}
void H5VL_log_filei_parse_fcpl (H5VL_log_file_t *fp, hid_t fcplid) {
herr_t err = 0;
H5VL_log_data_layout_t layout;
char *env;
err = H5Pget_data_layout (fcplid, &layout);
CHECK_ERR
if (layout == H5VL_LOG_DATA_LAYOUT_CHUNK_ALIGNED) {
fp->config |= H5VL_FILEI_CONFIG_DATA_ALIGN;
}
env = getenv ("H5VL_LOG_DATA_LAYOUT");
if (env) {
if (strcmp (env, "align") == 0) {
fp->config &= ~H5VL_FILEI_CONFIG_DATA_ALIGN;
} else {
fp->config |= H5VL_FILEI_CONFIG_DATA_ALIGN;
}
}
/* check if env H5VL_LOG_NSUBFILES is set. env has higher precedence */
env = getenv ("H5VL_LOG_NSUBFILES");
if (env) {
/* -1 is one subfile per node */
/* 0 disables subfiling */
fp->ngroup = -1;
if (strlen (env) > 0) fp->ngroup = atoi (env);
} else {
/* env is not set, check if nsubfiles is set by H5Pset_subfiling */
err = H5Pget_subfiling (fcplid, &(fp->ngroup));
CHECK_ERR
}
if (fp->ngroup != H5VL_LOG_SUBFILING_OFF) { fp->config |= H5VL_FILEI_CONFIG_SUBFILING; }
}
// Under VOL does not recognize logvol-specific properties
// Remove all logvol properties
hid_t H5VL_log_filei_get_under_plist (hid_t faplid) {
herr_t err = 0;
htri_t pexist;
hid_t ret = H5I_INVALID_HID;
H5VL_logi_err_finally finally ([&ret, err] () -> void {
if (err != 0) {
if (ret != H5I_INVALID_HID) H5Pclose (ret);
}
});
static std::string pnames[] = {
"H5VL_log_nb_buffer_size", "H5VL_log_idx_buffer_size", "H5VL_log_metadata_merge",
"H5VL_log_metadata_share", "H5VL_log_metadata_zip", "H5VL_log_sel_encoding",
"H5VL_log_data_layout", "H5VL_log_subfiling", "H5VL_log_single_subfile_read",
"H5VL_log_passthru",
};
try {
ret = H5Pcopy (faplid);
CHECK_ID (ret)
for (auto &pname : pnames) {
pexist = H5Pexist (ret, pname.c_str ());
CHECK_ID (pexist)
if (pexist) {
err = H5Premove (ret, pname.c_str ());
CHECK_ERR
}
}
}
H5VL_LOGI_EXP_CATCH_ERR
err_out:;
return ret;
}
H5VL_log_buffer_block_t *H5VL_log_filei_pool_new_block (size_t bsize) {
// herr_t err = 0;
std::unique_ptr<H5VL_log_buffer_block_t> bp;
assert (bsize > 0);
bp = std::make_unique<H5VL_log_buffer_block_t> ();
bp->cur = bp->begin = (char *)malloc (bsize);
CHECK_PTR (bp->begin);
bp->end = bp->begin + bsize;
bp->next = NULL;
return bp.release ();
}
void H5VL_log_filei_pool_alloc (H5VL_log_buffer_pool_t *p, ssize_t bsize, void **buf) {
H5VL_log_buffer_block_t *bp;
// Need to add blocks
if (p->head->cur + bsize > p->head->end) {
if (!(p->inf)) { ERR_OUT ("Out of buffer") }
if (bsize > p->bsize) {
bp = H5VL_log_filei_pool_new_block (bsize);
} else {
if (p->free_blocks) { // pull from recycle list
bp = p->free_blocks;
p->free_blocks = bp->next;
} else {
bp = H5VL_log_filei_pool_new_block ((size_t)(p->bsize));
}
}
bp->next = p->head;
p->head = bp;
}
*buf = p->head->cur;
p->head->cur += bsize;
}
void H5VL_log_filei_pool_init (H5VL_log_buffer_pool_t *p, ssize_t bsize) {
if (bsize < 0) {
p->bsize = DEFAULT_SIZE;
p->inf = 1;
} else {
p->bsize = bsize;
p->inf = 0;
}
if (p->bsize) {
p->head = H5VL_log_filei_pool_new_block ((size_t)(p->bsize));
} else {
p->head = NULL;
}
p->free_blocks = NULL;
}
void H5VL_log_filei_pool_free (H5VL_log_buffer_pool_t *p) {
H5VL_log_buffer_block_t *i, *j = NULL;
for (i = p->head->next; i; i = i->next) {
i->cur = i->begin;
j = i;
}
if (j) {
j->next = p->free_blocks;
p->free_blocks = p->head->next;
p->head->next = NULL;
}
p->head->cur = p->head->begin;
}
void H5VL_log_filei_pool_finalize (H5VL_log_buffer_pool_t *p) {
H5VL_log_buffer_block_t *i, *j;
for (i = p->head; i; i = j) {
j = i->next;
free (i->begin);
free (i);
}
p->head = NULL;
for (i = p->free_blocks; i; i = j) {
j = i->next;
free (i->begin);
free (i);
}
p->free_blocks = NULL;
p->bsize = 0;
p->inf = 0;
}
void H5VL_log_filei_contig_buffer_init (H5VL_log_contig_buffer_t *bp, size_t init_size) {
bp->begin = (char *)malloc (init_size);
CHECK_PTR (bp->begin);
bp->cur = bp->begin;
bp->end = bp->begin + init_size;
}
void H5VL_log_filei_contig_buffer_free (H5VL_log_contig_buffer_t *bp) { free (bp->begin); }
void *H5VL_log_filei_contig_buffer_alloc (H5VL_log_contig_buffer_t *bp, size_t size) {
char *tmp;
if (bp->cur + size > bp->end) {
size_t new_size = bp->end - bp->begin;
size_t used = bp->end - bp->cur;
while (used + size > new_size) new_size <<= 1;
tmp = (char *)realloc (bp->begin, new_size);
if (!tmp) return NULL;
bp->begin = tmp;
bp->cur = bp->begin + used;
bp->end = bp->begin + new_size;
}
tmp = bp->cur;
bp->cur += size;
return (void *)tmp;
}
void H5VL_log_filei_contig_buffer_alloc (H5VL_log_buffer_pool_t *p) {
H5VL_log_buffer_block_t *i, *j;
for (i = p->head; i; i = j) {
j = i->next;
free (i->begin);
free (i);
}
p->head = NULL;
for (i = p->free_blocks; i; i = j) {
j = i->next;
free (i->begin);
free (i);
}
p->free_blocks = NULL;
p->bsize = 0;
p->inf = 0;
}
void H5VL_log_filei_flush (H5VL_log_file_t *fp, hid_t dxplid) {
H5VL_LOGI_PROFILING_TIMER_START;
H5VL_log_nb_flush_write_reqs (fp);
H5VL_log_nb_flush_read_reqs (fp, fp->rreqs, dxplid);
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILEI_FLUSH);
}
static inline void print_info (MPI_Info *info_used) {
int i, nkeys;
if (*info_used == MPI_INFO_NULL) {
printf ("MPI File Info is NULL\n");
return;
}
MPI_Info_get_nkeys (*info_used, &nkeys);
printf ("MPI File Info: nkeys = %d\n", nkeys);
for (i = 0; i < nkeys; i++) {
char key[MPI_MAX_INFO_KEY], value[MPI_MAX_INFO_VAL];
int valuelen, flag;
MPI_Info_get_nthkey (*info_used, i, key);
MPI_Info_get_valuelen (*info_used, key, &valuelen, &flag);
MPI_Info_get (*info_used, key, valuelen + 1, value, &flag);
printf ("MPI File Info: [%2d] key = %25s, value = %s\n", i, key, value);
}
printf ("-----------------------------------------------------------\n");
}
void H5VL_log_filei_close (H5VL_log_file_t *fp) {
herr_t err = 0;
int mpierr;
int attbuf[5];
void *lib_state = NULL;
H5VL_logi_err_finally finally (
[&lib_state] () -> void { H5VL_logi_restore_lib_stat (lib_state); });
#ifdef LOGVOL_DEBUG
if (H5VL_logi_debug_verbose ()) { printf ("H5VL_log_filei_close(%p, ...)\n", fp); }
#endif
if (!fp->is_log_based_file) {
H5VLfile_close (fp->uo, fp->uvlid, fp->dxplid, NULL);
// Clean up
H5VL_log_filei_rm (fp);
MPI_Comm_free (&(fp->comm));
if (fp->info != MPI_INFO_NULL) { MPI_Info_free (&(fp->info)); }
H5Pclose (fp->dxplid);
H5Pclose (fp->ufaplid);
delete fp;
return;
}
H5VL_LOGI_PROFILING_TIMER_START;
// Reset hdf5 context to allow file operations within other object close operations
H5VL_logi_reset_lib_stat (lib_state);
if (fp->flag != H5F_ACC_RDONLY) {
// Flush write requests
if (fp->config & H5VL_FILEI_CONFIG_DATA_ALIGN) {
H5VL_log_nb_flush_write_reqs_align (fp, fp->dxplid);
} else {
H5VL_log_nb_flush_write_reqs (fp);
}
// Generate metadata table
H5VL_log_filei_metaflush (fp);
// Update file attr
attbuf[0] = fp->ndset;
attbuf[1] = fp->nldset;
attbuf[2] = fp->nmdset;
attbuf[3] = fp->config;
attbuf[4] = fp->ngroup;
// Att in the subfile
if (fp->sfp && fp->sfp != fp->uo) {
attbuf[3] =
fp->config & (~(H5VL_FILEI_CONFIG_SUBFILING)); // No subfiling flag in a subfile
H5VL_logi_put_att (fp->sfp, fp->uvlid, H5I_FILE, H5VL_LOG_FILEI_ATTR, H5T_NATIVE_INT32,
attbuf, fp->dxplid);
attbuf[1] = 0; // No data and metadata in the main file
attbuf[2] = 0;
attbuf[3] |= H5VL_FILEI_CONFIG_SUBFILING; // Turn subfiling flag back on
}
// Att in the main file
H5VL_logi_put_att (fp, H5VL_LOG_FILEI_ATTR, H5T_NATIVE_INT32, attbuf, fp->dxplid);
}
// Close the log group
H5VL_LOGI_PROFILING_TIMER_START
err = H5VLgroup_close (fp->lgp, fp->uvlid, fp->dxplid, NULL);
CHECK_ERR
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VLGROUP_CLOSE);
H5VL_logi_restore_lib_stat (lib_state);
#ifdef LOGVOL_PROFILING
{
MPI_Info info;
char *_env_str = getenv ("H5VL_LOG_PRINT_MPI_INFO");
if (_env_str != NULL && *_env_str != '0') {
if (fp->rank == 0) {
MPI_File_get_info (fp->fh, &info);
print_info (&info);
MPI_Info_free (&info);
}
}
}
#endif
// Close the file with MPI
mpierr = MPI_File_close (&(fp->fh));
CHECK_MPIERR
// Close the file with posix
if (fp->config & H5VL_FILEI_CONFIG_DATA_ALIGN) { close (fp->fd); }
// Free compression buffer
free (fp->zbuf);
// Free dataset info
for (auto info : fp->dsets_info) {
if (info) {
if (info->fill) { free (info->fill); }
delete info;
}
}
// Free read index
delete fp->idx;
// Close the file with under VOL
H5VL_LOGI_PROFILING_TIMER_START;
err = H5VLfile_close (fp->uo, fp->uvlid, H5P_DATASET_XFER_DEFAULT, NULL);
CHECK_ERR
if (fp->sfp && (fp->sfp != fp->uo)) {
err = H5VLfile_close (fp->sfp, fp->uvlid, H5P_DATASET_XFER_DEFAULT, NULL);
CHECK_ERR
}
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VLFILE_CLOSE);
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VL_LOG_FILE_CLOSE);
#ifdef ENABLE_PROFILING
{
char *_env_str = getenv ("H5VL_LOG_SHOW_PROFILING_INFO");
if (_env_str != NULL && *_env_str != '0') { H5VL_log_profile_print (fp); }
}
#endif
H5VL_log_filei_rm (fp);
// Clean up
if (fp->group_comm != fp->comm) { MPI_Comm_free (&(fp->group_comm)); }
MPI_Comm_free (&(fp->comm));
if (fp->info != MPI_INFO_NULL) { MPI_Info_free (&(fp->info)); }
H5Pclose (fp->dxplid);
H5Pclose (fp->ufaplid);
delete fp;
} /* end H5VL_log_file_close() */
void H5VL_log_filei_parse_strip_info (H5VL_log_file_t *fp) {
MPI_Info info = MPI_INFO_NULL;
int mpierr;
int exist;
char val[128];
H5VL_logi_err_finally finally ([&info] () -> void {
if (info != MPI_INFO_NULL) MPI_Info_free (&info);
});
mpierr = MPI_File_get_info (fp->fh, &info);
CHECK_MPIERR
fp->scount = -1;
fp->ssize = -1;
mpierr = MPI_Info_get (info, "striping_factor", 128, val, &exist);
CHECK_MPIERR
if (exist) { fp->scount = atoi (val); }
mpierr = MPI_Info_get (info, "striping_unit", 128, val, &exist);
CHECK_MPIERR
if (exist) { fp->ssize = (size_t)atoll (val); }
}
void H5VL_log_filei_create_subfile (H5VL_log_file_t *fp,
unsigned flags,
hid_t fapl_id,
hid_t dxpl_id) {
herr_t err = 0;
int attbuf[5];
int stat;
// Create subfile dir
if (fp->rank == 0) {
stat = mkdir ((fp->name + ".subfiles").c_str (), S_IRWXU);
// Skip creation if already exist
if (stat == -1 && errno == EEXIST) stat = 0;
}
MPI_Bcast (&stat, 1, MPI_INT, 0, fp->comm);
if (stat != 0) { RET_ERR ("Cannot create subfile dir") }
// Create the subfiles with underlying VOL
err = H5Pset_fapl_mpio (fapl_id, fp->group_comm, MPI_INFO_NULL);
CHECK_ERR
H5VL_LOGI_PROFILING_TIMER_START;
fp->subname = fp->name + ".subfiles/" + std::string (basename ((char *)(fp->name.c_str ()))) +
"." + std::to_string (fp->group_id);
fp->sfp = H5VLfile_create (fp->subname.c_str (), flags, H5P_FILE_CREATE_DEFAULT, fapl_id,
dxpl_id, NULL);
CHECK_PTR (fp->sfp)
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VLFILE_CREATE);
// Att
attbuf[0] = fp->ndset;
attbuf[1] = fp->nldset;
attbuf[2] = fp->nmdset;
attbuf[3] = fp->config & !(H5VL_FILEI_CONFIG_SUBFILING); // No subfiling flag in a subfile
attbuf[4] = fp->ngroup;
H5VL_logi_add_att (fp->sfp, fp->uvlid, H5I_FILE, H5VL_LOG_FILEI_ATTR, H5T_STD_I32LE,
H5T_NATIVE_INT32, H5VL_LOG_FILEI_NATTR, attbuf, dxpl_id, NULL);
}
void H5VL_log_filei_open_subfile (H5VL_log_file_t *fp,
unsigned flags,
hid_t fapl_id,
hid_t dxpl_id) {
herr_t err = 0;
int attbuf[5];
int stat;
// Open subfile dir
if (fp->rank == 0) {
DIR *dir;
dir = opendir ((fp->name + ".subfiles").c_str ());
if (dir) {
closedir (dir);
stat = 0;
} else {
stat = -1;
}
}
MPI_Bcast (&stat, 1, MPI_INT, 0, fp->comm);
if (stat != 0) { RET_ERR ("Cannot open subfile dir") }
// Create the subfiles with underlying VOL
err = H5Pset_fapl_mpio (fapl_id, fp->group_comm, MPI_INFO_NULL);
CHECK_ERR
H5VL_LOGI_PROFILING_TIMER_START;
fp->subname = fp->name + ".subfiles/" + fp->name + "." + std::to_string (fp->group_id);
fp->sfp = H5VLfile_open (fp->subname.c_str (), flags, fapl_id, dxpl_id, NULL);
CHECK_PTR (fp->sfp)
H5VL_LOGI_PROFILING_TIMER_STOP (fp, TIMER_H5VLFILE_CREATE);
// Att
H5VL_logi_get_att (fp, H5VL_LOG_FILEI_ATTR, H5T_NATIVE_INT32, attbuf, fp->dxplid);
fp->nldset = attbuf[1];
fp->nmdset = attbuf[2];
}
void H5VL_log_filei_calc_node_rank (H5VL_log_file_t *fp) {
herr_t err = 0;
int mpierr;
int i, j;
MPI_Info info = MPI_INFO_NULL;
int *group_ranks = NULL; // Rank of the group (processes sharing a subfile)
H5VL_logi_err_finally finally ([&info, &group_ranks] () -> void {
if (info != MPI_INFO_NULL) MPI_Info_free (&info);
H5VL_log_free (group_ranks);
});
group_ranks = (int *)malloc (sizeof (int) * fp->np);
CHECK_PTR (group_ranks);
/* H5VL_FILEI_CONFIG_SUBFILING has been checked before entering this
* subroutine in H5VL_log_filei_post_open(). Thus fp->ngroup is not 0.
*/
if (fp->ngroup > 0) {
mpierr =
MPI_Comm_split (fp->comm, fp->rank * fp->ngroup / fp->np, fp->rank, &(fp->group_comm));
} else { /* fp->ngroup < 0 */
mpierr = MPI_Comm_split_type (fp->comm, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL,
&(fp->group_comm));
}
CHECK_ERR
mpierr = MPI_Comm_rank (fp->group_comm, &(fp->group_rank));
CHECK_MPIERR
mpierr = MPI_Comm_size (fp->group_comm, &(fp->group_np));
CHECK_MPIERR
mpierr = MPI_Allgather (&(fp->group_rank), 1, MPI_INT, group_ranks, 1, MPI_INT, fp->comm);
CHECK_MPIERR
// Assign group ID based on the global rank of group rank 0
fp->group_id = 0;
for (i = 0; i < fp->rank; i++) {
if (group_ranks[i] == 0) { fp->group_id++; }
}
// Calculate number of groups
fp->ngroup = fp->group_id;
for (; i < fp->np; i++) {
if (group_ranks[i] == 0) { fp->ngroup++; }
}
mpierr = MPI_Bcast (&(fp->group_id), 1, MPI_INT, 0, fp->group_comm);
CHECK_MPIERR
if (fp->config & H5VL_FILEI_CONFIG_DATA_ALIGN) {
// What ost it should write to
fp->target_ost = fp->group_id % fp->scount;
// Find previous and next node sharing the ost
fp->prev_rank = -1;
fp->next_rank = -1;
j = fp->scount;
for (i = fp->rank - 1; i > -1; i--) {
if (group_ranks[i] == 0) {
j--;
if (j == 0) {
fp->prev_rank = i;
break;
}
}
}
j = fp->scount;
for (i = fp->rank + 1; i < fp->np; i++) {
if (group_ranks[i] == 0) {
j--;
if (j == 0) {
fp->next_rank = i;
break;
}
}
}
}
}
/*-------------------------------------------------------------------------
* Function: H5VL_log_dataset_open
*
* Purpose: Opens a dataset in a container
*