GitLab maintenance scheduled form Friday, 2021-06-18 5:00pm to Satursday, 2021-06-19 10:00pm CT - Services will be unavailable during this time.

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

Philip Carns's avatar
Philip Carns committed
7 8 9 10 11
/* catalog of stdio functions instrumented by this module
 *
 * functions for opening streams
 * --------------
 * FILE    *fdopen(int, const char *);                      DONE
Philip Carns's avatar
Philip Carns committed
12
 * FILE    *fopen(const char *, const char *);              DONE
Philip Carns's avatar
Philip Carns committed
13
 * FILE    *fopen64(const char *, const char *);            DONE
Philip Carns's avatar
Philip Carns committed
14
 * FILE    *freopen(const char *, const char *, FILE *);    DONE
Philip Carns's avatar
Philip Carns committed
15
 * FILE    *freopen64(const char *, const char *, FILE *);  DONE
Philip Carns's avatar
Philip Carns committed
16 17 18 19 20 21 22
 *
 * functions for closing streams
 * --------------
 * int      fclose(FILE *);                                 DONE
 *
 * functions for flushing streams
 * --------------
Philip Carns's avatar
Philip Carns committed
23
 * int      fflush(FILE *);                                 DONE
Philip Carns's avatar
Philip Carns committed
24 25 26
 *
 * functions for reading data
 * --------------
Philip Carns's avatar
Philip Carns committed
27
 * int      fgetc(FILE *);                                  DONE
Philip Carns's avatar
Philip Carns committed
28
 * char    *fgets(char *, int, FILE *);                     DONE
Philip Carns's avatar
Philip Carns committed
29
 * size_t   fread(void *, size_t, size_t, FILE *);          DONE
Philip Carns's avatar
Philip Carns committed
30 31
 * int      fscanf(FILE *, const char *, ...);              DONE
 * int      vfscanf(FILE *, const char *, va_list);         DONE
Philip Carns's avatar
Philip Carns committed
32
 * int      getc(FILE *);                                   DONE
Philip Carns's avatar
Philip Carns committed
33
 * int      getw(FILE *);                                   DONE
Philip Carns's avatar
Philip Carns committed
34 35 36
 *
 * functions for writing data
 * --------------
Philip Carns's avatar
Philip Carns committed
37 38
 * int      fprintf(FILE *, const char *, ...);             DONE
 * int      vfprintf(FILE *, const char *, va_list);        DONE
Philip Carns's avatar
Philip Carns committed
39
 * int      fputc(int, FILE *);                             DONE
Philip Carns's avatar
Philip Carns committed
40
 * int      fputs(const char *, FILE *);                    DONE
Philip Carns's avatar
Philip Carns committed
41
 * size_t   fwrite(const void *, size_t, size_t, FILE *);   DONE
Philip Carns's avatar
Philip Carns committed
42
 * int      putc(int, FILE *);                              DONE
Philip Carns's avatar
Philip Carns committed
43
 * int      putw(int, FILE *);                              DONE
Philip Carns's avatar
Philip Carns committed
44 45 46 47
 *
 * functions for changing file position
 * --------------
 * int      fseek(FILE *, long int, int);                   DONE
Philip Carns's avatar
Philip Carns committed
48
 * int      fseeko(FILE *, off_t, int);                     DONE
Philip Carns's avatar
Philip Carns committed
49
 * int      fseeko64(FILE *, off_t, int);                   DONE
50 51
 * int      fsetpos(FILE *, const fpos_t *);                DONE
 * int      fsetpos64(FILE *, const fpos_t *);              DONE
Philip Carns's avatar
Philip Carns committed
52
 * void     rewind(FILE *);                                 DONE
Philip Carns's avatar
Philip Carns committed
53
 *
54 55 56 57 58
 * Omissions: 
 *   - _unlocked() variants of the various flush, read, and write
 *     functions.  There are many of these, but they are not available on all
 *     systems and the man page advises not to use them.
 *   - ungetc()
Philip Carns's avatar
Philip Carns committed
59 60
 */

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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>
81 82
#include <stdint.h>
#include <limits.h>
83 84 85 86 87 88

#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));
Philip Carns's avatar
Philip Carns committed
89
DARSHAN_FORWARD_DECL(fdopen, FILE*, (int fd, const char *mode));
Philip Carns's avatar
Philip Carns committed
90
DARSHAN_FORWARD_DECL(freopen, FILE*, (const char *path, const char *mode, FILE *stream));
Philip Carns's avatar
Philip Carns committed
91
DARSHAN_FORWARD_DECL(freopen64, FILE*, (const char *path, const char *mode, FILE *stream));
92
DARSHAN_FORWARD_DECL(fclose, int, (FILE *fp));
Philip Carns's avatar
Philip Carns committed
93
DARSHAN_FORWARD_DECL(fflush, int, (FILE *fp));
94
DARSHAN_FORWARD_DECL(fwrite, size_t, (const void *ptr, size_t size, size_t nmemb, FILE *stream));
Philip Carns's avatar
Philip Carns committed
95
DARSHAN_FORWARD_DECL(fputc, int, (int c, FILE *stream));
Philip Carns's avatar
Philip Carns committed
96
DARSHAN_FORWARD_DECL(putw, int, (int w, FILE *stream));
Philip Carns's avatar
Philip Carns committed
97
DARSHAN_FORWARD_DECL(fputs, int, (const char *s, FILE *stream));
Philip Carns's avatar
Philip Carns committed
98
DARSHAN_FORWARD_DECL(fprintf, int, (FILE *stream, const char *format, ...));
99
DARSHAN_FORWARD_DECL(printf, int, (const char *format, ...));
Philip Carns's avatar
Philip Carns committed
100
DARSHAN_FORWARD_DECL(vfprintf, int, (FILE *stream, const char *format, va_list));
101
DARSHAN_FORWARD_DECL(vprintf, int, (const char *format, va_list));
102
DARSHAN_FORWARD_DECL(fread, size_t, (void *ptr, size_t size, size_t nmemb, FILE *stream));
Philip Carns's avatar
Philip Carns committed
103
DARSHAN_FORWARD_DECL(fgetc, int, (FILE *stream));
Philip Carns's avatar
Philip Carns committed
104
DARSHAN_FORWARD_DECL(getw, int, (FILE *stream));
Philip Carns's avatar
Philip Carns committed
105
DARSHAN_FORWARD_DECL(_IO_getc, int, (FILE *stream));
Philip Carns's avatar
Philip Carns committed
106
DARSHAN_FORWARD_DECL(_IO_putc, int, (int, FILE *stream));
Philip Carns's avatar
Philip Carns committed
107
DARSHAN_FORWARD_DECL(fscanf, int, (FILE *stream, const char *format, ...));
Philip Carns's avatar
Philip Carns committed
108
DARSHAN_FORWARD_DECL(__isoc99_fscanf, int, (FILE *stream, const char *format, ...));
Philip Carns's avatar
Philip Carns committed
109
DARSHAN_FORWARD_DECL(vfscanf, int, (FILE *stream, const char *format, va_list ap));
Philip Carns's avatar
Philip Carns committed
110
DARSHAN_FORWARD_DECL(fgets, char*, (char *s, int size, FILE *stream));
111
DARSHAN_FORWARD_DECL(fseek, int, (FILE *stream, long offset, int whence));
Philip Carns's avatar
Philip Carns committed
112
DARSHAN_FORWARD_DECL(fseeko, int, (FILE *stream, off_t offset, int whence));
Philip Carns's avatar
Philip Carns committed
113
DARSHAN_FORWARD_DECL(fseeko64, int, (FILE *stream, off_t offset, int whence));
114
DARSHAN_FORWARD_DECL(fsetpos, int, (FILE *stream, const fpos_t *pos));
115
DARSHAN_FORWARD_DECL(fsetpos64, int, (FILE *stream, const fpos64_t *pos));
Philip Carns's avatar
Philip Carns committed
116
DARSHAN_FORWARD_DECL(rewind, void, (FILE *stream));
117

118 119
/* structure to track stdio stats at runtime */
struct stdio_file_record_ref
120
{
121
    struct darshan_stdio_file* file_rec;
122 123 124 125
    int64_t offset;
    double last_meta_end;
    double last_read_end;
    double last_write_end;
126
    int fs_type;
127 128 129 130 131 132 133 134
};

/* The stdio_runtime structure maintains necessary state for storing
 * STDIO file records and for coordinating with darshan-core at 
 * shutdown time.
 */
struct stdio_runtime
{
135 136 137
    void *rec_id_hash;
    void *stream_hash;
    int file_rec_count;
138 139 140 141 142 143 144 145
};

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

static void stdio_runtime_initialize(void);
146 147 148 149 150 151
static void stdio_shutdown(
    MPI_Comm mod_comm,
    darshan_record_id *shared_recs,
    int shared_rec_count,
    void **stdio_buf,
    int *stdio_buf_sz);
152 153
static void stdio_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype);
154 155 156
static void stdio_shared_record_variance(
    MPI_Comm mod_comm, struct darshan_stdio_file *inrec_array,
    struct darshan_stdio_file *outrec_array, int shared_rec_count);
157 158 159
static struct stdio_file_record_ref *stdio_track_new_file_record(
    darshan_record_id rec_id, const char *path);
static void stdio_cleanup_runtime();
160 161 162 163

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

164 165
#define STDIO_PRE_RECORD() do { \
    STDIO_LOCK(); \
166
    if(!darshan_core_disabled_instrumentation()) { \
167 168
        if(!stdio_runtime) stdio_runtime_initialize(); \
        if(stdio_runtime) break; \
169
    } \
170 171
    STDIO_UNLOCK(); \
    return(ret); \
172 173 174 175 176 177
} while(0)

#define STDIO_POST_RECORD() do { \
    STDIO_UNLOCK(); \
} while(0)

178
#define STDIO_RECORD_OPEN(__ret, __path, __tm1, __tm2) do { \
179 180 181
    darshan_record_id rec_id; \
    struct stdio_file_record_ref* rec_ref; \
    char *newpath; \
182
    int __fd; \
183
    if(__ret == NULL) break; \
184 185 186 187 188
    newpath = darshan_clean_file_path(__path); \
    if(!newpath) newpath = (char*)__path; \
    if(darshan_core_excluded_path(newpath)) { \
        if(newpath != (char*)__path) free(newpath); \
        break; \
189
    } \
190
    rec_id = darshan_core_gen_record_id(newpath); \
Philip Carns's avatar
Philip Carns committed
191
    rec_ref = darshan_lookup_record_ref(stdio_runtime->rec_id_hash, &rec_id, sizeof(rec_id)); \
192 193 194 195 196 197 198 199 200 201 202 203 204
    if(!rec_ref) rec_ref = stdio_track_new_file_record(rec_id, newpath); \
    if(!rec_ref) { \
        if(newpath != (char*)__path) free(newpath); \
        break; \
    } \
    rec_ref->offset = 0; \
    rec_ref->file_rec->counters[STDIO_OPENS] += 1; \
    if(rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] == 0 || \
     rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] > __tm1) \
        rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] = __tm1; \
    rec_ref->file_rec->fcounters[STDIO_F_OPEN_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(rec_ref->file_rec->fcounters[STDIO_F_META_TIME], __tm1, __tm2, rec_ref->last_meta_end); \
    darshan_add_record_ref(&(stdio_runtime->stream_hash), &(__ret), sizeof(__ret), rec_ref); \
205 206
    __fd = fileno(__ret); \
    darshan_instrument_fs_data(rec_ref->fs_type, newpath, __fd); \
207
    if(newpath != (char*)__path) free(newpath); \
208 209 210
} while(0)


211
#define STDIO_RECORD_READ(__fp, __bytes,  __tm1, __tm2) do{ \
212
    struct stdio_file_record_ref* rec_ref; \
213
    int64_t this_offset; \
Philip Carns's avatar
Philip Carns committed
214
    rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &(__fp), sizeof(__fp)); \
215 216 217 218 219 220 221 222 223 224 225
    if(!rec_ref) break; \
    this_offset = rec_ref->offset; \
    rec_ref->offset = this_offset + __bytes; \
    if(rec_ref->file_rec->counters[STDIO_MAX_BYTE_READ] < (this_offset + __bytes - 1)) \
        rec_ref->file_rec->counters[STDIO_MAX_BYTE_READ] = (this_offset + __bytes - 1); \
    rec_ref->file_rec->counters[STDIO_BYTES_READ] += __bytes; \
    rec_ref->file_rec->counters[STDIO_READS] += 1; \
    if(rec_ref->file_rec->fcounters[STDIO_F_READ_START_TIMESTAMP] == 0 || \
     rec_ref->file_rec->fcounters[STDIO_F_READ_START_TIMESTAMP] > __tm1) \
        rec_ref->file_rec->fcounters[STDIO_F_READ_START_TIMESTAMP] = __tm1; \
    rec_ref->file_rec->fcounters[STDIO_F_READ_END_TIMESTAMP] = __tm2; \
226
    DARSHAN_TIMER_INC_NO_OVERLAP(rec_ref->file_rec->fcounters[STDIO_F_READ_TIME], __tm1, __tm2, rec_ref->last_read_end); \
227 228
} while(0)

Philip Carns's avatar
Philip Carns committed
229
#define STDIO_RECORD_WRITE(__fp, __bytes,  __tm1, __tm2, __fflush_flag) do{ \
230
    struct stdio_file_record_ref* rec_ref; \
231
    int64_t this_offset; \
Philip Carns's avatar
Philip Carns committed
232
    rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &(__fp), sizeof(__fp)); \
233 234 235 236 237 238
    if(!rec_ref) break; \
    this_offset = rec_ref->offset; \
    rec_ref->offset = this_offset + __bytes; \
    if(rec_ref->file_rec->counters[STDIO_MAX_BYTE_WRITTEN] < (this_offset + __bytes - 1)) \
        rec_ref->file_rec->counters[STDIO_MAX_BYTE_WRITTEN] = (this_offset + __bytes - 1); \
    rec_ref->file_rec->counters[STDIO_BYTES_WRITTEN] += __bytes; \
Philip Carns's avatar
Philip Carns committed
239
    if(__fflush_flag) \
240
        rec_ref->file_rec->counters[STDIO_FLUSHES] += 1; \
Philip Carns's avatar
Philip Carns committed
241
    else \
242 243 244 245 246 247
        rec_ref->file_rec->counters[STDIO_WRITES] += 1; \
    if(rec_ref->file_rec->fcounters[STDIO_F_WRITE_START_TIMESTAMP] == 0 || \
     rec_ref->file_rec->fcounters[STDIO_F_WRITE_START_TIMESTAMP] > __tm1) \
        rec_ref->file_rec->fcounters[STDIO_F_WRITE_START_TIMESTAMP] = __tm1; \
    rec_ref->file_rec->fcounters[STDIO_F_WRITE_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(rec_ref->file_rec->fcounters[STDIO_F_WRITE_TIME], __tm1, __tm2, rec_ref->last_write_end); \
248
} while(0)
249

Philip Carns's avatar
Philip Carns committed
250
FILE* DARSHAN_DECL(fopen)(const char *path, const char *mode)
Philip Carns's avatar
Philip Carns committed
251 252 253 254
{
    FILE* ret;
    double tm1, tm2;

Philip Carns's avatar
Philip Carns committed
255
    MAP_OR_FAIL(fopen);
Philip Carns's avatar
Philip Carns committed
256 257

    tm1 = darshan_core_wtime();
Philip Carns's avatar
Philip Carns committed
258
    ret = __real_fopen(path, mode);
Philip Carns's avatar
Philip Carns committed
259 260
    tm2 = darshan_core_wtime();

261
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
262
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
263
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
264 265 266 267

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
268
FILE* DARSHAN_DECL(fopen64)(const char *path, const char *mode)
269 270
{
    FILE* ret;
271
    double tm1, tm2;
272

Shane Snyder's avatar
Shane Snyder committed
273
    MAP_OR_FAIL(fopen64);
274

275
    tm1 = darshan_core_wtime();
Philip Carns's avatar
Philip Carns committed
276
    ret = __real_fopen64(path, mode);
277 278
    tm2 = darshan_core_wtime();

279
    STDIO_PRE_RECORD();
280
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
281
    STDIO_POST_RECORD();
282 283 284 285

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
286
FILE* DARSHAN_DECL(fdopen)(int fd, const char *mode)
287 288
{
    FILE* ret;
Philip Carns's avatar
Philip Carns committed
289
    double tm1, tm2;
290

Philip Carns's avatar
Philip Carns committed
291
    MAP_OR_FAIL(fdopen);
292

Philip Carns's avatar
Philip Carns committed
293
    tm1 = darshan_core_wtime();
Philip Carns's avatar
Philip Carns committed
294 295 296
    ret = __real_fdopen(fd, mode);
    tm2 = darshan_core_wtime();

297
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
298
    STDIO_RECORD_OPEN(ret, "UNKNOWN-FDOPEN", tm1, tm2);
299
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
300 301 302 303 304 305 306 307 308 309 310 311 312

    return(ret);
}

FILE* DARSHAN_DECL(freopen)(const char *path, const char *mode, FILE *stream)
{
    FILE* ret;
    double tm1, tm2;

    MAP_OR_FAIL(freopen);

    tm1 = darshan_core_wtime();
    ret = __real_freopen(path, mode, stream);
Philip Carns's avatar
Philip Carns committed
313 314
    tm2 = darshan_core_wtime();

315
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
316
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
317
    STDIO_POST_RECORD();
318 319 320 321

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
322 323 324 325 326 327 328 329 330 331 332
FILE* DARSHAN_DECL(freopen64)(const char *path, const char *mode, FILE *stream)
{
    FILE* ret;
    double tm1, tm2;

    MAP_OR_FAIL(freopen64);

    tm1 = darshan_core_wtime();
    ret = __real_freopen64(path, mode, stream);
    tm2 = darshan_core_wtime();

333
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
334
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
335
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
336 337 338 339 340

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
341 342 343 344 345 346 347 348 349 350 351
int DARSHAN_DECL(fflush)(FILE *fp)
{
    double tm1, tm2;
    int ret;

    MAP_OR_FAIL(fflush);

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

352
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
353 354
    if(ret >= 0)
        STDIO_RECORD_WRITE(fp, 0, tm1, tm2, 1);
355
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
356 357 358 359

    return(ret);
}

360 361 362 363
int DARSHAN_DECL(fclose)(FILE *fp)
{
    double tm1, tm2;
    int ret;
364
    struct stdio_file_record_ref *rec_ref;
365 366 367 368 369 370 371

    MAP_OR_FAIL(fclose);

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

372 373 374
    STDIO_PRE_RECORD();
    rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &fp, sizeof(fp));
    if(rec_ref)
375
    {
376 377 378 379
        if(rec_ref->file_rec->fcounters[STDIO_F_CLOSE_START_TIMESTAMP] == 0 ||
         rec_ref->file_rec->fcounters[STDIO_F_CLOSE_START_TIMESTAMP] > tm1)
           rec_ref->file_rec->fcounters[STDIO_F_CLOSE_START_TIMESTAMP] = tm1;
        rec_ref->file_rec->fcounters[STDIO_F_CLOSE_END_TIMESTAMP] = tm2;
380
        DARSHAN_TIMER_INC_NO_OVERLAP(
381 382 383
            rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
            tm1, tm2, rec_ref->last_meta_end);
        darshan_delete_record_ref(&(stdio_runtime->stream_hash), &fp, sizeof(fp));
384
    }
385
    STDIO_POST_RECORD();
386 387 388 389

    return(ret);
}

390 391 392 393 394 395 396 397 398 399 400
size_t DARSHAN_DECL(fwrite)(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t ret;
    double tm1, tm2;

    MAP_OR_FAIL(fwrite);

    tm1 = darshan_core_wtime();
    ret = __real_fwrite(ptr, size, nmemb, stream);
    tm2 = darshan_core_wtime();

401
    STDIO_PRE_RECORD();
402
    if(ret > 0)
Philip Carns's avatar
Philip Carns committed
403
        STDIO_RECORD_WRITE(stream, size*ret, tm1, tm2, 0);
404
    STDIO_POST_RECORD();
405 406 407 408

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
409 410 411 412 413 414 415 416 417 418 419 420

int DARSHAN_DECL(fputc)(int c, FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(fputc);

    tm1 = darshan_core_wtime();
    ret = __real_fputc(c, stream);
    tm2 = darshan_core_wtime();

421
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
422 423
    if(ret != EOF)
        STDIO_RECORD_WRITE(stream, 1, tm1, tm2, 0);
424
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
425 426 427 428

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
429 430 431 432 433 434 435 436 437 438 439
int DARSHAN_DECL(putw)(int w, FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(putw);

    tm1 = darshan_core_wtime();
    ret = __real_putw(w, stream);
    tm2 = darshan_core_wtime();

440
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
441 442
    if(ret != EOF)
        STDIO_RECORD_WRITE(stream, sizeof(int), tm1, tm2, 0);
443
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
444 445 446 447 448

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
449 450 451 452 453 454 455 456 457 458 459 460

int DARSHAN_DECL(fputs)(const char *s, FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(fputs);

    tm1 = darshan_core_wtime();
    ret = __real_fputs(s, stream);
    tm2 = darshan_core_wtime();

461
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
462 463
    if(ret != EOF && ret > 0)
        STDIO_RECORD_WRITE(stream, strlen(s), tm1, tm2, 0);
464
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
465 466 467 468

    return(ret);
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
int DARSHAN_DECL(vprintf)(const char *format, va_list ap)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(vprintf);

    tm1 = darshan_core_wtime();
    ret = __real_vprintf(format, ap);
    tm2 = darshan_core_wtime();

    STDIO_PRE_RECORD();
    if(ret > 0)
        STDIO_RECORD_WRITE(stdout, ret, tm1, tm2, 0);
    STDIO_POST_RECORD();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
488 489 490 491 492 493 494 495 496 497 498
int DARSHAN_DECL(vfprintf)(FILE *stream, const char *format, va_list ap)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(vfprintf);

    tm1 = darshan_core_wtime();
    ret = __real_vfprintf(stream, format, ap);
    tm2 = darshan_core_wtime();

499
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
500
    if(ret > 0)
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
        STDIO_RECORD_WRITE(stream, ret, tm1, tm2, 0);
    STDIO_POST_RECORD();

    return(ret);
}


int DARSHAN_DECL(printf)(const char *format, ...)
{
    int ret;
    double tm1, tm2;
    va_list ap;

    MAP_OR_FAIL(vprintf);

    tm1 = darshan_core_wtime();
    /* NOTE: we intentionally switch to vprintf here to handle the variable
     * length arguments.
     */
    va_start(ap, format);
    ret = __real_vprintf(format, ap);
    va_end(ap);
    tm2 = darshan_core_wtime();

    STDIO_PRE_RECORD();
    if(ret > 0)
        STDIO_RECORD_WRITE(stdout, ret, tm1, tm2, 0);
528
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

    return(ret);
}

int DARSHAN_DECL(fprintf)(FILE *stream, const char *format, ...)
{
    int ret;
    double tm1, tm2;
    va_list ap;

    MAP_OR_FAIL(vfprintf);

    tm1 = darshan_core_wtime();
    /* NOTE: we intentionally switch to vfprintf here to handle the variable
     * length arguments.
     */
    va_start(ap, format);
    ret = __real_vfprintf(stream, format, ap);
    va_end(ap);
    tm2 = darshan_core_wtime();

550
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
551
    if(ret > 0)
552
        STDIO_RECORD_WRITE(stream, ret, tm1, tm2, 0);
553
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
554 555 556 557

    return(ret);
}

558 559 560 561 562 563 564 565 566 567 568
size_t DARSHAN_DECL(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t ret;
    double tm1, tm2;

    MAP_OR_FAIL(fread);

    tm1 = darshan_core_wtime();
    ret = __real_fread(ptr, size, nmemb, stream);
    tm2 = darshan_core_wtime();

569
    STDIO_PRE_RECORD();
570 571
    if(ret > 0)
        STDIO_RECORD_READ(stream, size*ret, tm1, tm2);
572
    STDIO_POST_RECORD();
573 574 575 576

    return(ret);
}

577
int DARSHAN_DECL(fgetc)(FILE *stream)
Philip Carns's avatar
Philip Carns committed
578 579 580 581 582 583 584 585 586 587
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(fgetc);

    tm1 = darshan_core_wtime();
    ret = __real_fgetc(stream);
    tm2 = darshan_core_wtime();

588
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
589 590
    if(ret != EOF)
        STDIO_RECORD_READ(stream, 1, tm1, tm2);
591
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
592 593 594 595 596

    return(ret);
}

/* NOTE: stdio.h typically implements getc() as a macro pointing to _IO_getc */
597
int DARSHAN_DECL(_IO_getc)(FILE *stream)
Philip Carns's avatar
Philip Carns committed
598 599 600 601 602 603 604 605 606 607
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(_IO_getc);

    tm1 = darshan_core_wtime();
    ret = __real__IO_getc(stream);
    tm2 = darshan_core_wtime();

608
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
609 610
    if(ret != EOF)
        STDIO_RECORD_READ(stream, 1, tm1, tm2);
611
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
612 613 614 615

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
616
/* NOTE: stdio.h typically implements putc() as a macro pointing to _IO_putc */
617
int DARSHAN_DECL(_IO_putc)(int c, FILE *stream)
Philip Carns's avatar
Philip Carns committed
618 619 620 621 622 623 624 625 626 627
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(_IO_putc);

    tm1 = darshan_core_wtime();
    ret = __real__IO_putc(c, stream);
    tm2 = darshan_core_wtime();

628
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
629 630
    if(ret != EOF)
        STDIO_RECORD_WRITE(stream, 1, tm1, tm2, 0);
631
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
632 633 634

    return(ret);
}
635 636

int DARSHAN_DECL(getw)(FILE *stream)
Philip Carns's avatar
Philip Carns committed
637 638 639 640 641 642 643 644 645 646
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(getw);

    tm1 = darshan_core_wtime();
    ret = __real_getw(stream);
    tm2 = darshan_core_wtime();

647
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
648 649
    if(ret != EOF || ferror(stream) == 0)
        STDIO_RECORD_READ(stream, sizeof(int), tm1, tm2);
650
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
651 652 653 654

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
/* NOTE: some glibc versions use __isoc99_fscanf as the underlying symbol
 * rather than fscanf
 */
int DARSHAN_DECL(__isoc99_fscanf)(FILE *stream, const char *format, ...)
{
    int ret;
    double tm1, tm2;
    va_list ap;
    long start_off, end_off;

    MAP_OR_FAIL(vfscanf);

    tm1 = darshan_core_wtime();
    /* NOTE: we intentionally switch to vfscanf here to handle the variable
     * length arguments.
     */
    start_off = ftell(stream);
    va_start(ap, format);
    ret = __real_vfscanf(stream, format, ap);
    va_end(ap);
    end_off = ftell(stream);
    tm2 = darshan_core_wtime();

678
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
679 680
    if(ret != 0)
        STDIO_RECORD_READ(stream, (end_off-start_off), tm1, tm2);
681
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
682 683 684 685

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
686

Philip Carns's avatar
Philip Carns committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
int DARSHAN_DECL(fscanf)(FILE *stream, const char *format, ...)
{
    int ret;
    double tm1, tm2;
    va_list ap;
    long start_off, end_off;

    MAP_OR_FAIL(vfscanf);

    tm1 = darshan_core_wtime();
    /* NOTE: we intentionally switch to vfscanf here to handle the variable
     * length arguments.
     */
    start_off = ftell(stream);
    va_start(ap, format);
    ret = __real_vfscanf(stream, format, ap);
    va_end(ap);
    end_off = ftell(stream);
    tm2 = darshan_core_wtime();

707
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
708
    if(ret != 0)
Philip Carns's avatar
Philip Carns committed
709
        STDIO_RECORD_READ(stream, (end_off-start_off), tm1, tm2);
710
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

    return(ret);
}

int DARSHAN_DECL(vfscanf)(FILE *stream, const char *format, va_list ap)
{
    int ret;
    double tm1, tm2;
    long start_off, end_off;

    MAP_OR_FAIL(vfscanf);

    tm1 = darshan_core_wtime();
    start_off = ftell(stream);
    ret = __real_vfscanf(stream, format, ap);
    end_off = ftell(stream);
    tm2 = darshan_core_wtime();

729
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
730 731
    if(ret != 0)
        STDIO_RECORD_READ(stream, end_off-start_off, tm1, tm2);
732
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
733 734 735 736 737

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
738 739 740 741 742 743 744 745 746 747 748
char* DARSHAN_DECL(fgets)(char *s, int size, FILE *stream)
{
    char *ret;
    double tm1, tm2;

    MAP_OR_FAIL(fgets);

    tm1 = darshan_core_wtime();
    ret = __real_fgets(s, size, stream);
    tm2 = darshan_core_wtime();

749
    STDIO_PRE_RECORD();
Philip Carns's avatar
Philip Carns committed
750 751
    if(ret != NULL)
        STDIO_RECORD_READ(stream, strlen(ret), tm1, tm2);
752
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
753 754 755 756 757

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
758 759 760
void DARSHAN_DECL(rewind)(FILE *stream)
{
    double tm1, tm2;
761
    struct stdio_file_record_ref *rec_ref;
Philip Carns's avatar
Philip Carns committed
762 763 764 765 766 767 768

    MAP_OR_FAIL(rewind);

    tm1 = darshan_core_wtime();
    __real_rewind(stream);
    tm2 = darshan_core_wtime();

769 770 771
    /* NOTE: we don't use STDIO_PRE_RECORD here because there is no return
     * value in this wrapper.
     */
Philip Carns's avatar
Philip Carns committed
772
    STDIO_LOCK();
773
    if(darshan_core_disabled_instrumentation()) {
774 775 776 777
        STDIO_UNLOCK();
        return;
    }
    if(!stdio_runtime) stdio_runtime_initialize();
778 779 780 781 782 783 784 785
    if(!stdio_runtime) {
        STDIO_UNLOCK();
        return;
    }

    rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));

    if(rec_ref)
Philip Carns's avatar
Philip Carns committed
786
    {
787
        rec_ref->offset = 0;
Philip Carns's avatar
Philip Carns committed
788
        DARSHAN_TIMER_INC_NO_OVERLAP(
789 790 791
            rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
            tm1, tm2, rec_ref->last_meta_end);
        rec_ref->file_rec->counters[STDIO_SEEKS] += 1;
Philip Carns's avatar
Philip Carns committed
792
    }
793
    STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
794 795 796 797

    return;
}

798 799 800
int DARSHAN_DECL(fseek)(FILE *stream, long offset, int whence)
{
    int ret;
801
    struct stdio_file_record_ref *rec_ref;
802 803 804 805 806 807 808 809 810 811
    double tm1, tm2;

    MAP_OR_FAIL(fseek);

    tm1 = darshan_core_wtime();
    ret = __real_fseek(stream, offset, whence);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
812 813 814
        STDIO_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));
        if(rec_ref)
Philip Carns's avatar
Philip Carns committed
815
        {
816
            rec_ref->offset = ftell(stream);
Philip Carns's avatar
Philip Carns committed
817
            DARSHAN_TIMER_INC_NO_OVERLAP(
818 819 820
                rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            rec_ref->file_rec->counters[STDIO_SEEKS] += 1;
Philip Carns's avatar
Philip Carns committed
821
        }
822
        STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
823 824 825 826 827 828 829 830
    }

    return(ret);
}

int DARSHAN_DECL(fseeko)(FILE *stream, off_t offset, int whence)
{
    int ret;
831
    struct stdio_file_record_ref *rec_ref;
Philip Carns's avatar
Philip Carns committed
832 833 834 835 836 837 838 839 840 841
    double tm1, tm2;

    MAP_OR_FAIL(fseeko);

    tm1 = darshan_core_wtime();
    ret = __real_fseeko(stream, offset, whence);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
842 843 844
        STDIO_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));
        if(rec_ref)
Philip Carns's avatar
Philip Carns committed
845
        {
846
            rec_ref->offset = ftell(stream);
Philip Carns's avatar
Philip Carns committed
847
            DARSHAN_TIMER_INC_NO_OVERLAP(
848 849 850
                rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            rec_ref->file_rec->counters[STDIO_SEEKS] += 1;
Philip Carns's avatar
Philip Carns committed
851
        }
852
        STDIO_POST_RECORD();
Philip Carns's avatar
Philip Carns committed
853 854 855 856 857 858 859 860
    }

    return(ret);
}

int DARSHAN_DECL(fseeko64)(FILE *stream, off_t offset, int whence)
{
    int ret;
861
    struct stdio_file_record_ref *rec_ref;
Philip Carns's avatar
Philip Carns committed
862 863 864 865 866 867 868 869 870 871
    double tm1, tm2;

    MAP_OR_FAIL(fseeko64);

    tm1 = darshan_core_wtime();
    ret = __real_fseeko64(stream, offset, whence);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
872 873 874
        STDIO_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));
        if(rec_ref)
875
        {
876
            rec_ref->offset = ftell(stream);
877
            DARSHAN_TIMER_INC_NO_OVERLAP(
878 879 880
                rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            rec_ref->file_rec->counters[STDIO_SEEKS] += 1;
881
        }
882
        STDIO_POST_RECORD();
883 884 885 886 887
    }

    return(ret);
}

888 889 890
int DARSHAN_DECL(fsetpos)(FILE *stream, const fpos_t *pos)
{
    int ret;
891
    struct stdio_file_record_ref *rec_ref;
892 893 894 895 896 897 898 899 900 901
    double tm1, tm2;

    MAP_OR_FAIL(fsetpos);

    tm1 = darshan_core_wtime();
    ret = __real_fsetpos(stream, pos);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
902 903 904
        STDIO_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));
        if(rec_ref)
905
        {
906
            rec_ref->offset = ftell(stream);
907
            DARSHAN_TIMER_INC_NO_OVERLAP(
908 909 910
                rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            rec_ref->file_rec->counters[STDIO_SEEKS] += 1;
911
        }
912
        STDIO_POST_RECORD();
913 914 915 916 917
    }

    return(ret);
}

918
int DARSHAN_DECL(fsetpos64)(FILE *stream, const fpos64_t *pos)
919 920
{
    int ret;
921
    struct stdio_file_record_ref *rec_ref;
922 923 924 925 926 927 928 929 930 931
    double tm1, tm2;

    MAP_OR_FAIL(fsetpos64);

    tm1 = darshan_core_wtime();
    ret = __real_fsetpos64(stream, pos);
    tm2 = darshan_core_wtime();

    if(ret >= 0)
    {
932 933 934
        STDIO_PRE_RECORD();
        rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));
        if(rec_ref)
935
        {
936
            rec_ref->offset = ftell(stream);
937
            DARSHAN_TIMER_INC_NO_OVERLAP(
938 939 940
                rec_ref->file_rec->fcounters[STDIO_F_META_TIME],
                tm1, tm2, rec_ref->last_meta_end);
            rec_ref->file_rec->counters[STDIO_SEEKS] += 1;
941
        }
942
        STDIO_POST_RECORD();
943 944 945 946 947 948
    }

    return(ret);
}


949 950 951 952 953 954 955
/**********************************************************
 * Internal functions for manipulating STDIO module state *
 **********************************************************/

/* initialize internal STDIO module data structures and register with darshan-core */
static void stdio_runtime_initialize()
{
956 957 958 959
    int stdio_buf_size;

    /* try to store default number of records for this module */
    stdio_buf_size = DARSHAN_DEF_MOD_REC_COUNT * sizeof(struct darshan_stdio_file);
960 961 962 963

    /* register the stdio module with darshan core */
    darshan_core_register_module(
        DARSHAN_STDIO_MOD,
964 965
        &stdio_shutdown,
        &stdio_buf_size,
966
        &my_rank,
967 968
        &darshan_mem_alignment);

969 970 971
    /* return if darshan-core does not provide enough module memory */
    if(stdio_buf_size < sizeof(struct darshan_stdio_file))
    {
Shane Snyder's avatar
Shane Snyder committed
972
        darshan_core_unregister_module(DARSHAN_STDIO_MOD);
973
        return;
974
    }
975 976 977 978

    stdio_runtime = malloc(sizeof(*stdio_runtime));
    if(!stdio_runtime)
    {
979
        darshan_core_unregister_module(DARSHAN_STDIO_MOD);
980 981
        return;
    }
982
    memset(stdio_runtime, 0, sizeof(*stdio_runtime));
983 984 985 986 987

    /* instantiate records for stdin, stdout, and stderr */
    STDIO_RECORD_OPEN(stdin, "<STDIN>", 0, 0);
    STDIO_RECORD_OPEN(stdout, "<STDOUT>", 0, 0);
    STDIO_RECORD_OPEN(stderr, "<STDERR>", 0, 0);
988 989
}

990 991 992
/************************************************************************
 * Functions exported by this module for coordinating with darshan-core *
 ************************************************************************/
993

994 995
static void stdio_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype)
996
{
997 998 999 1000
    struct darshan_stdio_file tmp_file;
    struct darshan_stdio_file *infile = infile_v;
    struct darshan_stdio_file *inoutfile = inoutfile_v;
    int i, j;
1001

1002
    assert(stdio_runtime);
1003

1004
    for(i=0; i<*len; i++)
1005
    {
1006 1007 1008
        memset(&tmp_file, 0, sizeof(struct darshan_stdio_file));
        tmp_file.base_rec.id = infile->base_rec.id;
        tmp_file.base_rec.rank = -1;
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
        /* sum */
        for(j=STDIO_OPENS; j<=STDIO_BYTES_READ; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + inoutfile->counters[j];
        }
        
        /* max */
        for(j=STDIO_MAX_BYTE_READ; j<=STDIO_MAX_BYTE_WRITTEN; j++)
        {
            if(infile->counters[j] > inoutfile->counters[j])
                tmp_file.counters[j] = infile->counters[j];
            else
                tmp_file.counters[j] = inoutfile->counters[j];
        }
1024

1025 1026 1027 1028 1029
        /* sum */
        for(j=STDIO_F_META_TIME; j<=STDIO_F_READ_TIME; j++)
        {
            tmp_file.fcounters[j] = infile->fcounters[j] + inoutfile->fcounters[j];
        }
1030

1031 1032 1033 1034 1035 1036 1037 1038 1039
        /* min non-zero (if available) value */
        for(j=STDIO_F_OPEN_START_TIMESTAMP; j<=STDIO_F_READ_START_TIMESTAMP; j++)
        {
            if((infile->fcounters[j] < inoutfile->fcounters[j] &&
               infile->fcounters[j] > 0) || inoutfile->fcounters[j] == 0) 
                tmp_file.fcounters[j] = infile->fcounters[j];
            else
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
        }
1040

1041 1042 1043 1044 1045 1046 1047 1048
        /* max */
        for(j=STDIO_F_OPEN_END_TIMESTAMP; j<=STDIO_F_READ_END_TIMESTAMP; j++)
        {
            if(infile->fcounters[j] > inoutfile->fcounters[j])
                tmp_file.fcounters[j] = infile->fcounters[j];
            else
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
        }
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 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
        /* min (zeroes are ok here; some procs don't do I/O) */
        if(infile->fcounters[STDIO_F_FASTEST_RANK_TIME] <
           inoutfile->fcounters[STDIO_F_FASTEST_RANK_TIME])
        {
            tmp_file.counters[STDIO_FASTEST_RANK] =
                infile->counters[STDIO_FASTEST_RANK];
            tmp_file.counters[STDIO_FASTEST_RANK_BYTES] =
                infile->counters[STDIO_FASTEST_RANK_BYTES];
            tmp_file.fcounters[STDIO_F_FASTEST_RANK_TIME] =
                infile->fcounters[STDIO_F_FASTEST_RANK_TIME];
        }
        else
        {
            tmp_file.counters[STDIO_FASTEST_RANK] =
                inoutfile->counters[STDIO_FASTEST_RANK];
            tmp_file.counters[STDIO_FASTEST_RANK_BYTES] =
                inoutfile->counters[STDIO_FASTEST_RANK_BYTES];
            tmp_file.fcounters[STDIO_F_FASTEST_RANK_TIME] =
                inoutfile->fcounters[STDIO_F_FASTEST_RANK_TIME];
        }

        /* max */
        if(infile->fcounters[STDIO_F_SLOWEST_RANK_TIME] >
           inoutfile->fcounters[STDIO_F_SLOWEST_RANK_TIME])
        {
            tmp_file.counters[STDIO_SLOWEST_RANK] =
                infile->counters[STDIO_SLOWEST_RANK];
            tmp_file.counters[STDIO_SLOWEST_RANK_BYTES] =
                infile->counters[STDIO_SLOWEST_RANK_BYTES];
            tmp_file.fcounters[STDIO_F_SLOWEST_RANK_TIME] =
                infile->fcounters[STDIO_F_SLOWEST_RANK_TIME];
        }
        else
        {
            tmp_file.counters[STDIO_SLOWEST_RANK] =
                inoutfile->counters[STDIO_SLOWEST_RANK];
            tmp_file.counters[STDIO_SLOWEST_RANK_BYTES] =
                inoutfile->counters[STDIO_SLOWEST_RANK_BYTES];
            tmp_file.fcounters[STDIO_F_SLOWEST_RANK_TIME] =
                inoutfile->fcounters[STDIO_F_SLOWEST_RANK_TIME];
        }

1092 1093 1094 1095
        /* update pointers */
        *inoutfile = tmp_file;
        inoutfile++;
        infile++;
1096 1097 1098 1099 1100
    }

    return;
}

1101
static void stdio_shutdown(
1102 1103 1104 1105 1106 1107
    MPI_Comm mod_comm,
    darshan_record_id *shared_recs,
    int shared_rec_count,
    void **stdio_buf,
    int *stdio_buf_sz)
{
1108 1109
    struct stdio_file_record_ref *rec_ref;
    struct darshan_stdio_file *stdio_rec_buf = *(struct darshan_stdio_file **)stdio_buf;
1110
    int i;
1111 1112
    struct darshan_stdio_file *red_send_buf = NULL;
    struct darshan_stdio_file *red_recv_buf = NULL;
1113 1114
    MPI_Datatype red_type;
    MPI_Op red_op;
1115
    int stdio_rec_count;
1116
    double stdio_time;
1117

1118
    STDIO_LOCK();
1119
    assert(stdio_runtime);
1120

1121
    stdio_rec_count = stdio_runtime->file_rec_count;
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131
    /* if there are globally shared files, do a shared file reduction */
    /* 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"))
    {
        /* necessary initialization of shared records */
        for(i = 0; i < shared_rec_count; i++)
        {
1132 1133 1134
            rec_ref = darshan_lookup_record_ref(stdio_runtime->rec_id_hash,
                &shared_recs[i], sizeof(darshan_record_id));
            assert(rec_ref);
1135

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
            stdio_time =
                rec_ref->file_rec->fcounters[STDIO_F_READ_TIME] +
                rec_ref->file_rec->fcounters[STDIO_F_WRITE_TIME] +
                rec_ref->file_rec->fcounters[STDIO_F_META_TIME];

            /* initialize fastest/slowest info prior to the reduction */
            rec_ref->file_rec->counters[STDIO_FASTEST_RANK] =
                rec_ref->file_rec->base_rec.rank;
            rec_ref->file_rec->counters[STDIO_FASTEST_RANK_BYTES] =
                rec_ref->file_rec->counters[STDIO_BYTES_READ] +
                rec_ref->file_rec->counters[STDIO_BYTES_WRITTEN];
            rec_ref->file_rec->fcounters[STDIO_F_FASTEST_RANK_TIME] =
                stdio_time;

            /* until reduction occurs, we assume that this rank is both
             * the fastest and slowest. It is up to the reduction operator
             * to find the true min and max.
             */
            rec_ref->file_rec->counters[STDIO_SLOWEST_RANK] =
                rec_ref->file_rec->counters[STDIO_FASTEST_RANK];
            rec_ref->file_rec->counters[STDIO_SLOWEST_RANK_BYTES] =
                rec_ref->file_rec->counters[STDIO_FASTEST_RANK_BYTES];
            rec_ref->file_rec->fcounters[STDIO_F_SLOWEST_RANK_TIME] =
                rec_ref->file_rec->fcounters[STDIO_F_FASTEST_RANK_TIME];

1161
            rec_ref->file_rec->base_rec.rank = -1;
1162 1163 1164 1165 1166 1167
        }

        /* 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
         */
1168
        darshan_record_sort(stdio_rec_buf, stdio_rec_count, sizeof(struct darshan_stdio_file));
1169 1170

        /* make *send_buf point to the shared files at the end of sorted array */
1171
        red_send_buf = &(stdio_rec_buf[stdio_rec_count-shared_rec_count]);
1172 1173 1174 1175

        /* allocate memory for the reduction output on rank 0 */
        if(my_rank == 0)
        {
1176
            red_recv_buf = malloc(shared_rec_count * sizeof(struct darshan_stdio_file));
1177 1178 1179 1180 1181 1182 1183 1184 1185
            if(!red_recv_buf)
            {
                return;
            }
        }

        /* construct a datatype for a STDIO file record.  This is serving no purpose
         * except to make sure we can do a reduction on proper boundaries
         */
1186
        PMPI_Type_contiguous(sizeof(struct darshan_stdio_file),
1187
            MPI_BYTE, &red_type);
1188
        PMPI_Type_commit(&red_type);
1189 1190

        /* register a STDIO file record reduction operator */
1191
        PMPI_Op_create(stdio_record_reduction_op, 1, &red_op);
1192 1193

        /* reduce shared STDIO file records */
1194
        PMPI_Reduce(red_send_buf, red_recv_buf,
1195 1196
            shared_rec_count, red_type, red_op, 0, mod_comm);

1197 1198 1199 1200
        /* get the time and byte variances for shared files */
        stdio_shared_record_variance(mod_comm, red_send_buf, red_recv_buf,
            shared_rec_count);

1201 1202 1203
        /* clean up reduction state */
        if(my_rank == 0)
        {
1204 1205 1206
            int tmp_ndx = stdio_rec_count - shared_rec_count;
            memcpy(&(stdio_rec_buf[tmp_ndx]), red_recv_buf,
                shared_rec_count * sizeof(struct darshan_stdio_file));
1207 1208 1209 1210
            free(red_recv_buf);
        }
        else
        {
1211
            stdio_rec_count -= shared_rec_count;
1212 1213
        }

1214 1215
        PMPI_Type_free(&red_type);
        PMPI_Op_free(&red_op);
1216 1217
    }

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
    /* filter out any records that have no activity on them; this is
     * specifically meant to filter out unused stdin, stdout, or stderr
     * entries
     *
     * NOTE: we can no longer use the darshan_lookup_record_ref()
     * function at this point to find specific records, because the
     * logic above has likely broken the mapping to the static array.
     * We walk it manually here instead.
     */
    for(i=0; i<stdio_rec_count; i++)
    {
        if(stdio_rec_buf[i].counters[STDIO_WRITES] == 0 &&
            stdio_rec_buf[i].counters[STDIO_READS] == 0)
        {
            if(i != (stdio_rec_count-1))
            {
                memmove(&stdio_rec_buf[i], &stdio_rec_buf[i+1],
                    (stdio_rec_count-i-1)*sizeof(stdio_rec_buf[i]));
                i--;
            }
1238
            stdio_rec_count--;
1239 1240 1241
        }
    }

1242 1243
    /* update output buffer size to account for shared file reduction */
    *stdio_buf_sz = stdio_rec_count * sizeof(struct darshan_stdio_file);
1244

1245 1246
    /* shutdown internal structures used for instrumenting */
    stdio_cleanup_runtime();
1247

1248 1249 1250
    STDIO_UNLOCK();
    
    return;
1251 1252
}

1253 1254
static struct stdio_file_record_ref *stdio_track_new_file_record(
    darshan_record_id rec_id, const char *path)
1255
{
1256 1257
    struct darshan_stdio_file *file_rec = NULL;
    struct stdio_file_record_ref *rec_ref = NULL;
1258
    struct darshan_fs_info fs_info;
1259
    int ret;
1260

1261 1262 1263 1264
    rec_ref = malloc(sizeof(*rec_ref));
    if(!rec_ref)
        return(NULL);
    memset(rec_ref, 0, sizeof(*rec_ref));
1265

1266 1267 1268 1269
    /* add a reference to this file record based on record id */
    ret = darshan_add_record_ref(&(stdio_runtime->rec_id_hash), &rec_id,
        sizeof(darshan_record_id), rec_ref);
    if(ret == 0)
1270
    {
1271 1272 1273
        free(rec_ref);
        return(NULL);
    }
1274

1275 1276 1277 1278 1279 1280
    /* register the actual file record with darshan-core so it is persisted
     * in the log file
     */
    file_rec = darshan_core_register_record(
        rec_id,
        path,
Philip Carns's avatar
typo  
Philip Carns committed
1281
        DARSHAN_STDIO_MOD,
1282
        sizeof(struct darshan_stdio_file),
1283
        &fs_info);
1284 1285 1286 1287 1288 1289 1290 1291

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

1293 1294 1295
    /* registering this file record was successful, so initialize some fields */
    file_rec->base_rec.id = rec_id;
    file_rec->base_rec.rank = my_rank;
1296
    rec_ref->fs_type = fs_info.fs_type;
1297 1298
    rec_ref->file_rec = file_rec;
    stdio_runtime->file_rec_count++;
1299

1300
    return(rec_ref);
Philip Carns's avatar