2 #include <cstring> // strchr
9 // Creates log categories
10 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
11 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
12 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
13 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
14 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
15 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
16 XBT_LOG_NEW_SUBCATEGORY(thrd, simu, "Messages from thread wrapper class");
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
20 #include "deployment.h"
25 #include "statistics.h"
31 #define DATA_DESCR_WIDTH 37
34 // Failure exit status
36 EXIT_NO_FAILURE = 0x00, // no error
37 EXIT_FAILURE_ARGS = 0x01, // bad arguments
38 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
39 EXIT_FAILURE_SIMU = 0x04, // simulation failed
40 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
41 EXIT_FAILURE_INTR = 0x10, // interrupted by user
42 EXIT_FAILURE_OTHER = 0x20, // other error
45 // Cannot be globally initialized...
47 condition_t* proc_cond;
48 unsigned proc_counter = 0;
52 statistics data_send_amount;
53 statistics data_recv_amount;
54 statistics data_send_count;
55 statistics data_recv_count;
56 statistics ctrl_send_amount;
57 statistics ctrl_recv_amount;
58 statistics ctrl_send_count;
59 statistics ctrl_recv_count;
63 static int simulation_main(int argc, char* argv[])
68 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
70 proc_mutex->acquire();
72 proc_mutex->release();
76 proc_mutex->acquire();
77 loads.push(proc->get_real_load());
78 comps.push(proc->get_comp_amount());
79 data_send_amount.push(proc->get_data_send_amount());
80 data_recv_amount.push(proc->get_data_recv_amount());
81 data_send_count.push(proc->get_data_send_count());
82 data_recv_count.push(proc->get_data_recv_count());
83 ctrl_send_amount.push(proc->get_ctrl_send_amount());
84 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
85 ctrl_send_count.push(proc->get_ctrl_send_count());
86 ctrl_recv_count.push(proc->get_ctrl_recv_count());
88 // Synchronization barrier...
89 // The goal is to circumvent a limitation in SimGrid (at least
90 // in version 3.5): a process must be alive when another one
91 // destroys a communication they had together.
94 proc_cond->broadcast();
95 while (proc_counter > 0)
96 proc_cond->wait(*proc_mutex);
97 proc_mutex->release();
101 catch (const std::invalid_argument& e) {
102 THROWF(arg_error, 0, "%s", e.what());
104 catch (const std::exception& e) {
105 THROWF(0, 0, "%s", e.what());
110 static void check_for_lost_load()
112 double total_init = process::get_total_load_init();
113 double total_exit = process::get_total_load_exit();
114 double lost = total_init - total_exit;
115 double lost_ratio = 100.0 * lost / total_init;
116 if (lost_ratio < -opt::load_ratio_threshold)
117 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
119 else if (lost_ratio > opt::load_ratio_threshold)
120 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
123 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
125 double total_running = process::get_total_load_running();
126 double running_ratio = 100.0 * total_running / total_init;
127 if (running_ratio < -opt::load_ratio_threshold)
128 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
129 total_running, running_ratio);
130 else if (running_ratio > opt::load_ratio_threshold)
131 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
132 total_running, running_ratio);
134 XBT_VERB("Running load at exit looks good: %g (%g%%)",
135 total_running, running_ratio);
138 static void signal_handler(int /*sig*/)
140 if (!opt::exit_request) {
141 XBT_CRITICAL(">>>>>>>>>>"
142 " caught CTRL-C: global exit requested "
144 opt::exit_request = true;
146 XBT_CRITICAL(">>>>>>>>>>"
147 " caught CTRL-C for the 2nd time: exit immediately "
149 exit(EXIT_FAILURE_INTR);
153 static void install_signal_handler()
155 struct sigaction action;
156 action.sa_handler = signal_handler;
157 sigemptyset(&action.sa_mask);
158 action.sa_flags = SA_RESTART;
159 if (sigaction(SIGINT, &action, NULL) == -1) {
160 std::cerr << "sigaction: " << strerror(errno) << "\n";
161 exit(EXIT_FAILURE_OTHER);
165 #define PR_STATS(descr, st) \
166 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
167 descr " (total/avg./stddev)................................", \
168 st.get_sum(), st.get_mean(), st.get_stddev())
170 int main(int argc, char* argv[])
172 // Note: variables used after THROW must be declared as volatile.
173 volatile int exit_status = 0; // global exit status
174 volatile double simulated_time = -1.0;
175 timestamp elapsed_time(timestamp::wallclock_time);
176 timestamp simulation_time(timestamp::cpu_time);
180 elapsed_time.start();
181 simulation_time.start();
183 // Set default logging parameters
184 bool do_log_control_set = true;
185 for (int i = 1 ; do_log_control_set && i < argc ; i++)
186 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
187 strchr(argv[i] + 1, 'v'));
188 if (do_log_control_set) {
189 // xbt_log_control_set("simu.thres:verbose");
190 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
191 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
194 // Initialize some MSG internal data.
195 // Note: MSG_global_init() may throw an exception, but it seems
196 // impossible to catch it correctly :-(
197 MSG_global_init(&argc, argv);
198 install_signal_handler();
200 // Parse global parameters
201 bool parse_res = opt::parse_args(&argc, argv);
203 || opt::version_requested || opt::help_requested) {
204 if (opt::version_requested)
205 std::clog << version::name << " (" << opt::program_name << ")"
206 << " version " << version::num << "\n"
207 << version::copyright << "\n"
208 "Compiled on " << version::date << "\n\n";
209 if (!parse_res || opt::help_requested)
212 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
214 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
215 version::date.c_str());
219 exit_status = EXIT_FAILURE_INIT; // =====
221 // Register the default function of an agent
222 // MSG_function_register("simulation_main", simulation_main);
223 MSG_function_register_default(simulation_main);
225 // Create the platform and the application.
226 XBT_DEBUG("Loading platform file...");
227 MSG_create_environment(opt::platform_file.c_str());
228 XBT_DEBUG("Creating hostdata...");
230 XBT_DEBUG("Deploying processes...");
231 if (opt::auto_depl::enabled) {
232 if (!opt::auto_depl::nhosts)
233 opt::auto_depl::nhosts = hostdata::size();
234 if (opt::auto_depl::nhosts > hostdata::size()) {
235 XBT_WARN("%u hosts is too much: limiting to %zu",
236 opt::auto_depl::nhosts, hostdata::size());
237 opt::auto_depl::nhosts = hostdata::size();
239 if (!opt::auto_depl::load)
240 opt::auto_depl::load = opt::auto_depl::nhosts;
241 MY_launch_application(); // it is already opt::* aware...
243 MSG_launch_application(opt::deployment_file.c_str());
246 // Register tracing categories
247 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
248 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
249 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
251 exit_status = EXIT_FAILURE_SIMU; // =====
253 proc_mutex = new mutex_t();
254 proc_cond = new condition_t();
256 // Launch the MSG simulation.
257 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
259 simulated_time = MSG_get_clock();
260 XBT_INFO("Simulation ended at %f.", simulated_time);
266 THROWF(0, 0, "MSG_main() failed with status %#x", res);
268 exit_status = EXIT_NO_FAILURE; // =====
271 int len = strlen(ex.msg);
272 if (len > 0 && ex.msg[len - 1] == '\n')
273 ex.msg[len - 1] = '\0'; // strip the ending '\n'
274 XBT_ERROR("%s", ex.msg);
275 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
279 // Clean the MSG simulation.
283 XBT_ERROR("MSG_clean() failed with status %#x", res);
284 exit_status |= EXIT_FAILURE_CLEAN;
287 // Report final simulation status.
288 if (simulated_time >= 0.0) {
289 simulation_time.stop();
291 check_for_lost_load();
292 XBT_INFO(",----[ Results ]");
293 PR_STATS("Load", loads);
294 PR_STATS("Computation", comps);
295 PR_STATS("Data send amount", data_send_amount);
296 PR_STATS("Data recv amount", data_recv_amount);
297 PR_STATS("Data send count", data_send_count);
298 PR_STATS("Data recv count", data_recv_count);
299 PR_STATS("Ctrl send amount", ctrl_send_amount);
300 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
301 PR_STATS("Ctrl send count", ctrl_send_count);
302 PR_STATS("Ctrl recv count", ctrl_recv_count);
303 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
304 "Total simulated time..................................",
306 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
307 "Total simulation time.................................",
308 simulation_time.duration());
309 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
310 "Elapsed (wall clock) time.............................",
311 elapsed_time.duration());
315 XBT_ERROR("Simulation failed (%#x).", exit_status);
317 XBT_INFO("Simulation succeeded.");