4 #include <cstring> // strchr
10 // Creates log categories
11 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
12 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
13 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
14 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
15 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
16 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
17 XBT_LOG_NEW_SUBCATEGORY(thrd, simu, "Messages from thread wrapper class");
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
21 #include "deployment.h"
26 #include "statistics.h"
32 #define DATA_DESCR_WIDTH 39
35 // Failure exit status
37 EXIT_NO_FAILURE = 0x00, // no error
38 EXIT_FAILURE_ARGS = 0x01, // bad arguments
39 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
40 EXIT_FAILURE_SIMU = 0x04, // simulation failed
41 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
42 EXIT_FAILURE_INTR = 0x10, // interrupted by user
43 EXIT_FAILURE_LOAD = 0x20, // lost load on exit
44 EXIT_FAILURE_OTHER = 0x40, // other error
47 // Cannot be globally initialized...
49 condition_t* proc_cond;
50 unsigned proc_counter = 0;
54 statistics comp_iterations;
55 statistics all_comp_iterations;
56 statistics data_send_amount;
57 statistics data_recv_amount;
58 statistics data_send_count;
59 statistics data_recv_count;
60 statistics ctrl_send_amount;
61 statistics ctrl_recv_amount;
62 statistics ctrl_send_count;
63 statistics ctrl_recv_count;
64 statistics idle_duration;
65 statistics convergence;
69 static int simulation_main(int argc, char* argv[])
74 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
76 proc_mutex->acquire();
78 proc_mutex->release();
82 proc_mutex->acquire();
83 loads.push(proc->get_real_load());
84 comps.push(proc->get_comp_amount());
85 comp_iterations.push(proc->get_comp_iter());
86 all_comp_iterations.push(proc->get_all_comp_iter());
87 data_send_amount.push(proc->get_data_send_amount());
88 data_recv_amount.push(proc->get_data_recv_amount());
89 data_send_count.push(proc->get_data_send_count());
90 data_recv_count.push(proc->get_data_recv_count());
91 ctrl_send_amount.push(proc->get_ctrl_send_amount());
92 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
93 ctrl_send_count.push(proc->get_ctrl_send_count());
94 ctrl_recv_count.push(proc->get_ctrl_recv_count());
95 idle_duration.push(proc->get_idle_duration());
96 double c = proc->get_convergence();
100 // Synchronization barrier...
101 // The goal is to circumvent a limitation in SimGrid (at least
102 // in version 3.5): a process must be alive when another one
103 // destroys a communication they had together.
106 proc_cond->broadcast();
107 while (proc_counter > 0)
108 proc_cond->wait(*proc_mutex);
109 proc_mutex->release();
113 catch (const std::invalid_argument& e) {
114 THROWF(arg_error, 0, "%s", e.what());
116 catch (const std::exception& e) {
117 THROWF(0, 0, "%s", e.what());
122 static bool check_for_lost_load()
125 double total_init = process::get_total_load_init();
126 double total_exit = process::get_total_load_exit();
127 double lost = total_init - total_exit;
128 double lost_ratio = 100.0 * lost / total_init;
129 if (lost_ratio < -opt::load_ratio_threshold) {
130 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
133 } else if (lost_ratio > opt::load_ratio_threshold) {
134 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
138 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
140 double total_running = process::get_total_load_running();
141 double running_ratio = 100.0 * total_running / total_init;
142 if (running_ratio < -opt::load_ratio_threshold) {
143 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
144 total_running, running_ratio);
146 } else if (running_ratio > opt::load_ratio_threshold) {
147 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
148 total_running, running_ratio);
151 XBT_VERB("Running load at exit looks good: %g (%g%%)",
152 total_running, running_ratio);
156 static void signal_handler(int /*sig*/)
158 if (!opt::exit_request) {
159 XBT_CRITICAL(">>>>>>>>>>"
160 " caught CTRL-C: global exit requested "
162 opt::exit_request = 1;
164 XBT_CRITICAL(">>>>>>>>>>"
165 " caught CTRL-C for the 2nd time: exit immediately "
167 exit(EXIT_FAILURE_INTR);
171 static void install_signal_handler()
173 struct sigaction action;
174 action.sa_handler = signal_handler;
175 sigemptyset(&action.sa_mask);
176 action.sa_flags = SA_RESTART;
177 if (sigaction(SIGINT, &action, NULL) == -1) {
178 std::cerr << "sigaction: " << strerror(errno) << "\n";
179 exit(EXIT_FAILURE_OTHER);
183 #define PR_VALUE(descr, format, ...) \
184 XBT_INFO("| %.*s: " format, DATA_DESCR_WIDTH, \
185 descr ".................................................", \
188 #define PR_STATS(descr, st) \
189 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
190 descr " (total/avg./stddev).............................", \
191 st.get_sum(), st.get_mean(), st.get_stddev())
193 int main(int argc, char* argv[])
195 // Note: variables used after THROW must be declared as volatile.
196 volatile int exit_status = 0; // global exit status
197 volatile double simulated_time = -1.0;
198 timestamp elapsed_time(timestamp::wallclock_time);
199 timestamp simulation_time(timestamp::cpu_time);
203 elapsed_time.start();
204 simulation_time.start();
206 // Set default logging parameters
207 bool do_log_control_set = true;
208 for (int i = 1 ; do_log_control_set && i < argc ; i++)
209 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
210 strchr(argv[i] + 1, 'v'));
211 if (do_log_control_set) {
212 // xbt_log_control_set("simu.thres:verbose");
213 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
214 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
217 // Initialize some MSG internal data.
218 // Note: MSG_global_init() may throw an exception, but it seems
219 // impossible to catch it correctly :-(
220 MSG_global_init(&argc, argv);
221 install_signal_handler();
223 // Parse global parameters
224 bool parse_res = opt::parse_args(&argc, argv);
226 || opt::version_requested || opt::help_requested) {
227 if (opt::version_requested)
228 std::clog << version::name << " (" << opt::program_name << ")"
229 << " version " << version::num << "\n"
230 << version::copyright << "\n"
231 "Compiled on " << version::date << "\n\n";
232 if (!parse_res || opt::help_requested)
235 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
237 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
238 version::date.c_str());
242 exit_status = EXIT_FAILURE_INIT; // =====
244 // Register the default function of an agent
245 // MSG_function_register("simulation_main", simulation_main);
246 MSG_function_register_default(simulation_main);
248 // Create the platform and the application.
249 XBT_DEBUG("Loading platform file...");
250 MSG_create_environment(opt::platform_file.c_str());
251 XBT_DEBUG("Creating hostdata...");
253 XBT_INFO("Loaded description of %zd hosts.", hostdata::size());
254 XBT_DEBUG("Deploying processes...");
255 if (opt::auto_depl::enabled) {
256 if (!opt::auto_depl::nhosts)
257 opt::auto_depl::nhosts = hostdata::size();
258 if (opt::auto_depl::nhosts > hostdata::size()) {
259 XBT_WARN("%u hosts is too much: limiting to %zu",
260 opt::auto_depl::nhosts, hostdata::size());
261 opt::auto_depl::nhosts = hostdata::size();
263 if (opt::auto_depl::load == 0.0) {
264 XBT_WARN("Initial load is zero! "
265 "Falling back on old behaviour (load = nhosts).");
266 opt::auto_depl::load = opt::auto_depl::nhosts;
267 } else if (opt::auto_depl::load < 0.0)
268 opt::auto_depl::load =
269 -opt::auto_depl::load * opt::auto_depl::nhosts;
270 double iload = std::trunc(opt::auto_depl::load);
271 if (opt::integer_transfer && opt::auto_depl::load != iload) {
272 XBT_WARN("Total load %g is not an integer. Truncate it.",
273 opt::auto_depl::load);
274 opt::auto_depl::load = iload;
276 MY_launch_application(); // it is already opt::* aware...
278 MSG_launch_application(opt::deployment_file.c_str());
281 // Register tracing categories
282 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
283 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
284 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
286 exit_status = EXIT_FAILURE_SIMU; // =====
288 proc_mutex = new mutex_t();
289 proc_cond = new condition_t();
291 // Launch the MSG simulation.
292 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
294 simulated_time = MSG_get_clock();
295 XBT_INFO("Simulation ended at %f.", simulated_time);
301 THROWF(0, 0, "MSG_main() failed with status %#x", res);
303 exit_status = EXIT_NO_FAILURE; // =====
306 int len = strlen(ex.msg);
307 if (len > 0 && ex.msg[len - 1] == '\n')
308 ex.msg[len - 1] = '\0'; // strip the ending '\n'
309 XBT_ERROR("%s", ex.msg);
310 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
314 // Clean the MSG simulation.
318 XBT_ERROR("MSG_clean() failed with status %#x", res);
319 exit_status |= EXIT_FAILURE_CLEAN;
322 // Report final simulation status.
323 if (simulated_time >= 0.0) {
324 simulation_time.stop();
326 if (!check_for_lost_load())
327 exit_status |= EXIT_FAILURE_LOAD;
329 XBT_INFO(",----[ Results ]");
330 PR_STATS("Load", loads);
331 PR_STATS("Computation", comps);
332 PR_STATS("Comp. iterations", comp_iterations);
333 PR_STATS("X-Comp. iterations", all_comp_iterations);
334 PR_STATS("Idle duration", idle_duration);
335 PR_STATS("Data send amount", data_send_amount);
336 PR_STATS("Data recv amount", data_recv_amount);
337 PR_STATS("Data send count", data_send_count);
338 PR_STATS("Data recv count", data_recv_count);
339 PR_STATS("Ctrl send amount", ctrl_send_amount);
340 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
341 PR_STATS("Ctrl send count", ctrl_send_count);
342 PR_STATS("Ctrl recv count", ctrl_recv_count);
343 PR_VALUE("Total simulated time", "%g", simulated_time);
344 PR_VALUE("Total simulation time", "%g", simulation_time.duration());
345 PR_VALUE("Elapsed (wall clock) time", "%g", elapsed_time.duration());
348 double load_imbalance = 100.0 * loads.get_stddev() / loads.get_mean();
349 double transfer_amount =
350 data_send_amount.get_sum() / opt::comm_cost(loads.get_sum());
352 XBT_INFO(",----[ Useful metrics ]");
353 PR_VALUE("Final load imbalance", "%g %s", load_imbalance,
354 "percent of the load average");
355 PR_VALUE("Data transfer amount", "%g %s", transfer_amount,
356 "times the total amount of data");
357 PR_VALUE("Number of hosts that converged", "%u / %u",
358 convergence.get_count(), loads.get_count());
359 PR_VALUE("Times of convergence (min/max/avg/dev)", "%g / %g / %g / %g",
360 convergence.get_min(), convergence.get_max(),
361 convergence.get_mean(), convergence.get_stddev());
366 XBT_ERROR("Simulation failed (%#x).", exit_status);
368 XBT_INFO("Simulation succeeded.");