Logo AND Algorithmique Numérique Distribuée

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