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 simulation_time;
173 simulation_time.start();
175 // Set default logging parameters
176 bool do_log_control_set = true;
177 for (int i = 1 ; do_log_control_set && i < argc ; i++)
178 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
179 strchr(argv[i] + 1, 'v'));
180 if (do_log_control_set) {
181 // xbt_log_control_set("simu.thres:verbose");
182 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
183 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
186 // Initialize some MSG internal data.
187 // Note: MSG_global_init() may throw an exception, but it seems
188 // impossible to catch it correctly :-(
189 MSG_global_init(&argc, argv);
190 install_signal_handler();
192 // Parse global parameters
193 bool parse_res = opt::parse_args(&argc, argv);
195 || opt::version_requested || opt::help_requested) {
196 if (opt::version_requested)
197 std::clog << version::name << " (" << opt::program_name << ")"
198 << " version " << version::num << "\n"
199 << version::copyright << "\n"
200 "Compiled on " << version::date << "\n\n";
201 if (!parse_res || opt::help_requested)
204 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
206 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
207 version::date.c_str());
211 exit_status = EXIT_FAILURE_INIT; // =====
213 // Register the default function of an agent
214 // MSG_function_register("simulation_main", simulation_main);
215 MSG_function_register_default(simulation_main);
217 // Create the platform and the application.
218 XBT_DEBUG("Loading platform file...");
219 MSG_create_environment(opt::platform_file.c_str());
220 XBT_DEBUG("Creating hostdata...");
222 XBT_DEBUG("Deploying processes...");
223 if (opt::auto_depl::enabled) {
224 if (!opt::auto_depl::nhosts)
225 opt::auto_depl::nhosts = hostdata::size();
226 if (opt::auto_depl::nhosts > hostdata::size()) {
227 XBT_WARN("%u hosts is too much: limiting to %zu",
228 opt::auto_depl::nhosts, hostdata::size());
229 opt::auto_depl::nhosts = hostdata::size();
231 if (!opt::auto_depl::load)
232 opt::auto_depl::load = opt::auto_depl::nhosts;
233 MY_launch_application(); // it is already opt::* aware...
235 MSG_launch_application(opt::deployment_file.c_str());
238 // Register tracing categories
239 TRACE_category_with_color(TRACE_CAT_COMP, TRACE_COLOR_COMP);
240 TRACE_category_with_color(TRACE_CAT_CTRL, TRACE_COLOR_CTRL);
241 TRACE_category_with_color(TRACE_CAT_DATA, TRACE_COLOR_DATA);
243 exit_status = EXIT_FAILURE_SIMU; // =====
245 proc_mutex = new mutex_t();
246 proc_cond = new condition_t();
248 // Launch the MSG simulation.
249 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
251 simulated_time = MSG_get_clock();
252 XBT_INFO("Simulation ended at %f.", simulated_time);
258 THROWF(0, 0, "MSG_main() failed with status %#x", res);
260 exit_status = EXIT_NO_FAILURE; // =====
263 int len = strlen(ex.msg);
264 if (len > 0 && ex.msg[len - 1] == '\n')
265 ex.msg[len - 1] = '\0'; // strip the ending '\n'
266 XBT_ERROR("%s", ex.msg);
267 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
271 // Clean the MSG simulation.
275 XBT_ERROR("MSG_clean() failed with status %#x", res);
276 exit_status |= EXIT_FAILURE_CLEAN;
279 // Report final simulation status.
280 if (simulated_time >= 0.0) {
281 simulation_time.stop();
282 check_for_lost_load();
283 XBT_INFO(",----[ Results ]");
284 PR_STATS("Load", loads);
285 PR_STATS("Computation", comps);
286 PR_STATS("Data send amount", data_send_amount);
287 PR_STATS("Data recv amount", data_recv_amount);
288 PR_STATS("Data send count", data_send_count);
289 PR_STATS("Data recv count", data_recv_count);
290 PR_STATS("Ctrl send amount", ctrl_send_amount);
291 PR_STATS("Ctrl recv amount", ctrl_recv_amount);
292 PR_STATS("Ctrl send count", ctrl_send_count);
293 PR_STATS("Ctrl recv count", ctrl_recv_count);
294 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
295 "Total simulated time..................................",
297 XBT_INFO("| %.*s: %g", DATA_DESCR_WIDTH,
298 "Total simulation time.................................",
299 simulation_time.duration());
303 XBT_ERROR("Simulation failed (%#x).", exit_status);
305 XBT_INFO("Simulation succeeded.");