Logo AND Algorithmique Numérique Distribuée

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