Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The windows implementation of simgrid use now the portable snprintf so, enable the...
[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
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
17                                 "Logging specific to MSG (kernel)");
18
19 MSG_Global_t msg_global = NULL;
20
21
22 /** \defgroup msg_simulation   MSG simulation Functions
23  *  \brief This section describes the functions you need to know to
24  *  set up a simulation. You should have a look at \ref MSG_examples 
25  *  to have an overview of their usage.
26  */
27 /** @addtogroup msg_simulation
28  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
29  */
30
31 /********************************* MSG **************************************/
32
33 /** \ingroup msg_simulation
34  * \brief Initialize some MSG internal data.
35  */
36 void MSG_global_init_args(int *argc, char **argv)
37 {
38   MSG_global_init(argc, argv);
39 }
40
41 /** \ingroup msg_simulation
42  * \brief Initialize some MSG internal data.
43  */
44 void MSG_global_init(int *argc, char **argv)
45 {
46   xbt_getpid = &MSG_process_self_PID;
47   if (!msg_global) {
48     SIMIX_global_init(argc, argv);
49
50     msg_global = xbt_new0(s_MSG_Global_t, 1);
51
52     msg_global->host = xbt_fifo_new();
53     msg_global->process_list = xbt_fifo_new();
54     msg_global->max_channel = 0;
55     msg_global->PID = 1;
56
57     SIMIX_function_register_process_create(_MSG_process_create_from_SIMIX);
58     SIMIX_function_register_process_cleanup(__MSG_process_cleanup);
59     SIMIX_function_register_process_kill(_MSG_process_kill_from_SIMIX);
60   }
61   return;
62 }
63
64 /** \ingroup msg_easier_life
65  * \brief Traces MSG events in the Paje format.
66  */
67
68 void MSG_paje_output(const char *filename)
69 {
70 }
71
72 /** \defgroup m_channel_management    Understanding channels
73  *  \brief This section briefly describes the channel notion of MSG
74  *  (#m_channel_t).
75  */
76 /** @addtogroup m_channel_management
77  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
78  * 
79  *
80  *  For convenience, the simulator provides the notion of channel
81  *  that is close to the tag notion in MPI. A channel is not a
82  *  socket. It doesn't need to be opened neither closed. It rather
83  *  corresponds to the ports opened on the different machines.
84  */
85
86
87 /** \ingroup m_channel_management
88  * \brief Set the number of channel in the simulation.
89  *
90  * This function has to be called to fix the number of channel in the
91    simulation before creating any host. Indeed, each channel is
92    represented by a different mailbox on each #m_host_t. This
93    function can then be called only once. This function takes only one
94    parameter.
95  * \param number the number of channel in the simulation. It has to be >0
96  */
97 MSG_error_t MSG_set_channel_number(int number)
98 {
99   xbt_assert0((msg_global)
100               && (msg_global->max_channel == 0),
101               "Channel number already set!");
102
103   msg_global->max_channel = number;
104
105   return MSG_OK;
106 }
107
108 /** \ingroup m_channel_management
109  * \brief Return the number of channel in the simulation.
110  *
111  * This function has to be called once the number of channel is fixed. I can't 
112    figure out a reason why anyone would like to call this function but nevermind.
113  * \return the number of channel in the simulation.
114  */
115 int MSG_get_channel_number(void)
116 {
117   xbt_assert0((msg_global)
118               && (msg_global->max_channel != 0),
119               "Channel number not set yet!");
120
121   return msg_global->max_channel;
122 }
123
124 void __MSG_display_process_status(void)
125 {
126 }
127
128
129 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
130 #include <signal.h>
131
132 static void _XBT_CALL inthandler(int ignored)
133 {
134   INFO0("CTRL-C pressed. Displaying status and bailing out");
135   __MSG_display_process_status();
136   exit(1);
137 }
138
139 /** \ingroup msg_simulation
140  * \brief Launch the MSG simulation
141  */
142 MSG_error_t MSG_main(void)
143 {
144   smx_cond_t cond = NULL;
145   smx_action_t smx_action;
146   xbt_fifo_t actions_done = xbt_fifo_new();
147   xbt_fifo_t actions_failed = xbt_fifo_new();
148
149   /* Prepare to display some more info when dying on Ctrl-C pressing */
150   signal(SIGINT, inthandler);
151
152   /* Clean IO before the run */
153   fflush(stdout);
154   fflush(stderr);
155
156   //surf_solve(); /* Takes traces into account. Returns 0.0 */
157   /* xbt_fifo_size(msg_global->process_to_run) */
158
159   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
160
161     while ((smx_action = xbt_fifo_pop(actions_failed))) {
162
163
164       DEBUG1("** %s failed **", smx_action->name);
165       while ((cond = xbt_fifo_pop(smx_action->cond_list))) {
166         SIMIX_cond_broadcast(cond);
167       }
168       /* action finished, destroy it */
169       //      SIMIX_action_destroy(smx_action);
170     }
171
172     while ((smx_action = xbt_fifo_pop(actions_done))) {
173
174       DEBUG1("** %s done **", smx_action->name);
175       while ((cond = xbt_fifo_pop(smx_action->cond_list))) {
176         SIMIX_cond_broadcast(cond);
177       }
178       /* action finished, destroy it */
179       //SIMIX_action_destroy(smx_action);
180     }
181   }
182   xbt_fifo_free(actions_failed);
183   xbt_fifo_free(actions_done);
184   return MSG_OK;
185 }
186
187 /** \ingroup msg_simulation
188  * \brief Kill all running process
189
190  * \param reset_PIDs should we reset the PID numbers. A negative
191  *   number means no reset and a positive number will be used to set the PID
192  *   of the next newly created process.
193  */
194 int MSG_process_killall(int reset_PIDs)
195 {
196   m_process_t p = NULL;
197   m_process_t self = MSG_process_self();
198
199   while ((p = xbt_fifo_pop(msg_global->process_list))) {
200     if (p != self)
201       MSG_process_kill(p);
202   }
203
204   if (reset_PIDs > 0) {
205     msg_global->PID = reset_PIDs;
206     msg_global->session++;
207   }
208
209   return msg_global->PID;
210
211 }
212
213 /** \ingroup msg_simulation
214  * \brief Clean the MSG simulation
215  */
216 MSG_error_t MSG_clean(void)
217 {
218   xbt_fifo_item_t i = NULL;
219   m_host_t h = NULL;
220   m_process_t p = NULL;
221
222
223   while ((p = xbt_fifo_pop(msg_global->process_list))) {
224     MSG_process_kill(p);
225   }
226
227   xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
228     __MSG_host_destroy(h);
229   }
230   xbt_fifo_free(msg_global->host);
231   xbt_fifo_free(msg_global->process_list);
232
233   free(msg_global);
234   msg_global = NULL;
235   SIMIX_clean();
236
237   return MSG_OK;
238 }
239
240
241 /** \ingroup msg_easier_life
242  * \brief A clock (in second).
243  */
244 double MSG_get_clock(void)
245 {
246   return SIMIX_get_clock();
247 }