darshan-hdf5.c 17.1 KB
Newer Older
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
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#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>
#define __USE_GNU
#include <pthread.h>

#include "uthash.h"

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

/* hope this doesn't change any time soon */
typedef int hid_t;
typedef int herr_t;

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));
DARSHAN_FORWARD_DECL(H5Fclose, herr_t, (hid_t file_id));

Shane Snyder's avatar
Shane Snyder committed
35
/* structure to track i/o stats for a given hdf5 file at runtime */
36 37 38 39 40 41
struct hdf5_file_runtime
{
    struct darshan_hdf5_file* file_record;
    UT_hash_handle hlink;
};

Shane Snyder's avatar
Shane Snyder committed
42 43 44 45 46 47 48 49 50 51 52
/* structure to associate a HDF5 hid with an existing file runtime structure */
struct hdf5_file_runtime_ref
{
    struct hdf5_file_runtime* file;
    hid_t hid;
    UT_hash_handle hlink;
};

/* necessary state for storing HDF5 file records and coordinating with
 * darshan-core at shutdown time
 */
53 54 55 56 57
struct hdf5_runtime
{
    struct hdf5_file_runtime* file_runtime_array;
    struct darshan_hdf5_file* file_record_array;
    int file_array_ndx;
Shane Snyder's avatar
Shane Snyder committed
58 59
    struct hdf5_file_runtime *file_hash;
    struct hdf5_file_runtime_ref* hid_hash;
60 61 62 63 64 65 66 67
};

static struct hdf5_runtime *hdf5_runtime = NULL;
static pthread_mutex_t hdf5_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static int instrumentation_disabled = 0;
static int my_rank = -1;

static void hdf5_runtime_initialize(void);
Shane Snyder's avatar
Shane Snyder committed
68 69 70 71 72 73 74
static struct hdf5_file_runtime* hdf5_file_by_name(const char *name);
static struct hdf5_file_runtime* hdf5_file_by_name_sethid(const char* name, hid_t hid);
static struct hdf5_file_runtime* hdf5_file_by_hid(hid_t hid);
static void hdf5_file_close_hid(hid_t hid);
static int hdf5_record_compare(const void* a, const void* b);
static void hdf5_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype);
75 76

static void hdf5_begin_shutdown(void);
Shane Snyder's avatar
Shane Snyder committed
77
static void hdf5_get_output_data(MPI_Comm mod_comm, darshan_record_id *shared_recs,
78 79 80 81 82 83 84 85 86 87 88 89 90 91
    int shared_rec_count, void **hdf5_buf, int *hdf5_buf_sz);
static void hdf5_shutdown(void);

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

/*********************************************************
 *        Wrappers for HDF5 functions of interest        * 
 *********************************************************/

hid_t DARSHAN_DECL(H5Fcreate)(const char *filename, unsigned flags,
    hid_t create_plist, hid_t access_plist)
{
    int ret;
Shane Snyder's avatar
Shane Snyder committed
92
    struct hdf5_file_runtime* file;
93 94 95 96 97
    char* tmp;
    double tm1;

    MAP_OR_FAIL(H5Fcreate);

Shane Snyder's avatar
Shane Snyder committed
98
    tm1 = darshan_core_wtime();
99 100 101 102 103 104 105 106 107 108 109 110 111
    ret = __real_H5Fcreate(filename, flags, create_plist, access_plist);
    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;
        }

Shane Snyder's avatar
Shane Snyder committed
112 113 114
        HDF5_LOCK();
        hdf5_runtime_initialize();
        file = hdf5_file_by_name_sethid(filename, ret);
115 116
        if(file)
        {
Shane Snyder's avatar
Shane Snyder committed
117 118 119
            if(file->file_record->fcounters[HDF5_F_OPEN_TIMESTAMP] == 0)
                file->file_record->fcounters[HDF5_F_OPEN_TIMESTAMP] = tm1;
            file->file_record->counters[HDF5_OPENS] += 1;
120
        }
Shane Snyder's avatar
Shane Snyder committed
121
        HDF5_UNLOCK();
122 123 124 125 126 127 128 129 130
    }

    return(ret);
}

hid_t DARSHAN_DECL(H5Fopen)(const char *filename, unsigned flags,
    hid_t access_plist)
{
    int ret;
Shane Snyder's avatar
Shane Snyder committed
131
    struct hdf5_file_runtime* file;
132 133 134 135 136
    char* tmp;
    double tm1;

    MAP_OR_FAIL(H5Fopen);

Shane Snyder's avatar
Shane Snyder committed
137
    tm1 = darshan_core_wtime();
138 139 140 141 142 143 144 145 146 147 148 149 150
    ret = __real_H5Fopen(filename, flags, access_plist);
    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;
        }

Shane Snyder's avatar
Shane Snyder committed
151 152 153
        HDF5_LOCK();
        hdf5_runtime_initialize();
        file = hdf5_file_by_name_sethid(filename, ret);
154 155
        if(file)
        {
Shane Snyder's avatar
Shane Snyder committed
156 157 158
            if(file->file_record->fcounters[HDF5_F_OPEN_TIMESTAMP] == 0)
                file->file_record->fcounters[HDF5_F_OPEN_TIMESTAMP] = tm1;
            file->file_record->counters[HDF5_OPENS] += 1;
159
        }
Shane Snyder's avatar
Shane Snyder committed
160
        HDF5_UNLOCK();
161 162 163 164 165 166 167 168
    }

    return(ret);

}

herr_t DARSHAN_DECL(H5Fclose)(hid_t file_id)
{
Shane Snyder's avatar
Shane Snyder committed
169
    struct hdf5_file_runtime* file;
170 171 172 173 174 175
    int ret;

    MAP_OR_FAIL(H5Fclose);

    ret = __real_H5Fclose(file_id);

Shane Snyder's avatar
Shane Snyder committed
176 177 178
    HDF5_LOCK();
    hdf5_runtime_initialize();
    file = hdf5_file_by_hid(file_id);
179 180
    if(file)
    {
Shane Snyder's avatar
Shane Snyder committed
181 182 183
        file->file_record->fcounters[HDF5_F_CLOSE_TIMESTAMP] =
            darshan_core_wtime();
        hdf5_file_close_hid(file_id);
184
    }
Shane Snyder's avatar
Shane Snyder committed
185
    HDF5_UNLOCK();
186 187 188 189 190 191 192 193 194

    return(ret);

}

/*********************************************************
 * Internal functions for manipulating HDF5 module state *
 *********************************************************/

Shane Snyder's avatar
Shane Snyder committed
195 196 197 198 199 200 201 202 203
/* initialize internal HDF5 module data strucutres and register with darshan-core */
static void hdf5_runtime_initialize()
{
    struct darshan_module_funcs hdf5_mod_fns =
    {
        .begin_shutdown = &hdf5_begin_shutdown,
        .get_output_data = &hdf5_get_output_data,
        .shutdown = &hdf5_shutdown
    };
204 205 206
    void *hdf5_buf;
    int hdf5_buf_size;
    int file_array_size;
Shane Snyder's avatar
Shane Snyder committed
207 208 209 210 211

    /* don't do anything if already initialized or instrumenation is disabled */
    if(hdf5_runtime || instrumentation_disabled)
        return;

212 213 214
    /* 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
215 216 217 218
    /* register hdf5 module with darshan-core */
    darshan_core_register_module(
        DARSHAN_HDF5_MOD,
        &hdf5_mod_fns,
219 220
        &hdf5_buf_size,
        &hdf5_buf,
Shane Snyder's avatar
Shane Snyder committed
221 222 223
        &my_rank,
        NULL);

224 225
    /* return if darshan-core does not provide enough module memory */
    if(hdf5_buf_size < sizeof(struct darshan_hdf5_file))
226 227
    {
        darshan_core_unregister_module(DARSHAN_HDF5_MOD);
Shane Snyder's avatar
Shane Snyder committed
228
        return;
229
    }
Shane Snyder's avatar
Shane Snyder committed
230 231 232

    hdf5_runtime = malloc(sizeof(*hdf5_runtime));
    if(!hdf5_runtime)
233 234
    {
        darshan_core_unregister_module(DARSHAN_HDF5_MOD);
Shane Snyder's avatar
Shane Snyder committed
235
        return;
236
    }
Shane Snyder's avatar
Shane Snyder committed
237 238
    memset(hdf5_runtime, 0, sizeof(*hdf5_runtime));

239 240 241 242
    /* set number of trackable files for the HDF5 module according to the
     * amount of memory returned by darshan-core
     */
    file_array_size = hdf5_buf_size / sizeof(struct darshan_hdf5_file);
Shane Snyder's avatar
Shane Snyder committed
243 244
    hdf5_runtime->file_array_ndx = 0;

245 246 247
    /* store pointer to HDF5 record buffer given by darshan-core */
    hdf5_runtime->file_record_array = (struct darshan_hdf5_file *)hdf5_buf;

Shane Snyder's avatar
Shane Snyder committed
248
    /* allocate array of runtime file records */
249
    hdf5_runtime->file_runtime_array = malloc(file_array_size *
Shane Snyder's avatar
Shane Snyder committed
250
                                              sizeof(struct hdf5_file_runtime));
251
    if(!hdf5_runtime->file_runtime_array)
Shane Snyder's avatar
Shane Snyder committed
252
    {
253 254 255
        free(hdf5_runtime);
        hdf5_runtime = NULL;
        darshan_core_unregister_module(DARSHAN_HDF5_MOD);
Shane Snyder's avatar
Shane Snyder committed
256 257
        return;
    }
258
    memset(hdf5_runtime->file_runtime_array, 0, file_array_size *
Shane Snyder's avatar
Shane Snyder committed
259 260 261 262 263 264 265 266 267
           sizeof(struct hdf5_file_runtime));

    return;
}

/* get a HDF5 file record for the given file path */
static struct hdf5_file_runtime* hdf5_file_by_name(const char *name)
{
    struct hdf5_file_runtime *file = NULL;
268
    struct darshan_hdf5_file *file_rec;
Shane Snyder's avatar
Shane Snyder committed
269 270
    char *newname = NULL;
    darshan_record_id file_id;
271
    int ret;
Shane Snyder's avatar
Shane Snyder committed
272 273 274 275 276 277 278 279

    if(!hdf5_runtime || instrumentation_disabled)
        return(NULL);

    newname = darshan_clean_file_path(name);
    if(!newname)
        newname = (char*)name;

280 281
    /* lookup the unique id for this filename */
    darshan_core_lookup_record(
282
        newname,
283
        &file_id);
284

Shane Snyder's avatar
Shane Snyder committed
285 286
    /* search the hash table for this file record, and return if found */
    HASH_FIND(hlink, hdf5_runtime->file_hash, &file_id, sizeof(darshan_record_id), file);
287
    if(!file)
Shane Snyder's avatar
Shane Snyder committed
288
    {
289
        /* register the record with the darshan core component */
290
        ret = darshan_core_register_record(file_id, newname, DARSHAN_HDF5_MOD,
291 292 293 294 295 296 297 298
            sizeof(struct darshan_hdf5_file), NULL);
        if(ret == 1)
        {
            /* register was successful */
            file = &(hdf5_runtime->file_runtime_array[hdf5_runtime->file_array_ndx]);
            file->file_record =
                &(hdf5_runtime->file_record_array[hdf5_runtime->file_array_ndx]);
            file_rec = file->file_record;
Shane Snyder's avatar
Shane Snyder committed
299

300 301
            file_rec->base_rec.id = file_id;
            file_rec->base_rec.rank = my_rank;
Shane Snyder's avatar
Shane Snyder committed
302

303 304 305 306 307 308
            /* add new record to file hash table */
            HASH_ADD(hlink, hdf5_runtime->file_hash, file_record->base_rec.id,
                sizeof(darshan_record_id), file);
            hdf5_runtime->file_array_ndx++;
        }
    }
Shane Snyder's avatar
Shane Snyder committed
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

    if(newname != name)
        free(newname);
    return(file);
}

/* get a HDF5 file record for the given file path, and also create a
 * reference structure using the returned hid
 */
static struct hdf5_file_runtime* hdf5_file_by_name_sethid(const char* name, hid_t hid)
{
    struct hdf5_file_runtime* file;
    struct hdf5_file_runtime_ref* ref;

    if(!hdf5_runtime || instrumentation_disabled)
        return(NULL);

    /* find file record by name first */
    file = hdf5_file_by_name(name);

    if(!file)
        return(NULL);

    /* search hash table for existing file ref for this fd */
    HASH_FIND(hlink, hdf5_runtime->hid_hash, &hid, sizeof(hid_t), ref);
    if(ref)
    {
        /* we have a reference.  Make sure it points to the correct file
         * and return it
         */
        ref->file = file;
        return(file);
    }

    /* if we hit this point, then we don't have a reference for this fd
     * in the table yet.  Add it.
     */
    ref = malloc(sizeof(*ref));
    if(!ref)
        return(NULL);
    memset(ref, 0, sizeof(*ref));

    ref->file = file;
    ref->hid = hid;
    HASH_ADD(hlink, hdf5_runtime->hid_hash, hid, sizeof(hid_t), ref);

    return(file);
}

/* get a HDF5 file record for the given hid */
static struct hdf5_file_runtime* hdf5_file_by_hid(hid_t hid)
{
    struct hdf5_file_runtime_ref* ref;

    if(!hdf5_runtime || instrumentation_disabled)
        return(NULL);

    /* search hash table for existing file ref for this hid */
    HASH_FIND(hlink, hdf5_runtime->hid_hash, &hid, sizeof(hid_t), ref);
    if(ref)
        return(ref->file);

    return(NULL);
}

/* free up HDF5 reference data structures for the given hid */
static void hdf5_file_close_hid(hid_t hid)
{
    struct hdf5_file_runtime_ref* ref;

    if(!hdf5_runtime || instrumentation_disabled)
        return;

    /* search hash table for this hid */
    HASH_FIND(hlink, hdf5_runtime->hid_hash, &hid, sizeof(hid_t), ref);
    if(ref)
    {
        /* we have a reference, delete it */
        HASH_DELETE(hlink, hdf5_runtime->hid_hash, ref);
        free(ref);
    }

    return;
}

/* compare function for sorting file records by descending rank */
static int hdf5_record_compare(const void* a_p, const void* b_p)
{
    const struct darshan_hdf5_file* a = a_p;
    const struct darshan_hdf5_file* b = b_p;

400
    if(a->base_rec.rank < b->base_rec.rank)
Shane Snyder's avatar
Shane Snyder committed
401
        return 1;
402
    if(a->base_rec.rank > b->base_rec.rank)
Shane Snyder's avatar
Shane Snyder committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
        return -1;

    return 0;
}

static void hdf5_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype)
{
    struct darshan_hdf5_file tmp_file;
    struct darshan_hdf5_file *infile = infile_v;
    struct darshan_hdf5_file *inoutfile = inoutfile_v;
    int i, j;

    assert(hdf5_runtime);

    for(i=0; i<*len; i++)
    {
        memset(&tmp_file, 0, sizeof(struct darshan_hdf5_file));
421 422
        tmp_file.base_rec.id = infile->base_rec.id;
        tmp_file.base_rec.rank = -1;
Shane Snyder's avatar
Shane Snyder committed
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

        /* sum */
        for(j=HDF5_OPENS; j<=HDF5_OPENS; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + inoutfile->counters[j];
        }

        /* min non-zero (if available) value */
        for(j=HDF5_F_OPEN_TIMESTAMP; j<=HDF5_F_OPEN_TIMESTAMP; j++)
        {
            if(infile->fcounters[j] > inoutfile->fcounters[j] && inoutfile->fcounters[j] > 0)
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
            else
                tmp_file.fcounters[j] = infile->fcounters[j];
        }

        /* max */
        for(j=HDF5_F_CLOSE_TIMESTAMP; j<=HDF5_F_CLOSE_TIMESTAMP; j++)
        {
            if(infile->fcounters[j] > inoutfile->fcounters[j])
                tmp_file.fcounters[j] = infile->fcounters[j];
            else
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
        }

        /* update pointers */
        *inoutfile = tmp_file;
        inoutfile++;
        infile++;
    }

    return;
}

/************************************************************************
 * Functions exported by HDF5 module for coordinating with darshan-core *
 ************************************************************************/

static void hdf5_begin_shutdown()
{
    assert(hdf5_runtime);

    HDF5_LOCK();
    /* disable further instrumentation while Darshan shuts down */
    instrumentation_disabled = 1;
    HDF5_UNLOCK();

    return;
}

static void hdf5_get_output_data(
    MPI_Comm mod_comm,
    darshan_record_id *shared_recs,
    int shared_rec_count,
    void **hdf5_buf,
    int *hdf5_buf_sz)
{
    struct hdf5_file_runtime *file;
    int i;
    struct darshan_hdf5_file *red_send_buf = NULL;
    struct darshan_hdf5_file *red_recv_buf = NULL;
    MPI_Datatype red_type;
    MPI_Op red_op;

    assert(hdf5_runtime);

    /* if there are globally shared files, do a shared file reduction */
490 491 492 493
    /* NOTE: the shared file reduction is also skipped if the 
     * DARSHAN_DISABLE_SHARED_REDUCTION environment variable is set.
     */
    if(shared_rec_count && !getenv("DARSHAN_DISABLE_SHARED_REDUCTION"))
Shane Snyder's avatar
Shane Snyder committed
494 495 496 497 498 499 500 501
    {
        /* necessary initialization of shared records */
        for(i = 0; i < shared_rec_count; i++)
        {
            HASH_FIND(hlink, hdf5_runtime->file_hash, &shared_recs[i],
                sizeof(darshan_record_id), file);
            assert(file);

502
            file->file_record->base_rec.rank = -1;
Shane Snyder's avatar
Shane Snyder committed
503 504 505 506 507 508 509 510 511 512 513
        }

        /* sort the array of files descending by rank so that we get all of the 
         * shared files (marked by rank -1) in a contiguous portion at end 
         * of the array
         */
        qsort(hdf5_runtime->file_record_array, hdf5_runtime->file_array_ndx,
            sizeof(struct darshan_hdf5_file), hdf5_record_compare);

        /* make *send_buf point to the shared files at the end of sorted array */
        red_send_buf =
514
            &(hdf5_runtime->file_record_array[hdf5_runtime->file_array_ndx-shared_rec_count]);
Shane Snyder's avatar
Shane Snyder committed
515 516 517 518 519 520

        /* allocate memory for the reduction output on rank 0 */
        if(my_rank == 0)
        {
            red_recv_buf = malloc(shared_rec_count * sizeof(struct darshan_hdf5_file));
            if(!red_recv_buf)
521
            {
Shane Snyder's avatar
Shane Snyder committed
522
                return;
523
            }
Shane Snyder's avatar
Shane Snyder committed
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
        }

        /* construct a datatype for a HDF5 file record.  This is serving no purpose
         * except to make sure we can do a reduction on proper boundaries
         */
        DARSHAN_MPI_CALL(PMPI_Type_contiguous)(sizeof(struct darshan_hdf5_file),
            MPI_BYTE, &red_type);
        DARSHAN_MPI_CALL(PMPI_Type_commit)(&red_type);

        /* register a HDF5 file record reduction operator */
        DARSHAN_MPI_CALL(PMPI_Op_create)(hdf5_record_reduction_op, 1, &red_op);

        /* reduce shared HDF5 file records */
        DARSHAN_MPI_CALL(PMPI_Reduce)(red_send_buf, red_recv_buf,
            shared_rec_count, red_type, red_op, 0, mod_comm);

        /* clean up reduction state */
        if(my_rank == 0)
        {
            int tmp_ndx = hdf5_runtime->file_array_ndx - shared_rec_count;
            memcpy(&(hdf5_runtime->file_record_array[tmp_ndx]), red_recv_buf,
                shared_rec_count * sizeof(struct darshan_hdf5_file));
            free(red_recv_buf);
        }
        else
        {
            hdf5_runtime->file_array_ndx -= shared_rec_count;
        }

        DARSHAN_MPI_CALL(PMPI_Type_free)(&red_type);
        DARSHAN_MPI_CALL(PMPI_Op_free)(&red_op);
    }

    *hdf5_buf = (void *)(hdf5_runtime->file_record_array);
    *hdf5_buf_sz = hdf5_runtime->file_array_ndx * sizeof(struct darshan_hdf5_file);

    return;
}

static void hdf5_shutdown()
{
    struct hdf5_file_runtime_ref *ref, *tmp;

    assert(hdf5_runtime);

    HASH_ITER(hlink, hdf5_runtime->hid_hash, ref, tmp)
    {
        HASH_DELETE(hlink, hdf5_runtime->hid_hash, ref);
        free(ref);
    }

    HASH_CLEAR(hlink, hdf5_runtime->file_hash); /* these entries are freed all at once below */

    free(hdf5_runtime->file_runtime_array);
    free(hdf5_runtime);
    hdf5_runtime = NULL;

    return;
}
583 584 585 586 587 588 589 590 591

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */