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

/* A scheduler interface for use by model-net. */

#include <assert.h>
#include <ross.h>

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

16 17
enum sched_type mn_sched_type = -1;

18 19 20 21 22 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/// scheduler-specific data structures (TODO: split specific schedulers into
/// their own files if we move beyond just these two)
/// NOTE: for now, scheduler data structures are the same - this may change in
/// later versions

typedef struct mn_sched {
    // method containing packet event to call
    struct model_net_method *method;
    struct qlist_head reqs; // of type mn_sched_qitem
    // this is an unfortunate result - we have to basically not free anything
    // in order to keep around the remote and local events
    // we desperately need GVT hooks to run our own garbage collection
    struct qlist_head free_reqs;
} mn_sched;

// at the moment, rr and fcfs only differ in how req queue is modified, queue
// items themselves are equivalent

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;

/// scheduler-specific function decls and function table

// void used to avoid ptr-to-ptr conv warnings
static void fcfs_init (struct model_net_method *method, void ** sched);
static void fcfs_destroy (void *sched);
static void fcfs_add (
        model_net_request *req, 
        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 fcfs_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp);
static int  fcfs_next(tw_stime *poffset, void *sched, model_net_sched_rc *rc, tw_lp *lp);
static void fcfs_next_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp);

static model_net_sched_interface fcfs_tab = 
{ &fcfs_init, &fcfs_destroy, &fcfs_add, &fcfs_add_rc, &fcfs_next, &fcfs_next_rc};

static void rr_init (struct model_net_method *method, void ** sched);
static void rr_destroy (void *sched);
static void rr_add (
        model_net_request *req,
        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 rr_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp);
static int  rr_next(tw_stime *poffset, void *sched, model_net_sched_rc *rc, tw_lp *lp);
static void rr_next_rc (void *sched, model_net_sched_rc *rc, tw_lp *lp);

static model_net_sched_interface rr_tab = 
{ &rr_init, &rr_destroy, &rr_add, &rr_add_rc, &rr_next, &rr_next_rc};

/// general scheduler functions

void model_net_sched_init(
        enum sched_type type, 
        struct model_net_method *method,
        model_net_sched *sched){
    sched->type = type;
    switch (type){
        case MN_SCHED_FCFS:
            sched->impl = &fcfs_tab;
            break;
        case MN_SCHED_RR:
            sched->impl = &rr_tab;
            break;
        default:
            fprintf(stderr, "unknown scheduler type");
            abort();
    }
    sched->impl->init(method, &sched->dat);
}

int model_net_sched_next(
        tw_stime *poffset,
        model_net_sched *sched,
        model_net_sched_rc *sched_rc,
        tw_lp *lp){
    return sched->impl->next(poffset, sched->dat, sched_rc, lp);
}

void model_net_sched_next_rc(
        model_net_sched *sched,
        model_net_sched_rc *sched_rc,
        tw_lp *lp) {
    sched->impl->next_rc(sched->dat, sched_rc, lp);
}

void model_net_sched_add(
        model_net_request *req,
        int remote_event_size,
        void * remote_event,
        int local_event_size,
        void * local_event,
        model_net_sched *sched,
        model_net_sched_rc *sched_rc,
        tw_lp *lp){
    sched->impl->add(req, remote_event_size, remote_event, local_event_size,
            local_event, sched->dat, sched_rc, lp);
}

void model_net_sched_add_rc(
        model_net_sched *sched,
        model_net_sched_rc *sched_rc,
        tw_lp *lp){
    sched->impl->add_rc(sched->dat, sched_rc, lp);
}

/// specific scheduler implementations

void fcfs_init(struct model_net_method *method, void ** sched){
    *sched = malloc(sizeof(mn_sched));
    mn_sched *ss = *sched;
    ss->method = method;
    INIT_QLIST_HEAD(&ss->reqs);
    INIT_QLIST_HEAD(&ss->free_reqs);
}

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

void fcfs_add (
        model_net_request *req, 
        int remote_event_size,
        void * remote_event,
        int local_event_size,
        void * local_event,
        void *sched, 
        model_net_sched_rc *rc, 
        tw_lp *lp){
165 166 167 168
    // 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.
169
    mn_sched_qitem *q = malloc(sizeof(mn_sched_qitem));
170 171
    assert(q);
    memset(q, 0, sizeof(*q));
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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 223 224 225 226 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    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);
    }
    if (local_event_size > 0){
        q->local_event = malloc(local_event_size);
        memcpy(q->local_event, local_event, local_event_size);
    }
    mn_sched *s = sched;
    qlist_add_tail(&q->ql, &s->reqs);
}

void fcfs_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp){
    mn_sched *s = sched;
    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);
}

int fcfs_next(tw_stime *poffset, void *sched, model_net_sched_rc *rc, tw_lp *lp){
    mn_sched *s = sched;
    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;
    }

    *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);

    // if last packet - remove from list, put into free list
    if (is_last_packet){
        qlist_pop(&s->reqs);
        qlist_add_tail(&q->ql, &s->free_reqs);
        rc->rtn = 1;
    }
    else{
        q->rem -= psize;
        rc->rtn = 0;
    }
    return rc->rtn;
}

void fcfs_next_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp){
    mn_sched *s = sched;
    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){
            qlist_add(qlist_pop_back(&s->free_reqs), &s->reqs);
        }
        else {
            assert(0);
        }
    }
}

void rr_init (struct model_net_method *method, void ** sched){
    *sched = malloc(sizeof(mn_sched));
    mn_sched *ss = *sched;
    ss->method = method;
    INIT_QLIST_HEAD(&ss->reqs);
    INIT_QLIST_HEAD(&ss->free_reqs);
}

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

void rr_add (
        model_net_request *req,
        int remote_event_size,
        void * remote_event,
        int local_event_size,
        void * local_event,
        void *sched,
        model_net_sched_rc *rc,
        tw_lp *lp){
278 279 280 281
    // 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.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    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);
    }
    if (local_event_size > 0){
        q->local_event = malloc(local_event_size);
        memcpy(q->local_event, local_event, local_event_size);
    }
    mn_sched *s = sched;
    qlist_add_tail(&q->ql, &s->reqs);
}
void rr_add_rc(void *sched, model_net_sched_rc *rc, tw_lp *lp){
    mn_sched *s = sched;
    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);
}

int rr_next(tw_stime *poffset, void *sched, model_net_sched_rc *rc, tw_lp *lp){
    mn_sched *s = sched;
    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;
    }

    *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);

    // if last packet - remove from list, put into free list
    if (is_last_packet){
        qlist_add_tail(&q->ql, &s->free_reqs);
        rc->rtn = 1;
    }
    else{
        q->rem -= psize;
        qlist_add_tail(&q->ql, &s->reqs);
        rc->rtn = 0;
    }
    return rc->rtn;
}

void rr_next_rc (void *sched, model_net_sched_rc *rc, tw_lp *lp){
    mn_sched *s = sched;
    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;
        }
        else if (rc->rtn == 1){
            // put back to *front* of list. We know it's the front because it was
            // in the front when it was deleted
            qlist_add(qlist_pop_back(&s->free_reqs), &s->reqs);
        }
        else {
            assert(0);
        }
    }
}

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