Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new option to any SimGrid-based simulator: --cfg=model-check:1 (for now, that's a...
[simgrid.git] / src / msg / global.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "msg/private.h"
8 #include "mc/mc.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/virtu.h"
12 #include "xbt/ex.h"             /* ex_backtrace_display */
13 #include "mailbox.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
16                                 "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
41 XBT_LOG_EXTERNAL_CATEGORY(msg_gos);
42 XBT_LOG_EXTERNAL_CATEGORY(msg_kernel);
43 XBT_LOG_EXTERNAL_CATEGORY(msg_mailbox);
44 XBT_LOG_EXTERNAL_CATEGORY(msg_process);
45
46 /** \ingroup msg_simulation
47  * \brief Initialize some MSG internal data.
48  */
49 void MSG_global_init(int *argc, char **argv)
50 {
51   xbt_getpid = MSG_process_self_PID;
52   if (!msg_global) {
53     /* Connect our log channels: that must be done manually under windows */
54     XBT_LOG_CONNECT(msg_gos, msg);
55     XBT_LOG_CONNECT(msg_kernel, msg);
56     XBT_LOG_CONNECT(msg_mailbox, msg);
57     XBT_LOG_CONNECT(msg_process, msg);
58
59     SIMIX_global_init(argc, argv);
60
61     msg_global = xbt_new0(s_MSG_Global_t, 1);
62
63     msg_global->host = xbt_fifo_new();
64     msg_global->process_list = xbt_fifo_new();
65     msg_global->max_channel = 0;
66     msg_global->PID = 1;
67     msg_global->sent_msg = 0;
68
69     /* initialization of the mailbox module */
70     MSG_mailbox_mod_init();
71
72     /* initialization of the action module */
73     _MSG_action_init();
74
75     SIMIX_function_register_process_create(_MSG_process_create_from_SIMIX);
76     SIMIX_function_register_process_cleanup(__MSG_process_cleanup);
77     SIMIX_function_register_process_kill(_MSG_process_kill_from_SIMIX);
78   }
79 }
80
81 /** \defgroup m_channel_management    Understanding channels
82  *  \brief This section briefly describes the channel notion of MSG
83  *  (#m_channel_t).
84  */
85 /** @addtogroup m_channel_management
86  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
87  *
88  *
89  *  For convenience, the simulator provides the notion of channel
90  *  that is close to the tag notion in MPI. A channel is not a
91  *  socket. It doesn't need to be opened neither closed. It rather
92  *  corresponds to the ports opened on the different machines.
93  */
94
95
96 /** \ingroup m_channel_management
97  * \brief Set the number of channel in the simulation.
98  *
99  * This function has to be called to fix the number of channel in the
100    simulation before creating any host. Indeed, each channel is
101    represented by a different mailbox on each #m_host_t. This
102    function can then be called only once. This function takes only one
103    parameter.
104  * \param number the number of channel in the simulation. It has to be >0
105  */
106 MSG_error_t MSG_set_channel_number(int number)
107 {
108   xbt_assert0((msg_global)
109               && (msg_global->max_channel == 0),
110               "Channel number already set!");
111
112   msg_global->max_channel = number;
113
114   return MSG_OK;
115 }
116
117 /** \ingroup m_channel_management
118  * \brief Return the number of channel in the simulation.
119  *
120  * This function has to be called once the number of channel is fixed. I can't
121    figure out a reason why anyone would like to call this function but nevermind.
122  * \return the number of channel in the simulation.
123  */
124 int MSG_get_channel_number(void)
125 {
126   xbt_assert0((msg_global)
127               && (msg_global->max_channel != 0),
128               "Channel number not set yet!");
129
130   return msg_global->max_channel;
131 }
132
133 /** \ingroup msg_simulation
134  * \brief Launch the MSG simulation
135  */
136 MSG_error_t MSG_main(void)
137 {
138   /* Clean IO before the run */
139   fflush(stdout);
140   fflush(stderr);
141   SIMIX_init();
142
143   if (_surf_do_model_check)
144     MC_modelcheck(1);
145   else
146     while (SIMIX_solve(NULL, NULL) != -1.0);
147   
148   return MSG_OK;
149 }
150
151 /** \ingroup msg_simulation
152  * \brief Kill all running process
153
154  * \param reset_PIDs should we reset the PID numbers. A negative
155  *   number means no reset and a positive number will be used to set the PID
156  *   of the next newly created process.
157  */
158 int MSG_process_killall(int reset_PIDs)
159 {
160   m_process_t p = NULL;
161   m_process_t self = MSG_process_self();
162
163   while ((p = xbt_fifo_pop(msg_global->process_list))) {
164     if (p != self)
165       MSG_process_kill(p);
166   }
167
168   if (reset_PIDs > 0) {
169     msg_global->PID = reset_PIDs;
170     msg_global->session++;
171   }
172
173   return msg_global->PID;
174
175 }
176
177 /** \ingroup msg_simulation
178  * \brief Clean the MSG simulation
179  */
180 MSG_error_t MSG_clean(void)
181 {
182   xbt_fifo_item_t i = NULL;
183   m_host_t h = NULL;
184   m_process_t p = NULL;
185
186 #ifdef HAVE_TRACING
187   TRACE_msg_clean ();
188 #endif
189
190   while ((p = xbt_fifo_pop(msg_global->process_list))) {
191     MSG_process_kill(p);
192   }
193
194   xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
195     __MSG_host_destroy(h);
196   }
197   xbt_fifo_free(msg_global->host);
198   xbt_fifo_free(msg_global->process_list);
199
200   free(msg_global);
201   msg_global = NULL;
202
203   /* cleanup all resources in the mailbox module */
204   MSG_mailbox_mod_exit();
205
206   /* initialization of the action module */
207   _MSG_action_exit();
208
209   SIMIX_clean();
210
211   return MSG_OK;
212 }
213
214
215 /** \ingroup msg_easier_life
216  * \brief A clock (in second).
217  */
218 double MSG_get_clock(void)
219 {
220   return SIMIX_get_clock();
221 }
222
223 unsigned long int MSG_get_sent_msg()
224 {
225   return msg_global->sent_msg;
226 }