codes-darshan-io-wrkld.c 58.6 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
#define DEF_INTER_IO_DELAY_PCT 0.4
#define DEF_INTER_CYC_DELAY_PCT 0.2
17

18
#define DARSHAN_NEGLIGIBLE_DELAY 0.001
19
20
21

#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 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
                                          int64_t aggregator_cnt, double inter_io_delay,
78
                                          double cur_time, struct rank_io_context *io_context);
79
80
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);
81
static void calc_io_delays(struct darshan_file *file, int64_t num_opens, int64_t num_io_ops,
82
                           double total_delay, double *first_io_delay, double *close_delay,
83
84
85
                           double *inter_open_delay, double *inter_io_delay);
static void file_sanity_check(struct darshan_file *file, struct darshan_job *job);

86
87
88
89
90
91
/* 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,
92
    .codes_workload_get_rank_cnt = darshan_io_workload_get_rank_cnt,
93
94
};

95
96
static int total_rank_cnt = 0;

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

101
102
103
/* load the workload generator for this rank, given input params */
static int darshan_io_workload_load(const char *params, int rank)
{
104
105
    darshan_params *d_params = (darshan_params *)params;
    darshan_fd logfile_fd;
106
107
    struct darshan_job job;
    struct darshan_file next_file;
108
    struct rank_io_context *my_ctx;
109
    struct darshan_io_op end_op;
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
        else if (next_file.rank < rank)
166
            continue;
167
168
        else
            break;
169
170
171
172

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

177
    darshan_log_close(logfile_fd);
178

179
180
181
182
183
184
    /* tack a workload end file on the end of the events list */
    /* NOTE: this helps us generate a delay from the last close to program termination */
    end_op.codes_op.op_type = CODES_WK_END;
    end_op.start_time = end_op.end_time = job.end_time - job.start_time + 1;
    darshan_insert_next_io_op(my_ctx->io_op_dat, &end_op);

185
186
187
188
189
190
191
192
193
194
195
196
197
    /* 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));
198
    rank_tbl_pop++;
199

200
201
202
203
204
    /* TODO: do we want this long-term */
    if (d_params->stream)
    {
        darshan_print_io_ops(my_ctx->io_op_dat, rank, d_params->stream);
    }
205

206
207
208
209
210
211
    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)
{
212
213
214
215
216
    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;

217
218
    assert(rank < total_rank_cnt);

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    /* 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);
241
242
 
        rank_tbl_pop--;
243
        if (!rank_tbl_pop){
244
            qhash_finalize(rank_tbl);
245
246
            rank_tbl = NULL;
        }
247
248
249
250
251
252
253
254
255
    }
    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;
256
257
258
259

    return;
}

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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;
}

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

375
        if ((tmp->start_time - last_op_time) <= DARSHAN_NEGLIGIBLE_DELAY)
376
377
378
379
380
381
382
383
384
385
386
387
388
389
        {
            /* 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;
        }
    }
390
391
392
393
394
395
396
397
398

    /* 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;
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
}

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

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
444
445
446
447
448
449
450
451
452
453
454
455
456
/* 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)
        {
457
458
            fprintf(log_stream, "Rank %d READ %"PRIu64" [sz = %"PRId64", off = %"PRId64
                    "] (%lf - %lf)\n",
459
460
461
462
463
464
465
466
467
                    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)
        {
468
469
            fprintf(log_stream, "Rank %d WRITE %"PRIu64" [sz = %"PRId64", off = %"PRId64
                    "] (%lf - %lf)\n",
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
                    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;
}

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* 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 */
/*                                       */
/*****************************************/
506
507
508

/* generate events for an independently opened file, and store these events */
static void generate_psx_ind_file_events(
509
    struct darshan_file *file, struct rank_io_context *io_context)
510
511
512
{
    int64_t io_ops_this_cycle;
    double cur_time = file->fcounters[CP_F_OPEN_TIMESTAMP];
513
    double total_delay;
514
515
516
517
518
519
520
521
522
523
524
525
526
    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 */
527
528
529
    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];
530
531
532

    /* calculate synthetic delay values */
    calc_io_delays(file, file->counters[CP_POSIX_OPENS],
533
534
                   file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES], total_delay,
                   &first_io_delay, &close_delay, &inter_open_delay, &inter_io_delay);
535
536
537

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

540
541
542
543
544
    /* set the create flag if the file was written to */
    if (file->counters[CP_BYTES_WRITTEN])
    {
        create_flag = 1;
    }
545
546
547
548
549
550

    /* 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 */
551
552
        cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                           io_context, 1);
553
554
555
556
557
558
559
560
561
562
        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 */
563
        cur_time = generate_psx_ind_io_events(file, io_ops_this_cycle, inter_io_delay,
564
                                              cur_time, io_context);
565
566
567
568
569

        /* 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 */
570
        cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context, 1);
571
572
573
574
575
576
577

        /* account for potential interopen delay if more than one open */
        if (file->counters[CP_POSIX_OPENS] > 1)
        {
            cur_time += inter_open_delay;
        }
    }
578
579
580
581
582
583
584
585
586

    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)
{
587
588
589
590
591
    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;
592
593
    int64_t extra_opens = 0;
    int64_t extra_io_ops = 0;
594
    int64_t total_io_ops = file->counters[CP_POSIX_READS] + file->counters[CP_POSIX_WRITES];
595
596
597
598
    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;
599
    int64_t rank_cnt;
600
601
    int create_flag = 0;
    double cur_time = file->fcounters[CP_F_OPEN_TIMESTAMP];
602
    double total_delay;
603
604
605
606
607
    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;
608
    int64_t i;
609
610
611
612
613
614
615
616
617
618

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

619
620
    if (file->counters[CP_COLL_OPENS] || file->counters[CP_INDEP_OPENS])
    {
621
622
        extra_opens = file->counters[CP_POSIX_OPENS] - file->counters[CP_COLL_OPENS] -
                      file->counters[CP_INDEP_OPENS];
623

624
625
        total_coll_opens = file->counters[CP_COLL_OPENS];
        total_ind_opens = file->counters[CP_POSIX_OPENS] - total_coll_opens - extra_opens;
626
627
628
629

        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;

630
631
632
633
634
635
636
637
        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));

638
            open_cycles = total_coll_opens / nprocs;
639
640
641
642
            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);
        }
643
        else
644
645
646
647
648
649
        {
            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));
650

651
652
653
654
            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);
        }
655
656
657
658
659
660
661
662
663
664
665
    }
    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;
666
667
668
669
670
            if (extra_io_ops != extra_opens)
            {
                extra_opens = 0;
                extra_io_ops = 0;
            }
671
672
673
        }

        total_coll_opens = 0;
674
        total_ind_opens = file->counters[CP_POSIX_OPENS] - extra_opens;
675
676
677

        total_ind_io_ops = total_io_ops - extra_io_ops;
        total_coll_io_ops = 0;
678

679
680
681
682
683
684
        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));

685
686
687
        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);
688
689
690
    }
    assert(extra_opens <= open_cycles);

691
    /* calculate average meta op time (for i/o and opens/closes) */
692
    meta_op_time = file->fcounters[CP_F_POSIX_META_TIME] / (2 * file->counters[CP_POSIX_OPENS]);
693

694
695
696
697
698
    /* it is rare to overwrite existing files, so set the create flag */
    if (file->counters[CP_BYTES_WRITTEN])
    {
        create_flag = 1;
    }
699

700
701
    /* generate all events for this collectively opened file */
    for (i = 0; i < open_cycles; i++)
702
    {
703
704
        ind_opens_this_cycle = ceil((double)total_ind_opens / (open_cycles - i));
        coll_opens_this_cycle = total_coll_opens / (open_cycles - i);
705

706
707
708
        /* assign any extra opens to rank 0 (these may correspond to file creations or
         * header reads/writes)
         */
709
        if (extra_opens && !(i % (open_cycles / extra_opens)))
710
        {
711
712
            cur_time = generate_psx_open_event(file, create_flag, meta_op_time, cur_time,
                                               io_context, (io_context->my_rank == 0));
713
714
            create_flag = 0;

715
716
717
            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,
718
                                                       cur_time, io_context);
719
720
                extra_io_ops--;
            }
721

722
723
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank == 0));
724
725
726
            file->counters[CP_POSIX_OPENS]--;
        }

727
        while (ind_opens_this_cycle)
728
        {
729
730
731
732
733
            if (ind_opens_this_cycle >= nprocs)
                rank_cnt = nprocs;
            else
                rank_cnt = ind_opens_this_cycle;

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

738
739
740
741
            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,
742
                                                   nprocs, inter_io_delay, cur_time, io_context);
743
744
745
            total_ind_io_ops -= ind_io_ops_this_cycle;

            cur_time += close_delay;
746

747
748
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context,
                                                (io_context->my_rank < rank_cnt));
749

750
751
752
753
754
755
            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;
756
757
        }

758
        while (coll_opens_this_cycle)
759
        {
760
            assert(!create_flag);
761

762
            cur_time = generate_barrier_event(file, 0, cur_time, io_context);
763
764

            cur_time = generate_psx_open_event(file, create_flag, meta_op_time,
765
                                               cur_time, io_context, 1);
766

767
768
769
770
771
772
773
774
775
776
777
778
            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,
779
                                                   inter_io_delay, cur_time, io_context);
780
781
782
783
            total_ind_io_ops -= ind_io_ops_this_cycle;
            total_coll_io_ops -= coll_io_ops_this_cycle;

            cur_time += close_delay;
784

785
            cur_time = generate_psx_close_event(file, meta_op_time, cur_time, io_context, 1);
786
787

            file->counters[CP_POSIX_OPENS] -= nprocs;
788
789
            file->counters[CP_COLL_OPENS] -= nprocs;
            coll_opens_this_cycle -= nprocs;
790
791
792
793
            total_coll_opens -= nprocs;

            if (file->counters[CP_POSIX_OPENS])
                cur_time += inter_cycle_delay;
794
795
        }
    }
796
797
798
799
800
801

    return;
}

/* fill in an open event structure and store it with the rank context */
static double generate_psx_open_event(
802
    struct darshan_file *file, int create_flag, double meta_op_time,
803
    double cur_time, struct rank_io_context *io_context, int insert_flag)
804
{
805
806
807
808
809
810
811
    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
    };
812
813
814

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

817
    /* store the open event (if this rank performed it) */
818
    if (insert_flag)
819
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
820
821
822
823
824
825

    return cur_time;
}

/* fill in a close event structure and store it with the rank context */
static double generate_psx_close_event(
826
    struct darshan_file *file, double meta_op_time, double cur_time,
827
    struct rank_io_context *io_context, int insert_flag)
828
{
829
830
831
832
833
834
    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
    };
835
836
837

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

840
    /* store the close event (if this rank performed it) */
841
    if (insert_flag)
842
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
843
844
845
846
847
848

    return cur_time;
}

/* fill in a barrier event structure and store it with the rank context */
static double generate_barrier_event(
849
    struct darshan_file *file, int64_t root, double cur_time, struct rank_io_context *io_context)
850
{
851
852
853
854
855
856
857
    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
    };
858
859

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

862
    /* store the barrier event */
863
    if (file->rank == -1)
864
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
865
866
867
868
869
870

    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(
871
    struct darshan_file *file, int64_t io_ops_this_cycle, double inter_io_delay,
872
    double cur_time, struct rank_io_context *io_context)
873
874
875
876
877
878
879
880
881
{
    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;
882
    struct darshan_io_op next_io_op;
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918

    /* 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 */
919
        determine_io_params(file, rw, 1, 1, &io_sz, &io_off);
920
921
922
        if (!rw)
        {
            /* generate a read event */
923
924
925
926
927
            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;
928
929
930
931
932

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

            /* update time, accounting for metadata time */
936
            cur_time += io_op_time;
937
            next_io_op.end_time = cur_time;
938
939
940
941
942
            file->counters[CP_POSIX_READS]--;
        }
        else
        {
            /* generate a write event */
943
944
945
946
947
            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;
948
949
950
951
952

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

            /* update time, accounting for metadata time */
956
            cur_time += io_op_time;
957
            next_io_op.end_time = cur_time;
958
959
960
961
962
963
964
            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);

965
966
        /* store the i/o event */
        darshan_insert_next_io_op(io_context->io_op_dat, &next_io_op);
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986

        /* 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 */
987
            cur_time += inter_io_delay;
988
989
990
991
992
993
994
995
996
997
998
999
        }
    }

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

1000
static double generate_psx_coll_io_events(
For faster browsing, not all history is shown. View entire blame