12 // Creates a new log category and makes it the default
13 XBT_LOG_NEW_DEFAULT_CATEGORY(simu, "Simulation messages");
15 // Failure exit status
17 EXIT_NO_FAILURE = 0x00, // no error
18 EXIT_FAILURE_ARGS = 0x01, // bad arguments
19 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
20 EXIT_FAILURE_SIMU = 0x04, // simulation failed
21 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
24 int simulation_main(int argc, char* argv[])
26 process proc(argc, argv);
30 int main(int argc, char* argv[])
32 // Note: variables used after THROW must be declared as volatile.
33 volatile int exit_status = 0; // global exit status
34 volatile double simulated_time = -1.0;
35 timestamp simulation_time;
39 simulation_time.start();
41 // Set default logging parameters
42 // xbt_log_control_set("simu.thres:verbose");
43 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
45 // Initialize some MSG internal data.
46 // Note: MSG_global_init() may throw an exception, but it seems
47 // impossible to catch it correctly :-(
48 MSG_global_init(&argc, argv);
50 // Parse global parameters
51 int parse_res = opt::parse_args(&argc, argv);
53 || opt::version_requested || opt::help_requested) {
54 if (opt::version_requested)
55 std::clog << version::name << " version " << version::num << "\n"
56 << version::copyright << "\n"
57 "Compiled on " << version::date << "\n\n";
58 if (!parse_res || opt::help_requested)
61 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
66 exit_status = EXIT_FAILURE_INIT; // =====
68 // Register the main function of an agent in a global table.
69 MSG_function_register("simulation_main", simulation_main);
70 // Preserve some compatibility with old code...
71 MSG_function_register("Calculs", simulation_main);
73 // Create the platform and the application.
74 MSG_create_environment(opt::platform_file);
76 MSG_launch_application(opt::application_file);
78 exit_status = EXIT_FAILURE_SIMU; // =====
80 // Launch the MSG simulation.
81 INFO0("Starting simulation...");
83 INFO0("Simulation ended.");
84 simulated_time = MSG_get_clock();
86 THROW1(0, 0, "MSG_main() failed with status %#x", res);
88 exit_status = EXIT_NO_FAILURE; // =====
91 int len = strlen(ex.msg);
92 if (len > 0 && ex.msg[len - 1] == '\n')
93 ex.msg[len - 1] = '\0'; // strip the ending '\n'
95 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
99 // Clean the MSG simulation.
103 ERROR1("MSG_clean() failed with status %#x", res);
104 exit_status |= EXIT_FAILURE_CLEAN;
107 // Report final simulation status.
108 if (simulated_time >= 0.0) {
109 simulation_time.stop();
110 INFO0(",----[ Results ]");
111 INFO1("| Total simulated time...: %g", simulated_time);
112 INFO1("| Total simulation time..: %g", simulation_time.duration());
116 ERROR1("Simulation failed (%#x).", exit_status);
118 INFO0("Simulation succeeded.");