darshan-stdio.c 15.6 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 35
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#define _XOPEN_SOURCE 500
#define _GNU_SOURCE

#include "darshan-runtime-config.h"
#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>
#include <libgen.h>
#include <pthread.h>

#include "uthash.h"
#include "utlist.h"

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

DARSHAN_FORWARD_DECL(fopen, FILE*, (const char *path, const char *mode));
DARSHAN_FORWARD_DECL(fopen64, FILE*, (const char *path, const char *mode));
36
DARSHAN_FORWARD_DECL(fclose, int, (FILE *fp));
37 38

/* The stdio_file_runtime structure maintains necessary runtime metadata
39
 * for the STDIO file record (darshan_stdio_record structure, defined in
40 41 42 43 44 45 46 47
 * darshan-stdio-log-format.h) pointed to by 'file_record'. This metadata
 * assists with the instrumenting of specific statistics in the file record.
 * 'hlink' is a hash table link structure used to add/remove this record
 * from the hash table of STDIO file records for this process. 
 *
 * RATIONALE: the STDIO module needs to track some stateful, volatile 
 * information about each open file (like the current file offset, most recent 
 * access time, etc.) to aid in instrumentation, but this information can't be
48
 * stored in the darshan_stdio_record struct because we don't want it to appear in
49
 * the final darshan log file.  We therefore associate a stdio_file_runtime
50
 * struct with each darshan_stdio_record struct in order to track this information.
51 52
  *
 * NOTE: There is a one-to-one mapping of stdio_file_runtime structs to
53
 * darshan_stdio_record structs.
54
 *
55
 * NOTE: The stdio_file_runtime struct contains a pointer to a darshan_stdio_record
56
 * struct (see the *file_record member) rather than simply embedding an entire
57
 * darshan_stdio_record struct.  This is done so that all of the darshan_stdio_record
58 59 60 61 62 63
 * structs can be kept contiguous in memory as a single array to simplify
 * reduction, compression, and storage.
 */
struct stdio_file_runtime
{
    /* TODO: make sure we need/want all of these fields */
64
    struct darshan_stdio_record* file_record;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    int64_t offset;
    int64_t last_byte_read;
    int64_t last_byte_written;
    enum darshan_io_type last_io_type;
    double last_meta_end;
    double last_read_end;
    double last_write_end;
    UT_hash_handle hlink;
};

/* The stdio_file_runtime_ref structure is used to associate a STDIO
 * stream with an already existing STDIO file record. This is
 * necessary as many STDIO I/O functions take only an input stream,
 * but STDIO file records are indexed by their full file paths (i.e., darshan
 * record identifiers for STDIO files are created by hashing the file path).
 * In other words, this structure is necessary as it allows us to look up a
 * file record either by a pathname (stdio_file_runtime) or by STDIO stream 
 * (stdio_file_runtime_ref), depending on which parameters are
 * available. This structure includes another hash table link, since separate
 * hashes are maintained for stdio_file_runtime structures and stdio_file_runtime_ref
 * structures.
 *
 * RATIONALE: In theory the FILE* information could be included in the
 * stdio_file_runtime struct rather than in a separate structure here.  The
 * reason we don't do that is because the same file could be opened multiple
 * times by a given process with different stream pointers and thus
 * simulataneously referenced using different stream pointers.  This practice is
 * not common, but we must support it.
 *
 * NOTE: there are potentially multiple stdio_file_runtime_ref structures
 * referring to a single stdio_file_runtime structure.  Most of the time there is
 * only one, however.
 */
struct stdio_file_runtime_ref
{
    struct stdio_file_runtime* file;
    FILE* stream;
    UT_hash_handle hlink;
};

/* The stdio_runtime structure maintains necessary state for storing
 * STDIO file records and for coordinating with darshan-core at 
 * shutdown time.
 */
struct stdio_runtime
{
    struct stdio_file_runtime* file_runtime_array;
112
    struct darshan_stdio_record* file_record_array;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    int file_array_size;
    int file_array_ndx;
    struct stdio_file_runtime* file_hash;
    struct stdio_file_runtime_ref* stream_hash;
};

static struct stdio_runtime *stdio_runtime = NULL;
static pthread_mutex_t stdio_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static int instrumentation_disabled = 0;
static int darshan_mem_alignment = 1;
static int my_rank = -1;

static void stdio_runtime_initialize(void);
static struct stdio_file_runtime* stdio_file_by_name(const char *name);
static struct stdio_file_runtime* stdio_file_by_name_setstream(const char* name, FILE *stream);
static struct stdio_file_runtime* stdio_file_by_stream(FILE* stream);
static void stdio_file_close_stream(FILE *stream);

static void stdio_begin_shutdown(void);
static void stdio_get_output_data(MPI_Comm mod_comm, darshan_record_id *shared_recs,
    int shared_rec_count, void **stdio_buf, int *stdio_buf_sz);
static void stdio_shutdown(void);

#define STDIO_LOCK() pthread_mutex_lock(&stdio_runtime_mutex)
#define STDIO_UNLOCK() pthread_mutex_unlock(&stdio_runtime_mutex)

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/* TODO: remember to clean up stream_flag in posix module */

#define STDIO_RECORD_OPEN(__ret, __path, __tm1, __tm2) do { \
    struct stdio_file_runtime* file; \
    char* exclude; \
    int tmp_index = 0; \
    if(__ret == NULL) break; \
    while((exclude = darshan_path_exclusions[tmp_index])) { \
        if(!(strncmp(exclude, __path, strlen(exclude)))) \
            break; \
        tmp_index++; \
    } \
    if(exclude) break; \
    file = stdio_file_by_name_setstream(__path, __ret); \
    if(!file) break; \
    file->offset = 0; \
    file->last_byte_written = 0; \
    file->last_byte_read = 0; \
    file->file_record->counters[STDIO_FOPENS] += 1; \
    if(file->file_record->fcounters[STDIO_F_OPEN_START_TIMESTAMP] == 0 || \
     file->file_record->fcounters[STDIO_F_OPEN_START_TIMESTAMP] > __tm1) \
        file->file_record->fcounters[STDIO_F_OPEN_START_TIMESTAMP] = __tm1; \
    file->file_record->fcounters[STDIO_F_OPEN_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(file->file_record->fcounters[STDIO_F_META_TIME], __tm1, __tm2, file->last_meta_end); \
} while(0)



167 168 169
FILE* DARSHAN_DECL(fopen)(const char *path, const char *mode)
{
    FILE* ret;
170
    double tm1, tm2;
171 172 173

    MAP_OR_FAIL(fopen);

174
    tm1 = darshan_core_wtime();
175
    ret = __real_fopen(path, mode);
176 177 178 179 180 181
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
    STDIO_UNLOCK();
182 183 184 185 186 187 188

    return(ret);
}

FILE* DARSHAN_DECL(fopen64)(const char *path, const char *mode)
{
    FILE* ret;
Philip Carns's avatar
Philip Carns committed
189
    double tm1, tm2;
190

Philip Carns's avatar
Philip Carns committed
191
    MAP_OR_FAIL(fopen);
192

Philip Carns's avatar
Philip Carns committed
193
    tm1 = darshan_core_wtime();
194
    ret = __real_fopen64(path, mode);
Philip Carns's avatar
Philip Carns committed
195 196 197 198 199 200
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
    STDIO_UNLOCK();
201 202 203 204

    return(ret);
}

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
int DARSHAN_DECL(fclose)(FILE *fp)
{
    struct stdio_file_runtime* file;
    double tm1, tm2;
    int ret;

    MAP_OR_FAIL(fclose);

    tm1 = darshan_core_wtime();
    ret = __real_fclose(fp);
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
    file = stdio_file_by_stream(fp);
    if(file)
    {
        file->last_byte_written = 0;
        file->last_byte_read = 0;
        if(file->file_record->fcounters[STDIO_F_CLOSE_START_TIMESTAMP] == 0 ||
         file->file_record->fcounters[STDIO_F_CLOSE_START_TIMESTAMP] > tm1)
           file->file_record->fcounters[STDIO_F_CLOSE_START_TIMESTAMP] = tm1;
        file->file_record->fcounters[STDIO_F_CLOSE_END_TIMESTAMP] = tm2;
        DARSHAN_TIMER_INC_NO_OVERLAP(
            file->file_record->fcounters[STDIO_F_META_TIME],
            tm1, tm2, file->last_meta_end);
        stdio_file_close_stream(fp);
    }
    STDIO_UNLOCK();

    return(ret);
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/**********************************************************
 * Internal functions for manipulating STDIO module state *
 **********************************************************/

/* initialize internal STDIO module data structures and register with darshan-core */
static void stdio_runtime_initialize()
{
    int mem_limit;
    struct darshan_module_funcs stdio_mod_fns =
    {
        .begin_shutdown = &stdio_begin_shutdown,
        .get_output_data = &stdio_get_output_data,
        .shutdown = &stdio_shutdown
    };

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

    /* register the stdio module with darshan core */
    darshan_core_register_module(
        DARSHAN_STDIO_MOD,
        &stdio_mod_fns,
        &my_rank,
        &mem_limit,
        &darshan_mem_alignment);

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

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

    /* set maximum number of file records according to max memory limit */
    /* NOTE: maximum number of records is based on the size of a stdio file record */
    /* TODO: should we base memory usage off file record or total runtime structure sizes? */
277
    stdio_runtime->file_array_size = mem_limit / sizeof(struct darshan_stdio_record);
278 279 280 281 282 283
    stdio_runtime->file_array_ndx = 0;

    /* allocate array of runtime file records */
    stdio_runtime->file_runtime_array = malloc(stdio_runtime->file_array_size *
                                               sizeof(struct stdio_file_runtime));
    stdio_runtime->file_record_array = malloc(stdio_runtime->file_array_size *
284
                                              sizeof(struct darshan_stdio_record));
285 286 287 288 289 290 291 292
    if(!stdio_runtime->file_runtime_array || !stdio_runtime->file_record_array)
    {
        stdio_runtime->file_array_size = 0;
        return;
    }
    memset(stdio_runtime->file_runtime_array, 0, stdio_runtime->file_array_size *
           sizeof(struct stdio_file_runtime));
    memset(stdio_runtime->file_record_array, 0, stdio_runtime->file_array_size *
293
           sizeof(struct darshan_stdio_record));
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

    return;
}

/* get a STDIO file record for the given file path */
static struct stdio_file_runtime* stdio_file_by_name(const char *name)
{
    struct stdio_file_runtime *file = NULL;
    char *newname = NULL;
    darshan_record_id file_id;
    int file_alignment;
    int limit_flag;

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

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

    limit_flag = (stdio_runtime->file_array_ndx >= stdio_runtime->file_array_size);

    /* get a unique id for this file from darshan core */
    darshan_core_register_record(
        (void*)newname,
        strlen(newname),
        DARSHAN_STDIO_MOD,
        1,
        limit_flag,
        &file_id,
        &file_alignment);

    /* the file record id is set to 0 if no memory is available for tracking
     * new records -- just fall through and ignore this record
     */
    if(file_id == 0)
    {
        if(newname != name)
            free(newname);
        return(NULL);
    }

    /* search the hash table for this file record, and return if found */
    HASH_FIND(hlink, stdio_runtime->file_hash, &file_id, sizeof(darshan_record_id), file);
    if(file)
    {
        if(newname != name)
            free(newname);
        return(file);
    }

    /* no existing record, assign a new file record from the global array */
    file = &(stdio_runtime->file_runtime_array[stdio_runtime->file_array_ndx]);
    file->file_record = &(stdio_runtime->file_record_array[stdio_runtime->file_array_ndx]);
    file->file_record->f_id = file_id;
    file->file_record->rank = my_rank;

    /* add new record to file hash table */
    HASH_ADD(hlink, stdio_runtime->file_hash, file_record->f_id, sizeof(darshan_record_id), file);
    stdio_runtime->file_array_ndx++;

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

/* get a STDIO file record for the given file path, and also create a
 * reference structure using the returned stream
 */
static struct stdio_file_runtime* stdio_file_by_name_setstream(const char* name, FILE *stream)
{
    struct stdio_file_runtime* file;
    struct stdio_file_runtime_ref* ref;

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

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

    if(!file)
        return(NULL);

    /* search hash table for existing file ref for this stream */
    HASH_FIND(hlink, stdio_runtime->stream_hash, &stream, sizeof(FILE*), 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 stream 
     * in the table yet.  Add it.
     */
    ref = malloc(sizeof(*ref));
    if(!ref)
        return(NULL);
    memset(ref, 0, sizeof(*ref));

    ref->file = file;
    ref->stream = stream;    
    HASH_ADD(hlink, stdio_runtime->stream_hash, stream, sizeof(FILE*), ref);

    return(file);
}

/* get a STDIO file record for the given stream */
static struct stdio_file_runtime* stdio_file_by_stream(FILE *stream)
{
    struct stdio_file_runtime_ref* ref;

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

    /* search hash table for existing file ref for this stream */
    HASH_FIND(hlink, stdio_runtime->stream_hash, &stream, sizeof(FILE*), ref);
    if(ref)
        return(ref->file);

    return(NULL);
}

/* free up reference data structures for the given stream */
static void stdio_file_close_stream(FILE *stream)
{
    struct stdio_file_runtime_ref* ref;

    if(!stdio_runtime || instrumentation_disabled)
        return;

    /* search hash table for this stream */
    HASH_FIND(hlink, stdio_runtime->stream_hash, &stream, sizeof(FILE*), ref);
    if(ref)
    {
        /* we have a reference, delete it */
        HASH_DELETE(hlink, stdio_runtime->stream_hash, ref);
        free(ref);
    }

    return;
}

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

static void stdio_begin_shutdown()
{
    assert(stdio_runtime);

    STDIO_LOCK();
    /* disable further instrumentation while Darshan shuts down */
    instrumentation_disabled = 1;
    STDIO_UNLOCK();

    return;
}

static void stdio_get_output_data(
    MPI_Comm mod_comm,
    darshan_record_id *shared_recs,
    int shared_rec_count,
    void **stdio_buf,
    int *stdio_buf_sz)
{
462
    /* TODO: implement reduction operator */
463 464 465 466

    assert(stdio_runtime);

    *stdio_buf = (void *)(stdio_runtime->file_record_array);
467
    *stdio_buf_sz = stdio_runtime->file_array_ndx * sizeof(struct darshan_stdio_record);
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

    return;
}

static void stdio_shutdown()
{
    struct stdio_file_runtime_ref *ref, *tmp;

    assert(stdio_runtime);

    HASH_ITER(hlink, stdio_runtime->stream_hash, ref, tmp)
    {
        HASH_DELETE(hlink, stdio_runtime->stream_hash, ref);
        free(ref);
    }

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

    free(stdio_runtime->file_runtime_array);
    free(stdio_runtime->file_record_array);
    free(stdio_runtime);
    stdio_runtime = NULL;
    instrumentation_disabled = 0;
    
    return;
}

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