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>
Philip Carns's avatar
Philip Carns committed
22
#include <limits.h>
23 24
#define __USE_GNU
#include <pthread.h>
25 26 27

#include "darshan.h"

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

32 33
extern char* __progname_full;

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

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

#define DARSHAN_DECL(__name) __name

44 45 46 47 48 49 50 51 52 53 54 55 56
#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); \
       } \
    }


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

59 60 61 62 63 64 65
#else

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

#define DARSHAN_DECL(__name) __wrap_ ## __name

66 67 68 69
#define MAP_OR_FAIL(func)

#define DARSHAN_MPI_CALL(func) func

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));
Philip Carns's avatar
Philip Carns committed
104
DARSHAN_FORWARD_DECL(chdir, int, (const char* path));
105

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

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

Philip Carns's avatar
Philip Carns committed
127 128
static char working_dir[PATH_MAX] = {0};

129 130
static double posix_wtime(void);

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

133 134 135 136 137 138 139 140 141
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
142
#define CP_RECORD_WRITE(__ret, __fd, __count, __pwrite_flag, __pwrite_offset, __aligned, __stream_flag, __tm1, __tm2) do{ \
143
    size_t stride; \
Philip Carns's avatar
Philip Carns committed
144
    int64_t this_offset; \
145 146
    int64_t file_alignment; \
    struct darshan_file_runtime* file; \
147
    double __elapsed = __tm2-__tm1; \
148 149 150
    if(__ret < 0) break; \
    file = darshan_file_by_fd(__fd); \
    if(!file) break; \
Philip Carns's avatar
Philip Carns committed
151 152 153 154
    if(__pwrite_flag) \
        this_offset = __pwrite_offset; \
    else \
        this_offset = file->offset; \
155
    file_alignment = CP_VALUE(file, CP_FILE_ALIGNMENT); \
Philip Carns's avatar
Philip Carns committed
156
    if(this_offset > file->last_byte_written) \
157
        CP_INC(file, CP_SEQ_WRITES, 1); \
Philip Carns's avatar
Philip Carns committed
158
    if(this_offset == (file->last_byte_written + 1)) \
159
        CP_INC(file, CP_CONSEC_WRITES, 1); \
Philip Carns's avatar
Philip Carns committed
160
    if(this_offset > 0 && this_offset > file->last_byte_written \
161
        && file->last_byte_written != 0) \
Philip Carns's avatar
Philip Carns committed
162
        stride = this_offset - file->last_byte_written - 1; \
163 164
    else \
        stride = 0; \
Philip Carns's avatar
Philip Carns committed
165 166 167
    file->last_byte_written = this_offset + __ret - 1; \
    file->offset = this_offset + __ret; \
    CP_MAX(file, CP_MAX_BYTE_WRITTEN, (this_offset + __ret -1)); \
168 169 170 171 172 173 174 175 176
    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
177
    if(file_alignment && (this_offset % file_alignment) != 0) \
178 179 180 181 182
        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; \
183
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
184 185 186
    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); \
187 188 189
    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); } \
190 191
} while(0)

192
#define CP_RECORD_READ(__ret, __fd, __count, __pread_flag, __pread_offset, __aligned, __stream_flag, __tm1, __tm2) do{ \
193
    size_t stride; \
194
    int64_t this_offset; \
195 196
    struct darshan_file_runtime* file; \
    int64_t file_alignment; \
197
    double __elapsed = __tm2-__tm1; \
198 199 200
    if(__ret < 0) break; \
    file = darshan_file_by_fd(__fd); \
    if(!file) break; \
201 202 203 204
    if(__pread_flag)\
        this_offset = __pread_offset; \
    else \
        this_offset = file->offset; \
205
    file_alignment = CP_VALUE(file, CP_FILE_ALIGNMENT); \
206
    if(this_offset > file->last_byte_read) \
207
        CP_INC(file, CP_SEQ_READS, 1); \
208
    if(this_offset == (file->last_byte_read + 1)) \
209
        CP_INC(file, CP_CONSEC_READS, 1); \
210
    if(this_offset > 0 && this_offset > file->last_byte_read \
211
        && file->last_byte_read != 0) \
212
        stride = this_offset - file->last_byte_read - 1; \
213 214
    else \
        stride = 0; \
215 216 217
    file->last_byte_read = this_offset + __ret - 1; \
    CP_MAX(file, CP_MAX_BYTE_READ, (this_offset + __ret -1)); \
    file->offset = this_offset + __ret; \
218 219 220 221 222 223 224 225 226
    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); \
227
    if(file_alignment && (this_offset % file_alignment) != 0) \
228 229 230 231 232
        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; \
233
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_read_end, CP_F_POSIX_READ_TIME); \
234 235 236
    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); \
237 238 239
    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); } \
240 241
} while(0)

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#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)

    
260 261 262 263 264 265 266
#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; \
267
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME); \
268 269 270
    CP_INC(__file, CP_POSIX_STATS, 1); \
} while(0)

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
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);
}

289 290 291 292 293 294 295 296 297 298 299
#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; \
300
    file = darshan_file_by_name_setfd(__path, __ret); \
301 302
    if(!file) break; \
    if(!CP_VALUE(file, CP_FILE_ALIGNMENT)){ \
303 304
        if(stat64(__path, &cp_stat_buf) == 0) { \
            CP_SET(file, CP_DEVICE, get_device(__path, &cp_stat_buf)); \
305
            CP_SET(file, CP_FILE_ALIGNMENT, cp_stat_buf.st_blksize); \
306
            CP_SET(file, CP_SIZE_AT_OPEN, cp_stat_buf.st_size); \
307
        }\
308 309 310 311 312 313 314 315 316 317 318 319
    }\
    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) \
320
        CP_F_SET(file, CP_F_OPEN_TIMESTAMP, posix_wtime()); \
321
    CP_F_INC_NO_OVERLAP(file, __tm1, __tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME); \
322 323
} while (0)

324
int DARSHAN_DECL(close)(int fd)
325 326 327 328 329 330
{
    struct darshan_file_runtime* file;
    int tmp_fd = fd;
    double tm1, tm2;
    int ret;

331 332
    MAP_OR_FAIL(close);

333 334 335 336 337 338 339 340 341 342
    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;
343
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, posix_wtime());
344
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
345
        darshan_file_close_fd(tmp_fd);
346 347 348 349 350 351
    }
    CP_UNLOCK();

    return(ret);
}

352
int DARSHAN_DECL(fclose)(FILE *fp)
353 354 355 356 357 358
{
    struct darshan_file_runtime* file;
    int tmp_fd = fileno(fp);
    double tm1, tm2;
    int ret;

359 360
    MAP_OR_FAIL(fclose);

361 362 363 364 365 366 367 368 369 370
    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;
371
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, posix_wtime());
372
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
373
        darshan_file_close_fd(tmp_fd);
374 375 376 377 378 379 380
    }
    CP_UNLOCK();

    return(ret);
}


381
int DARSHAN_DECL(fsync)(int fd)
382 383 384 385 386
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

387 388
    MAP_OR_FAIL(fsync);

389 390 391 392 393 394 395 396 397 398 399
    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)
    {
400
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
401 402 403 404 405 406 407
        CP_INC(file, CP_POSIX_FSYNCS, 1);
    }
    CP_UNLOCK();

    return(ret);
}

408
int DARSHAN_DECL(fdatasync)(int fd)
409 410 411 412 413
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

414 415
    MAP_OR_FAIL(fdatasync);

416 417 418 419 420 421 422 423 424 425
    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)
    {
426
        CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_write_end, CP_F_POSIX_WRITE_TIME); \
427 428 429 430 431 432 433 434
        CP_INC(file, CP_POSIX_FDSYNCS, 1);
    }
    CP_UNLOCK();

    return(ret);
}


435
void* DARSHAN_DECL(mmap64)(void *addr, size_t length, int prot, int flags,
436 437 438 439 440
    int fd, off64_t offset)
{
    void* ret;
    struct darshan_file_runtime* file;

441 442
    MAP_OR_FAIL(mmap64);

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    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);
}


459
void* DARSHAN_DECL(mmap)(void *addr, size_t length, int prot, int flags,
460 461 462 463 464
    int fd, off_t offset)
{
    void* ret;
    struct darshan_file_runtime* file;

465 466
    MAP_OR_FAIL(mmap);

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    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);
}

482
int DARSHAN_DECL(creat)(const char* path, mode_t mode)
483 484 485 486
{
    int ret;
    double tm1, tm2;

487 488
    MAP_OR_FAIL(creat);

489 490 491 492 493 494 495 496 497 498 499
    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);
}

500
int DARSHAN_DECL(creat64)(const char* path, mode_t mode)
501 502 503 504
{
    int ret;
    double tm1, tm2;

505 506
    MAP_OR_FAIL(creat64);

507 508 509 510 511 512 513 514 515 516 517
    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);
}

518
int DARSHAN_DECL(open64)(const char* path, int flags, ...)
519 520 521 522 523
{
    int mode = 0;
    int ret;
    double tm1, tm2;

524 525
    MAP_OR_FAIL(open64);

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

551
int DARSHAN_DECL(open)(const char *path, int flags, ...)
552 553 554 555 556
{
    int mode = 0;
    int ret;
    double tm1, tm2;

557 558
    MAP_OR_FAIL(open);

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

Philip Carns's avatar
Philip Carns committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
int DARSHAN_DECL(chdir)(const char *path)
{
    int ret;

    MAP_OR_FAIL(chdir);

    ret = __real_chdir(path);
    if(ret == 0)
    {
        strncpy(working_dir, path, PATH_MAX-1);
    }

    return(ret);
}

599
FILE* DARSHAN_DECL(fopen64)(const char *path, const char *mode)
600 601 602 603 604
{
    FILE* ret;
    int fd;
    double tm1, tm2;

605 606
    MAP_OR_FAIL(fopen64);

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    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);
}

622
FILE* DARSHAN_DECL(fopen)(const char *path, const char *mode)
623 624 625 626 627
{
    FILE* ret;
    int fd;
    double tm1, tm2;

628 629
    MAP_OR_FAIL(fopen);

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    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);
}

645
int DARSHAN_DECL(__xstat64)(int vers, const char *path, struct stat64 *buf)
646 647 648 649
{
    int ret;
    double tm1, tm2;

650 651
    MAP_OR_FAIL(__xstat64);

652 653 654 655 656 657 658
    tm1 = darshan_wtime();
    ret = __real___xstat64(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
659
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
660 661 662 663 664
    CP_UNLOCK();

    return(ret);
}

665
int DARSHAN_DECL(__lxstat64)(int vers, const char *path, struct stat64 *buf)
666 667 668 669
{
    int ret;
    double tm1, tm2;

670 671
    MAP_OR_FAIL(__lxstat64);

672 673 674 675 676 677 678
    tm1 = darshan_wtime();
    ret = __real___lxstat64(vers, path, buf);
    tm2 = darshan_wtime();
    if(ret < 0 || !S_ISREG(buf->st_mode))
        return(ret);

    CP_LOCK();
679
    CP_LOOKUP_RECORD_STAT(path, buf, tm1, tm2);
680 681 682 683 684
    CP_UNLOCK();

    return(ret);
}

685
int DARSHAN_DECL(__fxstat64)(int vers, int fd, struct stat64 *buf)
686 687 688 689 690
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

691 692
    MAP_OR_FAIL(__fxstat64);

693 694 695 696 697 698 699 700 701 702 703 704 705 706
    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)
    {
707
        CP_RECORD_STAT(file, buf, tm1, tm2);
708 709 710 711 712 713 714
    }
    CP_UNLOCK();

    return(ret);
}


715
int DARSHAN_DECL(__xstat)(int vers, const char *path, struct stat *buf)
716 717 718 719
{
    int ret;
    double tm1, tm2;

720 721
    MAP_OR_FAIL(__xstat);

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

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

    return(ret);
}

735
int DARSHAN_DECL(__lxstat)(int vers, const char *path, struct stat *buf)
736 737 738 739
{
    int ret;
    double tm1, tm2;

740 741
    MAP_OR_FAIL(__lxstat);

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

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

    return(ret);
}

755
int DARSHAN_DECL(__fxstat)(int vers, int fd, struct stat *buf)
756 757 758 759 760
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

761 762
    MAP_OR_FAIL(__fxstat);

763 764 765 766 767 768 769
    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 */
770
    if((void*)buf == (void*)&cp_stat_buf)
771 772 773 774 775 776
        return(ret);

    CP_LOCK();
    file = darshan_file_by_fd(fd);
    if(file)
    {
777
        CP_RECORD_STAT(file, buf, tm1, tm2);
778 779 780 781 782 783
    }
    CP_UNLOCK();

    return(ret);
}

784
ssize_t DARSHAN_DECL(pread64)(int fd, void *buf, size_t count, off64_t offset)
785 786 787 788 789
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

790 791
    MAP_OR_FAIL(pread64);

792
    if((unsigned long)buf % darshan_mem_alignment == 0)
793 794 795 796 797 798
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pread64(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
799
    CP_RECORD_READ(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
800 801 802 803
    CP_UNLOCK();
    return(ret);
}

804
ssize_t DARSHAN_DECL(pread)(int fd, void *buf, size_t count, off_t offset)
805 806 807 808 809
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

810 811
    MAP_OR_FAIL(pread);

812
    if((unsigned long)buf % darshan_mem_alignment == 0)
813 814 815 816 817 818
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_pread(fd, buf, count, offset);
    tm2 = darshan_wtime();
    CP_LOCK();
819
    CP_RECORD_READ(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
820 821 822 823 824
    CP_UNLOCK();
    return(ret);
}


825
ssize_t DARSHAN_DECL(pwrite)(int fd, const void *buf, size_t count, off_t offset)
826 827 828 829 830
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

831 832
    MAP_OR_FAIL(pwrite);

833
    if((unsigned long)buf % darshan_mem_alignment == 0)
834 835 836 837 838 839
        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
840
    CP_RECORD_WRITE(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
841 842 843 844
    CP_UNLOCK();
    return(ret);
}

845
ssize_t DARSHAN_DECL(pwrite64)(int fd, const void *buf, size_t count, off64_t offset)
846 847 848 849 850
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

851 852
    MAP_OR_FAIL(pwrite64);

853
    if((unsigned long)buf % darshan_mem_alignment == 0)
854 855 856 857 858 859
        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
860
    CP_RECORD_WRITE(ret, fd, count, 1, offset, aligned_flag, 0, tm1, tm2);
861 862 863 864
    CP_UNLOCK();
    return(ret);
}

865
ssize_t DARSHAN_DECL(readv)(int fd, const struct iovec *iov, int iovcnt)
866 867 868 869 870 871
{
    ssize_t ret;
    int aligned_flag = 1;
    int i;
    double tm1, tm2;

872 873
    MAP_OR_FAIL(readv);

874 875
    for(i=0; i<iovcnt; i++)
    {
876
        if(((unsigned long)iov[i].iov_base % darshan_mem_alignment) != 0)
877 878 879 880 881 882 883
            aligned_flag = 0;
    }

    tm1 = darshan_wtime();
    ret = __real_readv(fd, iov, iovcnt);
    tm2 = darshan_wtime();
    CP_LOCK();
884
    CP_RECORD_READ(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
885 886 887 888
    CP_UNLOCK();
    return(ret);
}

889
ssize_t DARSHAN_DECL(writev)(int fd, const struct iovec *iov, int iovcnt)
890 891 892 893 894 895
{
    ssize_t ret;
    int aligned_flag = 1;
    int i;
    double tm1, tm2;

896 897
    MAP_OR_FAIL(writev);

898 899
    for(i=0; i<iovcnt; i++)
    {
900
        if(!((unsigned long)iov[i].iov_base % darshan_mem_alignment == 0))
901 902 903 904 905 906 907
            aligned_flag = 0;
    }

    tm1 = darshan_wtime();
    ret = __real_writev(fd, iov, iovcnt);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
908
    CP_RECORD_WRITE(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
909 910 911 912
    CP_UNLOCK();
    return(ret);
}

913
size_t DARSHAN_DECL(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream)
914 915 916 917 918
{
    size_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

919 920
    MAP_OR_FAIL(fread);

921
    if((unsigned long)ptr % darshan_mem_alignment == 0)
922 923 924 925 926 927 928
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_fread(ptr, size, nmemb, stream);
    tm2 = darshan_wtime();
    CP_LOCK();
    if(ret > 0)
929
        CP_RECORD_READ(size*ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
930
    else
931
        CP_RECORD_READ(ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
932 933 934 935
    CP_UNLOCK();
    return(ret);
}

936
ssize_t DARSHAN_DECL(read)(int fd, void *buf, size_t count)
937 938 939 940 941
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

942 943
    MAP_OR_FAIL(read);

944
    if((unsigned long)buf % darshan_mem_alignment == 0)
945 946 947 948 949 950
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_read(fd, buf, count);
    tm2 = darshan_wtime();
    CP_LOCK();
951
    CP_RECORD_READ(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
952 953 954 955
    CP_UNLOCK();
    return(ret);
}

956
ssize_t DARSHAN_DECL(write)(int fd, const void *buf, size_t count)
957 958 959 960 961
{
    ssize_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

962 963
    MAP_OR_FAIL(write);

964
    if((unsigned long)buf % darshan_mem_alignment == 0)
965 966 967 968 969 970
        aligned_flag = 1;

    tm1 = darshan_wtime();
    ret = __real_write(fd, buf, count);
    tm2 = darshan_wtime();
    CP_LOCK();
Philip Carns's avatar
Philip Carns committed
971
    CP_RECORD_WRITE(ret, fd, count, 0, 0, aligned_flag, 0, tm1, tm2);
972 973 974 975
    CP_UNLOCK();
    return(ret);
}

976
size_t DARSHAN_DECL(fwrite)(const void *ptr, size_t size, size_t nmemb, FILE *stream)
977 978 979 980 981
{
    size_t ret;
    int aligned_flag = 0;
    double tm1, tm2;

982 983
    MAP_OR_FAIL(fwrite);

984
    if((unsigned long)ptr % darshan_mem_alignment == 0)
985 986 987 988 989 990 991
        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
992
        CP_RECORD_WRITE(size*ret, fileno(stream), (size*nmemb), 0, 0, aligned_flag, 1, tm1, tm2);
993
    else
Philip Carns's avatar
Philip Carns committed
994
        CP_RECORD_WRITE(ret, fileno(stream), 0, 0, 0, aligned_flag, 1, tm1, tm2);
995 996 997 998
    CP_UNLOCK();
    return(ret);
}

999
off64_t DARSHAN_DECL(lseek64)(int fd, off64_t offset, int whence)
1000
{
Philip Carns's avatar
Philip Carns committed
1001
    off64_t ret;
1002 1003 1004
    struct darshan_file_runtime* file;
    double tm1, tm2;

1005 1006
    MAP_OR_FAIL(lseek64);

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    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;
1017
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1018 1019 1020 1021 1022 1023 1024
            CP_INC(file, CP_POSIX_SEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

1025
off_t DARSHAN_DECL(lseek)(int fd, off_t offset, int whence)
1026 1027 1028 1029 1030
{
    off_t ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

1031 1032
    MAP_OR_FAIL(lseek);

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    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;
1043
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1044 1045 1046 1047 1048 1049 1050
            CP_INC(file, CP_POSIX_SEEKS, 1);
        }
        CP_UNLOCK();
    }
    return(ret);
}

1051
int DARSHAN_DECL(fseek)(FILE *stream, long offset, int whence)
1052 1053 1054 1055 1056
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

1057 1058
    MAP_OR_FAIL(fseek);

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    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;
1069
            CP_F_INC_NO_OVERLAP(file, tm1, tm2, file->last_posix_meta_end, CP_F_POSIX_META_TIME);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
            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;
1092
    char* envstr;
1093 1094 1095
    char* truncate_string = "<TRUNCATED>";
    int truncate_offset;
    int chars_left = 0;
1096 1097
    int ret;
    int tmpval;
1098 1099 1100 1101 1102 1103 1104

    disable = getenv("DARSHAN_DISABLE");
    if(disable)
    {
        /* turn off tracing */
        return;
    }
Philip Carns's avatar
Philip Carns committed
1105 1106 1107 1108 1109 1110 1111
    
    /* find current working directory */
    if(getcwd(working_dir, PATH_MAX) == NULL)
    {
        /* turn off tracing if we can't find working dir */
        return;
    }
1112 1113 1114 1115 1116 1117 1118 1119

    disable_timing = getenv("DARSHAN_DISABLE_TIMING");

    if(darshan_global_job != NULL)
    {
        return;
    }

1120 1121 1122
    #if (__CP_MEM_ALIGNMENT < 1)
        #error Darshan must be configured with a positive value for --with-mem-align
    #endif
1123 1124 1125
    envstr = getenv("DARSHAN_MEMALIGN");
    if (envstr)
    {
1126 1127 1128 1129 1130 1131
        ret = sscanf(envstr, "%d", &tmpval);
        /* silently ignore if the env variable is set poorly */
        if(ret == 1 && tmpval > 0)
        {
            darshan_mem_alignment = tmpval;
        }
1132 1133 1134 1135 1136 1137
    }
    else
    {
        darshan_mem_alignment = __CP_MEM_ALIGNMENT;
    }

1138 1139 1140 1141 1142 1143
    /* avoid floating point errors on faulty input */
    if (darshan_mem_alignment < 1)
    {
        darshan_mem_alignment = 1;
    }

Philip Carns's avatar
Philip Carns committed
1144

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    /* 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);
1166
    darshan_global_job->log_job.magic_nr = CP_MAGIC_NR;
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