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_OTHER = 0x10, // other error
44 // Cannot be globally initialized...
46 condition_t* proc_cond;
47 unsigned proc_counter = 0;
51 statistics data_send_amount;
52 statistics data_recv_amount;
53 statistics data_send_count;
54 statistics data_recv_count;
55 statistics ctrl_send_amount;
56 statistics ctrl_recv_amount;
57 statistics ctrl_send_count;
58 statistics ctrl_recv_count;
62 static int simulation_main(int argc, char* argv[])
67 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
69 proc_mutex->acquire();
71 proc_mutex->release();
75 proc_mutex->acquire();
76 loads.push(proc->get_real_load());
77 comps.push(proc->get_comp_amount());
78 data_send_amount.push(proc->get_data_send_amount());
79 data_recv_amount.push(proc->get_data_recv_amount());
80 data_send_count.push(proc->get_data_send_count());
81 data_recv_count.push(proc->get_data_recv_count());
82 ctrl_send_amount.push(proc->get_ctrl_send_amount());
83 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
84 ctrl_send_count.push(proc->get_ctrl_send_count());
85 ctrl_recv_count.push(proc->get_ctrl_recv_count());
87 // Synchronization barrier...
88 // The goal is to circumvent a limitation in SimGrid (at least
89 // in version 3.5): a process must be alive when another one
90 // destroys a communication they had together.
93 proc_cond->broadcast();
94 while (proc_counter > 0)
95 proc_cond->wait(*proc_mutex);
96 proc_mutex->release();
100 catch (const std::invalid_argument& e) {
101 THROWF(arg_error, 0, "%s", e.what());
103 catch (const std::exception& e) {
104 THROWF(0, 0, "%s", e.what());
109 static void check_for_lost_load()
111 double total_init = process::get_total_load_init();
112 double total_exit = process::get_total_load_exit();
113 double lost = total_init - total_exit;
114 double lost_ratio = 100.0 * lost / total_init;
115 if (lost_ratio < -opt::load_ratio_threshold)
116 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
118 else if (lost_ratio > opt::load_ratio_threshold)
119 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
122 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
124 double total_running = process::get_total_load_running();
125 double running_ratio = 100.0 * total_running / total_init;
126 if (running_ratio < -opt::load_ratio_threshold)
127 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
128 total_running, running_ratio);
129 else if (running_ratio > opt::load_ratio_threshold)
130 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
131 total_running, running_ratio);
133 XBT_VERB("Running load at exit looks good: %g (%g%%)",
134 total_running, running_ratio);
137 static void signal_handler(int /*sig*/)
139 if (!opt::exit_request) {
140 XBT_CRITICAL(">>>>>>>>>>"
141 " caught CTRL-C: global exit requested "
143 opt::exit_request = true;
147 static void install_signal_handler()
149 struct sigaction action;
150 action.sa_handler = signal_handler;
151 sigemptyset(&action.sa_mask);
152 action.sa_flags = SA_RESTART;
153 if (sigaction (SIGINT, &action, NULL) == -1) {
154 std::cerr << "sigaction: " << strerror(errno) << "\n";
155 exit(EXIT_FAILURE_OTHER);
159 #define PR_STATS(descr, st) \
160 XBT_INFO("| %.*s: %g / %g / %g", DATA_DESCR_WIDTH, \
161 descr " (total/avg./stddev)................................", \
162 st.get_sum(), st.get_mean(), st.get_stddev())
164 int main(int argc, char* argv[])
166 // Note: variables used after THROW must be declared as volatile.
167 volatile int exit_status = 0; // global exit status
168 volatile double simulated_time = -1.0;
169 timestamp elapsed_time(timestamp::wallclock_time);
170 timestamp simulation_time(timestamp::cpu_time);
174 elapsed_time.start();
175 simulation_time.start();
177 // Set default logging parameters
178 bool do_log_control_set = true;
179 for (int i = 1 ; do_log_control_set && i < argc ; i++)
180 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
181 strchr(argv[i] + 1, 'v'));
182 if (do_log_control_set) {
183 // xbt_log_control_set("simu.thres:verbose");
184 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
185 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
188 // Initialize some MSG internal data.
189 // Note: MSG_global_init() may throw an exception, but it seems
190 // impossible to catch it correctly :-(
191 MSG_global_init(&argc, argv);
192 install_signal_handler();
194 // Parse global parameters
195 bool parse_res = opt::parse_args(&argc, argv);
197 || opt::version_requested || opt::help_requested) {
198 if (opt::version_requested)
199 std::clog << version::name << " (" << opt::program_name << ")"
200 << " version " << version::num << "\n"
201 << version::copyright << "\n"
202 "Compiled on " << version::date << "\n\n";
203 if (!parse_res || opt::help_requested)
206 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
208 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
209 version::date.c_str());
213 exit_status = EXIT_FAILURE_INIT; // =====
215 // Register the default function of an agent
216 // MSG_function_register("simulation_main", simulation_main);
217 MSG_function_register_default(simulation_main);
219 // Create the platform and the application.
220 XBT_DEBUG("Loading platform file...");
221 MSG_create_environment(opt::platform_file.c_str());
222 XBT_DEBUG("Creating hostdata...");
224 XBT_DEBUG("Deploying processes...");
225 if (opt::auto_depl::enabled) {
226 if (!opt::auto_depl::nhosts)
227 opt::auto_depl::nhosts = hostdata::size();
228 if (opt::auto_depl::nhosts > hostdata::size()) {
229 XBT_WARN("%u hosts is too much: limiting to %zu",
230 opt::auto_depl::nhosts, hostdata::size());
231 opt::auto_depl::nhosts = hostdata::size();
233 if (!opt::auto_depl::load)
234 opt::auto_depl::load = opt::auto_depl::nhosts;
235 MY_launch_application(); // it is already opt::* aware...
237 MSG_launch_application(opt::deployment_file.c_str());
240 // Register tracing categories
241 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
242 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
243 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
245 exit_status = EXIT_FAILURE_SIMU; // =====
247 proc_mutex = new mutex_t();
248 proc_cond = new condition_t();
250 // Launch the MSG simulation.
251 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
253 simulated_time = MSG_get_clock();
254 XBT_INFO("Simulation ended at %f.", simulated_time);
260 THROWF(0, 0, "MSG_main() failed with status %#x", res);
262 exit_status = EXIT_NO_FAILURE; // =====
265 int len = strlen(ex.msg);
266 if (len > 0 && ex.msg[len - 1] == '\n')
267 ex.msg[len - 1] = '\0'; // strip the ending '\n'
268 XBT_ERROR("%s", ex.msg);
269 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
273 // Clean the MSG simulation.
277 XBT_ERROR("MSG_clean() failed with status %#x", res);
278 exit_status |= EXIT_FAILURE_CLEAN;
281 // Report final simulation status.
282 if (simulated_time >= 0.0) {
283 simulation_time.stop();
285 check_for_lost_load();
286 XBT_INFO(",----[ Results ]");
287 PR_STATS("Load", loads);
288 PR_STATS("Computation", comps);
289 PR_STATS("Data send amount", data_send_amount);
290 PR_STATS("Data recv amount", data_recv_amount);
291 PR_STATS("Data send count", data_send_count);
292 PR_STATS("Data recv count", data_recv_count);
293 PR_STATS("Ctrl send amount", ctrl_send_amount);
294 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
295 PR_STATS("Ctrl send count", ctrl_send_count);
296 PR_STATS("Ctrl recv count", ctrl_recv_count);
297 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
298 "Total simulated time..................................",
300 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
301 "Total simulation time.................................",
302 simulation_time.duration());
303 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
304 "Elapsed (wall clock) time.............................",
305 elapsed_time.duration());
309 XBT_ERROR("Simulation failed (%#x).", exit_status);
311 XBT_INFO("Simulation succeeded.");