1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid/msg.h"
11 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_pastry,
14 "Messages specific for this msg example");
16 /***************************************
20 * - handle node departure *
21 * - handle objects on the network *
22 * - handle neighborood in the update *
24 ***************************************/
28 #define MAILBOX_NAME_SIZE 10
31 #define LEVELS_COUNT 8 // sizeof(int)*8/DOMAIN_SIZE
32 #define LEVEL_SIZE 16 // 2^DOMAIN_SIZE
33 #define NEIGHBORHOOD_SIZE 6
34 #define NAMESPACE_SIZE 6
35 #define MAILBOX_NAME_SIZE 10
37 static int nb_bits = 16;
38 static int timeout = 50;
39 static int max_simulation_time = 1000;
41 extern long int smx_total_comms;
44 typedef struct s_node {
45 int id; //128bits generated random(2^128 -1)
47 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
48 int namespace_set[NAMESPACE_SIZE];
49 int neighborhood_set[NEIGHBORHOOD_SIZE];
50 int routing_table[LEVELS_COUNT][LEVEL_SIZE];
52 msg_comm_t comm_receive; // current communication to receive
53 xbt_fifo_t pending_tasks;
56 typedef struct s_state {
58 int namespace_set[NAMESPACE_SIZE];
59 int neighborhood_set[NEIGHBORHOOD_SIZE];
60 int routing_table[LEVELS_COUNT][LEVEL_SIZE];
61 } s_state_t, *state_t;
64 * Types of tasks exchanged between nodes.
73 typedef struct s_task_data {
74 e_task_type_t type; // type of task
75 int sender_id; // id paramater (used by some types of tasks)
76 //int request_finger; // finger parameter (used by some types of tasks)
77 int answer_id; // answer (used by some types of tasks)
78 char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
79 //const char* issuer_host_name; // used for logging
82 } s_task_data_t, *task_data_t;
85 static void print_node(node_t node);
86 static void print_node_id(node_t node);
87 static void print_node_neighborood_set(node_t node);
88 static void print_node_routing_table(node_t node);
89 static void print_node_namespace_set(node_t node);
90 static state_t node_get_state(node_t node);
91 static void get_mailbox(int node_id, char* mailbox);
92 static int domain(int a, int level);
93 static int shl(int a, int b);
94 static int closest_in_namespace_set(node_t node, int dest);
95 static int routing_next(node_t node, int dest);
96 static void create(node_t node);
97 static int join(node_t node);
101 * \brief Gets the mailbox name of a host given its chord id.
102 * \param node_id id of a node
103 * \param mailbox pointer to where the mailbox name should be written
104 * (there must be enough space)
106 static void get_mailbox(int node_id, char* mailbox)
108 snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
112 * Get the specific level of a node id
115 static int domain(int a, int level) {
116 if (domain_mask == 0)
117 domain_mask = pow(2, DOMAIN_SIZE) - 1;
118 int shift = (LEVELS_COUNT-level-1)*DOMAIN_SIZE;
119 return (a >> shift) & domain_mask;
123 * Get the shared domains between the two givens ids
125 static int shl(int a, int b) {
127 while(l<LEVELS_COUNT && domain(a,l) == domain(b,l))
133 * Get the closest id to the dest in the node namespace_set
135 static int closest_in_namespace_set(node_t node, int dest) {
138 if ((node->namespace_set[NAMESPACE_SIZE-1] <= dest) & (dest <= node->namespace_set[0])) {
139 best_dist = abs(node->id - dest);
142 for (i=0; i<NAMESPACE_SIZE; i++) {
143 if (node->namespace_set[i]!=-1) {
144 dist = abs(node->namespace_set[i] - dest);
145 if (dist<best_dist) {
147 res = node->namespace_set[i];
156 * Find the next node to forward a meassage to
158 static int routing_next(node_t node, int dest) {
159 int closest = closest_in_namespace_set(node, dest);
164 int l = shl(node->id, dest);
165 res = node->routing_table[l][domain(dest, l)];
170 int dist = abs(node->id - dest);
172 for (i=l; i<LEVELS_COUNT; i++) {
173 for (j=0; j<LEVEL_SIZE; j++) {
174 res = node->routing_table[i][j];
175 if (res!=-1 && abs(res - dest)<dist)
180 for (i=0; i<NEIGHBORHOOD_SIZE; i++) {
181 res = node->neighborhood_set[i];
182 if (res!=-1 && shl(res, dest)>=l && abs(res - dest)<dist)
186 for (i=0; i<NAMESPACE_SIZE; i++) {
187 res = node->namespace_set[i];
188 if (res!=-1 && shl(res, dest)>=l && abs(res - dest)<dist)
196 * Handle a given task
198 static void handle_task(node_t node, msg_task_t task) {
199 XBT_DEBUG("Handling task %p", task);
200 char mailbox[MAILBOX_NAME_SIZE];
201 int i, j, min, max, d;
202 msg_task_t task_sent;
203 task_data_t req_data;
204 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
205 e_task_type_t type = task_data->type;
206 // If the node is not ready keep the task for later
207 if (node->ready != 0 && !(type==TASK_JOIN_LAST_REPLY || type==TASK_JOIN_REPLY)) {
208 XBT_DEBUG("Task pending %i", type);
209 xbt_fifo_push(node->pending_tasks, task);
214 * Try to join the ring
217 int next = routing_next(node, task_data->answer_id);
218 XBT_DEBUG("Join request from %08x forwarding to %08x", task_data->answer_id, next);
219 type = TASK_JOIN_LAST_REPLY;
221 req_data = xbt_new0(s_task_data_t,1);
222 req_data->answer_id = task_data->sender_id;
223 req_data->steps = task_data->steps + 1;
225 // if next different from current node forward the join
226 if (next!=node->id) {
227 get_mailbox(next, mailbox);
228 task_data->sender_id = node->id;
230 task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, task_data);
231 MSG_task_send_with_timeout(task_sent, mailbox, timeout);
232 type = TASK_JOIN_REPLY;
235 // send back the current node state to the joining node
236 req_data->type = type;
237 req_data->sender_id = node->id;
238 get_mailbox(node->id, req_data->answer_to);
239 req_data->state = node_get_state(node);
240 task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
241 MSG_task_send_with_timeout(task_sent, task_data->answer_to, timeout);
245 * Join reply from all the node touched by the join
247 case TASK_JOIN_LAST_REPLY:
248 // if last node touched reply, copy its namespace set
249 // TODO: it's work only if the two nodes are side to side (is it really the case ?)
250 j = (task_data->sender_id < node->id) ? -1 : 0;
251 for (i=0; i<NAMESPACE_SIZE/2; i++) {
252 node->namespace_set[i] = task_data->state->namespace_set[i-j];
253 node->namespace_set[NAMESPACE_SIZE-1-i] = task_data->state->namespace_set[NAMESPACE_SIZE-1-i-j-1];
255 node->namespace_set[NAMESPACE_SIZE/2+j] = task_data->sender_id;
256 node->ready += task_data->steps + 1;
257 case TASK_JOIN_REPLY:
258 XBT_DEBUG("Joining Reply");
260 // if first node touched reply, copy its neighborood set
261 if (task_data->sender_id == node->known_id) {
262 node->neighborhood_set[0] = task_data->sender_id;
263 for (i=1; i<NEIGHBORHOOD_SIZE; i++)
264 node->neighborhood_set[i] = task_data->state->neighborhood_set[i-1];
267 // copy the corresponding routing table levels
268 min = (node->id==task_data->answer_id) ? 0 : shl(node->id, task_data->answer_id);
269 max = shl(node->id, task_data->sender_id)+1;
270 for (i=min;i<max;i++) {
271 d = domain(node->id, i);
272 for (j=0; j<LEVEL_SIZE; j++)
274 node->routing_table[i][j] = task_data->state->routing_table[i][j];
278 // if the node is ready, do all the pending tasks and send update to known nodes
279 if (node->ready==0) {
280 XBT_DEBUG("Node %i is ready!!!", node->id);
282 while(xbt_fifo_size(node->pending_tasks))
283 handle_task(node, xbt_fifo_pop(node->pending_tasks));
285 for (i=0; i<NAMESPACE_SIZE; i++) {
286 j = node->namespace_set[i];
288 XBT_DEBUG("Send update to %i", j);
289 get_mailbox(j, mailbox);
291 req_data = xbt_new0(s_task_data_t,1);
292 req_data->answer_id = node->id;
294 req_data->type = TASK_UPDATE;
295 req_data->sender_id = node->id;
296 get_mailbox(node->id, req_data->answer_to);
297 req_data->state = node_get_state(node);
298 task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
299 MSG_task_send_with_timeout(task_sent, mailbox, timeout);
306 * Recieved an update of state
309 XBT_DEBUG("Task update %i !!!", node->id);
311 /* Update namespace ses */
312 printf("Task update from %i !!!\n", task_data->sender_id);
314 print_node_namespace_set(node);
315 int curr_namespace_set[NAMESPACE_SIZE];
316 int task_namespace_set[NAMESPACE_SIZE+1];
318 // Copy the current namedspace
319 // and the task state namespace with state->id in the middle
321 for (; i<NAMESPACE_SIZE/2; i++){
322 curr_namespace_set[i] = node->namespace_set[i];
323 task_namespace_set[i] = task_data->state->namespace_set[i];
325 task_namespace_set[i] = task_data->state->id;
326 for (; i<NAMESPACE_SIZE; i++){
327 curr_namespace_set[i] = node->namespace_set[i];
328 task_namespace_set[i+1] = task_data->state->namespace_set[i];
331 // get the index of values before and after node->id in task_namespace
334 for (i=0; i<=NAMESPACE_SIZE; i++) {
335 j = task_namespace_set[i];
336 if (i<NAMESPACE_SIZE)
337 printf("%08x %08x | ", j, curr_namespace_set[i]);
338 if (j != -1 && j < node->id) min = i;
339 if (j != -1 && max == -1 && j > node->id) max = i;
343 // add lower elements
344 j = NAMESPACE_SIZE/2-1;
345 for (i=NAMESPACE_SIZE/2-1; i>=0; i--) {
346 printf("i:%i, j:%i, min:%i, currj:%08x, taskmin:%08x\n", i, j, min, curr_namespace_set[j], task_namespace_set[min]);
348 node->namespace_set[i] = curr_namespace_set[j];
350 } else if (curr_namespace_set[j] == task_namespace_set[min]) {
351 node->namespace_set[i] = curr_namespace_set[j];
353 } else if (curr_namespace_set[j] > task_namespace_set[min]) {
354 node->namespace_set[i] = curr_namespace_set[j];
357 node->namespace_set[i] = task_namespace_set[min];
362 // add greater elements
363 j = NAMESPACE_SIZE/2;
364 for (i=NAMESPACE_SIZE/2; i<NAMESPACE_SIZE; i++) {
365 printf("i:%i, j:%i, max:%i, currj:%08x, taskmax:%08x\n", i, j, max, curr_namespace_set[j], task_namespace_set[max]);
366 if (min<0 || max>=NAMESPACE_SIZE) {
367 node->namespace_set[i] = curr_namespace_set[j];
369 } else if (curr_namespace_set[j] == -1) {
370 node->namespace_set[i] = task_namespace_set[max];
372 } else if (curr_namespace_set[j] == task_namespace_set[max]) {
373 node->namespace_set[i] = curr_namespace_set[j];
375 } else if (curr_namespace_set[j] < task_namespace_set[max]) {
376 node->namespace_set[i] = curr_namespace_set[j];
379 node->namespace_set[i] = task_namespace_set[max];
383 print_node_namespace_set(node);
385 /* Update routing table */
386 for (i=shl(node->id, task_data->state->id); i<LEVELS_COUNT; i++) {
387 for (j=0; j<LEVEL_SIZE; j++) {
388 if (node->routing_table[i][j]==-1 && task_data->state->routing_table[i][j]==-1)
389 node->routing_table[i][j] = task_data->state->routing_table[i][j];
396 * \brief Initializes the current node as the first one of the system.
397 * \param node the current node
399 static void create(node_t node){
401 XBT_DEBUG("Create a new Pastry ring...");
407 static int join(node_t node){
408 task_data_t req_data = xbt_new0(s_task_data_t,1);
409 req_data->type = TASK_JOIN;
410 req_data->sender_id = node->id;
411 req_data->answer_id = node->id;
413 get_mailbox(node->id, req_data->answer_to);
415 char mailbox[MAILBOX_NAME_SIZE];
416 get_mailbox(node->known_id, mailbox);
418 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
419 XBT_DEBUG("Trying to join Pastry ring... (with node %s)", mailbox);
420 MSG_task_send_with_timeout(task_sent, mailbox, timeout);
426 * Print the node infomation
428 static void print_node(node_t node) {
431 print_node_neighborood_set(node);
432 print_node_routing_table(node);
433 print_node_namespace_set(node);
439 static void print_node_id(node_t node) {
441 printf(" id: %i '%08x' ", node->id, node->id);
442 for (i=0;i<LEVELS_COUNT;i++)
443 printf(" %x", domain(node->id, i));
448 * Print the node neighborood set
450 static void print_node_neighborood_set(node_t node) {
452 printf(" neighborood:\n");
453 for (i=0; i<NEIGHBORHOOD_SIZE; i++)
454 printf(" %08x\n", node->neighborhood_set[i]);
458 * Print the routing table
460 static void print_node_routing_table(node_t node) {
462 printf(" routing table:\n");
463 for (i=0; i<LEVELS_COUNT; i++){
465 for (j=0; j<LEVEL_SIZE; j++)
466 printf("%08x ", node->routing_table[i][j]);
472 * Print the node namespace set
474 static void print_node_namespace_set(node_t node) {
476 printf(" namespace:\n");
477 for (i=0; i<NAMESPACE_SIZE; i++)
478 printf(" %08x\n", node->namespace_set[i]);
484 * Get the corresponding state of a node
486 static state_t node_get_state(node_t node) {
488 state_t state = xbt_new0(s_state_t,1);
489 state->id = node->id;
490 for (i=0; i<NEIGHBORHOOD_SIZE; i++)
491 state->neighborhood_set[i] = node->neighborhood_set[i];
493 for (i=0; i<LEVELS_COUNT; i++)
494 for (j=0; j<LEVEL_SIZE; j++)
495 state->routing_table[i][j] = node->routing_table[i][j];
497 for (i=0; i<NAMESPACE_SIZE; i++)
498 state->namespace_set[i] = node->namespace_set[i];
505 * \brief Node Function
508 * - the id of a guy I know in the system (except for the first node)
509 * - the time to sleep before I join (except for the first node)
510 * - the deadline time
512 static int node(int argc, char *argv[])
514 double init_time = MSG_get_clock();
515 msg_task_t task_received = NULL;
516 int join_success = 0;
518 xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
520 node.id = xbt_str_parse_int(argv[1], "Invalid ID: %s");
523 node.pending_tasks = xbt_fifo_new();
524 get_mailbox(node.id, node.mailbox);
525 XBT_DEBUG("New node with id %s (%08x)", node.mailbox, node.id);
528 for (i=0; i<LEVELS_COUNT; i++){
529 d = domain(node.id, i);
530 for (j=0; j<LEVEL_SIZE; j++)
531 node.routing_table[i][j] = (d==j) ? node.id : -1;
534 for (i=0; i<NEIGHBORHOOD_SIZE; i++)
535 node.neighborhood_set[i] = -1;
537 for (i=0; i<NAMESPACE_SIZE; i++)
538 node.namespace_set[i] = -1;
540 if (argc == 3) { // first ring
541 XBT_DEBUG("Hey! Let's create the system.");
542 deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
547 node.known_id = xbt_str_parse_int(argv[2], "Invalid known ID: %s");
548 double sleep_time = xbt_str_parse_double(argv[3], "Invalid sleep time: %s");
549 deadline = xbt_str_parse_double(argv[4], "Invalid deadline: %s");
551 // sleep before starting
552 XBT_DEBUG("Let's sleep during %f", sleep_time);
553 MSG_process_sleep(sleep_time);
554 XBT_DEBUG("Hey! Let's join the system.");
556 join_success = join(&node);
560 XBT_DEBUG("Waiting ….");
562 while (MSG_get_clock() < init_time + deadline
563 // && MSG_get_clock() < node.last_change_date + 1000
564 && MSG_get_clock() < max_simulation_time) {
565 if (node.comm_receive == NULL) {
566 task_received = NULL;
567 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
568 // FIXME: do not make MSG_task_irecv() calls from several functions
570 if (!MSG_comm_test(node.comm_receive)) {
571 MSG_process_sleep(5);
573 // a transfer has occurred
575 msg_error_t status = MSG_comm_get_status(node.comm_receive);
577 if (status != MSG_OK) {
578 XBT_DEBUG("Failed to receive a task. Nevermind.");
579 MSG_comm_destroy(node.comm_receive);
580 node.comm_receive = NULL;
583 // the task was successfully received
584 MSG_comm_destroy(node.comm_receive);
585 node.comm_receive = NULL;
586 handle_task(&node, task_received);
599 /*typedef struct s_node {
601 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
602 s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
603 int pred_id; // predecessor id
604 char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
605 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
606 msg_comm_t comm_receive; // current communication to receive
607 double last_change_date; // last time I changed a finger or my predecessor
608 } s_node_t, *node_t;*/
611 * \brief Main function.
613 int main(int argc, char *argv[])
615 MSG_init(&argc, argv);
617 "Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n"
618 "\tExample: %s ../msg_platform.xml pastry10.xml\n",
621 char **options = &argv[1];
622 while (!strncmp(options[0], "-", 1)) {
624 int length = strlen("-nb_bits=");
625 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
626 nb_bits = xbt_str_parse_int(options[0] + length, "Invalid nb_bits parameter: %s");
627 XBT_DEBUG("Set nb_bits to %d", nb_bits);
631 length = strlen("-timeout=");
632 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
633 timeout = xbt_str_parse_int(options[0] + length, "Invalid timeout parameter: %s");
634 XBT_DEBUG("Set timeout to %d", timeout);
637 xbt_die("Invalid chord option '%s'", options[0]);
643 const char* platform_file = options[0];
644 const char* application_file = options[1];
646 MSG_create_environment(platform_file);
648 MSG_function_register("node", node);
649 MSG_launch_application(application_file);
651 msg_error_t res = MSG_main();
652 XBT_CRITICAL("Messages created: %ld", smx_total_comms);
653 XBT_INFO("Simulated time: %g", MSG_get_clock());
655 return res != MSG_OK;