Logo AND Algorithmique Numérique Distribuée

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