3 /* Copyright (c) 2002-2007 Arnaud Legrand. */
4 /* Copyright (c) 2007 Bruno Donassolo. */
5 /* All rights reserved. */
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. */
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
13 #include "xbt/virtu.h"
14 #include "xbt/ex.h" /* ex_backtrace_display */
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
18 "Logging specific to MSG (kernel)");
20 MSG_Global_t msg_global = NULL;
23 /** \defgroup msg_simulation MSG simulation Functions
24 * \brief This section describes the functions you need to know to
25 * set up a simulation. You should have a look at \ref MSG_examples
26 * to have an overview of their usage.
28 /** @addtogroup msg_simulation
29 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
32 /********************************* MSG **************************************/
34 /** \ingroup msg_simulation
35 * \brief Initialize some MSG internal data.
37 void MSG_global_init_args(int *argc, char **argv)
39 MSG_global_init(argc, argv);
43 XBT_LOG_EXTERNAL_CATEGORY(msg_gos);
44 XBT_LOG_EXTERNAL_CATEGORY(msg_kernel);
45 XBT_LOG_EXTERNAL_CATEGORY(msg_mailbox);
46 XBT_LOG_EXTERNAL_CATEGORY(msg_process);
48 /** \ingroup msg_simulation
49 * \brief Initialize some MSG internal data.
51 void MSG_global_init(int *argc, char **argv)
53 xbt_getpid = MSG_process_self_PID;
55 /* Connect our log channels: that must be done manually under windows */
56 XBT_LOG_CONNECT(msg_gos, msg);
57 XBT_LOG_CONNECT(msg_kernel, msg);
58 XBT_LOG_CONNECT(msg_mailbox, msg);
59 XBT_LOG_CONNECT(msg_process, msg);
61 SIMIX_global_init(argc, argv);
63 msg_global = xbt_new0(s_MSG_Global_t, 1);
65 msg_global->host = xbt_fifo_new();
66 msg_global->process_list = xbt_fifo_new();
67 msg_global->max_channel = 0;
69 msg_global->sent_msg = 0;
71 /* initialization of the mailbox module */
72 MSG_mailbox_mod_init();
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);
81 /** \ingroup msg_easier_life
82 * \brief Traces MSG events in the Paje format.
85 void MSG_paje_output(const char *filename)
89 /** \defgroup m_channel_management Understanding channels
90 * \brief This section briefly describes the channel notion of MSG
93 /** @addtogroup m_channel_management
94 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
97 * For convenience, the simulator provides the notion of channel
98 * that is close to the tag notion in MPI. A channel is not a
99 * socket. It doesn't need to be opened neither closed. It rather
100 * corresponds to the ports opened on the different machines.
104 /** \ingroup m_channel_management
105 * \brief Set the number of channel in the simulation.
107 * This function has to be called to fix the number of channel in the
108 simulation before creating any host. Indeed, each channel is
109 represented by a different mailbox on each #m_host_t. This
110 function can then be called only once. This function takes only one
112 * \param number the number of channel in the simulation. It has to be >0
114 MSG_error_t MSG_set_channel_number(int number)
116 xbt_assert0((msg_global)
117 && (msg_global->max_channel == 0),
118 "Channel number already set!");
120 msg_global->max_channel = number;
125 /** \ingroup m_channel_management
126 * \brief Return the number of channel in the simulation.
128 * This function has to be called once the number of channel is fixed. I can't
129 figure out a reason why anyone would like to call this function but nevermind.
130 * \return the number of channel in the simulation.
132 int MSG_get_channel_number(void)
134 xbt_assert0((msg_global)
135 && (msg_global->max_channel != 0),
136 "Channel number not set yet!");
138 return msg_global->max_channel;
141 void __MSG_display_process_status(void)
146 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
149 static void _XBT_CALL inthandler(int ignored)
151 INFO0("CTRL-C pressed. Displaying status and bailing out");
152 __MSG_display_process_status();
156 /** \ingroup msg_simulation
157 * \brief Launch the MSG simulation
159 MSG_error_t MSG_main(void)
161 smx_cond_t cond = NULL;
162 smx_action_t smx_action;
163 xbt_fifo_t actions_done = xbt_fifo_new();
164 xbt_fifo_t actions_failed = xbt_fifo_new();
166 /* Prepare to display some more info when dying on Ctrl-C pressing */
167 signal(SIGINT, inthandler);
169 /* Clean IO before the run */
174 //surf_solve(); /* Takes traces into account. Returns 0.0 */
175 /* xbt_fifo_size(msg_global->process_to_run) */
177 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
179 while ((smx_action = xbt_fifo_pop(actions_failed))) {
182 DEBUG1("** %s failed **", smx_action->name);
183 while ((cond = xbt_fifo_pop(smx_action->cond_list))) {
184 SIMIX_cond_broadcast(cond);
188 while ((smx_action = xbt_fifo_pop(actions_done))) {
190 DEBUG1("** %s done **", smx_action->name);
191 while ((cond = xbt_fifo_pop(smx_action->cond_list))) {
192 SIMIX_cond_broadcast(cond);
196 xbt_fifo_free(actions_failed);
197 xbt_fifo_free(actions_done);
201 /** \ingroup msg_simulation
202 * \brief Kill all running process
204 * \param reset_PIDs should we reset the PID numbers. A negative
205 * number means no reset and a positive number will be used to set the PID
206 * of the next newly created process.
208 int MSG_process_killall(int reset_PIDs)
210 m_process_t p = NULL;
211 m_process_t self = MSG_process_self();
213 while ((p = xbt_fifo_pop(msg_global->process_list))) {
218 if (reset_PIDs > 0) {
219 msg_global->PID = reset_PIDs;
220 msg_global->session++;
223 return msg_global->PID;
227 /** \ingroup msg_simulation
228 * \brief Clean the MSG simulation
230 MSG_error_t MSG_clean(void)
232 xbt_fifo_item_t i = NULL;
234 m_process_t p = NULL;
237 while ((p = xbt_fifo_pop(msg_global->process_list))) {
241 xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
242 __MSG_host_destroy(h);
244 xbt_fifo_free(msg_global->host);
245 xbt_fifo_free(msg_global->process_list);
250 /* cleanup all resources in the mailbox module */
251 MSG_mailbox_mod_exit();
261 /** \ingroup msg_easier_life
262 * \brief A clock (in second).
264 double MSG_get_clock(void)
266 return SIMIX_get_clock();
269 unsigned long int MSG_get_sent_msg() {
270 return msg_global->sent_msg;