1 /* Copyright (c) 2016-2017. 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 #include "simgrid/s4u.hpp"
10 #include <xbt/RngStream.h>
14 #define MAX_SIMULATION_TIME 1000
15 #define PERIODIC_STABILIZE_DELAY 20
16 #define PERIODIC_FIX_FINGERS_DELAY 120
17 #define PERIODIC_CHECK_PREDECESSOR_DELAY 120
18 #define PERIODIC_LOOKUP_DELAY 10
19 #define SLEEP_DELAY 4.9999
28 simgrid::s4u::Host* host = nullptr;
31 static simgrid::xbt::Extension<simgrid::s4u::Host, HostChord> EXTENSION_ID;
33 explicit HostChord(simgrid::s4u::Host* ptr) : host(ptr)
35 std::string descr = std::string("RngSream<") + host->cname() + ">";
36 stream_ = RngStream_CreateStream(descr.c_str());
39 ~HostChord() { RngStream_DeleteStream(&stream_); };
41 RngStream getStream() { return stream_; };
44 /* Types of tasks exchanged between nodes. */
47 FIND_SUCCESSOR_ANSWER,
49 GET_PREDECESSOR_ANSWER,
54 PREDECESSOR_ALIVE_ANSWER
59 e_message_type_t type; // type of message
60 std::string issuer_host_name; // used for logging
61 int request_id = -1; // id (used by some types of messages)
62 int request_finger = 1; // finger parameter (used by some types of messages)
63 int answer_id = -1; // answer (used by some types of messages)
64 simgrid::s4u::MailboxPtr answer_to; // mailbox to send an answer to (if any)
66 explicit ChordMessage(e_message_type_t type) : type(type)
68 issuer_host_name = simgrid::s4u::this_actor::host()->name();
71 ~ChordMessage() = default;
76 double start_time_ = -1;
77 double deadline_ = -1;
80 int pred_id_ = -1; // predecessor id
81 simgrid::s4u::MailboxPtr mailbox_; // my mailbox
82 int* fingers_; // finger table,(fingers[0] is my successor)
83 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
87 explicit Node(std::vector<std::string> args);
89 void join(int known_id);
94 void setFinger(int finger_index, int id);
96 void printFingerTable();
98 void setPredecessor(int predecessor_id);
99 void checkPredecessor();
100 int remoteGetPredecessor(int ask_to);
101 int closestPrecedingFinger(int id);
102 int findSuccessor(int id);
103 int remoteFindSuccessor(int ask_to, int id);
105 void notify(int predecessor_candidate_id);
106 void remoteNotify(int notify_id, int predecessor_candidate_id);
108 void handleMessage(ChordMessage* message);
112 simgrid::s4u::this_actor::sleep_for(start_time_);
113 if (known_id_ == -1) {
114 setPredecessor(-1); // -1 means that I have no predecessor
123 ChordMessage* message = nullptr;
124 void* data = nullptr;
125 double now = simgrid::s4u::Engine::getClock();
126 double next_stabilize_date = start_time_ + PERIODIC_STABILIZE_DELAY;
127 double next_fix_fingers_date = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
128 double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
129 double next_lookup_date = start_time_ + PERIODIC_LOOKUP_DELAY;
131 while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
133 simgrid::s4u::Comm& comm_receive = simgrid::s4u::this_actor::irecv(mailbox_, &data);
134 while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && !comm_receive.test()) {
135 // no task was received: make some periodic calls
136 if (now >= next_stabilize_date) {
138 next_stabilize_date = simgrid::s4u::Engine::getClock() + PERIODIC_STABILIZE_DELAY;
139 } else if (now >= next_fix_fingers_date) {
141 next_fix_fingers_date = simgrid::s4u::Engine::getClock() + PERIODIC_FIX_FINGERS_DELAY;
142 } else if (now >= next_check_predecessor_date) {
144 next_check_predecessor_date = simgrid::s4u::Engine::getClock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
145 } else if (now >= next_lookup_date) {
147 next_lookup_date = simgrid::s4u::Engine::getClock() + PERIODIC_LOOKUP_DELAY;
149 // nothing to do: sleep for a while
150 simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
152 now = simgrid::s4u::Engine::getClock();
155 if (data != nullptr) {
156 message = static_cast<ChordMessage*>(data);
157 handleMessage(message);
159 now = simgrid::s4u::Engine::getClock();
161 if (data != nullptr) {
162 delete static_cast<ChordMessage*>(data);