Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : new example bugged1 for stateful dpor
[simgrid.git] / src / msg / global.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "msg/private.h"
8 #include "mc/mc.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/virtu.h"
12 #include "xbt/ex.h"             /* ex_backtrace_display */
13 #include "mailbox.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
16                                 "Logging specific to MSG (kernel)");
17
18 MSG_Global_t msg_global = NULL;
19
20
21 /** \defgroup msg_simulation   MSG simulation Functions
22  *  \brief This section describes the functions you need to know to
23  *  set up a simulation. You should have a look at \ref MSG_examples
24  *  to have an overview of their usage.
25  */
26 /** @addtogroup msg_simulation
27  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
28  */
29
30 /********************************* MSG **************************************/
31
32 /** \ingroup msg_simulation
33  * \brief Initialize some MSG internal data.
34  */
35 void MSG_global_init_args(int *argc, char **argv)
36 {
37   MSG_global_init(argc, argv);
38 }
39
40
41 XBT_LOG_EXTERNAL_CATEGORY(msg_gos);
42 XBT_LOG_EXTERNAL_CATEGORY(msg_kernel);
43 XBT_LOG_EXTERNAL_CATEGORY(msg_mailbox);
44 XBT_LOG_EXTERNAL_CATEGORY(msg_process);
45
46 /** \ingroup msg_simulation
47  * \brief Initialize some MSG internal data.
48  */
49 void MSG_global_init(int *argc, char **argv)
50 {
51 #ifdef HAVE_TRACING
52   TRACE_global_init(argc, argv);
53 #endif
54
55   xbt_getpid = MSG_process_self_PID;
56   if (!msg_global) {
57     /* Connect our log channels: that must be done manually under windows */
58     XBT_LOG_CONNECT(msg_gos, msg);
59     XBT_LOG_CONNECT(msg_kernel, msg);
60     XBT_LOG_CONNECT(msg_mailbox, msg);
61     XBT_LOG_CONNECT(msg_process, msg);
62
63     SIMIX_global_init(argc, argv);
64
65     msg_global = xbt_new0(s_MSG_Global_t, 1);
66
67     msg_global->max_channel = 0;
68     msg_global->PID = 1;
69     msg_global->sent_msg = 0;
70
71     /* initialization of the action module */
72     _MSG_action_init();
73
74     SIMIX_function_register_process_create(MSG_process_create_from_SIMIX);
75     SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX);
76     SIMIX_function_register_process_kill(MSG_process_kill_from_SIMIX);
77   }
78 #ifdef HAVE_TRACING
79   TRACE_start();
80 #endif
81
82   XBT_DEBUG("ADD MSG LEVELS");
83   MSG_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __MSG_host_destroy);
84 }
85
86 /** \defgroup m_channel_management    Understanding channels
87  *  \brief This section briefly describes the channel notion of MSG
88  *  (#m_channel_t).
89  */
90 /** @addtogroup m_channel_management
91  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
92  *
93  *
94  *  For convenience, the simulator provides the notion of channel
95  *  that is close to the tag notion in MPI. A channel is not a
96  *  socket. It doesn't need to be opened neither closed. It rather
97  *  corresponds to the ports opened on the different machines.
98  */
99
100
101 /** \ingroup m_channel_management
102  * \brief Set the number of channel in the simulation.
103  *
104  * This function has to be called to fix the number of channel in the
105    simulation before creating any host. Indeed, each channel is
106    represented by a different mailbox on each #m_host_t. This
107    function can then be called only once. This function takes only one
108    parameter.
109  * \param number the number of channel in the simulation. It has to be >0
110  */
111 MSG_error_t MSG_set_channel_number(int number)
112 {
113   xbt_assert((msg_global)
114               && (msg_global->max_channel == 0),
115               "Channel number already set!");
116
117   msg_global->max_channel = number;
118
119   return MSG_OK;
120 }
121
122 /** \ingroup m_channel_management
123  * \brief Return the number of channel in the simulation.
124  *
125  * This function has to be called once the number of channel is fixed. I can't
126    figure out a reason why anyone would like to call this function but nevermind.
127  * \return the number of channel in the simulation.
128  */
129 int MSG_get_channel_number(void)
130 {
131   xbt_assert((msg_global)
132               && (msg_global->max_channel != 0),
133               "Channel number not set yet!");
134
135   return msg_global->max_channel;
136 }
137
138 /** \ingroup msg_simulation
139  * \brief Launch the MSG simulation
140  */
141 MSG_error_t MSG_main(void)
142 {
143   /* Clean IO before the run */
144   fflush(stdout);
145   fflush(stderr);
146
147   if (MC_IS_ENABLED) {
148     MC_modelcheck();
149   }
150   else {
151     SIMIX_run();
152   }
153   return MSG_OK;
154 }
155
156 MSG_error_t MSG_main_stateful(void)
157 {
158   /* Clean IO before the run */
159   fflush(stdout);
160   fflush(stderr);
161
162   if (MC_IS_ENABLED) {
163     MC_modelcheck_stateful();
164   }
165   else {
166     SIMIX_run();
167   }
168   return MSG_OK;
169 }
170
171 MSG_error_t MSG_main_with_automaton(xbt_automaton_t a)
172 {
173   /* Clean IO before the run */
174   fflush(stdout);
175   fflush(stderr);
176
177   if (MC_IS_ENABLED) {
178     MC_modelcheck_with_automaton(a);
179   }
180   else {
181     SIMIX_run();
182   }
183   return MSG_OK;
184 }
185
186 /** \ingroup msg_simulation
187  * \brief Kill all running process
188
189  * \param reset_PIDs should we reset the PID numbers. A negative
190  *   number means no reset and a positive number will be used to set the PID
191  *   of the next newly created process.
192  */
193 int MSG_process_killall(int reset_PIDs)
194 {
195   SIMIX_req_process_killall();
196
197   if (reset_PIDs > 0) {
198     msg_global->PID = reset_PIDs;
199     msg_global->session++;
200   }
201
202   return msg_global->PID;
203
204 }
205
206 /** \ingroup msg_simulation
207  * \brief Clean the MSG simulation
208  */
209 MSG_error_t MSG_clean(void)
210 {
211
212 #ifdef HAVE_TRACING
213   TRACE_surf_release();
214 #endif
215
216   MSG_process_killall(0);
217
218   /* initialization of the action module */
219   _MSG_action_exit();
220
221 #ifdef HAVE_TRACING
222   TRACE_end();
223 #endif
224
225   SIMIX_clean();
226
227   free(msg_global);
228   msg_global = NULL;
229
230   return MSG_OK;
231 }
232
233
234 /** \ingroup msg_easier_life
235  * \brief A clock (in second).
236  */
237 XBT_INLINE double MSG_get_clock(void)
238 {
239   return SIMIX_get_clock();
240 }
241
242 unsigned long int MSG_get_sent_msg()
243 {
244   return msg_global->sent_msg;
245 }