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");
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
17 #include "deployment.h"
22 #include "statistics.h"
29 // Failure exit status
31 EXIT_NO_FAILURE = 0x00, // no error
32 EXIT_FAILURE_ARGS = 0x01, // bad arguments
33 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
34 EXIT_FAILURE_SIMU = 0x04, // simulation failed
35 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
39 condition_t proc_cond;
40 unsigned proc_counter = 0;
47 static int simulation_main(int argc, char* argv[])
52 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
61 comps.push(proc->get_comp());
62 loads.push(proc->get_real_load());
64 // Synchronization barrier...
65 // The goal is to circumvent a limitation in SimGrid (at least
66 // in version 3.5): a process must be alive when another one
67 // destroys a communication they had together.
70 proc_cond.broadcast();
71 while (proc_counter > 0)
72 proc_cond.wait(proc_mutex);
77 catch (std::invalid_argument& e) {
78 THROW1(arg_error, 0, "%s", e.what());
83 static void check_for_lost_load()
85 double total_init = process::get_total_load_init();
87 double total_exit = process::get_total_load_exit();
88 double lost = total_init - total_exit;
89 double lost_ratio = 100.0 * lost / total_init;
90 if (lost_ratio < -opt::load_ratio_threshold)
91 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
93 else if (lost_ratio > opt::load_ratio_threshold)
94 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
97 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
99 double total_running = process::get_total_load_running();
100 double running_ratio = 100.0 * total_running / total_init;
101 if (running_ratio < -opt::load_ratio_threshold)
102 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
103 total_running, running_ratio);
104 else if (running_ratio > opt::load_ratio_threshold)
105 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
106 total_running, running_ratio);
108 XBT_VERB("Running load at exit looks good: %g (%g%%)",
109 total_running, running_ratio);
112 #define PR_STATS(descr, st) \
113 XBT_INFO("| %.*s: %g / %g / %g", 39, \
114 descr " total/avg./stddev. at exit.........................", \
115 st.get_sum(), st.get_mean(), st.get_stddev())
117 int main(int argc, char* argv[])
119 // Note: variables used after THROW must be declared as volatile.
120 volatile int exit_status = 0; // global exit status
121 volatile double simulated_time = -1.0;
122 timestamp simulation_time;
126 simulation_time.start();
128 // Set default logging parameters
129 bool do_log_control_set = true;
130 for (int i = 1 ; do_log_control_set && i < argc ; i++)
131 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
132 strchr(argv[i] + 1, 'v'));
133 if (do_log_control_set) {
134 // xbt_log_control_set("simu.thres:verbose");
135 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
136 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
139 // Initialize some MSG internal data.
140 // Note: MSG_global_init() may throw an exception, but it seems
141 // impossible to catch it correctly :-(
142 MSG_global_init(&argc, argv);
144 // Parse global parameters
145 bool parse_res = opt::parse_args(&argc, argv);
147 || opt::version_requested || opt::help_requested) {
148 if (opt::version_requested)
149 std::clog << version::name << " (" << opt::program_name << ")"
150 << " version " << version::num << "\n"
151 << version::copyright << "\n"
152 "Compiled on " << version::date << "\n\n";
153 if (!parse_res || opt::help_requested)
156 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
158 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
159 version::date.c_str());
163 exit_status = EXIT_FAILURE_INIT; // =====
165 // Register the default function of an agent
166 // MSG_function_register("simulation_main", simulation_main);
167 MSG_function_register_default(simulation_main);
169 // Create the platform and the application.
170 MSG_create_environment(opt::platform_file.c_str());
172 if (opt::auto_depl::enabled) {
173 if (!opt::auto_depl::nhosts)
174 opt::auto_depl::nhosts = hostdata::size();
175 if (opt::auto_depl::nhosts > hostdata::size()) {
176 XBT_WARN("%u hosts is too much: limiting to %zu",
177 opt::auto_depl::nhosts, hostdata::size());
178 opt::auto_depl::nhosts = hostdata::size();
180 if (!opt::auto_depl::load)
181 opt::auto_depl::load = opt::auto_depl::nhosts;
182 MY_launch_application(); // it is already opt::* aware...
184 MSG_launch_application(opt::deployment_file.c_str());
187 // Register tracing categories
188 TRACE_category(TRACE_CAT_COMP);
189 TRACE_category(TRACE_CAT_CTRL);
190 TRACE_category(TRACE_CAT_DATA);
192 exit_status = EXIT_FAILURE_SIMU; // =====
194 // Launch the MSG simulation.
195 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
197 simulated_time = MSG_get_clock();
198 XBT_INFO("Simulation ended at %f.", simulated_time);
201 THROW1(0, 0, "MSG_main() failed with status %#x", res);
203 exit_status = EXIT_NO_FAILURE; // =====
206 int len = strlen(ex.msg);
207 if (len > 0 && ex.msg[len - 1] == '\n')
208 ex.msg[len - 1] = '\0'; // strip the ending '\n'
209 XBT_ERROR("%s", ex.msg);
210 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
214 // Clean the MSG simulation.
218 XBT_ERROR("MSG_clean() failed with status %#x", res);
219 exit_status |= EXIT_FAILURE_CLEAN;
222 // Report final simulation status.
223 if (simulated_time >= 0.0) {
224 simulation_time.stop();
225 check_for_lost_load();
226 XBT_INFO(",----[ Results ]");
227 PR_STATS("Load", loads);
228 PR_STATS("Computation", comps);
229 XBT_INFO("| Total simulated time...................: %g",
231 XBT_INFO("| Total simulation time..................: %g",
232 simulation_time.duration());
236 XBT_ERROR("Simulation failed (%#x).", exit_status);
238 XBT_INFO("Simulation succeeded.");