darshan-stdio.c 39.7 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
/* TODO list (general) for this module:
 * - add stdio page to darshan-job-summary
 * - figure out what to do about posix module compatibility
 *   - remove stdio counters in POSIX or keep and set to -1?
Philip Carns's avatar
Philip Carns committed
11
12
13
14
15
 *   - affected counters in posix module:
 *     - POSIX_FOPENS
 *     - POSIX_FREADS
 *     - POSIX_FWRITES
 *     - POSIX_FSEEKS
Philip Carns's avatar
Philip Carns committed
16
17
 */

Philip Carns's avatar
Philip Carns committed
18
19
20
21
22
/* 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
23
 * FILE    *fopen(const char *, const char *);              DONE
Philip Carns's avatar
Philip Carns committed
24
 * FILE    *fopen64(const char *, const char *);            DONE
Philip Carns's avatar
Philip Carns committed
25
 * FILE    *freopen(const char *, const char *, FILE *);    DONE
Philip Carns's avatar
Philip Carns committed
26
 * FILE    *freopen64(const char *, const char *, FILE *);  DONE
Philip Carns's avatar
Philip Carns committed
27
28
29
30
31
32
33
 *
 * functions for closing streams
 * --------------
 * int      fclose(FILE *);                                 DONE
 *
 * functions for flushing streams
 * --------------
Philip Carns's avatar
Philip Carns committed
34
 * int      fflush(FILE *);                                 DONE
Philip Carns's avatar
Philip Carns committed
35
36
37
 *
 * functions for reading data
 * --------------
Philip Carns's avatar
Philip Carns committed
38
 * int      fgetc(FILE *);                                  DONE
Philip Carns's avatar
Philip Carns committed
39
 * char    *fgets(char *, int, FILE *);                     DONE
Philip Carns's avatar
Philip Carns committed
40
 * size_t   fread(void *, size_t, size_t, FILE *);          DONE
Philip Carns's avatar
Philip Carns committed
41
42
 * int      fscanf(FILE *, const char *, ...);              DONE
 * int      vfscanf(FILE *, const char *, va_list);         DONE
Philip Carns's avatar
Philip Carns committed
43
 * int      getc(FILE *);                                   DONE
Philip Carns's avatar
Philip Carns committed
44
 * int      getw(FILE *);                                   DONE
Philip Carns's avatar
Philip Carns committed
45
46
47
 *
 * functions for writing data
 * --------------
Philip Carns's avatar
Philip Carns committed
48
49
 * int      fprintf(FILE *, const char *, ...);             DONE
 * int      vfprintf(FILE *, const char *, va_list);        DONE
Philip Carns's avatar
Philip Carns committed
50
 * int      fputc(int, FILE *);                             DONE
Philip Carns's avatar
Philip Carns committed
51
 * int      fputs(const char *, FILE *);                    DONE
Philip Carns's avatar
Philip Carns committed
52
 * size_t   fwrite(const void *, size_t, size_t, FILE *);   DONE
Philip Carns's avatar
Philip Carns committed
53
 * int      putc(int, FILE *);                              DONE
Philip Carns's avatar
Philip Carns committed
54
 * int      putw(int, FILE *);                              DONE
Philip Carns's avatar
Philip Carns committed
55
56
57
58
 *
 * functions for changing file position
 * --------------
 * int      fseek(FILE *, long int, int);                   DONE
Philip Carns's avatar
Philip Carns committed
59
 * int      fseeko(FILE *, off_t, int);                     DONE
Philip Carns's avatar
Philip Carns committed
60
 * int      fseeko64(FILE *, off_t, int);                   DONE
Philip Carns's avatar
Philip Carns committed
61
62
 * int      fsetpos(FILE *, const fpos_t *);                DONE
 * int      fsetpos64(FILE *, const fpos_t *);              DONE
Philip Carns's avatar
Philip Carns committed
63
 * void     rewind(FILE *);                                 DONE
Philip Carns's avatar
Philip Carns committed
64
 *
65
66
67
68
69
 * 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
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
#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));
Philip Carns's avatar
Philip Carns committed
101
DARSHAN_FORWARD_DECL(fdopen, FILE*, (int fd, const char *mode));
Philip Carns's avatar
Philip Carns committed
102
DARSHAN_FORWARD_DECL(freopen, FILE*, (const char *path, const char *mode, FILE *stream));
Philip Carns's avatar
Philip Carns committed
103
DARSHAN_FORWARD_DECL(freopen64, FILE*, (const char *path, const char *mode, FILE *stream));
104
DARSHAN_FORWARD_DECL(fclose, int, (FILE *fp));
Philip Carns's avatar
Philip Carns committed
105
DARSHAN_FORWARD_DECL(fflush, int, (FILE *fp));
106
DARSHAN_FORWARD_DECL(fwrite, size_t, (const void *ptr, size_t size, size_t nmemb, FILE *stream));
Philip Carns's avatar
Philip Carns committed
107
DARSHAN_FORWARD_DECL(fputc, int, (int c, FILE *stream));
Philip Carns's avatar
Philip Carns committed
108
DARSHAN_FORWARD_DECL(putw, int, (int w, FILE *stream));
Philip Carns's avatar
Philip Carns committed
109
DARSHAN_FORWARD_DECL(fputs, int, (const char *s, FILE *stream));
Philip Carns's avatar
Philip Carns committed
110
111
DARSHAN_FORWARD_DECL(fprintf, int, (FILE *stream, const char *format, ...));
DARSHAN_FORWARD_DECL(vfprintf, int, (FILE *stream, const char *format, va_list));
112
DARSHAN_FORWARD_DECL(fread, size_t, (void *ptr, size_t size, size_t nmemb, FILE *stream));
Philip Carns's avatar
Philip Carns committed
113
DARSHAN_FORWARD_DECL(fgetc, int, (FILE *stream));
Philip Carns's avatar
Philip Carns committed
114
DARSHAN_FORWARD_DECL(getw, int, (FILE *stream));
Philip Carns's avatar
Philip Carns committed
115
DARSHAN_FORWARD_DECL(_IO_getc, int, (FILE *stream));
Philip Carns's avatar
Philip Carns committed
116
DARSHAN_FORWARD_DECL(_IO_putc, int, (int, FILE *stream));
Philip Carns's avatar
Philip Carns committed
117
DARSHAN_FORWARD_DECL(fscanf, int, (FILE *stream, const char *format, ...));
Philip Carns's avatar
Philip Carns committed
118
DARSHAN_FORWARD_DECL(__isoc99_fscanf, int, (FILE *stream, const char *format, ...));
Philip Carns's avatar
Philip Carns committed
119
DARSHAN_FORWARD_DECL(vfscanf, int, (FILE *stream, const char *format, va_list ap));
Philip Carns's avatar
Philip Carns committed
120
DARSHAN_FORWARD_DECL(fgets, char*, (char *s, int size, FILE *stream));
121
DARSHAN_FORWARD_DECL(fseek, int, (FILE *stream, long offset, int whence));
Philip Carns's avatar
Philip Carns committed
122
DARSHAN_FORWARD_DECL(fseeko, int, (FILE *stream, off_t offset, int whence));
Philip Carns's avatar
Philip Carns committed
123
DARSHAN_FORWARD_DECL(fseeko64, int, (FILE *stream, off_t offset, int whence));
Philip Carns's avatar
Philip Carns committed
124
125
DARSHAN_FORWARD_DECL(fsetpos, int, (FILE *stream, const fpos_t *pos));
DARSHAN_FORWARD_DECL(fsetpos64, int, (FILE *stream, const fpos_t *pos));
Philip Carns's avatar
Philip Carns committed
126
DARSHAN_FORWARD_DECL(rewind, void, (FILE *stream));
127
128

/* The stdio_file_runtime structure maintains necessary runtime metadata
129
 * for the STDIO file record (darshan_stdio_record structure, defined in
130
131
132
133
134
135
136
137
 * 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
138
 * stored in the darshan_stdio_record struct because we don't want it to appear in
139
 * the final darshan log file.  We therefore associate a stdio_file_runtime
140
 * struct with each darshan_stdio_record struct in order to track this information.
141
142
  *
 * NOTE: There is a one-to-one mapping of stdio_file_runtime structs to
143
 * darshan_stdio_record structs.
144
 *
145
 * NOTE: The stdio_file_runtime struct contains a pointer to a darshan_stdio_record
146
 * struct (see the *file_record member) rather than simply embedding an entire
147
 * darshan_stdio_record struct.  This is done so that all of the darshan_stdio_record
148
149
150
151
152
 * structs can be kept contiguous in memory as a single array to simplify
 * reduction, compression, and storage.
 */
struct stdio_file_runtime
{
153
    struct darshan_stdio_record* file_record;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    int64_t offset;
    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;
198
    struct darshan_stdio_record* file_record_array;
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    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);
221
222
223
static void stdio_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype);
static int stdio_record_compare(const void* a, const void* b);
224
225
226
227

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

Philip Carns's avatar
Philip Carns committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#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; \
242
    file->file_record->counters[STDIO_OPENS] += 1; \
Philip Carns's avatar
Philip Carns committed
243
244
245
246
247
248
249
250
    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)


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#define STDIO_RECORD_READ(__fp, __bytes,  __tm1, __tm2) do{ \
    int64_t this_offset; \
    struct stdio_file_runtime* file; \
    file = stdio_file_by_stream(__fp); \
    if(!file) break; \
    this_offset = file->offset; \
    file->offset = this_offset + __bytes; \
    if(file->file_record->counters[STDIO_MAX_BYTE_READ] < (this_offset + __bytes - 1)) \
        file->file_record->counters[STDIO_MAX_BYTE_READ] = (this_offset + __bytes - 1); \
    file->file_record->counters[STDIO_BYTES_READ] += __bytes; \
    file->file_record->counters[STDIO_READS] += 1; \
    if(file->file_record->fcounters[STDIO_F_READ_START_TIMESTAMP] == 0 || \
     file->file_record->fcounters[STDIO_F_READ_START_TIMESTAMP] > __tm1) \
        file->file_record->fcounters[STDIO_F_READ_START_TIMESTAMP] = __tm1; \
    file->file_record->fcounters[STDIO_F_READ_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(file->file_record->fcounters[STDIO_F_READ_TIME], __tm1, __tm2, file->last_write_end); \
} while(0)

Philip Carns's avatar
Philip Carns committed
269
#define STDIO_RECORD_WRITE(__fp, __bytes,  __tm1, __tm2, __fflush_flag) do{ \
270
271
272
273
274
275
276
277
278
    int64_t this_offset; \
    struct stdio_file_runtime* file; \
    file = stdio_file_by_stream(__fp); \
    if(!file) break; \
    this_offset = file->offset; \
    file->offset = this_offset + __bytes; \
    if(file->file_record->counters[STDIO_MAX_BYTE_WRITTEN] < (this_offset + __bytes - 1)) \
        file->file_record->counters[STDIO_MAX_BYTE_WRITTEN] = (this_offset + __bytes - 1); \
    file->file_record->counters[STDIO_BYTES_WRITTEN] += __bytes; \
Philip Carns's avatar
Philip Carns committed
279
280
281
282
    if(__fflush_flag) \
        file->file_record->counters[STDIO_FLUSHES] += 1; \
    else \
        file->file_record->counters[STDIO_WRITES] += 1; \
283
284
285
286
287
288
    if(file->file_record->fcounters[STDIO_F_WRITE_START_TIMESTAMP] == 0 || \
     file->file_record->fcounters[STDIO_F_WRITE_START_TIMESTAMP] > __tm1) \
        file->file_record->fcounters[STDIO_F_WRITE_START_TIMESTAMP] = __tm1; \
    file->file_record->fcounters[STDIO_F_WRITE_END_TIMESTAMP] = __tm2; \
    DARSHAN_TIMER_INC_NO_OVERLAP(file->file_record->fcounters[STDIO_F_WRITE_TIME], __tm1, __tm2, file->last_write_end); \
} while(0)
Philip Carns's avatar
Philip Carns committed
289

Philip Carns's avatar
Philip Carns committed
290
FILE* DARSHAN_DECL(fopen)(const char *path, const char *mode)
Philip Carns's avatar
Philip Carns committed
291
292
293
294
{
    FILE* ret;
    double tm1, tm2;

Philip Carns's avatar
Philip Carns committed
295
    MAP_OR_FAIL(fopen);
Philip Carns's avatar
Philip Carns committed
296
297

    tm1 = darshan_core_wtime();
Philip Carns's avatar
Philip Carns committed
298
    ret = __real_fopen(path, mode);
Philip Carns's avatar
Philip Carns committed
299
300
301
302
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
Philip Carns's avatar
Philip Carns committed
303
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
Philip Carns's avatar
Philip Carns committed
304
305
306
307
308
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
309
FILE* DARSHAN_DECL(fopen64)(const char *path, const char *mode)
310
311
{
    FILE* ret;
Philip Carns's avatar
Philip Carns committed
312
    double tm1, tm2;
313
314
315

    MAP_OR_FAIL(fopen);

Philip Carns's avatar
Philip Carns committed
316
    tm1 = darshan_core_wtime();
Philip Carns's avatar
Philip Carns committed
317
    ret = __real_fopen64(path, mode);
Philip Carns's avatar
Philip Carns committed
318
319
320
321
322
323
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
    STDIO_UNLOCK();
324
325
326
327

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
328
FILE* DARSHAN_DECL(fdopen)(int fd, const char *mode)
329
330
{
    FILE* ret;
Philip Carns's avatar
Philip Carns committed
331
    double tm1, tm2;
332

Philip Carns's avatar
Philip Carns committed
333
    MAP_OR_FAIL(fdopen);
334

Philip Carns's avatar
Philip Carns committed
335
    tm1 = darshan_core_wtime();
Philip Carns's avatar
Philip Carns committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
    ret = __real_fdopen(fd, mode);
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
    STDIO_RECORD_OPEN(ret, "UNKNOWN-FDOPEN", tm1, tm2);
    STDIO_UNLOCK();

    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
356
357
358
359
360
361
    tm2 = darshan_core_wtime();

    STDIO_LOCK();
    stdio_runtime_initialize();
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
    STDIO_UNLOCK();
362
363
364
365

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    STDIO_RECORD_OPEN(ret, path, tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret >= 0)
        STDIO_RECORD_WRITE(fp, 0, tm1, tm2, 1);
    STDIO_UNLOCK();

    return(ret);
}

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
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)
    {
        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);
}

437
438
439
440
441
442
443
444
445
446
447
448
449
450
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret > 0)
Philip Carns's avatar
Philip Carns committed
451
        STDIO_RECORD_WRITE(stream, size*ret, tm1, tm2, 0);
452
453
454
455
456
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != EOF)
        STDIO_RECORD_WRITE(stream, 1, tm1, tm2, 0);
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != EOF)
        STDIO_RECORD_WRITE(stream, sizeof(int), tm1, tm2, 0);
    STDIO_UNLOCK();

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != EOF && ret > 0)
        STDIO_RECORD_WRITE(stream, strlen(s), tm1, tm2, 0);
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
int DARSHAN_DECL(vfprintf)(FILE *stream, const char *format, va_list ap)
{
    int ret;
    double tm1, tm2;
    long start_off, end_off;

    MAP_OR_FAIL(vfprintf);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret > 0)
        STDIO_RECORD_WRITE(stream, (end_off-start_off), tm1, tm2, 0);
    STDIO_UNLOCK();

    return(ret);
}


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

    MAP_OR_FAIL(vfprintf);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret > 0)
        STDIO_RECORD_WRITE(stream, (end_off-start_off), tm1, tm2, 0);
    STDIO_UNLOCK();

    return(ret);
}

573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret > 0)
        STDIO_RECORD_READ(stream, size*ret, tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
593
594
595
596
597
598
599
600
601
602
603
604
605
size_t DARSHAN_DECL(fgetc)(FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(fgetc);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
Philip Carns's avatar
Philip Carns committed
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
    if(ret != EOF)
        STDIO_RECORD_READ(stream, 1, tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}

/* NOTE: stdio.h typically implements getc() as a macro pointing to _IO_getc */
size_t DARSHAN_DECL(_IO_getc)(FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(_IO_getc);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
Philip Carns's avatar
Philip Carns committed
627
628
629
630
631
632
633
    if(ret != EOF)
        STDIO_RECORD_READ(stream, 1, tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/* NOTE: stdio.h typically implements putc() as a macro pointing to _IO_putc */
size_t DARSHAN_DECL(_IO_putc)(int c, FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(_IO_putc);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != EOF)
        STDIO_RECORD_WRITE(stream, 1, tm1, tm2, 0);
    STDIO_UNLOCK();

    return(ret);
}
Philip Carns's avatar
Philip Carns committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
size_t DARSHAN_DECL(getw)(FILE *stream)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(getw);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != EOF || ferror(stream) == 0)
        STDIO_RECORD_READ(stream, sizeof(int), tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/* 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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != 0)
        STDIO_RECORD_READ(stream, (end_off-start_off), tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
706

Philip Carns's avatar
Philip Carns committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != 0)
Philip Carns's avatar
Philip Carns committed
730
        STDIO_RECORD_READ(stream, (end_off-start_off), tm1, tm2);
Philip Carns's avatar
Philip Carns committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
    STDIO_UNLOCK();

    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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != 0)
        STDIO_RECORD_READ(stream, end_off-start_off, tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
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();

    STDIO_LOCK();
    stdio_runtime_initialize();
    if(ret != NULL)
        STDIO_RECORD_READ(stream, strlen(ret), tm1, tm2);
    STDIO_UNLOCK();

    return(ret);
}


Philip Carns's avatar
Philip Carns committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
void DARSHAN_DECL(rewind)(FILE *stream)
{
    struct stdio_file_runtime* file;
    double tm1, tm2;

    MAP_OR_FAIL(rewind);

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

    STDIO_LOCK();
    stdio_runtime_initialize();
    file = stdio_file_by_stream(stream);
    if(file)
    {
        file->offset = 0;
        DARSHAN_TIMER_INC_NO_OVERLAP(
            file->file_record->fcounters[STDIO_F_META_TIME],
            tm1, tm2, file->last_meta_end);
        file->file_record->counters[STDIO_SEEKS] += 1;
    }
    STDIO_UNLOCK();

    return;
}

808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
int DARSHAN_DECL(fseek)(FILE *stream, long offset, int whence)
{
    int ret;
    struct stdio_file_runtime* file;
    double tm1, tm2;

    MAP_OR_FAIL(fseek);

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

    if(ret >= 0)
    {
        STDIO_LOCK();
        stdio_runtime_initialize();
Philip Carns's avatar
Philip Carns committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
        file = stdio_file_by_stream(stream);
        if(file)
        {
            file->offset = ftell(stream);
            DARSHAN_TIMER_INC_NO_OVERLAP(
                file->file_record->fcounters[STDIO_F_META_TIME],
                tm1, tm2, file->last_meta_end);
            file->file_record->counters[STDIO_SEEKS] += 1;
        }
        STDIO_UNLOCK();
    }

    return(ret);
}

int DARSHAN_DECL(fseeko)(FILE *stream, off_t offset, int whence)
{
    int ret;
    struct stdio_file_runtime* file;
    double tm1, tm2;

    MAP_OR_FAIL(fseeko);

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

    if(ret >= 0)
    {
        STDIO_LOCK();
        stdio_runtime_initialize();
Philip Carns's avatar
Philip Carns committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
        file = stdio_file_by_stream(stream);
        if(file)
        {
            file->offset = ftell(stream);
            DARSHAN_TIMER_INC_NO_OVERLAP(
                file->file_record->fcounters[STDIO_F_META_TIME],
                tm1, tm2, file->last_meta_end);
            file->file_record->counters[STDIO_SEEKS] += 1;
        }
        STDIO_UNLOCK();
    }

    return(ret);
}

int DARSHAN_DECL(fseeko64)(FILE *stream, off_t offset, int whence)
{
    int ret;
    struct stdio_file_runtime* file;
    double tm1, tm2;

    MAP_OR_FAIL(fseeko64);

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

    if(ret >= 0)
    {
        STDIO_LOCK();
        stdio_runtime_initialize();
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
        file = stdio_file_by_stream(stream);
        if(file)
        {
            file->offset = ftell(stream);
            DARSHAN_TIMER_INC_NO_OVERLAP(
                file->file_record->fcounters[STDIO_F_META_TIME],
                tm1, tm2, file->last_meta_end);
            file->file_record->counters[STDIO_SEEKS] += 1;
        }
        STDIO_UNLOCK();
    }

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
int DARSHAN_DECL(fsetpos)(FILE *stream, const fpos_t *pos)
{
    int ret;
    struct stdio_file_runtime* file;
    double tm1, tm2;

    MAP_OR_FAIL(fsetpos);

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

    if(ret >= 0)
    {
        STDIO_LOCK();
        stdio_runtime_initialize();
        file = stdio_file_by_stream(stream);
        if(file)
        {
            file->offset = ftell(stream);
            DARSHAN_TIMER_INC_NO_OVERLAP(
                file->file_record->fcounters[STDIO_F_META_TIME],
                tm1, tm2, file->last_meta_end);
            file->file_record->counters[STDIO_SEEKS] += 1;
        }
        STDIO_UNLOCK();
    }

    return(ret);
}

int DARSHAN_DECL(fsetpos64)(FILE *stream, const fpos_t *pos)
{
    int ret;
    struct stdio_file_runtime* file;
    double tm1, tm2;

    MAP_OR_FAIL(fsetpos64);

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

    if(ret >= 0)
    {
        STDIO_LOCK();
        stdio_runtime_initialize();
        file = stdio_file_by_stream(stream);
        if(file)
        {
            file->offset = ftell(stream);
            DARSHAN_TIMER_INC_NO_OVERLAP(
                file->file_record->fcounters[STDIO_F_META_TIME],
                tm1, tm2, file->last_meta_end);
            file->file_record->counters[STDIO_SEEKS] += 1;
        }
        STDIO_UNLOCK();
    }

    return(ret);
}


964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
/**********************************************************
 * 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? */
1003
    stdio_runtime->file_array_size = mem_limit / sizeof(struct darshan_stdio_record);
1004
1005
1006
1007
1008
1009
    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 *
1010
                                              sizeof(struct darshan_stdio_record));
1011
1012
1013
1014
1015
1016
1017
1018
    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 *
1019
           sizeof(struct darshan_stdio_record));
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
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
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
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
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187

    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)
{
1188
1189
1190
1191
1192
1193
1194
    struct stdio_file_runtime *file;
    int i;
    struct darshan_stdio_record *red_send_buf = NULL;
    struct darshan_stdio_record *red_recv_buf = NULL;
    MPI_Datatype red_type;
    MPI_Op red_op;

1195
1196
    assert(stdio_runtime);

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
    /* 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++)
        {
            HASH_FIND(hlink, stdio_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(stdio_runtime->file_record_array, stdio_runtime->file_array_ndx,
            sizeof(struct darshan_stdio_record), stdio_record_compare);

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

        /* allocate memory for the reduction output on rank 0 */
        if(my_rank == 0)
        {
            red_recv_buf = malloc(shared_rec_count * sizeof(struct darshan_stdio_record));
            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
         */
        DARSHAN_MPI_CALL(PMPI_Type_contiguous)(sizeof(struct darshan_stdio_record),
            MPI_BYTE, &red_type);
        DARSHAN_MPI_CALL(PMPI_Type_commit)(&red_type);

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

        /* reduce shared STDIO 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 = stdio_runtime->file_array_ndx - shared_rec_count;
            memcpy(&(stdio_runtime->file_record_array[tmp_ndx]), red_recv_buf,
                shared_rec_count * sizeof(struct darshan_stdio_record));
            free(red_recv_buf);
        }
        else
        {
            stdio_runtime->file_array_ndx -= shared_rec_count;
        }

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

1265
    *stdio_buf = (void *)(stdio_runtime->file_record_array);
1266
    *stdio_buf_sz = stdio_runtime->file_array_ndx * sizeof(struct darshan_stdio_record);
1267
1268
1269
1270

    return;
}

1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
/* compare function for sorting file records by descending rank */
static int stdio_record_compare(const void* a_p, const void* b_p)
{
    const struct darshan_stdio_record* a = a_p;
    const struct darshan_stdio_record* b = b_p;

    if(a->rank < b->rank)
        return 1;
    if(a->rank > b->rank)
        return -1;

    return 0;
}

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

    assert(stdio_runtime);

    for(i=0; i<*len; i++)
    {
        memset(&tmp_file, 0, sizeof(struct darshan_stdio_record));
        tmp_file.f_id = infile->f_id;
        tmp_file.rank = -1;

        /* 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];
        }

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

        /* 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];
        }

        /* 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];
        }

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

    return;
}

1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
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
 */