6 #include "deployment.h"
14 // Creates log categories
15 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
16 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
17 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
18 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
19 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
20 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
24 // Failure exit status
26 EXIT_NO_FAILURE = 0x00, // no error
27 EXIT_FAILURE_ARGS = 0x01, // bad arguments
28 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
29 EXIT_FAILURE_SIMU = 0x04, // simulation failed
30 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
33 int simulation_main(int argc, char* argv[])
38 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
42 catch (std::invalid_argument& e) {
43 THROW1(arg_error, 0, "%s", e.what());
48 void check_for_lost_load()
50 const double threshold = 1e-4;
51 double total_init = process::get_total_load_init();
52 double total_exit = process::get_total_load_exit();
53 double lost = total_init - total_exit;
54 double lost_ratio = 100 * lost / total_init;
55 if (lost_ratio < -threshold) {
56 CRITICAL2("Gained load at exit! %g (%g%%) <============",
58 } else if (lost_ratio > threshold) {
59 CRITICAL2("Lost load at exit! %g (%g%%) <============",
62 DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
67 int main(int argc, char* argv[])
69 // Note: variables used after THROW must be declared as volatile.
70 volatile int exit_status = 0; // global exit status
71 volatile double simulated_time = -1.0;
72 timestamp simulation_time;
76 simulation_time.start();
78 // Set default logging parameters
79 bool do_log_control_set = true;
80 for (int i = 1 ; do_log_control_set && i < argc ; i++)
81 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
82 strchr(argv[i] + 1, 'v'));
83 if (do_log_control_set) {
84 // xbt_log_control_set("simu.thres:verbose");
85 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
86 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
89 // Initialize some MSG internal data.
90 // Note: MSG_global_init() may throw an exception, but it seems
91 // impossible to catch it correctly :-(
92 MSG_global_init(&argc, argv);
94 // Parse global parameters
95 int parse_res = opt::parse_args(&argc, argv);
97 || opt::version_requested || opt::help_requested) {
98 if (opt::version_requested)
99 std::clog << version::name << " (" << opt::program_name << ")"
100 << " version " << version::num << "\n"
101 << version::copyright << "\n"
102 "Compiled on " << version::date << "\n\n";
103 if (!parse_res || opt::help_requested)
106 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
108 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
109 version::date.c_str());
113 exit_status = EXIT_FAILURE_INIT; // =====
115 // Register the default function of an agent
116 // MSG_function_register("simulation_main", simulation_main);
117 MSG_function_register_default(simulation_main);
119 // Create the platform and the application.
120 MSG_create_environment(opt::platform_file.c_str());
122 if (opt::auto_depl::enabled) {
123 if (!opt::auto_depl::nhosts)
124 opt::auto_depl::nhosts = hostdata::size();
125 if (opt::auto_depl::nhosts > hostdata::size()) {
126 WARN2("%u hosts is too much: limiting to %u",
127 opt::auto_depl::nhosts, (unsigned )hostdata::size());
128 opt::auto_depl::nhosts = hostdata::size();
130 if (!opt::auto_depl::load)
131 opt::auto_depl::load = opt::auto_depl::nhosts;
132 MY_launch_application(); // it is already opt::* aware...
134 MSG_launch_application(opt::deployment_file.c_str());
137 exit_status = EXIT_FAILURE_SIMU; // =====
139 // Launch the MSG simulation.
140 INFO1("Starting simulation at %f...", MSG_get_clock());
142 simulated_time = MSG_get_clock();
143 INFO1("Simulation ended at %f.", simulated_time);
144 check_for_lost_load();
146 THROW1(0, 0, "MSG_main() failed with status %#x", res);
148 exit_status = EXIT_NO_FAILURE; // =====
151 int len = strlen(ex.msg);
152 if (len > 0 && ex.msg[len - 1] == '\n')
153 ex.msg[len - 1] = '\0'; // strip the ending '\n'
154 ERROR1("%s", ex.msg);
155 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
159 // Clean the MSG simulation.
163 ERROR1("MSG_clean() failed with status %#x", res);
164 exit_status |= EXIT_FAILURE_CLEAN;
167 // Report final simulation status.
168 if (simulated_time >= 0.0) {
169 simulation_time.stop();
170 INFO0(",----[ Results ]");
171 INFO1("| Total simulated time...: %g", simulated_time);
172 INFO1("| Total simulation time..: %g", simulation_time.duration());
176 ERROR1("Simulation failed (%#x).", exit_status);
178 INFO0("Simulation succeeded.");