codes-darshan-io-wrkld.c 58.7 KB
Newer Older
1 2 3 4 5
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */
6
#include <assert.h>
7
#include <math.h>
8 9

#include "codes/codes-workload.h"
10
#include "codes/quickhash.h"
11
#include "codes-workload-method.h"
12

13
#include "darshan-logutils.h"
14

15 16 17
#define DEF_INTER_IO_DELAY_PCT 0.2
#define DEF_INTER_CYC_DELAY_PCT 0.4

18 19 20 21
#define DARSHAN_NEGLIGIBLE_DELAY .001

#define RANK_HASH_TABLE_SIZE 397

22
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
23

24 25
#define ALIGN_BY_8(x) ((x) + ((x) % 8))

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/* structure for storing a darshan workload operation (a codes op with 2 timestamps) */
struct darshan_io_op
{
    struct codes_workload_op codes_op;
    double start_time;
    double end_time;
};

/* I/O context structure managed by each rank in the darshan workload */
struct rank_io_context
{
    int64_t my_rank;
    double last_op_time;
    void *io_op_dat;
    struct qhash_head hash_link;
};

/* Darshan workload generator's implementation of the CODES workload API */
44 45
static int darshan_io_workload_load(const char *params, int rank);
static void darshan_io_workload_get_next(int rank, struct codes_workload_op *op);
46
static int darshan_io_workload_get_rank_cnt(const char *params);
47 48 49 50 51 52 53 54
static int darshan_rank_hash_compare(void *key, struct qhash_head *link);

/* Darshan I/O op data structure access (insert, remove) abstraction */
static void *darshan_init_io_op_dat(void);
static void darshan_insert_next_io_op(void *io_op_dat, struct darshan_io_op *io_op);
static void darshan_remove_next_io_op(void *io_op_dat, struct darshan_io_op *io_op,
                                      double last_op_time);
static void darshan_finalize_io_op_dat(void *io_op_dat);
55
static void darshan_print_io_ops(void *io_op_dat, int rank, FILE *log_stream);
56 57
static int darshan_io_op_compare(const void *p1, const void *p2);

58
/* Helper functions for implementing the Darshan workload generator */
59 60 61 62 63
static void generate_psx_ind_file_events(struct darshan_file *file,
                                         struct rank_io_context *io_context);
static void generate_psx_coll_file_events(struct darshan_file *file,
                                          struct rank_io_context *io_context,
                                          int64_t nprocs, int64_t aggregator_cnt);
64
static double generate_psx_open_event(struct darshan_file *file, int create_flag,
65
                                      double meta_op_time, double cur_time,
66
                                      struct rank_io_context *io_context, int insert_flag);
67
static double generate_psx_close_event(struct darshan_file *file, double meta_op_time,
68 69
                                       double cur_time, struct rank_io_context *io_context,
                                       int insert_flag);
70 71
static double generate_barrier_event(struct darshan_file *file, int64_t root, double cur_time,
                                     struct rank_io_context *io_context);
72
static double generate_psx_ind_io_events(struct darshan_file *file, int64_t io_ops_this_cycle,
73 74
                                         double inter_io_delay, double meta_op_time,
                                         double cur_time, struct rank_io_context *io_context);
75 76
static double generate_psx_coll_io_events(struct darshan_file *file, int64_t ind_io_ops_this_cycle,
                                          int64_t coll_io_ops_this_cycle, int64_t nprocs,
77 78
                                          int64_t aggregator_cnt, double inter_io_delay,
                                          double meta_op_time, double cur_time,
79
                                          struct rank_io_context *io_context);
80 81
static void determine_io_params(struct darshan_file *file, int write_flag, int64_t io_this_op,
                                int64_t proc_count, size_t *io_sz, off_t *io_off);
82
static void calc_io_delays(struct darshan_file *file, int64_t num_opens, int64_t num_io_ops,
83
                           double total_delay, double *first_io_delay, double *close_delay,
84 85 86
                           double *inter_open_delay, double *inter_io_delay);
static void file_sanity_check(struct darshan_file *file, struct darshan_job *job);

87 88 89 90 91 92
/* workload method name and function pointers for the CODES workload API */
struct codes_workload_method darshan_io_workload_method =
{
    .method_name = "darshan_io_workload",
    .codes_workload_load = darshan_io_workload_load,
    .codes_workload_get_next = darshan_io_workload_get_next,
93
    .codes_workload_get_rank_cnt = darshan_io_workload_get_rank_cnt,
94 95
};

96 97
static int total_rank_cnt = 0;

98
/* hash table to store per-rank workload contexts */
99
static struct qhash_table *rank_tbl = NULL;
100
static int rank_tbl_pop = 0;
101

102 103 104
/* load the workload generator for this rank, given input params */
static int darshan_io_workload_load(const char *params, int rank)
{
105 106
    darshan_params *d_params = (darshan_params *)params;
    darshan_fd logfile_fd;
107 108
    struct darshan_job job;
    struct darshan_file next_file;
109
    struct rank_io_context *my_ctx;
110
    int ret;
111

112
    if (!d_params)
113 114
        return -1;

115
    /* open the darshan log to begin reading in file i/o info */
116 117
    logfile_fd = darshan_log_open(d_params->log_file_path, "r");
    if (logfile_fd < 0)
118
        return -1;
119

120 121 122 123 124 125 126
    /* get the per-job stats from the log */
    ret = darshan_log_getjob(logfile_fd, &job);
    if (ret < 0)
    {
        darshan_log_close(logfile_fd);
        return -1;
    }
127 128 129 130 131
    if (!total_rank_cnt)
    {
        total_rank_cnt = job.nprocs;
    }
    assert(rank < total_rank_cnt);
132

133 134 135 136 137 138 139 140 141 142 143
    /* allocate the i/o context needed by this rank */
    my_ctx = malloc(sizeof(struct rank_io_context));
    if (!my_ctx)
    {
        darshan_log_close(logfile_fd);
        return -1;
    }
    my_ctx->my_rank = (int64_t)rank;
    my_ctx->last_op_time = 0.0;
    my_ctx->io_op_dat = darshan_init_io_op_dat();

144 145 146 147 148 149 150 151 152 153
    /* loop over all files contained in the log file */
    while ((ret = darshan_log_getfile(logfile_fd, &job, &next_file)) > 0)
    {
        /* generate all i/o events contained in this independent file */
        if (next_file.rank == rank)
        {
            /* make sure the file i/o counters are valid */
            file_sanity_check(&next_file, &job);

            /* generate i/o events and store them in this rank's workload context */
154
            generate_psx_ind_file_events(&next_file, my_ctx);
155 156 157 158 159 160
        }
        /* generate all i/o events involving this rank in this collective file */
        else if (next_file.rank == -1)
        {
            /* make sure the file i/o counters are valid */
            file_sanity_check(&next_file, &job);
161 162 163

            /* generate collective i/o events and store them in the rank context */
            generate_psx_coll_file_events(&next_file, my_ctx, job.nprocs, d_params->aggregator_cnt);
164
        }
165 166
        else
            continue;
167 168 169 170

        assert(next_file.counters[CP_POSIX_OPENS] == 0);
        assert(next_file.counters[CP_POSIX_READS] == 0);
        assert(next_file.counters[CP_POSIX_WRITES] == 0);
171 172 173 174
    }
    if (ret < 0)
        return -1;

175
    darshan_log_close(logfile_fd);
176

177 178 179 180 181 182 183 184 185 186 187 188 189
    /* finalize the rank's i/o context so i/o ops may be retrieved later (in order) */
    darshan_finalize_io_op_dat(my_ctx->io_op_dat);

    /* initialize the hash table of rank contexts, if it has not been initialized */
    if (!rank_tbl)
    {
        rank_tbl = qhash_init(darshan_rank_hash_compare, quickhash_64bit_hash, RANK_HASH_TABLE_SIZE);
        if (!rank_tbl)
            return -1;
    }

    /* add this rank context to the hash table */
    qhash_add(rank_tbl, &(my_ctx->my_rank), &(my_ctx->hash_link));
190
    rank_tbl_pop++;
191

192 193 194 195 196
    /* TODO: do we want this long-term */
    if (d_params->stream)
    {
        darshan_print_io_ops(my_ctx->io_op_dat, rank, d_params->stream);
    }
197

198 199 200 201 202 203
    return 0;
}

/* pull the next event (independent or collective) for this rank from its event context */
static void darshan_io_workload_get_next(int rank, struct codes_workload_op *op)
{
204 205 206 207 208
    int64_t my_rank = (int64_t)rank;
    struct qhash_head *hash_link = NULL;
    struct rank_io_context *tmp = NULL;
    struct darshan_io_op next_io_op;

209 210
    assert(rank < total_rank_cnt);

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    /* find i/o context for this rank in the rank hash table */
    hash_link = qhash_search(rank_tbl, &my_rank);

    /* terminate the workload if there is no valid rank context */
    if (!hash_link)
    {
        op->op_type = CODES_WK_END;
        return;
    }

    /* get access to the rank's io_context data */
    tmp = qhash_entry(hash_link, struct rank_io_context, hash_link);
    assert(tmp->my_rank == my_rank);

    /* get the next darshan i/o op out of this rank's context */
    darshan_remove_next_io_op(tmp->io_op_dat, &next_io_op, tmp->last_op_time);

    /* free the rank's i/o context if this is the last i/o op */
    if (next_io_op.codes_op.op_type == CODES_WK_END)
    {
        qhash_del(hash_link);
        free(tmp);
233 234
 
        rank_tbl_pop--;
235
        if (!rank_tbl_pop){
236
            qhash_finalize(rank_tbl);
237 238
            rank_tbl = NULL;
        }
239 240 241 242 243 244 245 246 247
    }
    else
    {
        /* else, set the last op time to be the end of the returned op */
        tmp->last_op_time = next_io_op.end_time;
    }

    /* return the codes op contained in the darshan i/o op */
    *op = next_io_op.codes_op;
248 249 250 251

    return;
}

252 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
static int darshan_io_workload_get_rank_cnt(const char *params)
{
    darshan_params *d_params = (darshan_params *)params;
    darshan_fd logfile_fd;
    struct darshan_job job;
    int ret;

    if (!d_params)
        return -1;

    /* open the darshan log to begin reading in file i/o info */
    logfile_fd = darshan_log_open(d_params->log_file_path, "r");
    if (logfile_fd < 0)
        return -1;

    /* get the per-job stats from the log */
    ret = darshan_log_getjob(logfile_fd, &job);
    if (ret < 0)
    {
        darshan_log_close(logfile_fd);
        return -1;
    }

    darshan_log_close(logfile_fd);

    return job.nprocs;
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
/* comparison function for comparing two hash keys (used for storing multiple io contexts) */
static int darshan_rank_hash_compare(
    void *key, struct qhash_head *link)
{
    int64_t *in_rank = (int64_t *)key;
    struct rank_io_context *tmp;

    tmp = qhash_entry(link, struct rank_io_context, hash_link);
    if (tmp->my_rank == *in_rank)
        return 1;

    return 0;
}

/*****************************************/
/*                                       */
/*   Darshan I/O op storage abstraction  */
/*                                       */
/*****************************************/

#define DARSHAN_IO_OP_INC_CNT 100000

/* dynamically allocated array data structure for storing darshan i/o events */
struct darshan_io_dat_array
{
    struct darshan_io_op *op_array;
    int64_t op_arr_ndx;
    int64_t op_arr_cnt;
};

/* initialize the dynamic array data structure */
static void *darshan_init_io_op_dat()
{
    struct darshan_io_dat_array *tmp;

    /* initialize the array data structure */
    tmp = malloc(sizeof(struct darshan_io_dat_array));
    assert(tmp);
    tmp->op_array = malloc(DARSHAN_IO_OP_INC_CNT * sizeof(struct darshan_io_op));
    assert(tmp->op_array);
    tmp->op_arr_ndx = 0;
    tmp->op_arr_cnt = DARSHAN_IO_OP_INC_CNT;

    /* return the array info for this rank's i/o context */
    return (void *)tmp;
}

/* store the i/o event in this rank's i/o context */
static void darshan_insert_next_io_op(
    void *io_op_dat, struct darshan_io_op *io_op)
{
    struct darshan_io_dat_array *array = (struct darshan_io_dat_array *)io_op_dat;
    struct darshan_io_op *tmp;

    /* realloc array if it is already full */
    if (array->op_arr_ndx == array->op_arr_cnt)
    {
        tmp = malloc((array->op_arr_cnt + DARSHAN_IO_OP_INC_CNT) * sizeof(struct darshan_io_op));
        assert(tmp);
        memcpy(tmp, array->op_array, array->op_arr_cnt * sizeof(struct darshan_io_op));
        free(array->op_array);
        array->op_array = tmp;
        array->op_arr_cnt += DARSHAN_IO_OP_INC_CNT;
    }

    /* add the darshan i/o op to the array */
    array->op_array[array->op_arr_ndx++] = *io_op;

    return;
}

/* pull the next i/o event out of this rank's i/o context */
static void darshan_remove_next_io_op(
    void *io_op_dat, struct darshan_io_op *io_op, double last_op_time)
{
    struct darshan_io_dat_array *array = (struct darshan_io_dat_array *)io_op_dat;

    /* if the array has been scanned completely already */
    if (array->op_arr_ndx == array->op_arr_cnt)
    {
        /* no more events just end the workload */
        io_op->codes_op.op_type = CODES_WK_END;

        /* free data structures */
        free(array->op_array);
        free(array);
    }
    else
    {
        struct darshan_io_op *tmp = &(array->op_array[array->op_arr_ndx]);

        if ((tmp->start_time - last_op_time) < DARSHAN_NEGLIGIBLE_DELAY)
        {
            /* there is no delay, just return the next op in the array */
            *io_op = *tmp;
            array->op_arr_ndx++;
        }
        else
        {
            /* there is a nonnegligible delay, so generate and return a delay event */
            io_op->codes_op.op_type = CODES_WK_DELAY;
            io_op->codes_op.u.delay.seconds = tmp->start_time - last_op_time;
            io_op->start_time = last_op_time;
            io_op->end_time = tmp->start_time;
        }
    }
}

/* sort the dynamic array in order of i/o op start time */
static void darshan_finalize_io_op_dat(
    void *io_op_dat)
{
    struct darshan_io_dat_array *array = (struct darshan_io_dat_array *)io_op_dat;

    /* sort this rank's i/o op list */
    qsort(array->op_array, array->op_arr_ndx, sizeof(struct darshan_io_op), darshan_io_op_compare);
    array->op_arr_cnt = array->op_arr_ndx;
    array->op_arr_ndx = 0;

    return;
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
/* print the events for this rank in order */
void darshan_print_io_ops(void *io_op_dat, int rank, FILE *log_stream)
{
    struct darshan_io_dat_array *io_array = (struct darshan_io_dat_array *)io_op_dat;
    struct darshan_io_op *event_list = io_array->op_array;
    int64_t event_list_cnt = io_array->op_arr_cnt;
    int64_t i;

    if (!event_list_cnt)
        return;

    for (i = 0; i < event_list_cnt; i++)
    {
        if (event_list[i].codes_op.op_type == CODES_WK_OPEN)
        {
            if (event_list[i].codes_op.u.open.create_flag == 0)
            {
                fprintf(log_stream, "Rank %d OPEN %"PRIu64" (%lf - %lf)\n",
                        rank,
                        event_list[i].codes_op.u.open.file_id,
                        event_list[i].start_time,
                        event_list[i].end_time);
            }
            else
            {
                fprintf(log_stream, "Rank %d CREATE %"PRIu64" (%lf - %lf)\n",
                        rank,
                        event_list[i].codes_op.u.open.file_id,
                        event_list[i].start_time,
                        event_list[i].end_time);
            }
        }
        else if (event_list[i].codes_op.op_type == CODES_WK_CLOSE)
        {
            fprintf(log_stream, "Rank %d CLOSE %"PRIu64" (%lf - %lf)\n",
                    rank,
                    event_list[i].codes_op.u.close.file_id,
                    event_list[i].start_time,
                    event_list[i].end_time);
        }
        else if (event_list[i].codes_op.op_type == CODES_WK_READ)
        {
444 445
            fprintf(log_stream, "Rank %d READ %"PRIu64" [sz = %"PRId64", off = %"PRId64
                    "] (%lf - %lf)\n",
446 447 448 449 450 451 452 453 454
                    rank,
                    event_list[i].codes_op.u.read.file_id,
                    (int64_t)event_list[i].codes_op.u.read.size,
                    (int64_t)event_list[i].codes_op.u.read.offset,
                    event_list[i].start_time,
                    event_list[i].end_time);
        }
        else if (event_list[i].codes_op.op_type == CODES_WK_WRITE)
        {
455 456
            fprintf(log_stream, "Rank %d WRITE %"PRIu64" [sz = %"PRId64", off = %"PRId64
                    "] (%lf - %lf)\n",
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
                    rank,
                    event_list[i].codes_op.u.write.file_id,
                    (int64_t)event_list[i].codes_op.u.write.size,
                    (int64_t)event_list[i].codes_op.u.write.offset,
                    event_list[i].start_time,
                    event_list[i].end_time);
        }
        else if (event_list[i].codes_op.op_type == CODES_WK_BARRIER)
        {
            fprintf(log_stream, "****");
        }
    }

    return;
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
/* comparison function for sorting darshan_io_ops in order of start timestamps */
static int darshan_io_op_compare(
    const void *p1, const void *p2)
{
    struct darshan_io_op *a = (struct darshan_io_op *)p1;
    struct darshan_io_op *b = (struct darshan_io_op *)p2;

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

/*****************************************/
/*                                       */
/* Darshan workload generation functions */
/*                                       */
/*****************************************/
493 494 495

/* generate events for an independently opened file, and store these events */
static void generate_psx_ind_file_events(
496
    struct darshan_file *file, struct rank_io_context *io_context)
497 498 499
{
    int64_t io_ops_this_cycle;
    double cur_time = file->fcounters[CP_F_OPEN_TIMESTAMP];
500
    double total_delay;
501 502 503 504 505 506 507 508 509 510 511 512 513
    double first_io_delay = 0.0;
    double close_delay = 0.0;
    double inter_open_delay = 0.0;
    double inter_io_delay = 0.0;
    double meta_op_time;
    int create_flag;
    int64_t i;

    /* if the file was never really opened, just return because we have no timing info */
    if (file->counters[CP_POSIX_OPENS] == 0)
        return;

    /* determine delay available per open-io-close cycle */
514 515 516
    total_delay = file->fcounters[CP_F_CLOSE_TIMESTAMP] - file->fcounters[CP_F_OPEN_TIMESTAMP] -
                  file->fcounters[CP_F_POSIX_READ_TIME] - file->fcounters[CP_F_POSIX_WRITE_TIME] -
                  file->fcounters[CP_F_POSIX_META_TIME];
517 518 519

    /* calculate synthetic delay values */
    calc_io_delays(file, file->counters[CP_POSIX_OPENS],
520 521
                   file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES], total_delay,
                   &first_io_delay, &close_delay, &inter_open_delay, &inter_io_delay);
522 523 524 525 526 527

    /* calculate average meta op time (for i/o and opens/closes) */
    /* TODO: this needs to be updated when we add in stat, seek, etc. */
    meta_op_time = file->fcounters[CP_F_POSIX_META_TIME] / ((2 * file->counters[CP_POSIX_OPENS]) +
                   file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES]);

528 529 530 531 532
    /* set the create flag if the file was written to */
    if (file->counters[CP_BYTES_WRITTEN])
    {
        create_flag = 1;
    }
533 534 535 536 537 538

    /* generate open/io/close events for all cycles */
    /* TODO: add stats */
    for (i = 0; file->counters[CP_POSIX_OPENS]; i++, file->counters[CP_POSIX_OPENS]--)
    {
        /* generate an open event */
539 540
        cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                           io_context, 1);
541 542 543 544 545 546 547 548 549 550
        create_flag = 0;

        /* account for potential delay from first open to first io */
        cur_time += first_io_delay;

        io_ops_this_cycle = ceil((double)(file->counters[CP_POSIX_READS] +
                                 file->counters[CP_POSIX_WRITES]) /
                                 file->counters[CP_POSIX_OPENS]);

        /* perform the calculated number of i/o operations for this file open */
551
        cur_time = generate_psx_ind_io_events(file, io_ops_this_cycle, inter_io_delay,
552
                                              meta_op_time, cur_time, io_context);
553 554 555 556 557

        /* account for potential delay from last io to close */
        cur_time += close_delay;

        /* generate a close for the open event at the start of the loop */
558
        cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context, 1);
559 560 561 562 563 564 565

        /* account for potential interopen delay if more than one open */
        if (file->counters[CP_POSIX_OPENS] > 1)
        {
            cur_time += inter_open_delay;
        }
    }
566 567 568 569 570 571 572 573 574

    return;
}

/* generate events for the i/o ops stored in a collectively opened file for this rank */
void generate_psx_coll_file_events(
    struct darshan_file *file, struct rank_io_context *io_context,
    int64_t nprocs, int64_t in_agg_cnt)
{
575 576 577 578 579
    int64_t open_cycles;
    int64_t total_ind_opens;
    int64_t total_coll_opens;
    int64_t ind_opens_this_cycle;
    int64_t coll_opens_this_cycle;
580 581
    int64_t extra_opens = 0;
    int64_t extra_io_ops = 0;
582
    int64_t total_io_ops = file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES];
583 584 585 586
    int64_t total_ind_io_ops;
    int64_t total_coll_io_ops;
    int64_t ind_io_ops_this_cycle;
    int64_t coll_io_ops_this_cycle;
587
    int64_t rank_cnt;
588 589
    int create_flag = 0;
    double cur_time = file->fcounters[CP_F_OPEN_TIMESTAMP];
590
    double total_delay;
591 592 593 594 595
    double first_io_delay = 0.0;
    double close_delay = 0.0;
    double inter_cycle_delay = 0.0;
    double inter_io_delay = 0.0;
    double meta_op_time;
596
    int64_t i;
597 598 599 600 601 602 603 604 605 606

    /* the collective file was never opened (i.e., just stat-ed), so return */
    if (!(file->counters[CP_POSIX_OPENS]))
        return;

    /*  in this case, posix opens are less than mpi opens...
     *  this is probably a mpi deferred open -- assume app will not use this, currently.
     */
    assert(file->counters[CP_POSIX_OPENS] >= nprocs);

607 608
    if (file->counters[CP_COLL_OPENS] || file->counters[CP_INDEP_OPENS])
    {
609 610
        extra_opens = file->counters[CP_POSIX_OPENS] - file->counters[CP_COLL_OPENS] -
                      file->counters[CP_INDEP_OPENS];
611

612 613
        total_coll_opens = file->counters[CP_COLL_OPENS];
        total_ind_opens = file->counters[CP_POSIX_OPENS] - total_coll_opens - extra_opens;
614 615 616 617

        total_ind_io_ops = file->counters[CP_INDEP_READS] + file->counters[CP_INDEP_WRITES];
        total_coll_io_ops = (file->counters[CP_COLL_READS] + file->counters[CP_COLL_WRITES]) / nprocs;

618 619 620 621 622 623 624 625
        if (file->counters[CP_COLL_OPENS])
        {
            total_delay = (file->fcounters[CP_F_CLOSE_TIMESTAMP] -
                           file->fcounters[CP_F_OPEN_TIMESTAMP] -
                           (file->fcounters[CP_F_POSIX_READ_TIME] / in_agg_cnt) -
                           (file->fcounters[CP_F_POSIX_WRITE_TIME] / in_agg_cnt) -
                           (file->fcounters[CP_F_POSIX_META_TIME] / in_agg_cnt));

626
            open_cycles = total_coll_opens / nprocs;
627 628 629 630
            calc_io_delays(file, ceil(((double)(total_coll_opens + total_ind_opens)) / nprocs),
                           total_coll_io_ops + ceil((double)total_ind_io_ops / nprocs), total_delay,
                           &first_io_delay, &close_delay, &inter_cycle_delay, &inter_io_delay);
        }
631
        else
632 633 634 635 636 637
        {
            total_delay = (file->fcounters[CP_F_CLOSE_TIMESTAMP] -
                           file->fcounters[CP_F_OPEN_TIMESTAMP] -
                           (file->fcounters[CP_F_POSIX_READ_TIME] / nprocs) -
                           (file->fcounters[CP_F_POSIX_WRITE_TIME] / nprocs) -
                           (file->fcounters[CP_F_POSIX_META_TIME] / nprocs));
638

639 640 641 642
            open_cycles = ceil((double)total_ind_opens / nprocs);
            calc_io_delays(file, open_cycles, ceil((double)total_ind_io_ops / nprocs), total_delay,
                           &first_io_delay, &close_delay, &inter_cycle_delay, &inter_io_delay);
        }
643 644 645 646 647 648 649 650 651 652 653
    }
    else
    {
        extra_opens = file->counters[CP_POSIX_OPENS] % nprocs;
        if (extra_opens && ((file->counters[CP_POSIX_OPENS] / nprocs) % extra_opens))
        {
            extra_opens = 0;
        }
        else
        {
            extra_io_ops = total_io_ops % nprocs;
654 655 656 657 658
            if (extra_io_ops != extra_opens)
            {
                extra_opens = 0;
                extra_io_ops = 0;
            }
659 660 661
        }

        total_coll_opens = 0;
662
        total_ind_opens = file->counters[CP_POSIX_OPENS] - extra_opens;
663 664 665

        total_ind_io_ops = total_io_ops - extra_io_ops;
        total_coll_io_ops = 0;
666

667 668 669 670 671 672
        total_delay = (file->fcounters[CP_F_CLOSE_TIMESTAMP] -
                       file->fcounters[CP_F_OPEN_TIMESTAMP] -
                       (file->fcounters[CP_F_POSIX_READ_TIME] / nprocs) -
                       (file->fcounters[CP_F_POSIX_WRITE_TIME] / nprocs) -
                       (file->fcounters[CP_F_POSIX_META_TIME] / nprocs));

673 674 675
        open_cycles = ceil((double)total_ind_opens / nprocs);
        calc_io_delays(file, open_cycles, ceil((double)total_ind_io_ops / nprocs), total_delay,
                       &first_io_delay, &close_delay, &inter_cycle_delay, &inter_io_delay);
676 677 678
    }
    assert(extra_opens <= open_cycles);

679 680 681 682
    /* calculate average meta op time (for i/o and opens/closes) */
    meta_op_time = file->fcounters[CP_F_POSIX_META_TIME] / ((2 * file->counters[CP_POSIX_OPENS]) +
                   file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES]);

683 684 685 686 687
    /* it is rare to overwrite existing files, so set the create flag */
    if (file->counters[CP_BYTES_WRITTEN])
    {
        create_flag = 1;
    }
688

689 690
    /* generate all events for this collectively opened file */
    for (i = 0; i < open_cycles; i++)
691
    {
692 693
        ind_opens_this_cycle = ceil((double)total_ind_opens / (open_cycles - i));
        coll_opens_this_cycle = total_coll_opens / (open_cycles - i);
694

695 696 697
        /* assign any extra opens to rank 0 (these may correspond to file creations or
         * header reads/writes)
         */
698
        if (extra_opens && !(i % (open_cycles / extra_opens)))
699
        {
700 701
            cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                               io_context, (io_context->my_rank == 0));
702 703
            create_flag = 0;

704 705 706 707 708 709
            if (!file->counters[CP_COLL_OPENS] && !file->counters[CP_INDEP_OPENS])
            {
                cur_time = generate_psx_coll_io_events(file, 1, 0, nprocs, nprocs, 0.0,
                                                       meta_op_time, cur_time, io_context);
                extra_io_ops--;
            }
710

711 712
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank == 0));
713 714 715
            file->counters[CP_POSIX_OPENS]--;
        }

716
        while (ind_opens_this_cycle)
717
        {
718 719 720 721 722
            if (ind_opens_this_cycle >= nprocs)
                rank_cnt = nprocs;
            else
                rank_cnt = ind_opens_this_cycle;

723 724
            cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                               io_context, (io_context->my_rank < rank_cnt));
725 726
            create_flag = 0;

727 728 729 730
            cur_time += first_io_delay;

            ind_io_ops_this_cycle = ceil(((double)total_ind_io_ops / total_ind_opens) * rank_cnt);
            cur_time = generate_psx_coll_io_events(file, ind_io_ops_this_cycle, 0, nprocs,
731
                                                   nprocs, inter_io_delay, meta_op_time,
732 733 734 735
                                                   cur_time, io_context);
            total_ind_io_ops -= ind_io_ops_this_cycle;

            cur_time += close_delay;
736

737 738
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank < rank_cnt));
739

740 741 742 743 744 745
            file->counters[CP_POSIX_OPENS] -= rank_cnt;
            ind_opens_this_cycle -= rank_cnt;
            total_ind_opens -= rank_cnt;

            if (file->counters[CP_POSIX_OPENS])
                cur_time += inter_cycle_delay;
746 747
        }

748
        while (coll_opens_this_cycle)
749
        {
750
            assert(!create_flag);
751

752
            cur_time = generate_barrier_event(file, 0, cur_time, io_context);
753 754

            cur_time = generate_psx_open_event(file, create_flag, meta_op_time,
755
                                               cur_time, io_context, 1);
756

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
            cur_time += first_io_delay;

            if (file->counters[CP_INDEP_OPENS])
                ind_io_ops_this_cycle = 0;
            else
                ind_io_ops_this_cycle = ceil((double)total_ind_io_ops / 
                                             (file->counters[CP_COLL_OPENS] / nprocs));

            coll_io_ops_this_cycle = ceil((double)total_coll_io_ops / 
                                          (file->counters[CP_COLL_OPENS] / nprocs));
            cur_time = generate_psx_coll_io_events(file, ind_io_ops_this_cycle,
                                                   coll_io_ops_this_cycle, nprocs, in_agg_cnt,
                                                   inter_io_delay, meta_op_time,
                                                   cur_time, io_context);
            total_ind_io_ops -= ind_io_ops_this_cycle;
            total_coll_io_ops -= coll_io_ops_this_cycle;

            cur_time += close_delay;
775

776
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context, 1);
777 778

            file->counters[CP_POSIX_OPENS] -= nprocs;
779 780
            file->counters[CP_COLL_OPENS] -= nprocs;
            coll_opens_this_cycle -= nprocs;
781 782 783 784
            total_coll_opens -= nprocs;

            if (file->counters[CP_POSIX_OPENS])
                cur_time += inter_cycle_delay;
785 786
        }
    }
787 788 789 790 791 792

    return;
}

/* fill in an open event structure and store it with the rank context */
static double generate_psx_open_event(
793
    struct darshan_file *file, int create_flag, double meta_op_time,
794
    double cur_time, struct rank_io_context *io_context, int insert_flag)
795
{
796 797 798 799 800 801 802
    struct darshan_io_op next_io_op = 
    {
        .codes_op.op_type = CODES_WK_OPEN,
        .codes_op.u.open.file_id = file->hash,
        .codes_op.u.open.create_flag = create_flag,
        .start_time = cur_time
    };
803 804 805

    /* set the end time of the event based on time spent in POSIX meta operations */
    cur_time += meta_op_time;
806
    next_io_op.end_time = cur_time;
807

808
    /* store the open event (if this rank performed it) */
809
    if (insert_flag)
810
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
811 812 813 814 815 816

    return cur_time;
}

/* fill in a close event structure and store it with the rank context */
static double generate_psx_close_event(
817
    struct darshan_file *file, double meta_op_time, double cur_time,
818
    struct rank_io_context *io_context, int insert_flag)
819
{
820 821 822 823 824 825
    struct darshan_io_op next_io_op =
    {
        .codes_op.op_type = CODES_WK_CLOSE,
        .codes_op.u.close.file_id = file->hash,
        .start_time = cur_time
    };
826 827 828

    /* set the end time of the event based on time spent in POSIX meta operations */
    cur_time += meta_op_time;
829
    next_io_op.end_time = cur_time;
830

831
    /* store the close event (if this rank performed it) */
832
    if (insert_flag)
833
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
834 835 836 837 838 839

    return cur_time;
}

/* fill in a barrier event structure and store it with the rank context */
static double generate_barrier_event(
840
    struct darshan_file *file, int64_t root, double cur_time, struct rank_io_context *io_context)
841
{
842 843 844 845 846 847 848
    struct darshan_io_op next_io_op =
    {
        .codes_op.op_type = CODES_WK_BARRIER, 
        .codes_op.u.barrier.count = -1, /* all processes */
        .codes_op.u.barrier.root = root,
        .start_time = cur_time
    };
849 850

    cur_time += .000001; /* small synthetic delay representing time to barrier */
851
    next_io_op.end_time = cur_time;
852

853
    /* store the barrier event */
854
    if (file->rank == -1)
855
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
856 857 858 859 860 861

    return cur_time;
}

/* generate all i/o events for one independent file open and store them with the rank context */
static double generate_psx_ind_io_events(
862 863
    struct darshan_file *file, int64_t io_ops_this_cycle, double inter_io_delay,
    double meta_op_time, double cur_time, struct rank_io_context *io_context)
864 865 866 867 868 869 870 871 872
{
    static int rw = -1; /* rw = 1 for write, 0 for read, -1 for uninitialized */
    static int64_t io_ops_this_rw;
    static double rd_bw = 0.0, wr_bw = 0.0;
    int64_t psx_rw_ops_remaining = file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES];
    double io_op_time;
    size_t io_sz;
    off_t io_off;
    int64_t i;
873
    struct darshan_io_op next_io_op;
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

    /* if there are no i/o ops, just return immediately */
    if (!io_ops_this_cycle)
        return cur_time;

    /* initialze static variables when a new file is opened */
    if (rw == -1)
    {
        /* initialize rw to be the first i/o operation found in the log */
        if (file->fcounters[CP_F_WRITE_START_TIMESTAMP] == 0.0)
            rw = 0;
        else if (file->fcounters[CP_F_READ_START_TIMESTAMP] == 0.0)
            rw = 1;
        else
            rw = (file->fcounters[CP_F_READ_START_TIMESTAMP] <
                  file->fcounters[CP_F_WRITE_START_TIMESTAMP]) ? 0 : 1;

        /* determine how many io ops to do before next rw switch */
        if (!rw)
            io_ops_this_rw = file->counters[CP_POSIX_READS] /
                             ((file->counters[CP_RW_SWITCHES] / 2) + 1);
        else
            io_ops_this_rw = file->counters[CP_POSIX_WRITES] /
                             ((file->counters[CP_RW_SWITCHES] / 2) + 1);

        /* initialize the rd and wr bandwidth values using total io size and time */
        if (file->fcounters[CP_F_POSIX_READ_TIME])
            rd_bw = file->counters[CP_BYTES_READ] / file->fcounters[CP_F_POSIX_READ_TIME];
        if (file->fcounters[CP_F_POSIX_WRITE_TIME])
            wr_bw = file->counters[CP_BYTES_WRITTEN] / file->fcounters[CP_F_POSIX_WRITE_TIME];
    }

    /* loop to generate all reads/writes for this open/close sequence */
    for (i = 0; i < io_ops_this_cycle; i++)
    {
        /* calculate what value to use for i/o size and offset */
910
        determine_io_params(file, rw, 1, 1, &io_sz, &io_off);
911 912 913
        if (!rw)
        {
            /* generate a read event */
914 915 916 917 918
            next_io_op.codes_op.op_type = CODES_WK_READ;
            next_io_op.codes_op.u.read.file_id = file->hash;
            next_io_op.codes_op.u.read.size = io_sz;
            next_io_op.codes_op.u.read.offset = io_off;
            next_io_op.start_time = cur_time;
919 920 921 922 923

            /* set the end time based on observed bandwidth and io size */
            if (rd_bw == 0.0)
                io_op_time = 0.0;
            else
924
                io_op_time = (io_sz / rd_bw);
925 926 927

            /* update time, accounting for metadata time */
            cur_time += (io_op_time + meta_op_time);
928
            next_io_op.end_time = cur_time;
929 930 931 932 933
            file->counters[CP_POSIX_READS]--;
        }
        else
        {
            /* generate a write event */
934 935 936 937 938
            next_io_op.codes_op.op_type = CODES_WK_WRITE;
            next_io_op.codes_op.u.write.file_id = file->hash;
            next_io_op.codes_op.u.write.size = io_sz;
            next_io_op.codes_op.u.write.offset = io_off;
            next_io_op.start_time = cur_time;
939 940 941 942 943

            /* set the end time based on observed bandwidth and io size */
            if (wr_bw == 0.0)
                io_op_time = 0.0;
            else
944
                io_op_time = (io_sz / wr_bw);
945 946 947

            /* update time, accounting for metadata time */
            cur_time += (io_op_time + meta_op_time);
948
            next_io_op.end_time = cur_time;
949 950 951 952 953 954 955
            file->counters[CP_POSIX_WRITES]--;
        }
        psx_rw_ops_remaining--;
        io_ops_this_rw--;
        assert(file->counters[CP_POSIX_READS] >= 0);
        assert(file->counters[CP_POSIX_WRITES] >= 0);

956 957
        /* store the i/o event */
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

        /* determine whether to toggle between reads and writes */
        if (!io_ops_this_rw && psx_rw_ops_remaining)
        {
            /* toggle the read/write flag */
            rw ^= 1;
            file->counters[CP_RW_SWITCHES]--;

            /* determine how many io ops to do before next rw switch */
            if (!rw)
                io_ops_this_rw = file->counters[CP_POSIX_READS] /
                                 ((file->counters[CP_RW_SWITCHES] / 2) + 1);
            else
                io_ops_this_rw = file->counters[CP_POSIX_WRITES] /
                                 ((file->counters[CP_RW_SWITCHES] / 2) + 1);
        }

        if (i != (io_ops_this_cycle - 1))
        {
            /* update current time to account for possible delay between i/o operations */
978
            cur_time += inter_io_delay;
979 980 981 982 983 984 985 986 987 988 989 990
        }
    }

    /* reset the static rw flag if this is the last open-close cycle for this file */
    if (file->counters[CP_POSIX_OPENS] == 1)
    {
        rw = -1;
    }

    return cur_time;
}

991 992
static double generate_psx_coll_io_events(
    struct darshan_file *file, int64_t ind_io_ops_this_cycle, int64_t coll_io_ops_this_cycle,
993 994
    int64_t nprocs, int64_t aggregator_cnt, double inter_io_delay, double meta_op_time,
    double cur_time, struct rank_io_context *io_context)
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
{
    static int rw = -1; /* rw = 1 for write, 0 for read, -1 for uninitialized */
    static int64_t io_ops_this_rw;
    static double rd_bw = 0.0, wr_bw = 0.0;
    int64_t psx_rw_ops_remaining = file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES];
    int64_t total_io_ops_this_cycle = ind_io_ops_this_cycle + coll_io_ops_this_cycle;
    int64_t total_coll_io_ops =
            (file->counters[CP_COLL_READS] + file->counters[CP_COLL_WRITES]) / nprocs;
    int64_t tmp_rank;
    int64_t next_ind_io_rank = 0;
    int64_t io_cnt;
    int64_t ranks_per_aggregator = nprocs / aggregator_cnt;
1007
    int64_t ind_ops_remaining = 0;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    double io_op_time;
    double max_cur_time = 0.0;
    int ind_coll;
    size_t io_sz;
    off_t io_off;
    int64_t i, j;
    struct darshan_io_op next_io_op;

    if (!total_io_ops_this_cycle)
        return cur_time;

    /* initialze static variables when a new file is opened */
    if (rw == -1)
    {
        /* initialize rw to be the first i/o operation found in the log */
        if (file->fcounters[CP_F_WRITE_START_TIMESTAMP] == 0.0)
            rw = 0;
        else if (file->fcounters[CP_F_READ_START_TIMESTAMP] == 0.0)
            rw = 1;
        else
            rw = (file->fcounters[CP_F_READ_START_TIMESTAMP] <
                  file->fcounters[CP_F_WRITE_START_TIMESTAMP]) ? 0 : 1;

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
        /* determine how many io ops to do before next rw switch */
        if (!rw)
        {
            if (file->counters[CP_COLL_OPENS])
                io_ops_this_rw =
                    ((file->counters[CP_COLL_READS] / nprocs) + file->counters[CP_INDEP_READS]) /
                    ((file->counters[CP_RW_SWITCHES] / (2 * aggregator_cnt)) + 1);
            else
                io_ops_this_rw = file->counters[CP_POSIX_READS] /
                                 ((file->counters[CP_RW_SWITCHES] / (2 * aggregator_cnt)) + 1);
        }
        else
        {
            if (file->counters[CP_COLL_OPENS])
                io_ops_this_rw =
                    ((file->counters[CP_COLL_WRITES] / nprocs) + file->counters[CP_INDEP_WRITES]) /
                    ((file->counters[CP_RW_SWITCHES] / (2 * aggregator_cnt)) + 1);
            else
                io_ops_this_rw = file->counters[CP_POSIX_WRITES] /
                                 ((file->counters[CP_RW_SWITCHES] / (2 * aggregator_cnt)) + 1);
        }

1053 1054 1055 1056 1057 1058 1059
        /* initialize the rd and wr bandwidth values using total io size and time */
        if (file->fcounters[CP_F_POSIX_READ_TIME])
            rd_bw = file->counters[CP_BYTES_READ] / file->fcounters[CP_F_POSIX_READ_TIME];
        if (file->fcounters[CP_F_POSIX_WRITE_TIME])
            wr_bw = file->counters[CP_BYTES_WRITTEN] / file->fcounters[CP_F_POSIX_WRITE_TIME];
    }

1060 1061 1062 1063 1064
    if (coll_io_ops_this_cycle)
        ind_ops_remaining = ceil((double)ind_io_ops_this_cycle / coll_io_ops_this_cycle);
    else
        ind_ops_remaining = ind_io_ops_this_cycle;

1065 1066
    for (i = 0; i < total_io_ops_this_cycle; i++)
    {
1067
        if (ind_ops_remaining)
1068 1069 1070 1071
        {
            ind_coll = 0;
            tmp_rank = (next_ind_io_rank++) % nprocs;
            io_cnt = 1;
1072 1073
            ind_io_ops_this_cycle--;
            ind_ops_remaining--;
1074 1075 1076 1077 1078 1079 1080 1081 1082
            if (!rw)
                file->counters[CP_INDEP_READS]--;
            else
                file->counters[CP_INDEP_WRITES]--;
        }
        else
        {
            ind_coll = 1;
            tmp_rank = 0;
1083
            coll_io_ops_this_cycle--;
1084 1085 1086
            if (!rw)
            {
                io_cnt = ceil((double)(file->counters[CP_POSIX_READS] -
1087
                              file->counters[CP_INDEP_READS]) / 
1088 1089 1090 1091 1092 1093
                              (file->counters[CP_COLL_READS] / nprocs));
                file->counters[CP_COLL_READS] -= nprocs;
            }
            else
            {
                io_cnt = ceil((double)(file->counters[CP_POSIX_WRITES] -
1094
                              file->counters[CP_INDEP_WRITES]) / 
1095 1096 1097
                              (file->counters[CP_COLL_WRITES] / nprocs));
                file->counters[CP_COLL_WRITES] -= nprocs;
            }
1098 1099 1100 1101 1102 1103 1104

            if (coll_io_ops_this_cycle)
                ind_ops_remaining = ceil((double)ind_io_ops_this_cycle / coll_io_ops_this_cycle);
            else
                ind_ops_remaining = ind_io_ops_this_cycle;

            cur_time = generate_barrier_event(file, 0, cur_time, io_context);
1105 1106 1107 1108
        }

        for (j = 0; j < io_cnt; j++)
        {
1109 1110
            determine_io_params(file, rw, (ind_coll) ? io_cnt - j : ind_io_ops_this_cycle + 1,
                                aggregator_cnt, &io_sz, &io_off);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
            if (!rw)
            {
                /* generate a read event */
                next_io_op.codes_op.op_type = CODES_WK_READ;
                next_io_op.codes_op.u.read.file_id = file->hash;
                next_io_op.codes_op.u.read.size = io_sz;
                next_io_op.codes_op.u.read.offset = io_off;
                next_io_op.start_time = cur_time;

                /* set the end time based on observed bandwidth and io size */
                if (rd_bw == 0.0)
                    io_op_time = 0.0;
                else
                    io_op_time = (io_sz / rd_bw);
                
                next_io_op.end_time = cur_time + io_op_time + meta_op_time;
                file->counters[CP_POSIX_READS]--;
            }
            else
            {
                /* generate a write event */
                next_io_op.codes_op.op_type = CODES_WK_WRITE;
                next_io_op.codes_op.u.write.file_id = file->hash;
                next_io_op.codes_op.u.write.size = io_sz;
                next_io_op.codes_op.u.write.offset = io_off;
                next_io_op.start_time = cur_time;

                /* set the end time based on observed bandwidth and io size */
                if (wr_bw == 0.0)
                    io_op_time = 0.0;
                else
                    io_op_time = (io_sz / wr_bw);

                next_io_op.end_time = cur_time + io_op_time + meta_op_time;
                file->counters[CP_POSIX_WRITES]--;
            }
            psx_rw_ops_remaining--;
            assert(file->counters[CP_POSIX_READS] >= 0);
            assert(file->counters[CP_POSIX_WRITES] >= 0);

            /*  store the i/o event */
            if (tmp_rank == io_context->my_rank)
                darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);

            if (next_io_op.end_time > max_cur_time)
                max_cur_time = next_io_op.end_time;

            tmp_rank += ranks_per_aggregator;
1159
            if (ind_coll && (tmp_rank >= (ranks_per_aggregator * aggregator_cnt)))
1160 1161 1162 1163 1164 1165 1166
            {
                tmp_rank = 0;
                cur_time = max_cur_time;
            }
        }
        io_ops_this_rw--;

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
        if (ind_coll)
        {
            total_coll_io_ops--;

            cur_time = max_cur_time;
            if (i != (total_io_ops_this_cycle - 1))
                cur_time += inter_io_delay;
        }
        else
        {
1177 1178
            if (tmp_rank == (nprocs - 1) || (i == (total_io_ops_this_cycle - 1)))
            {
1179 1180
                cur_time = max_cur_time;

1181 1182 1183
                if (i != (total_io_ops_this_cycle - 1))
                    cur_time += inter_io_delay;
            }
1184 1185
        }

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
        /* determine whether to toggle between reads and writes */
        if (!io_ops_this_rw && psx_rw_ops_remaining)
        {
            /* toggle the read/write flag */
            rw ^= 1;
            file->counters[CP_RW_SWITCHES] -= aggregator_cnt;

            /* determine how many io ops to do before next rw switch */
            if (!rw)
            {
                if (file->counters[CP_COLL_OPENS])
                    io_ops_this_rw =
                        ((file->counters[CP_COLL_READS] / nprocs) +
                        file->counters[CP_INDEP_READS]) / ((file->counters[CP_RW_SWITCHES] /
                        (2 * aggregator_cnt)) + 1);
                else
                    io_ops_this_rw = file->counters[CP_POSIX_READS] /
                                     ((file->counters[CP_RW_SWITCHES] / (2 * aggregator_cnt)) + 1);
            }
            else
            {
                if (file->counters[CP_COLL_OPENS])
                    io_ops_this_rw =
                        ((file->counters[CP_COLL_WRITES] / nprocs) +
                        file->counters[CP_INDEP_WRITES]) / ((file->counters[CP_RW_SWITCHES] /
                        (2 * aggregator_cnt)) + 1);
                else
                    io_ops_this_rw = file->counters[CP_POSIX_WRITES] /
                                     ((file->counters[CP_RW_SWITCHES] / (2 * aggregator_cnt)) + 1);
            }
        }
    }

    /* reset the static rw flag if this is the last open-close cycle for this file */
    if (file->counters[CP_POSIX_OPENS] <= nprocs)
    {
        rw = -1;
    }

1225
    return cur_time;
1226 1227
}

1228
/* WARNING: BRUTE FORCE */
1229
static void determine_io_params(
1230
    struct darshan_file *file, int write_flag, int64_t io_this_op, int64_t proc_count,
1231 1232 1233 1234
    size_t *io_sz, off_t *io_off)
{
    static uint64_t next_rd_off = 0;
    static uint64_t next_wr_off = 0;
1235 1236 1237 1238 1239 1240
    static int size_bin_ndx = -1;
    static int64_t io_this_size_bin = 0;
    static int64_t rd_common_counts[4];
    static int64_t wr_common_counts[4];
    int64_t *rd_size_bins = &(file->counters[CP_SIZE_READ_0_100]);
    int64_t *wr_size_bins = &(file->counters[CP_SIZE_WRITE_0_100]);
1241
    int64_t *size_bins = NULL;
1242 1243
    int64_t *common_accesses = &(file->counters[CP_ACCESS1_ACCESS]); /* 4 common accesses */
    int64_t *common_access_counts = &(file->counters[CP_ACCESS1_COUNT]); /* common access counts */
1244
    int64_t *total_io_size = NULL;
1245
    int64_t last_io_byte;
1246
    int look_for_small_bin = 0;
1247
    int i, j = 0;
1248 1249 1250 1251 1252 1253
    const int64_t size_bin_min_vals[10] = { 0, 100, 1024, 10 * 1024, 100 * 1024, 1024 * 1024,
                                            4 * 1024 * 1024, 10 * 1024 * 1024, 100 * 1024 * 1024,
                                            1024 * 1024 * 1024 };
    const int64_t size_bin_max_vals[10] = { 100, 1024, 10 * 1024, 100 * 1024, 1024 * 1024,
                                            4 * 1024 * 1024, 10 * 1024 * 1024, 100 * 1024 * 1024,
                                            1024 * 1024 * 1024, INT64_MAX };
1254

1255 1256 1257
    assert(io_this_op);

    if (size_bin_ndx == -1)
1258 1259 1260
    {
        for (i = 0; i < 4; i++)
        {
1261 1262
            for (j = 0; j < 10; j++)
            {
1263 1264
                if ((common_accesses[i] >= size_bin_min_vals[j]) &&
                    (common_accesses[i] <= size_bin_max_vals[j]))
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
                {
                    if (rd_size_bins[j] && wr_size_bins[j])
                    {
                        rd_common_counts[i] = MIN(common_access_counts[i] / 2, rd_size_bins[j]);
                        wr_common_counts[i] = common_access_counts[i] - rd_common_counts[i];
                    }
                    else if (rd_size_bins[j])
                    {
                        rd_common_counts[i] = common_access_counts[i];
                        wr_common_counts[i] = 0;
                    }
                    else if (wr_size_bins[j])
                    {
                        rd_common_counts[i] = 0;
                        wr_common_counts[i] = common_access_counts[i];
                    }
                    else
                    {
                        rd_common_counts[i] = wr_common_counts[i] = 0;
                    }
                    break;
                }
            }
1288 1289 1290 1291 1292 1293 1294 1295
        }
    }

    /* assign data values depending on whether the operation is a read or write */
    if (write_flag)
    {
        total_io_size = &(file->counters[CP_BYTES_WRITTEN]);
        last_io_byte = file->counters[CP_MAX_BYTE_WRITTEN];
1296 1297
        size_bins = wr_size_bins;
        common_access_counts = wr_common_counts;
1298 1299 1300 1301 1302
    }
    else
    {
        total_io_size = &(file->counters[CP_BYTES_READ]);
        last_io_byte = file->counters[CP_MAX_BYTE_READ];
1303 1304
        size_bins = rd_size_bins;
        common_access_counts = rd_common_counts;
1305 1306
    }

1307
    if (!io_this_size_bin)
1308
    {
1309
        if (io_this_op < proc_count)
1310
        {
1311 1312
            look_for_small_bin = 1;
            for (i = 0; i < 10; i++)
1313
            {
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
                if (size_bins[i] % proc_count)
                {
                    if (!io_this_size_bin)
                    {
                        size_bin_ndx = i;
                        io_this_size_bin = MIN(size_bins[i] % proc_count, io_this_op);
                    }
                    else if ((size_bins[i] % proc_count) < io_this_size_bin)
                    {
                        size_bin_ndx = i;
                        io_this_size_bin = size_bins[i] % proc_count;
                    }
                }
1327 1328
            }
        }
1329
        else
1330
        {
1331
            for (i = 0; i < 10; i++)
1332
            {
1333
                if (size_bins[i] && ((size_bins[i] % proc_count) == 0))
1334
                {
1335 1336
                    if (!io_this_size_bin ||
                        (io_this_size_bin && (size_bins[i] > size_bins[size_bin_ndx])))
1337
                    {
1338 1339
                        size_bin_ndx = i;
                        io_this_size_bin = proc_count;
1340 1341
                    }
                }
1342 1343 1344 1345 1346 1347 1348 1349
            }
        }

        if (!io_this_size_bin)
        {
            for (i = 0; i < 10; i++)
            {
                if (size_bins[i])
1350
                {
1351 1352 1353
                    size_bin_ndx = i;
                    io_this_size_bin = size_bins[i];
                    if (io_this_size_bin > io_this_op)
1354
                    {
1355
                        io_this_size_bin = io_this_op;
1356
                    }
1357
                    break;
1358 1359 1360 1361
                }
            }
        }
    }
1362 1363 1364

    *io_sz = 0;
    if (*total_io_size > 0)
1365
    {
1366 1367 1368
        if ((write_flag && (file->counters[CP_POSIX_WRITES] == 1)) ||
            (!write_flag && (file->counters[CP_POSIX_READS] == 1)))
        {
1369
            *io_sz = ALIGN_BY_8(*total_io_size);
1370 1371
        }
        else
1372
        {
1373
            /* try to assign a common access first (intelligently) */
1374 1375
            for (j = 0; j < 4; j++)
            {
1376
                if (common_access_counts[j] &&
1377 1378
                    (common_accesses[j] >= size_bin_min_vals[size_bin_ndx]) &&
                    (common_accesses[j] <= size_bin_max_vals[size_bin_ndx]))
1379
                {
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
                    if (look_for_small_bin)
                    {
                        if (common_access_counts[j] == io_this_op)
                        {
                            *io_sz = common_accesses[j];
                            common_access_counts[j]--;
                            break;
                        }
                    }
                    else
                    {
                        *io_sz = common_accesses[j];
                        common_access_counts[j]--;
                        break;
                    }
1395 1396
                }

1397
                if ((j == 3) && look_for_small_bin)
1398
                {
1399 1400
                    j = 0;
                    look_for_small_bin = 0;
1401 1402
                }
            }
1403 1404 1405

            /* if no common accesses left, then assign default size for this bin */
            if (*io_sz == 0)
1406 1407 1408 1409 1410
            {
                size_t gen_size;
                gen_size = (size_bin_max_vals[size_bin_ndx] - size_bin_min_vals[size_bin_ndx]) / 2;
                *io_sz = ALIGN_BY_8(gen_size);
            }
1411 1412 1413 1414 1415
        }
        assert(*io_sz);
    }

    *total_io_size -= *io_sz;
1416 1417
    size_bins[size_bin_ndx]--;
    io_this_size_bin--;
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427

    /* next, determine the offset to use */

    /*  for now we just assign a random offset that makes sure not to write past the recorded
     *  last byte written in the file.
     */
    if (*io_sz == 0)
    {
        *io_off = last_io_byte + 1;
    }
1428
    else
1429
    {
1430 1431 1432 1433
        if (write_flag)
        {
            if ((next_wr_off + *io_sz) > (last_io_byte + 1))
                next_wr_off = 0;
1434

1435 1436 1437 1438 1439 1440 1441
            *io_off = next_wr_off;
            next_wr_off += *io_sz;
        }
        else
        {
            if ((next_rd_off + *io_sz) > (last_io_byte + 1))
                next_rd_off = 0;
1442

1443 1444 1445
            *io_off = next_rd_off;
            next_rd_off += *io_sz;
        }
1446 1447 1448 1449 1450
    }

    /* reset static variable if this is the last i/o op for this file */
    if ((file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES]) == 1)
    {
1451 1452
        io_this_size_bin = 0;
        size_bin_ndx = -1;
1453 1454 1455 1456 1457 1458 1459 1460 1461
        next_rd_off = next_wr_off = 0;
    }

    return;
}

/* calculate the simulated "delay" between different i/o events using delay info
 * from the file counters */
static void calc_io_delays(
1462
    struct darshan_file *file, int64_t num_opens, int64_t num_io_ops, double total_delay,
1463 1464 1465 1466 1467 1468 1469
    double *first_io_delay, double *close_delay, double *inter_open_delay, double *inter_io_delay)
{
    double first_io_time, last_io_time;
    double first_io_pct, close_pct, inter_open_pct, inter_io_pct;
    double total_delay_pct;
    double tmp_inter_io_pct, tmp_inter_open_pct;

1470
    if (total_delay > 0.0)
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    {
        /* determine the time of the first io operation */
        if (!file->fcounters[CP_F_WRITE_START_TIMESTAMP])
            first_io_time = file->fcounters[CP_F_READ_START_TIMESTAMP];
        else if (!file->fcounters[CP_F_READ_START_TIMESTAMP])
            first_io_time = file->fcounters[CP_F_WRITE_START_TIMESTAMP];
        else if (file->fcounters[CP_F_READ_START_TIMESTAMP] <
                 file->fcounters[CP_F_WRITE_START_TIMESTAMP])
            first_io_time = file->fcounters[CP_F_READ_START_TIMESTAMP];
        else
            first_io_time = file->fcounters[CP_F_WRITE_START_TIMESTAMP];

        /* determine the time of the last io operation */
        if (file->fcounters[CP_F_READ_END_TIMESTAMP] > file->fcounters[CP_F_WRITE_END_TIMESTAMP])
            last_io_time = file->fcounters[CP_F_READ_END_TIMESTAMP];
        else
            last_io_time = file->fcounters[CP_F_WRITE_END_TIMESTAMP];

        /* no delay contribution for inter-open delay if there is only a single open */
        if (num_opens > 1)
            inter_open_pct = DEF_INTER_CYC_DELAY_PCT;

        /* no delay contribution for inter-io delay if there is one or less io op */
1494
        if ((num_io_ops - num_opens) > 0)
1495 1496 1497 1498 1499
            inter_io_pct = DEF_INTER_IO_DELAY_PCT;

        /* determine delay contribution for first io and close delays */
        if (first_io_time != 0.0)
        {
1500 1501 1502 1503
            first_io_pct = (first_io_time - file->fcounters[CP_F_OPEN_TIMESTAMP]) *
                           (num_opens / total_delay);
            close_pct = (file->fcounters[CP_F_CLOSE_TIMESTAMP] - last_io_time) *
                        (num_opens / total_delay);
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
        }
        else
        {
            first_io_pct = 0.0;
            close_pct = 1 - inter_open_pct;
        }

        /* adjust per open delay percentages using a simple heuristic */
        total_delay_pct = inter_open_pct + inter_io_pct + first_io_pct + close_pct;
        if ((total_delay_pct < 1) && (inter_open_pct || inter_io_pct))
        {
            /* only adjust inter-open and inter-io delays if we underestimate */
            tmp_inter_open_pct = (inter_open_pct / (inter_open_pct + inter_io_pct)) *
                                 (1 - first_io_pct - close_pct);
            tmp_inter_io_pct = (inter_io_pct / (inter_open_pct + inter_io_pct)) *
                               (1 - first_io_pct - close_pct);
            inter_open_pct = tmp_inter_open_pct;
            inter_io_pct = tmp_inter_io_pct;
        }
        else
        {
            inter_open_pct += (inter_open_pct / total_delay_pct) * (1 - total_delay_pct);
            inter_io_pct += (inter_io_pct / total_delay_pct) * (1 - total_delay_pct);
            first_io_pct += (first_io_pct / total_delay_pct) * (1 - total_delay_pct);
            close_pct += (close_pct / total_delay_pct) * (1 - total_delay_pct);
        }

1531 1532
        *first_io_delay = (first_io_pct * total_delay) / num_opens;
        *close_delay = (close_pct * total_delay) / num_opens;
1533 1534

        if (num_opens > 1)
1535
            *inter_open_delay = (inter_open_pct * total_delay) / (num_opens - 1);
1536
        if ((num_io_ops - num_opens) > 0)
1537
            *inter_io_delay = (inter_io_pct * total_delay) / (num_io_ops - num_opens);
1538 1539 1540 1541 1542 1543 1544 1545 1546
    }

    return;
}

/* check to make sure file stats are valid and properly formatted */
static void file_sanity_check(
    struct darshan_file *file, struct darshan_job *job)
{
1547 1548 1549
    int64_t ops_not_implemented;
    int64_t ops_total;

1550 1551 1552 1553 1554 1555 1556 1557
    /* make sure we have log version 2.03 or greater */
    if (strcmp(job->version_string, "2.03") < 0)
    {
        fprintf(stderr, "Error: Darshan log version must be >= 2.03 (using %s)\n",
                job->version_string);
        exit(EXIT_FAILURE);
    }

1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    assert(file->counters[CP_POSIX_OPENS] != -1);
    assert(file->fcounters[CP_F_OPEN_TIMESTAMP] != -1);
    assert(file->counters[CP_COLL_OPENS] != -1);
    assert(file->fcounters[CP_F_CLOSE_TIMESTAMP] != -1);
    assert(file->counters[CP_POSIX_READS] != -1);
    assert(file->counters[CP_POSIX_WRITES] != -1);
    assert(file->fcounters[CP_F_POSIX_READ_TIME] != -1);
    assert(file->fcounters[CP_F_POSIX_WRITE_TIME] != -1);
    assert(file->fcounters[CP_F_POSIX_META_TIME] != -1);
    assert(file->fcounters[CP_F_READ_START_TIMESTAMP] != -1);
    assert(file->fcounters[CP_F_WRITE_START_TIMESTAMP] != -1);
    assert(file->fcounters[CP_F_READ_END_TIMESTAMP] != -1);
    assert(file->fcounters[CP_F_WRITE_END_TIMESTAMP] != -1);
    assert(file->counters[CP_BYTES_READ] != -1);
    assert(file->counters[CP_BYTES_WRITTEN] != -1);
    assert(file->counters[CP_RW_SWITCHES] != -1);

    /* set file close time to the end of execution if it is not given */
    if (file->fcounters[CP_F_CLOSE_TIMESTAMP] == 0.0)
        file->fcounters[CP_F_CLOSE_TIMESTAMP] = job->end_time - job->start_time + 1;

    /* collapse fopen/fread/etc. calls into the corresponding open/read/etc. counters */
    file->counters[CP_POSIX_OPENS] += file->counters[CP_POSIX_FOPENS];
    file->counters[CP_POSIX_READS] += file->counters[CP_POSIX_FREADS];
    file->counters[CP_POSIX_WRITES] += file->counters[CP_POSIX_FWRITES];
1583 1584 1585 1586 1587 1588 1589 1590
    file->counters[CP_POSIX_SEEKS] += file->counters[CP_POSIX_FSEEKS];

    /* reduce total meta time by percentage of ops not currently implemented */
    ops_not_implemented = file->counters[CP_POSIX_SEEKS] + file->counters[CP_POSIX_STATS] +
                          file->counters[CP_POSIX_FSYNCS];
    ops_total = ops_not_implemented + (2 * file->counters[CP_POSIX_OPENS]) + 
                file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES];
    file->fcounters[CP_F_POSIX_META_TIME] *= (1 - ((double)ops_not_implemented / ops_total));
1591 1592 1593

    return;
}
1594 1595 1596 1597 1598 1599 1600 1601 1602

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */