Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / msg / 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 "msg_mailbox.h"
9 #include "mc/mc.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/virtu.h"
13 #include "xbt/ex.h"             /* ex_backtrace_display */
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 #ifdef MSG_USE_DEPRECATED
68     msg_global->max_channel = 0;
69 #endif
70     msg_global->PID = 1;
71     msg_global->sent_msg = 0;
72     msg_global->task_copy_callback = NULL;
73     msg_global->process_data_cleanup = NULL;
74
75     /* initialization of the action module */
76     _MSG_action_init();
77
78     SIMIX_function_register_process_create(MSG_process_create_from_SIMIX);
79     SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX);
80     SIMIX_function_register_process_kill(MSG_process_kill_from_SIMIX);
81   }
82 #ifdef HAVE_TRACING
83   TRACE_start();
84 #endif
85
86   XBT_DEBUG("ADD MSG LEVELS");
87   MSG_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __MSG_host_destroy);
88 }
89
90 #ifdef MSG_USE_DEPRECATED
91 /** \defgroup m_channel_management    Understanding channels
92  *  \brief This section briefly describes the channel notion of MSG
93  *  (#m_channel_t).
94  */
95 /** @addtogroup m_channel_management
96  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
97  *
98  *
99  *  For convenience, the simulator provides the notion of channel
100  *  that is close to the tag notion in MPI. A channel is not a
101  *  socket. It doesn't need to be opened neither closed. It rather
102  *  corresponds to the ports opened on the different machines.
103  */
104
105
106 /** \ingroup m_channel_management
107  * \brief Set the number of channel in the simulation.
108  *
109  * This function has to be called to fix the number of channel in the
110    simulation before creating any host. Indeed, each channel is
111    represented by a different mailbox on each #m_host_t. This
112    function can then be called only once. This function takes only one
113    parameter.
114  * \param number the number of channel in the simulation. It has to be >0
115  */
116 MSG_error_t MSG_set_channel_number(int number)
117 {
118   XBT_WARN("DEPRECATED! Now use alias");
119   xbt_assert((msg_global)
120               && (msg_global->max_channel == 0),
121               "Channel number already set!");
122
123   msg_global->max_channel = number;
124
125   return MSG_OK;
126 }
127
128 /** \ingroup m_channel_management
129  * \brief Return the number of channel in the simulation.
130  *
131  * This function has to be called once the number of channel is fixed. I can't
132    figure out a reason why anyone would like to call this function but nevermind.
133  * \return the number of channel in the simulation.
134  */
135 int MSG_get_channel_number(void)
136 {
137   XBT_WARN("DEPRECATED! Now use alias");
138   xbt_assert((msg_global)
139               && (msg_global->max_channel != 0),
140               "Channel number not set yet!");
141
142   return msg_global->max_channel;
143 }
144 #endif
145
146 /** \ingroup msg_simulation
147  * \brief Launch the MSG simulation
148  */
149 MSG_error_t MSG_main(void)
150 {
151   /* Clean IO before the run */
152   fflush(stdout);
153   fflush(stderr);
154
155   if (MC_IS_ENABLED) {
156     MC_modelcheck();
157   }
158   else {
159     SIMIX_run();
160   }
161   return MSG_OK;
162 }
163
164 MSG_error_t MSG_main_stateful(void)
165 {
166   /* Clean IO before the run */
167   fflush(stdout);
168   fflush(stderr);
169
170   if (MC_IS_ENABLED) {
171     MC_modelcheck_stateful();
172   }
173   else {
174     SIMIX_run();
175   }
176   return MSG_OK;
177 }
178
179
180 MSG_error_t MSG_main_liveness(xbt_automaton_t a, char *prgm)
181 {
182   /* Clean IO before the run */
183   fflush(stdout);
184   fflush(stderr);
185
186   if (MC_IS_ENABLED) {
187     MC_modelcheck_liveness(a, prgm);
188   }
189   else {
190     SIMIX_run();
191   }
192   return MSG_OK;
193 }
194
195 /** \ingroup msg_simulation
196  * \brief Kill all running process
197
198  * \param reset_PIDs should we reset the PID numbers. A negative
199  *   number means no reset and a positive number will be used to set the PID
200  *   of the next newly created process.
201  */
202 int MSG_process_killall(int reset_PIDs)
203 {
204   simcall_process_killall();
205
206   if (reset_PIDs > 0) {
207     msg_global->PID = reset_PIDs;
208     msg_global->session++;
209   }
210
211   return msg_global->PID;
212
213 }
214
215 /** \ingroup msg_simulation
216  * \brief Clean the MSG simulation
217  */
218 MSG_error_t MSG_clean(void)
219 {
220   XBT_DEBUG("Closing MSG");
221
222 #ifdef HAVE_TRACING
223   TRACE_surf_release();
224 #endif
225
226   MSG_process_killall(0);
227
228   /* initialization of the action module */
229   _MSG_action_exit();
230
231 #ifdef HAVE_TRACING
232   TRACE_end();
233 #endif
234
235   SIMIX_clean();
236
237   free(msg_global);
238   msg_global = NULL;
239
240   return MSG_OK;
241 }
242
243
244 /** \ingroup msg_easier_life
245  * \brief A clock (in second).
246  */
247 XBT_INLINE double MSG_get_clock(void)
248 {
249   return SIMIX_get_clock();
250 }
251
252 unsigned long int MSG_get_sent_msg()
253 {
254   return msg_global->sent_msg;
255 }