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> comps;
45 std::vector<double> loads;
50 static int simulation_main(int argc, char* argv[])
55 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
57 comps.push_back(proc->get_comp());
58 loads.push_back(proc->get_load());
61 catch (std::invalid_argument& e) {
62 THROW1(arg_error, 0, "%s", e.what());
67 static void check_for_lost_load()
69 double total_init = process::get_total_load_init();
71 double total_exit = process::get_total_load_exit();
72 double lost = total_init - total_exit;
73 double lost_ratio = 100.0 * lost / total_init;
74 if (lost_ratio < -opt::load_ratio_threshold)
75 CRITICAL2("Gained load at exit! %g (%g%%) <============",
77 else if (lost_ratio > opt::load_ratio_threshold)
78 CRITICAL2("Lost load at exit! %g (%g%%) <============",
81 DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
83 double total_running = process::get_total_load_running();
84 double running_ratio = 100.0 * total_running / total_init;
85 if (running_ratio < -opt::load_ratio_threshold)
86 CRITICAL2("Negative running load at exit! %g (%g%%) <============",
87 total_running, running_ratio);
88 else if (running_ratio > opt::load_ratio_threshold)
89 CRITICAL2("Remaining running load at exit! %g (%g%%) <============",
90 total_running, running_ratio);
92 DEBUG2("Running load at exit looks good: %g (%g%%)",
93 total_running, running_ratio);
96 static void statistics(const std::vector<double>& vec,
97 double* sum, double* avg, double* stddev)
100 using std::tr1::placeholders::_1;
102 unsigned n = vec.size();
103 double vec_sum = std::accumulate(vec.begin(), vec.end(), 0.0);
104 double vec_avg = vec_sum / n;
112 std::vector<double> diff(vec);
113 std::transform(diff.begin(), diff.end(), diff.begin(),
114 bind(std::minus<double>(), _1, vec_avg));
115 double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0);
116 double square_sum = std::inner_product(diff.begin(), diff.end(),
118 double variance = (square_sum - (epsilon * epsilon) / n) / n;
119 *stddev = sqrt(variance);
123 static void compute_metrics()
125 statistics(comps, &comp_total, &comp_avg, &comp_stddev);
126 statistics(loads, NULL, &load_avg, &load_stddev);
129 int main(int argc, char* argv[])
131 // Note: variables used after THROW must be declared as volatile.
132 volatile int exit_status = 0; // global exit status
133 volatile double simulated_time = -1.0;
134 timestamp simulation_time;
138 simulation_time.start();
140 // Set default logging parameters
141 bool do_log_control_set = true;
142 for (int i = 1 ; do_log_control_set && i < argc ; i++)
143 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
144 strchr(argv[i] + 1, 'v'));
145 if (do_log_control_set) {
146 // xbt_log_control_set("simu.thres:verbose");
147 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
148 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
151 // Initialize some MSG internal data.
152 // Note: MSG_global_init() may throw an exception, but it seems
153 // impossible to catch it correctly :-(
154 MSG_global_init(&argc, argv);
156 // Parse global parameters
157 bool parse_res = opt::parse_args(&argc, argv);
159 || opt::version_requested || opt::help_requested) {
160 if (opt::version_requested)
161 std::clog << version::name << " (" << opt::program_name << ")"
162 << " version " << version::num << "\n"
163 << version::copyright << "\n"
164 "Compiled on " << version::date << "\n\n";
165 if (!parse_res || opt::help_requested)
168 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
170 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
171 version::date.c_str());
175 exit_status = EXIT_FAILURE_INIT; // =====
177 // Register the default function of an agent
178 // MSG_function_register("simulation_main", simulation_main);
179 MSG_function_register_default(simulation_main);
181 // Create the platform and the application.
182 MSG_create_environment(opt::platform_file.c_str());
184 if (opt::auto_depl::enabled) {
185 if (!opt::auto_depl::nhosts)
186 opt::auto_depl::nhosts = hostdata::size();
187 if (opt::auto_depl::nhosts > hostdata::size()) {
188 WARN2("%u hosts is too much: limiting to %u",
189 opt::auto_depl::nhosts, (unsigned )hostdata::size());
190 opt::auto_depl::nhosts = hostdata::size();
192 if (!opt::auto_depl::load)
193 opt::auto_depl::load = opt::auto_depl::nhosts;
194 MY_launch_application(); // it is already opt::* aware...
196 MSG_launch_application(opt::deployment_file.c_str());
199 exit_status = EXIT_FAILURE_SIMU; // =====
201 // Launch the MSG simulation.
202 INFO1("Starting simulation at %f...", MSG_get_clock());
204 simulated_time = MSG_get_clock();
205 INFO1("Simulation ended at %f.", simulated_time);
207 THROW1(0, 0, "MSG_main() failed with status %#x", res);
209 exit_status = EXIT_NO_FAILURE; // =====
212 int len = strlen(ex.msg);
213 if (len > 0 && ex.msg[len - 1] == '\n')
214 ex.msg[len - 1] = '\0'; // strip the ending '\n'
215 ERROR1("%s", ex.msg);
216 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
220 // Clean the MSG simulation.
224 ERROR1("MSG_clean() failed with status %#x", res);
225 exit_status |= EXIT_FAILURE_CLEAN;
228 // Report final simulation status.
229 if (simulated_time >= 0.0) {
230 simulation_time.stop();
231 check_for_lost_load();
233 INFO0(",----[ Results ]");
234 INFO2("| Load avg./stddev. at exit..............: %g / %g",
235 load_avg, load_stddev);
236 INFO3("| Computation total/avg./stddev. at exit.: %g / %g / %g",
237 comp_total, comp_avg, comp_stddev);
238 INFO1("| Total simulated time...................: %g", simulated_time);
239 INFO1("| Total simulation time..................: %g",
240 simulation_time.duration());
244 ERROR1("Simulation failed (%#x).", exit_status);
246 INFO0("Simulation succeeded.");