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"
32 // Failure exit status
34 EXIT_NO_FAILURE = 0x00, // no error
35 EXIT_FAILURE_ARGS = 0x01, // bad arguments
36 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
37 EXIT_FAILURE_SIMU = 0x04, // simulation failed
38 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
41 std::vector<double> comps;
46 std::vector<double> loads;
51 static int simulation_main(int argc, char* argv[])
56 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
58 comps.push_back(proc->get_comp());
59 loads.push_back(proc->get_load());
62 catch (std::invalid_argument& e) {
63 THROW1(arg_error, 0, "%s", e.what());
68 static void check_for_lost_load()
70 double total_init = process::get_total_load_init();
72 double total_exit = process::get_total_load_exit();
73 double lost = total_init - total_exit;
74 double lost_ratio = 100.0 * lost / total_init;
75 if (lost_ratio < -opt::load_ratio_threshold)
76 CRITICAL2("Gained load at exit! %g (%g%%) <============",
78 else if (lost_ratio > opt::load_ratio_threshold)
79 CRITICAL2("Lost load at exit! %g (%g%%) <============",
82 DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
84 double total_running = process::get_total_load_running();
85 double running_ratio = 100.0 * total_running / total_init;
86 if (running_ratio < -opt::load_ratio_threshold)
87 CRITICAL2("Negative running load at exit! %g (%g%%) <============",
88 total_running, running_ratio);
89 else if (running_ratio > opt::load_ratio_threshold)
90 CRITICAL2("Remaining running load at exit! %g (%g%%) <============",
91 total_running, running_ratio);
93 DEBUG2("Running load at exit looks good: %g (%g%%)",
94 total_running, running_ratio);
97 static void statistics(const std::vector<double>& vec,
98 double* sum, double* avg, double* stddev)
100 using std::tr1::bind;
101 using std::tr1::placeholders::_1;
103 unsigned n = vec.size();
104 double vec_sum = std::accumulate(vec.begin(), vec.end(), 0.0);
105 double vec_avg = vec_sum / n;
113 std::vector<double> diff(vec);
114 std::transform(diff.begin(), diff.end(), diff.begin(),
115 bind(std::minus<double>(), _1, vec_avg));
116 double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0);
117 double square_sum = std::inner_product(diff.begin(), diff.end(),
119 double variance = (square_sum - (epsilon * epsilon) / n) / n;
120 *stddev = sqrt(variance);
124 static void compute_metrics()
126 statistics(comps, &comp_total, &comp_avg, &comp_stddev);
127 statistics(loads, NULL, &load_avg, &load_stddev);
130 int main(int argc, char* argv[])
132 // Note: variables used after THROW must be declared as volatile.
133 volatile int exit_status = 0; // global exit status
134 volatile double simulated_time = -1.0;
135 timestamp simulation_time;
139 simulation_time.start();
141 // Set default logging parameters
142 bool do_log_control_set = true;
143 for (int i = 1 ; do_log_control_set && i < argc ; i++)
144 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
145 strchr(argv[i] + 1, 'v'));
146 if (do_log_control_set) {
147 // xbt_log_control_set("simu.thres:verbose");
148 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
149 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
152 // Initialize some MSG internal data.
153 // Note: MSG_global_init() may throw an exception, but it seems
154 // impossible to catch it correctly :-(
155 MSG_global_init(&argc, argv);
157 // Parse global parameters
158 bool parse_res = opt::parse_args(&argc, argv);
160 || opt::version_requested || opt::help_requested) {
161 if (opt::version_requested)
162 std::clog << version::name << " (" << opt::program_name << ")"
163 << " version " << version::num << "\n"
164 << version::copyright << "\n"
165 "Compiled on " << version::date << "\n\n";
166 if (!parse_res || opt::help_requested)
169 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
171 INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
172 version::date.c_str());
176 exit_status = EXIT_FAILURE_INIT; // =====
178 // Register the default function of an agent
179 // MSG_function_register("simulation_main", simulation_main);
180 MSG_function_register_default(simulation_main);
182 // Create the platform and the application.
183 MSG_create_environment(opt::platform_file.c_str());
185 if (opt::auto_depl::enabled) {
186 if (!opt::auto_depl::nhosts)
187 opt::auto_depl::nhosts = hostdata::size();
188 if (opt::auto_depl::nhosts > hostdata::size()) {
189 WARN2("%u hosts is too much: limiting to %u",
190 opt::auto_depl::nhosts, (unsigned )hostdata::size());
191 opt::auto_depl::nhosts = hostdata::size();
193 if (!opt::auto_depl::load)
194 opt::auto_depl::load = opt::auto_depl::nhosts;
195 MY_launch_application(); // it is already opt::* aware...
197 MSG_launch_application(opt::deployment_file.c_str());
200 // Register tracing categories
201 TRACE_category(TRACE_CAT_COMP);
202 TRACE_category(TRACE_CAT_CTRL);
203 TRACE_category(TRACE_CAT_DATA);
205 exit_status = EXIT_FAILURE_SIMU; // =====
207 // Launch the MSG simulation.
208 INFO1("Starting simulation at %f...", MSG_get_clock());
210 simulated_time = MSG_get_clock();
211 INFO1("Simulation ended at %f.", simulated_time);
213 THROW1(0, 0, "MSG_main() failed with status %#x", res);
215 exit_status = EXIT_NO_FAILURE; // =====
218 int len = strlen(ex.msg);
219 if (len > 0 && ex.msg[len - 1] == '\n')
220 ex.msg[len - 1] = '\0'; // strip the ending '\n'
221 ERROR1("%s", ex.msg);
222 DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
226 // Clean the MSG simulation.
230 ERROR1("MSG_clean() failed with status %#x", res);
231 exit_status |= EXIT_FAILURE_CLEAN;
234 // Report final simulation status.
235 if (simulated_time >= 0.0) {
236 simulation_time.stop();
237 check_for_lost_load();
239 INFO0(",----[ Results ]");
240 INFO2("| Load avg./stddev. at exit..............: %g / %g",
241 load_avg, load_stddev);
242 INFO3("| Computation total/avg./stddev. at exit.: %g / %g / %g",
243 comp_total, comp_avg, comp_stddev);
244 INFO1("| Total simulated time...................: %g", simulated_time);
245 INFO1("| Total simulation time..................: %g",
246 simulation_time.duration());
250 ERROR1("Simulation failed (%#x).", exit_status);
252 INFO0("Simulation succeeded.");