Logo AND Algorithmique Numérique Distribuée

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