1 /* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord, "Messages specific for this msg example");
10 static int nb_bits = 24;
11 static int nb_keys = 0;
12 static int timeout = 50;
13 static int max_simulation_time = 1000;
14 static int periodic_stabilize_delay = 20;
15 static int periodic_fix_fingers_delay = 120;
16 static int periodic_check_predecessor_delay = 120;
17 static int periodic_lookup_delay = 10;
19 static const double sleep_delay = 4.9999;
23 static xbt_dynar_t host_list;
25 /* Global initialization of the Chord simulation. */
26 static void chord_initialize(void)
28 // compute the powers of 2 once for all
29 powers2 = xbt_new(int, nb_bits);
32 for (i = 0; i < nb_bits; i++) {
37 XBT_DEBUG("Sets nb_keys to %d", nb_keys);
40 host_list = MSG_hosts_as_dynar();
41 xbt_dynar_foreach(host_list, i, host) {
44 snprintf(descr, sizeof descr, "RngSream<%s>", MSG_host_get_name(host));
45 stream = RngStream_CreateStream(descr);
46 MSG_host_set_property_value(host, "stream", (char*)stream, NULL);
50 static void chord_exit(void)
54 xbt_dynar_foreach(host_list, i, host) {
55 RngStream stream = (RngStream)MSG_host_get_property_value(host, "stream");
56 RngStream_DeleteStream(&stream);
58 xbt_dynar_free(&host_list);
63 /* Turns an id into an equivalent id in [0, nb_keys). */
64 static int normalize(int id)
69 /* Returns whether an id belongs to the interval [start, end].
71 * The parameters are normalized to make sure they are between 0 and nb_keys - 1).
72 * 1 belongs to [62, 3]
73 * 1 does not belong to [3, 62]
74 * 63 belongs to [62, 3]
75 * 63 does not belong to [3, 62]
76 * 24 belongs to [21, 29]
77 * 24 does not belong to [29, 21]
79 * \param id id to check
80 * \param start lower bound
81 * \param end upper bound
82 * \return a non-zero value if id in in [start, end]
84 static int is_in_interval(int id, int start, int end)
86 int i = normalize(id);
87 int s = normalize(start);
88 int e = normalize(end);
90 // make sure end >= start and id >= start
102 /* Gets the mailbox name of a host given its chord id.
103 * \param node_id id of a node
104 * \param mailbox pointer to where the mailbox name should be written
105 * (there must be enough space)
107 static void get_mailbox(int node_id, char* mailbox)
109 snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
112 /* Frees the memory used by a task and destroy it */
113 static void task_free(void* task)
115 // TODO add a parameter data_free_function to MSG_task_create?
117 xbt_free(MSG_task_get_data(task));
118 MSG_task_destroy(task);
122 /* Displays the finger table of a node. */
123 static void print_finger_table(node_t node)
125 if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
126 XBT_VERB("My finger table:");
127 XBT_VERB("Start | Succ");
128 for (int i = 0; i < nb_bits; i++) {
129 XBT_VERB(" %3d | %3d", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
131 XBT_VERB("Predecessor: %d", node->pred_id);
135 /* Sets a finger of the current node.
137 * \param node the current node
138 * \param finger_index index of the finger to set (0 to nb_bits - 1)
139 * \param id the id to set for this finger
141 static void set_finger(node_t node, int finger_index, int id)
143 if (id != node->fingers[finger_index].id) {
144 node->fingers[finger_index].id = id;
145 get_mailbox(id, node->fingers[finger_index].mailbox);
146 node->last_change_date = MSG_get_clock();
147 XBT_DEBUG("My new finger #%d is %d", finger_index, id);
151 /* Sets the predecessor of the current node.
153 * \param node the current node
154 * \param id the id to predecessor, or -1 to unset the predecessor
156 static void set_predecessor(node_t node, int predecessor_id)
158 if (predecessor_id != node->pred_id) {
159 node->pred_id = predecessor_id;
161 if (predecessor_id != -1) {
162 get_mailbox(predecessor_id, node->pred_mailbox);
164 node->last_change_date = MSG_get_clock();
166 XBT_DEBUG("My new predecessor is %d", predecessor_id);
170 /* Node main Function
174 * - the id of a guy I know in the system (except for the first node)
175 * - the time to sleep before I join (except for the first node)
177 /* This function is called when the current node receives a task.
179 * \param node the current node
180 * \param task the task to handle (don't touch it afterward: it will be destroyed, reused or forwarded)
182 static void handle_task(node_t node, msg_task_t task)
184 XBT_DEBUG("Handling task %p", task);
185 char mailbox[MAILBOX_NAME_SIZE];
186 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
187 e_task_type_t type = task_data->type;
190 case TASK_FIND_SUCCESSOR:
191 XBT_DEBUG("Receiving a 'Find Successor' request from %s for id %d",
192 task_data->issuer_host_name, task_data->request_id);
193 // is my successor the successor?
194 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
195 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
196 task_data->answer_id = node->fingers[0].id;
197 XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
198 task_data->issuer_host_name, task_data->answer_to, task_data->request_id, task_data->answer_id);
199 MSG_task_dsend(task, task_data->answer_to, task_free);
201 // otherwise, forward the request to the closest preceding finger in my table
202 int closest = closest_preceding_node(node, task_data->request_id);
203 XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
204 task_data->request_id, closest);
205 get_mailbox(closest, mailbox);
206 MSG_task_dsend(task, mailbox, task_free);
210 case TASK_GET_PREDECESSOR:
211 XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
212 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
213 task_data->answer_id = node->pred_id;
214 XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
215 task_data->issuer_host_name, task_data->answer_to, task_data->answer_id);
216 MSG_task_dsend(task, task_data->answer_to, task_free);
220 // someone is telling me that he may be my new predecessor
221 XBT_DEBUG("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
222 notify(node, task_data->request_id);
226 case TASK_PREDECESSOR_LEAVING:
227 // my predecessor is about to quit
228 XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
229 // modify my predecessor
230 set_predecessor(node, task_data->request_id);
233 >> notify my new predecessor
234 >> send a notify_predecessors !!
238 case TASK_SUCCESSOR_LEAVING:
239 // my successor is about to quit
240 XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
241 // modify my successor FIXME : this should be implicit ?
242 set_finger(node, 0, task_data->request_id);
245 >> notify my new successor
246 >> update my table & predecessors table */
249 case TASK_FIND_SUCCESSOR_ANSWER:
250 case TASK_GET_PREDECESSOR_ANSWER:
251 case TASK_PREDECESSOR_ALIVE_ANSWER:
252 XBT_DEBUG("Ignoring unexpected task of type %d (%p)", (int)type, task);
256 case TASK_PREDECESSOR_ALIVE:
257 XBT_DEBUG("Receiving a 'Predecessor Alive' request from %s", task_data->issuer_host_name);
258 task_data->type = TASK_PREDECESSOR_ALIVE_ANSWER;
259 XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)",
260 task_data->issuer_host_name, task_data->answer_to);
261 MSG_task_dsend(task, task_data->answer_to, task_free);
269 /* Initializes the current node as the first one of the system */
270 void create(node_t node)
272 XBT_DEBUG("Create a new Chord ring...");
273 set_predecessor(node, -1); // -1 means that I have no predecessor
274 print_finger_table(node);
277 /* Makes the current node join the ring, knowing the id of a node already in the ring
279 * \param node the current node
280 * \param known_id id of a node already in the ring
281 * \return 1 if the join operation succeeded, 0 otherwise
283 int join(node_t node, int known_id)
285 XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
286 set_predecessor(node, -1); // no predecessor (yet)
288 int successor_id = remote_find_successor(node, known_id, node->id);
289 if (successor_id == -1) {
290 XBT_INFO("Cannot join the ring.");
293 set_finger(node, 0, successor_id);
294 print_finger_table(node);
297 return successor_id != -1;
300 /* Makes the current node quit the system */
301 void leave(node_t node)
303 XBT_DEBUG("Well Guys! I Think it's time for me to quit ;)");
307 /* Notifies the successor and the predecessor of the current node before leaving */
308 void quit_notify(node_t node)
310 char mailbox[MAILBOX_NAME_SIZE];
311 //send the PREDECESSOR_LEAVING to our successor
312 task_data_t req_data = xbt_new0(s_task_data_t,1);
313 req_data->type = TASK_PREDECESSOR_LEAVING;
314 req_data->request_id = node->pred_id;
315 get_mailbox(node->id, req_data->answer_to);
316 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
318 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
319 XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d",node->fingers[0].id);
320 if (MSG_task_send_with_timeout(task_sent, node->fingers[0].mailbox, timeout)== MSG_TIMEOUT) {
321 XBT_DEBUG("Timeout expired when sending a 'PREDECESSOR_LEAVING' to my successor %d", node->fingers[0].id);
322 task_free(task_sent);
325 //send the SUCCESSOR_LEAVING to our predecessor
326 get_mailbox(node->pred_id, mailbox);
327 task_data_t req_data_s = xbt_new0(s_task_data_t,1);
328 req_data_s->type = TASK_SUCCESSOR_LEAVING;
329 req_data_s->request_id = node->fingers[0].id;
330 req_data_s->request_id = node->pred_id;
331 get_mailbox(node->id, req_data_s->answer_to);
332 req_data_s->issuer_host_name = MSG_host_get_name(MSG_host_self());
334 msg_task_t task_sent_s = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data_s);
335 XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d",node->pred_id);
336 if (MSG_task_send_with_timeout(task_sent_s, mailbox, timeout)== MSG_TIMEOUT) {
337 XBT_DEBUG("Timeout expired when sending a 'SUCCESSOR_LEAVING' to my predecessor %d", node->pred_id);
338 task_free(task_sent_s);
342 /* Makes the current node find the successor node of an id.
344 * \param node the current node
345 * \param id the id to find
346 * \return the id of the successor node, or -1 if the request failed
348 int find_successor(node_t node, int id)
350 // is my successor the successor?
351 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
352 return node->fingers[0].id;
355 // otherwise, ask the closest preceding finger in my table
356 int closest = closest_preceding_node(node, id);
357 return remote_find_successor(node, closest, id);
360 /* \brief Asks another node the successor node of an id.
362 * \param node the current node
363 * \param ask_to the node to ask to
364 * \param id the id to find
365 * \return the id of the successor node, or -1 if the request failed
367 int remote_find_successor(node_t node, int ask_to, int id)
371 char mailbox[MAILBOX_NAME_SIZE];
372 get_mailbox(ask_to, mailbox);
373 task_data_t req_data = xbt_new0(s_task_data_t, 1);
374 req_data->type = TASK_FIND_SUCCESSOR;
375 req_data->request_id = id;
376 get_mailbox(node->id, req_data->answer_to);
377 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
379 // send a "Find Successor" request to ask_to_id
380 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
381 XBT_DEBUG("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
382 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
385 XBT_DEBUG("Failed to send the 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
386 task_free(task_sent);
388 // receive the answer
389 XBT_DEBUG("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
390 task_sent, ask_to, id);
392 if (node->comm_receive == NULL) {
393 msg_task_t task_received = NULL;
394 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
397 res = MSG_comm_wait(node->comm_receive, timeout);
400 XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
401 task_sent, (int)res);
403 MSG_comm_destroy(node->comm_receive);
404 node->comm_receive = NULL;
407 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
408 XBT_DEBUG("Received a task (%p)", task_received);
409 task_data_t ans_data = MSG_task_get_data(task_received);
411 // Once upon a time, our code assumed that here, task_received != task_sent all the time
413 // This assumption is wrong (as messages from differing round can interleave), leading to a bug in our code.
414 // We failed to find this bug directly, as it only occurred on large platforms, leading to hardly usable traces.
415 // Instead, we used the model-checker to track down the issue by adding the following test here in the code:
416 // if (MC_is_active()) {
417 // MC_assert(task_received == task_sent);
419 // That explained the bug in a snap, with a very cool example and everything.
421 // This MC_assert is now desactivated as the case is now properly handled in our code and we don't want the
422 // MC to fail any further under that condition, but this comment is here to as a memorial for this first
423 // brillant victory of the model-checking in the SimGrid community :)
425 if (task_received != task_sent ||
426 ans_data->type != TASK_FIND_SUCCESSOR_ANSWER) {
427 // this is not the expected answer
428 MSG_comm_destroy(node->comm_receive);
429 node->comm_receive = NULL;
430 handle_task(node, task_received);
433 // this is our answer
434 XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
435 ans_data->request_id, task_received, id, ans_data->answer_id);
436 successor = ans_data->answer_id;
438 MSG_comm_destroy(node->comm_receive);
439 node->comm_receive = NULL;
440 task_free(task_received);
449 /* Asks its predecessor to a remote node
451 * \param node the current node
452 * \param ask_to the node to ask to
453 * \return the id of its predecessor node, or -1 if the request failed
454 * (or if the node does not know its predecessor)
456 int remote_get_predecessor(node_t node, int ask_to)
458 int predecessor_id = -1;
460 char mailbox[MAILBOX_NAME_SIZE];
461 get_mailbox(ask_to, mailbox);
462 task_data_t req_data = xbt_new0(s_task_data_t, 1);
463 req_data->type = TASK_GET_PREDECESSOR;
464 get_mailbox(node->id, req_data->answer_to);
465 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
467 // send a "Get Predecessor" request to ask_to_id
468 XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to);
469 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
470 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
473 XBT_DEBUG("Failed to send the 'Get Predecessor' request (task %p) to %d",
475 task_free(task_sent);
479 // receive the answer
480 XBT_DEBUG("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
481 task_sent, ask_to, req_data->answer_to);
484 if (node->comm_receive == NULL) { // FIXME simplify this
485 msg_task_t task_received = NULL;
486 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
489 res = MSG_comm_wait(node->comm_receive, timeout);
492 XBT_DEBUG("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
493 task_sent, (int)res);
495 MSG_comm_destroy(node->comm_receive);
496 node->comm_receive = NULL;
499 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
500 task_data_t ans_data = MSG_task_get_data(task_received);
502 /*if (MC_is_active()) {
503 MC_assert(task_received == task_sent);
506 if (task_received != task_sent ||
507 ans_data->type != TASK_GET_PREDECESSOR_ANSWER) {
508 MSG_comm_destroy(node->comm_receive);
509 node->comm_receive = NULL;
510 handle_task(node, task_received);
513 XBT_DEBUG("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
514 task_received, ask_to, ans_data->answer_id);
515 predecessor_id = ans_data->answer_id;
517 MSG_comm_destroy(node->comm_receive);
518 node->comm_receive = NULL;
519 task_free(task_received);
525 return predecessor_id;
528 /* Returns the closest preceding finger of an id with respect to the finger table of the current node.
530 * \param node the current node
531 * \param id the id to find
532 * \return the closest preceding finger of that id
534 int closest_preceding_node(node_t node, int id)
537 for (i = nb_bits - 1; i >= 0; i--) {
538 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
539 return node->fingers[i].id;
545 /* This function is called periodically. It checks the immediate successor of the current node. */
546 void stabilize(node_t node)
548 XBT_DEBUG("Stabilizing node");
550 // get the predecessor of my immediate successor
552 int successor_id = node->fingers[0].id;
553 if (successor_id != node->id) {
554 candidate_id = remote_get_predecessor(node, successor_id);
557 candidate_id = node->pred_id;
560 // this node is a candidate to become my new successor
561 if (candidate_id != -1
562 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
563 set_finger(node, 0, candidate_id);
565 if (successor_id != node->id) {
566 remote_notify(node, successor_id, node->id);
570 /* Notifies the current node that its predecessor may have changed. */
571 void notify(node_t node, int predecessor_candidate_id) {
573 if (node->pred_id == -1
574 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
576 set_predecessor(node, predecessor_candidate_id);
577 print_finger_table(node);
580 XBT_DEBUG("I don't have to change my predecessor to %d", predecessor_candidate_id);
584 /* Notifies a remote node that its predecessor may have changed. */
585 void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
587 task_data_t req_data = xbt_new0(s_task_data_t, 1);
588 req_data->type = TASK_NOTIFY;
589 req_data->request_id = predecessor_candidate_id;
590 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
592 // send a "Notify" request to notify_id
593 msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
594 XBT_DEBUG("Sending a 'Notify' request (task %p) to %d", task, notify_id);
595 char mailbox[MAILBOX_NAME_SIZE];
596 get_mailbox(notify_id, mailbox);
597 MSG_task_dsend(task, mailbox, task_free);
600 /* refreshes the finger table of the current node (called periodically) */
601 void fix_fingers(node_t node) {
603 XBT_DEBUG("Fixing fingers");
604 int i = node->next_finger_to_fix;
605 int id = find_successor(node, node->id + powers2[i]);
608 if (id != node->fingers[i].id) {
609 set_finger(node, i, id);
610 print_finger_table(node);
612 node->next_finger_to_fix = (i + 1) % nb_bits;
616 /* checks whether the predecessor has failed (called periodically) */
617 void check_predecessor(node_t node)
619 XBT_DEBUG("Checking whether my predecessor is alive");
621 if(node->pred_id == -1)
626 char mailbox[MAILBOX_NAME_SIZE];
627 get_mailbox(node->pred_id, mailbox);
628 task_data_t req_data = xbt_new0(s_task_data_t,1);
629 req_data->type = TASK_PREDECESSOR_ALIVE;
630 req_data->request_id = node->pred_id;
631 get_mailbox(node->id, req_data->answer_to);
632 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
634 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
635 XBT_DEBUG("Sending a 'Predecessor Alive' request to my predecessor %d", node->pred_id);
637 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
640 XBT_DEBUG("Failed to send the 'Predecessor Alive' request (task %p) to %d", task_sent, node->pred_id);
641 task_free(task_sent);
643 // receive the answer
644 XBT_DEBUG("Sent 'Predecessor Alive' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
645 task_sent, node->pred_id, req_data->answer_to);
648 if (node->comm_receive == NULL) { // FIXME simplify this
649 msg_task_t task_received = NULL;
650 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
653 res = MSG_comm_wait(node->comm_receive, timeout);
656 XBT_DEBUG("Failed to receive the answer to my 'Predecessor Alive' request (task %p): %d",
657 task_sent, (int)res);
659 MSG_comm_destroy(node->comm_receive);
660 node->comm_receive = NULL;
663 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
664 if (task_received != task_sent) {
665 MSG_comm_destroy(node->comm_receive);
666 node->comm_receive = NULL;
667 handle_task(node, task_received);
669 XBT_DEBUG("Received the answer to my 'Predecessor Alive' request (task %p) : my predecessor %d is alive",
670 task_received, node->pred_id);
672 MSG_comm_destroy(node->comm_receive);
673 node->comm_receive = NULL;
674 task_free(task_received);
681 /* Performs a find successor request to a random id */
682 void random_lookup(node_t node)
684 int random_index = RngStream_RandInt (node->stream, 0, nb_bits - 1);
685 int random_id = node->fingers[random_index].id;
686 XBT_DEBUG("Making a lookup request for id %d", random_id);
687 int res = find_successor(node, random_id);
688 XBT_DEBUG("The successor of node %d is %d", random_id, res);
691 static int node(int argc, char *argv[])
693 /* Reduce the run size for the MC */
694 if(MC_is_active() || MC_record_replay_is_active()){
695 periodic_stabilize_delay = 8;
696 periodic_fix_fingers_delay = 8;
697 periodic_check_predecessor_delay = 8;
700 double init_time = MSG_get_clock();
701 msg_task_t task_received = NULL;
703 int join_success = 0;
705 double next_stabilize_date = init_time + periodic_stabilize_delay;
706 double next_fix_fingers_date = init_time + periodic_fix_fingers_delay;
707 double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
708 double next_lookup_date = init_time + periodic_lookup_delay;
710 xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
712 // initialize my node
714 node.id = xbt_str_parse_int(argv[1],"Invalid ID: %s");
715 node.stream = (RngStream)MSG_host_get_property_value(MSG_host_self(), "stream");
716 get_mailbox(node.id, node.mailbox);
717 node.next_finger_to_fix = 0;
718 node.fingers = xbt_new0(s_finger_t, nb_bits);
719 node.last_change_date = init_time;
721 for (i = 0; i < nb_bits; i++) {
722 node.fingers[i].id = -1;
723 set_finger(&node, i, node.id);
726 if (argc == 3) { // first ring
727 deadline = xbt_str_parse_double(argv[2],"Invalid deadline: %s");
731 int known_id = xbt_str_parse_int(argv[2],"Invalid root ID: %s");
732 deadline = xbt_str_parse_double(argv[4],"Invalid deadline: %s");
734 XBT_DEBUG("Hey! Let's join the system.");
736 join_success = join(&node, known_id);
740 double now = MSG_get_clock();
743 while (now < init_time + deadline && now < max_simulation_time) {
744 if (node.comm_receive == NULL) {
745 task_received = NULL;
746 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
747 // FIXME: do not make MSG_task_irecv() calls from several functions
750 if (!MSG_comm_test(node.comm_receive)) { // no task was received: make some periodic calls
751 if(MC_is_active() || MC_record_replay_is_active()){
752 int sub_protocol = MC_random(0, 4);
753 if(MC_is_active() && !MC_visited_reduction() && no_op)
755 if(listen == 0 && (sub_protocol > 0)){
756 if(sub_protocol == 1)
758 else if(sub_protocol == 2)
760 else if(sub_protocol == 3)
761 check_predecessor(&node);
763 random_lookup(&node);
766 MSG_process_sleep(sleep_delay);
767 if(!MC_visited_reduction())
771 if (now >= next_stabilize_date) {
773 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
774 }else if (now >= next_fix_fingers_date) {
776 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
777 }else if (now >= next_check_predecessor_date) {
778 check_predecessor(&node);
779 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
780 }else if (now >= next_lookup_date) {
781 random_lookup(&node);
782 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
784 // nothing to do: sleep for a while
785 MSG_process_sleep(sleep_delay);
788 } else { // a transfer has occurred
789 msg_error_t status = MSG_comm_get_status(node.comm_receive);
790 MSG_comm_destroy(node.comm_receive);
791 node.comm_receive = NULL;
793 if (status == MSG_OK)
794 handle_task(&node, task_received);
796 XBT_DEBUG("Failed to receive a task. Nevermind.");
798 now = MSG_get_clock();
801 if (node.comm_receive) {
802 /* handle last task if any */
803 if (MSG_comm_wait(node.comm_receive, 0) == MSG_OK)
804 task_free(task_received);
805 MSG_comm_destroy(node.comm_receive);
806 node.comm_receive = NULL;
813 // stop the simulation
814 xbt_free(node.fingers);
818 int main(int argc, char *argv[])
820 MSG_init(&argc, argv);
821 xbt_assert(argc > 2, "Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n"
822 "\tExample: %s ../msg_platform.xml chord.xml\n", argv[0], argv[0]);
824 char **options = &argv[1];
825 while (!strncmp(options[0], "-", 1)) {
826 int length = strlen("-nb_bits=");
827 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
828 nb_bits = xbt_str_parse_int(options[0] + length, "Invalid nb_bits parameter: %s");
829 XBT_DEBUG("Set nb_bits to %d", nb_bits);
831 length = strlen("-timeout=");
832 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
833 timeout = xbt_str_parse_int(options[0] + length, "Invalid timeout parameter: %s");
834 XBT_DEBUG("Set timeout to %d", timeout);
836 xbt_die("Invalid chord option '%s'", options[0]);
842 MSG_create_environment(options[0]);
846 MSG_function_register("node", node);
847 MSG_launch_application(options[1]);
849 msg_error_t res = MSG_main();
850 XBT_INFO("Simulated time: %g", MSG_get_clock());
854 return res != MSG_OK;