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 iter_deviation;
57 statistics data_send_amount;
58 statistics data_recv_amount;
59 statistics data_send_count;
60 statistics data_recv_count;
61 statistics ctrl_send_amount;
62 statistics ctrl_recv_amount;
63 statistics ctrl_send_count;
64 statistics ctrl_recv_count;
65 statistics idle_duration;
66 statistics convergence;
70 static int simulation_main(int argc, char* argv[])
75 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
77 proc_mutex->acquire();
79 proc_mutex->release();
83 proc_mutex->acquire();
84 loads.push(proc->get_real_load());
85 comps.push(proc->get_comp_amount());
86 comp_iterations.push(proc->get_comp_iter());
87 all_comp_iterations.push(proc->get_all_comp_iter());
88 iter_deviation.push(proc->get_iter_deviation());
89 data_send_amount.push(proc->get_data_send_amount());
90 data_recv_amount.push(proc->get_data_recv_amount());
91 data_send_count.push(proc->get_data_send_count());
92 data_recv_count.push(proc->get_data_recv_count());
93 ctrl_send_amount.push(proc->get_ctrl_send_amount());
94 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
95 ctrl_send_count.push(proc->get_ctrl_send_count());
96 ctrl_recv_count.push(proc->get_ctrl_recv_count());
97 idle_duration.push(proc->get_idle_duration());
98 double c = proc->get_convergence();
102 // Synchronization barrier...
103 // The goal is to circumvent a limitation in SimGrid (at least
104 // in version 3.5): a process must be alive when another one
105 // destroys a communication they had together.
108 proc_cond->broadcast();
109 while (proc_counter > 0)
110 proc_cond->wait(*proc_mutex);
111 proc_mutex->release();
115 catch (const std::invalid_argument& e) {
116 THROWF(arg_error, 0, "%s", e.what());
118 catch (const std::exception& e) {
119 THROWF(0, 0, "%s", e.what());
124 static bool check_for_lost_load()
127 double total_init = process::get_total_load_init();
128 double total_exit = process::get_total_load_exit();
129 double lost = total_init - total_exit;
130 double lost_ratio = 100.0 * lost / total_init;
131 if (lost_ratio < -opt::load_ratio_threshold) {
132 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
135 } else if (lost_ratio > opt::load_ratio_threshold) {
136 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
140 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
142 double total_running = process::get_total_load_running();
143 double running_ratio = 100.0 * total_running / total_init;
144 if (running_ratio < -opt::load_ratio_threshold) {
145 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
146 total_running, running_ratio);
148 } else if (running_ratio > opt::load_ratio_threshold) {
149 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
150 total_running, running_ratio);
153 XBT_VERB("Running load at exit looks good: %g (%g%%)",
154 total_running, running_ratio);
158 static void signal_handler(int /*sig*/)
160 if (!opt::exit_request) {
161 XBT_CRITICAL(">>>>>>>>>>"
162 " caught CTRL-C: global exit requested "
164 opt::exit_request = 1;
166 XBT_CRITICAL(">>>>>>>>>>"
167 " caught CTRL-C for the 2nd time: exit immediately "
169 exit(EXIT_FAILURE_INTR);
173 static void install_signal_handler()
175 struct sigaction action;
176 action.sa_handler = signal_handler;
177 sigemptyset(&action.sa_mask);
178 action.sa_flags = SA_RESTART;
179 if (sigaction(SIGINT, &action, NULL) == -1) {
180 std::cerr << "sigaction: " << strerror(errno) << "\n";
181 exit(EXIT_FAILURE_OTHER);
185 #define PR_VALUE(descr, format, ...) \
186 XBT_INFO("| %.*s: " format, DATA_DESCR_WIDTH, \
187 descr ".................................................", \
190 #define PR_STATS(descr, st) \
191 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
192 descr " (sum/avg/dev)...................................", \
193 st.get_sum(), st.get_mean(), st.get_stddev())
195 int main(int argc, char* argv[])
197 // Note: variables used after THROW must be declared as volatile.
198 volatile int exit_status = 0; // global exit status
199 volatile double simulated_time = -1.0;
200 timestamp elapsed_time(timestamp::wallclock_time);
201 timestamp simulation_time(timestamp::cpu_time);
205 elapsed_time.start();
206 simulation_time.start();
208 // Set default logging parameters
209 bool do_log_control_set = true;
210 for (int i = 1 ; do_log_control_set && i < argc ; i++)
211 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
212 strchr(argv[i] + 1, 'v'));
213 if (do_log_control_set) {
214 // xbt_log_control_set("simu.thres:verbose");
215 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
216 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
219 // Initialize some MSG internal data.
220 // Note: MSG_global_init() may throw an exception, but it seems
221 // impossible to catch it correctly :-(
222 MSG_global_init(&argc, argv);
223 install_signal_handler();
225 // Parse global parameters
226 bool parse_res = opt::parse_args(&argc, argv);
228 || opt::version_requested || opt::help_requested) {
229 if (opt::version_requested)
230 std::clog << version::name << " (" << opt::program_name << ")"
231 << " version " << version::num << "\n"
232 << version::copyright << "\n"
233 "Compiled on " << version::date << "\n\n";
234 if (!parse_res || opt::help_requested)
237 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
239 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
240 version::date.c_str());
244 exit_status = EXIT_FAILURE_INIT; // =====
246 // Register the default function of an agent
247 // MSG_function_register("simulation_main", simulation_main);
248 MSG_function_register_default(simulation_main);
250 // Create the platform and the application.
251 XBT_DEBUG("Loading platform file...");
252 MSG_create_environment(opt::platform_file.c_str());
253 XBT_DEBUG("Creating hostdata...");
255 XBT_INFO("Loaded description of %zd hosts.", hostdata::size());
256 XBT_DEBUG("Deploying processes...");
257 if (opt::auto_depl::enabled) {
258 if (!opt::auto_depl::nhosts)
259 opt::auto_depl::nhosts = hostdata::size();
260 if (opt::auto_depl::nhosts > hostdata::size()) {
261 XBT_WARN("%u hosts is too much: limiting to %zu",
262 opt::auto_depl::nhosts, hostdata::size());
263 opt::auto_depl::nhosts = hostdata::size();
265 if (opt::auto_depl::load == 0.0) {
266 XBT_WARN("Initial load is zero! "
267 "Falling back on old behaviour (load = nhosts).");
268 opt::auto_depl::load = opt::auto_depl::nhosts;
269 } else if (opt::auto_depl::load < 0.0)
270 opt::auto_depl::load =
271 -opt::auto_depl::load * opt::auto_depl::nhosts;
272 double iload = std::trunc(opt::auto_depl::load);
273 if (opt::integer_transfer && opt::auto_depl::load != iload) {
274 XBT_WARN("Total load %g is not an integer. Truncate it.",
275 opt::auto_depl::load);
276 opt::auto_depl::load = iload;
278 MY_launch_application(); // it is already opt::* aware...
280 MSG_launch_application(opt::deployment_file.c_str());
283 // Register tracing categories
284 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
285 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
286 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
288 exit_status = EXIT_FAILURE_SIMU; // =====
290 proc_mutex = new mutex_t();
291 proc_cond = new condition_t();
293 // Launch the MSG simulation.
294 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
296 simulated_time = MSG_get_clock();
297 XBT_INFO("Simulation ended at %f.", simulated_time);
303 THROWF(0, 0, "MSG_main() failed with status %#x", res);
305 exit_status = EXIT_NO_FAILURE; // =====
308 int len = strlen(ex.msg);
309 if (len > 0 && ex.msg[len - 1] == '\n')
310 ex.msg[len - 1] = '\0'; // strip the ending '\n'
311 XBT_ERROR("%s", ex.msg);
312 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
316 // Clean the MSG simulation.
320 XBT_ERROR("MSG_clean() failed with status %#x", res);
321 exit_status |= EXIT_FAILURE_CLEAN;
324 // Report final simulation status.
325 if (simulated_time >= 0.0) {
326 simulation_time.stop();
328 if (!check_for_lost_load())
329 exit_status |= EXIT_FAILURE_LOAD;
331 XBT_INFO(",----[ Results ]");
332 PR_STATS("Load", loads);
333 PR_STATS("Computation", comps);
334 PR_STATS("Comp. iterations", comp_iterations);
335 PR_STATS("X-Comp. iterations", all_comp_iterations);
336 PR_STATS("Supernumer. comp. iter.", iter_deviation);
337 PR_STATS("Idle duration", idle_duration);
338 PR_STATS("Data send amount", data_send_amount);
339 PR_STATS("Data recv amount", data_recv_amount);
340 PR_STATS("Data send count", data_send_count);
341 PR_STATS("Data recv count", data_recv_count);
342 PR_STATS("Ctrl send amount", ctrl_send_amount);
343 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
344 PR_STATS("Ctrl send count", ctrl_send_count);
345 PR_STATS("Ctrl recv count", ctrl_recv_count);
346 PR_VALUE("Total simulated time", "%g", simulated_time);
347 PR_VALUE("Total simulation time", "%g", simulation_time.duration());
348 PR_VALUE("Elapsed (wall clock) time", "%g", elapsed_time.duration());
351 double load_imbalance = 100.0 * loads.get_stddev() / loads.get_mean();
352 double transfer_amount =
353 data_send_amount.get_sum() / opt::comm_cost(loads.get_sum());
355 XBT_INFO(",----[ Useful metrics ]");
356 PR_VALUE("Final load imbalance", "%g %s", load_imbalance,
357 "percent of the load average");
358 PR_VALUE("Data transfer amount", "%g %s", transfer_amount,
359 "times the total amount of data");
360 PR_VALUE("Number of hosts that converged", "%u / %u",
361 convergence.get_count(), loads.get_count());
362 PR_VALUE("Times of convergence (min/max/avg/dev)", "%g / %g / %g / %g",
363 convergence.get_min(), convergence.get_max(),
364 convergence.get_mean(), convergence.get_stddev());
369 XBT_ERROR("Simulation failed (%#x).", exit_status);
371 XBT_INFO("Simulation succeeded.");