model-net-sched-impl.c 19.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#include <stdlib.h>
#include <assert.h>

#include "model-net-sched-impl.h"
#include "codes/model-net-sched.h"
#include "codes/model-net-method.h"
#include "codes/quicklist.h"

15 16 17 18 19 20 21 22
#define MN_SCHED_DEBUG_VERBOSE 0

#if MN_SCHED_DEBUG_VERBOSE
#define dprintf(_fmt, ...) printf(_fmt, __VA_ARGS__);
#else
#define dprintf(_fmt, ...)
#endif

23 24 25 26 27 28 29 30 31 32 33 34 35
/// scheduler-specific data structures 

typedef struct mn_sched_qitem {
    model_net_request req;
    // remaining bytes to send
    uint64_t rem;
    // pointers to event structures 
    // sizes are given in the request struct
    void * remote_event;
    void * local_event;
    struct qlist_head ql;
} mn_sched_qitem;

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// fcfs and round-robin each use a single queue
typedef struct mn_sched_queue {
    // method containing packet event to call
    const struct model_net_method *method;
    struct qlist_head reqs; // of type mn_sched_qitem
} mn_sched_queue;

// priority scheduler consists of a bunch of rr/fcfs queues
typedef struct mn_sched_prio {
    mn_prio_params params;
    const model_net_sched_interface *sub_sched_iface;
    mn_sched_queue ** sub_scheds; // one for each params.num_prios
} mn_sched_prio;

/// scheduler-specific function decls and tables
51 52 53

/// FCFS
// void used to avoid ptr-to-ptr conv warnings
54 55 56 57
static void fcfs_init (
        const struct model_net_method     * method, 
        const model_net_sched_cfg_params  * params,
        void                             ** sched);
58 59
static void fcfs_destroy (void *sched);
static void fcfs_add (
60 61 62 63 64 65 66 67 68
        model_net_request  * req,
        void               * sched_msg_params,
        int                  remote_event_size,
        void               * remote_event,
        int                  local_event_size,
        void               * local_event,
        void               * sched,
        model_net_sched_rc * rc,
        tw_lp              * lp);
69
static void fcfs_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp);
70 71 72 73 74 75 76 77 78 79 80
static int  fcfs_next(
        tw_stime           * poffset,
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp);
static void fcfs_next_rc(
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp);
81

82 83 84 85 86
// ROUND-ROBIN
static void rr_init (
        const struct model_net_method     * method, 
        const model_net_sched_cfg_params  * params,
        void                             ** sched);
87 88
static void rr_destroy (void *sched);
static void rr_add (
89 90 91 92 93 94 95 96 97
        model_net_request  * req,
        void               * sched_msg_params,
        int                  remote_event_size,
        void               * remote_event,
        int                  local_event_size,
        void               * local_event,
        void               * sched,
        model_net_sched_rc * rc,
        tw_lp              * lp);
98
static void rr_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static int  rr_next(
        tw_stime           * poffset,
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp);
static void rr_next_rc (
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp);
static void prio_init (
        const struct model_net_method     * method, 
        const model_net_sched_cfg_params  * params,
        void                             ** sched);
static void prio_destroy (void *sched);
static void prio_add (
        model_net_request  * req,
        void               * sched_msg_params,
        int                  remote_event_size,
        void               * remote_event,
        int                  local_event_size,
        void               * local_event,
        void               * sched,
        model_net_sched_rc * rc,
        tw_lp              * lp);
static void prio_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp);
static int  prio_next(
        tw_stime           * poffset,
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp);
static void prio_next_rc (
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp);
137 138

/// function tables (names defined by X macro in model-net-sched.h)
139
static const model_net_sched_interface fcfs_tab = 
140
{ &fcfs_init, &fcfs_destroy, &fcfs_add, &fcfs_add_rc, &fcfs_next, &fcfs_next_rc};
141
static const model_net_sched_interface rr_tab = 
142
{ &rr_init, &rr_destroy, &rr_add, &rr_add_rc, &rr_next, &rr_next_rc};
143 144
static const model_net_sched_interface prio_tab =
{ &prio_init, &prio_destroy, &prio_add, &prio_add_rc, &prio_next, &prio_next_rc};
145 146

#define X(a,b,c) c,
147
const model_net_sched_interface * sched_interfaces[] = {
148 149 150 151 152 153
    SCHEDULER_TYPES
};
#undef X

/// FCFS implementation 

154 155 156 157 158 159
void fcfs_init(
        const struct model_net_method     * method, 
        const model_net_sched_cfg_params  * params,
        void                             ** sched){
    *sched = malloc(sizeof(mn_sched_queue));
    mn_sched_queue *ss = *sched;
160 161 162 163 164 165 166 167 168
    ss->method = method;
    INIT_QLIST_HEAD(&ss->reqs);
}

void fcfs_destroy(void *sched){
    free(sched);
}

void fcfs_add (
169 170 171 172 173 174 175 176 177
        model_net_request  * req,
        void               * sched_msg_params,
        int                  remote_event_size,
        void               * remote_event,
        int                  local_event_size,
        void               * local_event,
        void               * sched,
        model_net_sched_rc * rc,
        tw_lp              * lp){
178 179 180 181 182 183 184 185 186 187 188 189
    // NOTE: in optimistic mode, we currently do not have a good way to
    // reliably free and re-initialize the q item and the local/remote events
    // when processing next/next_rc events. Hence, the memory leaks. Later on
    // we'll figure out a better way to handle this.
    mn_sched_qitem *q = malloc(sizeof(mn_sched_qitem));
    assert(q);
    q->req = *req;
    q->rem = req->is_pull ? PULL_MSG_SIZE : req->msg_size;
    if (remote_event_size > 0){
        q->remote_event = malloc(remote_event_size);
        memcpy(q->remote_event, remote_event, remote_event_size);
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
190
    else { q->remote_event = NULL; }
191 192 193 194
    if (local_event_size > 0){
        q->local_event = malloc(local_event_size);
        memcpy(q->local_event, local_event, local_event_size);
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
195
    else { q->local_event = NULL; }
196
    mn_sched_queue *s = sched;
197 198 199 200
    qlist_add_tail(&q->ql, &s->reqs);
}

void fcfs_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp){
201
    mn_sched_queue *s = sched;
202 203 204
    struct qlist_head *ent = qlist_pop_back(&s->reqs);
    assert(ent != NULL);
    mn_sched_qitem *q = qlist_entry(ent, mn_sched_qitem, ql);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
205 206 207
    // free'ing NULLs is a no-op 
    free(q->remote_event);
    free(q->local_event);
208 209 210
    free(q);
}

211 212 213 214 215 216 217
int fcfs_next(
        tw_stime           * poffset,
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    mn_sched_queue *s = sched;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    struct qlist_head *ent = s->reqs.next;
    if (ent == &s->reqs){
        rc->rtn = -1;
        return -1;
    }
    mn_sched_qitem *q = qlist_entry(ent, mn_sched_qitem, ql);
    
    // issue the next packet
    int is_last_packet;
    uint64_t psize;
    if (q->req.packet_size >= q->rem) {
        psize = q->rem;
        is_last_packet = 1;
    }
    else{
        psize = q->req.packet_size;
        is_last_packet = 0;
    }

237 238
    dprintf("%lu (mn):    issuing packet of size %lu (of %lu) from %lu to %lu\n",
            lp->gid, psize, q->rem, q->req.src_lp, q->req.final_dest_lp);
239 240 241 242 243
    *poffset = s->method->model_net_method_packet_event(q->req.category,
            q->req.final_dest_lp, psize, q->req.is_pull, q->req.msg_size, 0.0,
            q->req.remote_event_size, q->remote_event, q->req.self_event_size,
            q->local_event, q->req.src_lp, lp, is_last_packet);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
244
    // if last packet - remove from list, free, save for rc
245 246
    if (is_last_packet){
        qlist_pop(&s->reqs);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
247 248 249 250 251 252 253 254 255 256 257 258
        rc->req = q->req;
        void *e_dat = rc_event_save;
        if (q->req.remote_event_size > 0){
            memcpy(e_dat, q->remote_event, q->req.remote_event_size);
            e_dat = (char*) e_dat + q->req.remote_event_size;
            free(q->remote_event);
        }
        if (q->req.self_event_size > 0){
            memcpy(e_dat, q->local_event, q->req.self_event_size);
            free(q->local_event);
        }
        free(q);
259 260 261 262 263 264 265 266 267
        rc->rtn = 1;
    }
    else{
        q->rem -= psize;
        rc->rtn = 0;
    }
    return rc->rtn;
}

268 269 270 271 272 273
void fcfs_next_rc(
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    mn_sched_queue *s = sched;
274 275 276 277 278 279 280 281 282 283 284 285
    if (rc->rtn == -1){
        // no op
    }
    else{
        s->method->model_net_method_packet_event_rc(lp);
        if (rc->rtn == 0){
            // just get the front and increment rem
            mn_sched_qitem *q = qlist_entry(s->reqs.next, mn_sched_qitem, ql);
            // just increment rem
            q->rem += q->req.packet_size;
        }
        else if (rc->rtn == 1){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
286 287 288 289
            // re-create the q item
            mn_sched_qitem *q = malloc(sizeof(mn_sched_qitem));
            assert(q);
            q->req = rc->req;
290 291
            q->rem = (q->req.is_pull ? PULL_MSG_SIZE : q->req.msg_size) % 
                q->req.packet_size;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
292 293 294 295 296 297 298 299 300
            if (q->rem == 0){ // processed exactly a packet's worth of data
                q->rem = q->req.packet_size;
            }
            void * e_dat = rc_event_save;
            if (q->req.remote_event_size > 0){
                q->remote_event = malloc(q->req.remote_event_size);
                memcpy(q->remote_event, e_dat, q->req.remote_event_size);
                e_dat = (char*) e_dat + q->req.remote_event_size;
            }
301
            else { q->remote_event = NULL; }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
302 303 304 305
            if (q->req.self_event_size > 0) {
                q->local_event = malloc(q->req.self_event_size);
                memcpy(q->local_event, e_dat, q->req.self_event_size);
            }
306
            else { q->local_event = NULL; }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
307 308
            // add back to front of list
            qlist_add(&q->ql, &s->reqs);
309 310 311 312 313 314 315
        }
        else {
            assert(0);
        }
    }
}

316 317 318 319 320 321
void rr_init (
        const struct model_net_method     * method, 
        const model_net_sched_cfg_params  * params,
        void                             ** sched){
    // same underlying representation
    fcfs_init(method, params, sched);
322 323 324
}

void rr_destroy (void *sched){
325 326
    // same underlying representation
    fcfs_destroy(sched);
327 328 329
}

void rr_add (
330 331 332 333 334 335 336 337 338
        model_net_request  * req,
        void               * sched_msg_params,
        int                  remote_event_size,
        void               * remote_event,
        int                  local_event_size,
        void               * local_event,
        void               * sched,
        model_net_sched_rc * rc,
        tw_lp              * lp){
339 340 341 342 343 344 345 346 347 348 349
    // NOTE: in optimistic mode, we currently do not have a good way to
    // reliably free and re-initialize the q item and the local/remote events
    // when processing next/next_rc events. Hence, the memory leaks. Later on
    // we'll figure out a better way to handle this.
    mn_sched_qitem *q = malloc(sizeof(mn_sched_qitem));
    q->req = *req;
    q->rem = req->is_pull ? PULL_MSG_SIZE : req->msg_size;
    if (remote_event_size > 0){
        q->remote_event = malloc(remote_event_size);
        memcpy(q->remote_event, remote_event, remote_event_size);
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
350
    else { q->remote_event = NULL; }
351 352 353 354
    if (local_event_size > 0){
        q->local_event = malloc(local_event_size);
        memcpy(q->local_event, local_event, local_event_size);
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
355
    else{ q->local_event = NULL; }
356
    mn_sched_queue *s = sched;
357 358 359
    qlist_add_tail(&q->ql, &s->reqs);
}
void rr_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp){
360
    mn_sched_queue *s = sched;
361 362 363 364 365 366 367 368
    struct qlist_head *ent = qlist_pop_back(&s->reqs);
    assert(ent != NULL);
    mn_sched_qitem *q = qlist_entry(ent, mn_sched_qitem, ql);
    if (q->remote_event) free(q->remote_event);
    if (q->local_event)  free(q->local_event);
    free(q);
}

369 370 371 372 373 374 375
int rr_next(
        tw_stime           * poffset,
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    mn_sched_queue *s = sched;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    struct qlist_head *ent = qlist_pop(&s->reqs);
    if (ent == NULL){
        rc->rtn = -1;
        return -1;
    }
    mn_sched_qitem *q = qlist_entry(ent, mn_sched_qitem, ql);

    // issue the next packet
    int is_last_packet;
    uint64_t psize;
    if (q->req.packet_size >= q->rem) {
        psize = q->rem;
        is_last_packet = 1;
    }
    else{
        psize = q->req.packet_size;
        is_last_packet = 0;
    }

395 396
    dprintf("%lu (mn):    issuing packet of size %lu (of %lu) from %lu to %lu\n",
            lp->gid, psize, q->rem, q->req.src_lp, q->req.final_dest_lp);
397 398 399 400 401
    *poffset = s->method->model_net_method_packet_event(q->req.category,
            q->req.final_dest_lp, psize, q->req.is_pull, q->req.msg_size, 0.0,
            q->req.remote_event_size, q->remote_event, q->req.self_event_size,
            q->local_event, q->req.src_lp, lp, is_last_packet);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
402
    // if last packet - remove from list, free, save for rc
403
    if (is_last_packet){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
404 405 406 407 408 409 410 411 412 413 414 415
        rc->req = q->req;
        void *e_dat = rc_event_save;
        if (q->req.remote_event_size > 0){
            memcpy(e_dat, q->remote_event, q->req.remote_event_size);
            e_dat = (char*) e_dat + q->req.remote_event_size;
            free(q->remote_event);
        }
        if (q->req.self_event_size > 0){
            memcpy(e_dat, q->local_event, q->req.self_event_size);
            free(q->local_event);
        }
        free(q);
416 417 418 419 420 421 422 423 424 425
        rc->rtn = 1;
    }
    else{
        q->rem -= psize;
        qlist_add_tail(&q->ql, &s->reqs);
        rc->rtn = 0;
    }
    return rc->rtn;
}

426 427 428 429 430 431
void rr_next_rc (
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    mn_sched_queue *s = sched;
432 433 434 435 436 437 438 439 440 441 442
    if (rc->rtn == -1){
        // no op
    }
    else {
        s->method->model_net_method_packet_event_rc(lp);
        if (rc->rtn == 0){
            // increment rem and put item back to front of list
            struct qlist_head *ent = qlist_pop_back(&s->reqs);
            qlist_add(ent, &s->reqs);
            mn_sched_qitem *q = qlist_entry(ent, mn_sched_qitem, ql);
            q->rem += q->req.packet_size;
443 444 445 446
            dprintf("%lu (mn): rc issuing packet of size %lu (of %lu) from "
                    "%lu to %lu\n",
                    lp->gid, q->req.packet_size, q->rem, q->req.src_lp,
                    q->req.final_dest_lp);
447 448
        }
        else if (rc->rtn == 1){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
449 450 451 452
            // re-create the q item
            mn_sched_qitem *q = malloc(sizeof(mn_sched_qitem));
            assert(q);
            q->req = rc->req;
453 454
            q->rem = (q->req.is_pull ? PULL_MSG_SIZE : q->req.msg_size) % 
                q->req.packet_size;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
455 456 457 458 459 460 461 462 463
            if (q->rem == 0){ // processed exactly a packet's worth of data
                q->rem = q->req.packet_size;
            }
            void * e_dat = rc_event_save;
            if (q->req.remote_event_size > 0){
                q->remote_event = malloc(q->req.remote_event_size);
                memcpy(q->remote_event, e_dat, q->req.remote_event_size);
                e_dat = (char*) e_dat + q->req.remote_event_size;
            }
464
            else { q->remote_event = NULL; }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
465 466 467 468
            if (q->req.self_event_size > 0) {
                q->local_event = malloc(q->req.self_event_size);
                memcpy(q->local_event, e_dat, q->req.self_event_size);
            }
469
            else { q->local_event = NULL; }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
470 471
            // add back to front of list
            qlist_add(&q->ql, &s->reqs);
472 473 474 475
            dprintf("%lu (mn): rc issuing packet of size %lu (of %lu) from "
                    "%lu to %lu\n",
                    lp->gid, q->rem, q->rem, q->req.src_lp,
                    q->req.final_dest_lp);
476 477 478 479 480 481 482
        }
        else {
            assert(0);
        }
    }
}

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 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
void prio_init (
        const struct model_net_method     * method, 
        const model_net_sched_cfg_params  * params,
        void                             ** sched){
    *sched = malloc(sizeof(mn_sched_prio));
    mn_sched_prio *ss = *sched;
    ss->params = params->u.prio;
    ss->sub_scheds = malloc(ss->params.num_prios*sizeof(mn_sched_queue*));
    ss->sub_sched_iface = sched_interfaces[ss->params.sub_stype];
    for (int i = 0; i < ss->params.num_prios; i++){
        ss->sub_sched_iface->init(method, params, (void**)&ss->sub_scheds[i]);
    }
}

void prio_destroy (void *sched){
    mn_sched_prio *ss = sched;
    for (int i = 0; i < ss->params.num_prios; i++){
        ss->sub_sched_iface->destroy(ss->sub_scheds[i]);
        free(ss->sub_scheds);
        free(ss);
    }
}

void prio_add (
        model_net_request  * req,
        void               * sched_msg_params,
        int                  remote_event_size,
        void               * remote_event,
        int                  local_event_size,
        void               * local_event,
        void               * sched,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    // sched_msg_params is simply an int
    mn_sched_prio *ss = sched;
    int prio = *(int*) sched_msg_params;
    if (prio == -1){
        // default prio - lowest possible 
        prio = ss->params.num_prios-1;
    }
    else if (prio >= ss->params.num_prios){
        tw_error(TW_LOC, "sched for lp %lu: invalid prio (%d vs [%d,%d))",
                lp->gid, prio, 0, ss->params.num_prios);
    }
    //NOTE: currently no support for sub-scheduler parameters
    ss->sub_sched_iface->add(req, NULL, remote_event_size, remote_event,
            local_event_size, local_event, ss->sub_scheds[prio], rc, lp);
    rc->prio = prio;
}

void prio_add_rc(void * sched, model_net_sched_rc *rc, tw_lp *lp){
    // just call the sub scheduler's add_rc
    mn_sched_prio *ss = sched;
    ss->sub_sched_iface->add_rc(ss->sub_scheds[rc->prio], rc, lp);
}

int prio_next(
        tw_stime           * poffset,
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    // check each priority, first one that's non-empty gets the next
    mn_sched_prio *ss = sched;
    for (int i = 0; i < ss->params.num_prios; i++){
        // TODO: this works for now while the other schedulers have the same
        // internal representation
        if (!qlist_empty(&ss->sub_scheds[i]->reqs)){
            rc->prio = i;
            return ss->sub_sched_iface->next(
                    poffset, ss->sub_scheds[i], rc_event_save, rc, lp);
        }
    }
    rc->prio = -1;
    return -1; // all sub schedulers had no work 
}

void prio_next_rc (
        void               * sched,
        void               * rc_event_save,
        model_net_sched_rc * rc,
        tw_lp              * lp){
    if (rc->prio != -1){
        // we called a next somewhere
        mn_sched_prio *ss = sched;
        ss->sub_sched_iface->next_rc(ss->sub_scheds[rc->prio], rc_event_save,
                rc, lp);
    }
    // else, no-op
}

574 575 576 577 578 579 580 581
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */