codes-darshan-io-wrkld.c 15.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

/* Darshan workload generator that plugs into the general CODES workload
 * generator API. This generator consumes an input file of Darshan I/O
 * events and passes these events to the underlying simulator.
 */
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>

#include "codes/codes-workload.h"
#include "codes-workload-method.h"
#include "darshan-io-events.h"
18
#include "codes/quickhash.h"
19
20
21
22

#define DARSHAN_MAX_EVENT_READ_COUNT 100
#define DARSHAN_NEGLIGIBLE_DELAY .001

23
24
#define RANK_HASH_TABLE_SIZE 397

25
/* structure for storing all context needed to retrieve events for this rank */
26
27
28
29
30
31
/* TODO: if many ross LPs are mapped to this PE, then memory usage is going to increase
 *       linearly, obviously due to the static events arrays used here to store darshan events.
 *       This really needs to be fixed by coming up with a more intelligent way of representing
 *       this workload (i.e. no intermediary format). Until then, you can lower the 
 *       DARSHAN_MAX_EVENT_READ_COUNT to as low as 1 to save memory but restrict performance.
 */
32
33
34
35
36
37
38
39
40
41
42
43
44
45
struct rank_events_context
{
    int rank;
    int ind_file_fd;
    int64_t ind_event_cnt;
    int coll_file_fd;
    int64_t coll_event_cnt;
    struct darshan_event ind_events[DARSHAN_MAX_EVENT_READ_COUNT];
    struct darshan_event coll_events[DARSHAN_MAX_EVENT_READ_COUNT];
    int ind_list_ndx;
    int ind_list_max;
    int coll_list_ndx;
    int coll_list_max;
    double last_event_time;
46
    struct qhash_head hash_link;
47
48
49
50
51
52
53
54
55
56
};

/* CODES workload API functions for workloads generated from darshan logs*/
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);
static void *darshan_io_workload_get_info(int rank);

/* helper functions for darshan workload CODES API */
static void darshan_read_events(struct rank_events_context *rank_context, int ind_flag);
static struct codes_workload_op darshan_event_to_codes_workload_op(struct darshan_event event);
57
static int hash_rank_compare(void *key, struct qhash_head *link);
58
59
60
61
62
63
64
65
66
67

/* 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,
    .codes_workload_get_info= darshan_io_workload_get_info,
};

68
69
static struct qhash_table *rank_tbl = NULL;
static int rank_tbl_pop = 0;
70
71

/* info about this darshan workload group needed by bgp model */
72
73
/* TODO: is this needed for darshan workloads? */
/* TODO: does this need to be stored in the rank context to support multiple workloads? */
74
75
static struct codes_workload_info darshan_workload_info = {-1, -1, -1, -1, -1};

76
77
78
/* load the workload generator for this rank, given input params */
static int darshan_io_workload_load(const char *params, int rank)
{
79
    const char *event_file = params; /* for now, params is just the input trace file name */
80
81
82
83
84
85
86
87
    int event_file_fd;
    int64_t nprocs;
    ssize_t bytes_read;
    int64_t *rank_offsets = NULL;
    int64_t off_beg = 0;
    int64_t off_end = 0;
    int64_t i;
    int ret;
88
    struct stat stat_buf;
89
90
    struct rank_events_context *new = NULL;

91
92
93
    if (!event_file)
        return -1;

94
95
96
97
98
99
100
101
102
103
104
    /*  allocate a new event context for this rank */
    new = malloc(sizeof(*new));
    if (!new)
        return -1;

    new->rank = rank;
    new->last_event_time = 0.0;

    /* open the input events file */
    event_file_fd = open(event_file, O_RDONLY);
    if (event_file_fd < 0)
105
106
    {
        free(new);
107
        return -1;
108
    }
109
110
111
112

    /* read the event file header to determine the number of original ranks */
    bytes_read = read(event_file_fd, &nprocs, sizeof(int64_t));
    if (bytes_read != sizeof(int64_t))
113
114
115
    {
        free(new);
        close(event_file_fd);
116
        return -1;
117
    }
118
119
120

    rank_offsets = malloc((nprocs + 1) * sizeof(int64_t));
    if (!rank_offsets)
121
122
123
    {
        free(new);
        close(event_file_fd);
124
        return -1;
125
    }
126
127
128
129

    /* read the event list offset for all ranks from the event file header */
    bytes_read = read(event_file_fd, rank_offsets, (nprocs + 1) * sizeof(int64_t));
    if (bytes_read != ((nprocs + 1) * sizeof(int64_t)))
130
131
132
133
    {
        free(new);
        free(rank_offsets);
        close(event_file_fd);
134
        return -1;
135
    }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

    /* determine the beginning and ending offsets for this rank's independent events */
    off_beg = rank_offsets[rank];
    if (off_beg)
    {
        /* set ending offset to be the next nonzero offset in the rank_offsets list */
        for (i = rank + 1; i < nprocs + 1; i++)
        {
            if (rank_offsets[i])
            {
                off_end = rank_offsets[i];
                break;
            }
        }

        /* if no ending is found in rank_offsets, set ending offset to be the end of file */
        if (i == (nprocs + 1))
        {
            ret = fstat(event_file_fd, &stat_buf);
            if (!ret)
                off_end = stat_buf.st_size;
            else
158
159
160
161
            {
                free(new);
                free(rank_offsets);
                close(event_file_fd);
162
                return -1;
163
            }
164
165
166
167
168
169
170
171
172
173
        }

        /* set the independent event count */
        assert(((off_end - off_beg) % sizeof(struct darshan_event)) == 0);
        new->ind_event_cnt = (off_end - off_beg) / sizeof(struct darshan_event);

        /* set independent file descriptor to the open events file and seek to this rank's offset */
        new->ind_file_fd = event_file_fd;
        ret = lseek(new->ind_file_fd, (off_t)off_beg, SEEK_SET);
        if (ret < 0)
174
175
176
177
        {
            free(new);
            free(rank_offsets);
            close(event_file_fd);
178
            return -1;
179
        }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    }
    else
    {
        new->ind_event_cnt = 0;
        new->ind_file_fd = -1;
    }

    /* determine the beginning and ending offsets for the collective events */
    off_beg = rank_offsets[nprocs];
    if (off_beg)
    {
        /* use fstat to find the ending offset of the file */
        ret = fstat(event_file_fd, &stat_buf);
        if (ret < 0)
194
195
196
197
        {
            free(new);
            free(rank_offsets);
            close(event_file_fd);
198
            return -1;
199
        }
200
201
202
203
204
205
206
207
208
209
210
        else
            off_end = (int64_t)stat_buf.st_size;

        /* set the collective event count */
        assert(((off_end - off_beg) % sizeof(struct darshan_event)) == 0);
        new->coll_event_cnt = (off_end - off_beg) / sizeof(struct darshan_event);

        /* open a new file descriptor for collective events, and seek to the beginning offset */
        new->coll_file_fd = open(event_file, O_RDONLY);
        ret = lseek(new->coll_file_fd, (off_t)off_beg, SEEK_SET);
        if (ret < 0)
211
212
213
214
        {
            free(new);
            free(rank_offsets);
            close(event_file_fd);
215
            return -1;
216
        }
217
218
219
220
221
222
223
224
225
226
227
228
229
    }
    else
    {
        new->coll_event_cnt = 0;
        new->coll_file_fd = -1;
    }

    /* initialize list variables to zero -- events are only read in the get_next function */
    new->ind_list_ndx = 0;
    new->ind_list_max = 0;
    new->coll_list_ndx = 0;
    new->coll_list_max = 0;

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    /* initialize the hash table of rank contexts, if it has not been initialized */
    if (!rank_tbl)
    {
        rank_tbl = qhash_init(hash_rank_compare, quickhash_32bit_hash, RANK_HASH_TABLE_SIZE);
        if (!rank_tbl)
        {
            free(new);
            free(rank_offsets);
            close(event_file_fd);
            close(new->coll_file_fd);
            return -1;
        }
    }

    /* add this rank context to the hash table */
    qhash_add(rank_tbl, &(new->rank), &(new->hash_link));
    rank_tbl_pop++;
247

248
249
250
251
252
253
254
255
256
    /* fill out the info required for this workload group */
    if (darshan_workload_info.group_id == -1)
    {
        darshan_workload_info.group_id = 1;
        darshan_workload_info.min_rank = 0;
        darshan_workload_info.max_rank = nprocs - 1;
        darshan_workload_info.num_lrank = nprocs;
    }

257
258
259
260
261
262
263
264
265
    free(rank_offsets);
    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)
{
    struct darshan_event next_event;
    int ind_event_flag;
266
267
    struct qhash_head *hash_link = NULL;
    struct rank_events_context *tmp = NULL;
268

269
270
    /* find event context for this rank in the rank hash table */
    hash_link = qhash_search(rank_tbl, &rank);
271

272
273
    /* terminate the workload if there is no valid rank context */
    if (!hash_link)
274
275
276
277
278
    {
        op->op_type = CODES_WK_END;
        return;
    }

279
    tmp = qhash_entry(hash_link, struct rank_events_context, hash_link);
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    assert(tmp->rank == rank);

    /* read in more independent events if necessary */
    if ((tmp->ind_list_ndx == tmp->ind_list_max) && tmp->ind_event_cnt)
    {
        darshan_read_events(tmp, 1);
    }

    /* read in more collective events if necessary */
    while (1)
    {
        if ((tmp->coll_list_ndx == tmp->coll_list_max) && tmp->coll_event_cnt)
        {
            darshan_read_events(tmp, 0);
        }
        else if ((tmp->coll_list_ndx == tmp->coll_list_max) && !tmp->coll_event_cnt)
        {
            break;
        }

        /* search through collective events until one for this rank is found */
        if (tmp->coll_list_ndx < tmp->coll_list_max)
        {
            if ((tmp->coll_events[tmp->coll_list_ndx].rank == tmp->rank) ||
                (tmp->coll_events[tmp->coll_list_ndx].rank == -1))
            {
                break;
            }
            else
            {
                tmp->coll_list_ndx++;
            }
        }
    }

    if ((tmp->ind_list_ndx == tmp->ind_list_max) && (tmp->coll_list_ndx == tmp->coll_list_max))
    {
        /* no more events -- just end the workload */
        op->op_type = CODES_WK_END;
319
320
321
322
323
        qhash_del(hash_link);
        free(tmp);
        rank_tbl_pop--;
        if (!rank_tbl_pop)
            qhash_finalize(rank_tbl);
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
    }
    else
    {
        /* determine whether the next event is from the independent or collective list */
        if (tmp->coll_list_ndx == tmp->coll_list_max)
        {
            next_event = tmp->ind_events[tmp->ind_list_ndx];
            ind_event_flag = 1;
        }
        else if (tmp->ind_list_ndx == tmp->ind_list_max)
        {
            next_event = tmp->coll_events[tmp->coll_list_ndx];
            ind_event_flag = 0;
        }
        else
        {
            /* both lists have events -- choose the event with the lowest timestamp */
            if (tmp->ind_events[tmp->ind_list_ndx].start_time <
                tmp->coll_events[tmp->coll_list_ndx].start_time)
            {
                next_event = tmp->ind_events[tmp->ind_list_ndx];
                ind_event_flag = 1;
            }
            else
            {
                next_event = tmp->coll_events[tmp->coll_list_ndx];
                ind_event_flag = 0;
            }
        }

        if ((next_event.start_time - tmp->last_event_time) < DARSHAN_NEGLIGIBLE_DELAY)
        {
            /* return the next event -- there is no delay */
            *op = darshan_event_to_codes_workload_op(next_event);
            tmp->last_event_time = next_event.end_time;
            if (ind_event_flag)
                tmp->ind_list_ndx++;
            else
                tmp->coll_list_ndx++;
        }
        else
        {
            /* there is a non-negligible delay, so pass back a delay event */
            op->op_type = CODES_WK_DELAY;
            op->u.delay.seconds = next_event.start_time - tmp->last_event_time;
            tmp->last_event_time = next_event.start_time;
        }
    }

    return;
}

376
377
/* return the workload info needed by the bgp model */
/* TODO: do we really need this? */
378
379
static void *darshan_io_workload_get_info(int rank)
{
380
    return &(darshan_workload_info);
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
}

/* read events from the event file -- ind_flag is set if reading independent events */
static void darshan_read_events(struct rank_events_context *rank_context, int ind_flag)
{
    int fd;
    int64_t *event_cnt;
    struct darshan_event *event_list;
    int *list_ndx;
    int *list_max;
    ssize_t bytes_read;

    /* set variable values depending on the value of ind_flag */
    if (ind_flag)
    {
        fd = rank_context->ind_file_fd;
        event_cnt = &(rank_context->ind_event_cnt);
        event_list = rank_context->ind_events;
        list_ndx = &(rank_context->ind_list_ndx);
        list_max = &(rank_context->ind_list_max);
    }
    else
    {
        fd = rank_context->coll_file_fd;
        event_cnt = &(rank_context->coll_event_cnt);
        event_list = rank_context->coll_events;
        list_ndx = &(rank_context->coll_list_ndx);
        list_max = &(rank_context->coll_list_max);
    }

    /* try to read maximum number of events, if possible */
    if (*event_cnt >= DARSHAN_MAX_EVENT_READ_COUNT)
    {
        bytes_read = read(fd, event_list, DARSHAN_MAX_EVENT_READ_COUNT *
                          sizeof(struct darshan_event));
        assert(bytes_read == (DARSHAN_MAX_EVENT_READ_COUNT * sizeof(struct darshan_event)));

        /* update event list variables */
        *list_max = DARSHAN_MAX_EVENT_READ_COUNT;
        *list_ndx = 0;
        *event_cnt -= DARSHAN_MAX_EVENT_READ_COUNT;
    }
    else
    {
        bytes_read = read(fd, event_list, *event_cnt * sizeof(struct darshan_event));
        assert(bytes_read == (*event_cnt * sizeof(struct darshan_event)));

        /* update event list variables */
        *list_max = *event_cnt;
        *list_ndx = 0;
        *event_cnt = 0;
    }

    return;
}

/* take a darshan event struct as input and convert it to a codes workload op */
static struct codes_workload_op darshan_event_to_codes_workload_op(struct darshan_event event)
{
    struct codes_workload_op codes_op;

442
    switch (event.type)
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    {
        case POSIX_OPEN:
            codes_op.op_type = CODES_WK_OPEN;
            codes_op.u.open.file_id = event.event_params.open.file;
            codes_op.u.open.create_flag = event.event_params.open.create_flag;
            break;
        case POSIX_CLOSE:
            codes_op.op_type = CODES_WK_CLOSE;
            codes_op.u.close.file_id = event.event_params.close.file;
            break;
        case POSIX_READ:
            codes_op.op_type = CODES_WK_READ;
            codes_op.u.read.file_id = event.event_params.read.file;
            codes_op.u.read.offset = event.event_params.read.offset;
            codes_op.u.read.size = event.event_params.read.size;
            break;
        case POSIX_WRITE:
            codes_op.op_type = CODES_WK_WRITE;
            codes_op.u.write.file_id = event.event_params.write.file;
            codes_op.u.write.offset = event.event_params.write.offset;
            codes_op.u.write.size = event.event_params.write.size;
            break;
        case BARRIER:
            codes_op.op_type = CODES_WK_BARRIER;
            codes_op.u.barrier.count = event.event_params.barrier.proc_count;
            codes_op.u.barrier.root = event.event_params.barrier.root;
            break;
        default:
            assert(0);
            break;
    }

    return codes_op;
}

478
479
480
481
482
483
484
485
486
487
488
489
static int hash_rank_compare(void *key, struct qhash_head *link)
{
    int *in_rank = (int *)key;
    struct rank_events_context *tmp;

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

    return 0;
}

490
491
492
493
494
495
496
497
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */