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
|
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <libgen.h> /* basename(3) */
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> /* struct timespec, nanosleep */
#include <unistd.h> /* getopt, PATH_MAX, ... */
#include "hdf5.h"
#include "nbcompat.h"
#define DATAROWS 1
#define DATACOLS 10
static const hsize_t dims[2] = {DATAROWS, DATACOLS};
static const hsize_t chunk_dims[2] = {1, 1};
static volatile sig_atomic_t unbroken = 1;
typedef struct {
uint64_t created, deleted;
} stats_pair_t;
typedef struct {
stats_pair_t datasets, groups;
uint64_t iterations;
} stats_t;
typedef struct {
hid_t dataset[4], dataspace, dapl, dcpl, file, group[2];
char output_file[PATH_MAX];
char progname[PATH_MAX];
struct timespec update_interval;
int verbose;
bool oneshot;
bool print_stats;
bool use_vfd_swmr;
uint64_t iterations_limit;
stats_t stats;
} state_t;
#define ALL_HID_INITIALIZER \
(state_t) \
{ \
.dataspace = H5I_INVALID_HID, .file = H5I_INVALID_HID, .verbose = 0, .oneshot = false, \
.use_vfd_swmr = true, .print_stats = false, .iterations_limit = UINT64_MAX, .output_file = "", \
.update_interval = (struct timespec){.tv_sec = 0, .tv_nsec = 1000000000UL / 10 /* 1/10 second */}, \
.stats = { \
{0, 0}, \
{0, 0} \
} \
}
static void state_init(state_t *, int, char **);
static void
write_dataset(state_t *s, int didx)
{
const int ndatasets = __arraycount(s->dataset);
hid_t ds;
int32_t data[DATAROWS][DATACOLS];
herr_t status;
unsigned int i, j;
for (i = 0; i < __arraycount(data); i++) {
for (j = 0; j < __arraycount(data[i]); j++) {
int k = (didx + j + i) % __arraycount(data[i]);
data[i][j] = (0 <= k && k < 3) ? 1 : 0;
if (s->verbose > 1)
fprintf(stderr, " %" PRId32, data[i][j]);
}
if (s->verbose > 1)
fprintf(stderr, "\n");
}
ds = s->dataset[didx % ndatasets];
status = H5Dwrite(ds, H5T_NATIVE_INT32, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
if (status < 0)
errx(EXIT_FAILURE, "H5Dwrite failed");
if (H5Dflush(ds) < 0)
errx(EXIT_FAILURE, "H5Dflush failed");
}
static void
usage(const char *progname)
{
fprintf(stderr,
"usage: %s [-u milliseconds]\n"
"\n"
"-o: oneshot mode, perform one iteration, wait for a signal, "
"then quit.\n"
"-s: print statistics at end\n"
"-S: disable VFD SWMR mode\n"
"-u ms: milliseconds interval between updates to %s.h5\n"
"-v: verbose mode, mention creation/deletion; -vv: print data\n"
"\n",
progname, progname);
exit(EXIT_FAILURE);
}
static void
print_stats(stats_t *s)
{
printf("%10" PRIu64 " groups created\n", s->groups.created);
printf("%10" PRIu64 " groups deleted\n", s->groups.deleted);
printf("%10" PRIu64 " datasets created\n", s->datasets.created);
printf("%10" PRIu64 " datasets deleted\n", s->datasets.deleted);
printf("%10" PRIu64 " iterations\n", s->iterations);
}
static void
state_init(state_t *s, int argc, char **argv)
{
int ch, i, rc;
char tfile[PATH_MAX];
char * end;
unsigned long millis;
uintmax_t niters;
*s = ALL_HID_INITIALIZER;
strlcpy(tfile, argv[0], sizeof(tfile));
strlcpy(s->progname, basename(tfile), sizeof(s->progname));
while ((ch = getopt(argc, argv, "n:ou:sSv")) != -1) {
switch (ch) {
case 'n':
niters = strtoumax(optarg, &end, 0);
if (niters == UINTMAX_MAX && errno == ERANGE) {
err(EXIT_FAILURE, "option -%c argument \"%s\"", ch, optarg);
}
else if (*end != '\0') {
errx(EXIT_FAILURE, "garbage after -%c argument \"%s\"", ch, optarg);
}
s->iterations_limit = niters;
break;
case 'o':
s->oneshot = true;
break;
case 's':
s->print_stats = true;
break;
case 'S':
s->use_vfd_swmr = false;
break;
case 'u':
errno = 0;
millis = strtoul(optarg, &end, 0);
if (millis == ULONG_MAX && errno == ERANGE) {
err(EXIT_FAILURE, "option -%c argument \"%s\"", ch, optarg);
}
else if (*end != '\0') {
errx(EXIT_FAILURE, "garbage after -%c argument \"%s\"", ch, optarg);
}
s->update_interval.tv_sec = millis / 1000UL;
s->update_interval.tv_nsec = (millis * 1000000UL) % 1000000000UL;
warnx("%lu milliseconds between updates", millis);
break;
case 'v':
s->verbose++;
break;
case '?':
default:
usage(s->progname);
}
}
argc -= optind;
argv += optind;
for (i = 0; i < __arraycount(s->dataset); i++)
s->dataset[i] = H5I_INVALID_HID;
for (i = 0; i < __arraycount(s->group); i++)
s->group[i] = H5I_INVALID_HID;
rc = snprintf(s->output_file, sizeof(s->output_file), "%s.h5", s->progname);
if (rc == -1 || rc >= sizeof(s->output_file))
errx(EXIT_FAILURE, "output filename was truncated");
}
static void
delete_group(state_t *s, const int64_t gidx)
{
hid_t g;
const int ngroups = __arraycount(s->group);
char gname[32];
assert(0 <= gidx);
snprintf(gname, sizeof(gname), "/group-%" PRId64, gidx);
g = s->group[gidx % ngroups];
if (H5Ldelete(s->file, gname, H5P_DEFAULT) < 0) {
errx(EXIT_FAILURE, "%s: H5Ldelete(, \"%s\", ) failed", __func__, gname);
}
if (H5Gclose(g) < 0)
errx(EXIT_FAILURE, "H5Gclose failed");
if (s->verbose > 0)
fprintf(stderr, "Deleted group %s\n", gname);
s->group[gidx % ngroups] = H5I_INVALID_HID;
s->stats.groups.deleted++;
}
static void
create_group(state_t *s, const int64_t gidx)
{
const int ngroups = __arraycount(s->group);
hid_t g;
char gname[32];
assert(0 <= gidx);
assert(s->group[gidx % ngroups] == H5I_INVALID_HID);
snprintf(gname, sizeof(gname), "/group-%" PRId64, gidx);
g = H5Gcreate(s->file, gname, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if (g < 0)
errx(EXIT_FAILURE, "H5Gcreate failed");
if (s->verbose > 0)
fprintf(stderr, "Created group %s\n", gname);
s->group[gidx % ngroups] = g;
s->stats.groups.created++;
}
static void
delete_dataset(state_t *s, const int64_t didx)
{
const int ndatasets = __arraycount(s->dataset);
char dname[32];
const int64_t gidx = didx / 2;
assert(0 <= gidx && 0 <= didx);
snprintf(dname, sizeof(dname), "/group-%" PRId64 "/dataset-%" PRId64, gidx, didx);
if (H5Ldelete(s->file, dname, H5P_DEFAULT) < 0) {
errx(EXIT_FAILURE, "%s: H5Ldelete(, \"%s\", ) failed", __func__, dname);
}
if (s->verbose > 0)
fprintf(stderr, "Deleted dataset %s\n", dname);
#if 1
const hid_t ds = s->dataset[didx % ndatasets];
if (H5Dclose(ds) < 0)
errx(EXIT_FAILURE, "H5Dclose failed");
#endif
s->dataset[didx % ndatasets] = H5I_INVALID_HID;
s->stats.datasets.deleted++;
}
static void
create_dataset(state_t *s, const int64_t didx)
{
const int ndatasets = __arraycount(s->dataset);
char dname[32];
const int64_t gidx = didx / 2;
hid_t ds;
assert(0 <= gidx && 0 <= didx);
assert(s->dataset[didx % ndatasets] == H5I_INVALID_HID);
s->dataspace = H5Screate_simple(__arraycount(dims), dims, NULL);
if (s->dataspace < 0)
errx(EXIT_FAILURE, "H5Screate_simple failed");
snprintf(dname, sizeof(dname), "/group-%" PRId64 "/dataset-%" PRId64, gidx, didx);
ds = H5Dcreate2(s->file, dname, H5T_STD_I32BE, s->dataspace, H5P_DEFAULT, s->dcpl, s->dapl);
if (H5Sclose(s->dataspace) < 0)
errx(EXIT_FAILURE, "H5Sclose failed");
s->dataspace = H5I_INVALID_HID;
if (ds < 0)
errx(EXIT_FAILURE, "H5Dcreate(, \"%s\", ) failed", dname);
if (s->verbose > 0)
fprintf(stderr, "Created dataset %s\n", dname);
s->dataset[didx % ndatasets] = ds;
s->stats.datasets.created++;
}
static void
create_and_write_dataset(state_t *s, const int64_t didx)
{
#if 0
const int64_t gidx = didx / 2;
const int ngroups = __arraycount(s->group);
const hid_t g = s->group[gidx % ngroups];
if (H5Odisable_mdc_flushes(g) < 0)
err(EXIT_FAILURE, "H5Odisable_mdc_flushes failed");
#endif
create_dataset(s, didx);
write_dataset(s, didx);
#if 0
if (H5Oenable_mdc_flushes(g) < 0)
err(EXIT_FAILURE, "H5Oenable_mdc_flushes failed");
#endif
}
static void
handle_signal(int signo)
{
char msg[] = "Handling signal\n";
write(STDERR_FILENO, msg, sizeof(msg) - 1);
unbroken = 0;
}
static void
disestablish_handler(const struct sigaction *osa)
{
if (sigaction(SIGINT, osa, NULL) == -1)
err(EXIT_FAILURE, "%s: sigaction", __func__);
}
static void
establish_handler(struct sigaction *osa)
{
struct sigaction sa;
memset(&sa, '\0', sizeof(sa));
sa.sa_handler = handle_signal;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, osa) == -1)
err(EXIT_FAILURE, "%s: sigaction", __func__);
}
int
main(int argc, char **argv)
{
hid_t fapl, fcpl;
struct sigaction osa;
state_t storage;
state_t * s = &storage;
int64_t i;
H5F_vfd_swmr_config_t config;
memset(&config, '\0', sizeof(config));
state_init(s, argc, argv);
fapl = H5Pcreate(H5P_FILE_ACCESS);
if (fapl < 0) {
errx(EXIT_FAILURE, "%s.%d H5Pcreate failed", __func__, __LINE__);
}
fcpl = H5Pcreate(H5P_FILE_CREATE);
if (fcpl < 0) {
errx(EXIT_FAILURE, "%s.%d H5Pcreate failed", __func__, __LINE__);
}
config.version = H5F__CURR_VFD_SWMR_CONFIG_VERSION;
config.tick_len = 4;
config.max_lag = 5;
#if 0 /* raw-data flushing is not implemented */
config.flush_raw_data = true;
#endif
config.writer = true;
config.md_pages_reserved = 128;
strlcpy(config.md_file_path, "./my_md_file", sizeof(config.md_file_path));
/* Enable page buffering */
if (H5Pset_page_buffer_size(fapl, 4096, 100, 0) < 0)
errx(EXIT_FAILURE, "H5Pset_page_buffer_size failed");
/* Enable VFD SWMR configuration */
if (s->use_vfd_swmr && H5Pset_vfd_swmr_config(fapl, &config) < 0)
errx(EXIT_FAILURE, "H5Pset_vfd_swmr_config failed");
/* Set file space strategy to paged aggregation in fcpl.
* Page buffering *requires* this strategy.
*
* I set the free-space threshold to 1GB so that deleted
* datasets are not recycled.
*/
if (H5Pset_file_space_strategy(fcpl, H5F_FSPACE_STRATEGY_PAGE, false, 1024 * 1024 * 1024) < 0)
errx(EXIT_FAILURE, "H5Pset_file_space_strategy failed");
s->file = H5Fcreate(s->output_file, H5F_ACC_TRUNC, fcpl, fapl);
H5Pclose(fapl);
if (s->file < 0)
errx(EXIT_FAILURE, "H5Fcreate failed");
if ((s->dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) {
errx(EXIT_FAILURE, "%s.%d: H5Pcreate failed", __func__, __LINE__);
}
if ((s->dapl = H5Pcreate(H5P_DATASET_ACCESS)) < 0) {
errx(EXIT_FAILURE, "%s.%d: H5Pcreate failed", __func__, __LINE__);
}
if (H5Pset_chunk(s->dcpl, 2, chunk_dims) < 0)
errx(EXIT_FAILURE, "H5Pset_chunk failed");
if (H5Pset_chunk_cache(s->dapl, H5D_CHUNK_CACHE_NSLOTS_DEFAULT, 0, H5D_CHUNK_CACHE_W0_DEFAULT) < 0)
errx(EXIT_FAILURE, "H5Pset_chunk_cache failed");
establish_handler(&osa);
for (i = 0; i < 4; i++) {
s->stats.iterations++;
if (i % 2 == 0)
create_group(s, i / 2);
create_and_write_dataset(s, i);
}
for (i = 5; unbroken; i += 2) {
delete_dataset(s, i - 5);
delete_dataset(s, i - 4);
delete_group(s, (i - 4) / 2);
create_group(s, i / 2);
create_and_write_dataset(s, i - 1);
create_and_write_dataset(s, i);
if (s->oneshot || ++s->stats.iterations >= s->iterations_limit)
break;
nanosleep(&s->update_interval, NULL);
}
if (s->oneshot) {
sigset_t mask;
sigemptyset(&mask);
H5Fvfd_swmr_end_tick(s->file);
(void)sigsuspend(&mask);
}
#if 0
fprintf(stderr, "Interrupted. Cleaning up.\n");
int j;
for (--i, j = 0; j < 4; j++, --i) {
if (i % 2 == 1) {
delete_dataset(s, i - 1);
delete_dataset(s, i);
delete_group(s, i / 2);
}
}
for (j = 0; j < 4; j++) {
assert(s->dataset[j] == H5I_INVALID_HID);
assert(s->group[j / 2] == H5I_INVALID_HID);
}
#endif
if (s->print_stats)
print_stats(&s->stats);
if (H5Fclose(s->file) < 0)
errx(EXIT_FAILURE, "H5Fclose failed");
if (H5Pclose(s->dapl) < 0)
errx(EXIT_FAILURE, "H5Pclose failed");
if (H5Pclose(s->dcpl) < 0)
errx(EXIT_FAILURE, "H5Pclose failed");
disestablish_handler(&osa);
return EXIT_SUCCESS;
}
|