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"
22 #include "statistics.h"
28 // Failure exit status
30 EXIT_NO_FAILURE = 0x00, // no error
31 EXIT_FAILURE_ARGS = 0x01, // bad arguments
32 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
33 EXIT_FAILURE_SIMU = 0x04, // simulation failed
34 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
37 struct statistics comps;
38 struct statistics loads;
42 static int simulation_main(int argc, char* argv[])
47 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
49 comps.push(proc->get_comp());
50 loads.push(proc->get_real_load());
53 catch (std::invalid_argument& e) {
54 THROW1(arg_error, 0, "%s", e.what());
59 static void check_for_lost_load()
61 double total_init = process::get_total_load_init();
63 double total_exit = process::get_total_load_exit();
64 double lost = total_init - total_exit;
65 double lost_ratio = 100.0 * lost / total_init;
66 if (lost_ratio < -opt::load_ratio_threshold)
67 CRITICAL2("Gained load at exit! %g (%g%%) <============",
69 else if (lost_ratio > opt::load_ratio_threshold)
70 CRITICAL2("Lost load at exit! %g (%g%%) <============",
73 VERB2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
75 double total_running = process::get_total_load_running();
76 double running_ratio = 100.0 * total_running / total_init;
77 if (running_ratio < -opt::load_ratio_threshold)
78 CRITICAL2("Negative running load at exit! %g (%g%%) <============",
79 total_running, running_ratio);
80 else if (running_ratio > opt::load_ratio_threshold)
81 CRITICAL2("Remaining running load at exit! %g (%g%%) <============",
82 total_running, running_ratio);
84 VERB2("Running load at exit looks good: %g (%g%%)",
85 total_running, running_ratio);
88 #define PR_STATS(descr, st) \
89 INFO5("| %.*s: %g / %g / %g", 39, \
90 descr " total/avg./stddev. at exit.........................", \
91 st.get_sum(), st.get_mean(), st.get_stddev())
93 int main(int argc, char* argv[])
95 // Note: variables used after THROW must be declared as volatile.
96 volatile int exit_status = 0; // global exit status
97 volatile double simulated_time = -1.0;
98 timestamp simulation_time;
102 simulation_time.start();
104 // Set default logging parameters
105 bool do_log_control_set = true;
106 for (int i = 1 ; do_log_control_set && i < argc ; i++)
107 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
108 strchr(argv[i] + 1, 'v'));
109 if (do_log_control_set) {
110 // xbt_log_control_set("simu.thres:verbose");
111 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
112 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
115 // Initialize some MSG internal data.
116 // Note: MSG_global_init() may throw an exception, but it seems
117 // impossible to catch it correctly :-(
118 MSG_global_init(&argc, argv);
120 // Parse global parameters
121 bool parse_res = opt::parse_args(&argc, argv);
123 || opt::version_requested || opt::help_requested) {
124 if (opt::version_requested)
125 std::clog << version::name << " (" << opt::program_name << ")"
126 << " version " << version::num << "\n"
127 << version::copyright << "\n"
128 "Compiled on " << version::date << "\n\n";
129 if (!parse_res || opt::help_requested)
132 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
134 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
135 version::date.c_str());
139 exit_status = EXIT_FAILURE_INIT; // =====
141 // Register the default function of an agent
142 // MSG_function_register("simulation_main", simulation_main);
143 MSG_function_register_default(simulation_main);
145 // Create the platform and the application.
146 MSG_create_environment(opt::platform_file.c_str());
148 if (opt::auto_depl::enabled) {
149 if (!opt::auto_depl::nhosts)
150 opt::auto_depl::nhosts = hostdata::size();
151 if (opt::auto_depl::nhosts > hostdata::size()) {
152 WARN2("%u hosts is too much: limiting to %u",
153 opt::auto_depl::nhosts, (unsigned )hostdata::size());
154 opt::auto_depl::nhosts = hostdata::size();
156 if (!opt::auto_depl::load)
157 opt::auto_depl::load = opt::auto_depl::nhosts;
158 MY_launch_application(); // it is already opt::* aware...
160 MSG_launch_application(opt::deployment_file.c_str());
163 // Register tracing categories
164 TRACE_category(TRACE_CAT_COMP);
165 TRACE_category(TRACE_CAT_CTRL);
166 TRACE_category(TRACE_CAT_DATA);
168 exit_status = EXIT_FAILURE_SIMU; // =====
170 // Launch the MSG simulation.
171 INFO1("Starting simulation at %f...", MSG_get_clock());
173 simulated_time = MSG_get_clock();
174 INFO1("Simulation ended at %f.", simulated_time);
176 THROW1(0, 0, "MSG_main() failed with status %#x", res);
178 exit_status = EXIT_NO_FAILURE; // =====
181 int len = strlen(ex.msg);
182 if (len > 0 && ex.msg[len - 1] == '\n')
183 ex.msg[len - 1] = '\0'; // strip the ending '\n'
184 ERROR1("%s", ex.msg);
185 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
189 // Clean the MSG simulation.
193 ERROR1("MSG_clean() failed with status %#x", res);
194 exit_status |= EXIT_FAILURE_CLEAN;
197 // Report final simulation status.
198 if (simulated_time >= 0.0) {
199 simulation_time.stop();
200 check_for_lost_load();
201 INFO0(",----[ Results ]");
202 PR_STATS("Load", loads);
203 PR_STATS("Computation", comps);
204 INFO1("| Total simulated time...................: %g", simulated_time);
205 INFO1("| Total simulation time..................: %g",
206 simulation_time.duration());
210 ERROR1("Simulation failed (%#x).", exit_status);
212 INFO0("Simulation succeeded.");