1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  * Programmer:  Robb Matzke <matzke@llnl.gov>
16  *              Friday, October 23, 1998
17  *
18  * Purpose:	This is the first half of a two-part test that makes sure
19  *		that a file can be read after an application crashes as long
20  *		as the file was flushed first.  We simulate a crash by
21  *		calling _exit(EXIT_SUCCESS) since this doesn't flush HDF5 caches but
22  *		still exits with success.
23  */
24 #include "h5test.h"
25 
26 /* This file needs to access the file driver testing code */
27 #define H5FD_FRIEND     /*suppress error about including H5FDpkg      */
28 #define H5FD_TESTING
29 #include "H5FDpkg.h"    /* File drivers             */
30 
31 const char *FILENAME[] = {
32     "flush",
33     "flush-swmr",
34     "noflush",
35     "noflush-swmr",
36     "flush_extend",
37     "flush_extend-swmr",
38     "noflush_extend",
39     "noflush_extend-swmr",
40     NULL
41 };
42 
43 /* Number and size of dataset dims, chunk size, etc. */
44 #define NDIMS       1
45 #define NELEMENTS   10000
46 #define CHUNK_SIZE  25
47 #define FIRST_DSET_NAME     "dset1"
48 #define SECOND_DSET_NAME    "dset2"
49 
50 /* Number of sub-groups created in the containing group */
51 #define NGROUPS     100
52 
53 static hid_t create_file(const char *filename, hid_t fapl_id, hbool_t swmr);
54 static herr_t add_dset_to_file(hid_t fid, const char *dset_name);
55 
56 
57 /*-------------------------------------------------------------------------
58  * Function:    create_file
59  *
60  * Purpose:     Creates files and datasets used in part 1 of the test
61  *
62  * Return:      Success:	a valid file ID
63  *              Failure:	-1
64  *
65  * Programmer:	Leon Arber
66  *              Sept. 26, 2006
67  *
68  *-------------------------------------------------------------------------
69  */
70 static hid_t
create_file(const char * filename,hid_t fapl_id,hbool_t swmr)71 create_file(const char *filename, hid_t fapl_id, hbool_t swmr)
72 {
73     hid_t   fid = -1;               /* file ID                          */
74     hid_t   top_gid = -1;           /* containing group ID              */
75     hid_t   gid = -1;               /* subgroup ID                      */
76     char    group_name[32];         /* group name                       */
77     unsigned    flags;              /* file open flags                  */
78     int     i;                      /* iterator                         */
79 
80     flags = H5F_ACC_TRUNC | (swmr ? H5F_ACC_SWMR_WRITE : 0);
81 
82     if((fid = H5Fcreate(filename, flags, H5P_DEFAULT, fapl_id)) < 0)
83         STACK_ERROR
84 
85     /* Create a chunked dataset */
86     if(add_dset_to_file(fid, FIRST_DSET_NAME) < 0)
87         TEST_ERROR
88 
89     /* Create some groups */
90     if((top_gid = H5Gcreate2(fid, "top_group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
91         STACK_ERROR
92     for(i = 0; i < NGROUPS; i++) {
93         HDsprintf(group_name, "group%02d", i);
94         if((gid = H5Gcreate2(top_gid, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
95             STACK_ERROR
96         if(H5Gclose(gid) < 0)
97             STACK_ERROR
98     } /* end for */
99 
100     if(H5Gclose(top_gid) < 0)
101         STACK_ERROR
102 
103     return fid;
104 
105 error:
106     H5E_BEGIN_TRY {
107         H5Fclose(fid);
108         H5Gclose(gid);
109         H5Gclose(top_gid);
110      } H5E_END_TRY;
111 
112     return -1;
113 } /* end create_file() */
114 
115 
116 /*-------------------------------------------------------------------------
117  * Function:    add_dset_to_file
118  *
119  * Purpose:     Add a dataset to the file.
120  *
121  * Return:      SUCCEED/FAIL
122  *
123  * Programmer:	Leon Arber
124  *              Oct. 4, 2006
125  *
126  *-------------------------------------------------------------------------
127  */
128 static herr_t
add_dset_to_file(hid_t fid,const char * dset_name)129 add_dset_to_file(hid_t fid, const char *dset_name)
130 {
131     hid_t   dcpl_id = -1;           /* dataset creation plist ID        */
132     hid_t   sid = -1;               /* dataspace ID                     */
133     hid_t   did = -1;               /* dataset ID                       */
134     int    *data = NULL;            /* data buffer                      */
135     hsize_t dims[1] = {NELEMENTS};  /* size of dataset                  */
136     hsize_t chunk_dims[1] = {CHUNK_SIZE};   /* chunk size               */
137     int     i;                      /* iterator                         */
138 
139     /* Create a chunked dataset */
140     if((dcpl_id = H5Pcreate(H5P_DATASET_CREATE)) < 0)
141         STACK_ERROR
142     if(H5Pset_chunk(dcpl_id, NDIMS, chunk_dims) < 0)
143         STACK_ERROR
144     if((sid = H5Screate_simple(NDIMS, dims, NULL)) < 0)
145         STACK_ERROR
146     if((did = H5Dcreate2(fid, dset_name, H5T_NATIVE_FLOAT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
147         STACK_ERROR
148 
149     /* Write some data */
150     if(NULL == (data = (int *)HDcalloc((size_t)NELEMENTS, sizeof(int))))
151         STACK_ERROR
152     for(i = 0; i < NELEMENTS; i++)
153         data[i] = i;
154     if(H5Dwrite(did, H5T_NATIVE_INT, sid, sid, H5P_DEFAULT, data) < 0)
155         STACK_ERROR
156 
157     if(H5Pclose(dcpl_id) < 0)
158         STACK_ERROR
159     if(H5Sclose(sid) < 0)
160         STACK_ERROR
161     if(H5Dclose(did) < 0)
162         STACK_ERROR
163 
164     HDfree(data);
165 
166     return SUCCEED;
167 
168 error:
169     H5E_BEGIN_TRY {
170         H5Pclose(dcpl_id);
171         H5Sclose(sid);
172         H5Dclose(did);
173      } H5E_END_TRY;
174 
175     HDfree(data);
176 
177     return FAIL;
178 } /* end add_dset_to_file() */
179 
180 
181 /*-------------------------------------------------------------------------
182  * Function:	main
183  *
184  * Purpose:     Creates files and datasets with and without flushing in
185  *              a variety of situations.
186  *
187  *              Part 1 of a two-part H5Fflush() test.
188  *
189  * Return:      EXIT_SUCCESS/EXIT_FAILURE
190  *
191  * Programmer:	Robb Matzke
192  *              Friday, October 23, 1998
193  *
194  *-------------------------------------------------------------------------
195  */
196 int
main(void)197 main(void)
198 {
199     char   *driver = NULL;      /* name of current VFD (from env var)       */
200     hbool_t vfd_supports_swmr;  /* whether the current VFD supports SWMR    */
201     hid_t   fid = -1;           /* file ID                                  */
202     hid_t   fapl_id = -1;       /* file access proplist ID                  */
203     char    filename[1024];     /* filename                                 */
204     hbool_t use_swmr;           /* whether or not to use SWMR I/O           */
205 
206     h5_reset();
207     if((fapl_id = h5_fileaccess()) < 0)
208         TEST_ERROR
209 
210     /* Check if the current VFD supports SWMR */
211     driver = HDgetenv("HDF5_DRIVER");
212     vfd_supports_swmr = H5FD_supports_swmr_test(driver);
213 
214     /*************************************************/
215     /* NOTE: Not closing the file ID is intentional! */
216     /*************************************************/
217 
218     /* Create a file and flush */
219     TESTING("H5Fflush (part1 with flush)");
220     h5_fixname(FILENAME[0], fapl_id, filename, sizeof(filename));
221     use_swmr = FALSE;
222     if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
223         TEST_ERROR
224     if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
225         FAIL_STACK_ERROR
226     PASSED();
227 
228     /* Create a file and flush w/ SWMR I/O */
229     TESTING("H5Fflush (part1 with flush + SWMR)");
230     if(vfd_supports_swmr) {
231         h5_fixname(FILENAME[1], fapl_id, filename, sizeof(filename));
232         use_swmr = TRUE;
233         if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
234             TEST_ERROR
235         if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
236             FAIL_STACK_ERROR
237         PASSED();
238     } /* end if */
239     else
240         SKIPPED();
241 
242     /* Create a file which will not be flushed */
243     TESTING("H5Fflush (part1 without flush)");
244     h5_fixname(FILENAME[2], fapl_id, filename, sizeof(filename));
245     use_swmr = FALSE;
246     if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
247         TEST_ERROR
248     PASSED();
249 
250     /* Create a file which will not be flushed w/ SWMR I/O */
251     TESTING("H5Fflush (part1 without flush + SWMR)");
252     if(vfd_supports_swmr) {
253         h5_fixname(FILENAME[3], fapl_id, filename, sizeof(filename));
254         use_swmr = TRUE;
255         if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
256             TEST_ERROR
257         PASSED();
258     } /* end if */
259     else
260         SKIPPED();
261 
262     /* Create a file, flush, add a dataset, flush */
263     TESTING("H5Fflush (part1 with flush and later addition and another flush)");
264     h5_fixname(FILENAME[4], fapl_id, filename, sizeof(filename));
265     use_swmr = FALSE;
266     if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
267         TEST_ERROR
268     if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
269         FAIL_STACK_ERROR
270     if(add_dset_to_file(fid, SECOND_DSET_NAME) < 0)
271         TEST_ERROR
272     if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
273         FAIL_STACK_ERROR
274     PASSED();
275 
276     /* Create a file, flush, add a dataset, flush w/ SWMR I/O */
277     TESTING("H5Fflush (part1 with flush and later addition and another flush + SWMR)");
278     if(vfd_supports_swmr) {
279         h5_fixname(FILENAME[5], fapl_id, filename, sizeof(filename));
280         use_swmr = TRUE;
281         if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
282             TEST_ERROR
283         if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
284             FAIL_STACK_ERROR
285         if(add_dset_to_file(fid, SECOND_DSET_NAME) < 0)
286             TEST_ERROR
287         if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
288             FAIL_STACK_ERROR
289         PASSED();
290     } /* end if */
291     else
292         SKIPPED();
293 
294     /* Create a file, flush, add a dataset, (no flush) */
295     TESTING("H5Fflush (part1 with flush and later addition)");
296     h5_fixname(FILENAME[6], fapl_id, filename, sizeof(filename));
297     use_swmr = FALSE;
298     if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
299         TEST_ERROR
300     if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
301         FAIL_STACK_ERROR
302     if(add_dset_to_file(fid, SECOND_DSET_NAME) < 0)
303         TEST_ERROR
304     PASSED();
305 
306     /* Create a file, flush, add a dataset, (no flush) w/ SWMR I/O */
307     TESTING("H5Fflush (part1 with flush and later addition + SWMR)");
308     if(vfd_supports_swmr) {
309         h5_fixname(FILENAME[7], fapl_id, filename, sizeof(filename));
310         use_swmr = TRUE;
311         if((fid = create_file(filename, fapl_id, use_swmr)) < 0)
312             TEST_ERROR
313         if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
314             FAIL_STACK_ERROR
315         if(add_dset_to_file(fid, SECOND_DSET_NAME) < 0)
316             TEST_ERROR
317         PASSED();
318     } /* end if */
319     else
320         SKIPPED();
321 
322     if(!vfd_supports_swmr)
323         HDprintf("NOTE: Some tests were skipped since the current VFD lacks SWMR support\n");
324 
325     /* Flush console output streams */
326     HDfflush(stdout);
327     HDfflush(stderr);
328 
329     /* DO NOT CLOSE FILE ID! */
330     if(H5Pclose(fapl_id) < 0)
331         STACK_ERROR
332 
333     /* _exit() is necessary since we want a hard close of the library */
334     HD_exit(EXIT_SUCCESS);
335 
336 error:
337     H5E_BEGIN_TRY {
338         H5Pclose(fapl_id);
339     } H5E_END_TRY;
340 
341     HDexit(EXIT_FAILURE);
342 } /* end main() */
343 
344