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

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

#include "darshan.h"

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

36
37
extern char* __progname_full;

38
#ifdef DARSHAN_PRELOAD
39
40
41
#define __USE_GNU
#include <dlfcn.h>
#include <stdlib.h>
42

43
44
45
46
47
#define DARSHAN_FORWARD_DECL(name,ret,args) \
  ret (*__real_ ## name)args = NULL;

#define DARSHAN_DECL(__name) __name

48
49
50
51
52
53
54
55
56
57
58
59
60
#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); \
       } \
    }


61
extern double (*__real_PMPI_Wtime)(void);
62

63
64
65
66
67
68
69
#else

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

#define DARSHAN_DECL(__name) __wrap_ ## __name

70
71
72
73
#define MAP_OR_FAIL(func)

#define DARSHAN_MPI_CALL(func) func

74
75
#endif

76
77
78
79
DARSHAN_FORWARD_DECL(mkstemp, int, (char *template));
DARSHAN_FORWARD_DECL(mkostemp, int, (char *template, int flags));
DARSHAN_FORWARD_DECL(mkstemps, int, (char *template, int suffixlen));
DARSHAN_FORWARD_DECL(mkostemps, int, (char *template, int suffixlen, int flags));
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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));
Philip Carns's avatar
Philip Carns committed
112
DARSHAN_FORWARD_DECL(aio_read, int, (struct aiocb *aiocbp));
113
DARSHAN_FORWARD_DECL(aio_read64, int, (struct aiocb64 *aiocbp));
Philip Carns's avatar
Philip Carns committed
114
DARSHAN_FORWARD_DECL(aio_write, int, (struct aiocb *aiocbp));
115
DARSHAN_FORWARD_DECL(aio_write64, int, (struct aiocb64 *aiocbp));
Philip Carns's avatar
Philip Carns committed
116
DARSHAN_FORWARD_DECL(lio_listio, int, (int mode, struct aiocb *const aiocb_list[], int nitems, struct sigevent *sevp));
117
DARSHAN_FORWARD_DECL(lio_listio64, int, (int mode, struct aiocb64 *const aiocb_list[], int nitems, struct sigevent *sevp));
Philip Carns's avatar
Philip Carns committed
118
DARSHAN_FORWARD_DECL(aio_return, ssize_t, (struct aiocb *aiocbp));
119
DARSHAN_FORWARD_DECL(aio_return64, ssize_t, (struct aiocb64 *aiocbp));
120

121
pthread_mutex_t cp_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
122
123
struct darshan_job_runtime* darshan_global_job = NULL;
static int my_rank = -1;
124
static struct stat64 cp_stat_buf;
125
static int darshan_mem_alignment = 1;
126

127
128
129
130
/* struct to track information about aio operations in flight */
struct darshan_aio_tracker
{
    double tm1;
131
    void *aiocbp;
132
133
134
    struct darshan_aio_tracker* next;
};

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* these are paths that we will not trace */
static char* exclusions[] = {
"/etc/",
"/dev/",
"/usr/",
"/bin/",
"/boot/",
"/lib/",
"/opt/",
"/sbin/",
"/sys/",
"/proc/",
NULL
};

150
151
static double posix_wtime(void);

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

154
155
156
157
158
159
160
161
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);
162
static char* clean_path(const char* path);
163
164
static void darshan_aio_tracker_add(int fd, void *aiocbp);
static struct darshan_aio_tracker* darshan_aio_tracker_del(int fd, void *aiocbp);
165

Philip Carns's avatar
Philip Carns committed
166
#define CP_RECORD_WRITE(__ret, __fd, __count, __pwrite_flag, __pwrite_offset, __aligned, __stream_flag, __tm1, __tm2) do{ \
167
    size_t stride; \
Philip Carns's avatar
Philip Carns committed
168
    int64_t this_offset; \
169
170
    int64_t file_alignment; \
    struct darshan_file_runtime* file; \
171
    double __elapsed = __tm2-__tm1; \
172
173
174
    if(__ret < 0) break; \
    file = darshan_file_by_fd(__fd); \
    if(!file) break; \
Philip Carns's avatar
Philip Carns committed
175
176
177
178
    if(__pwrite_flag) \
        this_offset = __pwrite_offset; \
    else \
        this_offset = file->offset; \
179
    file_alignment = CP_VALUE(file, CP_FILE_ALIGNMENT); \
Philip Carns's avatar
Philip Carns committed
180
    if(this_offset > file->last_byte_written) \
181
        CP_INC(file, CP_SEQ_WRITES, 1); \
Philip Carns's avatar
Philip Carns committed
182
    if(this_offset == (file->last_byte_written + 1)) \
183
        CP_INC(file, CP_CONSEC_WRITES, 1); \
Philip Carns's avatar
Philip Carns committed
184
    if(this_offset > 0 && this_offset > file->last_byte_written \
185
        && file->last_byte_written != 0) \
Philip Carns's avatar
Philip Carns committed
186
        stride = this_offset - file->last_byte_written - 1; \
187
188
    else \
        stride = 0; \
Philip Carns's avatar
Philip Carns committed
189
190
191
    file->last_byte_written = this_offset + __ret - 1; \
    file->offset = this_offset + __ret; \
    CP_MAX(file, CP_MAX_BYTE_WRITTEN, (this_offset + __ret -1)); \
192
193
194
195
196
197
198
199
200
    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
201
    if(file_alignment > 0 && (this_offset % file_alignment) != 0) \
202
203
204
205
206
        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; \
207
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
208
209
210
    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); \
211
212
213
    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); } \
214
215
} while(0)

216
#define CP_RECORD_READ(__ret, __fd, __count, __pread_flag, __pread_offset, __aligned, __stream_flag, __tm1, __tm2) do{ \
217
    size_t stride; \
218
    int64_t this_offset; \
219
220
    struct darshan_file_runtime* file; \
    int64_t file_alignment; \
221
    double __elapsed = __tm2-__tm1; \
222
223
224
    if(__ret < 0) break; \
    file = darshan_file_by_fd(__fd); \
    if(!file) break; \
225
226
227
228
    if(__pread_flag)\
        this_offset = __pread_offset; \
    else \
        this_offset = file->offset; \
229
    file_alignment = CP_VALUE(file, CP_FILE_ALIGNMENT); \
230
    if(this_offset > file->last_byte_read) \
231
        CP_INC(file, CP_SEQ_READS, 1); \
232
    if(this_offset == (file->last_byte_read + 1)) \
233
        CP_INC(file, CP_CONSEC_READS, 1); \
234
    if(this_offset > 0 && this_offset > file->last_byte_read \
235
        && file->last_byte_read != 0) \
236
        stride = this_offset - file->last_byte_read - 1; \
237
238
    else \
        stride = 0; \
239
240
241
    file->last_byte_read = this_offset + __ret - 1; \
    CP_MAX(file, CP_MAX_BYTE_READ, (this_offset + __ret -1)); \
    file->offset = this_offset + __ret; \
242
243
244
245
246
247
248
249
250
    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
251
    if(file_alignment > 0 && (this_offset % file_alignment) != 0) \
252
253
254
255
256
        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; \
257
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_read_end, CP_F_POSIX_READ_TIME); \
258
259
260
    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); \
261
262
263
    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); } \
264
265
} while(0)

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#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)

    
284
#define CP_RECORD_STAT(__file, __statbuf, __tm1, __tm2) do { \
285
    if(!CP_VALUE((__file), CP_POSIX_STATS) && !CP_VALUE((__file), CP_POSIX_OPENS)){ \
286
287
288
289
        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; \
290
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME); \
291
292
293
    CP_INC(__file, CP_POSIX_STATS, 1); \
} while(0)

Philip Carns's avatar
Philip Carns committed
294
#ifdef __CP_STAT_AT_OPEN
295
#define CP_STAT_FILE(_f, _p, _r) do { \
296
    if(!CP_VALUE((_f), CP_POSIX_STATS) && !CP_VALUE((_f), CP_POSIX_OPENS)){ \
297
        if(fstat64(_r, &cp_stat_buf) == 0) { \
Philip Carns's avatar
Philip Carns committed
298
299
300
301
302
303
            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
304
#define CP_STAT_FILE(_f, _p, _r) do { }while(0)
Philip Carns's avatar
Philip Carns committed
305
306
#endif

307
308
309
310
311
312
313
314
315
316
317
#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; \
318
    file = darshan_file_by_name_setfd(__path, __ret); \
319
    if(!file) break; \
320
    CP_STAT_FILE(file, __path, __ret); \
321
322
323
324
325
326
327
328
329
330
331
    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) \
Philip Carns's avatar
Philip Carns committed
332
        CP_F_SET(file, CP_F_OPEN_TIMESTAMP, __tm1); \
333
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME); \
334
335
} while (0)

336
int DARSHAN_DECL(close)(int fd)
337
338
339
340
341
342
{
    struct darshan_file_runtime* file;
    int tmp_fd = fd;
    double tm1, tm2;
    int ret;

343
344
    MAP_OR_FAIL(close);

345
346
347
348
349
350
351
352
353
354
    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;
355
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, posix_wtime());
356
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
357
        darshan_file_close_fd(tmp_fd);
358
359
360
361
362
363
    }
    CP_UNLOCK();

    return(ret);
}

364
int DARSHAN_DECL(fclose)(FILE *fp)
365
366
367
368
369
370
{
    struct darshan_file_runtime* file;
    int tmp_fd = fileno(fp);
    double tm1, tm2;
    int ret;

371
372
    MAP_OR_FAIL(fclose);

373
374
375
376
377
378
379
380
381
382
    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;
383
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, posix_wtime());
384
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
385
        darshan_file_close_fd(tmp_fd);
386
387
388
389
390
391
392
    }
    CP_UNLOCK();

    return(ret);
}


393
int DARSHAN_DECL(fsync)(int fd)
394
395
396
397
398
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

399
400
    MAP_OR_FAIL(fsync);

401
402
403
404
405
406
407
408
409
410
411
    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)
    {
412
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
413
414
415
416
417
418
419
        CP_INC(file, CP_POSIX_FSYNCS, 1);
    }
    CP_UNLOCK();

    return(ret);
}

420
int DARSHAN_DECL(fdatasync)(int fd)
421
422
423
424
425
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

426
427
    MAP_OR_FAIL(fdatasync);

428
429
430
431
432
433
434
435
436
437
    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)
    {
438
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
439
440
441
442
443
444
445
446
        CP_INC(file, CP_POSIX_FDSYNCS, 1);
    }
    CP_UNLOCK();

    return(ret);
}


447
void* DARSHAN_DECL(mmap64)(void *addr, size_t length, int prot, int flags,
448
449
450
451
452
    int fd, off64_t offset)
{
    void* ret;
    struct darshan_file_runtime* file;

453
454
    MAP_OR_FAIL(mmap64);

455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    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);
}


471
void* DARSHAN_DECL(mmap)(void *addr, size_t length, int prot, int flags,
472
473
474
475
476
    int fd, off_t offset)
{
    void* ret;
    struct darshan_file_runtime* file;

477
478
    MAP_OR_FAIL(mmap);

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    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);
}

494
int DARSHAN_DECL(creat)(const char* path, mode_t mode)
495
496
497
498
{
    int ret;
    double tm1, tm2;

499
500
    MAP_OR_FAIL(creat);

501
502
503
504
505
506
507
508
509
510
511
    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);
}

512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
int DARSHAN_DECL(mkstemp)(char* template)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(mkstemp);

    tm1 = darshan_wtime();
    ret = __real_mkstemp(template);
    tm2 = darshan_wtime();

    CP_LOCK();
    CP_RECORD_OPEN(ret, template, 0, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

int DARSHAN_DECL(mkostemp)(char* template, int flags)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(mkostemp);

    tm1 = darshan_wtime();
    ret = __real_mkostemp(template, flags);
    tm2 = darshan_wtime();

    CP_LOCK();
    CP_RECORD_OPEN(ret, template, 0, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}


int DARSHAN_DECL(mkstemps)(char* template, int suffixlen)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(mkstemps);

    tm1 = darshan_wtime();
    ret = __real_mkstemps(template, suffixlen);
    tm2 = darshan_wtime();

    CP_LOCK();
    CP_RECORD_OPEN(ret, template, 0, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}


int DARSHAN_DECL(mkostemps)(char* template, int suffixlen, int flags)
{
    int ret;
    double tm1, tm2;

    MAP_OR_FAIL(mkostemps);

    tm1 = darshan_wtime();
    ret = __real_mkostemps(template, suffixlen, flags);
    tm2 = darshan_wtime();

    CP_LOCK();
    CP_RECORD_OPEN(ret, template, 0, 0, tm1, tm2);
    CP_UNLOCK();

    return(ret);
}

586
int DARSHAN_DECL(creat64)(const char* path, mode_t mode)
587
588
589
590
{
    int ret;
    double tm1, tm2;

591
592
    MAP_OR_FAIL(creat64);

593
594
595
596
597
598
599
600
601
602
603
    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);
}

604
int DARSHAN_DECL(open64)(const char* path, int flags, ...)
605
606
607
608
609
{
    int mode = 0;
    int ret;
    double tm1, tm2;

610
611
    MAP_OR_FAIL(open64);

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
    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);
}

637
int DARSHAN_DECL(open)(const char *path, int flags, ...)
638
639
640
641
642
{
    int mode = 0;
    int ret;
    double tm1, tm2;

643
644
    MAP_OR_FAIL(open);

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
    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);
}

670
FILE* DARSHAN_DECL(fopen64)(const char *path, const char *mode)
671
672
673
674
675
{
    FILE* ret;
    int fd;
    double tm1, tm2;

676
677
    MAP_OR_FAIL(fopen64);

678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
    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);
}

693
FILE* DARSHAN_DECL(fopen)(const char *path, const char *mode)
694
695
696
697
698
{
    FILE* ret;
    int fd;
    double tm1, tm2;

699
700
    MAP_OR_FAIL(fopen);

701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
    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);
}

716
int DARSHAN_DECL(__xstat64)(int vers, const char *path, struct stat64 *buf)
717
718
719
720
{
    int ret;
    double tm1, tm2;

721
722
    MAP_OR_FAIL(__xstat64);

723
724
725
726
727
728
729
    tm1 = darshan_wtime();
    ret = __real___xstat64(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
730
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
731
732
733
734
735
    CP_UNLOCK();

    return(ret);
}

736
int DARSHAN_DECL(__lxstat64)(int vers, const char *path, struct stat64 *buf)
737
738
739
740
{
    int ret;
    double tm1, tm2;

741
742
    MAP_OR_FAIL(__lxstat64);

743
744
745
746
747
748
749
    tm1 = darshan_wtime();
    ret = __real___lxstat64(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
750
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
751
752
753
754
755
    CP_UNLOCK();

    return(ret);
}

756
int DARSHAN_DECL(__fxstat64)(int vers, int fd, struct stat64 *buf)
757
758
759
760
761
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

762
763
    MAP_OR_FAIL(__fxstat64);

764
765
766
767
768
769
770
771
772
773
774
775
776
777
    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)
    {
778
        CP_RECORD_STAT(file, buf, tm1, tm2);
779
780
781
782
783
784
785
    }
    CP_UNLOCK();

    return(ret);
}


786
int DARSHAN_DECL(__xstat)(int vers, const char *path, struct stat *buf)
787
788
789
790
{
    int ret;
    double tm1, tm2;

791
792
    MAP_OR_FAIL(__xstat);

793
794
795
796
797
798
799
    tm1 = darshan_wtime();
    ret = __real___xstat(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
800
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
801
802
803
804
805
    CP_UNLOCK();

    return(ret);
}

806
int DARSHAN_DECL(__lxstat)(int vers, const char *path, struct stat *buf)
807
808
809
810
{
    int ret;
    double tm1, tm2;

811
812
    MAP_OR_FAIL(__lxstat);

813
814
815
816
817
818
819
    tm1 = darshan_wtime();
    ret = __real___lxstat(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
820
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
821
822
823
824
825
    CP_UNLOCK();

    return(ret);
}

826
int DARSHAN_DECL(__fxstat)(int vers, int fd, struct stat *buf)
827
828
829
830
831
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

832
833
    MAP_OR_FAIL(__fxstat);

834
835
836
837
838
839
840
    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 */
841
    if((void*)buf == (void*)&cp_stat_buf)
842
843
844
845
846
847
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
848
        CP_RECORD_STAT(file, buf, tm1, tm2);
849
850
851
852
853
854
    }
    CP_UNLOCK();

    return(ret);
}

855
ssize_t DARSHAN_DECL(pread64)(int fd, void *buf, size_t count, off64_t offset)
856
857
858
859
860
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

861
862
    MAP_OR_FAIL(pread64);

863
    if((unsigned long)buf % darshan_mem_alignment == 0)
864
865
866
867
868
869
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pread64(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
870
    CP_RECORD_READ(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
871
872
873
874
    CP_UNLOCK();
    return(ret);
}

875
ssize_t DARSHAN_DECL(pread)(int fd, void *buf, size_t count, off_t offset)
876
877
878
879
880
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

881
882
    MAP_OR_FAIL(pread);

883
    if((unsigned long)buf % darshan_mem_alignment == 0)
884
885
886
887
888
889
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pread(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
890
    CP_RECORD_READ(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
891
892
893
894
895
    CP_UNLOCK();
    return(ret);
}


896
ssize_t DARSHAN_DECL(pwrite)(int fd, const void *buf, size_t count, off_t offset)
897
898
899
900
901
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

902
903
    MAP_OR_FAIL(pwrite);

904
    if((unsigned long)buf % darshan_mem_alignment == 0)
905
906
907
908
909
910
        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
911
    CP_RECORD_WRITE(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
912
913
914
915
    CP_UNLOCK();
    return(ret);
}

916
ssize_t DARSHAN_DECL(pwrite64)(int fd, const void *buf, size_t count, off64_t offset)
917
918
919
920
921
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

922
923
    MAP_OR_FAIL(pwrite64);

924
    if((unsigned long)buf % darshan_mem_alignment == 0)
925
926
927
928
929
930
        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
931
    CP_RECORD_WRITE(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
932
933
934
935
    CP_UNLOCK();
    return(ret);
}

936
ssize_t DARSHAN_DECL(readv)(int fd, const struct iovec *iov, int iovcnt)
937
938
939
940
941
942
{
    ssize_t ret;
    int aligned_flag = 1;
    int i;
    double tm1, tm2;

943
944
    MAP_OR_FAIL(readv);

945
946
    for(i=0; i<iovcnt; i++)
    {
947
        if(((unsigned long)iov[i].iov_base % darshan_mem_alignment) != 0)
948
949
950
951
952
953
954
            aligned_flag = 0;
    }

    tm1 = darshan_wtime();
    ret = __real_readv(fd, iov, iovcnt);
    tm2 = darshan_wtime();
    CP_LOCK();
955
    CP_RECORD_READ(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
956
957
958
959
    CP_UNLOCK();
    return(ret);
}

960
ssize_t DARSHAN_DECL(writev)(int fd, const struct iovec *iov, int iovcnt)
961
962
963
964
965
966
{
    ssize_t ret;
    int aligned_flag = 1;
    int i;
    double tm1, tm2;

967
968
    MAP_OR_FAIL(writev);

969
970
    for(i=0; i<iovcnt; i++)
    {
971
        if(!((unsigned long)iov[i].iov_base % darshan_mem_alignment == 0))
972
973
974
975
976
977
978
            aligned_flag = 0;
    }

    tm1 = darshan_wtime();
    ret = __real_writev(fd, iov, iovcnt);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
979
    CP_RECORD_WRITE(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
980
981
982
983
    CP_UNLOCK();
    return(ret);
}

984
size_t DARSHAN_DECL(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream)
985
986
987
988
989
{
    size_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

990
991
    MAP_OR_FAIL(fread);

992
    if((unsigned long)ptr % darshan_mem_alignment == 0)
993
994
995
996
997
998
999
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_fread(ptr, size, nmemb, stream);
    tm2 = darshan_wtime();
    CP_LOCK();
    if(ret > 0)
1000
        CP_RECORD_READ(size*ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
1001
    else
1002
        CP_RECORD_READ(ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
1003
1004
1005
1006
    CP_UNLOCK();
    return(ret);
}

1007
ssize_t DARSHAN_DECL(read)(int fd, void *buf, size_t count)
1008
1009
1010
1011
1012
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

1013
1014
    MAP_OR_FAIL(read);

1015
    if((unsigned long)buf % darshan_mem_alignment == 0)
1016
1017
1018
1019
1020
1021
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_read(fd, buf, count);
    tm2 = darshan_wtime();
    CP_LOCK();
1022
    CP_RECORD_READ(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
1023
1024
1025
1026
    CP_UNLOCK();
    return(ret);
}

1027
ssize_t DARSHAN_DECL(write)(int fd, const void *buf, size_t count)
1028
1029
1030
1031
1032
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

1033
1034
    MAP_OR_FAIL(write);

1035
    if((unsigned long)buf % darshan_mem_alignment == 0)
1036
1037
1038
1039
1040
1041
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_write(fd, buf, count);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
1042
    CP_RECORD_WRITE(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
1043
1044
1045
1046
    CP_UNLOCK();
    return(ret);
}

1047
size_t DARSHAN_DECL(fwrite)(const void *ptr, size_t size, size_t nmemb, FILE *stream)
1048
1049
1050
1051
1052
{
    size_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

1053
1054
    MAP_OR_FAIL(fwrite);

1055
    if((unsigned long)ptr % darshan_mem_alignment == 0)
1056
1057
1058
1059
1060
1061
1062
        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
1063
        CP_RECORD_WRITE(size*ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
1064
    else
Philip Carns's avatar
Philip Carns committed
1065
        CP_RECORD_WRITE(ret, fileno(stream), 0, 0, 0, aligned_flag, 1, tm1, tm2);
1066
1067
1068
1069
    CP_UNLOCK();
    return(ret);
}

1070
off64_t DARSHAN_DECL(lseek64)(int fd, off64_t offset, int whence)
1071
{
Philip Carns's avatar
Philip Carns committed
1072
    off64_t ret;
1073
1074
1075
    struct darshan_file_runtime* file;
    double tm1, tm2;

1076
1077
    MAP_OR_FAIL(lseek64);

1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
    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;
1088
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1089
1090
1091
1092
1093
1094
1095
            CP_INC(file, CP_POSIX_SEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

1096
off_t DARSHAN_DECL(lseek)(int fd, off_t offset, int whence)
1097
1098
1099
1100
1101
{
    off_t ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

1102
1103
    MAP_OR_FAIL(lseek);

1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
    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;
1114
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1115
1116
1117
1118
1119
1120
1121
            CP_INC(file, CP_POSIX_SEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

1122
ssize_t DARSHAN_DECL(aio_return64)(struct aiocb64 *aiocbp)
Philip Carns's avatar
Philip Carns committed
1123
1124
{
    int ret;
1125
1126
1127
    double tm2;
    struct darshan_aio_tracker *tmp;
    int aligned_flag = 0;
Philip Carns's avatar
Philip Carns committed
1128
1129
1130
1131

    MAP_OR_FAIL(aio_return64);

    ret = __real_aio_return64(aiocbp);
1132
    tm2 = darshan_wtime();
1133
    tmp = darshan_aio_tracker_del(aiocbp->aio_fildes, aiocbp);
1134
1135
1136
1137
1138
1139

    if(tmp)
    {
        if((unsigned long)aiocbp->aio_buf % darshan_mem_alignment == 0)
            aligned_flag = 1;
        CP_LOCK();
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
        if(aiocbp->aio_lio_opcode == LIO_WRITE)
        {
            CP_RECORD_WRITE(ret, aiocbp->aio_fildes, aiocbp->aio_nbytes,
                1, aiocbp->aio_offset, aligned_flag, 0, tmp->tm1, tm2);
        }
        if(aiocbp->aio_lio_opcode == LIO_READ)
        {
            CP_RECORD_READ(ret, aiocbp->aio_fildes, aiocbp->aio_nbytes,
                1, aiocbp->aio_offset, aligned_flag, 0, tmp->tm1, tm2);
        }
1150
1151
1152
        CP_UNLOCK();
        free(tmp);
    }
Philip Carns's avatar
Philip Carns committed
1153
1154
1155
1156
1157
1158
1159

    return(ret);
}

ssize_t DARSHAN_DECL(aio_return)(struct aiocb *aiocbp)
{
    int ret;
1160
1161
1162
    double tm2;
    struct darshan_aio_tracker *tmp;
    int aligned_flag = 0;
Philip Carns's avatar
Philip Carns committed
1163
1164
1165
1166

    MAP_OR_FAIL(aio_return);

    ret = __real_aio_return(aiocbp);
1167
    tm2 = darshan_wtime();
1168
    tmp = darshan_aio_tracker_del(aiocbp->aio_fildes, aiocbp);
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

    if(tmp)
    {
        if((unsigned long)aiocbp->aio_buf % darshan_mem_alignment == 0)
            aligned_flag = 1;
        CP_LOCK();
        CP_RECORD_WRITE(ret, aiocbp->aio_fildes, aiocbp->aio_nbytes,
            1, aiocbp->aio_offset, aligned_flag, 0, tmp->tm1, tm2);
        CP_UNLOCK();
        free(tmp);
    }
Philip Carns's avatar
Philip Carns committed
1180
1181
1182
1183
1184
1185
1186
1187

    return(ret);
}

int DARSHAN_DECL(lio_listio)(int mode, struct aiocb *const aiocb_list[],
    int nitems, struct sigevent *sevp)
{
    int ret;
Philip Carns's avatar
Philip Carns committed
1188
    int i;
Philip Carns's avatar
Philip Carns committed
1189
1190
1191
1192

    MAP_OR_FAIL(lio_listio);

    ret = __real_lio_listio(mode, aiocb_list, nitems, sevp);
Philip Carns's avatar
Philip Carns committed
1193
1194
1195
1196
    if(ret == 0)
    {
        for(i=0; i<nitems; i++)
        {
1197
            darshan_aio_tracker_add(aiocb_list[i]->aio_fildes, aiocb_list[i]);        
Philip Carns's avatar
Philip Carns committed
1198
1199
        }
    }
Philip Carns's avatar
Philip Carns committed
1200
1201
1202
1203

    return(ret);
}

1204
int DARSHAN_DECL(lio_listio64)(int mode, struct aiocb64 *const aiocb_list[],
Philip Carns's avatar
Philip Carns committed
1205
1206
1207
    int nitems, struct sigevent *sevp)
{
    int ret;
Philip Carns's avatar
Philip Carns committed
1208
    int i;
Philip Carns's avatar
Philip Carns committed
1209

Philip Carns's avatar
Philip Carns committed
1210
    MAP_OR_FAIL(lio_listio);
Philip Carns's avatar
Philip Carns committed
1211

1212
    ret = __real_lio_listio64(mode, aiocb_list, nitems, sevp);
Philip Carns's avatar
Philip Carns committed
1213
1214
1215
1216
    if(ret == 0)
    {
        for(i=0; i<nitems; i++)
        {
1217
            darshan_aio_tracker_add(aiocb_list[i]->aio_fildes, aiocb_list[i]);        
Philip Carns's avatar
Philip Carns committed
1218
1219
        }
    }
Philip Carns's avatar
Philip Carns committed
1220
1221
1222
1223

    return(ret);
}

1224
int DARSHAN_DECL(aio_write64)(struct aiocb64 *aiocbp)
Philip Carns's avatar
Philip Carns committed
1225
1226
1227
1228
1229
1230
{
    int ret;

    MAP_OR_FAIL(aio_write64);

    ret = __real_aio_write64(aiocbp);
1231
    if(ret == 0)
1232
        darshan_aio_tracker_add(aiocbp->aio_fildes, aiocbp);
Philip Carns's avatar
Philip Carns committed
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243

    return(ret);
}

int DARSHAN_DECL(aio_write)(struct aiocb *aiocbp)
{
    int ret;

    MAP_OR_FAIL(aio_write);

    ret = __real_aio_write(aiocbp);
1244
    if(ret == 0)
1245
        darshan_aio_tracker_add(aiocbp->aio_fildes, aiocbp);
Philip Carns's avatar
Philip Carns committed
1246
1247
1248
1249

    return(ret);
}

1250
int DARSHAN_DECL(aio_read64)(struct aiocb64 *aiocbp)
Philip Carns's avatar
Philip Carns committed
1251
1252
1253
1254
1255
1256
{
    int ret;

    MAP_OR_FAIL(aio_read64);

    ret = __real_aio_read64(aiocbp);
1257
    if(ret == 0)
1258
        darshan_aio_tracker_add(aiocbp->aio_fildes, aiocbp);
Philip Carns's avatar
Philip Carns committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269

    return(ret);
}

int DARSHAN_DECL(aio_read)(struct aiocb *aiocbp)
{
    int ret;

    MAP_OR_FAIL(aio_read);

    ret = __real_aio_read(aiocbp);
1270
    if(ret == 0)
1271
        darshan_aio_tracker_add(aiocbp->aio_fildes, aiocbp);
Philip Carns's avatar
Philip Carns committed
1272
1273
1274
1275

    return(ret);
}

1276
int DARSHAN_DECL(fseek)(FILE *stream, long offset, int whence)
1277
1278
1279
1280
1281
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

1282
1283
    MAP_OR_FAIL(fseek);

1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
    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;
1294
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
            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;
1317
    char* envstr;
1318
1319
1320
    char* truncate_string = "<TRUNCATED>";
    int truncate_offset;
    int chars_left = 0;
1321
1322
    int ret;
    int tmpval;
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337

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

    disable_timing = getenv("DARSHAN_DISABLE_TIMING");

    if(darshan_global_job != NULL)
    {
        return;
    }

1338
1339
1340
    #if (__CP_MEM_ALIGNMENT < 1)
        #error Darshan must be configured with a positive value for --with-mem-align
    #endif
1341
1342
1343
    envstr = getenv("DARSHAN_MEMALIGN");
    if (envstr)
    {
1344
1345
1346
1347
1348
1349
        ret = sscanf(envstr, "%d", &tmpval);
        /* silently ignore if the env variable is set poorly */
        if(ret == 1 && tmpval > 0)
        {
            darshan_mem_alignment = tmpval;
        }
1350
1351
1352
1353
1354
1355
    }
    else
    {
        darshan_mem_alignment = __CP_MEM_ALIGNMENT;
    }

1356
1357
1358
1359
1360
1361
    /* avoid floating point errors on faulty input */
    if (darshan_mem_alignment < 1)
    {
        darshan_mem_alignment = 1;
    }

1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
    /* 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);
1383
    darshan_global_job->log_job.magic_nr = CP_MAGIC_NR;
1384
1385
1386
    darshan_global_job->log_job.uid = getuid();
    darshan_global_job->log_job.start_time = time(NULL);
    darshan_global_job->log_job.nprocs = nprocs;
1387
    darshan_global_job->wtime_offset = posix_wtime();
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
    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);
    }
1420
1421
1422
1423
1424
1425
1426
1427

    /* collect information about command line and 
     * mounted file systems 
     */
    darshan_global_job->trailing_data = 
        darshan_get_exe_and_mounts(darshan_global_job);

    return;