darshan-hdf5.c 56.1 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8 9
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include "darshan-runtime-config.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <search.h>
#include <assert.h>
#include <pthread.h>

#include "darshan.h"
#include "darshan-dynamic.h"

27
#include <hdf5.h>
28

29
/* H5F prototypes */
30 31
DARSHAN_FORWARD_DECL(H5Fcreate, hid_t, (const char *filename, unsigned flags, hid_t create_plist, hid_t access_plist));
DARSHAN_FORWARD_DECL(H5Fopen, hid_t, (const char *filename, unsigned flags, hid_t access_plist));
32
DARSHAN_FORWARD_DECL(H5Fflush, herr_t, (hid_t object_id, H5F_scope_t scope));
33
DARSHAN_FORWARD_DECL(H5Fclose, herr_t, (hid_t file_id));
34

35 36 37 38 39 40 41 42 43 44 45
/* H5D prototypes */
DARSHAN_FORWARD_DECL(H5Dcreate1, hid_t, (hid_t loc_id, const char *name, hid_t type_id, hid_t space_id, hid_t dcpl_id));
DARSHAN_FORWARD_DECL(H5Dcreate2, hid_t, (hid_t loc_id, const char *name, hid_t dtype_id, hid_t space_id, hid_t lcpl_id, hid_t dcpl_id, hid_t dapl_id));
DARSHAN_FORWARD_DECL(H5Dopen1, hid_t, (hid_t loc_id, const char *name));
DARSHAN_FORWARD_DECL(H5Dopen2, hid_t, (hid_t loc_id, const char *name, hid_t dapl_id));
DARSHAN_FORWARD_DECL(H5Dread, herr_t, (hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, void * buf));
DARSHAN_FORWARD_DECL(H5Dwrite, herr_t, (hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, const void * buf));
#ifdef DARSHAN_HDF5_VERS_1_10_PLUS
DARSHAN_FORWARD_DECL(H5Dflush, herr_t, (hid_t dataset_id));
#endif
DARSHAN_FORWARD_DECL(H5Dclose, herr_t, (hid_t dataset_id));
46

47 48
/* structure that can track i/o stats for a given HDF5 file record at runtime */
struct hdf5_file_record_ref
Shane Snyder's avatar
Shane Snyder committed
49
{
50
    struct darshan_hdf5_file* file_rec;
51 52 53 54 55 56 57 58 59 60 61 62 63
    double last_meta_end;
}; 

/* structure that can track i/o stats for a given HDF5 dataset record at runtime */
struct hdf5_dataset_record_ref
{
    struct darshan_hdf5_dataset* dataset_rec;
    enum darshan_io_type last_io_type;
    double last_read_end;
    double last_write_end;
    double last_meta_end;
    void *access_root;
    int access_count;
Shane Snyder's avatar
Shane Snyder committed
64 65
};

66
/* struct to encapsulate runtime state for the HDF5 module */
67 68
struct hdf5_runtime
{
69 70
    void *rec_id_hash;
    void *hid_hash;
71
    int rec_count;
72 73
};

74 75 76
static void hdf5_file_runtime_initialize(
    void);
static void hdf5_dataset_runtime_initialize(
77 78
    void);
static struct hdf5_file_record_ref *hdf5_track_new_file_record(
79 80 81 82 83 84 85 86
    darshan_record_id rec_id, const char *rec_name);
static struct hdf5_dataset_record_ref *hdf5_track_new_dataset_record(
    darshan_record_id rec_id, const char *rec_name);
static void hdf5_finalize_dataset_records(
    void *rec_ref_p, void *user_ptr);
static void hdf5_cleanup_file_runtime(
    void);
static void hdf5_cleanup_dataset_runtime(
87
    void);
88
#ifdef HAVE_MPI
89 90 91 92 93 94 95 96 97 98 99
static void hdf5_file_record_reduction_op(
    void* inrec_v, void* inoutrec_v, int *len, MPI_Datatype *datatype);
static void hdf5_dataset_record_reduction_op(
    void* inrec_v, void* inoutrec_v, int *len, MPI_Datatype *datatype);
static void hdf5_shared_dataset_record_variance(
    MPI_Comm mod_comm, struct darshan_hdf5_dataset *inrec_array,
    struct darshan_hdf5_dataset *outrec_array, int shared_rec_count);
static void hdf5_file_mpi_redux(
    void *hdf5_buf, MPI_Comm mod_comm,
    darshan_record_id *shared_recs, int shared_rec_count);
static void hdf5_dataset_mpi_redux(
100 101 102
    void *hdf5_buf, MPI_Comm mod_comm,
    darshan_record_id *shared_recs, int shared_rec_count);
#endif
103 104 105
static void hdf5_file_shutdown(
    void **hdf5_buf, int *hdf5_buf_sz);
static void hdf5_dataset_shutdown(
106
    void **hdf5_buf, int *hdf5_buf_sz);
107

108 109
static struct hdf5_runtime *hdf5_file_runtime = NULL;
static struct hdf5_runtime *hdf5_dataset_runtime = NULL;
110 111 112 113 114 115
static pthread_mutex_t hdf5_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static int my_rank = -1;

#define HDF5_LOCK() pthread_mutex_lock(&hdf5_runtime_mutex)
#define HDF5_UNLOCK() pthread_mutex_unlock(&hdf5_runtime_mutex)

116 117 118 119 120
/*********************************************************
 *        Wrappers for H5F functions of interest         * 
 *********************************************************/

#define H5F_PRE_RECORD() do { \
121
    HDF5_LOCK(); \
122
    if(!darshan_core_disabled_instrumentation()) { \
123 124
        if(!hdf5_file_runtime) hdf5_file_runtime_initialize(); \
        if(hdf5_file_runtime) break; \
125
    } \
126 127
    HDF5_UNLOCK(); \
    return(ret); \
128 129
} while(0)

130
#define H5F_POST_RECORD() do { \
131 132 133
    HDF5_UNLOCK(); \
} while(0)

134
#define H5F_RECORD_OPEN(__ret, __path, __use_mpio, __tm1, __tm2) do { \
135 136 137 138 139 140 141
    darshan_record_id __rec_id; \
    struct hdf5_file_record_ref *__rec_ref; \
    char *__newpath; \
    __newpath = darshan_clean_file_path(__path); \
    if(!__newpath) __newpath = (char *)__path; \
    if(darshan_core_excluded_path(__newpath)) { \
        if(__newpath != __path) free(__newpath); \
142 143
        break; \
    } \
144 145 146 147 148
    __rec_id = darshan_core_gen_record_id(__newpath); \
    __rec_ref = darshan_lookup_record_ref(hdf5_file_runtime->rec_id_hash, &__rec_id, sizeof(darshan_record_id)); \
    if(!__rec_ref) __rec_ref = hdf5_track_new_file_record(__rec_id, __newpath); \
    if(!__rec_ref) { \
        if(__newpath != __path) free(__newpath); \
149 150
        break; \
    } \
151
    __rec_ref->file_rec->counters[H5F_USE_MPIIO] = __use_mpio; \
152 153 154 155 156 157 158 159 160
    __rec_ref->file_rec->counters[H5F_OPENS] += 1; \
    if(__rec_ref->file_rec->fcounters[H5F_F_OPEN_START_TIMESTAMP] == 0 || \
     __rec_ref->file_rec->fcounters[H5F_F_OPEN_START_TIMESTAMP] > __tm1) \
        __rec_ref->file_rec->fcounters[H5F_F_OPEN_START_TIMESTAMP] = __tm1; \
    __rec_ref->file_rec->fcounters[H5F_F_OPEN_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(__rec_ref->file_rec->fcounters[H5F_F_META_TIME], \
        __tm1, __tm2, __rec_ref->last_meta_end); \
    darshan_add_record_ref(&(hdf5_file_runtime->hid_hash), &__ret, sizeof(hid_t), __rec_ref); \
    if(__newpath != __path) free(__newpath); \
161 162
} while(0)

163 164 165
hid_t DARSHAN_DECL(H5Fcreate)(const char *filename, unsigned flags,
    hid_t create_plist, hid_t access_plist)
{
166
    hid_t ret;
167
    char* tmp;
168
    double tm1, tm2;
169
    unsigned majnum, minnum, relnum;
170
    int tmp_rank = my_rank;
171
    int use_mpio = 0;
172

173
    H5get_libversion(&majnum, &minnum, &relnum);
174 175 176
#ifdef DARSHAN_HDF5_VERS_1_10_PLUS
    if((majnum == 1) && (minnum < 10))
    {
177 178 179 180 181 182 183 184
        if(tmp_rank < 0)
#ifdef HAVE_MPI
            MPI_Comm_rank(MPI_COMM_WORLD, &tmp_rank);
#else
            tmp_rank = 0;
#endif

        if(tmp_rank == 0)
185 186 187 188 189
        {
            darshan_core_fprintf(stderr, "Darshan HDF5 module error: runtime library version (%d.%d) incompatible with Darshan module (1.10+).\n", majnum, minnum);
        }
        return(-1);
    }
190
#else
191
    if((majnum > 1) || (minnum >= 10))
192
    {
193 194 195 196 197 198 199 200
        if(tmp_rank < 0)
#ifdef HAVE_MPI
            MPI_Comm_rank(MPI_COMM_WORLD, &tmp_rank);
#else
            tmp_rank = 0;
#endif

        if(tmp_rank == 0)
201
        {
202
            darshan_core_fprintf(stderr, "Darshan HDF5 module error: runtime library version (%d.%d) incompatible with Darshan module (1.10-).\n", majnum, minnum);
203 204 205
        }
        return(-1);
    }
206
#endif
207 208 209

    MAP_OR_FAIL(H5Fcreate);

Shane Snyder's avatar
Shane Snyder committed
210
    tm1 = darshan_core_wtime();
211
    ret = __real_H5Fcreate(filename, flags, create_plist, access_plist);
212
    tm2 = darshan_core_wtime();
213

214 215 216 217 218 219 220 221 222 223 224 225
    if(ret >= 0)
    {
        /* use ROMIO approach to strip prefix if present */
        /* strip off prefix if there is one, but only skip prefixes
         * if they are greater than length one to allow for windows
         * drive specifications (e.g. c:\...) 
         */
        tmp = strchr(filename, ':');
        if (tmp > filename + 1) {
            filename = tmp + 1;
        }

226
#ifdef DARSHAN_HDF5_PAR_BUILD
227
        if(access_plist != H5P_DEFAULT && H5Pget_driver(access_plist) == H5FD_MPIO)
228 229 230
            use_mpio = 1;
#endif

231
        H5F_PRE_RECORD();
232
        H5F_RECORD_OPEN(ret, filename, use_mpio, tm1, tm2);
233
        H5F_POST_RECORD();
234 235 236 237 238 239 240 241
    }

    return(ret);
}

hid_t DARSHAN_DECL(H5Fopen)(const char *filename, unsigned flags,
    hid_t access_plist)
{
242
    hid_t ret;
243
    char* tmp;
244
    double tm1, tm2;
245
    unsigned majnum, minnum, relnum;
246
    int tmp_rank = my_rank;
247
    int use_mpio = 0;
248

249
    H5get_libversion(&majnum, &minnum, &relnum);
250 251 252
#ifdef DARSHAN_HDF5_VERS_1_10_PLUS
    if((majnum == 1) && (minnum < 10))
    {
253 254 255 256 257 258 259 260
        if(tmp_rank < 0)
#ifdef HAVE_MPI
            MPI_Comm_rank(MPI_COMM_WORLD, &tmp_rank);
#else
            tmp_rank = 0;
#endif

        if(tmp_rank == 0)
261 262 263 264 265
        {
            darshan_core_fprintf(stderr, "Darshan HDF5 module error: runtime library version (%d.%d) incompatible with Darshan module (1.10+).\n", majnum, minnum);
        }
        return(-1);
    }
266
#else
267
    if((majnum > 1) || (minnum >= 10))
268
    {
269 270 271 272 273 274 275 276
        if(tmp_rank < 0)
#ifdef HAVE_MPI
            MPI_Comm_rank(MPI_COMM_WORLD, &tmp_rank);
#else
            tmp_rank = 0;
#endif

        if(tmp_rank == 0)
277
        {
278
            darshan_core_fprintf(stderr, "Darshan HDF5 module error: runtime library version (%d.%d) incompatible with Darshan module (1.10-).\n", majnum, minnum);
279 280 281
        }
        return(-1);
    }
282
#endif
283 284 285

    MAP_OR_FAIL(H5Fopen);

Shane Snyder's avatar
Shane Snyder committed
286
    tm1 = darshan_core_wtime();
287
    ret = __real_H5Fopen(filename, flags, access_plist);
288
    tm2 = darshan_core_wtime();
289

290 291 292 293 294 295 296 297 298 299 300 301
    if(ret >= 0)
    {
        /* use ROMIO approach to strip prefix if present */
        /* strip off prefix if there is one, but only skip prefixes
         * if they are greater than length one to allow for windows
         * drive specifications (e.g. c:\...) 
         */
        tmp = strchr(filename, ':');
        if (tmp > filename + 1) {
            filename = tmp + 1;
        }

302
#ifdef DARSHAN_HDF5_PAR_BUILD
303
        if(access_plist != H5P_DEFAULT && H5Pget_driver(access_plist) == H5FD_MPIO)
304 305 306
            use_mpio = 1;
#endif

307
        H5F_PRE_RECORD();
308
        H5F_RECORD_OPEN(ret, filename, use_mpio, tm1, tm2);
309
        H5F_POST_RECORD();
310 311 312 313 314 315
    }

    return(ret);

}

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
herr_t DARSHAN_DECL(H5Fflush)(hid_t object_id, H5F_scope_t scope)
{
    struct hdf5_file_record_ref *rec_ref;
    hid_t file_id;
    double tm1, tm2;
    herr_t ret;

    MAP_OR_FAIL(H5Fflush);

    tm1 = darshan_core_wtime();
    ret = __real_H5Fflush(object_id, scope);
    tm2 = darshan_core_wtime();

    /* convert object_id to file_id so we can look it up */
    if(ret >= 0)
    {
        file_id = H5Iget_file_id(object_id);
        if(file_id > 0)
        {
            H5F_PRE_RECORD();
            rec_ref = darshan_lookup_record_ref(hdf5_file_runtime->hid_hash,
                &file_id, sizeof(hid_t));
            if(rec_ref)
            {
                rec_ref->file_rec->counters[H5F_FLUSHES] += 1;
                DARSHAN_TIMER_INC_NO_OVERLAP(
                    rec_ref->file_rec->fcounters[H5F_F_META_TIME],
                    tm1, tm2, rec_ref->last_meta_end);
            }
            H5F_POST_RECORD();
        }
    }

    return(ret);
}

352 353
herr_t DARSHAN_DECL(H5Fclose)(hid_t file_id)
{
354
    struct hdf5_file_record_ref *rec_ref;
355
    double tm1, tm2;
356
    herr_t ret;
357 358 359

    MAP_OR_FAIL(H5Fclose);

360
    tm1 = darshan_core_wtime();
361
    ret = __real_H5Fclose(file_id);
362
    tm2 = darshan_core_wtime();
363

364
    if(ret >= 0)
365
    {
366 367
        H5F_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(hdf5_file_runtime->hid_hash,
368
            &file_id, sizeof(hid_t));
369 370 371 372 373 374 375 376 377 378 379 380 381
        if(rec_ref)
        {
            if(rec_ref->file_rec->fcounters[H5F_F_CLOSE_START_TIMESTAMP] == 0 ||
             rec_ref->file_rec->fcounters[H5F_F_CLOSE_START_TIMESTAMP] > tm1)
               rec_ref->file_rec->fcounters[H5F_F_CLOSE_START_TIMESTAMP] = tm1;
            rec_ref->file_rec->fcounters[H5F_F_CLOSE_END_TIMESTAMP] = tm2;
            DARSHAN_TIMER_INC_NO_OVERLAP(
                rec_ref->file_rec->fcounters[H5F_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            darshan_delete_record_ref(&(hdf5_file_runtime->hid_hash),
                &file_id, sizeof(hid_t));
        }
        H5F_POST_RECORD();
382 383 384 385 386 387
    }

    return(ret);

}

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
/*********************************************************
 *        Wrappers for H5D functions of interest         * 
 *********************************************************/

#define DARSHAN_HDF5_MAX_NAME_LEN 256
#define DARSHAN_HDF5_DATASET_DELIM ":"

#define H5D_PRE_RECORD() do { \
    HDF5_LOCK(); \
    if(!darshan_core_disabled_instrumentation()) { \
        if(!hdf5_dataset_runtime) hdf5_dataset_runtime_initialize(); \
        if(hdf5_dataset_runtime) break; \
    } \
    HDF5_UNLOCK(); \
    return(ret); \
} while(0)

#define H5D_POST_RECORD() do { \
    HDF5_UNLOCK(); \
} while(0)

#define H5D_RECORD_OPEN(__ret, __loc_id, __name, __type_id, __space_id, __dcpl_id, __use_depr,  __tm1, __tm2) do { \
    char *__file_path, *__tmp_ptr; \
    char __rec_name[DARSHAN_HDF5_MAX_NAME_LEN] = {0}; \
    ssize_t __req_name_len = DARSHAN_HDF5_MAX_NAME_LEN-1, __ret_name_len; \
413
    darshan_record_id __rec_id, __file_rec_id = 0; \
414 415 416 417 418 419 420 421 422 423 424 425 426
    struct hdf5_dataset_record_ref *__rec_ref; \
    hsize_t __chunk_dims[H5D_MAX_NDIMS] = {0}; \
    int __i, __n_chunk_dims = 0; \
    /* get corresponding file name */\
    __ret_name_len = H5Fget_name(__loc_id, __rec_name, __req_name_len); \
    if(__ret_name_len < 0) break; \
    else if(__ret_name_len < __req_name_len) { \
        /* fully resolve file path */\
        __file_path = darshan_clean_file_path(__rec_name); \
        if(darshan_core_excluded_path(__file_path)) { \
            free(__file_path); \
            break; \
        } \
427
        __file_rec_id = darshan_core_gen_record_id(__file_path); \
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
        strncpy(__rec_name, __file_path, __req_name_len); \
        free(__file_path); \
        if(strlen(__rec_name) + 2 <= __req_name_len) { \
            /* append dataset name if we have space */\
            __tmp_ptr = __rec_name + strlen(__rec_name); \
            strcat(__tmp_ptr, DARSHAN_HDF5_DATASET_DELIM); \
            __tmp_ptr += 1; \
            __req_name_len = DARSHAN_HDF5_MAX_NAME_LEN - 1 - strlen(__rec_name); \
            __ret_name_len = H5Iget_name(__loc_id, __tmp_ptr, __req_name_len); \
            if(__ret_name_len < 0) return(ret); \
            else if(__ret_name_len < __req_name_len) { \
                __tmp_ptr = __rec_name + strlen(__rec_name); \
                __req_name_len = DARSHAN_HDF5_MAX_NAME_LEN - 1 - strlen(__rec_name); \
                strncat(__tmp_ptr, __name, __req_name_len); \
            } \
        } \
    } \
    __rec_id = darshan_core_gen_record_id(__rec_name); \
    __rec_ref = darshan_lookup_record_ref(hdf5_dataset_runtime->rec_id_hash, &__rec_id, sizeof(darshan_record_id)); \
    if(!__rec_ref) __rec_ref = hdf5_track_new_dataset_record(__rec_id, __rec_name); \
    if(!__rec_ref) break; \
    __rec_ref->dataset_rec->counters[H5D_OPENS] += 1; \
    __rec_ref->dataset_rec->counters[H5D_USE_DEPRECATED] = __use_depr; \
    if(__rec_ref->dataset_rec->fcounters[H5D_F_OPEN_START_TIMESTAMP] == 0 || \
     __rec_ref->dataset_rec->fcounters[H5D_F_OPEN_START_TIMESTAMP] > __tm1) \
        __rec_ref->dataset_rec->fcounters[H5D_F_OPEN_START_TIMESTAMP] = __tm1; \
    __rec_ref->dataset_rec->fcounters[H5D_F_OPEN_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(__rec_ref->dataset_rec->fcounters[H5D_F_META_TIME], \
        __tm1, __tm2, __rec_ref->last_meta_end); \
    __rec_ref->dataset_rec->counters[H5D_DATASPACE_NDIMS] = H5Sget_simple_extent_ndims(__space_id); \
    __rec_ref->dataset_rec->counters[H5D_DATASPACE_NPOINTS] = H5Sget_simple_extent_npoints(__space_id); \
    if(__dcpl_id != H5P_DEFAULT && H5Pget_layout(__dcpl_id) == H5D_CHUNKED) { \
        __n_chunk_dims = H5Pget_chunk(__dcpl_id, H5D_MAX_NDIMS, __chunk_dims); \
        __n_chunk_dims = (__n_chunk_dims < H5D_MAX_NDIMS) ? __n_chunk_dims : H5D_MAX_NDIMS; \
        for(__i = 0; __i < __n_chunk_dims; __i++) \
            __rec_ref->dataset_rec->counters[H5D_CHUNK_SIZE_D1 + __i] = __chunk_dims[__n_chunk_dims - __i - 1]; \
    } \
    __rec_ref->dataset_rec->counters[H5D_DATATYPE_SIZE] = H5Tget_size(__type_id); \
466
    __rec_ref->dataset_rec->file_rec_id = __file_rec_id; \
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
    darshan_add_record_ref(&(hdf5_dataset_runtime->hid_hash), &__ret, sizeof(hid_t), __rec_ref); \
} while(0)

hid_t DARSHAN_DECL(H5Dcreate1)(hid_t loc_id, const char *name, hid_t type_id, hid_t space_id, hid_t dcpl_id)
{
    double tm1, tm2;
    hid_t ret;

    MAP_OR_FAIL(H5Dcreate1);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dcreate1(loc_id, name, type_id, space_id, dcpl_id);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        H5D_PRE_RECORD();
        H5D_RECORD_OPEN(ret, loc_id, name, type_id, space_id, dcpl_id, 1, tm1, tm2);
        H5D_POST_RECORD();
    }

    return(ret);
}

hid_t DARSHAN_DECL(H5Dcreate2)(hid_t loc_id, const char *name, hid_t dtype_id, hid_t space_id,
    hid_t lcpl_id, hid_t dcpl_id, hid_t dapl_id)
{
    double tm1, tm2;
    hid_t ret;

    MAP_OR_FAIL(H5Dcreate2);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dcreate2(loc_id, name, dtype_id, space_id, lcpl_id, dcpl_id, dapl_id);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        H5D_PRE_RECORD();
        H5D_RECORD_OPEN(ret, loc_id, name, dtype_id, space_id, dcpl_id, 0, tm1, tm2);
        H5D_POST_RECORD();
    }

    return(ret);
}

hid_t DARSHAN_DECL(H5Dopen1)(hid_t loc_id, const char *name)
{
    hid_t dtype_id;
    hid_t space_id;
    hid_t dcpl_id;
    double tm1, tm2;
    hid_t ret;

    MAP_OR_FAIL(H5Dopen1);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dopen1(loc_id, name);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        /* query dataset datatype, dataspace, and creation property list */
        dtype_id = H5Dget_type(ret);
        if(dtype_id < 0)
            return(ret);
        space_id = H5Dget_space(ret);
        if(space_id < 0)
        {
            H5Tclose(dtype_id);
            return(ret);
        }
        dcpl_id = H5Dget_create_plist(ret);
        if(dcpl_id < 0)
        {
            H5Tclose(dtype_id);
            H5Sclose(space_id);
            return(ret);
        }

        H5D_PRE_RECORD();
        H5D_RECORD_OPEN(ret, loc_id, name, dtype_id, space_id, dcpl_id, 1, tm1, tm2);
        H5D_POST_RECORD();

        H5Tclose(dtype_id);
        H5Sclose(space_id);
        H5Pclose(dcpl_id);
    }

    return(ret);
}

hid_t DARSHAN_DECL(H5Dopen2)(hid_t loc_id, const char *name, hid_t dapl_id)
{
    hid_t dtype_id;
    hid_t space_id;
    hid_t dcpl_id;
    double tm1, tm2;
    hid_t ret;

    MAP_OR_FAIL(H5Dopen2);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dopen2(loc_id, name, dapl_id);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        /* query dataset datatype, dataspace, and creation property list */
        dtype_id = H5Dget_type(ret);
        if(dtype_id < 0)
            return(ret);
        space_id = H5Dget_space(ret);
        if(space_id < 0)
        {
            H5Tclose(dtype_id);
            return(ret);
        }
        dcpl_id = H5Dget_create_plist(ret);
        if(dcpl_id < 0)
        {   
            H5Tclose(dtype_id);
            H5Sclose(space_id);
            return(ret);
        }

        H5D_PRE_RECORD();
        H5D_RECORD_OPEN(ret, loc_id, name, dtype_id, space_id, dcpl_id, 0, tm1, tm2);
        H5D_POST_RECORD();

        H5Tclose(dtype_id);
        H5Sclose(space_id);
        H5Pclose(dcpl_id);
    }

    return(ret);
}

herr_t DARSHAN_DECL(H5Dread)(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id,
    hid_t file_space_id, hid_t xfer_plist_id, void * buf)
{
    struct hdf5_dataset_record_ref *rec_ref;
    size_t access_size;
    size_t type_size;
    ssize_t file_sel_npoints;
    H5S_sel_type file_sel_type;
    hsize_t start_dims[H5D_MAX_NDIMS] = {0};
    hsize_t stride_dims[H5D_MAX_NDIMS] = {0};
    hsize_t count_dims[H5D_MAX_NDIMS] = {0};
    hsize_t block_dims[H5D_MAX_NDIMS] = {0};
    int64_t common_access_vals[H5D_MAX_NDIMS+H5D_MAX_NDIMS+1] = {0};
    struct darshan_common_val_counter *cvc;
    int i;
    double tm1, tm2, elapsed;
    herr_t ret;
    herr_t tmp_ret;

    MAP_OR_FAIL(H5Dread);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dread(dataset_id, mem_type_id, mem_space_id, file_space_id,
        xfer_plist_id, buf);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        H5D_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(hdf5_dataset_runtime->hid_hash,
            &dataset_id, sizeof(hid_t));
        if(rec_ref)
        {
            rec_ref->dataset_rec->counters[H5D_READS] += 1;
            if(rec_ref->last_io_type == DARSHAN_IO_WRITE)
                rec_ref->dataset_rec->counters[H5D_RW_SWITCHES] += 1;
            rec_ref->last_io_type = DARSHAN_IO_READ;
            if(file_space_id == H5S_ALL)
            {
                file_sel_npoints = rec_ref->dataset_rec->counters[H5D_DATASPACE_NPOINTS];
                file_sel_type = H5S_SEL_ALL;
            }
            else
            {
                file_sel_npoints = H5Sget_select_npoints(file_space_id);
                file_sel_type = H5Sget_select_type(file_space_id);
            }
#ifdef DARSHAN_HDF5_VERS_1_10_PLUS
            if(file_sel_type == H5S_SEL_ALL)
                rec_ref->dataset_rec->counters[H5D_REGULAR_HYPERSLAB_SELECTS] += 1;
            else if(file_sel_type == H5S_SEL_POINTS)
                rec_ref->dataset_rec->counters[H5D_POINT_SELECTS] += 1;
657
            else if (file_sel_type == H5S_SEL_HYPERSLABS)
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
            {
                if(H5Sis_regular_hyperslab(file_space_id))
                {
                    rec_ref->dataset_rec->counters[H5D_REGULAR_HYPERSLAB_SELECTS] += 1;
                    H5Sget_regular_hyperslab(file_space_id,
                        start_dims, stride_dims, count_dims, block_dims);
                    for(i = 0; i < H5D_MAX_NDIMS; i++)
                    {
                        common_access_vals[1+i] = count_dims[H5D_MAX_NDIMS - i - 1] *
                            block_dims[H5D_MAX_NDIMS - i - 1];
                        common_access_vals[1+i+H5D_MAX_NDIMS] =
                            stride_dims[H5D_MAX_NDIMS - i - 1];
                    }
                }
                else
                    rec_ref->dataset_rec->counters[H5D_IRREGULAR_HYPERSLAB_SELECTS] += 1;
            }
#else
            rec_ref->dataset_rec->counters[H5D_POINT_SELECTS] = -1;
            rec_ref->dataset_rec->counters[H5D_REGULAR_HYPERSLAB_SELECTS] = -1;
            rec_ref->dataset_rec->counters[H5D_IRREGULAR_HYPERSLAB_SELECTS] = -1;
            for(i = 0; i < H5D_MAX_NDIMS; i++)
            {
                common_access_vals[1+i] = -1;
                common_access_vals[1+i+H5D_MAX_NDIMS] = -1;
            }
#endif
            type_size = rec_ref->dataset_rec->counters[H5D_DATATYPE_SIZE];
            access_size = file_sel_npoints * type_size;
            rec_ref->dataset_rec->counters[H5D_BYTES_READ] += access_size;
            DARSHAN_BUCKET_INC(
                &(rec_ref->dataset_rec->counters[H5D_SIZE_READ_AGG_0_100]), access_size);
            common_access_vals[0] = access_size;
            cvc = darshan_track_common_val_counters(&rec_ref->access_root,
                common_access_vals, H5D_MAX_NDIMS+H5D_MAX_NDIMS+1, &rec_ref->access_count);
            if(cvc) DARSHAN_UPDATE_COMMON_VAL_COUNTERS(
                &(rec_ref->dataset_rec->counters[H5D_ACCESS1_ACCESS]),
                &(rec_ref->dataset_rec->counters[H5D_ACCESS1_COUNT]),
                cvc->vals, cvc->nvals, cvc->freq, 0);
697
#ifdef DARSHAN_HDF5_PAR_BUILD
698 699
            if(xfer_plist_id != H5P_DEFAULT)
            {
700
                H5FD_mpio_xfer_t xfer_mode;
701 702 703 704
                tmp_ret = H5Pget_dxpl_mpio(xfer_plist_id, &xfer_mode);
                if(tmp_ret >= 0 && xfer_mode == H5FD_MPIO_COLLECTIVE)
                    rec_ref->dataset_rec->counters[H5D_USE_MPIIO_COLLECTIVE] = 1;
            }
705
#endif
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
            if(rec_ref->dataset_rec->fcounters[H5D_F_READ_START_TIMESTAMP] == 0 ||
             rec_ref->dataset_rec->fcounters[H5D_F_READ_START_TIMESTAMP] > tm1)
                rec_ref->dataset_rec->fcounters[H5D_F_READ_START_TIMESTAMP] = tm1;
            rec_ref->dataset_rec->fcounters[H5D_F_READ_END_TIMESTAMP] = tm2;
            elapsed = tm2 - tm1;
            if(rec_ref->dataset_rec->fcounters[H5D_F_MAX_READ_TIME] < elapsed)
            {
                rec_ref->dataset_rec->fcounters[H5D_F_MAX_READ_TIME] = elapsed;
                rec_ref->dataset_rec->counters[H5D_MAX_READ_TIME_SIZE] = access_size;
            }
            DARSHAN_TIMER_INC_NO_OVERLAP(
                rec_ref->dataset_rec->fcounters[H5D_F_READ_TIME],
                tm1, tm2, rec_ref->last_read_end);
        }
        H5D_POST_RECORD();
    }

    return(ret);
}

herr_t DARSHAN_DECL(H5Dwrite)(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id,
    hid_t file_space_id, hid_t xfer_plist_id, const void * buf)
{
    struct hdf5_dataset_record_ref *rec_ref;
    size_t access_size;
    size_t type_size;
    ssize_t file_sel_npoints;
    H5S_sel_type file_sel_type;
    hsize_t start_dims[H5D_MAX_NDIMS] = {0};
    hsize_t stride_dims[H5D_MAX_NDIMS] = {0};
    hsize_t count_dims[H5D_MAX_NDIMS] = {0};
    hsize_t block_dims[H5D_MAX_NDIMS] = {0};
    int64_t common_access_vals[H5D_MAX_NDIMS+H5D_MAX_NDIMS+1] = {0};
    struct darshan_common_val_counter *cvc;
    int i;
    double tm1, tm2, elapsed;
    herr_t ret;
    herr_t tmp_ret;

    MAP_OR_FAIL(H5Dwrite);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dwrite(dataset_id, mem_type_id, mem_space_id, file_space_id,
        xfer_plist_id, buf);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        H5D_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(hdf5_dataset_runtime->hid_hash,
            &dataset_id, sizeof(hid_t));
        if(rec_ref)
        {
            rec_ref->dataset_rec->counters[H5D_WRITES] += 1;
            if(rec_ref->last_io_type == DARSHAN_IO_READ)
                rec_ref->dataset_rec->counters[H5D_RW_SWITCHES] += 1;
            rec_ref->last_io_type = DARSHAN_IO_WRITE;
            if(file_space_id == H5S_ALL)
            {
                file_sel_npoints = rec_ref->dataset_rec->counters[H5D_DATASPACE_NPOINTS];
                file_sel_type = H5S_SEL_ALL;
            }
            else
            {
                file_sel_npoints = H5Sget_select_npoints(file_space_id);
                file_sel_type = H5Sget_select_type(file_space_id);
            }
#ifdef DARSHAN_HDF5_VERS_1_10_PLUS
            if(file_sel_type == H5S_SEL_ALL)
                rec_ref->dataset_rec->counters[H5D_REGULAR_HYPERSLAB_SELECTS] += 1;
            else if(file_sel_type == H5S_SEL_POINTS)
                rec_ref->dataset_rec->counters[H5D_POINT_SELECTS] += 1;
778
            else if (file_sel_type == H5S_SEL_HYPERSLABS)
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
            {
                if(H5Sis_regular_hyperslab(file_space_id))
                {
                    rec_ref->dataset_rec->counters[H5D_REGULAR_HYPERSLAB_SELECTS] += 1;
                    H5Sget_regular_hyperslab(file_space_id,
                        start_dims, stride_dims, count_dims, block_dims);
                    for(i = 0; i < H5D_MAX_NDIMS; i++)
                    {
                        common_access_vals[1+i] = count_dims[H5D_MAX_NDIMS - i - 1] *
                            block_dims[H5D_MAX_NDIMS - i - 1];
                        common_access_vals[1+i+H5D_MAX_NDIMS] =
                            stride_dims[H5D_MAX_NDIMS - i - 1];
                    }
                }
                else
                    rec_ref->dataset_rec->counters[H5D_IRREGULAR_HYPERSLAB_SELECTS] += 1;
            }
#else
            rec_ref->dataset_rec->counters[H5D_POINT_SELECTS] = -1;
            rec_ref->dataset_rec->counters[H5D_REGULAR_HYPERSLAB_SELECTS] = -1;
            rec_ref->dataset_rec->counters[H5D_IRREGULAR_HYPERSLAB_SELECTS] = -1;
            for(i = 0; i < H5D_MAX_NDIMS; i++)
            {
                common_access_vals[1+i] = -1;
                common_access_vals[1+i+H5D_MAX_NDIMS] = -1;
            }
#endif
            type_size = rec_ref->dataset_rec->counters[H5D_DATATYPE_SIZE];
            access_size = file_sel_npoints * type_size;
            rec_ref->dataset_rec->counters[H5D_BYTES_WRITTEN] += access_size;
            DARSHAN_BUCKET_INC(
                &(rec_ref->dataset_rec->counters[H5D_SIZE_WRITE_AGG_0_100]), access_size);
            common_access_vals[0] = access_size;
            cvc = darshan_track_common_val_counters(&rec_ref->access_root,
                common_access_vals, H5D_MAX_NDIMS+H5D_MAX_NDIMS+1, &rec_ref->access_count);
            if(cvc) DARSHAN_UPDATE_COMMON_VAL_COUNTERS(
                &(rec_ref->dataset_rec->counters[H5D_ACCESS1_ACCESS]),
                &(rec_ref->dataset_rec->counters[H5D_ACCESS1_COUNT]),
                cvc->vals, cvc->nvals, cvc->freq, 0);
818
#ifdef DARSHAN_HDF5_PAR_BUILD
819 820
            if(xfer_plist_id != H5P_DEFAULT)
            {
821
                H5FD_mpio_xfer_t xfer_mode;
822 823 824 825
                tmp_ret = H5Pget_dxpl_mpio(xfer_plist_id, &xfer_mode);
                if(tmp_ret >= 0 && xfer_mode == H5FD_MPIO_COLLECTIVE)
                    rec_ref->dataset_rec->counters[H5D_USE_MPIIO_COLLECTIVE] = 1;
            }
826
#endif
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
            if(rec_ref->dataset_rec->fcounters[H5D_F_WRITE_START_TIMESTAMP] == 0 ||
             rec_ref->dataset_rec->fcounters[H5D_F_WRITE_START_TIMESTAMP] > tm1)
                rec_ref->dataset_rec->fcounters[H5D_F_WRITE_START_TIMESTAMP] = tm1;
            rec_ref->dataset_rec->fcounters[H5D_F_WRITE_END_TIMESTAMP] = tm2;
            elapsed = tm2 - tm1;
            if(rec_ref->dataset_rec->fcounters[H5D_F_MAX_WRITE_TIME] < elapsed)
            {
                rec_ref->dataset_rec->fcounters[H5D_F_MAX_WRITE_TIME] = elapsed;
                rec_ref->dataset_rec->counters[H5D_MAX_WRITE_TIME_SIZE] = access_size;
            }
            DARSHAN_TIMER_INC_NO_OVERLAP(
                rec_ref->dataset_rec->fcounters[H5D_F_WRITE_TIME],
                tm1, tm2, rec_ref->last_write_end);
        }
        H5D_POST_RECORD();
    }

    return(ret);
}

#ifdef DARSHAN_HDF5_VERS_1_10_PLUS
herr_t DARSHAN_DECL(H5Dflush)(hid_t dataset_id)
{
    struct hdf5_dataset_record_ref *rec_ref;
    double tm1, tm2;
    herr_t ret;

    MAP_OR_FAIL(H5Dflush);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dflush(dataset_id);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        H5D_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(hdf5_dataset_runtime->hid_hash,
            &dataset_id, sizeof(hid_t));
        if(rec_ref)
        {
            rec_ref->dataset_rec->counters[H5D_FLUSHES] += 1;
            DARSHAN_TIMER_INC_NO_OVERLAP(
                rec_ref->dataset_rec->fcounters[H5D_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
        }
        H5D_POST_RECORD();
    }

    return(ret);
}
#endif

herr_t DARSHAN_DECL(H5Dclose)(hid_t dataset_id)
{
    struct hdf5_dataset_record_ref *rec_ref;
    double tm1, tm2;
    herr_t ret;

    MAP_OR_FAIL(H5Dclose);

    tm1 = darshan_core_wtime();
    ret = __real_H5Dclose(dataset_id);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
        H5D_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(hdf5_dataset_runtime->hid_hash,
            &dataset_id, sizeof(hid_t));
        if(rec_ref)
        {
            if(rec_ref->dataset_rec->fcounters[H5D_F_CLOSE_START_TIMESTAMP] == 0 ||
             rec_ref->dataset_rec->fcounters[H5D_F_CLOSE_START_TIMESTAMP] > tm1)
               rec_ref->dataset_rec->fcounters[H5D_F_CLOSE_START_TIMESTAMP] = tm1;
            rec_ref->dataset_rec->fcounters[H5D_F_CLOSE_END_TIMESTAMP] = tm2;
            DARSHAN_TIMER_INC_NO_OVERLAP(rec_ref->dataset_rec->fcounters[H5D_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            darshan_delete_record_ref(&(hdf5_dataset_runtime->hid_hash), &dataset_id, sizeof(hid_t));
        }
        H5D_POST_RECORD();
    }

    return(ret);
}

912 913 914 915
/*********************************************************
 * Internal functions for manipulating HDF5 module state *
 *********************************************************/

Shane Snyder's avatar
Shane Snyder committed
916
/* initialize internal HDF5 module data strucutres and register with darshan-core */
917
static void hdf5_file_runtime_initialize()
Shane Snyder's avatar
Shane Snyder committed
918
{
Philip Carns's avatar
Philip Carns committed
919
    size_t hdf5_buf_size;
920 921
    darshan_module_funcs mod_funcs = {
#ifdef HAVE_MPI
922
    .mod_redux_func = &hdf5_file_mpi_redux,
923
#endif
924
    .mod_shutdown_func = &hdf5_file_shutdown
925
    };
Shane Snyder's avatar
Shane Snyder committed
926

927 928 929
    /* try and store the default number of records for this module */
    hdf5_buf_size = DARSHAN_DEF_MOD_REC_COUNT * sizeof(struct darshan_hdf5_file);

Shane Snyder's avatar
Shane Snyder committed
930 931
    /* register hdf5 module with darshan-core */
    darshan_core_register_module(
932
        DARSHAN_H5F_MOD,
933
        mod_funcs,
934
        &hdf5_buf_size,
Shane Snyder's avatar
Shane Snyder committed
935 936 937
        &my_rank,
        NULL);

938 939
    hdf5_file_runtime = malloc(sizeof(*hdf5_file_runtime));
    if(!hdf5_file_runtime)
940
    {
941
        darshan_core_unregister_module(DARSHAN_H5F_MOD);
Shane Snyder's avatar
Shane Snyder committed
942
        return;
943
    }
944 945 946 947 948 949 950
    memset(hdf5_file_runtime, 0, sizeof(*hdf5_file_runtime));

    return;
}

static void hdf5_dataset_runtime_initialize()
{
wkliao's avatar
wkliao committed
951
    size_t hdf5_buf_size;
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
    darshan_module_funcs mod_funcs = {
#ifdef HAVE_MPI
    .mod_redux_func = &hdf5_dataset_mpi_redux,
#endif
    .mod_shutdown_func = &hdf5_dataset_shutdown
    };

    /* try and store the default number of records for this module */
    hdf5_buf_size = DARSHAN_DEF_MOD_REC_COUNT * sizeof(struct darshan_hdf5_dataset);

    /* register hdf5 module with darshan-core */
    darshan_core_register_module(
        DARSHAN_H5D_MOD,
        mod_funcs,
        &hdf5_buf_size,
        &my_rank,
        NULL);

    hdf5_dataset_runtime = malloc(sizeof(*hdf5_dataset_runtime));
    if(!hdf5_dataset_runtime)
    {
        darshan_core_unregister_module(DARSHAN_H5D_MOD);
        return;
    }
    memset(hdf5_dataset_runtime, 0, sizeof(*hdf5_dataset_runtime));
Shane Snyder's avatar
Shane Snyder committed
977 978 979 980

    return;
}

981 982
static struct hdf5_file_record_ref *hdf5_track_new_file_record(
    darshan_record_id rec_id, const char *path)
Shane Snyder's avatar
Shane Snyder committed
983
{
984 985
    struct darshan_hdf5_file *file_rec = NULL;
    struct hdf5_file_record_ref *rec_ref = NULL;
986
    int ret;
Shane Snyder's avatar
Shane Snyder committed
987

988 989
    rec_ref = malloc(sizeof(*rec_ref));
    if(!rec_ref)
Shane Snyder's avatar
Shane Snyder committed
990
        return(NULL);
991
    memset(rec_ref, 0, sizeof(*rec_ref));
Shane Snyder's avatar
Shane Snyder committed
992

993
    /* add a reference to this file record based on record id */
994
    ret = darshan_add_record_ref(&(hdf5_file_runtime->rec_id_hash), &rec_id,
995 996
        sizeof(darshan_record_id), rec_ref);
    if(ret == 0)
Shane Snyder's avatar
Shane Snyder committed
997
    {
998
        free(rec_ref);
Shane Snyder's avatar
Shane Snyder committed
999 1000 1001
        return(NULL);
    }

1002 1003
    /* register the actual file record with darshan-core so it is persisted
     * in the log file
Shane Snyder's avatar
Shane Snyder committed
1004
     */
1005 1006 1007
    file_rec = darshan_core_register_record(
        rec_id,
        path,
1008
        DARSHAN_H5F_MOD,
1009 1010
        sizeof(struct darshan_hdf5_file),
        NULL);
Shane Snyder's avatar
Shane Snyder committed
1011

1012
    if(!file_rec)
Shane Snyder's avatar
Shane Snyder committed
1013
    {
1014
        darshan_delete_record_ref(&(hdf5_file_runtime->rec_id_hash),
1015 1016 1017
            &rec_id, sizeof(darshan_record_id));
        free(rec_ref);
        return(NULL);
Shane Snyder's avatar
Shane Snyder committed
1018 1019
    }

1020
    /* registering this dataset record was successful, so initialize some fields */
1021 1022 1023
    file_rec->base_rec.id = rec_id;
    file_rec->base_rec.rank = my_rank;
    rec_ref->file_rec = file_rec;
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    hdf5_file_runtime->rec_count++;

    return(rec_ref);
}

static struct hdf5_dataset_record_ref *hdf5_track_new_dataset_record(
    darshan_record_id rec_id, const char *path)
{
    struct darshan_hdf5_dataset *dataset_rec = NULL;
    struct hdf5_dataset_record_ref *rec_ref = NULL;
    int ret;

    rec_ref = malloc(sizeof(*rec_ref));
    if(!rec_ref)
        return(NULL);
    memset(rec_ref, 0, sizeof(*rec_ref));

    /* add a reference to this dataset record based on record id */
    ret = darshan_add_record_ref(&(hdf5_dataset_runtime->rec_id_hash), &rec_id,
        sizeof(darshan_record_id), rec_ref);
    if(ret == 0)
    {
        free(rec_ref);
        return(NULL);
    }

    /* register the actual dataset record with darshan-core so it is persisted
     * in the log file
     */
    dataset_rec = darshan_core_register_record(
        rec_id,
        path,
        DARSHAN_H5D_MOD,
        sizeof(struct darshan_hdf5_dataset),
        NULL);

    if(!dataset_rec)
    {
        darshan_delete_record_ref(&(hdf5_dataset_runtime->rec_id_hash),
            &rec_id, sizeof(darshan_record_id));
        free(rec_ref);
        return(NULL);
    }

    /* registering this dataset record was successful, so initialize some fields */
    dataset_rec->base_rec.id = rec_id;
    dataset_rec->base_rec.rank = my_rank;
    rec_ref->dataset_rec = dataset_rec;
    hdf5_dataset_runtime->rec_count++;

#ifndef DARSHAN_HDF5_VERS_1_10_PLUS
    /* flushes weren't introduced until H5 version 1.10+ */
    rec_ref->dataset_rec->counters[H5D_FLUSHES] = -1;
#endif
1078 1079

    return(rec_ref);
Shane Snyder's avatar
Shane Snyder committed
1080 1081
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
static void hdf5_finalize_dataset_records(void *rec_ref_p, void *user_ptr)
{
    struct hdf5_dataset_record_ref *rec_ref =
        (struct hdf5_dataset_record_ref *)rec_ref_p;

    tdestroy(rec_ref->access_root, free);
    return;
}

static void hdf5_cleanup_file_runtime()
1092
{
1093 1094
    darshan_clear_record_refs(&(hdf5_file_runtime->hid_hash), 0);
    darshan_clear_record_refs(&(hdf5_file_runtime->rec_id_hash), 1);
1095

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
    free(hdf5_file_runtime);
    hdf5_file_runtime = NULL;

    return;
}

static void hdf5_cleanup_dataset_runtime()
{
    darshan_clear_record_refs(&(hdf5_dataset_runtime->hid_hash), 0);
    darshan_clear_record_refs(&(hdf5_dataset_runtime->rec_id_hash), 1);

    free(hdf5_dataset_runtime);
    hdf5_dataset_runtime = NULL;
1109 1110 1111 1112 1113

    return;
}

#ifdef HAVE_MPI
1114
static void hdf5_file_record_reduction_op(void* inrec_v, void* inoutrec_v,
Shane Snyder's avatar
Shane Snyder committed
1115 1116 1117
    int *len, MPI_Datatype *datatype)
{
    struct darshan_hdf5_file tmp_file;
1118 1119
    struct darshan_hdf5_file *inrec = inrec_v;
    struct darshan_hdf5_file *inoutrec = inoutrec_v;
Shane Snyder's avatar
Shane Snyder committed
1120 1121 1122 1123 1124
    int i, j;

    for(i=0; i<*len; i++)
    {
        memset(&tmp_file, 0, sizeof(struct darshan_hdf5_file));
1125
        tmp_file.base_rec.id = inrec->base_rec.id;
1126
        tmp_file.base_rec.rank = -1;
Shane Snyder's avatar
Shane Snyder committed
1127 1128

        /* sum */
1129
        for(j=H5F_OPENS; j<=H5F_FLUSHES; j++)
Shane Snyder's avatar
Shane Snyder committed
1130
        {
1131
            tmp_file.counters[j] = inrec->counters[j] + inoutrec->counters[j];
Shane Snyder's avatar
Shane Snyder committed
1132 1133
        }

1134 1135 1136
        if(inoutrec->counters[H5F_USE_MPIIO] == 1 || inrec->counters[H5F_USE_MPIIO] == 1)
            tmp_file.counters[H5F_USE_MPIIO] = 1;

Shane Snyder's avatar
Shane Snyder committed
1137
        /* min non-zero (if available) value */
1138
        for(j=H5F_F_OPEN_START_TIMESTAMP; j<=H5F_F_CLOSE_START_TIMESTAMP; j++)
Shane Snyder's avatar
Shane Snyder committed
1139
        {
1140 1141 1142
            if((inrec->fcounters[j] < inoutrec->fcounters[j] &&
               inrec->fcounters[j] > 0) || inoutrec->fcounters[j] == 0) 
                tmp_file.fcounters[j] = inrec->fcounters[j];
1143
            else
1144
                tmp_file.fcounters[j] = inoutrec->fcounters[j];
Shane Snyder's avatar
Shane Snyder committed
1145 1146 1147
        }

        /* max */
1148
        for(j=H5F_F_OPEN_END_TIMESTAMP; j<=H5F_F_CLOSE_END_TIMESTAMP; j++)
Shane Snyder's avatar
Shane Snyder committed
1149
        {
1150 1151
            if(inrec->fcounters[j] > inoutrec->fcounters[j])
                tmp_file.fcounters[j] = inrec->fcounters[j];
Shane Snyder's avatar
Shane Snyder committed
1152
            else
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
                tmp_file.fcounters[j] = inoutrec->fcounters[j];
        }

        /* sum */
        tmp_file.fcounters[H5F_F_META_TIME] =
            inrec->fcounters[H5F_F_META_TIME] + inoutrec->fcounters[H5F_F_META_TIME];

        /* update pointers */
        *inoutrec = tmp_file;
        inoutrec++;
        inrec++;
    }

    return;
}

static void hdf5_dataset_record_reduction_op(void* inrec_v, void* inoutrec_v,
    int *len, MPI_Datatype *datatype)
{
    struct darshan_hdf5_dataset tmp_dataset;
    struct darshan_hdf5_dataset *inrec = inrec_v;
    struct darshan_hdf5_dataset *inoutrec = inoutrec_v;
    int i, j, j2, k, k2;

    for(i=0; i<*len; i++)
    {
        memset(&tmp_dataset, 0, sizeof(struct darshan_hdf5_dataset));
        tmp_dataset.base_rec.id = inrec->base_rec.id;
        tmp_dataset.base_rec.rank = -1;
1182
        tmp_dataset.file_rec_id = inrec->file_rec_id;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

        /* sum */
        for(j=H5D_OPENS; j<=H5D_POINT_SELECTS; j++)
        {
            tmp_dataset.counters[j] = inrec->counters[j] + inoutrec->counters[j];
        }

        /* skip H5D_MAX_*_TIME_SIZE; handled in floating point section */

        for(j=H5D_SIZE_READ_AGG_0_100; j<=H5D_SIZE_WRITE_AGG_1G_PLUS; j++)
        {
            tmp_dataset.counters[j] = inrec->counters[j] + inoutrec->counters[j];
        }

        /* first collapse any duplicates */
        for(j=H5D_ACCESS1_ACCESS, j2=H5D_ACCESS1_COUNT; j<=H5D_ACCESS4_ACCESS;
            j+=(H5D_MAX_NDIMS+H5D_MAX_NDIMS+1), j2++)
        {
            for(k=H5D_ACCESS1_ACCESS, k2=H5D_ACCESS1_COUNT; k<=H5D_ACCESS4_ACCESS;
                k+=(H5D_MAX_NDIMS+H5D_MAX_NDIMS+1), k2++)
            {
                if(!memcmp(&inrec->counters[j], &inoutrec->counters[k],
                    sizeof(int64_t) * (H5D_MAX_NDIMS+H5D_MAX_NDIMS+1)))
                {
                    memset(&inoutrec->counters[k], 0, sizeof(int64_t) *
                        (H5D_MAX_NDIMS+H5D_MAX_NDIMS+1));
                    inrec->counters[j2] += inoutrec->counters[k2];
                    inoutrec->counters[k2] = 0;
                }
            }
        }

        /* first set */
        for(j=H5D_ACCESS1_ACCESS, j2=H5D_ACCESS1_COUNT; j<=H5D_ACCESS4_ACCESS;
            j+=(H5D_MAX_NDIMS+H5D_MAX_NDIMS+1), j2++)
        {
            DARSHAN_UPDATE_COMMON_VAL_COUNTERS(
                &(tmp_dataset.counters[H5D_ACCESS1_ACCESS]),
                &(tmp_dataset.counters[H5D_ACCESS1_COUNT]),
                &inrec->counters[j], H5D_MAX_NDIMS+H5D_MAX_NDIMS+1,
                inrec->counters[j2], 0);
        }

        /* second set */
        for(j=H5D_ACCESS1_ACCESS, j2=H5D_ACCESS1_COUNT; j<=H5D_ACCESS4_ACCESS;
            j+=(H5D_MAX_NDIMS+H5D_MAX_NDIMS+1), j2++)
        {
            DARSHAN_UPDATE_COMMON_VAL_COUNTERS(
                &(tmp_dataset.counters[H5D_ACCESS1_ACCESS]),
                &(tmp_dataset.counters[H5D_ACCESS1_COUNT]),
                &inoutrec->counters[j], H5D_MAX_NDIMS+H5D_MAX_NDIMS+1,
                inoutrec->counters[j2], 0);
        }

        tmp_dataset.counters[H5D_DATASPACE_NDIMS] = inrec->counters[H5D_DATASPACE_NDIMS];
        tmp_dataset.counters[H5D_DATASPACE_NPOINTS] = inrec->counters[H5D_DATASPACE_NPOINTS];
        tmp_dataset.counters[H5D_DATATYPE_SIZE] = inrec->counters[H5D_DATATYPE_SIZE];

        for(j=H5D_CHUNK_SIZE_D1; j<=H5D_CHUNK_SIZE_D5; j++)
            tmp_dataset.counters[j] = inrec->counters[j];

        if(inoutrec->counters[H5D_USE_MPIIO_COLLECTIVE] == 1 ||
                inrec->counters[H5D_USE_MPIIO_COLLECTIVE] == 1)
            tmp_dataset.counters[H5D_USE_MPIIO_COLLECTIVE] = 1;

        if(inoutrec->counters[H5D_USE_DEPRECATED] == 1 ||
                inrec->counters[H5D_USE_DEPRECATED] == 1)
            tmp_dataset.counters[H5D_USE_DEPRECATED] = 1;

        /* min non-zero (if available) value */
        for(j=H5D_F_OPEN_START_TIMESTAMP; j<=H5D_F_CLOSE_START_TIMESTAMP; j++)
        {
            if((inrec->fcounters[j] < inoutrec->fcounters[j] &&
               inrec->fcounters[j] > 0) || inoutrec->fcounters[j] == 0) 
                tmp_dataset.fcounters[j] = inrec->fcounters[j];
            else
                tmp_dataset.fcounters[j] = inoutrec->fcounters[j];
        }

        /* max */
        for(j=H5D_F_OPEN_END_TIMESTAMP; j<=H5D_F_CLOSE_END_TIMESTAMP; j++)
        {
            if(inrec->fcounters[j] > inoutrec->fcounters[j])
                tmp_dataset.fcounters[j] = inrec->fcounters[j];
            else
                tmp_dataset.fcounters[j] = inoutrec->fcounters[j];
        }

        /* sum */
        for(j=H5D_F_READ_TIME; j<=H5D_F_META_TIME; j++)
        {
            tmp_dataset.fcounters[j] = inrec->fcounters[j] + inoutrec->fcounters[j];
        }

        /* max (special case) */
        if(inrec->fcounters[H5D_F_MAX_READ_TIME] >
            inoutrec->fcounters[H5D_F_MAX_READ_TIME])
        {
            tmp_dataset.fcounters[H5D_F_MAX_READ_TIME] =
                inrec->fcounters[H5D_F_MAX_READ_TIME];
            tmp_dataset.counters[H5D_MAX_READ_TIME_SIZE] =
                inrec->counters[H5D_MAX_READ_TIME_SIZE];
        }
        else
        {
            tmp_dataset.fcounters[H5D_F_MAX_READ_TIME] =
                inoutrec->fcounters[H5D_F_MAX_READ_TIME];
            tmp_dataset.counters[H5D_MAX_READ_TIME_SIZE] =
                inoutrec->counters[H5D_MAX_READ_TIME_SIZE];
        }

        /* max (special case) */
        if(inrec->fcounters[H5D_F_MAX_WRITE_TIME] >
            inoutrec->fcounters[H5D_F_MAX_WRITE_TIME])
        {
            tmp_dataset.fcounters[H5D_F_MAX_WRITE_TIME] =
                inrec->fcounters[H5D_F_MAX_WRITE_TIME];
            tmp_dataset.counters[H5D_MAX_WRITE_TIME_SIZE] =
                inrec->counters[H5D_MAX_WRITE_TIME_SIZE];
        }
        else
        {
            tmp_dataset.fcounters[H5D_F_MAX_WRITE_TIME] =
                inoutrec->fcounters[H5D_F_MAX_WRITE_TIME];
            tmp_dataset.counters[H5D_MAX_WRITE_TIME_SIZE] =
                inoutrec->counters[H5D_MAX_WRITE_TIME_SIZE];
        }

        /* min (zeroes are ok here; some procs don't do I/O) */
        if(inrec->fcounters[H5D_F_FASTEST_RANK_TIME] <
            inoutrec->fcounters[H5D_F_FASTEST_RANK_TIME])
        {
            tmp_dataset.counters[H5D_FASTEST_RANK] =
                inrec->counters[H5D_FASTEST_RANK];
            tmp_dataset.counters[H5D_FASTEST_RANK_BYTES] =
                inrec->counters[H5D_FASTEST_RANK_BYTES];
            tmp_dataset.fcounters[H5D_F_FASTEST_RANK_TIME] =
                inrec->fcounters[H5D_F_FASTEST_RANK_TIME];
        }
        else
        {
            tmp_dataset.counters[H5D_FASTEST_RANK] =
                inoutrec->counters[H5D_FASTEST_RANK];
            tmp_dataset.counters[H5D_FASTEST_RANK_BYTES] =