Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3870712c14df8fc006452d9d810a5a4ce7dffe5c
[simgrid.git] / src / msg / global.c
1 /*     $Id$      */
2
3 /* Copyright (c) 2002-2007 Arnaud Legrand.                                  */
4 /* Copyright (c) 2007 Bruno Donassolo.                                      */
5 /* All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/virtu.h"
14 #include "xbt/ex.h"             /* ex_backtrace_display */
15 #include "mailbox.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
18                                 "Logging specific to MSG (kernel)");
19
20 MSG_Global_t msg_global = NULL;
21
22
23 /** \defgroup msg_simulation   MSG simulation Functions
24  *  \brief This section describes the functions you need to know to
25  *  set up a simulation. You should have a look at \ref MSG_examples
26  *  to have an overview of their usage.
27  */
28 /** @addtogroup msg_simulation
29  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
30  */
31
32 /********************************* MSG **************************************/
33
34 /** \ingroup msg_simulation
35  * \brief Initialize some MSG internal data.
36  */
37 void MSG_global_init_args(int *argc, char **argv)
38 {
39   MSG_global_init(argc, argv);
40 }
41
42
43 XBT_LOG_EXTERNAL_CATEGORY(msg_gos);
44 XBT_LOG_EXTERNAL_CATEGORY(msg_kernel);
45 XBT_LOG_EXTERNAL_CATEGORY(msg_mailbox);
46 XBT_LOG_EXTERNAL_CATEGORY(msg_process);
47
48 /** \ingroup msg_simulation
49  * \brief Initialize some MSG internal data.
50  */
51 void MSG_global_init(int *argc, char **argv)
52 {
53   xbt_getpid = MSG_process_self_PID;
54   if (!msg_global) {
55     /* Connect our log channels: that must be done manually under windows */
56     XBT_LOG_CONNECT(msg_gos, msg);
57     XBT_LOG_CONNECT(msg_kernel, msg);
58     XBT_LOG_CONNECT(msg_mailbox, msg);
59     XBT_LOG_CONNECT(msg_process, msg);
60
61     SIMIX_global_init(argc, argv);
62
63     msg_global = xbt_new0(s_MSG_Global_t, 1);
64
65     msg_global->host = xbt_fifo_new();
66     msg_global->process_list = xbt_fifo_new();
67     msg_global->max_channel = 0;
68     msg_global->PID = 1;
69     msg_global->sent_msg = 0;
70
71     /* initialization of the mailbox module */
72     MSG_mailbox_mod_init();
73
74     /* initialization of the action module */
75     _MSG_action_init();
76
77     SIMIX_function_register_process_create(_MSG_process_create_from_SIMIX);
78     SIMIX_function_register_process_cleanup(__MSG_process_cleanup);
79     SIMIX_function_register_process_kill(_MSG_process_kill_from_SIMIX);
80   }
81   return;
82 }
83
84 /** \ingroup msg_easier_life
85  * \brief Traces MSG events in the Paje format.
86  */
87
88 void MSG_paje_output(const char *filename)
89 {
90 }
91
92 /** \defgroup m_channel_management    Understanding channels
93  *  \brief This section briefly describes the channel notion of MSG
94  *  (#m_channel_t).
95  */
96 /** @addtogroup m_channel_management
97  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
98  *
99  *
100  *  For convenience, the simulator provides the notion of channel
101  *  that is close to the tag notion in MPI. A channel is not a
102  *  socket. It doesn't need to be opened neither closed. It rather
103  *  corresponds to the ports opened on the different machines.
104  */
105
106
107 /** \ingroup m_channel_management
108  * \brief Set the number of channel in the simulation.
109  *
110  * This function has to be called to fix the number of channel in the
111    simulation before creating any host. Indeed, each channel is
112    represented by a different mailbox on each #m_host_t. This
113    function can then be called only once. This function takes only one
114    parameter.
115  * \param number the number of channel in the simulation. It has to be >0
116  */
117 MSG_error_t MSG_set_channel_number(int number)
118 {
119   xbt_assert0((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_assert0((msg_global)
138               && (msg_global->max_channel != 0),
139               "Channel number not set yet!");
140
141   return msg_global->max_channel;
142 }
143
144 /** \ingroup msg_simulation
145  * \brief Launch the MSG simulation
146  */
147 MSG_error_t MSG_main(void)
148 {
149   smx_action_t smx_action;
150   xbt_fifo_t actions_done = xbt_fifo_new();
151   xbt_fifo_t actions_failed = xbt_fifo_new();
152
153   /* Clean IO before the run */
154   fflush(stdout);
155   fflush(stderr);
156   SIMIX_init();
157
158   //surf_solve(); /* Takes traces into account. Returns 0.0 */
159   /* xbt_fifo_size(msg_global->process_to_run) */
160
161   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
162
163     while ((smx_action = xbt_fifo_pop(actions_failed))) {
164       DEBUG1("** %s failed **", SIMIX_action_get_name(smx_action));
165       SIMIX_action_signal_all(smx_action);
166     }
167
168     while ((smx_action = xbt_fifo_pop(actions_done))) {
169       DEBUG1("** %s done **", SIMIX_action_get_name(smx_action));
170       SIMIX_action_signal_all(smx_action);      
171     }
172   }
173   xbt_fifo_free(actions_failed);
174   xbt_fifo_free(actions_done);
175   return MSG_OK;
176 }
177
178 /** \ingroup msg_simulation
179  * \brief Kill all running process
180
181  * \param reset_PIDs should we reset the PID numbers. A negative
182  *   number means no reset and a positive number will be used to set the PID
183  *   of the next newly created process.
184  */
185 int MSG_process_killall(int reset_PIDs)
186 {
187   m_process_t p = NULL;
188   m_process_t self = MSG_process_self();
189
190   while ((p = xbt_fifo_pop(msg_global->process_list))) {
191     if (p != self)
192       MSG_process_kill(p);
193   }
194
195   if (reset_PIDs > 0) {
196     msg_global->PID = reset_PIDs;
197     msg_global->session++;
198   }
199
200   return msg_global->PID;
201
202 }
203
204 /** \ingroup msg_simulation
205  * \brief Clean the MSG simulation
206  */
207 MSG_error_t MSG_clean(void)
208 {
209   xbt_fifo_item_t i = NULL;
210   m_host_t h = NULL;
211   m_process_t p = NULL;
212
213
214   while ((p = xbt_fifo_pop(msg_global->process_list))) {
215     MSG_process_kill(p);
216   }
217
218   xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
219     __MSG_host_destroy(h);
220   }
221   xbt_fifo_free(msg_global->host);
222   xbt_fifo_free(msg_global->process_list);
223
224   free(msg_global);
225   msg_global = NULL;
226
227   /* cleanup all resources in the mailbox module */
228   MSG_mailbox_mod_exit();
229
230   SIMIX_clean();
231
232   return MSG_OK;
233 }
234
235
236 /** \ingroup msg_easier_life
237  * \brief A clock (in second).
238  */
239 double MSG_get_clock(void)
240 {
241   return SIMIX_get_clock();
242 }
243
244 unsigned long int MSG_get_sent_msg()
245 {
246   return msg_global->sent_msg;
247 }