simplenet-upd.c 16.3 KB
Newer Older
1
/*
2
 * Copyright (C) 2013 University of Chicago.
3
 * See COPYRIGHT notice in top-level directory.
4
 *
5 6 7 8 9 10
 */

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

11 12
#include "codes/lp-io.h"
#include "codes/jenkins-hash.h"
13
#include "codes/model-net-method.h"
14 15 16
#include "codes/model-net.h"
#include "codes/codes_mapping.h"
#include "codes/codes.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#define CATEGORY_NAME_MAX 16
#define CATEGORY_MAX 12

/*Define simplenet data types and structs*/
typedef struct sn_message sn_message;
typedef struct sn_state sn_state;

/* types of events that will constitute triton requests */
enum sn_event_type 
{
    MSG_READY = 1,  /* sender has transmitted msg to receiver */
    MSG_START,      /* initiate a transmission */
};


struct sn_state
{
    /* next idle times for network card, both inbound and outbound */
    tw_stime net_send_next_idle;
    tw_stime net_recv_next_idle;
38
    struct mn_stats sn_stats_array[CATEGORY_MAX];
39 40 41 42 43 44 45 46
};

struct sn_message
{
    int magic; /* magic number */
    enum sn_event_type event_type;
    tw_lpid src_gid; /* who transmitted this msg? */
    tw_lpid final_dest_gid; /* who is eventually targetted with this msg? */
47
    uint64_t net_msg_size_bytes;     /* size of modeled network message */
48 49 50 51 52 53 54
    int event_size_bytes;     /* size of simulator event message that will be tunnelled to destination */
    int local_event_size_bytes;     /* size of simulator event message that delivered locally upon local completion */
    char category[CATEGORY_NAME_MAX]; /* category for communication */

    /* for reverse computation */
    tw_stime net_send_next_idle_saved;
    tw_stime net_recv_next_idle_saved;
55 56
    tw_stime send_time_saved;
    tw_stime recv_time_saved;
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
};
/* net startup cost, ns */
static double global_net_startup_ns = 0;
/* net bw, MB/s */
static double global_net_bw_mbs = 0;

static int sn_magic = 0;

/* returns a pointer to the lptype struct to use for simplenet LPs */
static const tw_lptype* sn_get_lp_type(void);

/* set model parameters:
 *
 * - net_startup_ns: network startup cost in ns.
 * - net_bs_mbs: network bandwidth in MiB/s (megabytes per second).
 */
static void sn_set_params(double net_startup_ns, double net_bw_mbs);

/* retrieve the size of the portion of the event struct that is consumed by
 * the simplenet module.  The caller should add this value to the size of
 * its own event structure to get the maximum total size of a message.
 */
static int sn_get_msg_sz(void);

/* Returns the simplenet magic number */
static int sn_get_magic();

/* allocate a new event that will pass through simplenet to arriave at its
 * destination:
 *
 * - category: category name to associate with this communication
 * - final_dest_gid: the LP that the message should be delivered to.
 * - event_size_bytes: size of event msg that will be delivered to
 * final_dest_gid.
 * - local_event_size_byte: size of event message that will delivered to
 *   local LP upon local send comletion (set to 0 if not used)
 * - net_msg_size_bytes: size of simulated network message in bytes.
 * - sender: LP calling this function.
 */
/* Modelnet interface events */
/* sets up the simplenet parameters through modelnet interface */
static void sn_setup(const void* net_params);

/* Issues a simplenet packet event call */
101
static tw_stime simplenet_packet_event(
102 103
     char* category, 
     tw_lpid final_dest_lp, 
104
     uint64_t packet_size, 
105
     tw_stime offset,
106 107 108 109 110 111 112 113 114
     int remote_event_size, 
     const void* remote_event, 
     int self_event_size,
     const void* self_event,
     tw_lp *sender,
     int is_last_pckt);
static void simplenet_packet_event_rc(tw_lp *sender);

static void simplenet_packet_event_rc(tw_lp *sender);
115 116 117

static void sn_report_stats();

118 119
static tw_lpid sn_find_local_device(tw_lp *sender);

120 121 122 123 124 125 126 127 128
/* data structure for model-net statistics */
struct model_net_method simplenet_method =
{
    .method_name = "simplenet",
    .mn_setup = sn_setup,
    .model_net_method_packet_event = simplenet_packet_event,
    .model_net_method_packet_event_rc = simplenet_packet_event_rc,
    .mn_get_lp_type = sn_get_lp_type,
    .mn_get_msg_sz = sn_get_msg_sz,
129
    .mn_report_stats = sn_report_stats,
130
    .model_net_method_find_local_device = sn_find_local_device,
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
};

static void sn_init(
    sn_state * ns,
    tw_lp * lp);
static void sn_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp);
static void sn_rev_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp);
static void sn_finalize(
    sn_state * ns,
    tw_lp * lp);

tw_lptype sn_lp = {
     (init_f) sn_init,
     (event_f) sn_event,
     (revent_f) sn_rev_event,
     (final_f) sn_finalize,
     (map_f) codes_mapping,
     sizeof(sn_state),
};

159
static tw_stime rate_to_ns(uint64_t bytes, double MB_p_s);
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
static void handle_msg_ready_rev_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp);
static void handle_msg_ready_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp);
static void handle_msg_start_rev_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp);
static void handle_msg_start_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp);

/* returns pointer to LP information for simplenet module */
static const tw_lptype* sn_get_lp_type()
{
    return(&sn_lp);
}

/* returns number of bytes that the simplenet module will consume in event
 * messages
 */
static int sn_get_msg_sz(void)
{
    return(sizeof(sn_message));
}

/* lets caller specify model parameters to use */
static void sn_set_params(double net_startup_ns, double net_bw_mbs)
{
Philip Carns's avatar
Philip Carns committed
198 199
    assert(net_startup_ns > 0);
    assert(net_bw_mbs > 0);
200 201 202 203 204 205
    global_net_startup_ns = net_startup_ns;
    global_net_bw_mbs = net_bw_mbs;
    
    return;
}

206 207 208 209 210 211
/* report network statistics */
static void sn_report_stats()
{
   /* TODO: Do we have some simplenet statistics to report like we have for torus and dragonfly? */
   return;
}
212 213 214 215
static void sn_init(
    sn_state * ns,
    tw_lp * lp)
{
216
    uint32_t h1 = 0, h2 = 0;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    memset(ns, 0, sizeof(*ns));

    /* all devices are idle to begin with */
    ns->net_send_next_idle = tw_now(lp);
    ns->net_recv_next_idle = tw_now(lp);

    bj_hashlittle2("simplenet", strlen("simplenet"), &h1, &h2);
    sn_magic = h1+h2;
    /* printf("\n sn_magic %d ", sn_magic); */

    return;
}

static void sn_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp)
{
236
    assert(m->magic == sn_magic);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

    switch (m->event_type)
    {
        case MSG_START:
            handle_msg_start_event(ns, b, m, lp);
            break;
        case MSG_READY:
            handle_msg_ready_event(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
    }
}

static void sn_rev_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp)
{
258
    assert(m->magic == sn_magic);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    switch (m->event_type)
    {
        case MSG_START:
            handle_msg_start_rev_event(ns, b, m, lp);
            break;
        case MSG_READY:
            handle_msg_ready_rev_event(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
    }

    return;
}

static void sn_finalize(
    sn_state * ns,
    tw_lp * lp)
{
280
    model_net_print_stats(lp->gid, &ns->sn_stats_array[0]);
281 282 283 284 285 286 287 288 289
    return;
}

int sn_get_magic()
{
  return sn_magic;
}

/* convert MiB/s and bytes to ns */
290
static tw_stime rate_to_ns(uint64_t bytes, double MB_p_s)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
    tw_stime time;

    /* bytes to MB */
    time = ((double)bytes)/(1024.0*1024.0);
    /* MB to s */
    time = time / MB_p_s;
    /* s to ns */
    time = time * 1000.0 * 1000.0 * 1000.0;

    return(time);
}

/* reverse computation for msg ready event */
static void handle_msg_ready_rev_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp)
{
311
    struct mn_stats* stat;
312 313 314

    ns->net_recv_next_idle = m->net_recv_next_idle_saved;
    
315
    stat = model_net_find_stats(m->category, ns->sn_stats_array);
316 317
    stat->recv_count--;
    stat->recv_bytes -= m->net_msg_size_bytes;
318
    stat->recv_time = m->recv_time_saved;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

    return;
}

/* handler for msg ready event.  This indicates that a message is available
 * to recv, but we haven't checked to see if the recv queue is available yet
 */
static void handle_msg_ready_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp)
{
    tw_stime recv_queue_time = 0;
    tw_event *e_new;
    sn_message *m_new;
335
    struct mn_stats* stat;
336 337 338

    //printf("handle_msg_ready_event(), lp %llu.\n", (unsigned long long)lp->gid);
    /* add statistics */
339
    stat = model_net_find_stats(m->category, ns->sn_stats_array);
340 341
    stat->recv_count++;
    stat->recv_bytes += m->net_msg_size_bytes;
342
    m->recv_time_saved = stat->recv_time;
343 344 345 346 347 348 349 350
    stat->recv_time += rate_to_ns(m->net_msg_size_bytes, global_net_bw_mbs);

    /* are we available to recv the msg? */
    /* were we available when the transmission was started? */
    if(ns->net_recv_next_idle > tw_now(lp))
        recv_queue_time += ns->net_recv_next_idle - tw_now(lp);

    /* calculate transfer time based on msg size and bandwidth */
351
    recv_queue_time += rate_to_ns(m->net_msg_size_bytes, global_net_bw_mbs);
352 353 354 355 356 357 358 359 360 361 362

    /* bump up input queue idle time accordingly */
    m->net_recv_next_idle_saved = ns->net_recv_next_idle;
    ns->net_recv_next_idle = recv_queue_time + tw_now(lp);

    /* copy only the part of the message used by higher level */
    if(m->event_size_bytes)
    {
      /* schedule event to final destination for when the recv is complete */
//      printf("\n Remote message to LP %d ", m->final_dest_gid); 

363
      e_new = tw_event_new(m->final_dest_gid, recv_queue_time, lp);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
      m_new = tw_event_data(e_new);
      char* tmp_ptr = (char*)m;
      tmp_ptr += sn_get_msg_sz();
      memcpy(m_new, tmp_ptr, m->event_size_bytes);
      tw_event_send(e_new);
    }

    return;
}

/* reverse computation for msg start event */
static void handle_msg_start_rev_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp)
{
    ns->net_send_next_idle = m->net_send_next_idle_saved;

    if(m->local_event_size_bytes > 0)
    {
        codes_local_latency_reverse(lp);
    }

388 389
    mn_stats* stat;
    stat = model_net_find_stats(m->category, ns->sn_stats_array);
390 391
    stat->send_count--;
    stat->send_bytes -= m->net_msg_size_bytes;
392
    stat->send_time = m->send_time_saved;
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

    return;
}

/* handler for msg start event; this indicates that the caller is trying to
 * transmit a message through this NIC
 */
static void handle_msg_start_event(
    sn_state * ns,
    tw_bf * b,
    sn_message * m,
    tw_lp * lp)
{
    tw_event *e_new;
    sn_message *m_new;
    tw_stime send_queue_time = 0;
409
    mn_stats* stat;
410 411 412
    int mapping_grp_id, mapping_type_id, mapping_rep_id, mapping_offset;
    tw_lpid dest_id;
    char lp_type_name[MAX_NAME_LENGTH], lp_group_name[MAX_NAME_LENGTH];
413 414 415
    int total_event_size;

    total_event_size = sn_get_msg_sz() + m->event_size_bytes + m->local_event_size_bytes;
416 417 418

    //printf("handle_msg_start_event(), lp %llu.\n", (unsigned long long)lp->gid);
    /* add statistics */
419
    stat = model_net_find_stats(m->category, ns->sn_stats_array);
420 421
    stat->send_count++;
    stat->send_bytes += m->net_msg_size_bytes;
422
    m->send_time_saved = stat->send_time;
Philip Carns's avatar
Philip Carns committed
423
    stat->send_time += (global_net_startup_ns + rate_to_ns(m->net_msg_size_bytes, global_net_bw_mbs));
424 425
    if(stat->max_event_size < total_event_size)
        stat->max_event_size = total_event_size;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

    /* calculate send time stamp */
    send_queue_time = global_net_startup_ns; /* net msg startup cost */
    /* bump up time if the NIC send queue isn't idle right now */
    if(ns->net_send_next_idle > tw_now(lp))
        send_queue_time += ns->net_send_next_idle - tw_now(lp);

    /* move the next idle time ahead to after this transmission is
     * _complete_ from the sender's perspective 
     */ 
    m->net_send_next_idle_saved = ns->net_send_next_idle;
    ns->net_send_next_idle = send_queue_time + tw_now(lp) +
        rate_to_ns(m->net_msg_size_bytes, global_net_bw_mbs);


    /* create new event to send msg to receiving NIC */
    codes_mapping_get_lp_info(m->final_dest_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, "modelnet_simplenet", mapping_rep_id , mapping_offset, &dest_id); 

//    printf("\n msg start sending to %d ", dest_id);
446
    e_new = tw_event_new(dest_id, send_queue_time, lp);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    m_new = tw_event_data(e_new);

    /* copy entire previous message over, including payload from user of
     * this module
     */
    memcpy(m_new, m, m->event_size_bytes + sn_get_msg_sz());
    m_new->event_type = MSG_READY;
    
    tw_event_send(e_new);

    /* if there is a local event to handle, then create an event for it as
     * well
     */
    if(m->local_event_size_bytes > 0)
    {
        char* local_event;

464
        e_new = tw_event_new(m->src_gid, send_queue_time+codes_local_latency(lp), lp);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        m_new = tw_event_data(e_new);

         local_event = (char*)m;
         local_event += sn_get_msg_sz() + m->event_size_bytes;         	 
        /* copy just the local event data over */
        memcpy(m_new, local_event, m->local_event_size_bytes);
        tw_event_send(e_new);
    }
    return;
}

/* Model-net function calls */

/*This method will serve as an intermediate layer between simplenet and modelnet. 
 * It takes the packets from modelnet layer and calls underlying simplenet methods*/
480
static tw_stime simplenet_packet_event(
481 482
		char* category,
		tw_lpid final_dest_lp,
483
		uint64_t packet_size,
484
                tw_stime offset,
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
		int remote_event_size,
		const void* remote_event,
		int self_event_size,
		const void* self_event,
		tw_lp *sender,
		int is_last_pckt)
{
     tw_event * e_new;
     tw_stime xfer_to_nic_time;
     sn_message * msg;
     tw_lpid dest_id;
     char* tmp_ptr;
     char lp_type_name[MAX_NAME_LENGTH], lp_group_name[MAX_NAME_LENGTH];

     int mapping_grp_id, mapping_rep_id, mapping_type_id, mapping_offset;
     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, "modelnet_simplenet", mapping_rep_id, mapping_offset, &dest_id);

     xfer_to_nic_time = codes_local_latency(sender);
504
     e_new = tw_event_new(dest_id, xfer_to_nic_time+offset, sender);
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
     msg = tw_event_data(e_new);
     strcpy(msg->category, category);
     msg->final_dest_gid = final_dest_lp;
     msg->src_gid = sender->gid;
     msg->magic = sn_get_magic();
     msg->net_msg_size_bytes = packet_size;
     msg->event_size_bytes = 0;
     msg->local_event_size_bytes = 0;
     msg->event_type = MSG_START;

     tmp_ptr = (char*)msg;
     tmp_ptr += sn_get_msg_sz();
      
    //printf("\n Sending to LP %d msg magic %d ", (int)dest_id, sn_get_magic()); 
     /*Fill in simplenet information*/     
     if(is_last_pckt) /* Its the last packet so pass in remote event information*/
      {
       if(remote_event_size)
	 {
           msg->event_size_bytes = remote_event_size;
           memcpy(tmp_ptr, remote_event, remote_event_size);
           tmp_ptr += remote_event_size;
	 }
       if(self_event_size)
       {
	   msg->local_event_size_bytes = self_event_size;
	   memcpy(tmp_ptr, self_event, self_event_size);
	   tmp_ptr += self_event_size;
       }
      // printf("\n Last packet size: %d ", sn_get_msg_sz() + remote_event_size + self_event_size);
      }
     tw_event_send(e_new);
537
     return xfer_to_nic_time;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
}

static void sn_setup(const void* net_params)
{
  simplenet_param* sn_param = (simplenet_param*)net_params;
  sn_set_params(sn_param->net_startup_ns, sn_param->net_bw_mbps); 
  //printf("\n Bandwidth setup %lf %lf ", sn_param->net_startup_ns, sn_param->net_bw_mbps);
}

static void simplenet_packet_event_rc(tw_lp *sender)
{
    codes_local_latency_reverse(sender);
    return;
}

553 554 555 556 557 558 559 560 561 562 563 564 565
static tw_lpid sn_find_local_device(tw_lp *sender)
{
     char lp_type_name[MAX_NAME_LENGTH], lp_group_name[MAX_NAME_LENGTH];
     int mapping_grp_id, mapping_rep_id, mapping_type_id, mapping_offset;
     tw_lpid dest_id;

     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, "modelnet_simplenet", mapping_rep_id, mapping_offset, &dest_id);

    return(dest_id);
}


566 567 568 569 570 571 572 573
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */