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

6 7 8 9
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE /* for tdestroy() */

#include <stdio.h>
10
#include <mntent.h>
11 12 13 14 15 16 17
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
18
#include <sys/stat.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include <zlib.h>
#include <assert.h>
#include <search.h>

#include "mpi.h"
#include "darshan.h"
#include "darshan-config.h"

extern char* __progname;

/* maximum number of memory segments each process will write to the log */
#define CP_MAX_MEM_SEGMENTS 8

#define CP_DATATYPE_INC(__file, __datatype) do {\
    int num_integers, num_addresses, num_datatypes, combiner, ret; \
    ret = MPI_Type_get_envelope(__datatype, &num_integers, &num_addresses, \
        &num_datatypes, &combiner); \
    if(ret == MPI_SUCCESS) { \
        switch(combiner) { \
            case MPI_COMBINER_NAMED:\
                CP_INC(__file,CP_COMBINER_NAMED,1); break; \
            case MPI_COMBINER_DUP:\
                CP_INC(__file,CP_COMBINER_DUP,1); break; \
            case MPI_COMBINER_CONTIGUOUS:\
                CP_INC(__file,CP_COMBINER_CONTIGUOUS,1); break; \
            case MPI_COMBINER_VECTOR:\
                CP_INC(__file,CP_COMBINER_VECTOR,1); break; \
            case MPI_COMBINER_HVECTOR_INTEGER:\
                CP_INC(__file,CP_COMBINER_HVECTOR_INTEGER,1); break; \
            case MPI_COMBINER_HVECTOR:\
                CP_INC(__file,CP_COMBINER_HVECTOR,1); break; \
            case MPI_COMBINER_INDEXED:\
                CP_INC(__file,CP_COMBINER_INDEXED,1); break; \
            case MPI_COMBINER_HINDEXED_INTEGER:\
                CP_INC(__file,CP_COMBINER_HINDEXED_INTEGER,1); break; \
            case MPI_COMBINER_HINDEXED:\
                CP_INC(__file,CP_COMBINER_HINDEXED,1); break; \
            case MPI_COMBINER_INDEXED_BLOCK:\
                CP_INC(__file,CP_COMBINER_INDEXED_BLOCK,1); break; \
            case MPI_COMBINER_STRUCT_INTEGER:\
                CP_INC(__file,CP_COMBINER_STRUCT_INTEGER,1); break; \
            case MPI_COMBINER_STRUCT:\
                CP_INC(__file,CP_COMBINER_STRUCT,1); break; \
            case MPI_COMBINER_SUBARRAY:\
                CP_INC(__file,CP_COMBINER_SUBARRAY,1); break; \
            case MPI_COMBINER_DARRAY:\
                CP_INC(__file,CP_COMBINER_DARRAY,1); break; \
            case MPI_COMBINER_F90_REAL:\
                CP_INC(__file,CP_COMBINER_F90_REAL,1); break; \
            case MPI_COMBINER_F90_COMPLEX:\
                CP_INC(__file,CP_COMBINER_F90_COMPLEX,1); break; \
            case MPI_COMBINER_F90_INTEGER:\
                CP_INC(__file,CP_COMBINER_F90_INTEGER,1); break; \
            case MPI_COMBINER_RESIZED:\
                CP_INC(__file,CP_COMBINER_RESIZED,1); break; \
        } \
    } \
} while(0)

#define CP_RECORD_MPI_WRITE(__ret, __fh, __count, __datatype, __counter, __tm1, __tm2) do { \
    struct darshan_file_runtime* file; \
    int size = 0; \
    MPI_Aint extent = 0; \
    if(__ret != MPI_SUCCESS) break; \
    file = darshan_file_by_fh(__fh); \
    if(!file) break; \
    MPI_Type_size(__datatype, &size);  \
    size = size * __count; \
    MPI_Type_extent(__datatype, &extent); \
    CP_BUCKET_INC(file, CP_SIZE_WRITE_AGG_0_100, size); \
    CP_BUCKET_INC(file, CP_EXTENT_WRITE_0_100, extent); \
    CP_INC(file, __counter, 1); \
    CP_DATATYPE_INC(file, __datatype); \
    CP_F_INC(file, CP_F_MPI_WRITE_TIME, (__tm2-__tm1)); \
    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); \
} while(0)

#define CP_RECORD_MPI_READ(__ret, __fh, __count, __datatype, __counter, __tm1, __tm2) do { \
    struct darshan_file_runtime* file; \
    int size = 0; \
    MPI_Aint extent = 0; \
    if(__ret != MPI_SUCCESS) break; \
    file = darshan_file_by_fh(__fh); \
    if(!file) break; \
    MPI_Type_size(__datatype, &size);  \
    size = size * __count; \
    MPI_Type_extent(__datatype, &extent); \
    CP_BUCKET_INC(file, CP_SIZE_READ_AGG_0_100, size); \
    CP_BUCKET_INC(file, CP_EXTENT_READ_0_100, extent); \
    CP_INC(file, __counter, 1); \
    CP_DATATYPE_INC(file, __datatype); \
    CP_F_INC(file, CP_F_MPI_READ_TIME, (__tm2-__tm1)); \
    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); \
} while(0)

static struct darshan_file_runtime* darshan_file_by_fh(MPI_File fh);
119 120 121
static void cp_log_construct_indices(struct darshan_job_runtime* final_job,
    int rank, int* inout_count, int* lengths, void** pointers, char*
    trailing_data);
122
static int cp_log_write(struct darshan_job_runtime* final_job, int rank, 
123
    char* logfile_name, int count, int* lengths, void** pointers, double start_log_time);
124
static int cp_log_reduction(struct darshan_job_runtime* final_job, int rank, 
125
    char* logfile_name, MPI_Offset* next_offset);
126 127 128 129 130 131
static void darshan_file_reduce(void* infile_v, 
    void* inoutfile_v, int *len, 
    MPI_Datatype *datatype);
static int cp_log_compress(struct darshan_job_runtime* final_job,
    int rank, int* inout_count, int* lengths, void** pointers);
static int file_compare(const void* a, const void* b);
Philip Carns's avatar
Philip Carns committed
132
static void darshan_mpi_initialize(int *argc, char ***argv);
133
static char*  darshan_get_exe_and_mounts(struct darshan_job_runtime* final_job);
134 135 136 137 138 139 140
static int darshan_file_variance(
    struct darshan_file *infile_array,
    struct darshan_file *outfile_array,
    int count, int rank);
static void pairwise_variance_reduce (
    void *invec, void *inoutvec, int *len, MPI_Datatype *dt);

141

142 143 144 145 146
#define CP_MAX_MNTS 32
uint64_t mnt_hash_array[CP_MAX_MNTS] = {0};
int64_t mnt_id_array[CP_MAX_MNTS] = {0};
uint64_t mnt_hash_array_root[CP_MAX_MNTS] = {0};
int64_t mnt_id_array_root[CP_MAX_MNTS] = {0};
147 148 149 150 151
struct
{
    int64_t mnt_id_local;
    int64_t mnt_id_root;
} mnt_mapping[CP_MAX_MNTS];
152

153 154 155 156 157 158 159
struct variance_dt
{
    double n;
    double T;
    double S;
};

160 161 162 163 164 165 166 167 168 169
int MPI_Init(int *argc, char ***argv)
{
    int ret;

    ret = PMPI_Init(argc, argv);
    if(ret != MPI_SUCCESS)
    {
        return(ret);
    }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    darshan_mpi_initialize(argc, argv);

    return(ret);
}

int MPI_Init_thread (int *argc, char ***argv, int required, int *provided)
{
    int ret;

    ret = PMPI_Init_thread(argc, argv, required, provided);
    if (ret != MPI_SUCCESS)
    {
        return(ret);
    }

    darshan_mpi_initialize(argc, argv);

    return(ret);
}

Philip Carns's avatar
Philip Carns committed
190
static void darshan_mpi_initialize(int *argc, char ***argv)
191 192 193 194
{
    int nprocs;
    int rank;

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    CP_LOCK();
    if(argc && argv)
    {
        darshan_initialize(*argc, *argv, nprocs, rank);
    }
    else
    {
        /* we don't see argc and argv here in fortran */
        darshan_initialize(0, NULL, nprocs, rank);
    }

    CP_UNLOCK();

211
    return;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

void darshan_shutdown(int timing_flag)
{
    int rank;
    char* logfile_name;
    struct darshan_job_runtime* final_job;
    double start_log_time = 0;
    int flags;
    int all_ret = 0;
    int local_ret = 0;
    MPI_Offset next_offset = 0;
    char* jobid_str;
    int jobid;
    int index_count = 0;
    int lengths[CP_MAX_MEM_SEGMENTS];
    void* pointers[CP_MAX_MEM_SEGMENTS];
    int ret;
    double red1=0, red2=0, gz1=0, gz2=0, write1=0, write2=0, tm_end=0;
231
    double bcst1=0, bcst2=0, bcst3=0;
232
    int nprocs;
233
    char* trailing_data = NULL;
234 235
    int i, j;
    int map_index = 0;
236
    time_t start_time_tmp = 0;
237 238 239 240 241

    CP_LOCK();
    if(!darshan_global_job)
    {
        CP_UNLOCK();
242
        return;
243 244 245 246 247 248 249 250 251
    }
    /* disable further tracing while hanging onto the data so that we can
     * write it out
     */
    final_job = darshan_global_job;
    darshan_global_job = NULL;
    flags = final_job->flags;
    CP_UNLOCK();

252
    start_log_time = MPI_Wtime();
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    /* figure out which access sizes to log */
    darshan_walk_file_accesses(final_job);

    /* if the records have been condensed, then zero out fields that are no
     * longer valid for safety 
     */
    if(final_job->flags & CP_FLAG_CONDENSED && final_job->file_count)
    {
        CP_SET(&final_job->file_runtime_array[0], CP_MODE, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_CONSEC_READS, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_CONSEC_WRITES, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_SEQ_READS, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_SEQ_WRITES, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE1_STRIDE, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE2_STRIDE, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE3_STRIDE, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE4_STRIDE, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE1_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE2_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE3_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_STRIDE4_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS1_ACCESS, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS2_ACCESS, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS3_ACCESS, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS4_ACCESS, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS1_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS2_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS3_COUNT, 0);
        CP_SET(&final_job->file_runtime_array[0], CP_ACCESS4_COUNT, 0);
        
        CP_F_SET(&final_job->file_runtime_array[0], CP_F_OPEN_TIMESTAMP, 0);
        CP_F_SET(&final_job->file_runtime_array[0], CP_F_CLOSE_TIMESTAMP, 0);
        CP_F_SET(&final_job->file_runtime_array[0], CP_F_READ_START_TIMESTAMP, 0);
        CP_F_SET(&final_job->file_runtime_array[0], CP_F_READ_END_TIMESTAMP, 0);
        CP_F_SET(&final_job->file_runtime_array[0], CP_F_WRITE_START_TIMESTAMP, 0);
        CP_F_SET(&final_job->file_runtime_array[0], CP_F_WRITE_END_TIMESTAMP, 0);
    }

    logfile_name = malloc(PATH_MAX);
    if(!logfile_name)
    {
        darshan_finalize(final_job);
        return;
    }

299
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
300

301
    /* collect information about command line and 
302 303
     * mounted file systems 
     */
304 305 306 307
    trailing_data = darshan_get_exe_and_mounts(final_job);

    /* broadcast mount point information from root */
    if(rank == 0)
308
    {
309 310 311 312
        memcpy(mnt_hash_array_root, mnt_hash_array,
            CP_MAX_MNTS*sizeof(uint64_t));
        memcpy(mnt_id_array_root, mnt_id_array,
            CP_MAX_MNTS*sizeof(int64_t));
313
    }
314 315

    bcst1=MPI_Wtime();
316 317 318 319
    MPI_Bcast(mnt_id_array_root, CP_MAX_MNTS*sizeof(int64_t), MPI_BYTE, 0,
        MPI_COMM_WORLD);
    MPI_Bcast(mnt_hash_array_root, CP_MAX_MNTS*sizeof(uint64_t), MPI_BYTE, 0,
        MPI_COMM_WORLD);
320 321
    bcst2=MPI_Wtime();

322 323 324 325 326 327 328 329 330 331 332 333
    /* identify any common mount points that have different device ids on
     * non-root processes
     */
    for(i=0; (i<CP_MAX_MNTS && mnt_hash_array_root[i] != 0); i++)
    {
        for(j=0; (j<CP_MAX_MNTS && mnt_hash_array[j] != 0); j++)
        {
            if(mnt_hash_array_root[i] == mnt_hash_array[j])
            {
                /* found a shared mount point */
                if(mnt_id_array_root[i] != mnt_id_array[j])
                {
334 335 336 337 338 339
                    /* mismatching ids; record correct mapping */
                    mnt_mapping[map_index].mnt_id_local =
                        mnt_id_array[j];
                    mnt_mapping[map_index].mnt_id_root = 
                        mnt_id_array_root[i];
                    map_index++;
340 341 342 343 344
                }
                break;
            }
        }
    }
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
 
    /* adjust affected file records */
    for(i=0; (i<final_job->file_count && map_index > 0); i++)
    {
        for(j=0; j<map_index; j++)
        {
            if(final_job->file_array[i].counters[CP_DEVICE] ==
                mnt_mapping[j].mnt_id_local)
            {
                final_job->file_array[i].counters[CP_DEVICE] =  
                    mnt_mapping[j].mnt_id_root;
                break;
            }
        }
    }
   
361 362 363 364 365 366 367
    /* construct log file name */
    if(rank == 0)
    {
        char cuser[L_cuserid] = {0};
        struct tm* my_tm;

        /* find a job id */
368
        jobid_str = getenv(CP_JOBID);
369 370 371 372 373 374 375 376 377 378 379 380
        if(jobid_str)
        {
            /* in cobalt we can find it in env var */
            ret = sscanf(jobid_str, "%d", &jobid);
        }
        if(!jobid_str || ret != 1)
        {
            /* use pid as fall back */
            jobid = getpid();
        }

        /* break out time into something human readable */
381 382
        start_time_tmp += final_job->log_job.start_time;
        my_tm = localtime(&start_time_tmp);
383 384 385 386 387

        /* note: getpwuid() causes link errors for static binaries */
        cuserid(cuser);

        ret = snprintf(logfile_name, PATH_MAX, 
388
            "%s/%d/%d/%d/%s_%s_id%d_%d-%d-%d.darshan_partial",
389
            __CP_LOG_PATH, (my_tm->tm_year+1900), 
390
            (my_tm->tm_mon+1), my_tm->tm_mday, 
391 392 393 394 395 396 397 398 399 400 401
            cuser, __progname, jobid,
            (my_tm->tm_mon+1), 
            my_tm->tm_mday, 
            (my_tm->tm_hour*60*60 + my_tm->tm_min*60 + my_tm->tm_sec));
        if(ret == (PATH_MAX-1))
        {
            /* file name was too big; squish it down */
            snprintf(logfile_name, PATH_MAX,
                "%s/id%d.darshan_partial",
                __CP_LOG_PATH, jobid);
        }
402 403 404

        /* add jobid */
        final_job->log_job.jobid = (int64_t)jobid;
405 406 407
    }

    /* broadcast log file name */
408
    bcst3=MPI_Wtime();
409
    MPI_Bcast(logfile_name, PATH_MAX, MPI_CHAR, 0, MPI_COMM_WORLD);
410 411 412 413 414

    final_job->log_job.end_time = time(NULL);

    /* reduce records for shared files */
    if(timing_flag)
415
        red1 = MPI_Wtime();
416 417
    local_ret = cp_log_reduction(final_job, rank, logfile_name, 
        &next_offset);
418
    if(timing_flag)
419 420
        red2 = MPI_Wtime();
    MPI_Allreduce(&local_ret, &all_ret, 1, MPI_INT, MPI_LOR, 
421 422 423 424 425 426
        MPI_COMM_WORLD);

    if(all_ret == 0)
    {
        /* collect data to write from local process */
        cp_log_construct_indices(final_job, rank, &index_count, lengths, 
427
            pointers, trailing_data);
428 429 430 431 432 433
    }

    if(all_ret == 0)
    {
        /* compress data */
        if(timing_flag)
434
            gz1 = MPI_Wtime();
435 436 437
        local_ret = cp_log_compress(final_job, rank, &index_count, 
            lengths, pointers);
        if(timing_flag)
438 439
            gz2 = MPI_Wtime();
        MPI_Allreduce(&local_ret, &all_ret, 1, MPI_INT, MPI_LOR, 
440 441 442 443 444 445 446
            MPI_COMM_WORLD);
    }

    if(all_ret == 0)
    {
        /* actually write out log file */
        if(timing_flag)
447
            write1 = MPI_Wtime();
448
        local_ret = cp_log_write(final_job, rank, logfile_name, 
449 450
            index_count, lengths, pointers, start_log_time);
        if(timing_flag)
451 452
            write2 = MPI_Wtime();
        MPI_Allreduce(&local_ret, &all_ret, 1, MPI_INT, MPI_LOR, 
453 454 455 456 457 458 459 460 461 462 463
            MPI_COMM_WORLD);
    }

    /* if any process failed to write log, then delete the whole file so we
     * don't leave corrupted results
     */
    if(all_ret != 0 && rank == 0)
    {
        unlink(logfile_name);
    }

464 465
    if(trailing_data)
        free(trailing_data);
466 467 468 469 470 471 472 473 474
    free(logfile_name);
    darshan_finalize(final_job);
    
    if(timing_flag)
    {
        double red_tm, red_slowest;
        double gz_tm, gz_slowest;
        double write_tm, write_slowest;
        double all_tm, all_slowest;
475
        double bcst_tm, bcst_slowest;
476
        
477
        tm_end = MPI_Wtime();
478

479
        bcst_tm=(bcst2-bcst1)+(red1-bcst3);
480 481 482 483 484
        red_tm = red2-red1;
        gz_tm = gz2-gz1;
        write_tm = write2-write1;
        all_tm = tm_end-start_log_time;

485
        MPI_Allreduce(&red_tm, &red_slowest, 1,
486
            MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
487
        MPI_Allreduce(&gz_tm, &gz_slowest, 1,
488
            MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
489
        MPI_Allreduce(&write_tm, &write_slowest, 1,
490
            MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
491
        MPI_Allreduce(&all_tm, &all_slowest, 1,
492
            MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
493 494
        MPI_Allreduce(&bcst_tm, &bcst_slowest, 1,
            MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
495 496 497 498 499

        if(rank == 0)
        {
            MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
            printf("#<op>\t<nprocs>\t<time>\n");
500
            printf("bcst\t%d\t%f\n", nprocs, bcst_slowest);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 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
            printf("reduce\t%d\t%f\n", nprocs, red_slowest);
            printf("gzip\t%d\t%f\n", nprocs, gz_slowest);
            printf("write\t%d\t%f\n", nprocs, write_slowest);
            printf("all\t%d\t%f\n", nprocs, all_slowest);
        }
    }

    return;
}

int MPI_Finalize(void)
{
    int ret;

    darshan_shutdown(0);

    ret = PMPI_Finalize();
    return(ret);
}

int MPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *fh) 
{
    int ret;
    struct darshan_file_runtime* file;
    char* tmp;
    int comm_size;
    int hash_index;
    uint64_t tmp_hash;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_open(comm, filename, amode, info, fh);
    tm2 = darshan_wtime();

    if(ret == MPI_SUCCESS)
    {
        CP_LOCK();

        /* use ROMIO approach to strip prefix if present */
        /* strip off prefix if there is one, but only skip prefixes
         * if they are greater than length one to allow for windows
         * drive specifications (e.g. c:\...) 
         */
        tmp = strchr(filename, ':');
        if (tmp > filename + 1) {
            filename = tmp + 1;
        }

        file = darshan_file_by_name(filename);
        /* TODO: handle the case of multiple concurrent opens */
        if(file && (file->fh == MPI_FILE_NULL))
        {
            file->fh = *fh;
            CP_SET(file, CP_MODE, amode);
            CP_F_INC(file, CP_F_MPI_META_TIME, (tm2-tm1));
            if(CP_F_VALUE(file, CP_F_OPEN_TIMESTAMP) == 0)
                CP_F_SET(file, CP_F_OPEN_TIMESTAMP, MPI_Wtime());
558
            MPI_Comm_size(comm, &comm_size);
559 560 561 562 563 564 565 566 567 568 569 570
            if(comm_size == 1)
            {
                CP_INC(file, CP_INDEP_OPENS, 1);
            }
            else
            {
                CP_INC(file, CP_COLL_OPENS, 1);
            }
            if(info != MPI_INFO_NULL)
            {
                CP_INC(file, CP_HINTS, 1);
            }
571
            tmp_hash = darshan_hash((void*)fh, sizeof(*fh), 0);
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
            hash_index = tmp_hash & CP_HASH_MASK;
            file->fh_prev = NULL;
            file->fh_next = darshan_global_job->fh_table[hash_index];
            if(file->fh_next)
                file->fh_next->fh_prev = file;
            darshan_global_job->fh_table[hash_index] = file;
        }
        CP_UNLOCK();
    }

    return(ret);
}

int MPI_File_close(MPI_File *fh) 
{
    int hash_index;
    uint64_t tmp_hash;
    struct darshan_file_runtime* file;
    MPI_File tmp_fh = *fh;
    double tm1, tm2;
    int ret;
    
    tm1 = darshan_wtime();
    ret = PMPI_File_close(fh);
    tm2 = darshan_wtime();

    CP_LOCK();
    file = darshan_file_by_fh(tmp_fh);
    if(file)
    {
        file->fh = MPI_FILE_NULL;
        CP_F_SET(file, CP_F_CLOSE_TIMESTAMP, MPI_Wtime());
        CP_F_INC(file, CP_F_MPI_META_TIME, (tm2-tm1));
        if(file->fh_prev == NULL)
        {
            /* head of fh hash table list */
608
            tmp_hash = darshan_hash((void*)&tmp_fh, sizeof(tmp_fh), 0);
609 610 611 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 637 638 639 640 641 642 643 644
            hash_index = tmp_hash & CP_HASH_MASK;
            darshan_global_job->fh_table[hash_index] = file->fh_next;
            if(file->fh_next)
                file->fh_next->fh_prev = NULL;
        }
        else
        {
            if(file->fh_prev)
                file->fh_prev->fh_next = file->fh_next;
            if(file->fh_next)
                file->fh_next->fh_prev = file->fh_prev;
        }
        file->fh_prev = NULL;
        file->fh_next = NULL;
        darshan_global_job->darshan_mru_file = file; /* in case we open it again, or hit posix calls */
    }
    CP_UNLOCK();

    return(ret);
}

int MPI_File_sync(MPI_File fh)
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_sync(fh);
    tm2 = darshan_wtime();
    if(ret == MPI_SUCCESS)
    {
        CP_LOCK();
        file = darshan_file_by_fh(fh);
        if(file)
        {
645
            CP_F_INC(file, CP_F_MPI_WRITE_TIME, (tm2-tm1));
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
            CP_INC(file, CP_SYNCS, 1);
        }
        CP_UNLOCK();
    }

    return(ret);
}


int MPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype, 
    MPI_Datatype filetype, char *datarep, MPI_Info info)
{
    int ret;
    struct darshan_file_runtime* file;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_set_view(fh, disp, etype, filetype, datarep, info);
    tm2 = darshan_wtime();
    if(ret == MPI_SUCCESS)
    {
        CP_LOCK();
        file = darshan_file_by_fh(fh);
        if(file)
        {
            CP_INC(file, CP_VIEWS, 1);
            if(info != MPI_INFO_NULL)
            {
                CP_F_INC(file, CP_F_MPI_META_TIME, (tm2-tm1));
                CP_INC(file, CP_HINTS, 1);
            }
            CP_DATATYPE_INC(file, filetype);
        }
        CP_UNLOCK();
    }

    return(ret);
}

int MPI_File_read(MPI_File fh, void *buf, int count, 
    MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_INDEP_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf,
    int count, MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_at(fh, offset, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_INDEP_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void * buf,
    int count, MPI_Datatype datatype, MPI_Status * status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_at_all(fh, offset, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_COLL_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_all(MPI_File fh, void * buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_all(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_COLL_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_shared(MPI_File fh, void * buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_shared(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_INDEP_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_ordered(MPI_File fh, void * buf, int count, 
    MPI_Datatype datatype, MPI_Status * status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_ordered(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_COLL_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_at_all_begin(MPI_File fh, MPI_Offset offset, void * buf,
    int count, MPI_Datatype datatype)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_at_all_begin(fh, offset, buf, count, datatype);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_SPLIT_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_all_begin(MPI_File fh, void * buf, int count, MPI_Datatype datatype)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_all_begin(fh, buf, count, datatype);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_SPLIT_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_read_ordered_begin(MPI_File fh, void * buf, int count, MPI_Datatype datatype)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_read_ordered_begin(fh, buf, count, datatype);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_SPLIT_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_iread_at(MPI_File fh, MPI_Offset offset, void * buf,
    int count, MPI_Datatype datatype, MPIO_Request *request)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_iread_at(fh, offset, buf, count, datatype, request);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_NB_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_iread(MPI_File fh, void * buf, int count, MPI_Datatype datatype, MPIO_Request * request)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_iread(fh, buf, count, datatype, request);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_NB_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_iread_shared(MPI_File fh, void * buf, int count,
    MPI_Datatype datatype, MPIO_Request * request)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_iread_shared(fh, buf, count, datatype, request);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_READ(ret, fh, count, datatype, CP_NB_READS, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}


int MPI_File_write(MPI_File fh, void *buf, int count, 
    MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_INDEP_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_at(MPI_File fh, MPI_Offset offset, void *buf,
    int count, MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_at(fh, offset, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_INDEP_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_at_all(MPI_File fh, MPI_Offset offset, void * buf,
    int count, MPI_Datatype datatype, MPI_Status * status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_at_all(fh, offset, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_COLL_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_all(MPI_File fh, void * buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_all(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_COLL_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_shared(MPI_File fh, void * buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_shared(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_INDEP_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_ordered(MPI_File fh, void * buf, int count, 
    MPI_Datatype datatype, MPI_Status * status)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_ordered(fh, buf, count, datatype, status);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_COLL_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_at_all_begin(MPI_File fh, MPI_Offset offset, void * buf,
    int count, MPI_Datatype datatype)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_at_all_begin(fh, offset, buf, count, datatype);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_SPLIT_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_all_begin(MPI_File fh, void * buf, int count, MPI_Datatype datatype)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_all_begin(fh, buf, count, datatype);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_SPLIT_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_write_ordered_begin(MPI_File fh, void * buf, int count, MPI_Datatype datatype)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_write_ordered_begin(fh, buf, count, datatype);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_SPLIT_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_iwrite_at(MPI_File fh, MPI_Offset offset, void * buf,
    int count, MPI_Datatype datatype, MPIO_Request *request)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_iwrite_at(fh, offset, buf, count, datatype, request);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_NB_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_iwrite(MPI_File fh, void * buf, int count, MPI_Datatype datatype, MPIO_Request * request)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_iwrite(fh, buf, count, datatype, request);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_NB_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

int MPI_File_iwrite_shared(MPI_File fh, void * buf, int count,
    MPI_Datatype datatype, MPIO_Request * request)
{
    int ret;
    double tm1, tm2;

    tm1 = darshan_wtime();
    ret = PMPI_File_iwrite_shared(fh, buf, count, datatype, request);
    tm2 = darshan_wtime();
    CP_LOCK();
    CP_RECORD_MPI_WRITE(ret, fh, count, datatype, CP_NB_WRITES, tm1, tm2);
    CP_UNLOCK();
    return(ret);
}

static struct darshan_file_runtime* darshan_file_by_fh(MPI_File fh)
{
    struct darshan_file_runtime* tmp_file;
    uint64_t tmp_hash = 0;
    int hash_index;

    if(!darshan_global_job)
        return(NULL);

    /* if we have already condensed the data, then just hand the first file
     * back
     */
    if(darshan_global_job->flags & CP_FLAG_CONDENSED)
    {
        return(&darshan_global_job->file_runtime_array[0]);
    }

    /* check most recently used */
    if(darshan_global_job->darshan_mru_file && darshan_global_job->darshan_mru_file->fh == fh)
    {
        return(darshan_global_job->darshan_mru_file);
    }

1059
    tmp_hash = darshan_hash((void*)(&fh), sizeof(fh), 0);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

    /* search hash table */
    hash_index = tmp_hash & CP_HASH_MASK;
    tmp_file = darshan_global_job->fh_table[hash_index];
    while(tmp_file)
    {
        if(tmp_file->fh == fh)
        {
            darshan_global_job->darshan_mru_file = tmp_file;
            return(tmp_file);
        }
        tmp_file = tmp_file->fh_next;
    }

    return(NULL);
}

/* cp_log_reduction()
 *
 * Identify shared files and reduce them to one log entry
 *
 * returns 0 on success, -1 on failure
 */
static int cp_log_reduction(struct darshan_job_runtime* final_job, int rank, 
1084
    char* logfile_name, MPI_Offset* next_offset)
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
{
    /* TODO: these need to be allocated differently now, too big */
    uint64_t hash_array[CP_MAX_FILES] = {0};
    int mask_array[CP_MAX_FILES] = {0};
    int all_mask_array[CP_MAX_FILES] = {0};
    int ret;
    int i;
    int j;
    MPI_Op reduce_op;
    MPI_Datatype rtype;
    struct darshan_file* tmp_array = NULL;
    int shared_count = 0;

    /* register a reduction operation */
1099
    ret = MPI_Op_create(darshan_file_reduce, 1, &reduce_op); 
1100 1101 1102 1103 1104 1105 1106 1107
    if(ret != 0)
    {
        return(-1);
    }

    /* construct a datatype for a file record.  This is serving no purpose
     * except to make sure we can do a reduction on proper boundaries
     */
1108 1109
    MPI_Type_contiguous(sizeof(struct darshan_file), MPI_BYTE, &rtype); 
    MPI_Type_commit(&rtype); 
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

    /* gather list of files that root process has opened */
    if(rank == 0)
    {
        for(i=0; i<final_job->file_count; i++)
        {
            hash_array[i] = final_job->file_array[i].hash;
        }
    }

    /* broadcast list of files to all other processes */
1121
    ret = MPI_Bcast(hash_array, (CP_MAX_FILES * sizeof(uint64_t)), 
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
        MPI_BYTE, 0, MPI_COMM_WORLD);
    if(ret != 0)
    {
        return(-1);
    }

    /* everyone looks to see if they have also opened that same file */
    for(i=0; (i<CP_MAX_FILES && hash_array[i] != 0); i++)
    {
        for(j=0; j<final_job->file_count; j++)
        {
            if(hash_array[i] && final_job->file_array[j].hash == hash_array[i])
            {
                /* we opened that file too */
                mask_array[i] = 1;
                break;
            }
        }
    }

    /* now allreduce so that everyone agrees on which files are shared */
1143
    ret = MPI_Allreduce(mask_array, all_mask_array, CP_MAX_FILES, MPI_INT, 
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
        MPI_LAND, MPI_COMM_WORLD);
    if(ret != 0)
    {
        return(-1);
    }

    /* walk through mask array counting entries and marking corresponding
     * files with a rank of -1
     */
    for(i=0; i<CP_MAX_FILES; i++)
    {
        if(all_mask_array[i])
        {
            shared_count++;
            for(j=0; j<final_job->file_count; j++)
            {
                if(final_job->file_array[j].hash == hash_array[i])
                {
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

                    /*
                     * Initialize fastest/slowest info prior
                     * to the reduction.
                     */
                    final_job->file_array[j].counters[CP_FASTEST_RANK] =
                      final_job->file_array[j].rank;
                    final_job->file_array[j].counters[CP_FASTEST_RANK_BYTES] =
                      final_job->file_array[j].counters[CP_BYTES_READ] +
                      final_job->file_array[j].counters[CP_BYTES_WRITTEN];
                    final_job->file_array[j].fcounters[CP_F_FASTEST_RANK_TIME] =
                      final_job->file_array[j].fcounters[CP_F_POSIX_META_TIME] +
                      final_job->file_array[j].fcounters[CP_F_POSIX_READ_TIME] +
                      final_job->file_array[j].fcounters[CP_F_POSIX_WRITE_TIME];

                    final_job->file_array[j].counters[CP_SLOWEST_RANK] =
                      final_job->file_array[j].rank;
                    final_job->file_array[j].counters[CP_SLOWEST_RANK_BYTES] =
                      final_job->file_array[j].counters[CP_BYTES_READ] +
                      final_job->file_array[j].counters[CP_BYTES_WRITTEN];
                    final_job->file_array[j].fcounters[CP_F_SLOWEST_RANK_TIME] =
                      final_job->file_array[j].fcounters[CP_F_POSIX_META_TIME] +
                      final_job->file_array[j].fcounters[CP_F_POSIX_READ_TIME] +
                      final_job->file_array[j].fcounters[CP_F_POSIX_WRITE_TIME];

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
                    final_job->file_array[j].rank = -1;
                    break;
                }
            }
        }
    }

    if(shared_count)
    {
        if(rank == 0)
        {
            /* root proc needs to allocate memory to store reduction */
            tmp_array = malloc(shared_count*sizeof(struct darshan_file));
            if(!tmp_array)
            {
                /* TODO: think more about how to handle errors like this */
                return(-1);
            }
        }

        /* sort the array of files descending by rank so that we get all of the 
         * shared files (marked by rank -1) in a contiguous portion at end 
         * of the array
         */
        qsort(final_job->file_array, final_job->file_count, 
            sizeof(struct darshan_file), file_compare);

1214
        ret = MPI_Reduce(
1215 1216 1217 1218 1219 1220 1221
            &final_job->file_array[final_job->file_count-shared_count], 
            tmp_array, shared_count, rtype, reduce_op, 0, MPI_COMM_WORLD);
        if(ret != 0)
        {
            return(-1);
        }

1222 1223 1224 1225 1226 1227 1228 1229
        ret = darshan_file_variance(
            &final_job->file_array[final_job->file_count-shared_count],
            tmp_array, shared_count, rank);
        if (ret)
        {
            return(-1);
        }

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
        if(rank == 0)
        {
            /* root replaces local files with shared ones */
            memcpy(&final_job->file_array[final_job->file_count-shared_count],
                tmp_array, shared_count*sizeof(struct darshan_file));
            free(tmp_array);
            tmp_array = NULL;
        }
        else
        {
            /* everyone else simply discards those file records */
            final_job->file_count -= shared_count;
        }
    }
    
    return(0);
}

/* TODO: should we use more of the CP macros here? */
static void darshan_file_reduce(void* infile_v, 
    void* inoutfile_v, int *len, 
    MPI_Datatype *datatype)
{
    struct darshan_file tmp_file;
    struct darshan_file* infile = infile_v;
    struct darshan_file* inoutfile = inoutfile_v;
    struct darshan_file_runtime tmp_runtime;
    int i;
    int j;
    int k;

    for(i=0; i<*len; i++)
    {
        memset(&tmp_file, 0, sizeof(tmp_file));

        tmp_file.hash = infile->hash;
        tmp_file.rank = -1; /* indicates shared across all procs */

        /* sum */
        for(j=CP_INDEP_OPENS; j<=CP_VIEWS; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + 
                inoutfile->counters[j];
        }

        /* pick one */
        tmp_file.counters[CP_MODE] = infile->counters[CP_MODE];


        /* sum */
        for(j=CP_BYTES_READ; j<=CP_BYTES_WRITTEN; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + 
                inoutfile->counters[j];
        }

        /* max */
        for(j=CP_MAX_BYTE_READ; j<=CP_MAX_BYTE_WRITTEN; j++)
        {
            tmp_file.counters[j] = (
                (infile->counters[j] > inoutfile->counters[j]) ? 
                infile->counters[j] :
                inoutfile->counters[j]);
        }

        /* sum */
        for(j=CP_CONSEC_READS; j<=CP_MEM_NOT_ALIGNED; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + 
                inoutfile->counters[j];
        }

        /* pick one */
        tmp_file.counters[CP_MEM_ALIGNMENT] = infile->counters[CP_MEM_ALIGNMENT];
        /* sum */
        for(j=CP_FILE_NOT_ALIGNED; j<=CP_FILE_NOT_ALIGNED; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + 
                inoutfile->counters[j];
        }

        /* pick one */
        tmp_file.counters[CP_FILE_ALIGNMENT] = infile->counters[CP_FILE_ALIGNMENT];
        
1314 1315
        /* skip CP_MAX_*_TIME_SIZE; handled in floating point section */

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 1384 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
        /* sum */
        for(j=CP_SIZE_READ_0_100; j<=CP_EXTENT_WRITE_1G_PLUS; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + 
                inoutfile->counters[j];
        }

        /* pick the 4 most common strides out of the 8 we have to chose from */

        /* first collapse any duplicates */
        for(j=CP_STRIDE1_STRIDE; j<=CP_STRIDE4_STRIDE; j++)
        {
            for(k=CP_STRIDE1_STRIDE; k<=CP_STRIDE4_STRIDE; k++)
            {
                if(infile->counters[j] == inoutfile->counters[k])
                {
                    infile->counters[j+4] += inoutfile->counters[k+4];
                    inoutfile->counters[k] = 0;
                    inoutfile->counters[k+4] = 0;
                }
            }
        }

        /* placeholder so we can re-use macros */
        tmp_runtime.log_file = &tmp_file;
        /* first set */
        for(j=CP_STRIDE1_STRIDE; j<=CP_STRIDE4_STRIDE; j++)
        {
            CP_COUNTER_INC(&tmp_runtime, infile->counters[j],
                infile->counters[j+4], 1, CP_STRIDE1_STRIDE, CP_STRIDE1_COUNT);
        }
        /* second set */
        for(j=CP_STRIDE1_STRIDE; j<=CP_STRIDE4_STRIDE; j++)
        {
            CP_COUNTER_INC(&tmp_runtime, inoutfile->counters[j],
                inoutfile->counters[j+4], 1, CP_STRIDE1_STRIDE, CP_STRIDE1_COUNT);
        }

        /* TODO: subroutine so we don't duplicate so much */
        /* same for access counts */

        /* first collapse any duplicates */
        for(j=CP_ACCESS1_ACCESS; j<=CP_ACCESS4_ACCESS; j++)
        {
            for(k=CP_ACCESS1_ACCESS; k<=CP_ACCESS4_ACCESS; k++)
            {
                if(infile->counters[j] == inoutfile->counters[k])
                {
                    infile->counters[j+4] += inoutfile->counters[k+4];
                    inoutfile->counters[k] = 0;
                    inoutfile->counters[k+4] = 0;
                }
            }
        }

        /* placeholder so we can re-use macros */
        tmp_runtime.log_file = &tmp_file;
        /* first set */
        for(j=CP_ACCESS1_ACCESS; j<=CP_ACCESS4_ACCESS; j++)
        {
            CP_COUNTER_INC(&tmp_runtime, infile->counters[j],
                infile->counters[j+4], 1, CP_ACCESS1_ACCESS, CP_ACCESS1_COUNT);
        }
        /* second set */
        for(j=CP_ACCESS1_ACCESS; j<=CP_ACCESS4_ACCESS; j++)
        {
            CP_COUNTER_INC(&tmp_runtime, inoutfile->counters[j],
                inoutfile->counters[j+4], 1, CP_ACCESS1_ACCESS, CP_ACCESS1_COUNT);
        }

        /* min */
        for(j=CP_F_OPEN_TIMESTAMP; j<=CP_F_WRITE_START_TIMESTAMP; j++)
        {
            if(infile->fcounters[j] > inoutfile->fcounters[j])
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
            else
                tmp_file.fcounters[j] = infile->fcounters[j];
        }

        /* max */
        for(j=CP_F_CLOSE_TIMESTAMP; j<=CP_F_WRITE_END_TIMESTAMP; j++)
        {
            if(infile->fcounters[j] > inoutfile->fcounters[j])
                tmp_file.fcounters[j] = infile->fcounters[j];
            else
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
        }

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

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
        /* max (special case) */
        if(infile->fcounters[CP_F_MAX_WRITE_TIME] > 
            inoutfile->fcounters[CP_F_MAX_WRITE_TIME])
        {
            tmp_file.fcounters[CP_F_MAX_WRITE_TIME] = 
                infile->fcounters[CP_F_MAX_WRITE_TIME];
            tmp_file.counters[CP_MAX_WRITE_TIME_SIZE] = 
                infile->counters[CP_MAX_WRITE_TIME_SIZE];
        }
        else
        {
            tmp_file.fcounters[CP_F_MAX_WRITE_TIME] = 
                inoutfile->fcounters[CP_F_MAX_WRITE_TIME];
            tmp_file.counters[CP_MAX_WRITE_TIME_SIZE] = 
                inoutfile->counters[CP_MAX_WRITE_TIME_SIZE];
        }

        if(infile->fcounters[CP_F_MAX_READ_TIME] > 
            inoutfile->fcounters[CP_F_MAX_READ_TIME])
        {
            tmp_file.fcounters[CP_F_MAX_READ_TIME] = 
                infile->fcounters[CP_F_MAX_READ_TIME];
            tmp_file.counters[CP_MAX_READ_TIME_SIZE] = 
                infile->counters[CP_MAX_READ_TIME_SIZE];
        }
        else
        {
            tmp_file.fcounters[CP_F_MAX_READ_TIME] = 
                inoutfile->fcounters[CP_F_MAX_READ_TIME];
            tmp_file.counters[CP_MAX_READ_TIME_SIZE] = 
                inoutfile->counters[CP_MAX_READ_TIME_SIZE];
        }

1444
        /* min */
1445 1446
        if(infile->fcounters[CP_F_FASTEST_RANK_TIME] <
           inoutfile->fcounters[CP_F_FASTEST_RANK_TIME])
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
        {
            tmp_file.counters[CP_FASTEST_RANK] =
                infile->counters[CP_FASTEST_RANK];
            tmp_file.counters[CP_FASTEST_RANK_BYTES] = 
                infile->counters[CP_FASTEST_RANK_BYTES];
            tmp_file.fcounters[CP_F_FASTEST_RANK_TIME] =
                infile->fcounters[CP_F_FASTEST_RANK_TIME];
        }
        else
        {
            tmp_file.counters[CP_FASTEST_RANK] =
                inoutfile->counters[CP_FASTEST_RANK];
            tmp_file.counters[CP_FASTEST_RANK_BYTES] =
                inoutfile->counters[CP_FASTEST_RANK_BYTES];
            tmp_file.fcounters[CP_F_FASTEST_RANK_TIME] = 
                inoutfile->fcounters[CP_F_FASTEST_RANK_TIME];
        }

        /* max */
        if(infile->fcounters[CP_F_SLOWEST_RANK_TIME] >
           inoutfile->fcounters[CP_F_SLOWEST_RANK_TIME])
        {
            tmp_file.counters[CP_SLOWEST_RANK] =
                infile->counters[CP_SLOWEST_RANK];
            tmp_file.counters[CP_SLOWEST_RANK_BYTES] =
                infile->counters[CP_SLOWEST_RANK_BYTES];
            tmp_file.fcounters[CP_F_SLOWEST_RANK_TIME] = 
                infile->fcounters[CP_F_SLOWEST_RANK_TIME];
        }
        else
        {
            tmp_file.counters[CP_SLOWEST_RANK] = 
                inoutfile->counters[CP_SLOWEST_RANK];
            tmp_file.counters[CP_SLOWEST_RANK_BYTES] = 
                inoutfile->counters[CP_SLOWEST_RANK_BYTES];
            tmp_file.fcounters[CP_F_SLOWEST_RANK_TIME] = 
                inoutfile->fcounters[CP_F_SLOWEST_RANK_TIME];
        }

1486
        /* pick one device id and file size */
1487
        tmp_file.counters[CP_DEVICE] = infile->counters[CP_DEVICE];
1488
        tmp_file.counters[CP_SIZE_AT_OPEN] = infile->counters[CP_SIZE_AT_OPEN];
1489

1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
        /* pick one name suffix */
        strcpy(tmp_file.name_suffix, infile->name_suffix);

        *inoutfile = tmp_file;
        inoutfile++;
        infile++;
    }
    
    return;
}
/* cp_log_construct_indices()
 *
 * create memory datatypes to describe the log data to write out
 */
static void cp_log_construct_indices(struct darshan_job_runtime* final_job, 
1505 1506
    int rank, int* inout_count, int* lengths, void** pointers, char*
    trailing_data)
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
{
    *inout_count = 0;

    if(rank == 0)
    {
        /* root process is responsible for writing header */
        lengths[*inout_count] = sizeof(final_job->log_job);
        pointers[*inout_count] = &final_job->log_job;
        (*inout_count)++;

        /* also string containing exe command line */
        lengths[*inout_count] = CP_EXE_LEN + 1; 
1519
        pointers[*inout_count] = trailing_data;
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
        (*inout_count)++;
    }

    /* everyone adds their own file records, if present */
    if(final_job->file_count > 0)
    {
        lengths[*inout_count] = final_job->file_count*CP_FILE_RECORD_SIZE;
        pointers[*inout_count] = final_job->file_array;
        (*inout_count)++;
    }
    
    return;
}

/* cp_log_write()
 *
 * actually write log information to disk
 */
static int cp_log_write(struct darshan_job_runtime* final_job, int rank, 
1539
    char* logfile_name, int count, int* lengths, void** pointers, double start_log_time)
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
{
    int ret;
    MPI_File fh;
    MPI_Status status;
    MPI_Datatype mtype;
    int my_total = 0;
    long my_total_long;
    long offset;
    int i;
    MPI_Aint displacements[CP_MAX_MEM_SEGMENTS];
    void* buf;
    int failed_write = 0;

    /* construct data type to describe everything we are writing */
    /* NOTE: there may be a bug in MPI-IO when using MPI_BOTTOM with an
     * hindexed data type.  We will instead use the first pointer as a base
     * and adjust the displacements relative to it.
     */
    buf = pointers[0];
    for(i=0; i<count; i++)
    {
        displacements[i] = (MPI_Aint)(pointers[i] - buf);
    }
1563 1564
    MPI_Type_hindexed(count, lengths, displacements, MPI_BYTE, &mtype);
    MPI_Type_commit(&mtype); 
1565 1566

    ret = PMPI_File_open(MPI_COMM_WORLD, logfile_name, MPI_MODE_CREATE |
1567
        MPI_MODE_WRONLY | MPI_MODE_EXCL, MPI_INFO_NULL, &fh);
1568 1569
    if(ret != MPI_SUCCESS)
    {
1570
        /* TODO: keep this print or not? */
1571
        fprintf(stderr, "darshan library warning: unable to open log file %s\n", logfile_name);
1572
        MPI_Type_free(&mtype);
1573 1574 1575 1576 1577 1578
        return(-1);
    }
   
    PMPI_File_set_size(fh, 0);

    /* figure out where everyone is writing */
1579
    MPI_Type_size(mtype, &my_total);
1580
    my_total_long = my_total;
1581
    MPI_Scan(&my_total_long, &offset, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD); 
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
    /* scan is inclusive; subtract local size back out */
    offset -= my_total_long;

    /* collectively write out file records from all processes */
    ret = PMPI_File_write_at_all(fh, offset, buf, 
        1, mtype, &status);
    if(ret != MPI_SUCCESS)
    {
        failed_write = 1;
    }

    PMPI_File_close(&fh);

    /* rename from *.darshan_partial to *-<logwritetime>.darshan.gz */
    if(rank == 0)
    {
        char* mod_index;
        double end_log_time;
        char* new_logfile_name;

        new_logfile_name = malloc(PATH_MAX);
        if(new_logfile_name)
        {
            new_logfile_name[0] = '\0';
1606
            end_log_time = MPI_Wtime();
1607 1608 1609 1610
            strcat(new_logfile_name, logfile_name);
            mod_index = strstr(new_logfile_name, ".darshan_partial");
            sprintf(mod_index, "_%d.darshan.gz", (int)(end_log_time-start_log_time+1));
            rename(logfile_name, new_logfile_name);
1611 1612
            /* set permissions on log file */
            chmod(new_logfile_name, (S_IRUSR)); 
1613 1614 1615 1616
            free(new_logfile_name);
        }
    }

1617
    MPI_Type_free(&mtype);
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

    if(failed_write)
    {
        return(-1);
    }
    return(0);
}

/* cp_log_compress()
 *
 * gzip memory buffers to write to log file.  Modifies the count, lengths,
 * and pointers to reference new buffer (or buffers)
 *
 * returns 0 on success, -1 on error
 */
/* TODO: pick settings for compression (memory, level, etc.) */
static int cp_log_compress(struct darshan_job_runtime* final_job,
    int rank, int* inout_count, int* lengths, void** pointers)
{
    int ret = 0;
    z_stream tmp_stream;
    int total_target = 0;
    int i;
    int no_data_flag = 1;

    /* do we actually have anything to write? */
    for(i=0; i<*inout_count; i++)
    {
        if(lengths[i])
        {
            no_data_flag = 0;
            break;
        }
    }

    if(no_data_flag)
    {
        /* nothing to compress */
        *inout_count = 0;
        return(0);
    }

    memset(&tmp_stream, 0, sizeof(tmp_stream));
    tmp_stream.zalloc = Z_NULL;
    tmp_stream.zfree = Z_NULL;
    tmp_stream.opaque = Z_NULL;

    ret = deflateInit2(&tmp_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
        31, 8, Z_DEFAULT_STRATEGY);
    if(ret != Z_OK)
    {
        return(-1);
    }

    tmp_stream.next_out = (void*)final_job->comp_buf;
    tmp_stream.avail_out = CP_COMP_BUF_SIZE;

    /* loop through all pointers to be compressed */
    for(i=0; i<*inout_count; i++)
    {
        total_target += lengths[i];
        tmp_stream.next_in = pointers[i];
        tmp_stream.avail_in = lengths[i];
        /* while we have not finished consuming all of the data available to
         * this point in the loop
         */
        while(tmp_stream.total_in < total_target)
        {
            if(tmp_stream.avail_out == 0)
            {
                /* We ran out of buffer space for compression.  In theory,
                 * we could start using some of the file_array buffer space
                 * without having to malloc again.  In practice, this case 
                 * is going to be practically impossible to hit.
                 */
                deflateEnd(&tmp_stream);
                return(-1);
            }

            /* compress data */
            ret = deflate(&tmp_stream, Z_NO_FLUSH);
            if(ret != Z_OK)
            {
                deflateEnd(&tmp_stream);
                return(-1);
            }
        }
    }
    
    /* flush compression and end */
    ret = deflate(&tmp_stream, Z_FINISH);
    if(ret != Z_STREAM_END)
    {
        deflateEnd(&tmp_stream);
        return(-1);
    }
    deflateEnd(&tmp_stream);

    /* substitute our new buffer */
    pointers[0] = final_job->comp_buf;
    lengths[0] = tmp_stream.total_out;
    *inout_count = 1;

    return(0);
}

static struct darshan_file_runtime* walker_file = NULL;
static int walker_validx;
static int walker_cntidx;

static void cp_access_walker(const void* nodep, const VISIT which, const int depth)
{
    struct cp_access_counter* counter;

    switch (which)
    {
        case postorder:
        case leaf:
            counter = *(struct cp_access_counter**)nodep;
            //printf("   type %d size: %lld, freq: %d\n", walker_validx, counter->size, counter->freq);
            CP_COUNTER_INC(walker_file, counter->size, counter->freq, 1, walker_validx, walker_cntidx);
        default:
            break;
    }

    return;
};

/* darshan_walk_file_accesses()
 *
 * goes through runtime collections of accesses sizes and chooses the 4 most
 * common for logging
 */
void darshan_walk_file_accesses(struct darshan_job_runtime* final_job)
{
    int i;

    for(i=0; i<final_job->file_count; i++)
    {
        //printf("file: %d\n", i);
        
        /* walk trees for both access sizes and stride sizes to pick 4 most
         * common of each
         */

        /* NOTE: setting global variables here for cp_access_walker() */
        walker_file = &final_job->file_runtime_array[i];
        walker_validx = CP_ACCESS1_ACCESS;
        walker_cntidx = CP_ACCESS1_COUNT;
        twalk(walker_file->access_root,
            cp_access_walker);
        tdestroy(walker_file->access_root, free);

        walker_validx = CP_STRIDE1_STRIDE;
        walker_cntidx = CP_STRIDE1_COUNT;
        twalk(walker_file->stride_root,
            cp_access_walker);
        tdestroy(walker_file->stride_root, free);
    }

    return;
}

static int file_compare(const void* a, const void* b)
{
    const struct darshan_file* f_a = a;
    const struct darshan_file* f_b = b;
    
    if(f_a->rank < f_b->rank)
        return 1;
    if(f_a->rank > f_b->rank)
        return -1;
    
    return 0;
}

1794
/* darshan_get_exe_and_mounts()
1795
 *
1796 1797
 * collects command line and list of mounted file systems into a string that
 * will be stored with the job header
1798
 */
1799
static char* darshan_get_exe_and_mounts(struct darshan_job_runtime* final_job)
1800 1801 1802 1803 1804 1805 1806 1807
{
    FILE* tab;
    struct mntent *entry;
    char* exclude;
    int tmp_index = 0;
    int ret;
    struct stat statbuf;
    int skip = 0;
1808 1809 1810
    char* trailing_data;
    int space_left;
    char tmp_mnt[256];
1811
    int mnt_array_index = 0;
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826

    /* skip these fs types */
    static char* fs_exclusions[] = {
        "tmpfs",
        "proc",
        "sysfs",
        "devpts",
        "binfmt_misc",
        "fusectl",
        "debugfs",
        "securityfs",
        "nfsd",
        NULL
    };

1827
    space_left = CP_EXE_LEN + 1;
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
    trailing_data = malloc(space_left);
    if(!trailing_data)
    {
        return(NULL);
    }
    memset(trailing_data, 0, space_left);

    /* length of exe has already been safety checked in darshan-posix.c */
    strcat(trailing_data, final_job->exe);
    space_left = CP_EXE_LEN - strlen(trailing_data);

1839 1840
    tab = setmntent("/etc/mtab", "r");
    if(!tab)
1841
        return(trailing_data);
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862

    while((entry = getmntent(tab)) != NULL)
    {
        tmp_index = 0;
        skip = 0;
        while((exclude = fs_exclusions[tmp_index]))
        {
            if(!(strcmp(exclude, entry->mnt_type)))
            {
                skip =1;
                break; 
            }
            tmp_index++;
        }

        if(skip)
            continue;

        ret = stat(entry->mnt_dir, &statbuf);
        if(ret == 0)
        {
1863 1864
            int64_t tmp_st_dev = statbuf.st_dev;

1865 1866 1867 1868
            /* record hash of mount point and id */
            if(mnt_array_index < CP_MAX_MNTS)
            {
                mnt_hash_array[mnt_array_index] =
1869
                    darshan_hash((void*)entry->mnt_dir, strlen(entry->mnt_dir), 0);
1870
                mnt_id_array[mnt_array_index] = tmp_st_dev;
1871 1872 1873
                mnt_array_index++;
            }

1874
            ret = snprintf(tmp_mnt, 256, "\n%lld\t%s\t%s", lld(tmp_st_dev), 
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
                entry->mnt_type, entry->mnt_dir);
            if(ret >= 256)
            {
                /* didn't fit; skip this entry */
                continue;
            }
            if(strlen(tmp_mnt) <= space_left)
            {
                strcat(trailing_data, tmp_mnt);
                space_left -= strlen(tmp_mnt);
            }
1886
#if 0
1887 1888
            printf("dev: %lld, mnt_pt: %s, type: %s\n",  
                lld(tmp_st_dev), entry->mnt_dir, entry->mnt_type);
1889 1890 1891
#endif
        }
    }
1892
    return(trailing_data);
1893 1894
}

1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
/*
 * Computes population variance of bytes moved and total time
 * for each rank on a shared file.
 */
static int darshan_file_variance(
    struct darshan_file *infile_array,
    struct darshan_file *outfile_array,
    int count, int rank)
{
    MPI_Op pw_var_op;
    MPI_Datatype var_dt;
    int ret;
    int i;
    struct variance_dt* var_array = NULL;
    struct variance_dt* varres_array = NULL;

    ret = MPI_Op_create(pairwise_variance_reduce, 1, &pw_var_op);
    if (ret != MPI_SUCCESS)
    {
        goto error_handler;
    }

    ret = MPI_Type_contiguous(sizeof(struct variance_dt), MPI_BYTE, &var_dt);
    if (ret != MPI_SUCCESS)
    {
        goto error_handler;
    }

    ret = MPI_Type_commit(&var_dt);
    if (ret != MPI_SUCCESS)
    {
        goto error_handler;
    }

    var_array = malloc(count*sizeof(struct variance_dt));
    if(!var_array)
    {
        goto error_handler;
    }       

    if (rank == 0)
    {
        varres_array = malloc(count*sizeof(struct variance_dt));
        if(!varres_array)
        {
            goto error_handler;
        }
    }
 
    /*
     * total time
     */

    for(i=0; i<count; i++)
    {
        var_array[i].n = 1;
        var_array[i].S = 0;
        var_array[i].T = infile_array[i].fcounters[CP_F_POSIX_META_TIME] +
                         infile_array[i].fcounters[CP_F_POSIX_READ_TIME] +
                         infile_array[i].fcounters[CP_F_POSIX_WRITE_TIME];
    } 

    ret = MPI_Reduce(
             var_array, varres_array, count, var_dt, pw_var_op,
             0, MPI_COMM_WORLD);
    if(ret != MPI_SUCCESS)
    {
        goto error_handler;
    }

    if (rank == 0)
    {
        for(i=0; i<count; i++)
        {
            outfile_array[i].fcounters[CP_F_VARIANCE_RANK_TIME] =
                (varres_array[i].S / varres_array[i].n);
        }
    }

    /*
     * total bytes
     */
    for(i=0; i<count; i++)
    {
        var_array[i].n = 1;
        var_array[i].S = 0;
        var_array[i].T = (double)
                         infile_array[i].counters[CP_BYTES_READ] +
                         infile_array[i].counters[CP_BYTES_WRITTEN];
    } 

    ret = MPI_Reduce(
             var_array, varres_array, count, var_dt, pw_var_op,
             0, MPI_COMM_WORLD);
    if(ret != MPI_SUCCESS)
    {
        goto error_handler;
    }

    if (rank == 0)
    {
        for(i=0; i<count; i++)
        {
            outfile_array[i].fcounters[CP_F_VARIANCE_RANK_BYTES] =
                (varres_array[i].S / varres_array[i].n);
        }
    }

    MPI_Type_free(&var_dt);

    ret = 0;

error_handler:
    if (var_array) free(var_array);
    if (varres_array) free(varres_array);

    return ret;
}

static void pairwise_variance_reduce (
    void *invec, void *inoutvec, int *len, MPI_Datatype *dt)
{
    int i;
    struct variance_dt *X = invec;
    struct variance_dt *Y = inoutvec;
    struct variance_dt  Z;

    for (i=0; i<*len; i++,X++,Y++)
    {
        Z.n = X->n + Y->n;
        Z.T = X->T + Y->T;
        Z.S = X->S + Y->S + (X->n/(Y->n*Z.n)) *
           ((Y->n/X->n)*X->T - Y->T) * ((Y->n/X->n)*X->T - Y->T);

        *Y = Z;
    }

    return;
}

2035 2036 2037 2038 2039 2040 2041 2042
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */