local-storage-model.c 25.1 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#include <assert.h>
#include <ross.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
9 10 11 12 13 14
#include <codes/lp-io.h>
#include <codes/jenkins-hash.h>
#include <codes/codes.h>
#include <codes/codes_mapping.h>
#include <codes/lp-type-lookup.h>
#include <codes/local-storage-model.h>
15
#include <codes/quicklist.h>
16
#include <codes/rc-stack.h>
17 18 19 20

#define CATEGORY_NAME_MAX 16
#define CATEGORY_MAX 12

21 22
int lsm_in_sequence = 0;
tw_stime lsm_msg_offset = 0.0;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/* holds statistics about disk traffic on each LP */
typedef struct lsm_stats_s
{
    char category[CATEGORY_NAME_MAX];
    long read_count;
    long read_bytes;
    long read_seeks;
    tw_stime read_time;
    long write_count;
    long write_bytes;
    long write_seeks;
    tw_stime write_time;
} lsm_stats_t;

/*
 * disk model parameters
 */
typedef struct disk_model_s
{
    unsigned int *request_sizes;
    double *write_rates;
    double *read_rates;
    double *write_overheads;
    double *read_overheads;
    double *write_seeks;
    double *read_seeks;
    unsigned int bins;
51 52 53 54
    // sched params
    //   0  - no scheduling
    //  >0  - make scheduler with use_sched priority lanes
    int use_sched;
55 56
} disk_model_t;

57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
 * lsm_message_data_t
 *   - data used for input in transfer time calculation
 *   - data comes for caller
 *   - object: id of byte stream which could be a file, object, etc.
 *   - offset: offset into byte stream
 *   - size: size in bytes of request
 */
typedef struct lsm_message_data_s
{
    uint64_t    object;
    uint64_t    offset;
    uint64_t    size;
    char category[CATEGORY_NAME_MAX]; /* category for traffic */
71
    int prio; // for scheduling
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
} lsm_message_data_t;

/*
 * lsm_sched_op_s - operation to be scheduled
 */
typedef struct lsm_sched_op_s
{
    lsm_message_data_t data;
    struct qlist_head ql;
} lsm_sched_op_t;

/*
 * lsm_sched_s - data structure for implementing scheduling loop
 */
typedef struct lsm_sched_s
{
    int num_prios;
89 90 91 92 93
    // number of pending requests, incremented on new and decremented on
    // complete
    int active_count;
    // scheduler mallocs data per-request - hold onto and free later
    struct rc_stack *freelist;
94 95 96
    struct qlist_head *queues;
} lsm_sched_t;

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/*
 * lsm_state_s
 *   - state tracking structure for each LP node
 *   - next_idle: next point in time the disk will be idle
 *   - model: disk parameters
 *   - current_offset: last offset the disk operated on
 *   - current_object: last object id that operated on
 */
typedef struct lsm_state_s
{
    tw_stime next_idle;
    disk_model_t *model;
    int64_t  current_offset;
    uint64_t current_object;
    lsm_stats_t lsm_stats_array[CATEGORY_MAX];
112 113 114
    /* scheduling state */
    int use_sched;
    lsm_sched_t sched;
115 116 117 118 119 120
} lsm_state_t;

/*
 * lsm_message_t
 *   - holds event data
 *   - event: event type
121
 *   - data: IO request data
122 123 124 125 126 127
 *   - wrap: wrapped event data of caller
 */
typedef struct lsm_message_s
{
    int magic; /* magic number */
    lsm_event_t event;
128
    int prio; // op priority (user-set on request, used by LSM in rc for complete)
129 130 131 132
    tw_stime    prev_idle;
    lsm_stats_t prev_stat;
    int64_t     prev_offset;
    uint64_t    prev_object;
133
    lsm_message_data_t data;
134
    struct codes_cb_params cb;
135 136 137 138 139 140 141 142 143
} lsm_message_t;

/*
 * Prototypes
 */
static void lsm_lp_init (lsm_state_t *ns, tw_lp *lp);
static void lsm_event (lsm_state_t *ns, tw_bf *b, lsm_message_t *m, tw_lp *lp);
static void lsm_rev_event (lsm_state_t *ns, tw_bf *b, lsm_message_t *m, tw_lp *lp);
static void lsm_finalize (lsm_state_t *ns, tw_lp *lp);
144 145 146 147 148 149
static void handle_io_sched_new(lsm_state_t *ns, tw_bf *b, lsm_message_t *m_in, tw_lp *lp);
static void handle_rev_io_sched_new(lsm_state_t *ns, tw_bf *b, lsm_message_t *m_in, tw_lp *lp);
static void handle_io_request(lsm_state_t *ns, tw_bf *b, lsm_message_data_t *data, lsm_message_t *m_in, tw_lp *lp);
static void handle_rev_io_request(lsm_state_t *ns, tw_bf *b, lsm_message_data_t *data, lsm_message_t *m_in, tw_lp *lp);
static void handle_io_sched_compl(lsm_state_t *ns, tw_bf *b, lsm_message_t *m_in, tw_lp *lp);
static void handle_rev_io_sched_compl(lsm_state_t *ns, tw_bf *b, lsm_message_t *m_in, tw_lp *lp);
150 151 152 153 154 155 156 157 158 159 160
static void handle_io_completion (lsm_state_t *ns, tw_bf *b, lsm_message_t *m_in, tw_lp *lp);
static void handle_rev_io_completion (lsm_state_t *ns, tw_bf *b, lsm_message_t *m_in, tw_lp *lp);
static lsm_stats_t *find_stats(const char* category, lsm_state_t *ns);
static void write_stats(tw_lp* lp, lsm_stats_t* stat);

/*
 * Globals
 */

static int lsm_magic = 0;

161 162 163 164
/* configuration parameters (by annotation) */
static disk_model_t model_unanno, *models_anno = NULL;
static const config_anno_map_t *anno_map = NULL;

165 166 167
/* sched temporary for lsm_set_event_priority */
static int temp_prio = -1;

168 169 170 171 172 173 174
/*
 * lsm_lp
 *   - implements ROSS callback interfaces
 */
tw_lptype lsm_lp =
{
    (init_f) lsm_lp_init,
175
    (pre_run_f) NULL,
176 177
    (event_f) lsm_event,
    (revent_f) lsm_rev_event,
178
    (commit_f) NULL,
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    (final_f) lsm_finalize,
    (map_f) codes_mapping,
    sizeof(lsm_state_t)
};

static tw_stime transfer_time_table (lsm_state_t *ns,
                                     lsm_stats_t *stat,
                                     int rw,
                                     uint64_t object,
                                     int64_t offset,
                                     uint64_t size)
{
    double mb;
    double time = 0.0;
    double disk_rate;
    double disk_seek;
    double disk_overhead;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
196
    unsigned int i;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    /* find nearest size rounded down. */
    for (i = 0; i < ns->model->bins; i++)
    {
        if (ns->model->request_sizes[i] > size)
        {
            break;
        }
    }
    if (i > 0) i--;

    if (rw)
    {
        /* read */
        disk_rate = ns->model->read_rates[i];
        disk_seek = ns->model->read_seeks[i];
        disk_overhead = ns->model->read_overheads[i];
    }
    else
    {
        /* write */
        disk_rate = ns->model->write_rates[i];
        disk_seek = ns->model->write_seeks[i];
        disk_overhead = ns->model->write_overheads[i];

    }
223

224 225 226
    /* transfer time */
    mb = ((double)size) / (1024.0 * 1024.0);
    time += (mb / disk_rate) * 1000.0 * 1000.0 * 1000.0;
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    /* request overhead */
    time += (disk_overhead * 1000.0);

    /* seek */
    if ((object != ns->current_object) ||
        (offset < ns->current_offset) ||
        (offset > (ns->current_offset+512)))
    {
        if (rw) stat->read_seeks++; else stat->write_seeks++;
        time += (disk_seek * 1000.0);
    }


    /* update statistics */
    if (rw)
    {
        stat->read_count += 1;
        stat->read_bytes += size;
        stat->read_time  += time;
    }
    else
    {
        stat->write_count += 1;
        stat->write_bytes += size;
        stat->write_time  += time;
    }

    return time;
}

258
void lsm_io_event_rc(tw_lp *sender)
259 260
{
    codes_local_latency_reverse(sender);
261
}
262

263
tw_lpid lsm_find_local_device(
264 265 266 267
        struct codes_mctx const * map_ctx,
        tw_lpid sender_gid)
{
    return codes_mctx_to_lpid(map_ctx, LSM_NAME, sender_gid);
268 269
}

270 271
void lsm_io_event(
        const char * lp_io_category,
272 273 274 275 276
        uint64_t io_object,
        int64_t  io_offset,
        uint64_t io_size_bytes,
        int      io_type,
        tw_stime delay,
277 278 279 280 281
        tw_lp *sender,
        struct codes_mctx const * map_ctx,
        int return_tag,
        msg_header const * return_header,
        struct codes_cb_info const * cb)
282
{
283 284 285
    assert(strlen(lp_io_category) < CATEGORY_NAME_MAX-1);
    assert(strlen(lp_io_category) > 0);
    SANITY_CHECK_CB(cb, lsm_return_t);
286

287
    tw_lpid lsm_id = codes_mctx_to_lpid(map_ctx, LSM_NAME, sender->gid);
288

289
    tw_stime delta = delay + codes_local_latency(sender);
290
    if (lsm_in_sequence) {
291 292 293 294
        tw_stime tmp = lsm_msg_offset;
        lsm_msg_offset += delta;
        delta += tmp;
    }
295 296 297

    tw_event *e = tw_event_new(lsm_id, delta, sender);
    lsm_message_t *m = tw_event_data(e);
298
    m->magic = lsm_magic;
299
    m->event = (lsm_event_t) io_type;
300 301 302
    m->data.object = io_object;
    m->data.offset = io_offset;
    m->data.size   = io_size_bytes;
303
    strcpy(m->data.category, lp_io_category);
304 305

    // get the priority count for checking
306
    int num_prios = lsm_get_num_priorities(map_ctx, sender->gid);
307 308 309 310 311 312 313 314 315 316
    // prio checks and sets
    if (num_prios <= 0) // disabled scheduler - ignore
        m->data.prio = 0;
    else if (temp_prio < 0) // unprovided priority - defer to max possible
        m->data.prio = num_prios-1;
    else if (temp_prio < num_prios) // valid priority
        m->data.prio = temp_prio;
    else
        tw_error(TW_LOC,
                "LP %lu, LSM LP %lu: Bad priority (%d supplied, %d lanes)\n",
317
                sender->gid, lsm_id, temp_prio, num_prios);
318 319
    // reset temp_prio
    temp_prio = -1;
320

321 322 323
    m->cb.info = *cb;
    m->cb.h = *return_header;
    m->cb.tag = return_tag;
324

325
    tw_event_send(e);
326 327
}

328
int lsm_get_num_priorities(
329 330
        struct codes_mctx const * map_ctx,
        tw_lpid sender_id)
331
{
332 333 334 335 336 337
    char const * annotation =
        codes_mctx_get_annotation(map_ctx, LSM_NAME, sender_id);

    if (annotation == NULL) {
        assert(anno_map->has_unanno_lp);
        return model_unanno.use_sched;
338 339 340
    }
    else {
        for (int i = 0; i < anno_map->num_annos; i++) {
341
            if (strcmp(anno_map->annotations[i].ptr, annotation) == 0)
342 343
                return models_anno[i].use_sched;
        }
344
        assert(0);
345 346 347 348 349 350 351 352 353
        return -1;
    }
}

void lsm_set_event_priority(int prio)
{
    temp_prio = prio;
}

354 355 356 357 358 359 360 361 362 363 364
/*
 * lsm_lp_init
 *   - initialize the lsm model
 *   - sets the disk to be idle now
 */
static void lsm_lp_init (lsm_state_t *ns, tw_lp *lp)
{
    memset(ns, 0, sizeof(*ns));

    ns->next_idle = tw_now(lp);

365 366 367 368 369 370 371 372 373
    // set the correct model
    const char *anno = codes_mapping_get_annotation_by_lpid(lp->gid);
    if (anno == NULL)
        ns->model = &model_unanno;
    else {
        int id = configuration_get_annotation_index(anno, anno_map);
        ns->model = &models_anno[id];
    }

374 375 376 377
    // initialize the scheduler if need be
    ns->use_sched = ns->model->use_sched > 0;
    if (ns->use_sched) {
        ns->sched.num_prios = ns->model->use_sched;
378 379
        ns->sched.active_count = 0;
        rc_stack_create(&ns->sched.freelist);
380 381 382 383 384 385
        ns->sched.queues =
            malloc(ns->sched.num_prios * sizeof(*ns->sched.queues));
        for (int i = 0; i < ns->sched.num_prios; i++)
            INIT_QLIST_HEAD(&ns->sched.queues[i]);
    }

386 387 388 389 390 391 392 393
    return;
}

/*
 * lsm_event
 *   - event handler callback
 *   - dispatches the events to the appropriate handlers
 *   - handles initializtion of node state
394
 */
395 396 397 398 399 400 401 402 403 404 405
static void lsm_event (lsm_state_t *ns, tw_bf *b, lsm_message_t *m, tw_lp *lp)
{
    assert(m->magic == lsm_magic);

    switch (m->event)
    {
        case LSM_WRITE_REQUEST:
        case LSM_READ_REQUEST:
            if (LSM_DEBUG)
                printf("svr(%llu): REQUEST obj:%llu off:%llu size:%llu\n",
                    (unsigned long long)lp->gid,
406 407 408
                    (unsigned long long)m->data.object,
                    (unsigned long long)m->data.offset,
                    (unsigned long long)m->data.size);
409
            assert(ns->model);
410 411 412 413
            if (ns->use_sched)
                handle_io_sched_new(ns, b, m, lp);
            else
                handle_io_request(ns, b, &m->data, m, lp);
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 442 443 444 445 446 447 448 449
            break;
        case LSM_WRITE_COMPLETION:
        case LSM_READ_COMPLETION:
            if (LSM_DEBUG)
                printf("svr(%llu): COMPLETION\n",
                    (unsigned long long)lp->gid);
            handle_io_completion(ns, b, m, lp);
            break;
        default:
            printf("svr(%llu): Unknown Event:%d\n",
                (unsigned long long)lp->gid,
                m->event);
            break;
    }

    return;
}

/*
 * lsm_rev_event
 *   - callback to reverse an event
 */
static void lsm_rev_event(lsm_state_t *ns,
                          tw_bf *b,
                          lsm_message_t *m,
                          tw_lp *lp)
{
    assert(m->magic == lsm_magic);

    switch (m->event)
    {
        case LSM_WRITE_REQUEST:
        case LSM_READ_REQUEST:
            if (LSM_DEBUG)
                printf("svr(%llu): reverse REQUEST obj:%llu off:%llu size:%llu\n",
                    (unsigned long long)lp->gid,
450 451 452 453 454 455 456
                    (unsigned long long)m->data.object,
                    (unsigned long long)m->data.offset,
                    (unsigned long long)m->data.size);
            if (ns->use_sched)
                handle_rev_io_sched_new(ns, b, m, lp);
            else
                handle_rev_io_request(ns, b, &m->data, m, lp);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
            break;
        case LSM_WRITE_COMPLETION:
        case LSM_READ_COMPLETION:
            if (LSM_DEBUG)
                printf("svr(%llu): reverse COMPLETION\n",
                    (unsigned long long)lp->gid);
            handle_rev_io_completion(ns, b, m, lp);
            break;
        default:
            printf("svr(%llu): reverse Unknown Event:%d\n",
                (unsigned long long)lp->gid,
                m->event);
            break;
    }

    return;
}
474

475 476
/*
 * lsm_finalize
477
 *   - callback to release model resources
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
 */
static void lsm_finalize(lsm_state_t *ns,
                         tw_lp *lp)
{
    int i;
    lsm_stats_t all;

    memset(&all, 0, sizeof(all));
    sprintf(all.category, "all");

    for(i=0; i<CATEGORY_MAX; i++)
    {
        if(strlen(ns->lsm_stats_array[i].category) > 0)
        {
            all.write_count += ns->lsm_stats_array[i].write_count;
            all.write_bytes += ns->lsm_stats_array[i].write_bytes;
            all.write_time += ns->lsm_stats_array[i].write_time;
            all.write_seeks += ns->lsm_stats_array[i].write_seeks;
            all.read_count += ns->lsm_stats_array[i].read_count;
            all.read_bytes += ns->lsm_stats_array[i].read_bytes;
            all.read_seeks += ns->lsm_stats_array[i].read_seeks;
            all.read_time += ns->lsm_stats_array[i].read_time;

            write_stats(lp, &ns->lsm_stats_array[i]);
        }
    }

    write_stats(lp, &all);

    return;
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
static void handle_io_sched_new(
        lsm_state_t *ns,
        tw_bf *b,
        lsm_message_t *m_in,
        tw_lp *lp)
{
    if (LSM_DEBUG)
        printf("handle_io_sched_new called\n");
    // if nothing else is going on, then issue directly
    if (!ns->sched.active_count)
        handle_io_request(ns, b, &m_in->data, m_in, lp);
    else {
        lsm_sched_op_t *op = malloc(sizeof(*op));
        op->data = m_in->data;
        qlist_add_tail(&op->ql, &ns->sched.queues[m_in->prio]);
    }
    ns->sched.active_count++;
}

static void handle_rev_io_sched_new(
        lsm_state_t *ns,
        tw_bf *b,
        lsm_message_t *m_in,
        tw_lp *lp)
{
    if (LSM_DEBUG)
        printf("handle_rev_io_sched_new called\n");
    ns->sched.active_count--;
    if (!ns->sched.active_count)
        handle_rev_io_request(ns, b, &m_in->data, m_in, lp);
    else {
        struct qlist_head *ent = qlist_pop_back(&ns->sched.queues[m_in->prio]);
        assert(ent);
        lsm_sched_op_t *op = qlist_entry(ent, lsm_sched_op_t, ql);
        free(op);
    }
}

static void handle_io_sched_compl(
        lsm_state_t *ns,
        tw_bf *b,
        lsm_message_t *m_in,
        tw_lp *lp)
{
    if (LSM_DEBUG)
        printf("handle_io_sched_compl called\n");
    ns->sched.active_count--;
    if (ns->sched.active_count) {
        lsm_sched_op_t *next = NULL;
        struct qlist_head *ent = NULL;
        for (int i = 0; i < ns->sched.num_prios; i++) {
            ent = qlist_pop(&ns->sched.queues[i]);
            if (ent != NULL) {
                next = qlist_entry(ent, lsm_sched_op_t, ql);
                m_in->prio = i;
                break;
            }
        }
        assert(next);
        handle_io_request(ns, b, &next->data, m_in, lp);
        // now done with this request metadata
        rc_stack_push(lp, next, free, ns->sched.freelist);
    }
}

static void handle_rev_io_sched_compl(
        lsm_state_t *ns,
        tw_bf *b,
        lsm_message_t *m_in,
        tw_lp *lp)
{
    if (LSM_DEBUG)
        printf("handle_rev_io_sched_compl called\n");
    if (ns->sched.active_count) {
        lsm_sched_op_t *prev = rc_stack_pop(ns->sched.freelist);
        handle_rev_io_request(ns, b, &prev->data, m_in, lp);
        qlist_add_tail(&prev->ql, &ns->sched.queues[m_in->prio]);
    }
    ns->sched.active_count++;
}


592 593 594 595 596 597 598 599
/*
 * handle_io_request
 *   - handles the IO request events
 *   - computes the next_idle time
 *   - fires disk completion event at computed time
 */
static void handle_io_request(lsm_state_t *ns,
                              tw_bf *b,
600
                              lsm_message_data_t *data,
601 602 603
                              lsm_message_t *m_in,
                              tw_lp *lp)
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
604
    (void)b;
605 606 607 608 609 610 611 612 613 614
    tw_stime queue_time, t_time;
    tw_event *e;
    lsm_message_t *m_out;
    lsm_stats_t *stat;
    int rw = (m_in->event == LSM_READ_REQUEST) ? 1 : 0;

    tw_stime (*transfer_time) (lsm_state_t *, lsm_stats_t *, int, uint64_t, int64_t, uint64_t);

    transfer_time = transfer_time_table;

615
    stat = find_stats(data->category, ns);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

    /* save history for reverse operation */
    m_in->prev_idle   = ns->next_idle;
    m_in->prev_stat   = *stat;
    m_in->prev_object = ns->current_object;
    m_in->prev_offset = ns->current_offset;

    if (ns->next_idle > tw_now(lp))
    {
        queue_time = ns->next_idle - tw_now(lp);
    }
    else
    {
        queue_time = 0;
    }


    t_time = transfer_time(ns,
                           stat,
                           rw,
636 637 638
                           data->object,
                           data->offset,
                           data->size);
639
    queue_time += t_time;
640
    ns->next_idle = queue_time + tw_now(lp);
641 642
    ns->current_offset = data->offset + data->size;
    ns->current_object = data->object;
643

644
    e = tw_event_new(lp->gid, queue_time, lp);
645
    m_out = (lsm_message_t*)tw_event_data(e);
646

647
    memcpy(m_out, m_in, sizeof(*m_in));
648 649 650 651 652 653 654 655 656
    if (m_out->event == LSM_WRITE_REQUEST)
    {
        m_out->event = LSM_WRITE_COMPLETION;
    }
    else
    {
        m_out->event = LSM_READ_COMPLETION;
    }

657 658
    m_out->prio = m_in->prio;

659 660 661 662 663 664 665 666 667 668 669 670
    tw_event_send(e);

    return;
}


/*
 * handle_rev_io_request
 *   - handle reversing the io request
 */
static void handle_rev_io_request(lsm_state_t *ns,
                                  tw_bf *b,
671
                                  lsm_message_data_t *data,
672 673 674
                                  lsm_message_t *m_in,
                                  tw_lp *lp)
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
675 676
    (void)b;
    (void)lp;
677
    lsm_stats_t *stat;
678

679
    stat = find_stats(data->category, ns);
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

    ns->next_idle = m_in->prev_idle;
    *stat = m_in->prev_stat;
    ns->current_object = m_in->prev_object;
    ns->current_offset = m_in->prev_offset;

    return;
}

/*
 * handle_io_completion
 *   - handle IO completion events
 *   - invoke the callers original completion event
 */
static void handle_io_completion (lsm_state_t *ns,
                                  tw_bf *b,
                                  lsm_message_t *m_in,
                                  tw_lp *lp)
{
699 700 701 702
    SANITY_CHECK_CB(&m_in->cb.info, lsm_return_t);

    tw_event * e = tw_event_new(m_in->cb.h.src, codes_local_latency(lp), lp);
    void * m = tw_event_data(e);
703

704
    GET_INIT_CB_PTRS(&m_in->cb, m, lp->gid, h, tag, rc, lsm_return_t);
705

706 707
    /* no failures to speak of yet */
    rc->rc = 0;
708 709 710

    tw_event_send(e);

711 712 713 714
    // continue the loop
    if (ns->use_sched)
        handle_io_sched_compl(ns, b, m_in, lp);

715 716 717 718 719 720 721 722 723 724 725 726 727
    return;
}

/*
 * handle_rev_io_completion
 *   - reverse io completion event
 *   - currently nothing to do in this case
 */
static void handle_rev_io_completion (lsm_state_t *ns,
                                      tw_bf *b,
                                      lsm_message_t *m_in,
                                      tw_lp *lp)
{
728 729 730
    if (ns->use_sched)
        handle_rev_io_sched_compl(ns, b, m_in, lp);

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    codes_local_latency_reverse(lp);
    return;
}

static lsm_stats_t *find_stats(const char* category, lsm_state_t *ns)
{
    int i;
    int new_flag = 0;
    int found_flag = 0;

    for(i=0; i<CATEGORY_MAX; i++)
    {
        if(strlen(ns->lsm_stats_array[i].category) == 0)
        {
            found_flag = 1;
            new_flag = 1;
            break;
        }
        if(strcmp(category, ns->lsm_stats_array[i].category) == 0)
        {
            found_flag = 1;
            new_flag = 0;
            break;
        }
    }
    assert(found_flag);

    if(new_flag)
    {
        strcpy(ns->lsm_stats_array[i].category, category);
    }
    return(&ns->lsm_stats_array[i]);

}

static void write_stats(tw_lp* lp, lsm_stats_t* stat)
{
    int ret;
    char id[32];
    char data[1024];

    sprintf(id, "lsm-category-%s", stat->category);
773
    sprintf(data, "lp:%ld\twrite_count:%ld\twrite_bytes:%ld\twrite_seeks:%ld\twrite_time:%f\t"
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
        "read_count:%ld\tread_bytes:%ld\tread_seeks:%ld\tread_time:%f\n",
        (long)lp->gid,
        stat->write_count,
        stat->write_bytes,
        stat->write_seeks,
        stat->write_time,
        stat->read_count,
        stat->read_bytes,
        stat->read_seeks,
        stat->read_time);

    ret = lp_io_write(lp->gid, id, strlen(data), data);
    assert(ret == 0);

    return;

}

792
void lsm_register(void)
793 794 795 796 797 798
{
    uint32_t h1=0, h2=0;

    bj_hashlittle2("localstorage", strlen("localstorage"), &h1, &h2);
    lsm_magic = h1+h2;

799 800 801 802
    lp_type_register(LSM_NAME, &lsm_lp);
}

// read the configuration file for a given annotation
803
static void read_config(ConfigHandle *ch, char const * anno, disk_model_t *model)
804 805
{
    char       **values;
806
    size_t       length;
807 808 809 810 811
    int          rc;
    // request sizes
    rc = configuration_get_multivalue(ch, LSM_NAME, "request_sizes", anno,
            &values,&length);
    assert(rc == 1);
812
    model->request_sizes = (unsigned int*)malloc(sizeof(int)*length);
813 814
    assert(model->request_sizes);
    model->bins = length;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
815
    for (size_t i = 0; i < length; i++)
816 817 818 819 820 821 822 823 824
    {
        model->request_sizes[i] = atoi(values[i]);
    }
    free(values);

    // write rates
    rc = configuration_get_multivalue(ch, LSM_NAME, "write_rates", anno,
            &values,&length);
    assert(rc == 1);
825
    model->write_rates = (double*)malloc(sizeof(double)*length);
826 827
    assert(model->write_rates);
    assert(length == model->bins);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
828
    for (size_t i = 0; i < length; i++)
829 830 831 832 833 834 835 836 837
    {
        model->write_rates[i] = strtod(values[i], NULL);
    }
    free(values);

    // read rates
    rc = configuration_get_multivalue(ch, LSM_NAME, "read_rates", anno,
            &values,&length);
    assert(rc == 1);
838
    model->read_rates = (double*)malloc(sizeof(double)*length);
839 840
    assert(model->read_rates);
    assert(model->bins == length);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
841
    for (size_t i = 0; i < length; i++)
842 843 844 845 846 847 848 849 850
    {
        model->read_rates[i] = strtod(values[i], NULL);
    }
    free(values);

    // write overheads
    rc = configuration_get_multivalue(ch, LSM_NAME, "write_overheads", anno,
            &values,&length);
    assert(rc == 1);
851
    model->write_overheads = (double*)malloc(sizeof(double)*length);
852 853
    assert(model->write_overheads);
    assert(model->bins == length);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
854
    for (size_t i = 0; i < length; i++)
855 856 857 858 859 860 861 862 863
    {
        model->write_overheads[i] = strtod(values[i], NULL);
    }
    free(values);

    // read overheades
    rc = configuration_get_multivalue(ch, LSM_NAME, "read_overheads", anno,
            &values,&length);
    assert(rc == 1);
864
    model->read_overheads = (double*)malloc(sizeof(double)*length);
865 866
    assert(model->read_overheads);
    assert(model->bins == length);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
867
    for (size_t i = 0; i < length; i++)
868 869 870 871 872 873 874 875 876
    {
        model->read_overheads[i] = strtod(values[i], NULL);
    }
    free(values);

    // write seek latency
    rc = configuration_get_multivalue(ch, LSM_NAME, "write_seeks", anno,
            &values,&length);
    assert(rc == 1);
877
    model->write_seeks = (double*)malloc(sizeof(double)*length);
878 879
    assert(model->write_seeks);
    assert(model->bins == length);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
880
    for (size_t i = 0; i < length; i++)
881 882 883 884 885 886 887 888 889
    {
        model->write_seeks[i] = strtod(values[i], NULL);
    }
    free(values);

    // read seek latency
    rc = configuration_get_multivalue(ch, LSM_NAME, "read_seeks", anno,
            &values,&length);
    assert(rc == 1);
890
    model->read_seeks = (double*)malloc(sizeof(double)*length);
891 892
    assert(model->read_seeks);
    assert(model->bins == length);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
893
    for (size_t i = 0; i < length; i++)
894 895 896 897
    {
        model->read_seeks[i] = strtod(values[i], NULL);
    }
    free(values);
898 899 900 901 902

    // scheduling parameters (this can fail)
    configuration_get_value_int(ch, LSM_NAME, "enable_scheduler", anno,
            &model->use_sched);
    assert(model->use_sched >= 0);
903 904 905 906
}

void lsm_configure(void)
{
907 908 909 910 911
    /* check and see if any lsm LPs are being used - otherwise,
     * skip the config */
    if (0 == codes_mapping_get_lp_count(NULL, 0, LSM_NAME, NULL, 1))
        return;

912 913
    anno_map = codes_mapping_get_lp_anno_map(LSM_NAME);
    assert(anno_map);
914
    models_anno = (disk_model_t*)malloc(anno_map->num_annos * sizeof(*models_anno));
915

916
    // read the configuration for unannotated entries
917
    if (anno_map->has_unanno_lp > 0){
918 919 920
        read_config(&config, NULL, &model_unanno);
    }

Jonathan Jenkins's avatar
Jonathan Jenkins committed
921
    for (int i = 0; i < anno_map->num_annos; i++){
922
        char const * anno = anno_map->annotations[i].ptr;
923 924
        read_config(&config, anno, &models_anno[i]);
    }
925 926 927 928 929 930 931 932 933 934
}

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