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

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

#include "darshan.h"

27
#ifndef HAVE_OFF64_T
28
29
30
typedef int64_t off64_t;
#endif

31
32
extern char* __progname_full;

33
#ifdef DARSHAN_PRELOAD
34
35
36
#define __USE_GNU
#include <dlfcn.h>
#include <stdlib.h>
37

38
39
40
41
42
#define DARSHAN_FORWARD_DECL(name,ret,args) \
  ret (*__real_ ## name)args = NULL;

#define DARSHAN_DECL(__name) __name

43
44
45
46
47
48
49
50
51
52
53
54
55
#define DARSHAN_MPI_CALL(func) __real_ ## func

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


56
extern double (*__real_PMPI_Wtime)(void);
57

58
59
60
61
62
63
64
#else

#define DARSHAN_FORWARD_DECL(name,ret,args) \
  extern ret __real_ ## name args;

#define DARSHAN_DECL(__name) __wrap_ ## __name

65
66
67
68
#define MAP_OR_FAIL(func)

#define DARSHAN_MPI_CALL(func) func

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#endif

DARSHAN_FORWARD_DECL(creat, int, (const char* path, mode_t mode));
DARSHAN_FORWARD_DECL(creat64, int, (const char* path, mode_t mode));
DARSHAN_FORWARD_DECL(open, int, (const char *path, int flags, ...));
DARSHAN_FORWARD_DECL(open64, int, (const char *path, int flags, ...));
DARSHAN_FORWARD_DECL(close, int, (int fd));
DARSHAN_FORWARD_DECL(write, ssize_t, (int fd, const void *buf, size_t count));
DARSHAN_FORWARD_DECL(read, ssize_t, (int fd, void *buf, size_t count));
DARSHAN_FORWARD_DECL(lseek, off_t, (int fd, off_t offset, int whence));
DARSHAN_FORWARD_DECL(lseek64, off64_t, (int fd, off64_t offset, int whence));
DARSHAN_FORWARD_DECL(pread, ssize_t, (int fd, void *buf, size_t count, off_t offset));
DARSHAN_FORWARD_DECL(pread64, ssize_t, (int fd, void *buf, size_t count, off64_t offset));
DARSHAN_FORWARD_DECL(pwrite, ssize_t, (int fd, const void *buf, size_t count, off_t offset));
DARSHAN_FORWARD_DECL(pwrite64, ssize_t, (int fd, const void *buf, size_t count, off64_t offset
));
DARSHAN_FORWARD_DECL(readv, ssize_t, (int fd, const struct iovec *iov, int iovcnt));
DARSHAN_FORWARD_DECL(writev, ssize_t, (int fd, const struct iovec *iov, int iovcnt));
DARSHAN_FORWARD_DECL(__fxstat, int, (int vers, int fd, struct stat *buf));
DARSHAN_FORWARD_DECL(__fxstat64, int, (int vers, int fd, struct stat64 *buf));
DARSHAN_FORWARD_DECL(__lxstat, int, (int vers, const char* path, struct stat *buf));
DARSHAN_FORWARD_DECL(__lxstat64, int, (int vers, const char* path, struct stat64 *buf));
DARSHAN_FORWARD_DECL(__xstat, int, (int vers, const char* path, struct stat *buf));
DARSHAN_FORWARD_DECL(__xstat64, int, (int vers, const char* path, struct stat64 *buf));
DARSHAN_FORWARD_DECL(mmap, void*, (void *addr, size_t length, int prot, int flags, int fd, off_t offset));
DARSHAN_FORWARD_DECL(mmap64, void*, (void *addr, size_t length, int prot, int flags, int fd, off64_t offset));
DARSHAN_FORWARD_DECL(fopen, FILE*, (const char *path, const char *mode));
DARSHAN_FORWARD_DECL(fopen64, FILE*, (const char *path, const char *mode));
DARSHAN_FORWARD_DECL(fclose, int, (FILE *fp));
DARSHAN_FORWARD_DECL(fread, size_t, (void *ptr, size_t size, size_t nmemb, FILE *stream));
DARSHAN_FORWARD_DECL(fwrite, size_t, (const void *ptr, size_t size, size_t nmemb, FILE *stream));
DARSHAN_FORWARD_DECL(fseek, int, (FILE *stream, long offset, int whence));
DARSHAN_FORWARD_DECL(fsync, int, (int fd));
DARSHAN_FORWARD_DECL(fdatasync, int, (int fd));

104
pthread_mutex_t cp_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
105
106
struct darshan_job_runtime* darshan_global_job = NULL;
static int my_rank = -1;
107
static struct stat64 cp_stat_buf;
108
static int darshan_mem_alignment = 1;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

/* these are paths that we will not trace */
static char* exclusions[] = {
"/etc/",
"/dev/",
"/usr/",
"/bin/",
"/boot/",
"/lib/",
"/opt/",
"/sbin/",
"/sys/",
"/proc/",
NULL
};

125
126
static double posix_wtime(void);

127
128
static void cp_access_counter(struct darshan_file_runtime* file, ssize_t size,     enum cp_counter_type type);

129
130
131
132
133
134
135
136
137
static struct darshan_file_ref* ref_by_handle(
    const void* handle,
    int handle_sz,
    enum darshan_handle_type handle_type);

static struct darshan_file_runtime* darshan_file_by_fd(int fd);
static void darshan_file_close_fd(int fd);
static struct darshan_file_runtime* darshan_file_by_name_setfd(const char* name, int fd);

Philip Carns's avatar
Philip Carns committed
138
#define CP_RECORD_WRITE(__ret, __fd, __count, __pwrite_flag, __pwrite_offset, __aligned, __stream_flag, __tm1, __tm2) do{ \
139
    size_t stride; \
Philip Carns's avatar
Philip Carns committed
140
    int64_t this_offset; \
141
142
    int64_t file_alignment; \
    struct darshan_file_runtime* file; \
143
    double __elapsed = __tm2-__tm1; \
144
145
146
    if(__ret < 0) break; \
    file = darshan_file_by_fd(__fd); \
    if(!file) break; \
Philip Carns's avatar
Philip Carns committed
147
148
149
150
    if(__pwrite_flag) \
        this_offset = __pwrite_offset; \
    else \
        this_offset = file->offset; \
151
    file_alignment = CP_VALUE(file, CP_FILE_ALIGNMENT); \
Philip Carns's avatar
Philip Carns committed
152
    if(this_offset > file->last_byte_written) \
153
        CP_INC(file, CP_SEQ_WRITES, 1); \
Philip Carns's avatar
Philip Carns committed
154
    if(this_offset == (file->last_byte_written + 1)) \
155
        CP_INC(file, CP_CONSEC_WRITES, 1); \
Philip Carns's avatar
Philip Carns committed
156
    if(this_offset > 0 && this_offset > file->last_byte_written \
157
        && file->last_byte_written != 0) \
Philip Carns's avatar
Philip Carns committed
158
        stride = this_offset - file->last_byte_written - 1; \
159
160
    else \
        stride = 0; \
Philip Carns's avatar
Philip Carns committed
161
162
163
    file->last_byte_written = this_offset + __ret - 1; \
    file->offset = this_offset + __ret; \
    CP_MAX(file, CP_MAX_BYTE_WRITTEN, (this_offset + __ret -1)); \
164
165
166
167
168
169
170
171
172
    CP_INC(file, CP_BYTES_WRITTEN, __ret); \
    if(__stream_flag) \
        CP_INC(file, CP_POSIX_FWRITES, 1); \
    else \
        CP_INC(file, CP_POSIX_WRITES, 1); \
    CP_BUCKET_INC(file, CP_SIZE_WRITE_0_100, __ret); \
    cp_access_counter(file, stride, CP_COUNTER_STRIDE); \
    if(!__aligned) \
        CP_INC(file, CP_MEM_NOT_ALIGNED, 1); \
Philip Carns's avatar
Philip Carns committed
173
    if(file_alignment > 0 && (this_offset % file_alignment) != 0) \
174
175
176
177
178
        CP_INC(file, CP_FILE_NOT_ALIGNED, 1); \
    cp_access_counter(file, __ret, CP_COUNTER_ACCESS); \
    if(file->last_io_type == CP_READ) \
        CP_INC(file, CP_RW_SWITCHES, 1); \
    file->last_io_type = CP_WRITE; \
179
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
180
181
182
    if(CP_F_VALUE(file, CP_F_WRITE_START_TIMESTAMP) == 0) \
        CP_F_SET(file, CP_F_WRITE_START_TIMESTAMP, __tm1); \
    CP_F_SET(file, CP_F_WRITE_END_TIMESTAMP, __tm2); \
183
184
185
    if(CP_F_VALUE(file, CP_F_MAX_WRITE_TIME) < __elapsed){ \
        CP_F_SET(file, CP_F_MAX_WRITE_TIME, __elapsed); \
        CP_SET(file, CP_MAX_WRITE_TIME_SIZE, __ret); } \
186
187
} while(0)

188
#define CP_RECORD_READ(__ret, __fd, __count, __pread_flag, __pread_offset, __aligned, __stream_flag, __tm1, __tm2) do{ \
189
    size_t stride; \
190
    int64_t this_offset; \
191
192
    struct darshan_file_runtime* file; \
    int64_t file_alignment; \
193
    double __elapsed = __tm2-__tm1; \
194
195
196
    if(__ret < 0) break; \
    file = darshan_file_by_fd(__fd); \
    if(!file) break; \
197
198
199
200
    if(__pread_flag)\
        this_offset = __pread_offset; \
    else \
        this_offset = file->offset; \
201
    file_alignment = CP_VALUE(file, CP_FILE_ALIGNMENT); \
202
    if(this_offset > file->last_byte_read) \
203
        CP_INC(file, CP_SEQ_READS, 1); \
204
    if(this_offset == (file->last_byte_read + 1)) \
205
        CP_INC(file, CP_CONSEC_READS, 1); \
206
    if(this_offset > 0 && this_offset > file->last_byte_read \
207
        && file->last_byte_read != 0) \
208
        stride = this_offset - file->last_byte_read - 1; \
209
210
    else \
        stride = 0; \
211
212
213
    file->last_byte_read = this_offset + __ret - 1; \
    CP_MAX(file, CP_MAX_BYTE_READ, (this_offset + __ret -1)); \
    file->offset = this_offset + __ret; \
214
215
216
217
218
219
220
221
222
    CP_INC(file, CP_BYTES_READ, __ret); \
    if(__stream_flag)\
        CP_INC(file, CP_POSIX_FREADS, 1); \
    else\
        CP_INC(file, CP_POSIX_READS, 1); \
    CP_BUCKET_INC(file, CP_SIZE_READ_0_100, __ret); \
    cp_access_counter(file, stride, CP_COUNTER_STRIDE); \
    if(!__aligned) \
        CP_INC(file, CP_MEM_NOT_ALIGNED, 1); \
Philip Carns's avatar
Philip Carns committed
223
    if(file_alignment > 0 && (this_offset % file_alignment) != 0) \
224
225
226
227
228
        CP_INC(file, CP_FILE_NOT_ALIGNED, 1); \
    cp_access_counter(file, __ret, CP_COUNTER_ACCESS); \
    if(file->last_io_type == CP_WRITE) \
        CP_INC(file, CP_RW_SWITCHES, 1); \
    file->last_io_type = CP_READ; \
229
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_read_end, CP_F_POSIX_READ_TIME); \
230
231
232
    if(CP_F_VALUE(file, CP_F_READ_START_TIMESTAMP) == 0) \
        CP_F_SET(file, CP_F_READ_START_TIMESTAMP, __tm1); \
    CP_F_SET(file, CP_F_READ_END_TIMESTAMP, __tm2); \
233
234
235
    if(CP_F_VALUE(file, CP_F_MAX_READ_TIME) < __elapsed){ \
        CP_F_SET(file, CP_F_MAX_READ_TIME, __elapsed); \
        CP_SET(file, CP_MAX_READ_TIME_SIZE, __ret); } \
236
237
} while(0)

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#define CP_LOOKUP_RECORD_STAT(__path, __statbuf, __tm1, __tm2) do { \
    char* exclude; \
    int tmp_index = 0; \
    struct darshan_file_runtime* file; \
    while((exclude = exclusions[tmp_index])) { \
        if(!(strncmp(exclude, __path, strlen(exclude)))) \
            break; \
        tmp_index++; \
    } \
    if(exclude) break; \
    file = darshan_file_by_name(__path); \
    if (file) \
    { \
        CP_RECORD_STAT(file, __statbuf, __tm1, __tm2); \
    } \
} while(0)

    
256
257
258
259
260
261
262
#define CP_RECORD_STAT(__file, __statbuf, __tm1, __tm2) do { \
    if(!CP_VALUE((__file), CP_FILE_ALIGNMENT)){ \
        CP_SET((__file), CP_DEVICE, (__statbuf)->st_dev); \
        CP_SET((__file), CP_FILE_ALIGNMENT, (__statbuf)->st_blksize); \
        CP_SET((__file), CP_SIZE_AT_OPEN, (__statbuf)->st_size); \
    }\
    (__file)->log_file->rank = my_rank; \
263
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME); \
264
265
266
    CP_INC(__file, CP_POSIX_STATS, 1); \
} while(0)

267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
static inline dev_t get_device(const char* path, struct stat64* statbuf)
{
    dev_t device = 0;

#ifdef __CP_ST_DEV_WORKAROUND
    struct stat64 dirstat;
    char* tmp_path = strdup(path);
    char* parent = dirname(tmp_path);
    if(parent && stat64(parent, &dirstat) == 0)
        device = dirstat.st_dev;
    if(tmp_path) free(tmp_path);
#else
    device = statbuf->st_dev;
#endif

    return(device);
}

Philip Carns's avatar
Philip Carns committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifdef __CP_STAT_AT_OPEN
#define CP_STAT_FILE(_f, _p) do { \
    if(!CP_VALUE(_f, CP_FILE_ALIGNMENT)){ \
        if(stat64(_p, &cp_stat_buf) == 0) { \
            CP_SET(_f, CP_DEVICE, get_device(_p, &cp_stat_buf)); \
            CP_SET(_f, CP_FILE_ALIGNMENT, cp_stat_buf.st_blksize); \
            CP_SET(_f, CP_SIZE_AT_OPEN, cp_stat_buf.st_size); \
        }\
    }\
}while(0)
#else
#define CP_STAT_FILE(_f, _p) do { }while(0)
#endif

299
300
301
302
303
304
305
306
307
308
309
#define CP_RECORD_OPEN(__ret, __path, __mode, __stream_flag, __tm1, __tm2) do { \
    struct darshan_file_runtime* file; \
    char* exclude; \
    int tmp_index = 0; \
    if(__ret < 0) break; \
    while((exclude = exclusions[tmp_index])) { \
        if(!(strncmp(exclude, __path, strlen(exclude)))) \
            break; \
        tmp_index++; \
    } \
    if(exclude) break; \
310
    file = darshan_file_by_name_setfd(__path, __ret); \
311
    if(!file) break; \
Philip Carns's avatar
Philip Carns committed
312
    CP_STAT_FILE(file, __path); \
313
314
315
316
317
318
319
320
321
322
323
    file->log_file->rank = my_rank; \
    if(__mode) \
        CP_SET(file, CP_MODE, __mode); \
    file->offset = 0; \
    file->last_byte_written = 0; \
    file->last_byte_read = 0; \
    if(__stream_flag)\
        CP_INC(file, CP_POSIX_FOPENS, 1); \
    else \
        CP_INC(file, CP_POSIX_OPENS, 1); \
    if(CP_F_VALUE(file, CP_F_OPEN_TIMESTAMP) == 0) \
324
        CP_F_SET(file, CP_F_OPEN_TIMESTAMP, posix_wtime()); \
325
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME); \
326
327
} while (0)

328
int DARSHAN_DECL(close)(int fd)
329
330
331
332
333
334
{
    struct darshan_file_runtime* file;
    int tmp_fd = fd;
    double tm1, tm2;
    int ret;

335
336
    MAP_OR_FAIL(close);

337
338
339
340
341
342
343
344
345
346
    tm1 = darshan_wtime();
    ret = __real_close(fd);
    tm2 = darshan_wtime();

    CP_LOCK();
    file = darshan_file_by_fd(tmp_fd);
    if(file)
    {
        file->last_byte_written = 0;
        file->last_byte_read = 0;
347
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, posix_wtime());
348
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
349
        darshan_file_close_fd(tmp_fd);
350
351
352
353
354
355
    }
    CP_UNLOCK();

    return(ret);
}

356
int DARSHAN_DECL(fclose)(FILE *fp)
357
358
359
360
361
362
{
    struct darshan_file_runtime* file;
    int tmp_fd = fileno(fp);
    double tm1, tm2;
    int ret;

363
364
    MAP_OR_FAIL(fclose);

365
366
367
368
369
370
371
372
373
374
    tm1 = darshan_wtime();
    ret = __real_fclose(fp);
    tm2 = darshan_wtime();
    
    CP_LOCK();
    file = darshan_file_by_fd(tmp_fd);
    if(file)
    {
        file->last_byte_written = 0;
        file->last_byte_read = 0;
375
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, posix_wtime());
376
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
377
        darshan_file_close_fd(tmp_fd);
378
379
380
381
382
383
384
    }
    CP_UNLOCK();

    return(ret);
}


385
int DARSHAN_DECL(fsync)(int fd)
386
387
388
389
390
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

391
392
    MAP_OR_FAIL(fsync);

393
394
395
396
397
398
399
400
401
402
403
    tm1 = darshan_wtime();
    ret = __real_fsync(fd);
    tm2 = darshan_wtime();

    if(ret < 0)
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
404
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
405
406
407
408
409
410
411
        CP_INC(file, CP_POSIX_FSYNCS, 1);
    }
    CP_UNLOCK();

    return(ret);
}

412
int DARSHAN_DECL(fdatasync)(int fd)
413
414
415
416
417
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

418
419
    MAP_OR_FAIL(fdatasync);

420
421
422
423
424
425
426
427
428
429
    tm1 = darshan_wtime();
    ret = __real_fdatasync(fd);
    tm2 = darshan_wtime();
    if(ret < 0)
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
430
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
431
432
433
434
435
436
437
438
        CP_INC(file, CP_POSIX_FDSYNCS, 1);
    }
    CP_UNLOCK();

    return(ret);
}


439
void* DARSHAN_DECL(mmap64)(void *addr, size_t length, int prot, int flags,
440
441
442
443
444
    int fd, off64_t offset)
{
    void* ret;
    struct darshan_file_runtime* file;

445
446
    MAP_OR_FAIL(mmap64);

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    ret = __real_mmap64(addr, length, prot, flags, fd, offset);
    if(ret == MAP_FAILED)
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
        CP_INC(file, CP_POSIX_MMAPS, 1);
    }
    CP_UNLOCK();

    return(ret);
}


463
void* DARSHAN_DECL(mmap)(void *addr, size_t length, int prot, int flags,
464
465
466
467
468
    int fd, off_t offset)
{
    void* ret;
    struct darshan_file_runtime* file;

469
470
    MAP_OR_FAIL(mmap);

471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    ret = __real_mmap(addr, length, prot, flags, fd, offset);
    if(ret == MAP_FAILED)
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
        CP_INC(file, CP_POSIX_MMAPS, 1);
    }
    CP_UNLOCK();

    return(ret);
}

486
int DARSHAN_DECL(creat)(const char* path, mode_t mode)
487
488
489
490
{
    int ret;
    double tm1, tm2;

491
492
    MAP_OR_FAIL(creat);

493
494
495
496
497
498
499
500
501
502
503
    tm1 = darshan_wtime();
    ret = __real_creat(path, mode);
    tm2 = darshan_wtime();

    CP_LOCK();
    CP_RECORD_OPEN(ret, path, mode, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

504
int DARSHAN_DECL(creat64)(const char* path, mode_t mode)
505
506
507
508
{
    int ret;
    double tm1, tm2;

509
510
    MAP_OR_FAIL(creat64);

511
512
513
514
515
516
517
518
519
520
521
    tm1 = darshan_wtime();
    ret = __real_creat64(path, mode);
    tm2 = darshan_wtime();

    CP_LOCK();
    CP_RECORD_OPEN(ret, path, mode, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

522
int DARSHAN_DECL(open64)(const char* path, int flags, ...)
523
524
525
526
527
{
    int mode = 0;
    int ret;
    double tm1, tm2;

528
529
    MAP_OR_FAIL(open64);

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
    if (flags & O_CREAT) 
    {
        va_list arg;
        va_start(arg, flags);
        mode = va_arg(arg, int);
        va_end(arg);

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

    CP_LOCK();
    CP_RECORD_OPEN(ret, path, mode, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

555
int DARSHAN_DECL(open)(const char *path, int flags, ...)
556
557
558
559
560
{
    int mode = 0;
    int ret;
    double tm1, tm2;

561
562
    MAP_OR_FAIL(open);

563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
    if (flags & O_CREAT) 
    {
        va_list arg;
        va_start(arg, flags);
        mode = va_arg(arg, int);
        va_end(arg);

        tm1 = darshan_wtime();
        ret = __real_open(path, flags, mode);
        tm2 = darshan_wtime();
    }
    else
    {
        tm1 = darshan_wtime();
        ret = __real_open(path, flags);
        tm2 = darshan_wtime();
    }

    CP_LOCK();
    CP_RECORD_OPEN(ret, path, mode, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

588
FILE* DARSHAN_DECL(fopen64)(const char *path, const char *mode)
589
590
591
592
593
{
    FILE* ret;
    int fd;
    double tm1, tm2;

594
595
    MAP_OR_FAIL(fopen64);

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
    tm1 = darshan_wtime();
    ret = __real_fopen64(path, mode);
    tm2 = darshan_wtime();
    if(ret == 0)
        fd = -1;
    else
        fd = fileno(ret);

    CP_LOCK();
    CP_RECORD_OPEN(fd, path, 0, 1, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

611
FILE* DARSHAN_DECL(fopen)(const char *path, const char *mode)
612
613
614
615
616
{
    FILE* ret;
    int fd;
    double tm1, tm2;

617
618
    MAP_OR_FAIL(fopen);

619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
    tm1 = darshan_wtime();
    ret = __real_fopen(path, mode);
    tm2 = darshan_wtime();
    if(ret == 0)
        fd = -1;
    else
        fd = fileno(ret);

    CP_LOCK();
    CP_RECORD_OPEN(fd, path, 0, 1, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

634
int DARSHAN_DECL(__xstat64)(int vers, const char *path, struct stat64 *buf)
635
636
637
638
{
    int ret;
    double tm1, tm2;

639
640
    MAP_OR_FAIL(__xstat64);

641
642
643
644
645
646
647
    tm1 = darshan_wtime();
    ret = __real___xstat64(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
648
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
649
650
651
652
653
    CP_UNLOCK();

    return(ret);
}

654
int DARSHAN_DECL(__lxstat64)(int vers, const char *path, struct stat64 *buf)
655
656
657
658
{
    int ret;
    double tm1, tm2;

659
660
    MAP_OR_FAIL(__lxstat64);

661
662
663
664
665
666
667
    tm1 = darshan_wtime();
    ret = __real___lxstat64(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
668
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
669
670
671
672
673
    CP_UNLOCK();

    return(ret);
}

674
int DARSHAN_DECL(__fxstat64)(int vers, int fd, struct stat64 *buf)
675
676
677
678
679
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

680
681
    MAP_OR_FAIL(__fxstat64);

682
683
684
685
686
687
688
689
690
691
692
693
694
695
    tm1 = darshan_wtime();
    ret = __real___fxstat64(vers, fd, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    /* skip logging if this was triggered internally */
    if(buf == &cp_stat_buf)
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
696
        CP_RECORD_STAT(file, buf, tm1, tm2);
697
698
699
700
701
702
703
    }
    CP_UNLOCK();

    return(ret);
}


704
int DARSHAN_DECL(__xstat)(int vers, const char *path, struct stat *buf)
705
706
707
708
{
    int ret;
    double tm1, tm2;

709
710
    MAP_OR_FAIL(__xstat);

711
712
713
714
715
716
717
    tm1 = darshan_wtime();
    ret = __real___xstat(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
718
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
719
720
721
722
723
    CP_UNLOCK();

    return(ret);
}

724
int DARSHAN_DECL(__lxstat)(int vers, const char *path, struct stat *buf)
725
726
727
728
{
    int ret;
    double tm1, tm2;

729
730
    MAP_OR_FAIL(__lxstat);

731
732
733
734
735
736
737
    tm1 = darshan_wtime();
    ret = __real___lxstat(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
738
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
739
740
741
742
743
    CP_UNLOCK();

    return(ret);
}

744
int DARSHAN_DECL(__fxstat)(int vers, int fd, struct stat *buf)
745
746
747
748
749
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

750
751
    MAP_OR_FAIL(__fxstat);

752
753
754
755
756
757
758
    tm1 = darshan_wtime();
    ret = __real___fxstat(vers, fd, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    /* skip logging if this was triggered internally */
759
    if((void*)buf == (void*)&cp_stat_buf)
760
761
762
763
764
765
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
766
        CP_RECORD_STAT(file, buf, tm1, tm2);
767
768
769
770
771
772
    }
    CP_UNLOCK();

    return(ret);
}

773
ssize_t DARSHAN_DECL(pread64)(int fd, void *buf, size_t count, off64_t offset)
774
775
776
777
778
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

779
780
    MAP_OR_FAIL(pread64);

781
    if((unsigned long)buf % darshan_mem_alignment == 0)
782
783
784
785
786
787
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pread64(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
788
    CP_RECORD_READ(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
789
790
791
792
    CP_UNLOCK();
    return(ret);
}

793
ssize_t DARSHAN_DECL(pread)(int fd, void *buf, size_t count, off_t offset)
794
795
796
797
798
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

799
800
    MAP_OR_FAIL(pread);

801
    if((unsigned long)buf % darshan_mem_alignment == 0)
802
803
804
805
806
807
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pread(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
808
    CP_RECORD_READ(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
809
810
811
812
813
    CP_UNLOCK();
    return(ret);
}


814
ssize_t DARSHAN_DECL(pwrite)(int fd, const void *buf, size_t count, off_t offset)
815
816
817
818
819
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

820
821
    MAP_OR_FAIL(pwrite);

822
    if((unsigned long)buf % darshan_mem_alignment == 0)
823
824
825
826
827
828
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pwrite(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
829
    CP_RECORD_WRITE(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
830
831
832
833
    CP_UNLOCK();
    return(ret);
}

834
ssize_t DARSHAN_DECL(pwrite64)(int fd, const void *buf, size_t count, off64_t offset)
835
836
837
838
839
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

840
841
    MAP_OR_FAIL(pwrite64);

842
    if((unsigned long)buf % darshan_mem_alignment == 0)
843
844
845
846
847
848
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pwrite64(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
849
    CP_RECORD_WRITE(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
850
851
852
853
    CP_UNLOCK();
    return(ret);
}

854
ssize_t DARSHAN_DECL(readv)(int fd, const struct iovec *iov, int iovcnt)
855
856
857
858
859
860
{
    ssize_t ret;
    int aligned_flag = 1;
    int i;
    double tm1, tm2;

861
862
    MAP_OR_FAIL(readv);

863
864
    for(i=0; i<iovcnt; i++)
    {
865
        if(((unsigned long)iov[i].iov_base % darshan_mem_alignment) != 0)
866
867
868
869
870
871
872
            aligned_flag = 0;
    }

    tm1 = darshan_wtime();
    ret = __real_readv(fd, iov, iovcnt);
    tm2 = darshan_wtime();
    CP_LOCK();
873
    CP_RECORD_READ(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
874
875
876
877
    CP_UNLOCK();
    return(ret);
}

878
ssize_t DARSHAN_DECL(writev)(int fd, const struct iovec *iov, int iovcnt)
879
880
881
882
883
884
{
    ssize_t ret;
    int aligned_flag = 1;
    int i;
    double tm1, tm2;

885
886
    MAP_OR_FAIL(writev);

887
888
    for(i=0; i<iovcnt; i++)
    {
889
        if(!((unsigned long)iov[i].iov_base % darshan_mem_alignment == 0))
890
891
892
893
894
895
896
            aligned_flag = 0;
    }

    tm1 = darshan_wtime();
    ret = __real_writev(fd, iov, iovcnt);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
897
    CP_RECORD_WRITE(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
898
899
900
901
    CP_UNLOCK();
    return(ret);
}

902
size_t DARSHAN_DECL(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream)
903
904
905
906
907
{
    size_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

908
909
    MAP_OR_FAIL(fread);

910
    if((unsigned long)ptr % darshan_mem_alignment == 0)
911
912
913
914
915
916
917
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_fread(ptr, size, nmemb, stream);
    tm2 = darshan_wtime();
    CP_LOCK();
    if(ret > 0)
918
        CP_RECORD_READ(size*ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
919
    else
920
        CP_RECORD_READ(ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
921
922
923
924
    CP_UNLOCK();
    return(ret);
}

925
ssize_t DARSHAN_DECL(read)(int fd, void *buf, size_t count)
926
927
928
929
930
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

931
932
    MAP_OR_FAIL(read);

933
    if((unsigned long)buf % darshan_mem_alignment == 0)
934
935
936
937
938
939
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_read(fd, buf, count);
    tm2 = darshan_wtime();
    CP_LOCK();
940
    CP_RECORD_READ(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
941
942
943
944
    CP_UNLOCK();
    return(ret);
}

945
ssize_t DARSHAN_DECL(write)(int fd, const void *buf, size_t count)
946
947
948
949
950
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

951
952
    MAP_OR_FAIL(write);

953
    if((unsigned long)buf % darshan_mem_alignment == 0)
954
955
956
957
958
959
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_write(fd, buf, count);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
960
    CP_RECORD_WRITE(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
961
962
963
964
    CP_UNLOCK();
    return(ret);
}

965
size_t DARSHAN_DECL(fwrite)(const void *ptr, size_t size, size_t nmemb, FILE *stream)
966
967
968
969
970
{
    size_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

971
972
    MAP_OR_FAIL(fwrite);

973
    if((unsigned long)ptr % darshan_mem_alignment == 0)
974
975
976
977
978
979
980
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_fwrite(ptr, size, nmemb, stream);
    tm2 = darshan_wtime();
    CP_LOCK();
    if(ret > 0)
Philip Carns's avatar
Philip Carns committed
981
        CP_RECORD_WRITE(size*ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
982
    else
Philip Carns's avatar
Philip Carns committed
983
        CP_RECORD_WRITE(ret, fileno(stream), 0, 0, 0, aligned_flag, 1, tm1, tm2);
984
985
986
987
    CP_UNLOCK();
    return(ret);
}

988
off64_t DARSHAN_DECL(lseek64)(int fd, off64_t offset, int whence)
989
{
Philip Carns's avatar
Philip Carns committed
990
    off64_t ret;
991
992
993
    struct darshan_file_runtime* file;
    double tm1, tm2;

994
995
    MAP_OR_FAIL(lseek64);

996
997
998
999
1000
1001
1002
1003
1004
1005
    tm1 = darshan_wtime();
    ret = __real_lseek64(fd, offset, whence);
    tm2 = darshan_wtime();
    if(ret >= 0)
    {
        CP_LOCK();
        file = darshan_file_by_fd(fd);
        if(file)
        {
            file->offset = ret;
1006
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1007
1008
1009
1010
1011
1012
1013
            CP_INC(file, CP_POSIX_SEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

1014
off_t DARSHAN_DECL(lseek)(int fd, off_t offset, int whence)
1015
1016
1017
1018
1019
{
    off_t ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

1020
1021
    MAP_OR_FAIL(lseek);

1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
    tm1 = darshan_wtime();
    ret = __real_lseek(fd, offset, whence);
    tm2 = darshan_wtime();
    if(ret >= 0)
    {
        CP_LOCK();
        file = darshan_file_by_fd(fd);
        if(file)
        {
            file->offset = ret;
1032
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1033
1034
1035
1036
1037
1038
1039
            CP_INC(file, CP_POSIX_SEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

1040
int DARSHAN_DECL(fseek)(FILE *stream, long offset, int whence)
1041
1042
1043
1044
1045
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

1046
1047
    MAP_OR_FAIL(fseek);

1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
    tm1 = darshan_wtime();
    ret = __real_fseek(stream, offset, whence);
    tm2 = darshan_wtime();
    if(ret >= 0)
    {
        CP_LOCK();
        file = darshan_file_by_fd(fileno(stream));
        if(file)
        {
            file->offset = ret;
1058
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
            CP_INC(file, CP_POSIX_FSEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

void darshan_finalize(struct darshan_job_runtime* job)
{
    if(!job)
    {
        return;
    }

    free(job);
}

void darshan_initialize(int argc, char** argv,  int nprocs, int rank)
{
    int i;
    char* disable;
    char* disable_timing;
1081
    char* envstr;
1082
1083
1084
    char* truncate_string = "<TRUNCATED>";
    int truncate_offset;
    int chars_left = 0;
1085
1086
    int ret;
    int tmpval;
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101

    disable = getenv("DARSHAN_DISABLE");
    if(disable)
    {
        /* turn off tracing */
        return;
    }

    disable_timing = getenv("DARSHAN_DISABLE_TIMING");

    if(darshan_global_job != NULL)
    {
        return;
    }

1102
1103
1104
    #if (__CP_MEM_ALIGNMENT < 1)
        #error Darshan must be configured with a positive value for --with-mem-align
    #endif
1105
1106
1107
    envstr = getenv("DARSHAN_MEMALIGN");
    if (envstr)
    {
1108
1109
1110
1111
1112
1113
        ret = sscanf(envstr, "%d", &tmpval);
        /* silently ignore if the env variable is set poorly */
        if(ret == 1 && tmpval > 0)
        {
            darshan_mem_alignment = tmpval;
        }
1114
1115
1116
1117
1118
1119
    }
    else
    {
        darshan_mem_alignment = __CP_MEM_ALIGNMENT;
    }

1120
1121
1122
1123
1124
1125
    /* avoid floating point errors on faulty input */
    if (darshan_mem_alignment < 1)
    {
        darshan_mem_alignment = 1;
    }

1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
    /* allocate structure to track darshan_global_job information */
    darshan_global_job = malloc(sizeof(*darshan_global_job));
    if(!darshan_global_job)
    {
        return;
    }
    memset(darshan_global_job, 0, sizeof(*darshan_global_job));

    if(disable_timing)
    {
        darshan_global_job->flags |= CP_FLAG_NOTIMING;
    }

    /* set up file records */
    for(i=0; i<CP_MAX_FILES; i++)
    {
        darshan_global_job->file_runtime_array[i].log_file = 
            &darshan_global_job->file_array[i];
    }

    strcpy(darshan_global_job->log_job.version_string, CP_VERSION);
1147
    darshan_global_job->log_job.magic_nr = CP_MAGIC_NR;
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
1188
1189
1190
1191
1192
1193
1194
1195
1196
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
    darshan_global_job->log_job.uid = getuid();
    darshan_global_job->log_job.start_time = time(NULL);
    darshan_global_job->log_job.nprocs = nprocs;
    my_rank = rank;

    /* record exe and arguments */
    for(i=0; i<argc; i++)
    {
        chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
        strncat(darshan_global_job->exe, argv[i], chars_left);
        if(i < (argc-1))
        {
            chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
            strncat(darshan_global_job->exe, " ", chars_left);
        }
    }

    /* if we don't see any arguments, then use glibc symbol to get
     * program name at least (this happens in fortran)
     */
    if(argc == 0)
    {
        chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
        strncat(darshan_global_job->exe, __progname_full, chars_left);
        chars_left = CP_EXE_LEN-strlen(darshan_global_job->exe);
        strncat(darshan_global_job->exe, " <unknown args>", chars_left);
    }

    if(chars_left == 0)
    {
        /* we ran out of room; mark that string was truncated */
        truncate_offset = CP_EXE_LEN - strlen(truncate_string);
        sprintf(&darshan_global_job->exe[truncate_offset], "%s", 
            truncate_string);
    }
}

/* darshan_condense()
 *
 * collapses all file statistics into a single unified set of counters; used
 * when we have opened too many files to track independently
 */
void darshan_condense(void)
{
    struct darshan_file_runtime* base_file;
    struct darshan_file_runtime* iter_file;
    int i;
    int j;

    if(!darshan_global_job)
        return;

    base_file = &darshan_global_job->file_runtime_array[0];

    /* iterate through files */
    for(j=1; j<darshan_global_job->file_count; j++)
    {
        iter_file = &darshan_global_job->file_runtime_array[j];

        /* iterate through records */
        for(i=0; i<CP_NUM_INDICES; i++)
        {
            switch(i)
            {
                /* NOTE: several fields cease to make sense if the records
                 * have been condensed.  Just let them get summed anyway.
                 */
                /* TODO: double check this */

                /* keep up with global maxes in case they are helpful */
                case CP_MAX_BYTE_READ:
                case CP_MAX_BYTE_WRITTEN:
                    CP_MAX(base_file, i, CP_VALUE(iter_file, i));
                    break;

1223
1224
1225
1226
1227
1228
1229
                /* do nothing with these; they are handled in the floating
                 * point loop 
                 */
                case CP_MAX_WRITE_TIME_SIZE:
                case CP_MAX_READ_TIME_SIZE:
                    break;

1230
1231
                /* pick one */
                case CP_DEVICE:
1232
                case CP_SIZE_AT_OPEN:
1233
1234
1235
                    CP_SET(base_file, i, CP_VALUE(iter_file, i));
                    break;

1236
1237
1238
1239
1240
1241
1242
1243
                /* most records can simply be added */
                default:
                    CP_INC(base_file, i, CP_VALUE(iter_file, i));
                    break;
            }
        }
        for(i=0; i<CP_F_NUM_INDICES; i++)
        {
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
            switch(i)
            {
                case CP_F_MAX_WRITE_TIME:
                    if(CP_F_VALUE(iter_file, i) > CP_F_VALUE(base_file, i))
                    {
                        CP_F_SET(base_file, i, CP_F_VALUE(iter_file, i));
                        CP_SET(base_file, CP_MAX_WRITE_TIME_SIZE, 
                            CP_VALUE(iter_file, CP_MAX_WRITE_TIME_SIZE));
                    }
                    break;
                case CP_F_MAX_READ_TIME:
                    if(CP_F_VALUE(iter_file, i) > CP_F_VALUE(base_file, i))
                    {
                        CP_F_SET(base_file, i, CP_F_VALUE(iter_file, i));
                        CP_SET(base_file, CP_MAX_READ_TIME_SIZE, 
                            CP_VALUE(iter_file, CP_MAX_READ_TIME_SIZE));
                    }
                    break;
                default:
                    CP_F_SET(base_file, i, CP_F_VALUE(iter_file, i) + CP_F_VALUE(base_file, i));
                    break;
            }
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
        }
    }
    
    base_file->log_file->hash = 0;
    
    darshan_global_job->flags |= CP_FLAG_CONDENSED;
    darshan_global_job->file_count = 1;

    /* clear hash tables for safety */
    memset(darshan_global_job->name_table, 0, CP_HASH_SIZE*sizeof(struct darshan_file_runtime*));
1276
    memset(darshan_global_job->handle_table, 0, CP_HASH_SIZE*sizeof(*darshan_global_job->handle_table));
1277
1278
1279
1280
    
    return;
}

1281
static struct darshan_file_runtime* darshan_file_by_name_setfd(const char* name, int fd)
1282
1283
1284
{
    struct darshan_file_runtime* tmp_file;

1285
    tmp_file = darshan_file_by_name_sethandle(name, &fd, sizeof(fd), DARSHAN_FD);
1286
1287
1288
    return(tmp_file);
}

1289
static void darshan_file_close_fd(int fd)
1290
{
1291
1292
1293
    darshan_file_closehandle(&fd, sizeof(fd), DARSHAN_FD);
    return;
}
1294

1295
1296
1297
static struct darshan_file_runtime* darshan_file_by_fd(int fd)
{
    struct darshan_file_runtime* tmp_file;
1298

1299
1300
1301
    tmp_file = darshan_file_by_handle(&fd, sizeof(fd), DARSHAN_FD);
    
    return(tmp_file);
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
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
1381
1382
1383
}

static int access_comparison(const void* a_p, const void* b_p)
{
    const struct cp_access_counter* a = a_p;
    const struct cp_access_counter* b = b_p;

    if(a->size < b->size)
        return(-1);
    if(a->size > b->size)
        return(1);
    return(0);
}

/* cp_access_counter()
 *
 * records the occurance of a particular access size for a file,
 * current implementation uses glibc red black tree
 */
static void cp_access_counter(struct darshan_file_runtime* file, ssize_t size, enum cp_counter_type type)
{
    struct cp_access_counter* counter;
    struct cp_access_counter* found;
    void* tmp;
    void** root;
    int* count;
    struct cp_access_counter tmp_counter;

    /* don't count sizes or strides of 0 */
    if(size == 0)
        return;
    
    switch(type)
    {
        case CP_COUNTER_ACCESS:
            root = &file->access_root;
            count = &file->access_count;
            break;
        case CP_COUNTER_STRIDE:
            root = &file->stride_root;
            count = &file->stride_count;
            break;
        default:
            return;
    }

    /* check to see if this size is already recorded */
    tmp_counter.size = size;
    tmp_counter.freq = 1;
    tmp = tfind(&tmp_counter, root, access_comparison);
    if(tmp)
    {
        found = *(struct cp_access_counter**)tmp;
        found->freq++;
        return;
    }

    /* we can add a new one as long as we haven't hit the limit */
    if(*count < CP_MAX_ACCESS_COUNT_RUNTIME)
    {
        counter = malloc(sizeof(*counter));
        if(!counter)
        {
            return;
        }

        counter->size = size;
        counter->freq = 1;

        tmp = tsearch(counter, root, access_comparison);
        found = *(struct cp_access_counter**)tmp;
        /* if we get a new answer out here we are in trouble; this was
         * already checked with the tfind()
         */
        assert(found == counter);

        (*count)++;
    }

    return;
}

1384
#if 0
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
void darshan_shutdown_bench(int argc, char** argv, int rank, int nprocs)
{
    int* fd_array;
    int64_t* size_array;
    int i;
    int nfiles;
    char path[256];
    int iters;
    
    /* combinations to build:
     * - 1 unique file per proc
     * - 1 shared file per proc
     * - 1024 unique file per proc
     * - 1024 shared per proc
     */

    srand(rank);
    fd_array = malloc(sizeof(int)*CP_MAX_FILES);
    size_array = malloc(sizeof(int64_t)*CP_MAX_ACCESS_COUNT_RUNTIME);

    assert(fd_array&&size_array);

    for(i=0; i<CP_MAX_FILES; i++)
        fd_array[i] = i;
    for(i=0; i<CP_MAX_ACCESS_COUNT_RUNTIME; i++)
        size_array[i] = rand();

    /* clear out existing stuff */
    darshan_walk_file_accesses(darshan_global_job);
    darshan_finalize(darshan_global_job);
    darshan_global_job = NULL;

    /***********************************************************/
    /* reset darshan to start clean */
    darshan_initialize(argc, argv, nprocs, rank);

    /* populate one unique file per proc */
    nfiles = 1;
    iters = 1;
    for(i=0; i<nfiles; i++)
    {
        sprintf(path, "%d-%d", i, rank);
        CP_RECORD_OPEN(fd_array[i], path, 777, 0, 0, 0);
    }

    for(i=0; i<iters; i++)
    {
Philip Carns's avatar
Philip Carns committed
1432
        CP_RECORD_WRITE(size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], fd_array[i%nfiles], size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], 0, 0, 1, 0, 1, 2);
1433
1434
1435
1436
    }

    if(rank == 0)
        printf("# 1 unique file per proc\n");
1437
    DARSHAN_MPI_CALL(PMPI_Barrier)(MPI_COMM_WORLD);
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
    darshan_shutdown(1);
    darshan_global_job = NULL;

    /***********************************************************/
    /* reset darshan to start clean */
    darshan_initialize(argc, argv, nprocs, rank);

    /* populate one shared file per proc */
    nfiles = 1;
    iters = 1;
    for(i=0; i<nfiles; i++)
    {
        sprintf(path, "%d", i);
        CP_RECORD_OPEN(fd_array[i], path, 777, 0, 0, 0);
    }

    for(i=0; i<iters; i++)
    {
Philip Carns's avatar
Philip Carns committed
1456
        CP_RECORD_WRITE(size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], fd_array[i%nfiles], size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], 0, 0, 1, 0, 1, 2);
1457
1458
1459
1460
    }

    if(rank == 0)
        printf("# 1 shared file across procs\n");
1461
    DARSHAN_MPI_CALL(PMPI_Barrier)(MPI_COMM_WORLD);
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
    darshan_shutdown(1);
    darshan_global_job = NULL;

    /***********************************************************/
    /* reset darshan to start clean */
    darshan_initialize(argc, argv, nprocs, rank);

    /* populate 1024 unique file per proc */
    nfiles = 1024;
    iters = 1024;
    for(i=0; i<nfiles; i++)
    {
        sprintf(path, "%d-%d", i, rank);
        CP_RECORD_OPEN(fd_array[i], path, 777, 0, 0, 0);
    }

    for(i=0; i<iters; i++)
    {
Philip Carns's avatar
Philip Carns committed
1480
        CP_RECORD_WRITE(size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], fd_array[i%nfiles], size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], 0, 0, 1, 0, 1, 2);
1481
1482
1483
1484
    }

    if(rank == 0)
        printf("# 1024 unique files per proc\n");
1485
    DARSHAN_MPI_CALL(PMPI_Barrier)(MPI_COMM_WORLD);
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
    darshan_shutdown(1);
    darshan_global_job = NULL;

    /***********************************************************/
    /* reset darshan to start clean */
    darshan_initialize(argc, argv, nprocs, rank);

    /* populate 1024 shared file per proc */
    nfiles = 1024;
    iters = 1024;
    for(i=0; i<nfiles; i++)
    {
        sprintf(path, "%d", i);
        CP_RECORD_OPEN(fd_array[i], path, 777, 0, 0, 0);
    }

    for(i=0; i<iters; i++)
    {
Philip Carns's avatar
Philip Carns committed
1504
        CP_RECORD_WRITE(size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], fd_array[i%nfiles], size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], 0, 0, 1, 0, 1, 2);
1505
1506
1507
1508
    }

    if(rank == 0)
        printf("# 1024 shared files across procs\n");
1509
    DARSHAN_MPI_CALL(PMPI_Barrier)(MPI_COMM_WORLD);
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
    darshan_shutdown(1);
    darshan_global_job = NULL;

    darshan_initialize(argc, argv, nprocs, rank);

    free(fd_array);
    free(size_array);

    return;
}

void darshan_search_bench(int argc, char** argv, int iters)
{
    int* fd_array;
    int64_t* size_array;
    int i,j;
    int skip = 32;
    int nfiles;
    char path[256];
    double tm1, tm2;
    
    fd_array = malloc(sizeof(int)*CP_MAX_FILES);
    size_array = malloc(sizeof(int64_t)*CP_MAX_ACCESS_COUNT_RUNTIME);

    assert(fd_array&&size_array);

    for(i=0; i<CP_MAX_FILES; i++)
        fd_array[i] = i;
    for(i=0; i<CP_MAX_ACCESS_COUNT_RUNTIME; i++)
        size_array[i] = rand();

    printf("#<iters>\t<numfiles>\t<numsizes>\t<total time>\t<per iter>\n");

    for(j=0; j<2; j++)
    {
        /* warm up */
        /* reset darshan to start clean */
        darshan_walk_file_accesses(darshan_global_job);
        darshan_finalize(darshan_global_job);
        darshan_global_job = NULL;
        darshan_initialize(argc, argv, 1, 0);

        nfiles = 1;
        /* populate entries for each file */
        for(i=0; i<nfiles; i++)
        {
            sprintf(path, "%d", i);
            CP_RECORD_OPEN(fd_array[i], path, 777, 0, 0, 0);
        }

        for(i=0; i<iters; i++)
        {
            if(j==0)
            {
Philip Carns's avatar
Philip Carns committed
1564
                CP_RECORD_WRITE(size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], fd_array[i%nfiles], size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], 0, 0, 1, 0, 1, 2);
1565
1566
1567
            }
            else
            {
Philip Carns's avatar
Philip Carns committed
1568
                CP_RECORD_WRITE(size_array[0], fd_array[i%nfiles], size_array[0], 0, 0, 1, 0, 1, 2);
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
            }
        }

        /* real timing */
        for(nfiles=0; nfiles<=CP_MAX_FILES; nfiles += skip)
        {
            if(nfiles == 0)
                nfiles = 1;

            /* reset darshan to start clean */
            darshan_walk_file_accesses(darshan_global_job);
            darshan_finalize(darshan_global_job);
            darshan_global_job = NULL;
            darshan_initialize(argc, argv, 1, 0);

            /* populate entries for each file */
            for(i=0; i<nfiles; i++)
            {
                sprintf(path, "%d", i);
                CP_RECORD_OPEN(fd_array[i], path, 777, 0, 0, 0);
            }

            tm1 = darshan_wtime();
            for(i=0; i<iters; i++)
            {
                if(j==0)
                {
Philip Carns's avatar
Philip Carns committed
1596
                    CP_RECORD_WRITE(size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], fd_array[i%nfiles], size_array[(i/nfiles)%CP_MAX_ACCESS_COUNT_RUNTIME], 0, 0, 1, 0, 1, 2);
1597
1598
1599
                }
                else
                {
Philip Carns's avatar
Philip Carns committed
1600
                    CP_RECORD_WRITE(size_array[0], fd_array[i%nfiles], size_array[0], 0, 0, 1, 0, 1, 2);
1601
1602
1603
1604
                }
            }
            tm2 = darshan_wtime();

Philip Carns's avatar
Philip Carns committed
1605
            /* printf("#<iters>\t<numfiles>\t<numsizes>\t<total time>\t<per iter>\n"); */
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
            printf("%d\t%d\t%d\t%f\t%.12f\n", iters, nfiles, (j==0?CP_MAX_ACCESS_COUNT_RUNTIME:1), tm2-tm1, (tm2-tm1)/iters);

            if(nfiles == 1)
                nfiles = 0;
        }
    }

    free(fd_array);
    free(size_array);
}
1616
#endif
1617

1618
1619
1620
1621
1622
static double posix_wtime(void)
{
    return DARSHAN_MPI_CALL(PMPI_Wtime)();
}

1623
1624
1625
1626
1627
1628
1629
double darshan_wtime(void)
{
    if(!darshan_global_job || darshan_global_job->flags & CP_FLAG_NOTIMING)
    {
        return(0);
    }