darshan-posix.c 17.2 KB
Newer Older
1 2 3 4 5
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

6
#include "darshan-runtime-config.h"
7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <search.h>
#include <assert.h>
21
#include <libgen.h>
22
#include <aio.h>
23
#define __USE_GNU
24
#include <pthread.h>
25

26
#include "uthash.h"
27
#include "darshan.h"
28
#include "darshan-posix-log-format.h"
29

30
#ifndef HAVE_OFF64_T
31 32
typedef int64_t off64_t;
#endif
33 34 35
#ifndef HAVE_AIOCB64
#define aiocb64 aiocb
#endif
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifdef DARSHAN_PRELOAD
#define __USE_GNU
#include <dlfcn.h>
#include <stdlib.h>

#define DARSHAN_FORWARD_DECL(name,ret,args) \
  ret (*__real_ ## name)args = NULL;

#define DARSHAN_DECL(__name) __name

#define DARSHAN_MPI_CALL(func) __real_ ## func

#define MAP_OR_FAIL(func) \
    if (!(__real_ ## func)) \
    { \
        __real_ ## func = dlsym(RTLD_NEXT, #func); \
        if(!(__real_ ## func)) { \
           fprintf(stderr, "Darshan failed to map symbol: %s\n", #func); \
           exit(1); \
       } \
    }

#else
60

61 62 63 64 65
#define DARSHAN_FORWARD_DECL(name,ret,args) \
  extern ret __real_ ## name args;

#define DARSHAN_DECL(__name) __wrap_ ## __name

66 67
#define DARSHAN_MPI_CALL(func) func

68 69
#define MAP_OR_FAIL(func)

70 71 72 73 74 75
#endif

DARSHAN_FORWARD_DECL(open, int, (const char *path, int flags, ...));
DARSHAN_FORWARD_DECL(open64, int, (const char *path, int flags, ...));
DARSHAN_FORWARD_DECL(close, int, (int fd));

76
struct posix_runtime_file
77
{
78
    struct darshan_posix_file* file_record;
79
    UT_hash_handle hlink;
80
};
81

82
struct posix_runtime_file_ref
83
{
84 85 86 87 88 89 90
    struct posix_runtime_file* file;
    int fd;
    UT_hash_handle hlink;
};

struct posix_runtime
{
91 92
    struct posix_runtime_file* file_runtime_array;
    struct darshan_posix_file* file_record_array;
93
    int file_array_size;
94
    int file_array_ndx;
95 96
    struct posix_runtime_file* file_hash;
    struct posix_runtime_file_ref* fd_hash;
97 98
    void *red_buf;
    int shared_rec_count;
99 100
};

101
static struct posix_runtime *posix_runtime = NULL;
102
static pthread_mutex_t posix_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Shane Snyder's avatar
Shane Snyder committed
103
static int instrumentation_disabled = 0;
104
static int my_rank = -1;
105

106
/* TODO: I'm sure these should be applied on all modules */
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* these are paths that we will not trace */
static char* exclusions[] = {
"/etc/",
"/dev/",
"/usr/",
"/bin/",
"/boot/",
"/lib/",
"/opt/",
"/sbin/",
"/sys/",
"/proc/",
NULL
};

122
static void posix_runtime_initialize(void);
123 124
static struct posix_runtime_file* posix_file_by_name(const char *name);
static struct posix_runtime_file* posix_file_by_name_setfd(const char* name, int fd);
Shane Snyder's avatar
Shane Snyder committed
125
static struct posix_runtime_file* posix_file_by_fd(int fd);
126 127
static void posix_file_close_fd(int fd);

Shane Snyder's avatar
Shane Snyder committed
128
static void posix_disable_instrumentation(void);
129 130
static void posix_prepare_for_reduction(darshan_record_id *shared_recs,
    int *shared_rec_count, void **send_buf, void **recv_buf, int *rec_size);
Shane Snyder's avatar
Shane Snyder committed
131
static void posix_reduce_records(void* infile_v, void* inoutfile_v,
132 133
    int *len, MPI_Datatype *datatype);
static void posix_get_output_data(void **buffer, int *size);
134
static void posix_shutdown(void);
135

136 137
#define POSIX_LOCK() pthread_mutex_lock(&posix_runtime_mutex)
#define POSIX_UNLOCK() pthread_mutex_unlock(&posix_runtime_mutex)
138

139
#define POSIX_SET(__file, __counter, __value) do {\
140
    (__file)->file_record->counters[__counter] = __value; \
141 142 143
} while(0)

#define POSIX_F_SET(__file, __counter, __value) do {\
144
    (__file)->file_record->fcounters[__counter] = __value; \
145 146 147
} while(0)

#define POSIX_INC(__file, __counter, __value) do {\
148
    (__file)->file_record->counters[__counter] += __value; \
149 150 151
} while(0)

#define POSIX_F_INC(__file, __counter, __value) do {\
152
    (__file)->file_record->fcounters[__counter] += __value; \
153 154 155 156 157 158 159 160 161 162 163 164
} while(0)

#define POSIX_F_INC_NO_OVERLAP(__file, __tm1, __tm2, __last, __counter) do { \
    if(__tm1 > __last) \
        POSIX_F_INC(__file, __counter, (__tm2-__tm1)); \
    else \
        POSIX_F_INC(__file, __counter, (__tm2 - __last)); \
    if(__tm2 > __last) \
        __last = __tm2; \
} while(0)

#define POSIX_VALUE(__file, __counter) \
165
    ((__file)->file_record->counters[__counter])
166 167

#define POSIX_F_VALUE(__file, __counter) \
168
    ((__file)->file_record->fcounters[__counter])
169 170

#define POSIX_MAX(__file, __counter, __value) do {\
171
    if((__file)->file_record->counters[__counter] < __value) \
172
    { \
173
        (__file)->file_record->counters[__counter] = __value; \
174 175 176
    } \
} while(0)

177
#define POSIX_RECORD_OPEN(__ret, __path, __mode, __stream_flag, __tm1, __tm2) do { \
178
    struct posix_runtime_file* file; \
179 180 181 182 183 184 185 186 187
    char* exclude; \
    int tmp_index = 0; \
    if(__ret < 0) break; \
    while((exclude = exclusions[tmp_index])) { \
        if(!(strncmp(exclude, __path, strlen(exclude)))) \
            break; \
        tmp_index++; \
    } \
    if(exclude) break; \
188
    file = posix_file_by_name_setfd(__path, __ret); \
189
    if(!file) break; \
190
    file->file_record->rank = my_rank; \
191
    if(__mode) \
192
        POSIX_SET(file, CP_MODE, __mode); \
193
    if(__stream_flag)\
194
        POSIX_INC(file, CP_POSIX_FOPENS, 1); \
195
    else \
196 197 198 199
        POSIX_INC(file, CP_POSIX_OPENS, 1); \
    if(POSIX_F_VALUE(file, CP_F_OPEN_TIMESTAMP) == 0) \
        POSIX_F_SET(file, CP_F_OPEN_TIMESTAMP, __tm1); \
} while(0)
200

201
int DARSHAN_DECL(open)(const char *path, int flags, ...)
202 203 204 205 206
{
    int mode = 0;
    int ret;
    double tm1, tm2;

207 208
    MAP_OR_FAIL(open);

209
    if(flags & O_CREAT) 
210 211 212 213 214 215
    {
        va_list arg;
        va_start(arg, flags);
        mode = va_arg(arg, int);
        va_end(arg);

216
        tm1 = darshan_core_wtime();
217
        ret = __real_open(path, flags, mode);
218
        tm2 = darshan_core_wtime();
219 220 221
    }
    else
    {
222
        tm1 = darshan_core_wtime();
223
        ret = __real_open(path, flags);
224
        tm2 = darshan_core_wtime();
225 226
    }

227 228 229
    POSIX_LOCK();
    posix_runtime_initialize();

230
    POSIX_RECORD_OPEN(ret, path, mode, 0, tm1, tm2);
231

232
    POSIX_UNLOCK();
233 234 235 236

    return(ret);
}

Shane Snyder's avatar
Shane Snyder committed
237 238 239 240 241 242
int DARSHAN_DECL(open64)(const char *path, int flags, ...)
{
    int mode = 0;
    int ret;
    double tm1, tm2;

243
    MAP_OR_FAIL(open64);
Shane Snyder's avatar
Shane Snyder committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    if(flags & O_CREAT)
    {
        va_list arg;
        va_start(arg, flags);
        mode = va_arg(arg, int);
        va_end(arg);

        tm1 = darshan_core_wtime();
        ret = __real_open64(path, flags, mode);
        tm2 = darshan_core_wtime();
    }
    else
    {
        tm1 = darshan_core_wtime();
        ret = __real_open64(path, flags);
        tm2 = darshan_core_wtime();
    }

    POSIX_LOCK();
    posix_runtime_initialize();

    POSIX_RECORD_OPEN(ret, path, mode, 0, tm1, tm2);
    POSIX_UNLOCK();

    return(ret);
}

272 273
int DARSHAN_DECL(close)(int fd)
{
Shane Snyder's avatar
Shane Snyder committed
274
    struct posix_runtime_file* file;
275 276 277 278 279 280 281 282 283 284 285
    double tm1, tm2;
    int ret;

    MAP_OR_FAIL(close);

    tm1 = darshan_core_wtime();
    ret = __real_close(fd);
    tm2 = darshan_core_wtime();

    POSIX_LOCK();
    posix_runtime_initialize();
286

Shane Snyder's avatar
Shane Snyder committed
287 288 289 290 291 292
    file = posix_file_by_fd(fd);
    if(file)
    {
        POSIX_F_SET(file, CP_F_CLOSE_TIMESTAMP, darshan_core_wtime());
        posix_file_close_fd(fd);
    }
293 294 295 296 297 298
    POSIX_UNLOCK();    

    return(ret);
}

/* ***************************************************** */
299

300
static void posix_runtime_initialize()
301
{
302 303 304 305 306 307
    char *alignstr;
    int tmpval;
    int ret;
    int mem_limit;
    struct darshan_module_funcs posix_mod_fns =
    {
Shane Snyder's avatar
Shane Snyder committed
308
        .disable_instrumentation = &posix_disable_instrumentation,
309
        .prepare_for_reduction = &posix_prepare_for_reduction,
Shane Snyder's avatar
Shane Snyder committed
310
        .reduce_records = &posix_reduce_records,
311
        .get_output_data = &posix_get_output_data,
312
        .shutdown = &posix_shutdown
313
    };
314

Shane Snyder's avatar
Shane Snyder committed
315 316
    /* don't do anything if already initialized or instrumenation is disabled */
    if(posix_runtime || instrumentation_disabled)
317
        return;
318

319 320
    /* register the posix module with darshan core */
    darshan_core_register_module(
321
        DARSHAN_POSIX_MOD,
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        &posix_mod_fns,
        &mem_limit);

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

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

    /* set maximum number of file records according to max memory limit */
    /* NOTE: maximum number of records is based on the size of a posix file record */
    posix_runtime->file_array_size = mem_limit / sizeof(struct darshan_posix_file);
337
    posix_runtime->file_array_ndx = 0;
338 339

    /* allocate array of runtime file records */
340 341 342 343 344
    posix_runtime->file_runtime_array = malloc(posix_runtime->file_array_size *
                                               sizeof(struct posix_runtime_file));
    posix_runtime->file_record_array = malloc(posix_runtime->file_array_size *
                                              sizeof(struct darshan_posix_file));
    if(!posix_runtime->file_runtime_array || !posix_runtime->file_record_array)
345 346 347 348
    {
        posix_runtime->file_array_size = 0;
        return;
    }
349 350 351 352
    memset(posix_runtime->file_runtime_array, 0, posix_runtime->file_array_size *
           sizeof(struct posix_runtime_file));
    memset(posix_runtime->file_record_array, 0, posix_runtime->file_array_size *
           sizeof(struct darshan_posix_file));
353

Shane Snyder's avatar
Shane Snyder committed
354 355
    /* TODO: can we move this out of here? perhaps register_module returns rank? */
    DARSHAN_MPI_CALL(PMPI_Comm_rank)(MPI_COMM_WORLD, &my_rank);
356

357 358
    return;
}
359

360
static struct posix_runtime_file* posix_file_by_name(const char *name)
361
{
362
    struct posix_runtime_file *file = NULL;
363
    char *newname = NULL;
364
    darshan_record_id file_id;
365

Shane Snyder's avatar
Shane Snyder committed
366
    if(!posix_runtime || instrumentation_disabled)
367
        return(NULL);
368

369
    newname = darshan_clean_file_path(name);
370
    if(!newname)
371
        newname = (char*)name;
372

373
    /* get a unique id for this file from darshan core */
Shane Snyder's avatar
Shane Snyder committed
374
    darshan_core_register_record(
375 376 377
        (void*)newname,
        strlen(newname),
        1,
378
        DARSHAN_POSIX_MOD,
379
        &file_id);
380

381
    /* search the hash table for this file record, and return if found */
382
    HASH_FIND(hlink, posix_runtime->file_hash, &file_id, sizeof(darshan_record_id), file);
383
    if(file)
384
    {
385
        if(newname != name)
386
            free(newname);
387
        return(file);
388 389
    }

390 391 392 393 394 395
    if(posix_runtime->file_array_ndx < posix_runtime->file_array_size);
    {
        /* no existing record, assign a new file record from the global array */
        file = &(posix_runtime->file_runtime_array[posix_runtime->file_array_ndx]);
        file->file_record = &(posix_runtime->file_record_array[posix_runtime->file_array_ndx]);
        file->file_record->f_id = file_id;
396

397
        /* add new record to file hash table */
398
        HASH_ADD(hlink, posix_runtime->file_hash, file_record->f_id, sizeof(darshan_record_id), file);
399

400 401
        posix_runtime->file_array_ndx++;
    }
402 403

    if(newname != name)
404
        free(newname);
405 406 407 408 409 410 411 412
    return(file);
}

static struct posix_runtime_file* posix_file_by_name_setfd(const char* name, int fd)
{
    struct posix_runtime_file* file;
    struct posix_runtime_file_ref* ref;

Shane Snyder's avatar
Shane Snyder committed
413
    if(!posix_runtime || instrumentation_disabled)
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
        return(NULL);

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

    if(!file)
        return(NULL);

    /* search hash table for existing file ref for this fd */
    HASH_FIND(hlink, posix_runtime->fd_hash, &fd, sizeof(int), 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));
Shane Snyder's avatar
Shane Snyder committed
440

441 442 443 444 445 446 447
    ref->file = file;
    ref->fd = fd;    
    HASH_ADD(hlink, posix_runtime->fd_hash, fd, sizeof(int), ref);

    return(file);
}

Shane Snyder's avatar
Shane Snyder committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
static struct posix_runtime_file* posix_file_by_fd(int fd)
{
    struct posix_runtime_file_ref* ref;

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

    /* search hash table for existing file ref for this fd */
    HASH_FIND(hlink, posix_runtime->fd_hash, &fd, sizeof(int), ref);
    if(ref)
        return(ref->file);

    return(NULL);
}

463 464
static void posix_file_close_fd(int fd)
{
465
    struct posix_runtime_file_ref* ref;
466

Shane Snyder's avatar
Shane Snyder committed
467
    if(!posix_runtime || instrumentation_disabled)
468 469 470 471
        return;

    /* search hash table for this fd */
    HASH_FIND(hlink, posix_runtime->fd_hash, &fd, sizeof(int), ref);
472
    if(ref)
473 474 475 476 477 478 479
    {
        /* we have a reference, delete it */
        HASH_DELETE(hlink, posix_runtime->fd_hash, ref);
        free(ref);
    }

    return;
480 481
}

482 483 484 485 486 487 488 489 490 491 492 493 494
static int posix_file_compare(const void* a, const void* b)
{
    const struct darshan_posix_file* f_a = a;
    const struct darshan_posix_file* f_b = b;

    if(f_a->rank < f_b->rank)
        return 1;
    if(f_a->rank > f_b->rank)
        return -1;

    return 0;
}

495 496
/* ***************************************************** */

Shane Snyder's avatar
Shane Snyder committed
497
static void posix_disable_instrumentation()
498
{
499 500
    assert(posix_runtime);

Shane Snyder's avatar
Shane Snyder committed
501 502 503
    POSIX_LOCK();
    instrumentation_disabled = 1;
    POSIX_UNLOCK();
504

Shane Snyder's avatar
Shane Snyder committed
505 506 507
    return;
}

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
static void posix_prepare_for_reduction(
    darshan_record_id *shared_recs,
    int *shared_rec_count,
    void **send_buf,
    void **recv_buf,
    int *rec_size)
{
    struct posix_runtime_file *file;
    struct darshan_posix_file *tmp_array;
    int i;

    assert(posix_runtime);

    /* necessary initialization of shared records (e.g., change rank to -1) */
    for(i = 0; i < *shared_rec_count; i++)
    {
        HASH_FIND(hlink, posix_runtime->file_hash, &shared_recs[i],
            sizeof(darshan_record_id), file);
        assert(file);

        /* TODO: any initialization before reduction */
        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(posix_runtime->file_record_array, posix_runtime->file_array_ndx,
        sizeof(struct darshan_posix_file), posix_file_compare);

    /* make *send_buf point to the shared files at the end of sorted array */
    *send_buf =
        &(posix_runtime->file_record_array[posix_runtime->file_array_ndx-(*shared_rec_count)]);

    /* allocate memory for the reduction output on rank 0 */
    if(my_rank == 0)
    {
        *recv_buf = malloc(*shared_rec_count * sizeof(struct darshan_posix_file));
        if(!(*recv_buf))
            return;
    }

    *rec_size = sizeof(struct darshan_posix_file);

Shane Snyder's avatar
Shane Snyder committed
553
    /* TODO: cleaner way to do this? */
554 555 556 557 558 559 560
    if(my_rank == 0)
        posix_runtime->red_buf = *recv_buf;
    posix_runtime->shared_rec_count = *shared_rec_count;

    return;
}

Shane Snyder's avatar
Shane Snyder committed
561
static void posix_reduce_records(
562 563 564 565
    void* infile_v,
    void* inoutfile_v,
    int *len,
    MPI_Datatype *datatype)
Shane Snyder's avatar
Shane Snyder committed
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
    struct darshan_posix_file tmp_file;
    struct darshan_posix_file *infile = infile_v;
    struct darshan_posix_file *inoutfile = inoutfile_v;
    int i;

    assert(posix_runtime);

    for(i = 0; i < *len; i++)
    {
        memset(&tmp_file, 0, sizeof(struct darshan_posix_file));

        tmp_file.f_id = infile->f_id;
        tmp_file.rank = -1;

        tmp_file.counters[CP_POSIX_OPENS] = infile->counters[CP_POSIX_OPENS] +
            inoutfile->counters[CP_POSIX_OPENS];

        if((infile->fcounters[CP_F_OPEN_TIMESTAMP] > inoutfile->fcounters[CP_F_OPEN_TIMESTAMP]) &&
            (inoutfile->fcounters[CP_F_OPEN_TIMESTAMP] > 0))
            tmp_file.fcounters[CP_F_OPEN_TIMESTAMP] = inoutfile->fcounters[CP_F_OPEN_TIMESTAMP];
        else
            tmp_file.fcounters[CP_F_OPEN_TIMESTAMP] = infile->fcounters[CP_F_OPEN_TIMESTAMP];

        if(infile->fcounters[CP_F_CLOSE_TIMESTAMP] > inoutfile->fcounters[CP_F_CLOSE_TIMESTAMP])
            tmp_file.fcounters[CP_F_CLOSE_TIMESTAMP] = infile->fcounters[CP_F_CLOSE_TIMESTAMP];
        else
            tmp_file.fcounters[CP_F_CLOSE_TIMESTAMP] = inoutfile->fcounters[CP_F_CLOSE_TIMESTAMP];

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

    return;
}

static void posix_get_output_data(
    void **buffer,
    int *size)
{
    assert(posix_runtime);

Shane Snyder's avatar
Shane Snyder committed
610
    /* TODO: cleaner way to do this? */
611 612 613 614 615 616 617 618 619 620 621 622
    /* clean up reduction state */
    if(my_rank == 0)
    {
        int tmp_ndx = posix_runtime->file_array_ndx - posix_runtime->shared_rec_count;
        memcpy(&(posix_runtime->file_record_array[tmp_ndx]), posix_runtime->red_buf,
            posix_runtime->shared_rec_count * sizeof(struct darshan_posix_file));
        free(posix_runtime->red_buf);
    }
    else
    {
        posix_runtime->file_array_ndx -= posix_runtime->shared_rec_count;
    }
623 624 625

    *buffer = (void *)(posix_runtime->file_record_array);
    *size = posix_runtime->file_array_ndx * sizeof(struct darshan_posix_file);
626

627
    return;
628 629
}

630
static void posix_shutdown()
631
{
632 633 634 635 636 637 638
    struct posix_runtime_file_ref *ref, *tmp;

    HASH_ITER(hlink, posix_runtime->fd_hash, ref, tmp)
    {
        HASH_DELETE(hlink, posix_runtime->fd_hash, ref);
        free(ref);
    }
639

640
    HASH_CLEAR(hlink, posix_runtime->file_hash); /* these entries are freed all at once below */
641 642 643 644 645

    free(posix_runtime->file_runtime_array);
    free(posix_runtime->file_record_array);
    free(posix_runtime);
    posix_runtime = NULL;
646 647

    return;
648 649
}

650 651 652 653 654 655 656 657
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */