Logo AND Algorithmique Numérique Distribuée

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