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 threshold.
41 // xbt_log_control_set("simu.thres:verbose");
43 // Initialize some MSG internal data.
44 // Note: MSG_global_init() may throw an exception, but it seems
45 // impossible to catch it correctly :-(
46 MSG_global_init(&argc, argv);
48 // Parse global parameters
49 int parse_res = opt::parse_args(&argc, argv);
51 || opt::version_requested || opt::help_requested) {
52 if (opt::version_requested)
53 std::clog << version::name << " version " << version::num << "\n"
54 << version::copyright << "\n"
55 "Compiled on " << version::date << "\n\n";
56 if (!parse_res || opt::help_requested)
59 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
64 exit_status = EXIT_FAILURE_INIT; // =====
66 // Register the main function of an agent in a global table.
67 MSG_function_register("simulation_main", simulation_main);
68 // Preserve some compatibility with old code...
69 MSG_function_register("Calculs", simulation_main);
71 // Create the platform and the application.
72 MSG_create_environment(opt::platform_file);
73 if (LOG_ISENABLED(xbt_log_priority_verbose)) {
74 int n = MSG_get_host_number();
75 m_host_t *h = MSG_get_host_table();
76 VERB1("Got %d hosts.", n);
77 for (int i = 0; i < n; i++)
78 VERB2("Host #%d named \"%s\".", i, MSG_host_get_name(h[i]));
81 MSG_launch_application(opt::application_file);
83 exit_status = EXIT_FAILURE_SIMU; // =====
85 // Launch the MSG simulation.
86 INFO0("Starting simulation...");
88 INFO0("Simulation ended.");
89 simulated_time = MSG_get_clock();
91 THROW1(0, 0, "MSG_main() failed with status %#x", res);
93 exit_status = EXIT_NO_FAILURE; // =====
96 int len = strlen(ex.msg);
97 if (len > 0 && ex.msg[len - 1] == '\n')
98 ex.msg[len - 1] = '\0'; // strip the ending '\n'
100 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
104 // Clean the MSG simulation.
107 ERROR1("MSG_clean() failed with status %#x", res);
108 exit_status |= EXIT_FAILURE_CLEAN;
111 // Report final simulation status.
112 if (simulated_time >= 0.0) {
113 simulation_time.stop();
114 INFO0(",----[ Results ]");
115 INFO1("| Total simulated time...: %g", simulated_time);
116 INFO1("| Total simulation time..: %g", simulation_time.duration());
120 ERROR1("Simulation failed (%#x).", exit_status);
122 INFO0("Simulation succeeded.");