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 */
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg, "Logging specific to MSG (kernel)");
18 MSG_Global_t msg_global = NULL;
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.
26 /** @addtogroup msg_simulation
27 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
30 /********************************* MSG **************************************/
32 /** \ingroup msg_simulation
33 * \brief Initialize some MSG internal data.
35 void MSG_global_init_args(int *argc, char **argv)
37 MSG_global_init(argc,argv);
40 /** \ingroup msg_simulation
41 * \brief Initialize some MSG internal data.
43 void MSG_global_init(int *argc, char **argv)
45 xbt_getpid = & MSG_process_self_PID;
47 SIMIX_global_init(argc, argv);
49 msg_global = xbt_new0(s_MSG_Global_t,1);
51 msg_global->host = xbt_fifo_new();
52 msg_global->process_list = xbt_fifo_new();
53 msg_global->max_channel = 0;
60 /** \ingroup msg_easier_life
61 * \brief Traces MSG events in the Paje format.
64 void MSG_paje_output(const char *filename)
68 /** \defgroup m_channel_management Understanding channels
69 * \brief This section briefly describes the channel notion of MSG
72 /** @addtogroup m_channel_management
73 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
76 * For convenience, the simulator provides the notion of channel
77 * that is close to the tag notion in MPI. A channel is not a
78 * socket. It doesn't need to be opened neither closed. It rather
79 * corresponds to the ports opened on the different machines.
83 /** \ingroup m_channel_management
84 * \brief Set the number of channel in the simulation.
86 * This function has to be called to fix the number of channel in the
87 simulation before creating any host. Indeed, each channel is
88 represented by a different mailbox on each #m_host_t. This
89 function can then be called only once. This function takes only one
91 * \param number the number of channel in the simulation. It has to be >0
93 MSG_error_t MSG_set_channel_number(int number)
95 xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
97 msg_global->max_channel = number;
102 /** \ingroup m_channel_management
103 * \brief Return the number of channel in the simulation.
105 * This function has to be called once the number of channel is fixed. I can't
106 figure out a reason why anyone would like to call this function but nevermind.
107 * \return the number of channel in the simulation.
109 int MSG_get_channel_number(void)
111 xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
113 return msg_global->max_channel;
116 void __MSG_display_process_status(void)
121 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
124 static void _XBT_CALL inthandler(int ignored)
126 INFO0("CTRL-C pressed. Displaying status and bailing out");
127 __MSG_display_process_status();
131 /** \ingroup msg_simulation
132 * \brief Launch the MSG simulation
134 MSG_error_t MSG_main(void)
136 smx_cond_t cond = NULL;
137 smx_action_t smx_action;
138 xbt_fifo_t actions_done = xbt_fifo_new();
139 xbt_fifo_t actions_failed = xbt_fifo_new();
141 /* Prepare to display some more info when dying on Ctrl-C pressing */
142 signal(SIGINT,inthandler);
144 /* Clean IO before the run */
148 //surf_solve(); /* Takes traces into account. Returns 0.0 */
149 /* xbt_fifo_size(msg_global->process_to_run) */
151 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
153 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
156 DEBUG1("** %s failed **",smx_action->name);
157 while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
158 SIMIX_cond_broadcast(cond);
160 /* action finished, destroy it */
161 // SIMIX_action_destroy(smx_action);
164 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
166 DEBUG1("** %s done **",smx_action->name);
167 while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
168 SIMIX_cond_broadcast(cond);
170 /* action finished, destroy it */
171 //SIMIX_action_destroy(smx_action);
174 xbt_fifo_free(actions_failed);
175 xbt_fifo_free(actions_done);
179 /** \ingroup msg_simulation
180 * \brief Kill all running process
182 * \param reset_PIDs should we reset the PID numbers. A negative
183 * number means no reset and a positive number will be used to set the PID
184 * of the next newly created process.
186 int MSG_process_killall(int reset_PIDs)
188 m_process_t p = NULL;
189 m_process_t self = MSG_process_self();
191 while((p=xbt_fifo_pop(msg_global->process_list))) {
192 if(p!=self) MSG_process_kill(p);
196 msg_global->PID = reset_PIDs;
197 msg_global->session++;
200 return msg_global->PID;
204 /** \ingroup msg_simulation
205 * \brief Clean the MSG simulation
207 MSG_error_t MSG_clean(void)
209 xbt_fifo_item_t i = NULL;
211 m_process_t p = NULL;
214 while((p=xbt_fifo_pop(msg_global->process_list))) {
218 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
219 __MSG_host_destroy(h);
221 xbt_fifo_free(msg_global->host);
222 xbt_fifo_free(msg_global->process_list);
231 /** \ingroup msg_easier_life
232 * \brief A clock (in second).
234 double MSG_get_clock(void)
236 return SIMIX_get_clock();