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
40 condition_t proc_cond;
41 unsigned proc_counter = 0;
48 static int simulation_main(int argc, char* argv[])
53 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
62 comps.push(proc->get_comp());
63 loads.push(proc->get_real_load());
65 // Synchronization barrier...
66 // The goal is to circumvent a limitation in SimGrid (at least
67 // in version 3.5): a process must be alive when another one
68 // destroys a communication they had together.
71 proc_cond.broadcast();
72 while (proc_counter > 0)
73 proc_cond.wait(proc_mutex);
78 catch (const std::invalid_argument& e) {
79 THROW1(arg_error, 0, "%s", e.what());
81 catch (const std::exception& e) {
82 THROW1(0, 0, "%s", e.what());
87 static void check_for_lost_load()
89 double total_init = process::get_total_load_init();
91 double total_exit = process::get_total_load_exit();
92 double lost = total_init - total_exit;
93 double lost_ratio = 100.0 * lost / total_init;
94 if (lost_ratio < -opt::load_ratio_threshold)
95 XBT_ERROR("Gained load at exit! %g (%g%%) <============",
97 else if (lost_ratio > opt::load_ratio_threshold)
98 XBT_ERROR("Lost load at exit! %g (%g%%) <============",
101 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
103 double total_running = process::get_total_load_running();
104 double running_ratio = 100.0 * total_running / total_init;
105 if (running_ratio < -opt::load_ratio_threshold)
106 XBT_ERROR("Negative running load at exit! %g (%g%%) <============",
107 total_running, running_ratio);
108 else if (running_ratio > opt::load_ratio_threshold)
109 XBT_ERROR("Remaining running load at exit! %g (%g%%) <============",
110 total_running, running_ratio);
112 XBT_VERB("Running load at exit looks good: %g (%g%%)",
113 total_running, running_ratio);
116 #define PR_STATS(descr, st) \
117 XBT_INFO("| %.*s: %g / %g / %g", 39, \
118 descr " total/avg./stddev. at exit.........................", \
119 st.get_sum(), st.get_mean(), st.get_stddev())
121 int main(int argc, char* argv[])
123 // Note: variables used after THROW must be declared as volatile.
124 volatile int exit_status = 0; // global exit status
125 volatile double simulated_time = -1.0;
126 timestamp simulation_time;
130 simulation_time.start();
132 // Set default logging parameters
133 bool do_log_control_set = true;
134 for (int i = 1 ; do_log_control_set && i < argc ; i++)
135 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
136 strchr(argv[i] + 1, 'v'));
137 if (do_log_control_set) {
138 // xbt_log_control_set("simu.thres:verbose");
139 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
140 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
143 // Initialize some MSG internal data.
144 // Note: MSG_global_init() may throw an exception, but it seems
145 // impossible to catch it correctly :-(
146 MSG_global_init(&argc, argv);
148 // Parse global parameters
149 bool parse_res = opt::parse_args(&argc, argv);
151 || opt::version_requested || opt::help_requested) {
152 if (opt::version_requested)
153 std::clog << version::name << " (" << opt::program_name << ")"
154 << " version " << version::num << "\n"
155 << version::copyright << "\n"
156 "Compiled on " << version::date << "\n\n";
157 if (!parse_res || opt::help_requested)
160 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
162 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
163 version::date.c_str());
167 exit_status = EXIT_FAILURE_INIT; // =====
169 // Register the default function of an agent
170 // MSG_function_register("simulation_main", simulation_main);
171 MSG_function_register_default(simulation_main);
173 // Create the platform and the application.
174 MSG_create_environment(opt::platform_file.c_str());
176 if (opt::auto_depl::enabled) {
177 if (!opt::auto_depl::nhosts)
178 opt::auto_depl::nhosts = hostdata::size();
179 if (opt::auto_depl::nhosts > hostdata::size()) {
180 XBT_WARN("%u hosts is too much: limiting to %zu",
181 opt::auto_depl::nhosts, hostdata::size());
182 opt::auto_depl::nhosts = hostdata::size();
184 if (!opt::auto_depl::load)
185 opt::auto_depl::load = opt::auto_depl::nhosts;
186 MY_launch_application(); // it is already opt::* aware...
188 MSG_launch_application(opt::deployment_file.c_str());
191 // Register tracing categories
192 TRACE_category(TRACE_CAT_COMP);
193 TRACE_category(TRACE_CAT_CTRL);
194 TRACE_category(TRACE_CAT_DATA);
196 exit_status = EXIT_FAILURE_SIMU; // =====
198 // Launch the MSG simulation.
199 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
201 simulated_time = MSG_get_clock();
202 XBT_INFO("Simulation ended at %f.", simulated_time);
205 THROW1(0, 0, "MSG_main() failed with status %#x", res);
207 exit_status = EXIT_NO_FAILURE; // =====
210 int len = strlen(ex.msg);
211 if (len > 0 && ex.msg[len - 1] == '\n')
212 ex.msg[len - 1] = '\0'; // strip the ending '\n'
213 XBT_ERROR("%s", ex.msg);
214 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
218 // Clean the MSG simulation.
222 XBT_ERROR("MSG_clean() failed with status %#x", res);
223 exit_status |= EXIT_FAILURE_CLEAN;
226 // Report final simulation status.
227 if (simulated_time >= 0.0) {
228 simulation_time.stop();
229 check_for_lost_load();
230 XBT_INFO(",----[ Results ]");
231 PR_STATS("Load", loads);
232 PR_STATS("Computation", comps);
233 XBT_INFO("| Total simulated time...................: %g",
235 XBT_INFO("| Total simulation time..................: %g",
236 simulation_time.duration());
240 XBT_ERROR("Simulation failed (%#x).", exit_status);
242 XBT_INFO("Simulation succeeded.");