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");
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
17 #include "deployment.h"
25 // Failure exit status
27 EXIT_NO_FAILURE = 0x00, // no error
28 EXIT_FAILURE_ARGS = 0x01, // bad arguments
29 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
30 EXIT_FAILURE_SIMU = 0x04, // simulation failed
31 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
34 int simulation_main(int argc, char* argv[])
39 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
43 catch (std::invalid_argument& e) {
44 THROW1(arg_error, 0, "%s", e.what());
49 void check_for_lost_load()
51 const double threshold = 1e-4;
52 double total_init = process::get_total_load_init();
53 double total_exit = process::get_total_load_exit();
54 double lost = total_init - total_exit;
55 double lost_ratio = 100 * lost / total_init;
56 if (lost_ratio < -threshold) {
57 CRITICAL2("Gained load at exit! %g (%g%%) <============",
59 } else if (lost_ratio > threshold) {
60 CRITICAL2("Lost load at exit! %g (%g%%) <============",
63 DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
68 int main(int argc, char* argv[])
70 // Note: variables used after THROW must be declared as volatile.
71 volatile int exit_status = 0; // global exit status
72 volatile double simulated_time = -1.0;
73 timestamp simulation_time;
77 simulation_time.start();
79 // Set default logging parameters
80 bool do_log_control_set = true;
81 for (int i = 1 ; do_log_control_set && i < argc ; i++)
82 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
83 strchr(argv[i] + 1, 'v'));
84 if (do_log_control_set) {
85 // xbt_log_control_set("simu.thres:verbose");
86 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
87 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
90 // Initialize some MSG internal data.
91 // Note: MSG_global_init() may throw an exception, but it seems
92 // impossible to catch it correctly :-(
93 MSG_global_init(&argc, argv);
95 // Parse global parameters
96 int parse_res = opt::parse_args(&argc, argv);
98 || opt::version_requested || opt::help_requested) {
99 if (opt::version_requested)
100 std::clog << version::name << " (" << opt::program_name << ")"
101 << " version " << version::num << "\n"
102 << version::copyright << "\n"
103 "Compiled on " << version::date << "\n\n";
104 if (!parse_res || opt::help_requested)
107 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
109 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
110 version::date.c_str());
114 exit_status = EXIT_FAILURE_INIT; // =====
116 // Register the default function of an agent
117 // MSG_function_register("simulation_main", simulation_main);
118 MSG_function_register_default(simulation_main);
120 // Create the platform and the application.
121 MSG_create_environment(opt::platform_file.c_str());
123 if (opt::auto_depl::enabled) {
124 if (!opt::auto_depl::nhosts)
125 opt::auto_depl::nhosts = hostdata::size();
126 if (opt::auto_depl::nhosts > hostdata::size()) {
127 WARN2("%u hosts is too much: limiting to %u",
128 opt::auto_depl::nhosts, (unsigned )hostdata::size());
129 opt::auto_depl::nhosts = hostdata::size();
131 if (!opt::auto_depl::load)
132 opt::auto_depl::load = opt::auto_depl::nhosts;
133 MY_launch_application(); // it is already opt::* aware...
135 MSG_launch_application(opt::deployment_file.c_str());
138 exit_status = EXIT_FAILURE_SIMU; // =====
140 // Launch the MSG simulation.
141 INFO1("Starting simulation at %f...", MSG_get_clock());
143 simulated_time = MSG_get_clock();
144 INFO1("Simulation ended at %f.", simulated_time);
145 check_for_lost_load();
147 THROW1(0, 0, "MSG_main() failed with status %#x", res);
149 exit_status = EXIT_NO_FAILURE; // =====
152 int len = strlen(ex.msg);
153 if (len > 0 && ex.msg[len - 1] == '\n')
154 ex.msg[len - 1] = '\0'; // strip the ending '\n'
155 ERROR1("%s", ex.msg);
156 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
160 // Clean the MSG simulation.
164 ERROR1("MSG_clean() failed with status %#x", res);
165 exit_status |= EXIT_FAILURE_CLEAN;
168 // Report final simulation status.
169 if (simulated_time >= 0.0) {
170 simulation_time.stop();
171 INFO0(",----[ Results ]");
172 INFO1("| Total simulated time...: %g", simulated_time);
173 INFO1("| Total simulation time..: %g", simulation_time.duration());
177 ERROR1("Simulation failed (%#x).", exit_status);
179 INFO0("Simulation succeeded.");