Logo AND Algorithmique Numérique Distribuée

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