13 // Creates log categories
14 XBT_LOG_NEW_CATEGORY(simu, "Simulation messages");
15 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
16 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
17 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
18 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
22 // Failure exit status
24 EXIT_NO_FAILURE = 0x00, // no error
25 EXIT_FAILURE_ARGS = 0x01, // bad arguments
26 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
27 EXIT_FAILURE_SIMU = 0x04, // simulation failed
28 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
31 #include "loba_least_loaded.h"
32 int simulation_main(int argc, char* argv[])
37 proc = new loba_least_loaded(argc, argv);
41 catch (std::invalid_argument& e) {
42 THROW1(arg_error, 0, "%s", e.what());
47 int main(int argc, char* argv[])
49 // Note: variables used after THROW must be declared as volatile.
50 volatile int exit_status = 0; // global exit status
51 volatile double simulated_time = -1.0;
52 timestamp simulation_time;
56 simulation_time.start();
58 // Set default logging parameters
59 // xbt_log_control_set("simu.thres:verbose");
60 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
62 // Initialize some MSG internal data.
63 // Note: MSG_global_init() may throw an exception, but it seems
64 // impossible to catch it correctly :-(
65 MSG_global_init(&argc, argv);
67 // Parse global parameters
68 int parse_res = opt::parse_args(&argc, argv);
70 || opt::version_requested || opt::help_requested) {
71 if (opt::version_requested)
72 std::clog << version::name << " (" << opt::program_name << ")"
73 << " version " << version::num << "\n"
74 << version::copyright << "\n"
75 "Compiled on " << version::date << "\n\n";
76 if (!parse_res || opt::help_requested)
79 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
84 exit_status = EXIT_FAILURE_INIT; // =====
86 // Register the main function of an agent in a global table.
87 MSG_function_register("simulation_main", simulation_main);
89 // Create the platform and the application.
90 MSG_create_environment(opt::platform_file.c_str());
92 if (opt::auto_depl::enabled) {
93 opt::auto_depl::nhosts = hostdata::size();
94 opt::auto_depl::load = hostdata::size();
96 // fixme: do auto deployment
98 catch (std::exception& e) {
99 THROW1(0, 0, "%s", e.what());
102 MSG_launch_application(opt::deployment_file.c_str());
105 exit_status = EXIT_FAILURE_SIMU; // =====
107 // Launch the MSG simulation.
108 INFO0("Starting simulation...");
110 INFO0("Simulation ended.");
111 simulated_time = MSG_get_clock();
113 THROW1(0, 0, "MSG_main() failed with status %#x", res);
115 exit_status = EXIT_NO_FAILURE; // =====
118 int len = strlen(ex.msg);
119 if (len > 0 && ex.msg[len - 1] == '\n')
120 ex.msg[len - 1] = '\0'; // strip the ending '\n'
121 ERROR1("%s", ex.msg);
122 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
126 // Clean the MSG simulation.
130 ERROR1("MSG_clean() failed with status %#x", res);
131 exit_status |= EXIT_FAILURE_CLEAN;
134 // Report final simulation status.
135 if (simulated_time >= 0.0) {
136 simulation_time.stop();
137 INFO0(",----[ Results ]");
138 INFO1("| Total simulated time...: %g", simulated_time);
139 INFO1("| Total simulation time..: %g", simulation_time.duration());
143 ERROR1("Simulation failed (%#x).", exit_status);
145 INFO0("Simulation succeeded.");