]> AND Private Git Repository - loba.git/blob - main.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
5d43190e14e4cc9512e8355a7304e4ee6058c891
[loba.git] / main.cpp
1 #include <cstring>
2 #include <iostream>
3 #include <stdexcept>
4 #include <msg/msg.h>
5 #include <xbt/log.h>
6 #include "deployment.h"
7 #include "hostdata.h"
8 #include "misc.h"
9 #include "options.h"
10 #include "process.h"
11 #include "timer.h"
12 #include "version.h"
13
14 // Creates log categories
15 XBT_LOG_NEW_CATEGORY(simu, "Root of simulation messages");
16 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
17 XBT_LOG_NEW_SUBCATEGORY(depl, main, "Messages from auto deployment");
18 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
19 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
20 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
21
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
23
24 // Failure exit status
25 enum {
26     EXIT_NO_FAILURE    = 0x00,  // no error
27     EXIT_FAILURE_ARGS  = 0x01,  // bad arguments
28     EXIT_FAILURE_INIT  = 0x02,  // failed to initialize simulator
29     EXIT_FAILURE_SIMU  = 0x04,  // simulation failed
30     EXIT_FAILURE_CLEAN = 0x08,  // error at cleanup
31 };
32
33 int simulation_main(int argc, char* argv[])
34 {
35     int result;
36     process* proc;
37     try {
38         proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv);
39         result = proc->run();
40         delete proc;
41     }
42     catch (std::invalid_argument& e) {
43         THROW1(arg_error, 0, "%s", e.what());
44     }
45     return result;
46 }
47
48 void check_for_lost_load()
49 {
50     const double threshold = 1e-4;
51     double total_init = process::get_total_load_init();
52     double total_exit = process::get_total_load_exit();
53     double lost = total_init - total_exit;
54     double lost_ratio = 100 * lost / total_init;
55     if (lost_ratio < -threshold) {
56         CRITICAL2("Gained load at exit! %g (%g%%) <============",
57                   lost, lost_ratio);
58     } else if (lost_ratio > threshold) {
59         CRITICAL2("Lost load at exit! %g (%g%%) <============",
60                   lost, lost_ratio);
61     } else {
62         DEBUG2("Total load at exit looks good: %g (%g%%)", lost, lost_ratio);
63     }
64
65 }
66
67 int main(int argc, char* argv[])
68 {
69     // Note: variables used after THROW must be declared as volatile.
70     volatile int exit_status = 0;   // global exit status
71     volatile double simulated_time = -1.0;
72     timestamp simulation_time;
73     xbt_ex_t ex;
74     MSG_error_t res;
75
76     simulation_time.start();
77
78     // Set default logging parameters
79     // xbt_log_control_set("simu.thres:verbose");
80     xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
81     xbt_log_control_set("main.fmt:'[%c/%p] %m%n'");
82
83     // Initialize some MSG internal data.
84     // Note: MSG_global_init() may throw an exception, but it seems
85     // impossible to catch it correctly :-(
86     MSG_global_init(&argc, argv);
87
88     // Parse global parameters
89     int parse_res = opt::parse_args(&argc, argv);
90     if (!parse_res
91         || opt::version_requested || opt::help_requested) {
92         if (opt::version_requested)
93             std::clog << version::name << " (" << opt::program_name << ")"
94                       << " version " << version::num << "\n"
95                       << version::copyright << "\n"
96                 "Compiled on " << version::date << "\n\n";
97         if (!parse_res || opt::help_requested)
98             opt::usage();
99         MSG_clean();
100         exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
101     }
102     INFO3("%s v%s (%s)", opt::program_name.c_str(), version::num.c_str(), version::date.c_str());
103     opt::print();
104
105     TRY {
106         exit_status = EXIT_FAILURE_INIT; // =====
107
108         // Register the default function of an agent
109         // MSG_function_register("simulation_main", simulation_main);
110         MSG_function_register_default(simulation_main);
111
112         // Create the platform and the application.
113         MSG_create_environment(opt::platform_file.c_str());
114         hostdata::create();
115         if (opt::auto_depl::enabled) {
116             if (!opt::auto_depl::nhosts)
117                 opt::auto_depl::nhosts = hostdata::size();
118             if (opt::auto_depl::nhosts > hostdata::size()) {
119                 WARN2("%u hosts is too much: limiting to %u",
120                       opt::auto_depl::nhosts, (unsigned )hostdata::size());
121                 opt::auto_depl::nhosts = hostdata::size();
122             }
123             if (!opt::auto_depl::load)
124                 opt::auto_depl::load = opt::auto_depl::nhosts;
125             MY_launch_application(); // it is already opt::* aware...
126         } else {
127             MSG_launch_application(opt::deployment_file.c_str());
128         }
129
130         exit_status = EXIT_FAILURE_SIMU; // =====
131
132         // Launch the MSG simulation.
133         INFO1("Starting simulation at %f...", MSG_get_clock());
134         res = MSG_main();
135         simulated_time = MSG_get_clock();
136         INFO1("Simulation ended at %f.", simulated_time);
137         check_for_lost_load();
138         if (res != MSG_OK)
139             THROW1(0, 0, "MSG_main() failed with status %#x", res);
140
141         exit_status = EXIT_NO_FAILURE; // =====
142     }
143     CATCH (ex) {
144         int len = strlen(ex.msg);
145         if (len > 0 && ex.msg[len - 1] == '\n')
146             ex.msg[len - 1] = '\0'; // strip the ending '\n'
147         ERROR1("%s", ex.msg);
148         DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
149         xbt_ex_free(ex);
150     }
151
152     // Clean the MSG simulation.
153     hostdata::destroy();
154     res = MSG_clean();
155     if (res != MSG_OK) {
156         ERROR1("MSG_clean() failed with status %#x", res);
157         exit_status |= EXIT_FAILURE_CLEAN;
158     }
159
160     // Report final simulation status.
161     if (simulated_time >= 0.0) {
162         simulation_time.stop();
163         INFO0(",----[ Results ]");
164         INFO1("| Total simulated time...: %g", simulated_time);
165         INFO1("| Total simulation time..: %g", simulation_time.duration());
166         INFO0("`----");
167     }
168     if (exit_status)
169         ERROR1("Simulation failed (%#x).", exit_status);
170     else
171         INFO0("Simulation succeeded.");
172
173     return exit_status;
174 }
175
176 // Local variables:
177 // mode: c++
178 // End: