10 // Creates log categories
11 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
12 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
13 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
14 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
15 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
16 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
20 #include "deployment.h"
29 // Failure exit status
31 EXIT_NO_FAILURE = 0x00, // no error
32 EXIT_FAILURE_ARGS = 0x01, // bad arguments
33 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
34 EXIT_FAILURE_SIMU = 0x04, // simulation failed
35 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
38 std::vector<double> loads;
43 static int simulation_main(int argc, char* argv[])
48 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
50 loads.push_back(proc->get_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 DEBUG2("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 DEBUG2("Running load at exit looks good: %g (%g%%)",
85 total_running, running_ratio);
88 static void compute_load_imbalance()
90 unsigned n = loads.size();
91 load_avg = std::accumulate(loads.begin(), loads.end(), 0.0) / n;
92 double variance = 0.0;
94 for (unsigned i = 0 ; i < n ; ++i) {
95 double diff = loads[i] - load_avg;
96 variance += diff * diff;
99 variance = (variance - (epsilon * epsilon) / n) / n;
100 load_stddev = sqrt(variance);
103 int main(int argc, char* argv[])
105 // Note: variables used after THROW must be declared as volatile.
106 volatile int exit_status = 0; // global exit status
107 volatile double simulated_time = -1.0;
108 timestamp simulation_time;
112 simulation_time.start();
114 // Set default logging parameters
115 bool do_log_control_set = true;
116 for (int i = 1 ; do_log_control_set && i < argc ; i++)
117 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
118 strchr(argv[i] + 1, 'v'));
119 if (do_log_control_set) {
120 // xbt_log_control_set("simu.thres:verbose");
121 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
122 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
125 // Initialize some MSG internal data.
126 // Note: MSG_global_init() may throw an exception, but it seems
127 // impossible to catch it correctly :-(
128 MSG_global_init(&argc, argv);
130 // Parse global parameters
131 bool parse_res = opt::parse_args(&argc, argv);
133 || opt::version_requested || opt::help_requested) {
134 if (opt::version_requested)
135 std::clog << version::name << " (" << opt::program_name << ")"
136 << " version " << version::num << "\n"
137 << version::copyright << "\n"
138 "Compiled on " << version::date << "\n\n";
139 if (!parse_res || opt::help_requested)
142 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
144 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
145 version::date.c_str());
149 exit_status = EXIT_FAILURE_INIT; // =====
151 // Register the default function of an agent
152 // MSG_function_register("simulation_main", simulation_main);
153 MSG_function_register_default(simulation_main);
155 // Create the platform and the application.
156 MSG_create_environment(opt::platform_file.c_str());
158 if (opt::auto_depl::enabled) {
159 if (!opt::auto_depl::nhosts)
160 opt::auto_depl::nhosts = hostdata::size();
161 if (opt::auto_depl::nhosts > hostdata::size()) {
162 WARN2("%u hosts is too much: limiting to %u",
163 opt::auto_depl::nhosts, (unsigned )hostdata::size());
164 opt::auto_depl::nhosts = hostdata::size();
166 if (!opt::auto_depl::load)
167 opt::auto_depl::load = opt::auto_depl::nhosts;
168 MY_launch_application(); // it is already opt::* aware...
170 MSG_launch_application(opt::deployment_file.c_str());
173 exit_status = EXIT_FAILURE_SIMU; // =====
175 // Launch the MSG simulation.
176 INFO1("Starting simulation at %f...", MSG_get_clock());
178 simulated_time = MSG_get_clock();
179 INFO1("Simulation ended at %f.", simulated_time);
180 check_for_lost_load();
181 compute_load_imbalance();
183 THROW1(0, 0, "MSG_main() failed with status %#x", res);
185 exit_status = EXIT_NO_FAILURE; // =====
188 int len = strlen(ex.msg);
189 if (len > 0 && ex.msg[len - 1] == '\n')
190 ex.msg[len - 1] = '\0'; // strip the ending '\n'
191 ERROR1("%s", ex.msg);
192 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
196 // Clean the MSG simulation.
200 ERROR1("MSG_clean() failed with status %#x", res);
201 exit_status |= EXIT_FAILURE_CLEAN;
204 // Report final simulation status.
205 if (simulated_time >= 0.0) {
206 simulation_time.stop();
207 INFO0(",----[ Results ]");
208 INFO2("| Load avg./stddev. at exit.: %g / %g", load_avg, load_stddev);
209 INFO1("| Total simulated time......: %g", simulated_time);
210 INFO1("| Total simulation time.....: %g", simulation_time.duration());
214 ERROR1("Simulation failed (%#x).", exit_status);
216 INFO0("Simulation succeeded.");