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"
28 // Failure exit status
30 EXIT_NO_FAILURE = 0x00, // no error
31 EXIT_FAILURE_ARGS = 0x01, // bad arguments
32 EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
33 EXIT_FAILURE_SIMU = 0x04, // simulation failed
34 EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
37 xbt_mutex_t proc_mutex;
39 unsigned proc_counter;
41 struct statistics comps;
42 struct statistics loads;
46 static int simulation_main(int argc, char* argv[])
51 proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
53 xbt_mutex_acquire(proc_mutex);
55 xbt_mutex_release(proc_mutex);
59 xbt_mutex_acquire(proc_mutex);
60 comps.push(proc->get_comp());
61 loads.push(proc->get_real_load());
63 // Synchronization barrier...
64 // The goal is to circumvent a limitation in SimGrid (at least
65 // in version 3.5): a process must be alive when another one
66 // destroys a communication they had together.
69 xbt_cond_broadcast(proc_cond);
70 while (proc_counter > 0)
71 xbt_cond_wait(proc_cond, proc_mutex);
72 xbt_mutex_release(proc_mutex);
76 catch (std::invalid_argument& e) {
77 THROW1(arg_error, 0, "%s", e.what());
82 static void check_for_lost_load()
84 double total_init = process::get_total_load_init();
86 double total_exit = process::get_total_load_exit();
87 double lost = total_init - total_exit;
88 double lost_ratio = 100.0 * lost / total_init;
89 if (lost_ratio < -opt::load_ratio_threshold)
90 XBT_CRITICAL("Gained load at exit! %g (%g%%) <============",
92 else if (lost_ratio > opt::load_ratio_threshold)
93 XBT_CRITICAL("Lost load at exit! %g (%g%%) <============",
96 XBT_VERB("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
98 double total_running = process::get_total_load_running();
99 double running_ratio = 100.0 * total_running / total_init;
100 if (running_ratio < -opt::load_ratio_threshold)
101 XBT_CRITICAL("Negative running load at exit! %g (%g%%) <============",
102 total_running, running_ratio);
103 else if (running_ratio > opt::load_ratio_threshold)
104 XBT_CRITICAL("Remaining running load at exit! %g (%g%%) <============",
105 total_running, running_ratio);
107 XBT_VERB("Running load at exit looks good: %g (%g%%)",
108 total_running, running_ratio);
111 #define PR_STATS(descr, st) \
112 XBT_INFO("| %.*s: %g / %g / %g", 39, \
113 descr " total/avg./stddev. at exit.........................", \
114 st.get_sum(), st.get_mean(), st.get_stddev())
116 int main(int argc, char* argv[])
118 // Note: variables used after THROW must be declared as volatile.
119 volatile int exit_status = 0; // global exit status
120 volatile double simulated_time = -1.0;
121 timestamp simulation_time;
125 simulation_time.start();
127 // Set default logging parameters
128 bool do_log_control_set = true;
129 for (int i = 1 ; do_log_control_set && i < argc ; i++)
130 do_log_control_set = !(argv[i][0] == '-' && argv[i][1] != '-' &&
131 strchr(argv[i] + 1, 'v'));
132 if (do_log_control_set) {
133 // xbt_log_control_set("simu.thres:verbose");
134 xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
135 xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
138 // Initialize some MSG internal data.
139 // Note: MSG_global_init() may throw an exception, but it seems
140 // impossible to catch it correctly :-(
141 MSG_global_init(&argc, argv);
143 // Parse global parameters
144 bool parse_res = opt::parse_args(&argc, argv);
146 || opt::version_requested || opt::help_requested) {
147 if (opt::version_requested)
148 std::clog << version::name << " (" << opt::program_name << ")"
149 << " version " << version::num << "\n"
150 << version::copyright << "\n"
151 "Compiled on " << version::date << "\n\n";
152 if (!parse_res || opt::help_requested)
155 exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
157 XBT_INFO("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(),
158 version::date.c_str());
162 exit_status = EXIT_FAILURE_INIT; // =====
164 // Register the default function of an agent
165 // MSG_function_register("simulation_main", simulation_main);
166 MSG_function_register_default(simulation_main);
168 // Create the platform and the application.
169 MSG_create_environment(opt::platform_file.c_str());
171 if (opt::auto_depl::enabled) {
172 if (!opt::auto_depl::nhosts)
173 opt::auto_depl::nhosts = hostdata::size();
174 if (opt::auto_depl::nhosts > hostdata::size()) {
175 XBT_WARN("%u hosts is too much: limiting to %u",
176 opt::auto_depl::nhosts, (unsigned )hostdata::size());
177 opt::auto_depl::nhosts = hostdata::size();
179 if (!opt::auto_depl::load)
180 opt::auto_depl::load = opt::auto_depl::nhosts;
181 MY_launch_application(); // it is already opt::* aware...
183 MSG_launch_application(opt::deployment_file.c_str());
186 // Register tracing categories
187 TRACE_category(TRACE_CAT_COMP);
188 TRACE_category(TRACE_CAT_CTRL);
189 TRACE_category(TRACE_CAT_DATA);
191 exit_status = EXIT_FAILURE_SIMU; // =====
193 proc_mutex = xbt_mutex_init();
194 proc_cond = xbt_cond_init();
197 // Launch the MSG simulation.
198 XBT_INFO("Starting simulation at %f...", MSG_get_clock());
200 simulated_time = MSG_get_clock();
201 XBT_INFO("Simulation ended at %f.", simulated_time);
203 xbt_cond_destroy(proc_cond);
204 xbt_mutex_destroy(proc_mutex);
207 THROW1(0, 0, "MSG_main() failed with status %#x", res);
209 exit_status = EXIT_NO_FAILURE; // =====
212 int len = strlen(ex.msg);
213 if (len > 0 && ex.msg[len - 1] == '\n')
214 ex.msg[len - 1] = '\0'; // strip the ending '\n'
215 XBT_ERROR("%s", ex.msg);
216 XBT_DEBUG("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
220 // Clean the MSG simulation.
224 XBT_ERROR("MSG_clean() failed with status %#x", res);
225 exit_status |= EXIT_FAILURE_CLEAN;
228 // Report final simulation status.
229 if (simulated_time >= 0.0) {
230 simulation_time.stop();
231 check_for_lost_load();
232 XBT_INFO(",----[ Results ]");
233 PR_STATS("Load", loads);
234 PR_STATS("Computation", comps);
235 XBT_INFO("| Total simulated time...................: %g",
237 XBT_INFO("| Total simulation time..................: %g",
238 simulation_time.duration());
242 XBT_ERROR("Simulation failed (%#x).", exit_status);
244 XBT_INFO("Simulation succeeded.");