resource-lp.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 165 166 167 168 169 170 171 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 278 279 280 281 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
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
*/

#include "codes/resource-lp.h"
#include "codes/resource.h"
#include "codes/codes_mapping.h"
#include "codes/jenkins-hash.h"
#include "ross.h"
#include <assert.h>
#include <stdio.h>


/**** BEGIN SIMULATION DATA STRUCTURES ****/

static int resource_magic; /* use this as sanity check on events */
/* TODO: we currently use a single config value to initialize the resource unit
 * count for all resources in the system. Later on, we'll want to do this on a
 * per-group basis */
static uint64_t avail_global;

typedef struct resource_state resource_state;
typedef struct resource_msg resource_msg;

#define TOKEN_DUMMY ((resource_token_t)-1)

/* event types */
enum resource_event
{
    RESOURCE_INIT = 100,
    RESOURCE_GET,
    RESOURCE_FREE,
    RESOURCE_RESERVE,
    RESOURCE_GET_RESERVED,
    RESOURCE_FREE_RESERVED
};

struct resource_state {
    resource r;
    int is_init;
};

struct resource_msg {
    msg_header h;
    /* request data */
    uint64_t req;
    resource_token_t tok; /* only for reserved calls */
    /* callback data */
    msg_header h_callback;
    int msg_size;
    int msg_header_offset;
    int msg_callback_offset;
};

/**** END SIMULATION DATA STRUCTURES ****/

/**** BEGIN LP, EVENT PROCESSING FUNCTION DECLS ****/

/* ROSS LP processing functions */  
static void resource_lp_ind_init(
    resource_state * ns,
    tw_lp * lp);
static void resource_event_handler(
    resource_state * ns,
    tw_bf * b,
    resource_msg * m,
    tw_lp * lp);
static void resource_rev_handler(
    resource_state * ns,
    tw_bf * b,
    resource_msg * m,
    tw_lp * lp);
static void resource_finalize(
    resource_state * ns,
    tw_lp * lp);

/* ROSS function pointer table for this LP */
static tw_lptype resource_lp = {
     (init_f) resource_lp_ind_init,
     (event_f) resource_event_handler,
     (revent_f) resource_rev_handler,
     (final_f)  resource_finalize, 
     (map_f) codes_mapping,
     sizeof(resource_state),
};

/**** END LP, EVENT PROCESSING FUNCTION DECLS ****/

/**** BEGIN IMPLEMENTATIONS ****/

void resource_lp_ind_init(
        resource_state * ns,
        tw_lp * lp){
    /* currently use global to initialize, may need to have other LPs init */
    ns->is_init = 1;
    resource_init(avail_global, &ns->r);
}

void resource_event_handler(
        resource_state * ns,
        tw_bf * b,
        resource_msg * m,
        tw_lp * lp){
    assert(m->h.magic == resource_magic);
    
    int send_ack = 0;
    int ret;
    resource_token_t tok = TOKEN_DUMMY;
    switch (m->h.event_type){
        case RESOURCE_INIT:
            assert(0);/* this should not be called */
            assert(ns->is_init == 0);
            resource_init(m->req, &ns->r);
            ns->is_init = 1;
            break;
        case RESOURCE_GET:
            assert(ns->is_init);
            ret = resource_get(m->req, &ns->r);
            send_ack = 1;
            break;
        case RESOURCE_FREE:
            assert(ns->is_init);
            resource_free(m->req, &ns->r);
            break;
        case RESOURCE_RESERVE:
            assert(ns->is_init);
            ret = resource_reserve(m->req, &tok, &ns->r);
            send_ack = 1;
            /* even though we don't expect reserve to RC, set the tok in the
             * message anyways */
            m->tok = tok;
            break;
        case RESOURCE_GET_RESERVED:
            assert(ns->is_init);
            ret = reserved_get(m->req, m->tok, &ns->r);
            send_ack = 1;
            break;
        case RESOURCE_FREE_RESERVED:
            assert(ns->is_init);
            reserved_free(m->req, m->tok, &ns->r);
            break;
        default:
            tw_error(TW_LOC, "resource event type not known");
            break;
    }
    if (send_ack){
        /* we use bitfield to determine whether previous op was a success,
         * allowing us to do rollback a bit easier */
        b->c0 = !ret;
        /* send return message */

        msg_header h;
        msg_set_header(m->h_callback.magic, m->h_callback.event_type, 
                lp->gid, &h);

        resource_callback c;
        c.ret = ret;
        c.tok = tok;

        /* before we send the message, sanity check the sizes */
        if (m->msg_size <= m->msg_header_offset+sizeof(h) &&
                m->msg_size <= m->msg_callback_offset+sizeof(c)){
            tw_event *e = codes_event_new(m->h_callback.src, 
                    codes_local_latency(lp), lp);
            void *msg = tw_event_data(e);
            memcpy(((char*)msg)+m->msg_header_offset, &h, sizeof(h));
            memcpy(((char*)msg)+m->msg_callback_offset, &c, sizeof(c));
            tw_event_send(e);
        }
        else{
            tw_error(TW_LOC, 
                    "message size not large enough to hold header/callback "
                    "structures");
        }
    }
}

void resource_rev_handler(
        resource_state * ns,
        tw_bf * b,
        resource_msg * m,
        tw_lp * lp){
    assert(m->h.magic == resource_magic);
    
    int send_ack = 0;
    switch (m->h.event_type){
        case RESOURCE_INIT:
            assert(0); /* not currently used */
            /* this really shouldn't happen, but who knows... */
            ns->is_init = 0;
            break;
        case RESOURCE_GET:
            send_ack = 1;
            if (b->c0){ resource_free(m->req, &ns->r); }
            break;
        case RESOURCE_FREE:
            /* "re-allocate" the resource (this MUST work under the current
             * implementation) */
            assert(0==resource_get(m->req, &ns->r));
            break;
        case RESOURCE_RESERVE:
            /* this reversal method is essentially a hack that relies on each
             * sequential reserve appending to the end of the list */
            send_ack = 1;
            if (b->c0){ ns->r.num_tokens--; }
            break;
        case RESOURCE_GET_RESERVED:
            send_ack = 1;
            if (b->c0){ reserved_free(m->req, m->tok, &ns->r); }
            break;
        case RESOURCE_FREE_RESERVED:
            /* "re-allocate" the resource (this MUST work under the current
             * implementation) */
            assert(0==reserved_get(m->req, m->tok, &ns->r));
            break;
        default:
            tw_error(TW_LOC, "resource event type not known");
    }
    if (send_ack){ codes_local_latency_reverse(lp); }
}

void resource_finalize(
        resource_state * ns,
        tw_lp * lp){
    /* Fill me in... */
}

/**** END IMPLEMENTATIONS ****/

/**** BEGIN USER-FACING FUNCTIONS ****/
void resource_lp_init(){
    uint32_t h1=0, h2=0;

    bj_hashlittle2("resource", strlen("resource"), &h1, &h2);
    resource_magic = h1+h2;

    lp_type_register("resource", &resource_lp);
}

void resource_lp_configure(){
    long int avail;
    int ret = configuration_get_value_longint(&config, "resource", 
            "available", &avail);
    if (ret){
        fprintf(stderr, "Could not find section:resource value:available for "
                        "resource LP\n");
        exit(1);
    }
    assert(avail > 0);
    avail_global = (uint64_t) avail;
}

static void resource_lp_issue_event(
        msg_header *header,
        uint64_t req,
        resource_token_t tok, /* only used in reserve_get/free */
        int msg_size,
        int msg_header_offset,
        int msg_callback_offset,
        enum resource_event type,
        tw_lp *sender){

    tw_lpid resource_lpid;

    /* map out the lpid of the resource */
    int mapping_grp_id, mapping_type_id, mapping_rep_id, mapping_offset;
    char lp_type_name[MAX_NAME_LENGTH], lp_group_name[MAX_NAME_LENGTH];
    codes_mapping_get_lp_info(sender->gid, lp_group_name, 
            &mapping_grp_id, &mapping_type_id, lp_type_name, 
            &mapping_rep_id, &mapping_offset);
    codes_mapping_get_lp_id(lp_group_name, "resource", mapping_rep_id, 
            mapping_offset, &resource_lpid); 

    tw_event *e = codes_event_new(resource_lpid, codes_local_latency(sender),
            sender);

    /* set message info */
    resource_msg *m = tw_event_data(e);
    msg_set_header(header->src, resource_magic, type, &m->h);
    m->req = req;
    m->tok = tok;

    /* set callback info */
    m->h_callback = *header;
    m->msg_size = msg_size;
    m->msg_header_offset = msg_header_offset;
    m->msg_callback_offset = msg_callback_offset;

    tw_event_send(e);
}

void resource_lp_get(
        msg_header *header,
        uint64_t req, 
        int msg_size, 
        int msg_header_offset,
        int msg_callback_offset,
        tw_lp *sender){
    resource_lp_issue_event(header, req, TOKEN_DUMMY, msg_size, 
            msg_header_offset, msg_callback_offset, RESOURCE_GET, sender);
}

/* no callback for frees thus far */
void resource_lp_free(msg_header *header, uint64_t req, tw_lp *sender){
    resource_lp_issue_event(header, req, TOKEN_DUMMY, -1,-1,-1,
            RESOURCE_FREE, sender);
}
void resource_lp_reserve(
        msg_header *header, 
        uint64_t req,
        int msg_size,
        int msg_header_offset,
        int msg_callback_offset,
        tw_lp *sender){
    resource_lp_issue_event(header, req, TOKEN_DUMMY, msg_size, 
            msg_header_offset, msg_callback_offset, RESOURCE_RESERVE, sender);
}
void resource_lp_get_reserved(
        msg_header *header,
        uint64_t req,
        resource_token_t tok,
        int msg_size, 
        int msg_header_offset,
        int msg_callback_offset,
        tw_lp *sender){
    resource_lp_issue_event(header, req, tok, msg_size, 
            msg_header_offset, msg_callback_offset, RESOURCE_GET_RESERVED,
            sender);
}
void resource_lp_free_reserved(
        msg_header *header, 
        uint64_t req, 
        resource_token_t tok,
        tw_lp *sender){
    resource_lp_issue_event(header, req, tok, -1,-1,-1, RESOURCE_FREE_RESERVED,
            sender);
}

/* rc functions - thankfully, they only use codes-local-latency, so no need 
 * to pass in any arguments */

static void resource_lp_issue_event_rc(tw_lp *sender){
    codes_local_latency_reverse(sender);
}

void resource_lp_get_rc(tw_lp *sender){
    resource_lp_issue_event_rc(sender);
}
void resource_lp_free_rc(tw_lp *sender){
    resource_lp_issue_event_rc(sender);
}
void resource_lp_reserve_rc(tw_lp *sender){
    resource_lp_issue_event_rc(sender);
}
void resource_lp_get_reserved_rc(tw_lp *sender){
    resource_lp_issue_event_rc(sender);
}
void resource_lp_free_reserved_rc(tw_lp *sender){
    resource_lp_issue_event_rc(sender);
}

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