7 // Creates log categories
8 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
9 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
10 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
11 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
12 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
13 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
14 XBT_LOG_NEW_SUBCATEGORY(thrd, simu, "Messages from thread wrapper class");
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
18 #include "deployment.h"
23 #include "statistics.h"
30 // Failure exit status
32 EXIT_NO_FAILURE = 0x00, // no error
33 EXIT_FAILURE_ARGS = 0x01, // bad arguments
34 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
35 EXIT_FAILURE_SIMU = 0x04, // simulation failed
36 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
39 // Cannot be globally initialized...
41 condition_t* proc_cond;
42 unsigned proc_counter = 0;
46 statistics data_send_amount;
47 statistics data_recv_amount;
48 statistics data_send_count;
49 statistics data_recv_count;
50 statistics ctrl_send_amount;
51 statistics ctrl_recv_amount;
52 statistics ctrl_send_count;
53 statistics ctrl_recv_count;
57 static int simulation_main(int argc, char* argv[])
62 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
64 proc_mutex->acquire();
66 proc_mutex->release();
70 proc_mutex->acquire();
71 loads.push(proc->get_real_load());
72 comps.push(proc->get_comp_amount());
73 data_send_amount.push(proc->get_data_send_amount());
74 data_recv_amount.push(proc->get_data_recv_amount());
75 data_send_count.push(proc->get_data_send_count());
76 data_recv_count.push(proc->get_data_recv_count());
77 ctrl_send_amount.push(proc->get_ctrl_send_amount());
78 ctrl_recv_amount.push(proc->get_ctrl_recv_amount());
79 ctrl_send_count.push(proc->get_ctrl_send_count());
80 ctrl_recv_count.push(proc->get_ctrl_recv_count());
82 // Synchronization barrier...
83 // The goal is to circumvent a limitation in SimGrid (at least
84 // in version 3.5): a process must be alive when another one
85 // destroys a communication they had together.
88 proc_cond->broadcast();
89 while (proc_counter > 0)
90 proc_cond->wait(*proc_mutex);
91 proc_mutex->release();
95 catch (const std::invalid_argument& e) {
96 THROW1(arg_error, 0, "%s", e.what());
98 catch (const std::exception& e) {
99 THROW1(0, 0, "%s", e.what());
104 static void check_for_lost_load()
106 double total_init = process::get_total_load_init();
107 double total_exit = process::get_total_load_exit();
108 double lost = total_init - total_exit;
109 double lost_ratio = 100.0 * lost / total_init;
110 if (lost_ratio < -opt::load_ratio_threshold)
111 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
113 else if (lost_ratio > opt::load_ratio_threshold)
114 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
117 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
119 double total_running = process::get_total_load_running();
120 double running_ratio = 100.0 * total_running / total_init;
121 if (running_ratio < -opt::load_ratio_threshold)
122 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
123 total_running, running_ratio);
124 else if (running_ratio > opt::load_ratio_threshold)
125 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
126 total_running, running_ratio);
128 XBT_VERB("Running load at exit looks good: %g (%g%%)",
129 total_running, running_ratio);
132 #define PR_STATS(descr, st) \
133 XBT_INFO("| %.*s: %g / %g / %g", 39, \
134 descr " total/avg./stddev. at exit.........................", \
135 st.get_sum(), st.get_mean(), st.get_stddev())
137 int main(int argc, char* argv[])
139 // Note: variables used after THROW must be declared as volatile.
140 volatile int exit_status = 0; // global exit status
141 volatile double simulated_time = -1.0;
142 timestamp simulation_time;
146 simulation_time.start();
148 // Set default logging parameters
149 bool do_log_control_set = true;
150 for (int i = 1 ; do_log_control_set && i < argc ; i++)
151 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
152 strchr(argv[i] + 1, 'v'));
153 if (do_log_control_set) {
154 // xbt_log_control_set("simu.thres:verbose");
155 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
156 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
159 // Initialize some MSG internal data.
160 // Note: MSG_global_init() may throw an exception, but it seems
161 // impossible to catch it correctly :-(
162 MSG_global_init(&argc, argv);
164 // Parse global parameters
165 bool parse_res = opt::parse_args(&argc, argv);
167 || opt::version_requested || opt::help_requested) {
168 if (opt::version_requested)
169 std::clog << version::name << " (" << opt::program_name << ")"
170 << " version " << version::num << "\n"
171 << version::copyright << "\n"
172 "Compiled on " << version::date << "\n\n";
173 if (!parse_res || opt::help_requested)
176 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
178 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
179 version::date.c_str());
183 exit_status = EXIT_FAILURE_INIT; // =====
185 // Register the default function of an agent
186 // MSG_function_register("simulation_main", simulation_main);
187 MSG_function_register_default(simulation_main);
189 // Create the platform and the application.
190 XBT_DEBUG("Loading platform file...");
191 MSG_create_environment(opt::platform_file.c_str());
192 XBT_DEBUG("Creating hostdata...");
194 XBT_DEBUG("Deploying processes...");
195 if (opt::auto_depl::enabled) {
196 if (!opt::auto_depl::nhosts)
197 opt::auto_depl::nhosts = hostdata::size();
198 if (opt::auto_depl::nhosts > hostdata::size()) {
199 XBT_WARN("%u hosts is too much: limiting to %zu",
200 opt::auto_depl::nhosts, hostdata::size());
201 opt::auto_depl::nhosts = hostdata::size();
203 if (!opt::auto_depl::load)
204 opt::auto_depl::load = opt::auto_depl::nhosts;
205 MY_launch_application(); // it is already opt::* aware...
207 MSG_launch_application(opt::deployment_file.c_str());
210 // Register tracing categories
211 TRACE_category(TRACE_CAT_COMP);
212 TRACE_category(TRACE_CAT_CTRL);
213 TRACE_category(TRACE_CAT_DATA);
215 exit_status = EXIT_FAILURE_SIMU; // =====
217 proc_mutex = new mutex_t();
218 proc_cond = new condition_t();
220 // Launch the MSG simulation.
221 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
223 simulated_time = MSG_get_clock();
224 XBT_INFO("Simulation ended at %f.", simulated_time);
230 THROW1(0, 0, "MSG_main() failed with status %#x", res);
232 exit_status = EXIT_NO_FAILURE; // =====
235 int len = strlen(ex.msg);
236 if (len > 0 && ex.msg[len - 1] == '\n')
237 ex.msg[len - 1] = '\0'; // strip the ending '\n'
238 XBT_ERROR("%s", ex.msg);
239 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
243 // Clean the MSG simulation.
247 XBT_ERROR("MSG_clean() failed with status %#x", res);
248 exit_status |= EXIT_FAILURE_CLEAN;
251 // Report final simulation status.
252 if (simulated_time >= 0.0) {
253 simulation_time.stop();
254 check_for_lost_load();
255 XBT_INFO(",----[ Results ]");
256 PR_STATS("Load", loads);
257 PR_STATS("Computation", comps);
258 PR_STATS("Data send", data_send_amount);
259 PR_STATS("Data recv", data_recv_amount);
260 PR_STATS("Data send #", data_send_count);
261 PR_STATS("Data recv #", data_recv_count);
262 PR_STATS("Ctrl send", ctrl_send_amount);
263 PR_STATS("Ctrl recv", ctrl_recv_amount);
264 PR_STATS("Ctrl send #", ctrl_send_count);
265 PR_STATS("Ctrl recv #", ctrl_recv_count);
266 XBT_INFO("| Total simulated time...................: %g",
268 XBT_INFO("| Total simulation time..................: %g",
269 simulation_time.duration());
273 XBT_ERROR("Simulation failed (%#x).", exit_status);
275 XBT_INFO("Simulation succeeded.");