codes-workload.c 16.1 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7
#include <assert.h>
8

Jonathan Jenkins's avatar
Jonathan Jenkins committed
9 10 11
#include <ross.h>
#include <codes/codes-workload.h>
#include <codes/codes.h>
12

13
/* list of available methods.  These are statically compiled for now, but we
14 15
 * could make generators optional via autoconf tests etc. if needed
 */
Philip Carns's avatar
Philip Carns committed
16 17 18 19
/* added by pj: differ POSIX and MPI IO in darshan 3.00*/
#define	DARSHAN_POSIX_IO 	1
#define	DARSHAN_MPI_IO		0

20
extern struct codes_workload_method test_workload_method;
21
extern struct codes_workload_method iolang_workload_method;
22 23 24
#ifdef USE_DUMPI
extern struct codes_workload_method dumpi_trace_workload_method;
#endif
Philip Carns's avatar
Philip Carns committed
25

26
#ifdef USE_DARSHAN
Philip Carns's avatar
Philip Carns committed
27 28 29 30 31
#if DARSHAN_POSIX_IO
extern struct codes_workload_method darshan_posix_io_workload_method;
#elif DARSHAN_MPI_IO
extern struct codes_workload_method darshan_mpi_io_workload_method;
#endif
32
#endif
Philip Carns's avatar
Philip Carns committed
33

34 35 36
#ifdef USE_RECORDER
extern struct codes_workload_method recorder_io_workload_method;
#endif
37
extern struct codes_workload_method checkpoint_workload_method;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
38
extern struct codes_workload_method iomock_workload_method;
39

40
static struct codes_workload_method const * method_array_default[] =
41 42
{
    &test_workload_method,
43
    &iolang_workload_method,
44 45 46
#ifdef USE_DUMPI
    &dumpi_trace_workload_method,
#endif
Philip Carns's avatar
Philip Carns committed
47

48
#ifdef USE_DARSHAN
Philip Carns's avatar
Philip Carns committed
49 50 51 52 53 54 55 56
/* added by pj: posix and mpi io */
#if	DARSHAN_POSIX_IO
    &darshan_posix_io_workload_method,
#elif DARNSHAN_MPI_IO
	/* TODO: MPI_IO */
	&darshan_mpi_io_workload_method,
#endif

57 58 59
#endif
#ifdef USE_RECORDER
    &recorder_io_workload_method,
60
#endif
61
    &checkpoint_workload_method,
Jonathan Jenkins's avatar
Jonathan Jenkins committed
62
    &iomock_workload_method,
63 64 65 66 67 68 69
    NULL
};

// once initialized, adding a workload generator is an error
static int is_workloads_init = 0;
static int num_user_methods = 0;
static struct codes_workload_method const ** method_array = NULL;
70

Philip Carns's avatar
Philip Carns committed
71 72 73 74 75
/* This shim layer is responsible for queueing up reversed operations and
 * re-issuing them so that the underlying workload generator method doesn't
 * have to worry about reverse events.
 *
 * NOTE: we could make this faster with a smarter data structure.  For now
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
 * we just have a linked list of rank_queue structs, one per rank that has
 * opened the workload.  We then have a linked list off of each of those
 * to hold a lifo queue of operations that have been reversed for that rank.
 */

/* holds an operation that has been reversed */
struct rc_op
{
    struct codes_workload_op op;
    struct rc_op* next;
};

/* tracks lifo queue of reversed operations for a given rank */
struct rank_queue
{
91
    int app;
92 93 94 95 96 97 98
    int rank;
    struct rc_op *lifo;
    struct rank_queue *next;
};

static struct rank_queue *ranks = NULL;

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
// only call this once
static void init_workload_methods(void)
{
    if (is_workloads_init)
        return;
    if (method_array == NULL)
        method_array = method_array_default;
    else {
        // note - includes null char
        int num_default_methods =
            (sizeof(method_array_default) / sizeof(method_array_default[0]));
        method_array = realloc(method_array,
                (num_default_methods + num_user_methods + 1) *
                sizeof(*method_array));
        memcpy(method_array+num_user_methods, method_array_default,
                num_default_methods * sizeof(*method_array_default));
    }
    is_workloads_init = 1;
}

119 120
codes_workload_config_return codes_workload_read_config(
        ConfigHandle * handle,
121 122 123
        char const * section_name,
        char const * annotation,
        int num_ranks)
124
{
125 126
    init_workload_methods();

127 128 129 130 131
    char type[MAX_NAME_LENGTH_WKLD];
    codes_workload_config_return r;
    r.type = NULL;
    r.params = NULL;

132
    int rc = configuration_get_value(handle, section_name, "workload_type",
133
            annotation, type, MAX_NAME_LENGTH_WKLD);
134 135 136 137 138 139 140 141 142 143
    if (rc <= 0)
        return r;

    for (int i = 0; method_array[i] != NULL; i++){
        struct codes_workload_method const * m = method_array[i];
        if (strcmp(m->method_name, type) == 0) {
            r.type = m->method_name;
            if (m->codes_workload_read_config == NULL)
                r.params = NULL;
            else
144 145
                r.params = m->codes_workload_read_config(handle, section_name,
                        annotation, num_ranks);
146 147 148 149 150 151 152 153 154 155 156 157 158
        }
    }

    return r;
}

void codes_workload_free_config_return(codes_workload_config_return *c)
{
    free(c->params);
    c->type = NULL;
    c->params = NULL;
}

159 160 161 162
int codes_workload_load(
        const char* type,
        const char* params,
        int app_id,
163
        int rank)
164
{
165 166
    init_workload_methods();

167 168
    int i;
    int ret;
169
    struct rank_queue *tmp;
170 171 172 173 174

    for(i=0; method_array[i] != NULL; i++)
    {
        if(strcmp(method_array[i]->method_name, type) == 0)
        {
175
            /* load appropriate workload generator */
176
            ret = method_array[i]->codes_workload_load(params, app_id, rank);
177 178 179 180
            if(ret < 0)
            {
                return(-1);
            }
181 182 183 184 185

            /* are we tracking information for this rank yet? */
            tmp = ranks;
            while(tmp)
            {
186
                if(tmp->rank == rank && tmp->app == app_id)
187 188 189 190 191
                    break;
                tmp = tmp->next;
            }
            if(tmp == NULL)
            {
192
                tmp = (struct rank_queue*)malloc(sizeof(*tmp));
193
                assert(tmp);
194
                tmp->app  = app_id;
195 196 197 198 199 200
                tmp->rank = rank;
                tmp->lifo = NULL;
                tmp->next = ranks;
                ranks = tmp;
            }

201 202 203 204 205
            return(i);
        }
    }

    fprintf(stderr, "Error: failed to find workload generator %s\n", type);
206 207 208
    return(-1);
}

209 210 211 212 213
void codes_workload_get_next(
        int wkld_id,
        int app_id,
        int rank,
        struct codes_workload_op *op)
214
{
215 216 217 218 219 220 221 222 223
    struct rank_queue *tmp;
    struct rc_op *tmp_op;

    /* first look to see if we have a reversed operation that we can
     * re-issue
     */
    tmp = ranks;
    while(tmp)
    {
224
        if(tmp->rank == rank && tmp->app == app_id)
225 226 227
            break;
        tmp = tmp->next;
    }
yangxuserene's avatar
yangxuserene committed
228 229
    if(tmp==NULL)
        printf("tmp is NULL, rank=%d, app_id = %d", rank, app_id);
230 231 232 233
    assert(tmp);
    if(tmp->lifo)
    {
        tmp_op = tmp->lifo;
234
        tmp->lifo = tmp_op->next;
235 236 237 238 239 240 241

        *op = tmp_op->op;
        free(tmp_op);
        return;
    }

    /* ask generator for the next operation */
242
    method_array[wkld_id]->codes_workload_get_next(app_id, rank, op);
243

244
    assert(op->op_type);
245 246 247
    return;
}

248 249 250 251 252
void codes_workload_get_next_rc(
        int wkld_id,
        int app_id,
        int rank,
        const struct codes_workload_op *op)
253
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
254
    (void)wkld_id; // currently unused
255 256 257 258 259 260
    struct rank_queue *tmp;
    struct rc_op *tmp_op;

    tmp = ranks;
    while(tmp)
    {
261
        if(tmp->rank == rank && tmp->app == app_id)
262 263 264 265 266
            break;
        tmp = tmp->next;
    }
    assert(tmp);

267
    tmp_op = (struct rc_op*)malloc(sizeof(*tmp_op));
268 269 270 271
    assert(tmp_op);
    tmp_op->op = *op;
    tmp_op->next = tmp->lifo;
    tmp->lifo = tmp_op;
272

273 274 275
    return;
}

276 277 278 279 280 281 282 283 284
void codes_workload_get_next_rc2(
                int wkld_id,
                int app_id,
                int rank)
{
    assert(method_array[wkld_id]->codes_workload_get_next_rc2);
    method_array[wkld_id]->codes_workload_get_next_rc2(app_id, rank);
}

Philip Carns's avatar
Philip Carns committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
int codes_workload_get_time(const char *type, const char *params, int app_id,
		int rank, double *read_time, double *write_time, int64_t *read_bytes, int64_t *written_bytes)
{
	int i;
	init_workload_methods();

	//printf("entering rank count, method_array = %p \n", method_array);
	for(i=0; method_array[i] != NULL; i++)
	{
		//printf("%p\n", method_array[i]);
		//printf(" geting time:: method_array[%d]->method_name = %s, type = %s\n", i, method_array[i]->method_name, type);
		if(strcmp(method_array[i]->method_name, type) == 0)
		{
			if (method_array[i]->codes_workload_get_time != NULL)
				return method_array[i]->codes_workload_get_time(
						params, app_id, rank, read_time, write_time, read_bytes, written_bytes);
			else
				return -1;
		}
	}
	return 0;
}

308 309 310 311
int codes_workload_get_rank_cnt(
        const char* type,
        const char* params,
        int app_id)
312
{
313
    int i;
Philip Carns's avatar
Philip Carns committed
314
    init_workload_methods();
315

Philip Carns's avatar
Philip Carns committed
316
    //printf("entering rank count, method_array = %p \n", method_array);
317 318
    for(i=0; method_array[i] != NULL; i++)
    {
Philip Carns's avatar
Philip Carns committed
319 320
    	//printf("%p\n", method_array[i]);
    	//printf("method_array[%d]->method_name = %s, type = %s\n", i, method_array[i]->method_name, type);
321 322
        if(strcmp(method_array[i]->method_name, type) == 0)
        {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
323 324 325 326 327
            if (method_array[i]->codes_workload_get_rank_cnt != NULL)
                return method_array[i]->codes_workload_get_rank_cnt(
                        params, app_id);
            else
                return -1;
328 329 330 331 332
        }
    }

    fprintf(stderr, "Error: failed to find workload generator %s\n", type);
    return(-1);
333 334
}

335 336 337 338 339 340
void codes_workload_print_op(
        FILE *f,
        struct codes_workload_op *op,
        int app_id,
        int rank)
{
341 342
    char *name;

343 344
    switch(op->op_type){
        case CODES_WK_END:
345
            fprintf(f, "op: app:%d rank:%d type:end\n", app_id, rank);
346 347
            break;
        case CODES_WK_DELAY:
348
            fprintf(f, "op: app:%d rank:%d type:delay seconds:%lf\n",
349
                    app_id, rank, op->u.delay.seconds);
350 351
            break;
        case CODES_WK_BARRIER:
352
            fprintf(f, "op: app:%d rank:%d type:barrier count:%d root:%d\n",
353
                    app_id, rank, op->u.barrier.count, op->u.barrier.root);
354 355
            break;
        case CODES_WK_OPEN:
356 357 358 359 360 361 362
        case CODES_WK_MPI_OPEN:
        case CODES_WK_MPI_COLL_OPEN:
            if(op->op_type == CODES_WK_OPEN) name = "open";
            if(op->op_type == CODES_WK_MPI_OPEN) name = "mpi_open";
            if(op->op_type == CODES_WK_MPI_COLL_OPEN) name = "mpi_coll_open";
            fprintf(f, "op: app:%d rank:%d type:%s file_id:%llu flag:%d\n",
                    app_id, rank, name, LLU(op->u.open.file_id), op->u.open.create_flag);
363 364
            break;
        case CODES_WK_CLOSE:
365 366 367 368 369
        case CODES_WK_MPI_CLOSE:
            if(op->op_type == CODES_WK_CLOSE) name = "close";
            if(op->op_type == CODES_WK_MPI_CLOSE) name = "mpi_close";
            fprintf(f, "op: app:%d rank:%d type:%s file_id:%llu\n",
                    app_id, rank, name, LLU(op->u.close.file_id));
370 371
            break;
        case CODES_WK_WRITE:
372 373 374 375 376 377
        case CODES_WK_MPI_WRITE:
        case CODES_WK_MPI_COLL_WRITE:
            if(op->op_type == CODES_WK_WRITE) name = "write";
            if(op->op_type == CODES_WK_MPI_WRITE) name = "mpi_write";
            if(op->op_type == CODES_WK_MPI_COLL_WRITE) name = "mpi_coll_write";
            fprintf(f, "op: app:%d rank:%d type:%s "
Jonathan Jenkins's avatar
Jonathan Jenkins committed
378
                       "file_id:%llu off:%llu size:%llu\n",
379
                    app_id, rank, name, LLU(op->u.write.file_id), LLU(op->u.write.offset),
Jonathan Jenkins's avatar
Jonathan Jenkins committed
380
                    LLU(op->u.write.size));
381 382
            break;
        case CODES_WK_READ:
383 384 385 386 387 388
        case CODES_WK_MPI_READ:
        case CODES_WK_MPI_COLL_READ:
            if(op->op_type == CODES_WK_READ) name = "read";
            if(op->op_type == CODES_WK_MPI_READ) name = "mpi_read";
            if(op->op_type == CODES_WK_MPI_COLL_READ) name = "mpi_coll_read";
            fprintf(f, "op: app:%d rank:%d type:%s "
Jonathan Jenkins's avatar
Jonathan Jenkins committed
389
                       "file_id:%llu off:%llu size:%llu\n",
390
                    app_id, rank, name, LLU(op->u.read.file_id), LLU(op->u.read.offset),
Jonathan Jenkins's avatar
Jonathan Jenkins committed
391
                    LLU(op->u.read.size));
392
            break;
393
        case CODES_WK_SEND:
394
            fprintf(f, "op: app:%d rank:%d type:send "
395
                    "src:%d dst:%d bytes:%"PRIu64" type:%d count:%d tag:%d "
Philip Carns's avatar
Philip Carns committed
396
                    "src:%d dst:%d bytes:%d type:%d count:%d tag:%d "
397
                    "start:%.5e end:%.5e\n",
398
                    app_id, rank,
399 400
                    op->u.send.source_rank, op->u.send.dest_rank,
                    op->u.send.num_bytes, op->u.send.data_type,
401 402
                    op->u.send.count, op->u.send.tag,
                    op->start_time, op->end_time);
403 404
            break;
        case CODES_WK_RECV:
405
            fprintf(f, "op: app:%d rank:%d type:recv "
406
                    "src:%d dst:%d bytes:%"PRIu64" type:%d count:%d tag:%d "
407
                    "start:%.5e end:%.5e\n",
408
                    app_id, rank,
409 410
                    op->u.recv.source_rank, op->u.recv.dest_rank,
                    op->u.recv.num_bytes, op->u.recv.data_type,
411 412
                    op->u.recv.count, op->u.recv.tag,
                    op->start_time, op->end_time);
413 414
            break;
        case CODES_WK_ISEND:
415
            fprintf(f, "op: app:%d rank:%d type:isend "
416
                    "src:%d dst:%d bytes:%"PRIu64" type:%d count:%d tag:%d "
417
                    "start:%.5e end:%.5e\n",
418
                    app_id, rank,
419 420
                    op->u.send.source_rank, op->u.send.dest_rank,
                    op->u.send.num_bytes, op->u.send.data_type,
421 422
                    op->u.send.count, op->u.send.tag,
                    op->start_time, op->end_time);
423 424
            break;
        case CODES_WK_IRECV:
425
            fprintf(f, "op: app:%d rank:%d type:irecv "
426
                    "src:%d dst:%d bytes:%"PRIu64" type:%d count:%d tag:%d "
427
                    "start:%.5e end:%.5e\n",
428
                    app_id, rank,
429 430
                    op->u.recv.source_rank, op->u.recv.dest_rank,
                    op->u.recv.num_bytes, op->u.recv.data_type,
431 432
                    op->u.recv.count, op->u.recv.tag,
                    op->start_time, op->end_time);
433
            break;
434 435
       case CODES_WK_REQ_FREE:
            fprintf(f, "op: app:%d rank:%d type:req free "
Jonathan Jenkins's avatar
Jonathan Jenkins committed
436
                    " req:%d ",
437 438 439
                    app_id, rank,
                    op->u.free.req_id);
            break;
440 441
#define PRINT_COL(_type_str) \
            fprintf(f, "op: app:%d rank:%d type:%s" \
442
                    " bytes:%d, start:%.5e, end:%.5e\n", app_id, rank, \
443 444
                    _type_str, op->u.collective.num_bytes, op->start_time, \
                    op->end_time)
445
        case CODES_WK_BCAST:
446
            PRINT_COL("bcast");
447 448
            break;
        case CODES_WK_ALLGATHER:
449
            PRINT_COL("allgather");
450 451
            break;
        case CODES_WK_ALLGATHERV:
452
            PRINT_COL("allgatherv");
453 454
            break;
        case CODES_WK_ALLTOALL:
455
            PRINT_COL("alltoall");
456 457
            break;
        case CODES_WK_ALLTOALLV:
458
            PRINT_COL("alltoallv");
459 460
            break;
        case CODES_WK_REDUCE:
461
            PRINT_COL("reduce");
462 463
            break;
        case CODES_WK_ALLREDUCE:
464
            PRINT_COL("allreduce");
465 466
            break;
        case CODES_WK_COL:
467
            PRINT_COL("collective");
468
            break;
469 470 471
#undef PRINT_COL
#define PRINT_WAIT(_type_str, _ct) \
            fprintf(f, "op: app:%d rank:%d type:%s" \
472
                    "num reqs:%d, start:%.5e, end:%.5e\n", \
473
                    app_id, rank, _type_str, _ct, op->start_time, op->end_time)
474
        case CODES_WK_WAITALL:
475
            PRINT_WAIT("waitall", op->u.waits.count);
476 477
            break;
        case CODES_WK_WAIT:
478
            PRINT_WAIT("wait", 1);
479 480
            break;
        case CODES_WK_WAITSOME:
481
            PRINT_WAIT("waitsome", op->u.waits.count);
482 483
            break;
        case CODES_WK_WAITANY:
484
            PRINT_WAIT("waitany", op->u.waits.count);
485
            break;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
486 487
        case CODES_WK_IGNORE:
            break;
488
        default:
Jonathan Jenkins's avatar
Jonathan Jenkins committed
489 490 491
            fprintf(stderr,
                    "%s:%d: codes_workload_print_op: unrecognized workload type "
                    "(op code %d)\n", __FILE__, __LINE__, op->op_type);
492 493 494
    }
}

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
void codes_workload_add_method(struct codes_workload_method const * method)
{
    static int method_array_cap = 8;
    if (is_workloads_init)
        tw_error(TW_LOC,
                "adding a workload method after initialization is forbidden");
    else if (method_array == NULL){
        method_array = malloc(method_array_cap * sizeof(*method_array));
        assert(method_array);
    }

    if (num_user_methods == method_array_cap) {
        method_array_cap *= 2;
        method_array = realloc(method_array,
                method_array_cap * sizeof(*method_array));
        assert(method_array);
    }

    method_array[num_user_methods++] = method;
}

516 517 518 519
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
520
 *  indent-tabs-mode: nil
521 522 523 524
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */