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

Private GIT Repository
Wip++...
[loba.git] / main.cpp
1 #include <cstring>
2 #include <iostream>
3 #include <msg/msg.h>
4 #include <xbt/log.h>
5 #include "hostdata.h"
6 #include "misc.h"
7 #include "options.h"
8 #include "process.h"
9 #include "timer.h"
10 #include "version.h"
11
12 // Creates log categories
13 XBT_LOG_NEW_CATEGORY(simu, "Simulation messages");
14 XBT_LOG_NEW_SUBCATEGORY(main, simu, "Messages from global infrastructure");
15 XBT_LOG_NEW_SUBCATEGORY(comm, simu, "Messages from asynchronous pipes");
16 XBT_LOG_NEW_SUBCATEGORY(proc, simu, "Messages from base process class");
17 XBT_LOG_NEW_SUBCATEGORY(loba, simu, "Messages from load-balancer");
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
20
21 // Failure exit status
22 enum {
23     EXIT_NO_FAILURE    = 0x00,  // no error
24     EXIT_FAILURE_ARGS  = 0x01,  // bad arguments
25     EXIT_FAILURE_INIT  = 0x02,  // failed to initialize simulator
26     EXIT_FAILURE_SIMU  = 0x04,  // simulation failed
27     EXIT_FAILURE_CLEAN = 0x08,  // error at cleanup
28 };
29
30 #include "loba_least_loaded.h"
31 int simulation_main(int argc, char* argv[])
32 {
33     //    process proc(argc, argv);
34     loba_least_loaded proc(argc, argv);
35     return proc.run();
36 }
37
38 int main(int argc, char* argv[])
39 {
40     // Note: variables used after THROW must be declared as volatile.
41     volatile int exit_status = 0;   // global exit status
42     volatile double simulated_time = -1.0;
43     timestamp simulation_time;
44     xbt_ex_t ex;
45     MSG_error_t res;
46
47     simulation_time.start();
48
49     // Set default logging parameters
50     // xbt_log_control_set("simu.thres:verbose");
51     xbt_log_control_set("simu.fmt:'[%h %r] [%c/%p] %m%n'");
52
53     // Initialize some MSG internal data.
54     // Note: MSG_global_init() may throw an exception, but it seems
55     // impossible to catch it correctly :-(
56     MSG_global_init(&argc, argv);
57
58     // Parse global parameters
59     int parse_res = opt::parse_args(&argc, argv);
60     if (!parse_res
61         || opt::version_requested || opt::help_requested) {
62         if (opt::version_requested)
63             std::clog << version::name << " version " << version::num << "\n"
64                       << version::copyright << "\n"
65                 "Compiled on " << version::date << "\n\n";
66         if (!parse_res || opt::help_requested)
67             opt::usage();
68         MSG_clean();
69         exit(parse_res ? EXIT_NO_FAILURE : EXIT_FAILURE_ARGS);
70     }
71     opt::print();
72
73     TRY {
74         exit_status = EXIT_FAILURE_INIT; // =====
75
76         // Register the main function of an agent in a global table.
77         MSG_function_register("simulation_main", simulation_main);
78         // Preserve some compatibility with old code...
79         MSG_function_register("Calculs", simulation_main);
80
81         // Create the platform and the application.
82         MSG_create_environment(opt::platform_file);
83         hostdata::create();
84         MSG_launch_application(opt::application_file);
85
86         exit_status = EXIT_FAILURE_SIMU; // =====
87
88         // Launch the MSG simulation.
89         INFO0("Starting simulation...");
90         res = MSG_main();
91         INFO0("Simulation ended.");
92         simulated_time = MSG_get_clock();
93         if (res != MSG_OK)
94             THROW1(0, 0, "MSG_main() failed with status %#x", res);
95
96         exit_status = EXIT_NO_FAILURE; // =====
97     }
98     CATCH (ex) {
99         int len = strlen(ex.msg);
100         if (len > 0 && ex.msg[len - 1] == '\n')
101             ex.msg[len - 1] = '\0'; // strip the ending '\n'
102         ERROR1("%s", ex.msg);
103         DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
104         xbt_ex_free(ex);
105     }
106
107     // Clean the MSG simulation.
108     hostdata::destroy();
109     res = MSG_clean();
110     if (res != MSG_OK) {
111         ERROR1("MSG_clean() failed with status %#x", res);
112         exit_status |= EXIT_FAILURE_CLEAN;
113     }
114
115     // Report final simulation status.
116     if (simulated_time >= 0.0) {
117         simulation_time.stop();
118         INFO0(",----[ Results ]");
119         INFO1("| Total simulated time...: %g", simulated_time);
120         INFO1("| Total simulation time..: %g", simulation_time.duration());
121         INFO0("`----");
122     }
123     if (exit_status)
124         ERROR1("Simulation failed (%#x).", exit_status);
125     else
126         INFO0("Simulation succeeded.");
127
128     return exit_status;
129 }
130
131 // Local variables:
132 // mode: c++
133 // End: