7 // Creates log categories
8 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
9 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
10 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
11 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
12 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
13 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
14 XBT_LOG_NEW_SUBCATEGORY(thrd, simu, "Messages from thread wrapper class");
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
18 #include "deployment.h"
23 #include "statistics.h"
29 #define DATA_DESCR_WIDTH 37
32 // Failure exit status
34 EXIT_NO_FAILURE = 0x00, // no error
35 EXIT_FAILURE_ARGS = 0x01, // bad arguments
36 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
37 EXIT_FAILURE_SIMU = 0x04, // simulation failed
38 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
41 // Cannot be globally initialized...
43 condition_t* proc_cond;
44 unsigned proc_counter = 0;
48 statistics data_send_amount;
49 statistics data_recv_amount;
50 statistics data_send_count;
51 statistics data_recv_count;
52 statistics ctrl_send_amount;
53 statistics ctrl_recv_amount;
54 statistics ctrl_send_count;
55 statistics ctrl_recv_count;
59 static int simulation_main(int argc, char* argv[])
64 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
66 proc_mutex->acquire();
68 proc_mutex->release();
72 proc_mutex->acquire();
73 loads.push(proc->get_real_load());
74 comps.push(proc->get_comp_amount());
75 data_send_amount.push(proc->get_data_send_amount());
76 data_recv_amount.push(proc->get_data_recv_amount());
77 data_send_count.push(proc->get_data_send_count());
78 data_recv_count.push(proc->get_data_recv_count());
79 ctrl_send_amount.push(proc->get_ctrl_send_amount());
80 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
81 ctrl_send_count.push(proc->get_ctrl_send_count());
82 ctrl_recv_count.push(proc->get_ctrl_recv_count());
84 // Synchronization barrier...
85 // The goal is to circumvent a limitation in SimGrid (at least
86 // in version 3.5): a process must be alive when another one
87 // destroys a communication they had together.
90 proc_cond->broadcast();
91 while (proc_counter > 0)
92 proc_cond->wait(*proc_mutex);
93 proc_mutex->release();
97 catch (const std::invalid_argument& e) {
98 THROW1(arg_error, 0, "%s", e.what());
100 catch (const std::exception& e) {
101 THROW1(0, 0, "%s", e.what());
106 static void check_for_lost_load()
108 double total_init = process::get_total_load_init();
109 double total_exit = process::get_total_load_exit();
110 double lost = total_init - total_exit;
111 double lost_ratio = 100.0 * lost / total_init;
112 if (lost_ratio < -opt::load_ratio_threshold)
113 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
115 else if (lost_ratio > opt::load_ratio_threshold)
116 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
119 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
121 double total_running = process::get_total_load_running();
122 double running_ratio = 100.0 * total_running / total_init;
123 if (running_ratio < -opt::load_ratio_threshold)
124 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
125 total_running, running_ratio);
126 else if (running_ratio > opt::load_ratio_threshold)
127 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
128 total_running, running_ratio);
130 XBT_VERB("Running load at exit looks good: %g (%g%%)",
131 total_running, running_ratio);
134 #define PR_STATS(descr, st) \
135 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
136 descr " (total/avg./stddev)................................", \
137 st.get_sum(), st.get_mean(), st.get_stddev())
139 int main(int argc, char* argv[])
141 // Note: variables used after THROW must be declared as volatile.
142 volatile int exit_status = 0; // global exit status
143 volatile double simulated_time = -1.0;
144 timestamp simulation_time;
148 simulation_time.start();
150 // Set default logging parameters
151 bool do_log_control_set = true;
152 for (int i = 1 ; do_log_control_set && i < argc ; i++)
153 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
154 strchr(argv[i] + 1, 'v'));
155 if (do_log_control_set) {
156 // xbt_log_control_set("simu.thres:verbose");
157 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
158 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
161 // Initialize some MSG internal data.
162 // Note: MSG_global_init() may throw an exception, but it seems
163 // impossible to catch it correctly :-(
164 MSG_global_init(&argc, argv);
166 // Parse global parameters
167 bool parse_res = opt::parse_args(&argc, argv);
169 || opt::version_requested || opt::help_requested) {
170 if (opt::version_requested)
171 std::clog << version::name << " (" << opt::program_name << ")"
172 << " version " << version::num << "\n"
173 << version::copyright << "\n"
174 "Compiled on " << version::date << "\n\n";
175 if (!parse_res || opt::help_requested)
178 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
180 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
181 version::date.c_str());
185 exit_status = EXIT_FAILURE_INIT; // =====
187 // Register the default function of an agent
188 // MSG_function_register("simulation_main", simulation_main);
189 MSG_function_register_default(simulation_main);
191 // Create the platform and the application.
192 XBT_DEBUG("Loading platform file...");
193 MSG_create_environment(opt::platform_file.c_str());
194 XBT_DEBUG("Creating hostdata...");
196 XBT_DEBUG("Deploying processes...");
197 if (opt::auto_depl::enabled) {
198 if (!opt::auto_depl::nhosts)
199 opt::auto_depl::nhosts = hostdata::size();
200 if (opt::auto_depl::nhosts > hostdata::size()) {
201 XBT_WARN("%u hosts is too much: limiting to %zu",
202 opt::auto_depl::nhosts, hostdata::size());
203 opt::auto_depl::nhosts = hostdata::size();
205 if (!opt::auto_depl::load)
206 opt::auto_depl::load = opt::auto_depl::nhosts;
207 MY_launch_application(); // it is already opt::* aware...
209 MSG_launch_application(opt::deployment_file.c_str());
212 // Register tracing categories
213 TRACE_category(TRACE_CAT_COMP);
214 TRACE_category(TRACE_CAT_CTRL);
215 TRACE_category(TRACE_CAT_DATA);
217 exit_status = EXIT_FAILURE_SIMU; // =====
219 proc_mutex = new mutex_t();
220 proc_cond = new condition_t();
222 // Launch the MSG simulation.
223 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
225 simulated_time = MSG_get_clock();
226 XBT_INFO("Simulation ended at %f.", simulated_time);
232 THROW1(0, 0, "MSG_main() failed with status %#x", res);
234 exit_status = EXIT_NO_FAILURE; // =====
237 int len = strlen(ex.msg);
238 if (len > 0 && ex.msg[len - 1] == '\n')
239 ex.msg[len - 1] = '\0'; // strip the ending '\n'
240 XBT_ERROR("%s", ex.msg);
241 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
245 // Clean the MSG simulation.
249 XBT_ERROR("MSG_clean() failed with status %#x", res);
250 exit_status |= EXIT_FAILURE_CLEAN;
253 // Report final simulation status.
254 if (simulated_time >= 0.0) {
255 simulation_time.stop();
256 check_for_lost_load();
257 XBT_INFO(",----[ Results ]");
258 PR_STATS("Load", loads);
259 PR_STATS("Computation", comps);
260 PR_STATS("Data send amount", data_send_amount);
261 PR_STATS("Data recv amount", data_recv_amount);
262 PR_STATS("Data send count", data_send_count);
263 PR_STATS("Data recv count", data_recv_count);
264 PR_STATS("Ctrl send amount", ctrl_send_amount);
265 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
266 PR_STATS("Ctrl send count", ctrl_send_count);
267 PR_STATS("Ctrl recv count", ctrl_recv_count);
268 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
269 "Total simulated time..................................",
271 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
272 "Total simulation time.................................",
273 simulation_time.duration());
277 XBT_ERROR("Simulation failed (%#x).", exit_status);
279 XBT_INFO("Simulation succeeded.");