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

/* SUMMARY:
 *
9
 *  MPI replay tool for replaying workloads from the codes workload API.
10 11 12 13 14 15 16 17 18 19
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <getopt.h>
#include <fcntl.h>
20
#include <sys/stat.h>
21 22
#include <mpi.h>

Jonathan Jenkins's avatar
Jonathan Jenkins committed
23 24 25 26
#include <codes/codes-workload.h>
#include <codes/quickhash.h>
#include <codes/configuration.h>
#include <codes/codes.h>
27

28
#define DEBUG_PROFILING 0
29

30
/* hash table entry for looking up file descriptor of a workload file id */
31
struct file_info
32 33 34 35 36 37 38 39 40 41 42 43
{
    struct qlist_head hash_link;
    uint64_t file_hash;
    int file_descriptor;
};

int replay_workload_op(struct codes_workload_op replay_op, int rank, long long int op_number);
int hash_file_compare(void *key, struct qlist_head *link);

/* command line options */
static int opt_verbose = 0;
static int opt_noop = 0;
44
static double opt_delay_pct = 1.0;
45 46 47 48

/* hash table for storing file descriptors of opened files */
static struct qhash_table *fd_table = NULL;

49 50 51
/* file stream to log rank events to, if verbose turned on */
static FILE *log_stream = NULL;

52
/* global variables for profiling different portions of the replay, if enabled */
53
#if DEBUG_PROFILING
54 55 56 57 58 59
static double total_open_time = 0.0;
static double total_close_time = 0.0;
static double total_read_time = 0.0;
static double total_write_time = 0.0;
static double total_delay_time = 0.0;
static double total_barrier_time = 0.0;
60
#endif
61

62 63
void usage(char *exename)
{
64 65
    fprintf(stderr, "Usage: %s [OPTIONS] --conf <conf_file_path>\n       "
            "--test-dir <workload_test_dir>\n\n", exename);
66
    fprintf(stderr, "\t<conf_file_path> : (absolute) path to a valid workload configuration file\n");
67 68
    fprintf(stderr, "\t<workload_test_dir> : the directory to replay the workload I/O in\n");
    fprintf(stderr, "\n\t[OPTIONS] includes:\n");
69
    fprintf(stderr, "\t\t--noop : do not perform i/o\n");
70
    fprintf(stderr, "\t\t    -v : verbose (output i/o details)\n");
71 72 73 74

    exit(1);
}

75
void parse_args(int argc, char **argv, char **conf_path, char **test_dir)
76 77 78 79
{
    int index;
    static struct option long_opts[] =
    {
80
        {"conf", 1, NULL, 'c'},
81
        {"test-dir", 1, NULL, 'd'},
82
        {"noop", 0, NULL, 'n'},
83
        {"delay", 1, NULL, 'p'},
84 85 86 87
        {"help", 0, NULL, 0},
        {0, 0, 0, 0}
    };

88
    *conf_path = NULL;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    *test_dir = NULL;
    while (1)
    {
        int c = getopt_long(argc, argv, "v", long_opts, &index);

        if (c == -1)
            break;

        switch (c)
        {
            case 'v':
                opt_verbose = 1;
                break;
            case 'n':
                opt_noop = 1;
                break;
105 106
            case 'c':
                *conf_path = optarg;
107 108 109 110
                break;
            case 'd':
                *test_dir = optarg;
                break;
111 112 113
            case 'p':
                opt_delay_pct = atof(optarg);
                break;
114 115 116 117 118 119 120 121
            case 0:
            case '?':
            default:
                usage(argv[0]);
                break;
        }
    }

122
    if (optind < argc || !(*conf_path) || !(*test_dir))
123 124 125 126 127 128 129
    {
        usage(argv[0]);
    }

    return;
}

130 131 132 133 134 135 136 137 138
int load_workload(char *conf_path, int rank)
{
    char workload_type[MAX_NAME_LENGTH_WKLD];

    /* load the config file across all ranks */
    configuration_load(conf_path, MPI_COMM_WORLD, &config);

    /* get the workload type out of PARAMS */
    configuration_get_value(&config, "PARAMS", "workload_type",
139
                            NULL, workload_type, MAX_NAME_LENGTH_WKLD);
140 141 142 143 144 145 146 147 148

    /* set up the workload parameters and load into the workload API */
    if (strcmp(workload_type, "darshan_io_workload") == 0)
    {
        struct darshan_params d_params;
        char aggregator_count[10];

        /* get the darshan params from the config file */
        configuration_get_value(&config, "PARAMS", "log_file_path",
149 150
                                NULL, d_params.log_file_path, MAX_NAME_LENGTH_WKLD);
        configuration_get_value(&config, "PARAMS", "aggregator_count", NULL, aggregator_count, 10);
151
        d_params.aggregator_cnt = atol(aggregator_count);
152

153
        return codes_workload_load(workload_type, (char *)&d_params, 0, rank);
154
    }
155
    else if (strcmp(workload_type, "iolang_workload") == 0)
156
    {
157
        struct iolang_params i_params;
158 159
        char rank_count[10];

160
        /* get the iolang i/o params from the config file */
161
        configuration_get_value(&config, "PARAMS", "io_kernel_meta_path",
162
                                NULL, i_params.io_kernel_meta_path, MAX_NAME_LENGTH_WKLD);
163
        configuration_get_value(&config, "PARAMS", "rank_count", NULL, rank_count, 10);
164 165 166
        strcpy(i_params.io_kernel_path, "");
        i_params.num_cns = atoi(rank_count);
        i_params.use_relpath = 1;
167

168
        return codes_workload_load(workload_type, (char *)&i_params, 0, rank);
169
    }
170 171
    else if (strcmp(workload_type, "recorder_io_workload") == 0) {
        struct recorder_params r_params;
172
        char nprocs[10];
173

174
        /* get the recorder params from the config file */
175
        configuration_get_value(&config, "PARAMS", "trace_dir_path",
176 177
                                NULL, r_params.trace_dir_path, MAX_NAME_LENGTH_WKLD);
        configuration_get_value(&config, "PARAMS", "nprocs", NULL, nprocs, 10);
178
        r_params.nprocs = atol(nprocs);
179

180
        return codes_workload_load(workload_type, (char *)&r_params, 0, rank);
181 182

	}
183 184 185 186 187 188 189
    else
    {
        fprintf(stderr, "Error: Invalid workload type specified (%s)\n", workload_type);
        return -1;
    }
}

190
char* buf = NULL;
191 192
int fd = -1;
int op_num = 0;
193

194 195
int main(int argc, char *argv[])
{
196
    char *conf_path;
197
    char *replay_test_path;
198 199
    char *log_dir = "log";
    char my_log_path[MAX_NAME_LENGTH_WKLD];
200 201 202 203 204
    int nprocs;
    int myrank;
    int workload_id;
    struct codes_workload_op next_op;
    long long int replay_op_number = 1;
205
    double load_start, load_end;
206 207 208
    int ret = 0;

    /* parse command line args */
209
    parse_args(argc, argv, &conf_path, &replay_test_path);
210 211 212 213 214 215

    /* initialize MPI */
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

216 217 218
    /* start workload load timer */
    load_start = MPI_Wtime();

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    /* change the working directory to be the test directory */
    ret = chdir(replay_test_path);
    if (ret < 0)
    {
        fprintf(stderr, "Unable to change to testing directory (%s)\n", strerror(errno));
        goto error_exit;
    }

    /* set the path for logging this rank's events, if verbose is turned on */
    if (opt_verbose)
    {
        mkdir(log_dir, 0755);
        snprintf(my_log_path, MAX_NAME_LENGTH_WKLD, "%s/rank-%d.log", log_dir, myrank);
        log_stream = fopen(my_log_path, "w");
        if (log_stream == NULL)
        {
            fprintf(stderr, "Unable to open log file %s\n", my_log_path);
            goto error_exit;
        }
    }

240 241
    /* initialize workload generator from config file */
    workload_id = load_workload(conf_path, myrank);
242 243 244 245 246 247 248 249 250 251 252 253 254
    if (workload_id < 0)
    {
        goto error_exit;
    }

    /* initialize hash table for storing file descriptors */
    fd_table = qhash_init(hash_file_compare, quickhash_64bit_hash, 29);
    if (!fd_table)
    {
        fprintf(stderr, "File descriptor hash table memory error\n");
        goto error_exit;
    }

255 256 257
    /* synchronize before replay */
    MPI_Barrier(MPI_COMM_WORLD);

258 259 260
    /* loading is finished */
    load_end = MPI_Wtime();

Shane Snyder's avatar
Shane Snyder committed
261 262
    if (myrank == 0) printf("Note: Workload took %.2lf seconds to load.\n", load_end - load_start);

263 264 265 266
    /* replay loop */
    while (1)
    {
        /* get the next replay operation from the workload generator */
267
        codes_workload_get_next(workload_id, 0, myrank, &next_op);
268 269 270

        if (next_op.op_type != CODES_WK_END)
        {
271 272

            if (next_op.op_type == CODES_WK_DELAY)
273
                next_op.u.delay.seconds *= opt_delay_pct;
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288
            /* replay the next workload operation */
            ret = replay_workload_op(next_op, myrank, replay_op_number++);
            if (ret < 0)
            {
                break;
            }
        }
        else
        {
            /* workload replay for this rank is complete */
            break;
        }
    }

289
    if (log_stream)
290 291
        fclose(log_stream);

292
    /* destroy and finalize the file descriptor hash table */
293
    qhash_destroy_and_finalize(fd_table, struct file_info, hash_link, free);
294

295 296 297 298 299 300
#if DEBUG_PROFILING
    printf("Rank %d:\td=%.4lf, b=%.4lf, o=%.4lf, c=%.4lf, r=%.4lf, w=%.4lf\n",
           myrank, total_delay_time, total_barrier_time, total_open_time,
           total_close_time, total_read_time, total_write_time);
#endif

301 302
error_exit:
    MPI_Finalize();
303

304 305 306 307 308
    return ret;
}

int replay_workload_op(struct codes_workload_op replay_op, int rank, long long int op_number)
{
Shane Snyder's avatar
Shane Snyder committed
309
    struct timespec delay;
310
    int open_flags = O_RDWR;
311
    char file_name[250];
312
    int fildes;
313
    struct file_info *tmp_list = NULL;
314 315
    struct qlist_head *hash_link = NULL;
    int ret;
316
    int i;
317 318

#if DEBUG_PROFILING
319
    double start, end;
320 321
    start = MPI_Wtime();
#endif
322 323 324 325 326

    switch (replay_op.op_type)
    {
        case CODES_WK_DELAY:
            if (opt_verbose)
327
                fprintf(log_stream, "[Rank %d] Operation %lld : DELAY %lf seconds\n",
328 329 330 331 332
                       rank, op_number, replay_op.u.delay.seconds);

            if (!opt_noop)
            {
                /* satisfy delay using second delay then microsecond delay */
Shane Snyder's avatar
Shane Snyder committed
333 334 335 336
                delay.tv_sec = (long long)replay_op.u.delay.seconds;
                delay.tv_nsec = (unsigned int)((replay_op.u.delay.seconds - delay.tv_sec) *
                                               1000 * 1000 * 1000);
                ret = nanosleep(&delay, NULL);
337 338 339 340 341 342 343 344
                if (ret)
                {
                    /* error in sleep */
                    errno = EINTR;
                    fprintf(stderr, "Rank %d failure on operation %lld [DELAY: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
345 346 347 348
#if DEBUG_PROFILING
                end = MPI_Wtime();
                total_delay_time += (end - start);
#endif
349 350 351 352
            }
            return 0;
        case CODES_WK_BARRIER:
            if (opt_verbose)
353
                fprintf(log_stream, "[Rank %d] Operation %lld : BARRIER\n", rank, op_number);
354 355 356 357 358 359 360 361 362 363 364 365

            if (!opt_noop)
            {
                /* implement barrier using MPI global barrier on all ranks */
                ret = MPI_Barrier(MPI_COMM_WORLD);
                if (ret != MPI_SUCCESS)
                {
                    /* error in MPI_Barrier */
                    fprintf(stderr, "Rank %d failure on operation %lld [BARRIER: %s]\n",
                            rank, op_number, "Invalid communicator");
                    return -1;
                }
366 367 368 369 370

#if DEBUG_PROFILING
                end = MPI_Wtime();
                total_barrier_time += (end - start);
#endif
371 372 373 374
            }
            return 0;
        case CODES_WK_OPEN:
            if (opt_verbose)
375
                fprintf(log_stream, "[Rank %d] Operation %lld: %s file %"PRIu64"\n", rank, op_number,
376 377 378 379 380 381 382 383 384
                       (replay_op.u.open.create_flag) ? "CREATE" : "OPEN", replay_op.u.open.file_id);

            if (!opt_noop)
            {
                /* set the create flag, if necessary */
                if (replay_op.u.open.create_flag)
                    open_flags |= O_CREAT;

                /* write the file hash to string to be used as the actual file name */
Shane Snyder's avatar
Shane Snyder committed
385
                snprintf(file_name, sizeof(file_name), "%"PRIu64, replay_op.u.open.file_id);
386 387 388 389 390 391 392 393 394 395 396 397

                /* perform the open operation */
                fildes = open(file_name, open_flags, 0666);
                if (fildes < 0)
                {
                    fprintf(stderr, "Rank %d failure on operation %lld [%s: %s]\n",
                            rank, op_number, (replay_op.u.open.create_flag) ? "CREATE" : "OPEN",
                            strerror(errno));
                    return -1;
                }

                /* save the file descriptor for this file in a hash table to be retrieved later */
398
                tmp_list = malloc(sizeof(struct file_info));
399 400 401 402 403 404 405 406 407
                if (!tmp_list)
                {
                    fprintf(stderr, "No memory available for file hash entry\n");
                    return -1;
                }

                tmp_list->file_hash = replay_op.u.open.file_id;
                tmp_list->file_descriptor = fildes;
                qhash_add(fd_table, &(replay_op.u.open.file_id), &(tmp_list->hash_link));
408

409 410 411 412 413 414 415 416 417 418
                if (!buf)
                {
                    buf = malloc(16*1024*1024);
                    assert(buf);
                    for(i=0; i<16*1024*1024; i++)
                    {
                        buf[i] = '1';
                    }
                }

419 420 421 422
#if DEBUG_PROFILING
                end = MPI_Wtime();
                total_open_time += (end - start);
#endif
423 424 425 426
            }
            return 0;
        case CODES_WK_CLOSE:
            if (opt_verbose)
427 428
                fprintf(log_stream, "[Rank %d] Operation %lld : CLOSE file %"PRIu64"\n",
                        rank, op_number, replay_op.u.close.file_id);
429 430 431 432 433 434

            if (!opt_noop)
            {
                /* search for the corresponding file descriptor in the hash table */
                hash_link = qhash_search_and_remove(fd_table, &(replay_op.u.close.file_id));
                assert(hash_link);
435
                tmp_list = qhash_entry(hash_link, struct file_info, hash_link);
436 437
                fildes = tmp_list->file_descriptor;
                free(tmp_list);
438

439 440 441 442 443 444 445 446
                /* perform the close operation */
                ret = close(fildes);
                if (ret < 0)
                {
                    fprintf(stderr, "Rank %d failure on operation %lld [CLOSE: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
447 448 449 450 451

#if DEBUG_PROFILING
                end = MPI_Wtime();
                total_close_time += (end - start);
#endif
452 453 454 455
            }
            return 0;
        case CODES_WK_WRITE:
            if (opt_verbose)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
456 457 458
                fprintf(log_stream, "[Rank %d] Operation %lld : WRITE file %llu (sz = %llu, off = %llu)\n",
                       rank, op_number, LLU(replay_op.u.write.file_id), LLU(replay_op.u.write.size),
                       LLU(replay_op.u.write.offset));
459 460 461 462 463 464

            if (!opt_noop)
            {
                /* search for the corresponding file descriptor in the hash table */
                hash_link = qhash_search(fd_table, &(replay_op.u.write.file_id));
                assert(hash_link);
465
                tmp_list = qhash_entry(hash_link, struct file_info, hash_link);
466 467 468
                fildes = tmp_list->file_descriptor;

                ret = pwrite(fildes, buf, replay_op.u.write.size, replay_op.u.write.offset);
469

470 471 472 473 474 475
                if (ret < 0)
                {
                    fprintf(stderr, "Rank %d failure on operation %lld [WRITE: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
476 477 478 479 480

#if DEBUG_PROFILING
                end = MPI_Wtime();
                total_write_time += (end - start);
#endif
481 482 483 484
            }
            return 0;
        case CODES_WK_READ:
            if (opt_verbose)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
485 486 487
                fprintf(log_stream, "[Rank %d] Operation %lld : READ file %llu (sz = %llu, off = %llu)\n",
                        rank, op_number, LLU(replay_op.u.read.file_id),
                        LLU(replay_op.u.read.size), LLU(replay_op.u.read.offset));
488

489 490 491 492 493
            if (!opt_noop)
            {
                /* search for the corresponding file descriptor in the hash table */
                hash_link = qhash_search(fd_table, &(replay_op.u.read.file_id));
                assert(hash_link);
494
                tmp_list = qhash_entry(hash_link, struct file_info, hash_link);
495 496 497
                fildes = tmp_list->file_descriptor;

                ret = pread(fildes, buf, replay_op.u.read.size, replay_op.u.read.offset);
498

499 500 501 502 503 504
                if (ret < 0)
                {
                    fprintf(stderr, "Rank %d failure on operation %lld [READ: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
505 506 507 508 509

#if DEBUG_PROFILING
                end = MPI_Wtime();
                total_read_time += (end - start);
#endif
510 511 512 513
            }
            return 0;
        default:
            fprintf(stderr, "** Rank %d: INVALID OPERATION (op count = %lld) **\n", rank, op_number);
514
            return 0;
515 516 517 518 519 520 521
    }

}

int hash_file_compare(void *key, struct qlist_head *link)
{
    uint64_t *in_file_hash = (uint64_t *)key;
522
    struct file_info *tmp_file;
523

524
    tmp_file = qlist_entry(link, struct file_info, hash_link);
525 526 527 528 529
    if (tmp_file->file_hash == *in_file_hash)
        return 1;

    return 0;
}
530 531 532 533 534 535 536 537 538 539

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