2 #include <cstring> // strchr
9 // Don't look at this dirty hack...
10 // Delete it when THROWF comes in SG/svn.
12 #define THROWF(...) THROW1(__VA_ARGS__)
15 // Creates log categories
16 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
17 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
18 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
19 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
20 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
21 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
22 XBT_LOG_NEW_SUBCATEGORY(thrd, simu, "Messages from thread wrapper class");
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
26 #include "deployment.h"
31 #include "statistics.h"
37 #define DATA_DESCR_WIDTH 37
40 // Failure exit status
42 EXIT_NO_FAILURE = 0x00, // no error
43 EXIT_FAILURE_ARGS = 0x01, // bad arguments
44 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
45 EXIT_FAILURE_SIMU = 0x04, // simulation failed
46 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
47 EXIT_FAILURE_OTHER = 0x10, // other error
50 // Cannot be globally initialized...
52 condition_t* proc_cond;
53 unsigned proc_counter = 0;
57 statistics data_send_amount;
58 statistics data_recv_amount;
59 statistics data_send_count;
60 statistics data_recv_count;
61 statistics ctrl_send_amount;
62 statistics ctrl_recv_amount;
63 statistics ctrl_send_count;
64 statistics ctrl_recv_count;
68 static int simulation_main(int argc, char* argv[])
73 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
75 proc_mutex->acquire();
77 proc_mutex->release();
81 proc_mutex->acquire();
82 loads.push(proc->get_real_load());
83 comps.push(proc->get_comp_amount());
84 data_send_amount.push(proc->get_data_send_amount());
85 data_recv_amount.push(proc->get_data_recv_amount());
86 data_send_count.push(proc->get_data_send_count());
87 data_recv_count.push(proc->get_data_recv_count());
88 ctrl_send_amount.push(proc->get_ctrl_send_amount());
89 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
90 ctrl_send_count.push(proc->get_ctrl_send_count());
91 ctrl_recv_count.push(proc->get_ctrl_recv_count());
93 // Synchronization barrier...
94 // The goal is to circumvent a limitation in SimGrid (at least
95 // in version 3.5): a process must be alive when another one
96 // destroys a communication they had together.
99 proc_cond->broadcast();
100 while (proc_counter > 0)
101 proc_cond->wait(*proc_mutex);
102 proc_mutex->release();
106 catch (const std::invalid_argument& e) {
107 THROWF(arg_error, 0, "%s", e.what());
109 catch (const std::exception& e) {
110 THROWF(0, 0, "%s", e.what());
115 static void check_for_lost_load()
117 double total_init = process::get_total_load_init();
118 double total_exit = process::get_total_load_exit();
119 double lost = total_init - total_exit;
120 double lost_ratio = 100.0 * lost / total_init;
121 if (lost_ratio < -opt::load_ratio_threshold)
122 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
124 else if (lost_ratio > opt::load_ratio_threshold)
125 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
128 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
130 double total_running = process::get_total_load_running();
131 double running_ratio = 100.0 * total_running / total_init;
132 if (running_ratio < -opt::load_ratio_threshold)
133 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
134 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);
139 XBT_VERB("Running load at exit looks good: %g (%g%%)",
140 total_running, running_ratio);
143 static void signal_handler(int /*sig*/)
145 if (!opt::exit_request) {
146 XBT_CRITICAL(">>>>>>>>>>"
147 " caught CTRL-C: global exit requested "
149 opt::exit_request = true;
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 simulation_time;
179 simulation_time.start();
181 // Set default logging parameters
182 bool do_log_control_set = true;
183 for (int i = 1 ; do_log_control_set && i < argc ; i++)
184 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
185 strchr(argv[i] + 1, 'v'));
186 if (do_log_control_set) {
187 // xbt_log_control_set("simu.thres:verbose");
188 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
189 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
192 // Initialize some MSG internal data.
193 // Note: MSG_global_init() may throw an exception, but it seems
194 // impossible to catch it correctly :-(
195 MSG_global_init(&argc, argv);
196 install_signal_handler();
198 // Parse global parameters
199 bool parse_res = opt::parse_args(&argc, argv);
201 || opt::version_requested || opt::help_requested) {
202 if (opt::version_requested)
203 std::clog << version::name << " (" << opt::program_name << ")"
204 << " version " << version::num << "\n"
205 << version::copyright << "\n"
206 "Compiled on " << version::date << "\n\n";
207 if (!parse_res || opt::help_requested)
210 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
212 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
213 version::date.c_str());
217 exit_status = EXIT_FAILURE_INIT; // =====
219 // Register the default function of an agent
220 // MSG_function_register("simulation_main", simulation_main);
221 MSG_function_register_default(simulation_main);
223 // Create the platform and the application.
224 XBT_DEBUG("Loading platform file...");
225 MSG_create_environment(opt::platform_file.c_str());
226 XBT_DEBUG("Creating hostdata...");
228 XBT_DEBUG("Deploying processes...");
229 if (opt::auto_depl::enabled) {
230 if (!opt::auto_depl::nhosts)
231 opt::auto_depl::nhosts = hostdata::size();
232 if (opt::auto_depl::nhosts > hostdata::size()) {
233 XBT_WARN("%u hosts is too much: limiting to %zu",
234 opt::auto_depl::nhosts, hostdata::size());
235 opt::auto_depl::nhosts = hostdata::size();
237 if (!opt::auto_depl::load)
238 opt::auto_depl::load = opt::auto_depl::nhosts;
239 MY_launch_application(); // it is already opt::* aware...
241 MSG_launch_application(opt::deployment_file.c_str());
244 // Register tracing categories
245 TRACE_category(TRACE_CAT_COMP);
246 TRACE_category(TRACE_CAT_CTRL);
247 TRACE_category(TRACE_CAT_DATA);
249 exit_status = EXIT_FAILURE_SIMU; // =====
251 proc_mutex = new mutex_t();
252 proc_cond = new condition_t();
254 // Launch the MSG simulation.
255 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
257 simulated_time = MSG_get_clock();
258 XBT_INFO("Simulation ended at %f.", simulated_time);
264 THROWF(0, 0, "MSG_main() failed with status %#x", res);
266 exit_status = EXIT_NO_FAILURE; // =====
269 int len = strlen(ex.msg);
270 if (len > 0 && ex.msg[len - 1] == '\n')
271 ex.msg[len - 1] = '\0'; // strip the ending '\n'
272 XBT_ERROR("%s", ex.msg);
273 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
277 // Clean the MSG simulation.
281 XBT_ERROR("MSG_clean() failed with status %#x", res);
282 exit_status |= EXIT_FAILURE_CLEAN;
285 // Report final simulation status.
286 if (simulated_time >= 0.0) {
287 simulation_time.stop();
288 check_for_lost_load();
289 XBT_INFO(",----[ Results ]");
290 PR_STATS("Load", loads);
291 PR_STATS("Computation", comps);
292 PR_STATS("Data send amount", data_send_amount);
293 PR_STATS("Data recv amount", data_recv_amount);
294 PR_STATS("Data send count", data_send_count);
295 PR_STATS("Data recv count", data_recv_count);
296 PR_STATS("Ctrl send amount", ctrl_send_amount);
297 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
298 PR_STATS("Ctrl send count", ctrl_send_count);
299 PR_STATS("Ctrl recv count", ctrl_recv_count);
300 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
301 "Total simulated time..................................",
303 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
304 "Total simulation time.................................",
305 simulation_time.duration());
309 XBT_ERROR("Simulation failed (%#x).", exit_status);
311 XBT_INFO("Simulation succeeded.");