codes-workload-test-svr-lp.c 4.11 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
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

/* Summary: example storage server LP type for workload demo/test */

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

#include "codes/lp-io.h"
#include "codes/codes.h"
#include "codes/codes-workload.h"
#include "codes-workload-test-svr-lp.h"
#include "codes-workload-test-cn-lp.h"

typedef struct svr_msg svr_msg;
typedef struct svr_state svr_state;

enum svr_event_type
{
Philip Carns's avatar
Philip Carns committed
24
    SVR_OP = 128,
25 26 27 28
};

struct svr_state
{
29
    tw_stime next_idle;
30 31 32 33 34 35 36
};

struct svr_msg
{
    enum svr_event_type event_type;
    struct codes_workload_op op;
    tw_lpid src;          /* source of this request or ack */
37
    tw_stime next_idle_rc;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
};

static void svr_init(
    svr_state * ns,
    tw_lp * lp);
static void svr_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
static void svr_rev_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
static void svr_finalize(
    svr_state * ns,
    tw_lp * lp);
static tw_peid node_mapping(
    tw_lpid gid);

tw_lptype svr_lp = {
60 61 62 63 64 65 66
    (init_f) svr_init,
    (pre_run_f) NULL,
    (event_f) svr_event,
    (revent_f) svr_rev_event,
    (final_f) svr_finalize, 
    (map_f) node_mapping,
    sizeof(svr_state),
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
};

static void handle_svr_op_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
static void handle_svr_op_event_rc(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);


static void svr_init(
    svr_state * ns,
    tw_lp * lp)
{
    memset(ns, 0, sizeof(*ns));

87 88
    ns->next_idle = tw_now(lp);

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
    return;
}

static void svr_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{

    switch (m->event_type)
    {
        case SVR_OP:
            handle_svr_op_event(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
    }
}

static void svr_rev_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
    switch (m->event_type)
    {
        case SVR_OP:
            handle_svr_op_event_rc(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
    }

    return;
}

static void svr_finalize(
    svr_state * ns,
    tw_lp * lp)
{
    char buffer[256];
    int ret;

136 137 138
    /* write out some statistics (the current time of each server as it
     * shuts down)
     */
139
    sprintf(buffer, "svr_lp:%ld\tcompletion_time:%f\n", (long)lp->gid, ns->next_idle);
140

141
    ret = lp_io_write(lp->gid, "servers", strlen(buffer)+1, buffer);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    assert(ret == 0);

    return;
}

static tw_peid node_mapping(
    tw_lpid gid)
{
    return (tw_peid) gid / g_tw_nlp;
}

void svr_op_start(tw_lp *lp, tw_lpid gid, const struct codes_workload_op *op)
{
    tw_event *e;
    svr_msg *m;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
158
    e = tw_event_new(gid, codes_local_latency(lp), lp);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    m = tw_event_data(e);
    m->event_type = SVR_OP;
    m->op = *op;
    m->src = lp->gid;
    tw_event_send(e);
}

void svr_op_start_rc(tw_lp *lp)
{
    codes_local_latency_reverse(lp);
}

static void handle_svr_op_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
177 178
    (void)b;
    printf("handle_svr_op_event at lp %llu, type %d\n", LLU(lp->gid), m->op.op_type);
179 180 181

    m->next_idle_rc = ns->next_idle;

182
    /* NOTE: this isn't a real server simulator, but we do want time to
183 184 185
     * elapse so that we can do correctness/regression testing.  For testing
     * purposes we use the value of the op_type field as the amount of
     * elapsed time for the operation to consume.
186
     */
187 188 189 190
    if(ns->next_idle > tw_now(lp))
        ns->next_idle += m->op.op_type;
    else
        ns->next_idle = tw_now(lp) + m->op.op_type;
191 192

    /* send event back to cn to let it know the operation is done */
193
    cn_op_complete(lp, ns->next_idle-tw_now(lp), m->src);
194 195 196 197 198 199 200 201 202 203

    return;
}

static void handle_svr_op_event_rc(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
204
    ns->next_idle = m->next_idle_rc;
205
    cn_op_complete_rc(lp);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
206 207
    (void)b;
    (void)m;
208 209 210 211 212 213 214 215 216 217 218
    return;
}

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