4 #include <cstring> // strchr
11 // Creates log categories
12 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
13 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
14 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
15 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
16 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
17 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
18 XBT_LOG_NEW_SUBCATEGORY(thrd, simu, "Messages from thread wrapper class");
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
22 #include "deployment.h"
27 #include "simgrid_features.h"
28 #include "statistics.h"
34 #define DATA_DESCR_WIDTH 39
37 // Failure exit status
39 EXIT_NO_FAILURE = 0x00, // no error
40 EXIT_FAILURE_ARGS = 0x01, // bad arguments
41 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
42 EXIT_FAILURE_SIMU = 0x04, // simulation failed
43 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
44 EXIT_FAILURE_INTR = 0x10, // interrupted by user
45 EXIT_FAILURE_LOAD = 0x20, // lost load on exit
46 EXIT_FAILURE_OTHER = 0x40, // other error
49 // Cannot be globally initialized...
51 condition_t* proc_cond;
52 unsigned proc_counter = 0;
56 statistics comp_iterations;
57 statistics all_comp_iterations;
58 statistics iter_deviation;
59 statistics data_send_amount;
60 statistics data_recv_amount;
61 statistics data_send_count;
62 statistics data_recv_count;
63 statistics ctrl_send_amount;
64 statistics ctrl_recv_amount;
65 statistics ctrl_send_count;
66 statistics ctrl_recv_count;
67 statistics idle_duration;
68 statistics convergence;
72 static int simulation_main(int argc, char* argv[])
77 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
79 proc_mutex->acquire();
81 proc_mutex->release();
85 proc_mutex->acquire();
86 loads.push(proc->get_real_load());
87 comps.push(proc->get_comp_amount());
88 comp_iterations.push(proc->get_comp_iter());
89 all_comp_iterations.push(proc->get_all_comp_iter());
90 iter_deviation.push(proc->get_iter_deviation());
91 data_send_amount.push(proc->get_data_send_amount());
92 data_recv_amount.push(proc->get_data_recv_amount());
93 data_send_count.push(proc->get_data_send_count());
94 data_recv_count.push(proc->get_data_recv_count());
95 ctrl_send_amount.push(proc->get_ctrl_send_amount());
96 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
97 ctrl_send_count.push(proc->get_ctrl_send_count());
98 ctrl_recv_count.push(proc->get_ctrl_recv_count());
99 idle_duration.push(proc->get_idle_duration());
100 double c = proc->get_convergence();
104 // Synchronization barrier...
105 // The goal is to circumvent a limitation in SimGrid (at least
106 // in version 3.5): a process must be alive when another one
107 // destroys a communication they had together.
110 proc_cond->broadcast();
111 while (proc_counter > 0)
112 proc_cond->wait(*proc_mutex);
113 proc_mutex->release();
120 static bool check_for_lost_load()
123 double total_init = process::get_total_load_init();
124 double total_exit = process::get_total_load_exit();
125 double lost = total_init - total_exit;
126 double lost_ratio = 100.0 * lost / total_init;
127 if (lost_ratio < -opt::load_ratio_threshold) {
128 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
131 } else if (lost_ratio > opt::load_ratio_threshold) {
132 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
136 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
138 double total_running = process::get_total_load_running();
139 double running_ratio = 100.0 * total_running / total_init;
140 if (running_ratio < -opt::load_ratio_threshold) {
141 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
142 total_running, running_ratio);
144 } else if (running_ratio > opt::load_ratio_threshold) {
145 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
146 total_running, running_ratio);
149 XBT_VERB("Running load at exit looks good: %g (%g%%)",
150 total_running, running_ratio);
154 static void check_file_access(const std::string& name)
156 if (access(name.c_str(), R_OK) != 0) {
157 std::cerr << "ERROR: cannot access to file \""
158 << name << "\" for reading: " << strerror(errno) << "\n";
159 exit(EXIT_FAILURE_ARGS);
163 static void signal_handler(int /*sig*/)
165 if (!opt::exit_request) {
166 XBT_CRITICAL(">>>>>>>>>>"
167 " caught CTRL-C: global exit requested "
169 opt::exit_request = 1;
171 XBT_CRITICAL(">>>>>>>>>>"
172 " caught CTRL-C for the 2nd time: exit immediately "
174 exit(EXIT_FAILURE_INTR);
178 static void install_signal_handler()
180 struct sigaction action;
181 action.sa_handler = signal_handler;
182 sigemptyset(&action.sa_mask);
183 action.sa_flags = SA_RESTART;
184 if (sigaction(SIGINT, &action, NULL) == -1) {
185 std::cerr << "ERROR: sigaction: " << strerror(errno) << "\n";
186 exit(EXIT_FAILURE_OTHER);
190 #define PR_VALUE(descr, format, ...) \
191 XBT_INFO("| %.*s: " format, DATA_DESCR_WIDTH, \
192 descr " ................................................", \
195 #define PR_STATS(descr, st) \
196 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
197 descr " (sum/avg/dev) ..................................", \
198 st.get_sum(), st.get_mean(), st.get_stddev())
200 int main(int argc, char* argv[])
202 int exit_status = 0; // global exit status
203 double simulated_time = -1.0;
204 timestamp elapsed_time(timestamp::wallclock_time);
205 timestamp simulation_time(timestamp::cpu_time);
208 elapsed_time.start();
209 simulation_time.start();
211 // Set default logging parameters
212 bool do_log_control_set = true;
213 for (int i = 1 ; do_log_control_set && i < argc ; i++)
214 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
215 strchr(argv[i] + 1, 'v'));
216 if (do_log_control_set) {
217 // xbt_log_control_set("simu.thres:verbose");
218 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
219 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
222 // Initialize some MSG internal data.
223 MSG_init(&argc, argv);
224 install_signal_handler();
226 // Parse global parameters
227 bool parse_res = opt::parse_args(&argc, argv);
229 || opt::version_requested || opt::help_requested) {
230 if (opt::version_requested)
231 std::clog << version::name << " (" << opt::program_name << ")"
232 << " version " << version::num << "\n"
233 << version::copyright << "\n"
234 "Compiled on " << version::date << "\n\n";
235 if (!parse_res || opt::help_requested)
238 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
240 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
241 version::date.c_str());
244 // Register the default function of an agent
245 // MSG_function_register("simulation_main", simulation_main);
246 MSG_function_register_default(simulation_main);
248 // Create the platform and the application.
249 XBT_DEBUG("Loading platform file...");
250 check_file_access(opt::platform_file);
251 MSG_create_environment(opt::platform_file.c_str());
252 XBT_DEBUG("Creating hostdata...");
254 XBT_INFO("Loaded description of %zd hosts.", hostdata::size());
255 XBT_DEBUG("Deploying processes...");
256 if (opt::auto_depl::enabled) {
257 if (!opt::auto_depl::nhosts)
258 opt::auto_depl::nhosts = hostdata::size();
259 if (opt::auto_depl::nhosts > hostdata::size()) {
260 XBT_WARN("%u hosts is too much: limiting to %zu",
261 opt::auto_depl::nhosts, hostdata::size());
262 opt::auto_depl::nhosts = hostdata::size();
264 if (opt::auto_depl::load == 0.0) {
265 XBT_WARN("Initial load is zero! "
266 "Falling back on old behaviour (load = nhosts).");
267 opt::auto_depl::load = opt::auto_depl::nhosts;
268 } else if (opt::auto_depl::load < 0.0)
269 opt::auto_depl::load =
270 -opt::auto_depl::load * opt::auto_depl::nhosts;
271 double iload = std::trunc(opt::auto_depl::load);
272 if (opt::integer_transfer && opt::auto_depl::load != iload) {
273 XBT_WARN("Total load %g is not an integer. Truncate it.",
274 opt::auto_depl::load);
275 opt::auto_depl::load = iload;
277 MY_launch_application(); // it is already opt::* aware...
279 check_file_access(opt::deployment_file);
280 MSG_launch_application(opt::deployment_file.c_str());
283 // Register tracing categories
284 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
285 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
286 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
288 proc_mutex = new mutex_t();
289 proc_cond = new condition_t();
290 process::set_proc_mutex(proc_mutex);
292 // Launch the MSG simulation.
293 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
295 simulated_time = MSG_get_clock();
296 XBT_INFO("Simulation ended at %f.", simulated_time);
298 process::set_proc_mutex(NULL);
303 exit_status = EXIT_NO_FAILURE;
305 XBT_ERROR("MSG_main() failed with status %#x", res);
306 exit_status = EXIT_FAILURE_SIMU;
309 // Clean the MSG simulation.
313 XBT_ERROR("MSG_clean() failed with status %#x", res);
314 exit_status |= EXIT_FAILURE_CLEAN;
317 // Report final simulation status.
318 if (simulated_time >= 0.0) {
319 simulation_time.stop();
321 if (!check_for_lost_load())
322 exit_status |= EXIT_FAILURE_LOAD;
324 XBT_INFO(",----[ Results ]");
325 PR_STATS("Load", loads);
326 PR_STATS("Computation", comps);
327 PR_STATS("Comp. iterations", comp_iterations);
328 PR_STATS("X-Comp. iterations", all_comp_iterations);
329 PR_STATS("Data send amount", data_send_amount);
330 PR_STATS("Data recv amount", data_recv_amount);
331 PR_STATS("Data send count", data_send_count);
332 PR_STATS("Data recv count", data_recv_count);
333 PR_STATS("Ctrl send amount", ctrl_send_amount);
334 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
335 PR_STATS("Ctrl send count", ctrl_send_count);
336 PR_STATS("Ctrl recv count", ctrl_recv_count);
337 PR_VALUE("Total simulated time", "%g", simulated_time);
338 PR_VALUE("Total simulation time", "%g", simulation_time.duration());
339 PR_VALUE("Elapsed (wall clock) time", "%g", elapsed_time.duration());
342 double load_imbalance = 100.0 * loads.get_stddev() / loads.get_mean();
343 double transfer_amount =
344 data_send_amount.get_sum() / opt::comm_cost(loads.get_sum());
346 XBT_INFO(",----[ Useful metrics ]");
347 PR_VALUE("Final load imbalance", "%g %s", load_imbalance,
348 "percent of the load average");
349 PR_VALUE("Data transfer amount", "%g %s", transfer_amount,
350 "times the total amount of data");
351 PR_VALUE("Number of hosts that converged", "%u / %u",
352 convergence.get_count(), loads.get_count());
353 PR_VALUE("Times of convergence (min/max/avg/dev)", "%g / %g / %g / %g",
354 convergence.get_min(), convergence.get_max(),
355 convergence.get_mean(), convergence.get_stddev());
356 PR_STATS("Idle duration", idle_duration);
357 PR_STATS("Supernumer. comp. iter.", iter_deviation);
362 XBT_ERROR("Simulation failed (%#x).", exit_status);
364 XBT_INFO("Simulation succeeded.");