codes-darshan-io-wrkld.c 64.4 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

12
#include "darshan-logutils.h"
13

14 15
#define DEF_INTER_IO_DELAY_PCT 0.2
#define DEF_INTER_CYC_DELAY_PCT 0.4
16

17
#define DARSHAN_NEGLIGIBLE_DELAY 0.00001
18 19 20

#define RANK_HASH_TABLE_SIZE 397

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

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

25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* 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;
39

40
    off_t next_off;
41

42 43 44
    struct qhash_head hash_link;
};

45 46
static void * darshan_io_workload_read_config(
        ConfigHandle * handle,
47 48 49
        char const * section_name,
        char const * annotation,
        int num_ranks);
50
/* Darshan workload generator's implementation of the CODES workload API */
51 52 53
static int darshan_io_workload_load(const char *params, int app_id, int rank);
static void darshan_io_workload_get_next(int app_id, int rank, struct codes_workload_op *op);
static int darshan_io_workload_get_rank_cnt(const char *params, int app_id);
54 55 56 57 58 59 60 61 62 63
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);
static int darshan_io_op_compare(const void *p1, const void *p2);

64
/* Helper functions for implementing the Darshan workload generator */
65 66 67 68 69
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);
70
static double generate_psx_open_event(struct darshan_file *file, int create_flag,
71
                                      double meta_op_time, double cur_time,
72
                                      struct rank_io_context *io_context, int insert_flag);
73
static double generate_psx_close_event(struct darshan_file *file, double meta_op_time,
74 75
                                       double cur_time, struct rank_io_context *io_context,
                                       int insert_flag);
76 77
static double generate_barrier_event(struct darshan_file *file, int64_t root, double cur_time,
                                     struct rank_io_context *io_context);
78
static double generate_psx_ind_io_events(struct darshan_file *file, int64_t io_ops_this_cycle,
79 80
                                         double inter_io_delay, double cur_time,
                                         struct rank_io_context *io_context);
81 82
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,
83
                                          int64_t aggregator_cnt, double inter_io_delay,
84 85
                                          double meta_op_time, double cur_time,
                                          struct rank_io_context *io_context);
86 87
static void determine_ind_io_params(struct darshan_file *file, int write_flag, size_t *io_sz,
                                    off_t *io_off, struct rank_io_context *io_context);
88 89 90
static void determine_coll_io_params(struct darshan_file *file, int write_flag, int64_t coll_op_cnt,
                                     int64_t agg_cnt, int64_t agg_ndx, size_t *io_sz, off_t *io_off,
                                     struct rank_io_context *io_context);
91
static void calc_io_delays(struct darshan_file *file, int64_t num_opens, int64_t num_io_ops,
92
                           double total_delay, double *first_io_delay, double *close_delay,
93 94 95
                           double *inter_open_delay, double *inter_io_delay);
static void file_sanity_check(struct darshan_file *file, struct darshan_job *job);

96 97 98 99
/* workload method name and function pointers for the CODES workload API */
struct codes_workload_method darshan_io_workload_method =
{
    .method_name = "darshan_io_workload",
100
    .codes_workload_read_config = darshan_io_workload_read_config,
101 102
    .codes_workload_load = darshan_io_workload_load,
    .codes_workload_get_next = darshan_io_workload_get_next,
103
    .codes_workload_get_rank_cnt = darshan_io_workload_get_rank_cnt,
104 105
};

106 107
static int total_rank_cnt = 0;

108
/* hash table to store per-rank workload contexts */
109
static struct qhash_table *rank_tbl = NULL;
110
static int rank_tbl_pop = 0;
111

112 113
static void * darshan_io_workload_read_config(
        ConfigHandle * handle,
114 115 116
        char const * section_name,
        char const * annotation,
        int num_ranks)
117 118 119 120 121 122 123
{
    darshan_params *d = malloc(sizeof(*d));
    assert(d);
    d->log_file_path[0] = '\0';
    d->aggregator_cnt = -1;

    int rc = configuration_get_value_relpath(handle, section_name,
124
            "darshan_log_file", annotation, d->log_file_path,
125 126 127 128
            MAX_NAME_LENGTH_WKLD);
    assert(rc > 0);
    int tmp;
    rc = configuration_get_value_int(&config, "workload", 
129
            "darshan_aggregator_count", annotation, &tmp);
130 131 132 133
    assert(rc == 0);
    d->aggregator_cnt = tmp;
    return d;
}
134

135
/* load the workload generator for this rank, given input params */
136
static int darshan_io_workload_load(const char *params, int app_id, int rank)
137
{
138 139
    darshan_params *d_params = (darshan_params *)params;
    darshan_fd logfile_fd;
140 141
    struct darshan_job job;
    struct darshan_file next_file;
142
    struct rank_io_context *my_ctx;
143
    int ret;
144

145 146
    APP_ID_UNSUPPORTED(app_id, "darshan")

147
    if (!d_params)
148 149
        return -1;

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

155 156 157 158 159 160 161
    /* 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;
    }
162 163 164 165 166
    if (!total_rank_cnt)
    {
        total_rank_cnt = job.nprocs;
    }
    assert(rank < total_rank_cnt);
167

168 169 170 171 172 173 174 175 176 177
    /* 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();
178
    my_ctx->next_off = 0;
179

180 181 182 183 184 185 186 187 188 189
    /* 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 */
190
            generate_psx_ind_file_events(&next_file, my_ctx);
191 192 193 194 195 196
        }
        /* 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);
197 198 199

            /* 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);
200
        }
201
        else if (next_file.rank < rank)
202
            continue;
203 204
        else
            break;
205

206 207 208
        assert(next_file.counters[CP_POSIX_OPENS] == 0);
        assert(next_file.counters[CP_POSIX_READS] == 0);
        assert(next_file.counters[CP_POSIX_WRITES] == 0);
209 210 211 212
    }
    if (ret < 0)
        return -1;

213
    darshan_log_close(logfile_fd);
214

215 216 217 218 219 220 221 222 223 224 225 226 227
    /* 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));
228
    rank_tbl_pop++;
229

230 231 232 233
    return 0;
}

/* pull the next event (independent or collective) for this rank from its event context */
234
static void darshan_io_workload_get_next(int app_id, int rank, struct codes_workload_op *op)
235
{
236 237 238 239 240
    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;

241 242
    assert(rank < total_rank_cnt);

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    /* 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);
265 266
 
        rank_tbl_pop--;
267 268
        if (!rank_tbl_pop)
        {
269
            qhash_finalize(rank_tbl);
270 271
            rank_tbl = NULL;
        }
272 273 274 275 276 277 278 279 280
    }
    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;
281 282 283 284

    return;
}

285
static int darshan_io_workload_get_rank_cnt(const char *params, int app_id)
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
{
    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;
}

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
/* 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;
    }
    else
    {
        struct darshan_io_op *tmp = &(array->op_array[array->op_arr_ndx]);

400
        if ((tmp->start_time - last_op_time) <= DARSHAN_NEGLIGIBLE_DELAY)
401 402 403 404 405 406 407 408 409 410 411 412 413 414
        {
            /* 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;
        }
    }
415 416 417 418 419 420 421 422 423

    /* if this is the end op, free data structures */
    if (io_op->codes_op.op_type == CODES_WK_END)
    {
        free(array->op_array);
        free(array);
    }

    return;
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
}

/* 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;
}

/* 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 */
/*                                       */
/*****************************************/
460 461 462

/* generate events for an independently opened file, and store these events */
static void generate_psx_ind_file_events(
463
    struct darshan_file *file, struct rank_io_context *io_context)
464 465 466
{
    int64_t io_ops_this_cycle;
    double cur_time = file->fcounters[CP_F_OPEN_TIMESTAMP];
467
    double total_delay;
468 469 470 471 472 473 474 475 476 477 478 479 480
    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 */
481 482 483
    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];
484 485 486

    /* calculate synthetic delay values */
    calc_io_delays(file, file->counters[CP_POSIX_OPENS],
487 488
                   file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES], total_delay,
                   &first_io_delay, &close_delay, &inter_open_delay, &inter_io_delay);
489 490 491

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

494 495 496 497 498
    /* set the create flag if the file was written to */
    if (file->counters[CP_BYTES_WRITTEN])
    {
        create_flag = 1;
    }
499 500 501 502 503 504

    /* 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 */
505 506
        cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                           io_context, 1);
507 508 509 510 511 512 513 514 515 516
        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 */
517
        cur_time = generate_psx_ind_io_events(file, io_ops_this_cycle, inter_io_delay,
518
                                              cur_time, io_context);
519 520 521 522 523

        /* 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 */
524
        cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context, 1);
525 526 527 528 529 530 531

        /* account for potential interopen delay if more than one open */
        if (file->counters[CP_POSIX_OPENS] > 1)
        {
            cur_time += inter_open_delay;
        }
    }
532 533 534 535 536 537 538 539 540

    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)
{
541 542 543 544 545
    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;
546 547
    int64_t extra_opens = 0;
    int64_t extra_io_ops = 0;
548
    int64_t total_io_ops = file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES];
549 550 551 552
    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;
553
    int64_t rank_cnt;
554
    int create_flag = 0;
555
    double cur_time = file->fcounters[CP_F_OPEN_TIMESTAMP];
556
    double total_delay;
557 558 559 560 561
    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;
562
    int64_t i;
563 564 565 566 567 568 569 570 571 572

    /* 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);

573 574
    total_delay = file->fcounters[CP_F_CLOSE_TIMESTAMP] -
                  file->fcounters[CP_F_OPEN_TIMESTAMP] -
575 576
                  ((file->fcounters[CP_F_MPI_META_TIME] + file->fcounters[CP_F_MPI_READ_TIME] +
                  file->fcounters[CP_F_MPI_WRITE_TIME]) / nprocs);
577

578 579
    if (file->counters[CP_COLL_OPENS] || file->counters[CP_INDEP_OPENS])
    {
580 581
        extra_opens = file->counters[CP_POSIX_OPENS] - file->counters[CP_COLL_OPENS] -
                      file->counters[CP_INDEP_OPENS];
582

583 584
        total_coll_opens = file->counters[CP_COLL_OPENS];
        total_ind_opens = file->counters[CP_POSIX_OPENS] - total_coll_opens - extra_opens;
585

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

589 590
        if (file->counters[CP_COLL_OPENS])
        {
591 592 593 594 595 596 597 598 599 600 601
            int tmp_ind_io_cycles;
            if (total_ind_io_ops < total_coll_io_ops)
            {
                tmp_ind_io_cycles = total_ind_io_ops;
            }
            else
            {
                tmp_ind_io_cycles = ceil(((double)total_ind_io_ops / total_coll_io_ops) / nprocs) *
                                    total_coll_io_ops;
            }

602
            open_cycles = total_coll_opens / nprocs;
603
            calc_io_delays(file, ceil(((double)(total_coll_opens + total_ind_opens)) / nprocs),
604
                           total_coll_io_ops + tmp_ind_io_cycles, total_delay,
605 606
                           &first_io_delay, &close_delay, &inter_cycle_delay, &inter_io_delay);
        }
607
        else
608 609 610 611 612
        {
            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);
        }
613 614 615 616 617 618 619 620 621 622 623
    }
    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;
624 625 626 627 628
            if (extra_io_ops != extra_opens)
            {
                extra_opens = 0;
                extra_io_ops = 0;
            }
629 630 631
        }

        total_coll_opens = 0;
632
        total_ind_opens = file->counters[CP_POSIX_OPENS] - extra_opens;
633 634 635

        total_ind_io_ops = total_io_ops - extra_io_ops;
        total_coll_io_ops = 0;
636 637 638 639

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

642
    /* calculate average meta op time (for i/o and opens/closes) */
643 644
    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]);
645

646 647 648 649 650
    /* it is rare to overwrite existing files, so set the create flag */
    if (file->counters[CP_BYTES_WRITTEN])
    {
        create_flag = 1;
    }
651

652 653
    /* generate all events for this collectively opened file */
    for (i = 0; i < open_cycles; i++)
654
    {
655 656
        ind_opens_this_cycle = ceil((double)total_ind_opens / (open_cycles - i));
        coll_opens_this_cycle = total_coll_opens / (open_cycles - i);
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
        /* assign any extra opens to as many ranks as possible if we are generating events
         * for a file that was only opened independently (these likely correspond to file
         * creations)
         */
        if(extra_opens && !coll_opens_this_cycle)
        {
            if (extra_opens >= nprocs)
                rank_cnt = nprocs;
            else
                rank_cnt = extra_opens;

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

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

            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank < rank_cnt));
            file->counters[CP_POSIX_OPENS] -= rank_cnt;
        }
684 685 686
        /* assign any extra opens to rank 0 (these may correspond to file creations or
         * header reads/writes)
         */
687
        else if (extra_opens && !(i % (open_cycles / extra_opens)))
688
        {
689 690
            cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                               io_context, (io_context->my_rank == 0));
691 692
            create_flag = 0;

693 694 695
            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,
696
                                                       meta_op_time, cur_time, io_context);
697 698
                extra_io_ops--;
            }
699

700 701
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank == 0));
702 703 704
            file->counters[CP_POSIX_OPENS]--;
        }

705
        while (ind_opens_this_cycle)
706
        {
707 708 709 710 711
            if (ind_opens_this_cycle >= nprocs)
                rank_cnt = nprocs;
            else
                rank_cnt = ind_opens_this_cycle;

712 713
            cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                               io_context, (io_context->my_rank < rank_cnt));
714 715
            create_flag = 0;

716 717 718 719
            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,
720 721
                                                   nprocs, inter_io_delay, meta_op_time,
                                                   cur_time, io_context);
722 723 724
            total_ind_io_ops -= ind_io_ops_this_cycle;

            cur_time += close_delay;
725

726 727
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank < rank_cnt));
728

729 730 731 732 733 734
            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;
735 736
        }

737
        while (coll_opens_this_cycle)
738
        {
739
            assert(!create_flag);
740

741
            cur_time = generate_barrier_event(file, 0, cur_time, io_context);
742

743
            cur_time = generate_psx_open_event(file, create_flag, meta_op_time,
744
                                               cur_time, io_context, 1);
745

746
            cur_time += first_io_delay;
747 748 749 750 751 752 753 754 755 756 757

            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,
758 759
                                                   inter_io_delay, meta_op_time, cur_time,
                                                   io_context);
760 761 762
            total_ind_io_ops -= ind_io_ops_this_cycle;
            total_coll_io_ops -= coll_io_ops_this_cycle;

763
            cur_time += close_delay;
764

765
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context, 1);
766 767

            file->counters[CP_POSIX_OPENS] -= nprocs;
768 769
            file->counters[CP_COLL_OPENS] -= nprocs;
            coll_opens_this_cycle -= nprocs;
770 771 772 773
            total_coll_opens -= nprocs;

            if (file->counters[CP_POSIX_OPENS])
                cur_time += inter_cycle_delay;
774 775
        }
    }
776 777 778 779 780 781

    return;
}

/* fill in an open event structure and store it with the rank context */
static double generate_psx_open_event(
782
    struct darshan_file *file, int create_flag, double meta_op_time,
783
    double cur_time, struct rank_io_context *io_context, int insert_flag)
784
{
785 786 787 788 789 790 791
    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
    };
792 793 794

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

797
    /* store the open event (if this rank performed it) */
798
    if (insert_flag)
799
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
800 801 802 803 804 805

    return cur_time;
}

/* fill in a close event structure and store it with the rank context */
static double generate_psx_close_event(
806
    struct darshan_file *file, double meta_op_time, double cur_time,
807
    struct rank_io_context *io_context, int insert_flag)
808
{
809 810 811 812 813 814
    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
    };
815 816 817

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

820
    /* store the close event (if this rank performed it) */
821
    if (insert_flag)
822
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
823

824 825
    io_context->next_off = 0;

826 827 828 829 830
    return cur_time;
}

/* fill in a barrier event structure and store it with the rank context */
static double generate_barrier_event(
831
    struct darshan_file *file, int64_t root, double cur_time, struct rank_io_context *io_context)
832
{
833 834 835
    struct darshan_io_op next_io_op =
    {
        .codes_op.op_type = CODES_WK_BARRIER, 
836
        .codes_op.u.barrier.count = 8192, /* all processes */
837 838 839
        .codes_op.u.barrier.root = root,
        .start_time = cur_time
    };
840 841

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

844
    /* store the barrier event */
845
    if (file->rank == -1)
846
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
847 848 849 850 851 852

    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(
853
    struct darshan_file *file, int64_t io_ops_this_cycle, double inter_io_delay,
854
    double cur_time, struct rank_io_context *io_context)
855 856 857 858 859 860 861 862 863
{
    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;
864
    struct darshan_io_op next_io_op;
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

    /* 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 */
901
        determine_ind_io_params(file, rw, &io_sz, &io_off, io_context);
902 903 904
        if (!rw)
        {
            /* generate a read event */
905 906 907 908 909
            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;
910 911 912 913 914

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

            /* update time, accounting for metadata time */
918
            cur_time += io_op_time;
919
            next_io_op.end_time = cur_time;
920 921 922 923 924
            file->counters[CP_POSIX_READS]--;
        }
        else
        {
            /* generate a write event */
925 926 927 928 929
            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;
930 931 932 933 934

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

            /* update time, accounting for metadata time */
938
            cur_time += io_op_time;
939
            next_io_op.end_time = cur_time;
940 941 942 943 944 945 946
            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);

947 948
        /* store the i/o event */
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968

        /* 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 */
969
            cur_time += inter_io_delay;
970 971 972 973 974 975 976 977 978 979 980 981
        }
    }

    /* 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;
}

982 983
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,
984 985
    int64_t nprocs, int64_t aggregator_cnt, double inter_io_delay, double meta_op_time,
    double cur_time, struct rank_io_context *io_context)
986 987 988 989 990 991 992 993 994
{
    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 tmp_rank;
    int64_t next_ind_io_rank = 0;
    int64_t io_cnt;
995
    double ranks_per_aggregator = (double)(nprocs - 1) / (aggregator_cnt - 1);
996
    int64_t ind_ops_remaining = 0;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    double io_op_time;
    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;

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
        /* 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);
        }

1040 1041 1042 1043 1044 1045 1046
        /* 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];
    }

1047 1048 1049 1050 1051
    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;

1052 1053
    for (i = 0; i < total_io_ops_this_cycle; i++)
    {
1054
        if (ind_ops_remaining)
1055 1056
        {
            tmp_rank = (next_ind_io_rank++) % nprocs;
1057
            ind_io_ops_this_cycle--;
1058 1059

            determine_ind_io_params(file, rw, &io_sz, &io_off, io_context);
1060
            if (!rw)
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
            {
                /* 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;

                /* 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);
                
                file->counters[CP_POSIX_READS]--;
1075
                file->counters[CP_INDEP_READS]--;
1076
            }
1077
            else
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
            {
                /* 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;

                /* 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);

                file->counters[CP_POSIX_WRITES]--;
1092
                file->counters[CP_INDEP_WRITES]--;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
            }
            next_io_op.start_time = cur_time;
            next_io_op.end_time = cur_time + io_op_time + meta_op_time;

            psx_rw_ops_remaining--;
            assert(file->counters[CP_POSIX_READS] >= 0);
            assert(file->counters[CP_POSIX_WRITES] >= 0);

            /* store the io operation if it belongs to this rank */
            if (tmp_rank == io_context->my_rank)
            {
1104 1105
                darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
                cur_time = next_io_op.end_time;
1106 1107
                if (coll_io_ops_this_cycle || (ind_ops_remaining > nprocs))
                    cur_time += inter_io_delay;
1108
            }
1109 1110 1111

            ind_ops_remaining--;
            if (!ind_ops_remaining) next_ind_io_rank = 0;
1112 1113 1114 1115
        }
        else
        {
            tmp_rank = 0;
1116
            coll_io_ops_this_cycle--;
1117

1118 1119 1120
            if (!rw)
            {
                io_cnt = ceil((double)(file->counters[CP_POSIX_READS] -
1121
                              file->counters[CP_INDEP_READS]) / 
1122 1123 1124 1125 1126 1127
                              (file->counters[CP_COLL_READS] / nprocs));
                file->counters[CP_COLL_READS] -= nprocs;
            }
            else
            {
                io_cnt = ceil((double)(file->counters[CP_POSIX_WRITES] -
1128
                              file->counters[CP_INDEP_WRITES]) / 
1129 1130 1131
                              (file->counters[CP_COLL_WRITES] / nprocs));
                file->counters[CP_COLL_WRITES] -= nprocs;
            }
1132 1133 1134 1135 1136 1137

            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;

1138 1139 1140
            for (j = 0; j < io_cnt; j += aggregator_cnt)
            {
                int64_t tmp_coll_cnt = MIN(io_cnt - j, aggregator_cnt);
1141
                int64_t tmp_agg_ndx;
1142 1143 1144

                cur_time = generate_barrier_event(file, 0, cur_time, io_context);

1145 1146 1147
                tmp_agg_ndx = (int64_t)round(io_context->my_rank / ranks_per_aggregator);
                if ((round(tmp_agg_ndx * ranks_per_aggregator) == io_context->my_rank) &&
                    (tmp_agg_ndx < tmp_coll_cnt))
1148
                {
1149
                    determine_coll_io_params(file, rw, io_cnt, tmp_coll_cnt, tmp_agg_ndx + 1, 
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
                                             &io_sz, &io_off, io_context);
                    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;

                        /* 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);
                        
                        file->counters[CP_POSIX_READS] -= tmp_coll_cnt;
                    }
                    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;

                        /* 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);

                        file->counters[CP_POSIX_WRITES] -= tmp_coll_cnt;
                    }
1183 1184
                    next_io_op.start_time = cur_time;
                    next_io_op.end_time = cur_time + io_op_time + meta_op_time;
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203

                    darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);

                    cur_time = next_io_op.end_time;
                }
                else
                {
                    if (!rw)
                    {
                        file->counters[CP_POSIX_READS] -= tmp_coll_cnt;
                    }
                    else
                    {
                        file->counters[CP_POSIX_WRITES] -= tmp_coll_cnt;
                    }
                }
                psx_rw_ops_remaining -= tmp_coll_cnt;
                assert(file->counters[CP_POSIX_READS] >= 0);
                assert(file->counters[CP_POSIX_WRITES] >= 0);
1204
            }
1205

1206 1207
            if (i != (total_io_ops_this_cycle - 1))
                cur_time += inter_io_delay;
1208
        }
1209
        io_ops_this_rw--;
1210

1211

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
        /* 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;
    }

1251
    return cur_time;
1252 1253
}

1254 1255 1256 1257 1258 1259 1260 1261
static void determine_coll_io_params(
    struct darshan_file *file, int write_flag, int64_t coll_op_cnt, int64_t agg_cnt,
    int64_t agg_ndx, size_t *io_sz, off_t *io_off, struct rank_io_context *io_context)
{
    static int64_t size_bins_left = 0;
    static int64_t agg_size_bins[10] = { 0 };
    static off_t agg_off = 0;
    int i, j;
1262
    int start_ndx, end_ndx;
1263 1264 1265 1266 1267
    int64_t *all_size_bins = NULL;
    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 */
    int64_t *total_io_size = NULL;
    int64_t tmp_cnt;
1268 1269 1270
    int64_t switch_cnt;
    int64_t leftover;
    int64_t bins_to_use;
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 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    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 };
    
    if (write_flag)
    {
        all_size_bins = &(file->counters[CP_SIZE_WRITE_0_100]);
        total_io_size = &(file->counters[CP_BYTES_WRITTEN]);
    }
    else
    {
        all_size_bins = &(file->counters[CP_SIZE_READ_0_100]);
        total_io_size = &(file->counters[CP_BYTES_READ]);
    }

    /* we enter this if statement if we have not yet calculated which size bins to use for the
     * current collective I/O operation.
     */
    if (!size_bins_left)
    {
        int64_t total_agg_size_bin_cnt = 0;
        int tmp_ndx = 9;

        /* find some size bins that we can assign accesses out of.
         * Note: we require the bins be reasonably close to each other in size, and be less
         * than 100 MiB.
         */
        for (i = 7; i >= 0; i--)
        {
            if (all_size_bins[i])
            {
                if (total_agg_size_bin_cnt)
                {
                    if ((tmp_ndx - 3) <= i)
                    {
                        tmp_ndx = i;
                        total_agg_size_bin_cnt += all_size_bins[i];
                    }
                    break;
                }
                else
                {
                    tmp_ndx = i;
                    total_agg_size_bin_cnt += all_size_bins[i];
                }
            }
        }

        /* assign accesses from found bins proportional to bin size */
        for (i = 7; i >= tmp_ndx; i--)
        {
            if (all_size_bins[i])
            {
1327 1328
                agg_size_bins[i] = ((double)all_size_bins[i] / total_agg_size_bin_cnt) *
                                   MIN(total_agg_size_bin_cnt, coll_op_cnt);
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
                size_bins_left += agg_size_bins[i];
                all_size_bins[i] -= agg_size_bins[i];
            }

            if (size_bins_left == coll_op_cnt) break;
        }

        /* if we still haven't assigned enough accesses, just assign them stupidly */
        if (size_bins_left < coll_op_cnt)
        {
            for (i = 9; i >= 0; i--)
            {
                if (all_size_bins[i])
                {
                    tmp_cnt = MIN(all_size_bins[i], coll_op_cnt - size_bins_left);
                    agg_size_bins[i] += tmp_cnt;
                    size_bins_left += tmp_cnt;
                    all_size_bins[i] -= tmp_cnt;
                }

                if (size_bins_left == coll_op_cnt) break;
            }
        }
        assert(size_bins_left == coll_op_cnt);

        ssize_t tmp_size;
1355 1356
        int64_t tmp_agg_ndx = 1; /* start at aggregator 1 */
        int64_t tmp_agg_cnt;
1357
        int64_t tmp_common_cnts[4];
1358
        int64_t tmp_size_bins[10];
1359
        memcpy(tmp_common_cnts, common_access_counts, 4 * sizeof(int64_t));
1360 1361
        memcpy(tmp_size_bins, agg_size_bins, 10 * sizeof(int64_t));
        tmp_cnt = coll_op_cnt;
1362
        agg_off = io_context->next_off;
1363

1364
        while (tmp_cnt)
1365
        {
1366 1367 1368 1369 1370 1371
            start_ndx = -1;
            end_ndx = -1;
            leftover = 0;
            tmp_agg_cnt = MIN(tmp_cnt, agg_cnt);
        
            for (i = 9; i >= 0; i--)
1372
            {
1373
                if (tmp_size_bins[i])
1374
                {
1375 1376 1377 1378
                    if (start_ndx == -1) start_ndx = i;

                    tmp_agg_cnt -= tmp_size_bins[i];
                    if (tmp_agg_cnt <= 0)
1379
                    {
1380
                        end_ndx = i;
1381 1382 1383
                        break;
                    }
                }
1384
            }
1385

1386 1387 1388 1389 1390
            i = start_ndx;
            tmp_agg_cnt = MIN(tmp_cnt, agg_cnt);
            while (tmp_agg_cnt)
            {
                if ((tmp_size_bins[i] >= tmp_agg_cnt) && !leftover)
1391
                {
1392
                    switch_cnt = tmp_agg_cnt;
1393
                }
1394 1395 1396 1397
                else if (tmp_size_bins[i])
                {
                    bins_to_use = MIN(tmp_size_bins[i], tmp_agg_cnt - leftover);
                    switch_cnt = ceil((double)bins_to_use / (tmp_agg_cnt - bins_to_use));
1398

1399 1400 1401
                    leftover += (bins_to_use - switch_cnt);
                }
                else
1402
                {
1403
                    switch_cnt = 0;
1404 1405
                }

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
                /* assign i/o operations until we need to move to the next size bin */
                while (switch_cnt)
                {
                    /* assign access sizes, starting with common and falling back to default */
                    tmp_size = -1;
                    for (j = 0; j < 4; j++)
                    {
                        if (tmp_common_cnts[j] &&
                            (common_accesses[j] > size_bin_min_vals[i]) &&
                            (common_accesses[j] <= size_bin_max_vals[i]))
                        {
                            tmp_size = common_accesses[j];
                            tmp_common_cnts[j]--;
                            break;
                        }
                    }

                    if (tmp_size == -1)
                    {
                        tmp_size = ALIGN_BY_8((size_bin_max_vals[i] - size_bin_min_vals[i]) / 2);
                    }

                    /* only increment offset for aggregators less than me */
                    if (tmp_agg_ndx < agg_ndx)
                    {
                        agg_off += tmp_size;
                    }
1433
                    io_context->next_off += tmp_size;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

                    tmp_cnt--;
                    tmp_agg_cnt--;
                    switch_cnt--;
                    tmp_size_bins[i]--;
                    tmp_agg_ndx++;
                    if (tmp_agg_ndx > agg_cnt) tmp_agg_ndx = 1;
                }

                i--;
                if (i < end_ndx)
                {
                    i = start_ndx;
                    leftover = 0;
                }
1449 1450 1451 1452 1453 1454 1455 1456
            }
        }
    }

    /* assign an actual access size, according to already initialized agg size bins */
    *io_sz = 0;
    if (*total_io_size > 0)
    {
1457 1458 1459 1460 1461 1462
        tmp_cnt = agg_cnt;
        start_ndx = -1;
        end_ndx = -1;
        leftover = 0;
        int my_ndx = -1;

1463 1464 1465 1466
        for (i = 9; i >= 0; i--)
        {
            if (agg_size_bins[i])
            {
1467
                if (start_ndx == -1) start_ndx = i;
1468

1469 1470
                tmp_cnt -= agg_size_bins[i];
                if (tmp_cnt <= 0)
1471
                {
1472 1473 1474 1475 1476
                    end_ndx = i;
                    break;
                }
            }
        }
1477

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
        i = start_ndx;
        tmp_cnt = agg_cnt;
        while (tmp_cnt)
        {
            if ((agg_size_bins[i] >= tmp_cnt) && !leftover)
            {
                switch_cnt = tmp_cnt;
            }
            else if (agg_size_bins[i])
            {
                bins_to_use = MIN(agg_size_bins[i], tmp_cnt - leftover);
                switch_cnt = ceil((double)bins_to_use / (tmp_cnt - bins_to_use));
1490

1491 1492 1493 1494 1495 1496
                leftover += (bins_to_use - switch_cnt);
            }
            else
            {
                switch_cnt = 0;
            }
1497

1498 1499 1500
            if (switch_cnt)
            {
                if (my_ndx == -1)
1501
                {
1502 1503 1504 1505
                    if (agg_ndx <= switch_cnt)
                        my_ndx = i;
                    else
                        agg_ndx -= switch_cnt;
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 1531 1532
                agg_size_bins[i] -= switch_cnt;
                if (tmp_cnt > switch_cnt)
                    tmp_cnt -= switch_cnt;
                else
                    tmp_cnt = 0;
            }

            i--;
            if (i < end_ndx)
            {
                i = start_ndx;
                leftover = 0;
            }
        }
        assert((my_ndx >= 0) && (my_ndx < 10));

        /* first try a common access size */
        for (j = 0; j < 4; j++)
        {
            if (common_access_counts[j] &&
                (common_accesses[j] > size_bin_min_vals[my_ndx]) &&
                (common_accesses[j] <= size_bin_max_vals[my_ndx])) 
            {
                *io_sz = common_accesses[j];
                common_access_counts[j]--;
1533 1534 1535
                break;
            }
        }
1536 1537 1538 1539 1540 1541 1542 1543

        /* if no common access size found, assign a default size */
        if (j == 4)
        {
            /* default size is the median of the range, aligned to be multiple of 8 */
            size_t gen_size = (size_bin_max_vals[my_ndx] - size_bin_min_vals[my_ndx]) / 2;
            *io_sz = ALIGN_BY_8(gen_size);
        }
1544 1545 1546
    }
    *total_io_size -= *io_sz;

1547 1548 1549 1550 1551
    /* decrement size bins counter */
    size_bins_left -= agg_cnt;
    if (size_bins_left < agg_ndx)
        size_bins_left = 0;

1552 1553 1554 1555 1556 1557 1558
    /* we simply assign offsets sequentially through an aggregator's file view */
    *io_off = agg_off;
    agg_off += *io_sz;

    return;
}

1559 1560 1561
static void determine_ind_io_params(
    struct darshan_file *file, int write_flag, size_t *io_sz, off_t *io_off,
    struct rank_io_context *io_context)
1562
{
1563
    int size_bin_ndx = 0;
1564 1565
    int64_t *rd_size_bins = &(file->counters[CP_SIZE_READ_0_100]);
    int64_t *wr_size_bins = &(file->counters[CP_SIZE_WRITE_0_100]);
1566
    int64_t *size_bins = NULL;
1567 1568
    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 */
1569
    int64_t *total_io_size = NULL;
1570
    int i, j = 0;
1571 1572 1573 1574 1575 1576
    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 };
1577 1578 1579 1580 1581

    /* assign data values depending on whether the operation is a read or write */
    if (write_flag)
    {
        total_io_size = &(file->counters[CP_BYTES_WRITTEN]);
1582
        size_bins = wr_size_bins;
1583 1584 1585 1586
    }
    else
    {
        total_io_size = &(file->counters[CP_BYTES_READ]);
1587
        size_bins = rd_size_bins;
1588 1589
    }

1590
    for (i = 0; i < 10; i++)
1591
    {
1592
        if (size_bins[i])
1593
        {
1594 1595
            size_bin_ndx = i;
            break;
1596 1597
        }
    }
1598 1599 1600

    *io_sz = 0;
    if (*total_io_size > 0)
1601
    {
1602 1603
        /* try to assign a common access first (intelligently) */
        for (j = 0; j < 4; j++)
1604
        {
1605 1606