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 58
struct hdf5_runtime
{
    struct hdf5_file_runtime* file_runtime_array;
    struct darshan_hdf5_file* file_record_array;
    int file_array_size;
    int file_array_ndx;
Shane Snyder's avatar
Shane Snyder committed
59 60
    struct hdf5_file_runtime *file_hash;
    struct hdf5_file_runtime_ref* hid_hash;
61 62 63 64 65 66 67 68
};

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
69 70 71 72 73 74 75
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);
76 77

static void hdf5_begin_shutdown(void);
Shane Snyder's avatar
Shane Snyder committed
78
static void hdf5_get_output_data(MPI_Comm mod_comm, darshan_record_id *shared_recs,
79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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
93
    struct hdf5_file_runtime* file;
94 95 96 97 98
    char* tmp;
    double tm1;

    MAP_OR_FAIL(H5Fcreate);

Shane Snyder's avatar
Shane Snyder committed
99
    tm1 = darshan_core_wtime();
100 101 102 103 104 105 106 107 108 109 110 111 112
    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
113 114 115
        HDF5_LOCK();
        hdf5_runtime_initialize();
        file = hdf5_file_by_name_sethid(filename, ret);
116 117
        if(file)
        {
Shane Snyder's avatar
Shane Snyder committed
118 119 120
            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;
121
        }
Shane Snyder's avatar
Shane Snyder committed
122
        HDF5_UNLOCK();
123 124 125 126 127 128 129 130 131
    }

    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
132
    struct hdf5_file_runtime* file;
133 134 135 136 137
    char* tmp;
    double tm1;

    MAP_OR_FAIL(H5Fopen);

Shane Snyder's avatar
Shane Snyder committed
138
    tm1 = darshan_core_wtime();
139 140 141 142 143 144 145 146 147 148 149 150 151
    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
152 153 154
        HDF5_LOCK();
        hdf5_runtime_initialize();
        file = hdf5_file_by_name_sethid(filename, ret);
155 156
        if(file)
        {
Shane Snyder's avatar
Shane Snyder committed
157 158 159
            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;
160
        }
Shane Snyder's avatar
Shane Snyder committed
161
        HDF5_UNLOCK();
162 163 164 165 166 167 168 169
    }

    return(ret);

}

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

    MAP_OR_FAIL(H5Fclose);

    ret = __real_H5Fclose(file_id);

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

    return(ret);

}

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

Shane Snyder's avatar
Shane Snyder committed
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
/* initialize internal HDF5 module data strucutres and register with darshan-core */
static void hdf5_runtime_initialize()
{
    int mem_limit;
    struct darshan_module_funcs hdf5_mod_fns =
    {
        .begin_shutdown = &hdf5_begin_shutdown,
        .get_output_data = &hdf5_get_output_data,
        .shutdown = &hdf5_shutdown
    };

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

    /* register hdf5 module with darshan-core */
    darshan_core_register_module(
        DARSHAN_HDF5_MOD,
        &hdf5_mod_fns,
        &my_rank,
        &mem_limit,
        NULL);

    /* return if no memory assigned by darshan-core */
    if(mem_limit == 0)
        return;

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

    /* set maximum number of file records according to max memory limit */
    /* NOTE: maximum number of records is based on the size of a hdf5 file record */
    /* TODO: should we base memory usage off file record or total runtime structure sizes? */
    hdf5_runtime->file_array_size = mem_limit / sizeof(struct darshan_hdf5_file);
    hdf5_runtime->file_array_ndx = 0;

    /* allocate array of runtime file records */
    hdf5_runtime->file_runtime_array = malloc(hdf5_runtime->file_array_size *
                                              sizeof(struct hdf5_file_runtime));
    hdf5_runtime->file_record_array = malloc(hdf5_runtime->file_array_size *
                                             sizeof(struct darshan_hdf5_file));
    if(!hdf5_runtime->file_runtime_array || !hdf5_runtime->file_record_array)
    {
        hdf5_runtime->file_array_size = 0;
        return;
    }
    memset(hdf5_runtime->file_runtime_array, 0, hdf5_runtime->file_array_size *
           sizeof(struct hdf5_file_runtime));
    memset(hdf5_runtime->file_record_array, 0, hdf5_runtime->file_array_size *
           sizeof(struct darshan_hdf5_file));

    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;
    char *newname = NULL;
    darshan_record_id file_id;
258
    int limit_flag;
Shane Snyder's avatar
Shane Snyder committed
259 260 261 262 263 264 265 266

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

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

267 268
    limit_flag = (hdf5_runtime->file_array_ndx >= hdf5_runtime->file_array_size);

Shane Snyder's avatar
Shane Snyder committed
269 270 271 272 273
    /* get a unique id for this file from darshan core */
    darshan_core_register_record(
        (void*)newname,
        strlen(newname),
        DARSHAN_HDF5_MOD,
274 275
        1,
        limit_flag,
Shane Snyder's avatar
Shane Snyder committed
276 277 278
        &file_id,
        NULL);

279 280 281 282 283 284 285 286 287 288
    /* if record is set to 0, darshan-core is out of space and will not
     * track this record, so we should avoid tracking it, too
     */
    if(file_id == 0)
    {
        if(newname != name)
            free(newname);
        return(NULL);
    }

Shane Snyder's avatar
Shane Snyder committed
289 290 291 292 293 294 295 296 297
    /* 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);
    if(file)
    {
        if(newname != name)
            free(newname);
        return(file);
    }

298 299 300 301 302
    /* no existing record, assign a new file record from the global array */
    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->file_record->f_id = file_id;
    file->file_record->rank = my_rank;
Shane Snyder's avatar
Shane Snyder committed
303

304 305 306
    /* add new record to file hash table */
    HASH_ADD(hlink, hdf5_runtime->file_hash, file_record->f_id, sizeof(darshan_record_id), file);
    hdf5_runtime->file_array_ndx++;
Shane Snyder's avatar
Shane Snyder committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

    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;

    if(a->rank < b->rank)
        return 1;
    if(a->rank > b->rank)
        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));
        tmp_file.f_id = infile->f_id;
        tmp_file.rank = -1;

        /* 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 */
488 489 490 491
    /* 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
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    {
        /* 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);

            file->file_record->rank = -1;
        }

        /* 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 =
512
            &(hdf5_runtime->file_record_array[hdf5_runtime->file_array_ndx-shared_rec_count]);
Shane Snyder's avatar
Shane Snyder committed
513 514 515 516 517 518

        /* 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)
519
            {
Shane Snyder's avatar
Shane Snyder committed
520
                return;
521
            }
Shane Snyder's avatar
Shane Snyder committed
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
        }

        /* 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->file_record_array);
    free(hdf5_runtime);
    hdf5_runtime = NULL;

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

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