Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3.5 figlet
[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->host = xbt_fifo_new();
68     msg_global->process_list = xbt_fifo_new();
69     msg_global->max_channel = 0;
70     msg_global->PID = 1;
71     msg_global->sent_msg = 0;
72
73     /* initialization of the mailbox module */
74     MSG_mailbox_mod_init();
75
76     /* initialization of the action module */
77     _MSG_action_init();
78
79     SIMIX_function_register_process_create(_MSG_process_create_from_SIMIX);
80     SIMIX_function_register_process_cleanup(__MSG_process_cleanup);
81     SIMIX_function_register_process_kill(_MSG_process_kill_from_SIMIX);
82   }
83 #ifdef HAVE_TRACING
84   TRACE_start();
85 #endif
86 }
87
88 /** \defgroup m_channel_management    Understanding channels
89  *  \brief This section briefly describes the channel notion of MSG
90  *  (#m_channel_t).
91  */
92 /** @addtogroup m_channel_management
93  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
94  *
95  *
96  *  For convenience, the simulator provides the notion of channel
97  *  that is close to the tag notion in MPI. A channel is not a
98  *  socket. It doesn't need to be opened neither closed. It rather
99  *  corresponds to the ports opened on the different machines.
100  */
101
102
103 /** \ingroup m_channel_management
104  * \brief Set the number of channel in the simulation.
105  *
106  * This function has to be called to fix the number of channel in the
107    simulation before creating any host. Indeed, each channel is
108    represented by a different mailbox on each #m_host_t. This
109    function can then be called only once. This function takes only one
110    parameter.
111  * \param number the number of channel in the simulation. It has to be >0
112  */
113 MSG_error_t MSG_set_channel_number(int number)
114 {
115   xbt_assert0((msg_global)
116               && (msg_global->max_channel == 0),
117               "Channel number already set!");
118
119   msg_global->max_channel = number;
120
121   return MSG_OK;
122 }
123
124 /** \ingroup m_channel_management
125  * \brief Return the number of channel in the simulation.
126  *
127  * This function has to be called once the number of channel is fixed. I can't
128    figure out a reason why anyone would like to call this function but nevermind.
129  * \return the number of channel in the simulation.
130  */
131 int MSG_get_channel_number(void)
132 {
133   xbt_assert0((msg_global)
134               && (msg_global->max_channel != 0),
135               "Channel number not set yet!");
136
137   return msg_global->max_channel;
138 }
139
140 /** \ingroup msg_simulation
141  * \brief Launch the MSG simulation
142  */
143 MSG_error_t MSG_main(void)
144 {
145   /* Clean IO before the run */
146   fflush(stdout);
147   fflush(stderr);
148   SIMIX_init();
149
150 #ifdef HAVE_MC
151   if (_surf_do_model_check)
152     MC_modelcheck(1);
153   else
154 #endif
155     while (SIMIX_solve(NULL, NULL) != -1.0);
156
157   return MSG_OK;
158 }
159
160 /** \ingroup msg_simulation
161  * \brief Kill all running process
162
163  * \param reset_PIDs should we reset the PID numbers. A negative
164  *   number means no reset and a positive number will be used to set the PID
165  *   of the next newly created process.
166  */
167 int MSG_process_killall(int reset_PIDs)
168 {
169   m_process_t p = NULL;
170   m_process_t self = MSG_process_self();
171
172   while ((p = xbt_fifo_pop(msg_global->process_list))) {
173     if (p != self)
174       MSG_process_kill(p);
175   }
176
177   if (reset_PIDs > 0) {
178     msg_global->PID = reset_PIDs;
179     msg_global->session++;
180   }
181
182   return msg_global->PID;
183
184 }
185
186 /** \ingroup msg_simulation
187  * \brief Clean the MSG simulation
188  */
189 MSG_error_t MSG_clean(void)
190 {
191   xbt_fifo_item_t i = NULL;
192   m_host_t h = NULL;
193   m_process_t p = NULL;
194
195 #ifdef HAVE_TRACING
196   TRACE_surf_release();
197 #endif
198
199   while ((p = xbt_fifo_pop(msg_global->process_list))) {
200     MSG_process_kill(p);
201   }
202
203   xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
204     __MSG_host_destroy(h);
205   }
206   xbt_fifo_free(msg_global->host);
207   xbt_fifo_free(msg_global->process_list);
208
209   free(msg_global);
210   msg_global = NULL;
211
212   /* cleanup all resources in the mailbox module */
213   MSG_mailbox_mod_exit();
214
215   /* initialization of the action module */
216   _MSG_action_exit();
217
218   SIMIX_clean();
219
220 #ifdef HAVE_TRACING
221   TRACE_end();
222 #endif
223
224   return MSG_OK;
225 }
226
227
228 /** \ingroup msg_easier_life
229  * \brief A clock (in second).
230  */
231 double MSG_get_clock(void)
232 {
233   return SIMIX_get_clock();
234 }
235
236 unsigned long int MSG_get_sent_msg()
237 {
238   return msg_global->sent_msg;
239 }