11 // Creates a new log category and makes it the default
12 XBT_LOG_NEW_DEFAULT_CATEGORY(simu, "Simulation messages");
14 // Failure exit status
16 EXIT_NO_FAILURE = 0x00, // no error
17 EXIT_FAILURE_ARGS = 0x01, // bad arguments
18 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
19 EXIT_FAILURE_SIMU = 0x04, // simulation failed
20 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
23 int simulation_main(int argc, char *argv[])
25 process proc(argc, argv);
29 int main(int argc, char *argv[])
31 // Note: variables used after THROW must be declared as volatile.
32 volatile int exit_status = 0; // global exit status
33 volatile double simulated_time = -1.0;
34 timestamp simulation_time;
38 simulation_time.start();
40 // Set default logging parameters
41 // xbt_log_control_set("simu.thres:verbose");
42 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
44 // Initialize some MSG internal data.
45 // Note: MSG_global_init() may throw an exception, but it seems
46 // impossible to catch it correctly :-(
47 MSG_global_init(&argc, argv);
49 // Parse global parameters
50 int parse_res = opt::parse_args(&argc, argv);
52 || opt::version_requested || opt::help_requested) {
53 if (opt::version_requested)
54 std::clog << version::name << " version " << version::num << "\n"
55 << version::copyright << "\n"
56 "Compiled on " << version::date << "\n\n";
57 if (!parse_res || opt::help_requested)
60 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
65 exit_status = EXIT_FAILURE_INIT; // =====
67 // Register the main function of an agent in a global table.
68 MSG_function_register("simulation_main", simulation_main);
69 // Preserve some compatibility with old code...
70 MSG_function_register("Calculs", simulation_main);
72 // Create the platform and the application.
73 MSG_create_environment(opt::platform_file);
74 if (LOG_ISENABLED(xbt_log_priority_verbose)) {
75 int n = MSG_get_host_number();
76 m_host_t *h = MSG_get_host_table();
77 VERB1("Got %d hosts.", n);
78 for (int i = 0; i < n; i++)
79 VERB2("Host #%d named \"%s\".", i, MSG_host_get_name(h[i]));
82 MSG_launch_application(opt::application_file);
84 exit_status = EXIT_FAILURE_SIMU; // =====
86 // Launch the MSG simulation.
87 INFO0("Starting simulation...");
89 INFO0("Simulation ended.");
90 simulated_time = MSG_get_clock();
92 THROW1(0, 0, "MSG_main() failed with status %#x", res);
94 exit_status = EXIT_NO_FAILURE; // =====
97 int len = strlen(ex.msg);
98 if (len > 0 && ex.msg[len - 1] == '\n')
99 ex.msg[len - 1] = '\0'; // strip the ending '\n'
100 ERROR1("%s", ex.msg);
101 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
105 // Clean the MSG simulation.
108 ERROR1("MSG_clean() failed with status %#x", res);
109 exit_status |= EXIT_FAILURE_CLEAN;
112 // Report final simulation status.
113 if (simulated_time >= 0.0) {
114 simulation_time.stop();
115 INFO0(",----[ Results ]");
116 INFO1("| Total simulated time...: %g", simulated_time);
117 INFO1("| Total simulation time..: %g", simulation_time.duration());
121 ERROR1("Simulation failed (%#x).", exit_status);
123 INFO0("Simulation succeeded.");