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 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.0 * lost / total_init;
56 if (lost_ratio < -opt::load_ratio_threshold)
57 CRITICAL2("Gained load at exit! %g (%g%%) <============",
59 else if (lost_ratio > opt::load_ratio_threshold)
60 CRITICAL2("Lost load at exit! %g (%g%%) <============",
63 DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
65 double total_running = process::get_total_load_running();
66 double running_ratio = 100.0 * total_running / total_init;
67 if (running_ratio < -opt::load_ratio_threshold)
68 CRITICAL2("Negative running load at exit! %g (%g%%) <============",
69 total_running, running_ratio);
70 else if (running_ratio > opt::load_ratio_threshold)
71 CRITICAL2("Remaining running load at exit! %g (%g%%) <============",
72 total_running, running_ratio);
74 DEBUG2("Running load at exit looks good: %g (%g%%)",
75 total_running, running_ratio);
78 int main(int argc, char* argv[])
80 // Note: variables used after THROW must be declared as volatile.
81 volatile int exit_status = 0; // global exit status
82 volatile double simulated_time = -1.0;
83 timestamp simulation_time;
87 simulation_time.start();
89 // Set default logging parameters
90 bool do_log_control_set = true;
91 for (int i = 1 ; do_log_control_set && i < argc ; i++)
92 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
93 strchr(argv[i] + 1, 'v'));
94 if (do_log_control_set) {
95 // xbt_log_control_set("simu.thres:verbose");
96 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
97 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
100 // Initialize some MSG internal data.
101 // Note: MSG_global_init() may throw an exception, but it seems
102 // impossible to catch it correctly :-(
103 MSG_global_init(&argc, argv);
105 // Parse global parameters
106 int parse_res = opt::parse_args(&argc, argv);
108 || opt::version_requested || opt::help_requested) {
109 if (opt::version_requested)
110 std::clog << version::name << " (" << opt::program_name << ")"
111 << " version " << version::num << "\n"
112 << version::copyright << "\n"
113 "Compiled on " << version::date << "\n\n";
114 if (!parse_res || opt::help_requested)
117 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
119 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
120 version::date.c_str());
124 exit_status = EXIT_FAILURE_INIT; // =====
126 // Register the default function of an agent
127 // MSG_function_register("simulation_main", simulation_main);
128 MSG_function_register_default(simulation_main);
130 // Create the platform and the application.
131 MSG_create_environment(opt::platform_file.c_str());
133 if (opt::auto_depl::enabled) {
134 if (!opt::auto_depl::nhosts)
135 opt::auto_depl::nhosts = hostdata::size();
136 if (opt::auto_depl::nhosts > hostdata::size()) {
137 WARN2("%u hosts is too much: limiting to %u",
138 opt::auto_depl::nhosts, (unsigned )hostdata::size());
139 opt::auto_depl::nhosts = hostdata::size();
141 if (!opt::auto_depl::load)
142 opt::auto_depl::load = opt::auto_depl::nhosts;
143 MY_launch_application(); // it is already opt::* aware...
145 MSG_launch_application(opt::deployment_file.c_str());
148 exit_status = EXIT_FAILURE_SIMU; // =====
150 // Launch the MSG simulation.
151 INFO1("Starting simulation at %f...", MSG_get_clock());
153 simulated_time = MSG_get_clock();
154 INFO1("Simulation ended at %f.", simulated_time);
155 check_for_lost_load();
157 THROW1(0, 0, "MSG_main() failed with status %#x", res);
159 exit_status = EXIT_NO_FAILURE; // =====
162 int len = strlen(ex.msg);
163 if (len > 0 && ex.msg[len - 1] == '\n')
164 ex.msg[len - 1] = '\0'; // strip the ending '\n'
165 ERROR1("%s", ex.msg);
166 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
170 // Clean the MSG simulation.
174 ERROR1("MSG_clean() failed with status %#x", res);
175 exit_status |= EXIT_FAILURE_CLEAN;
178 // Report final simulation status.
179 if (simulated_time >= 0.0) {
180 simulation_time.stop();
181 INFO0(",----[ Results ]");
182 INFO1("| Total simulated time...: %g", simulated_time);
183 INFO1("| Total simulation time..: %g", simulation_time.duration());
187 ERROR1("Simulation failed (%#x).", exit_status);
189 INFO0("Simulation succeeded.");