codes-workload-mpi-replay.c 16.5 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 23 24
#include <mpi.h>

#include "codes/codes-workload.h"
#include "codes/quickhash.h"
25
#include "codes/configuration.h"
26

27
#define WORKLOAD_PRINT 1
28

29 30
#define WORKLOAD_DELAY_PCT 0.95

31
/* hash table entry for looking up file descriptor of a workload file id */
32
struct file_info
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
    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;

/* 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 53
void usage(char *exename)
{
54 55
    fprintf(stderr, "Usage: %s [OPTIONS] --conf <conf_file_path>\n       "
            "--test-dir <workload_test_dir>\n\n", exename);
56
    fprintf(stderr, "\t<conf_file_path> : (absolute) path to a valid workload configuration file\n");
57 58
    fprintf(stderr, "\t<workload_test_dir> : the directory to replay the workload I/O in\n");
    fprintf(stderr, "\n\t[OPTIONS] includes:\n");
59
    fprintf(stderr, "\t\t--noop : do not perform i/o\n");
60
    fprintf(stderr, "\t\t    -v : verbose (output i/o details)\n");
61 62 63 64

    exit(1);
}

65
void parse_args(int argc, char **argv, char **conf_path, char **test_dir)
66 67 68 69
{
    int index;
    static struct option long_opts[] =
    {
70
        {"conf", 1, NULL, 'c'},
71
        {"test-dir", 1, NULL, 'd'},
72
        {"noop", 0, NULL, 'n'},
73 74 75 76
        {"help", 0, NULL, 0},
        {0, 0, 0, 0}
    };

77
    *conf_path = NULL;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    *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;
94 95
            case 'c':
                *conf_path = optarg;
96 97 98 99 100 101 102 103 104 105 106 107
                break;
            case 'd':
                *test_dir = optarg;
                break;
            case 0:
            case '?':
            default:
                usage(argv[0]);
                break;
        }
    }

108
    if (optind < argc || !(*conf_path) || !(*test_dir))
109 110 111 112 113 114 115
    {
        usage(argv[0]);
    }

    return;
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
int load_workload(char *conf_path, int rank)
{
    config_lpgroups_t paramconf;
    char workload_type[MAX_NAME_LENGTH_WKLD];

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

    /* get the PARAMS group out of the config file */
    configuration_get_lpgroups(&config, "PARAMS", &paramconf);

    /* get the workload type out of PARAMS */
    configuration_get_value(&config, "PARAMS", "workload_type",
                            workload_type, MAX_NAME_LENGTH_WKLD);

    /* 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",
                                d_params.log_file_path, MAX_NAME_LENGTH_WKLD);
        configuration_get_value(&config, "PARAMS", "aggregator_count", aggregator_count, 10);
        d_params.aggregator_cnt = atoi(aggregator_count);

143
#if WORKLOAD_PRINT
144
        d_params.stream = NULL;
145
#else
146
        d_params.stream = log_stream;
147 148 149
        opt_verbose = 0;
#endif

150 151 152 153 154 155 156 157 158 159
        return codes_workload_load(workload_type, (char *)&d_params, rank);
    }
    else if (strcmp(workload_type, "bgp_io_workload") == 0)
    {
        struct bgp_params b_params;
        char rank_count[10];

        /* get the bgp i/o params from the config file */
        configuration_get_value(&config, "PARAMS", "io_kernel_meta_path",
                                b_params.io_kernel_meta_path, MAX_NAME_LENGTH_WKLD);
160
        configuration_get_value(&config, "PARAMS", "bgp_config_file",
161 162 163 164 165 166 167 168
                                b_params.bgp_config_file, MAX_NAME_LENGTH_WKLD);
        configuration_get_value(&config, "PARAMS", "rank_count", rank_count, 10);
        strcpy(b_params.io_kernel_path, "");
        strcpy(b_params.io_kernel_def_path, "");
        b_params.num_cns = atoi(rank_count);

        return codes_workload_load(workload_type, (char *)&b_params, rank);
    }
169 170 171 172 173 174 175 176 177 178 179
    else if (strcmp(workload_type, "recorder_io_workload") == 0) {
        struct recorder_params r_params;

        /* get the darshan params from the config file */
        configuration_get_value(&config, "PARAMS", "trace_dir_path",
                                r_params.trace_dir_path, MAX_NAME_LENGTH_WKLD);
        r_params.stream = NULL;

        return codes_workload_load(workload_type, (char *)&r_params, rank);

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

187 188
int main(int argc, char *argv[])
{
189
    char *conf_path;
190
    char *replay_test_path;
191 192
    char *log_dir = "log";
    char my_log_path[MAX_NAME_LENGTH_WKLD];
193 194 195 196 197
    int nprocs;
    int myrank;
    int workload_id;
    struct codes_workload_op next_op;
    long long int replay_op_number = 1;
198 199
    double load_start, load_end;
    int first_op = 1;
200 201 202
    int ret = 0;

    /* parse command line args */
203
    parse_args(argc, argv, &conf_path, &replay_test_path);
204 205 206 207 208 209

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

210 211 212
    /* start workload load timer */
    load_start = MPI_Wtime();

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    /* 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;
        }
    }

234 235
    /* initialize workload generator from config file */
    workload_id = load_workload(conf_path, myrank);
236 237 238 239 240 241 242 243 244 245 246 247 248
    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;
    }

249 250 251
    /* loading is finished */
    load_end = MPI_Wtime();

252 253 254 255 256 257
    /* replay loop */
    while (1)
    {
        /* get the next replay operation from the workload generator */
        codes_workload_get_next(workload_id, myrank, &next_op);

258 259 260 261 262 263 264 265 266 267 268
        /* subtract the load time from any initial delay to be fair */
        if (first_op && (next_op.op_type == CODES_WK_DELAY))
        {
            if (next_op.u.delay.seconds > (load_end - load_start))
                next_op.u.delay.seconds -= (load_end - load_start);
            else
                continue;

            first_op = 0;
        }

269 270
        if (next_op.op_type != CODES_WK_END)
        {
271 272 273 274

            if (next_op.op_type == CODES_WK_DELAY)
                next_op.u.delay.seconds *= WORKLOAD_DELAY_PCT;

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

error_exit:
    MPI_Finalize();
297

298 299 300 301 302 303 304 305 306 307
    return ret;
}

int replay_workload_op(struct codes_workload_op replay_op, int rank, long long int op_number)
{
    unsigned int secs;
    unsigned int usecs;
    int open_flags = O_RDWR;
    char file_name[50];
    int fildes;
308
    struct file_info *tmp_list = NULL;
309 310 311 312 313 314 315
    struct qlist_head *hash_link = NULL;
    char *buf = NULL;
    int ret;

    switch (replay_op.op_type)
    {
        case CODES_WK_DELAY:
316
#if WORKLOAD_PRINT
317
            if (opt_verbose)
318
                fprintf(log_stream, "[Rank %d] Operation %lld : DELAY %lf seconds\n",
319
                       rank, op_number, replay_op.u.delay.seconds);
320
#endif
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

            if (!opt_noop)
            {
                /* satisfy delay using second delay then microsecond delay */
                secs = floor(replay_op.u.delay.seconds);
                usecs = round((replay_op.u.delay.seconds - secs) * 1000 * 1000);
                ret = sleep(secs);
                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;
                }
                ret = usleep(usecs);
                if (ret < 0)
                {
                    /* error in usleep */
                    fprintf(stderr, "Rank %d failure on operation %lld [DELAY: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
            }
            return 0;
        case CODES_WK_BARRIER:
347
#if WORKLOAD_PRINT
348
            if (opt_verbose)
349
                fprintf(log_stream, "[Rank %d] Operation %lld : BARRIER\n", rank, op_number);
350
#endif
351 352 353 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;
                }
            }
            return 0;
        case CODES_WK_OPEN:
366
#if WORKLOAD_PRINT
367
            if (opt_verbose)
368
                fprintf(log_stream, "[Rank %d] Operation %lld: %s file %"PRIu64"\n", rank, op_number,
369
                       (replay_op.u.open.create_flag) ? "CREATE" : "OPEN", replay_op.u.open.file_id);
370
#endif
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

            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 */
                snprintf(file_name, sizeof(file_name), "%"PRIu64, replay_op.u.open.file_id);

                /* 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 */
392
                tmp_list = malloc(sizeof(struct file_info));
393 394 395 396 397 398 399 400 401 402 403 404
                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));
            }
            return 0;
        case CODES_WK_CLOSE:
405
#if WORKLOAD_PRINT
406
            if (opt_verbose)
407 408
                fprintf(log_stream, "[Rank %d] Operation %lld : CLOSE file %"PRIu64"\n",
                        rank, op_number, replay_op.u.close.file_id);
409
#endif
410 411 412 413 414 415

            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);
416
                tmp_list = qhash_entry(hash_link, struct file_info, hash_link);
417 418
                fildes = tmp_list->file_descriptor;
                free(tmp_list);
419

420 421 422 423 424 425 426 427 428 429 430
                /* 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;
                }
            }
            return 0;
        case CODES_WK_WRITE:
431
#if WORKLOAD_PRINT
432
            if (opt_verbose)
433
                fprintf(log_stream, "[Rank %d] Operation %lld : WRITE file %"PRIu64" (sz = %"PRId64
434
                       ", off = %"PRId64")\n",
435
                       rank, op_number, replay_op.u.write.file_id, replay_op.u.write.size,
436
                       replay_op.u.write.offset);
437
#endif
438 439 440 441 442 443

            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);
444
                tmp_list = qhash_entry(hash_link, struct file_info, hash_link);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
                fildes = tmp_list->file_descriptor;

                /* perform the write operation */
                buf = malloc(replay_op.u.write.size);
                if (!buf)
                {
                    fprintf(stderr, "No memory available for write buffer\n");
                    return -1;
                }
                ret = pwrite(fildes, buf, replay_op.u.write.size, replay_op.u.write.offset);
                free(buf);
                if (ret < 0)
                {
                    fprintf(stderr, "Rank %d failure on operation %lld [WRITE: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
            }
            return 0;
        case CODES_WK_READ:
465
#if WORKLOAD_PRINT
466
            if (opt_verbose)
467 468
                fprintf(log_stream, "[Rank %d] Operation %lld : READ file %"PRIu64" (sz = %"PRId64
                        ", off = %"PRId64")\n", rank, op_number, replay_op.u.read.file_id,
469
                       replay_op.u.read.size, replay_op.u.read.offset);
470
#endif
471 472 473 474 475
            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);
476
                tmp_list = qhash_entry(hash_link, struct file_info, hash_link);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
                fildes = tmp_list->file_descriptor;

                /* perform the write operation */
                buf = malloc(replay_op.u.read.size);
                if (!buf)
                {
                    fprintf(stderr, "No memory available for write buffer\n");
                    return -1;
                }
                ret = pread(fildes, buf, replay_op.u.read.size, replay_op.u.read.offset);
                free(buf);
                if (ret < 0)
                {
                    fprintf(stderr, "Rank %d failure on operation %lld [READ: %s]\n",
                            rank, op_number, strerror(errno));
                    return -1;
                }
            }
            return 0;
        default:
            fprintf(stderr, "** Rank %d: INVALID OPERATION (op count = %lld) **\n", rank, op_number);
498
            return 0;
499 500 501 502 503 504 505
    }

}

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

508
    tmp_file = qlist_entry(link, struct file_info, hash_link);
509 510 511 512 513
    if (tmp_file->file_hash == *in_file_hash)
        return 1;

    return 0;
}
514 515 516 517 518 519 520 521 522 523

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