3 #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_LOAD = 0x20, // lost load on exit
43 EXIT_FAILURE_OTHER = 0x40, // other error
46 // Cannot be globally initialized...
48 condition_t* proc_cond;
49 unsigned proc_counter = 0;
53 statistics data_send_amount;
54 statistics data_recv_amount;
55 statistics data_send_count;
56 statistics data_recv_count;
57 statistics ctrl_send_amount;
58 statistics ctrl_recv_amount;
59 statistics ctrl_send_count;
60 statistics ctrl_recv_count;
64 static int simulation_main(int argc, char* argv[])
69 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
71 proc_mutex->acquire();
73 proc_mutex->release();
77 proc_mutex->acquire();
78 loads.push(proc->get_real_load());
79 comps.push(proc->get_comp_amount());
80 data_send_amount.push(proc->get_data_send_amount());
81 data_recv_amount.push(proc->get_data_recv_amount());
82 data_send_count.push(proc->get_data_send_count());
83 data_recv_count.push(proc->get_data_recv_count());
84 ctrl_send_amount.push(proc->get_ctrl_send_amount());
85 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
86 ctrl_send_count.push(proc->get_ctrl_send_count());
87 ctrl_recv_count.push(proc->get_ctrl_recv_count());
89 // Synchronization barrier...
90 // The goal is to circumvent a limitation in SimGrid (at least
91 // in version 3.5): a process must be alive when another one
92 // destroys a communication they had together.
95 proc_cond->broadcast();
96 while (proc_counter > 0)
97 proc_cond->wait(*proc_mutex);
98 proc_mutex->release();
102 catch (const std::invalid_argument& e) {
103 THROWF(arg_error, 0, "%s", e.what());
105 catch (const std::exception& e) {
106 THROWF(0, 0, "%s", e.what());
111 static bool check_for_lost_load()
114 double total_init = process::get_total_load_init();
115 double total_exit = process::get_total_load_exit();
116 double lost = total_init - total_exit;
117 double lost_ratio = 100.0 * lost / total_init;
118 if (lost_ratio < -opt::load_ratio_threshold) {
119 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
122 } else if (lost_ratio > opt::load_ratio_threshold) {
123 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
127 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
129 double total_running = process::get_total_load_running();
130 double running_ratio = 100.0 * total_running / total_init;
131 if (running_ratio < -opt::load_ratio_threshold) {
132 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
133 total_running, running_ratio);
135 } else if (running_ratio > opt::load_ratio_threshold) {
136 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
137 total_running, running_ratio);
140 XBT_VERB("Running load at exit looks good: %g (%g%%)",
141 total_running, running_ratio);
145 static void signal_handler(int /*sig*/)
147 if (!opt::exit_request) {
148 XBT_CRITICAL(">>>>>>>>>>"
149 " caught CTRL-C: global exit requested "
151 opt::exit_request = 1;
153 XBT_CRITICAL(">>>>>>>>>>"
154 " caught CTRL-C for the 2nd time: exit immediately "
156 exit(EXIT_FAILURE_INTR);
160 static void install_signal_handler()
162 struct sigaction action;
163 action.sa_handler = signal_handler;
164 sigemptyset(&action.sa_mask);
165 action.sa_flags = SA_RESTART;
166 if (sigaction(SIGINT, &action, NULL) == -1) {
167 std::cerr << "sigaction: " << strerror(errno) << "\n";
168 exit(EXIT_FAILURE_OTHER);
172 #define PR_STATS(descr, st) \
173 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
174 descr " (total/avg./stddev)................................", \
175 st.get_sum(), st.get_mean(), st.get_stddev())
177 int main(int argc, char* argv[])
179 // Note: variables used after THROW must be declared as volatile.
180 volatile int exit_status = 0; // global exit status
181 volatile double simulated_time = -1.0;
182 timestamp elapsed_time(timestamp::wallclock_time);
183 timestamp simulation_time(timestamp::cpu_time);
187 elapsed_time.start();
188 simulation_time.start();
190 // Set default logging parameters
191 bool do_log_control_set = true;
192 for (int i = 1 ; do_log_control_set && i < argc ; i++)
193 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
194 strchr(argv[i] + 1, 'v'));
195 if (do_log_control_set) {
196 // xbt_log_control_set("simu.thres:verbose");
197 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
198 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
201 // Initialize some MSG internal data.
202 // Note: MSG_global_init() may throw an exception, but it seems
203 // impossible to catch it correctly :-(
204 MSG_global_init(&argc, argv);
205 install_signal_handler();
207 // Parse global parameters
208 bool parse_res = opt::parse_args(&argc, argv);
210 || opt::version_requested || opt::help_requested) {
211 if (opt::version_requested)
212 std::clog << version::name << " (" << opt::program_name << ")"
213 << " version " << version::num << "\n"
214 << version::copyright << "\n"
215 "Compiled on " << version::date << "\n\n";
216 if (!parse_res || opt::help_requested)
219 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
221 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
222 version::date.c_str());
226 exit_status = EXIT_FAILURE_INIT; // =====
228 // Register the default function of an agent
229 // MSG_function_register("simulation_main", simulation_main);
230 MSG_function_register_default(simulation_main);
232 // Create the platform and the application.
233 XBT_DEBUG("Loading platform file...");
234 MSG_create_environment(opt::platform_file.c_str());
235 XBT_DEBUG("Creating hostdata...");
237 XBT_INFO("Loaded description of %zd hosts.", hostdata::size());
238 XBT_DEBUG("Deploying processes...");
239 if (opt::auto_depl::enabled) {
240 if (!opt::auto_depl::nhosts)
241 opt::auto_depl::nhosts = hostdata::size();
242 if (opt::auto_depl::nhosts > hostdata::size()) {
243 XBT_WARN("%u hosts is too much: limiting to %zu",
244 opt::auto_depl::nhosts, hostdata::size());
245 opt::auto_depl::nhosts = hostdata::size();
247 if (opt::auto_depl::load == 0.0) {
248 XBT_WARN("Initial load is zero! "
249 "Falling back on old behaviour (load = nhosts).");
250 opt::auto_depl::load = opt::auto_depl::nhosts;
251 } else if (opt::auto_depl::load < 0.0)
252 opt::auto_depl::load =
253 -opt::auto_depl::load * opt::auto_depl::nhosts;
254 MY_launch_application(); // it is already opt::* aware...
256 MSG_launch_application(opt::deployment_file.c_str());
259 // Register tracing categories
260 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
261 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
262 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
264 exit_status = EXIT_FAILURE_SIMU; // =====
266 proc_mutex = new mutex_t();
267 proc_cond = new condition_t();
269 // Launch the MSG simulation.
270 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
272 simulated_time = MSG_get_clock();
273 XBT_INFO("Simulation ended at %f.", simulated_time);
279 THROWF(0, 0, "MSG_main() failed with status %#x", res);
281 exit_status = EXIT_NO_FAILURE; // =====
284 int len = strlen(ex.msg);
285 if (len > 0 && ex.msg[len - 1] == '\n')
286 ex.msg[len - 1] = '\0'; // strip the ending '\n'
287 XBT_ERROR("%s", ex.msg);
288 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
292 // Clean the MSG simulation.
296 XBT_ERROR("MSG_clean() failed with status %#x", res);
297 exit_status |= EXIT_FAILURE_CLEAN;
300 // Report final simulation status.
301 if (simulated_time >= 0.0) {
302 simulation_time.stop();
304 if (!check_for_lost_load())
305 exit_status |= EXIT_FAILURE_LOAD;
306 XBT_INFO(",----[ Results ]");
307 PR_STATS("Load", loads);
308 PR_STATS("Computation", comps);
309 PR_STATS("Data send amount", data_send_amount);
310 PR_STATS("Data recv amount", data_recv_amount);
311 PR_STATS("Data send count", data_send_count);
312 PR_STATS("Data recv count", data_recv_count);
313 PR_STATS("Ctrl send amount", ctrl_send_amount);
314 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
315 PR_STATS("Ctrl send count", ctrl_send_count);
316 PR_STATS("Ctrl recv count", ctrl_recv_count);
317 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
318 "Total simulated time..................................",
320 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
321 "Total simulation time.................................",
322 simulation_time.duration());
323 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
324 "Elapsed (wall clock) time.............................",
325 elapsed_time.duration());
329 XBT_ERROR("Simulation failed (%#x).", exit_status);
331 XBT_INFO("Simulation succeeded.");