4 #include <tr1/functional>
12 // Creates log categories
13 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
14 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
15 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
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 #include "deployment.h"
31 // Failure exit status
33 EXIT_NO_FAILURE = 0x00, // no error
34 EXIT_FAILURE_ARGS = 0x01, // bad arguments
35 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
36 EXIT_FAILURE_SIMU = 0x04, // simulation failed
37 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
40 std::vector<double> loads;
45 static int simulation_main(int argc, char* argv[])
50 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
52 loads.push_back(proc->get_load());
55 catch (std::invalid_argument& e) {
56 THROW1(arg_error, 0, "%s", e.what());
61 static void check_for_lost_load()
63 double total_init = process::get_total_load_init();
65 double total_exit = process::get_total_load_exit();
66 double lost = total_init - total_exit;
67 double lost_ratio = 100.0 * lost / total_init;
68 if (lost_ratio < -opt::load_ratio_threshold)
69 CRITICAL2("Gained load at exit! %g (%g%%) <============",
71 else if (lost_ratio > opt::load_ratio_threshold)
72 CRITICAL2("Lost load at exit! %g (%g%%) <============",
75 DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
77 double total_running = process::get_total_load_running();
78 double running_ratio = 100.0 * total_running / total_init;
79 if (running_ratio < -opt::load_ratio_threshold)
80 CRITICAL2("Negative running load at exit! %g (%g%%) <============",
81 total_running, running_ratio);
82 else if (running_ratio > opt::load_ratio_threshold)
83 CRITICAL2("Remaining running load at exit! %g (%g%%) <============",
84 total_running, running_ratio);
86 DEBUG2("Running load at exit looks good: %g (%g%%)",
87 total_running, running_ratio);
90 static void compute_load_imbalance()
93 using std::tr1::placeholders::_1;
95 unsigned n = loads.size();
96 load_avg = std::accumulate(loads.begin(), loads.end(), 0.0) / n;
98 std::vector<double> diff(loads);
99 std::transform(diff.begin(), diff.end(), diff.begin(),
100 bind(std::minus<double>(), _1, load_avg));
101 double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0);
102 double square_sum = std::inner_product(diff.begin(), diff.end(),
104 double variance = (square_sum - (epsilon * epsilon) / n) / n;
105 load_stddev = sqrt(variance);
108 int main(int argc, char* argv[])
110 // Note: variables used after THROW must be declared as volatile.
111 volatile int exit_status = 0; // global exit status
112 volatile double simulated_time = -1.0;
113 timestamp simulation_time;
117 simulation_time.start();
119 // Set default logging parameters
120 bool do_log_control_set = true;
121 for (int i = 1 ; do_log_control_set && i < argc ; i++)
122 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
123 strchr(argv[i] + 1, 'v'));
124 if (do_log_control_set) {
125 // xbt_log_control_set("simu.thres:verbose");
126 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
127 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
130 // Initialize some MSG internal data.
131 // Note: MSG_global_init() may throw an exception, but it seems
132 // impossible to catch it correctly :-(
133 MSG_global_init(&argc, argv);
135 // Parse global parameters
136 bool parse_res = opt::parse_args(&argc, argv);
138 || opt::version_requested || opt::help_requested) {
139 if (opt::version_requested)
140 std::clog << version::name << " (" << opt::program_name << ")"
141 << " version " << version::num << "\n"
142 << version::copyright << "\n"
143 "Compiled on " << version::date << "\n\n";
144 if (!parse_res || opt::help_requested)
147 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
149 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
150 version::date.c_str());
154 exit_status = EXIT_FAILURE_INIT; // =====
156 // Register the default function of an agent
157 // MSG_function_register("simulation_main", simulation_main);
158 MSG_function_register_default(simulation_main);
160 // Create the platform and the application.
161 MSG_create_environment(opt::platform_file.c_str());
163 if (opt::auto_depl::enabled) {
164 if (!opt::auto_depl::nhosts)
165 opt::auto_depl::nhosts = hostdata::size();
166 if (opt::auto_depl::nhosts > hostdata::size()) {
167 WARN2("%u hosts is too much: limiting to %u",
168 opt::auto_depl::nhosts, (unsigned )hostdata::size());
169 opt::auto_depl::nhosts = hostdata::size();
171 if (!opt::auto_depl::load)
172 opt::auto_depl::load = opt::auto_depl::nhosts;
173 MY_launch_application(); // it is already opt::* aware...
175 MSG_launch_application(opt::deployment_file.c_str());
178 exit_status = EXIT_FAILURE_SIMU; // =====
180 // Launch the MSG simulation.
181 INFO1("Starting simulation at %f...", MSG_get_clock());
183 simulated_time = MSG_get_clock();
184 INFO1("Simulation ended at %f.", simulated_time);
185 check_for_lost_load();
186 compute_load_imbalance();
188 THROW1(0, 0, "MSG_main() failed with status %#x", res);
190 exit_status = EXIT_NO_FAILURE; // =====
193 int len = strlen(ex.msg);
194 if (len > 0 && ex.msg[len - 1] == '\n')
195 ex.msg[len - 1] = '\0'; // strip the ending '\n'
196 ERROR1("%s", ex.msg);
197 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
201 // Clean the MSG simulation.
205 ERROR1("MSG_clean() failed with status %#x", res);
206 exit_status |= EXIT_FAILURE_CLEAN;
209 // Report final simulation status.
210 if (simulated_time >= 0.0) {
211 simulation_time.stop();
212 INFO0(",----[ Results ]");
213 INFO2("| Load avg./stddev. at exit.: %g / %g", load_avg, load_stddev);
214 INFO1("| Total simulated time......: %g", simulated_time);
215 INFO1("| Total simulation time.....: %g", simulation_time.duration());
219 ERROR1("Simulation failed (%#x).", exit_status);
221 INFO0("Simulation succeeded.");