1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "msg/private.h"
8 #include "xbt/sysdep.h"
10 #include "xbt/virtu.h"
11 #include "xbt/ex.h" /* ex_backtrace_display */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
15 "Logging specific to MSG (kernel)");
17 MSG_Global_t msg_global = NULL;
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.
25 /** @addtogroup msg_simulation
26 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
29 /********************************* MSG **************************************/
31 /** \ingroup msg_simulation
32 * \brief Initialize some MSG internal data.
34 void MSG_global_init_args(int *argc, char **argv)
36 MSG_global_init(argc, argv);
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);
45 /** \ingroup msg_simulation
46 * \brief Initialize some MSG internal data.
48 void MSG_global_init(int *argc, char **argv)
50 xbt_getpid = MSG_process_self_PID;
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);
58 SIMIX_global_init(argc, argv);
60 msg_global = xbt_new0(s_MSG_Global_t, 1);
62 msg_global->host = xbt_fifo_new();
63 msg_global->process_list = xbt_fifo_new();
64 msg_global->max_channel = 0;
66 msg_global->sent_msg = 0;
68 /* initialization of the mailbox module */
69 MSG_mailbox_mod_init();
71 /* initialization of the action module */
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 /** \defgroup m_channel_management Understanding channels
82 * \brief This section briefly describes the channel notion of MSG
85 /** @addtogroup m_channel_management
86 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
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.
96 /** \ingroup m_channel_management
97 * \brief Set the number of channel in the simulation.
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
104 * \param number the number of channel in the simulation. It has to be >0
106 MSG_error_t MSG_set_channel_number(int number)
108 xbt_assert0((msg_global)
109 && (msg_global->max_channel == 0),
110 "Channel number already set!");
112 msg_global->max_channel = number;
117 /** \ingroup m_channel_management
118 * \brief Return the number of channel in the simulation.
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.
124 int MSG_get_channel_number(void)
126 xbt_assert0((msg_global)
127 && (msg_global->max_channel != 0),
128 "Channel number not set yet!");
130 return msg_global->max_channel;
133 /** \ingroup msg_simulation
134 * \brief Launch the MSG simulation
136 MSG_error_t MSG_main(void)
138 /* Clean IO before the run */
143 while (SIMIX_solve(NULL, NULL) != -1.0);
148 /** \ingroup msg_simulation
149 * \brief Kill all running process
151 * \param reset_PIDs should we reset the PID numbers. A negative
152 * number means no reset and a positive number will be used to set the PID
153 * of the next newly created process.
155 int MSG_process_killall(int reset_PIDs)
157 m_process_t p = NULL;
158 m_process_t self = MSG_process_self();
160 while ((p = xbt_fifo_pop(msg_global->process_list))) {
165 if (reset_PIDs > 0) {
166 msg_global->PID = reset_PIDs;
167 msg_global->session++;
170 return msg_global->PID;
174 /** \ingroup msg_simulation
175 * \brief Clean the MSG simulation
177 MSG_error_t MSG_clean(void)
179 xbt_fifo_item_t i = NULL;
181 m_process_t p = NULL;
187 while ((p = xbt_fifo_pop(msg_global->process_list))) {
191 xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
192 __MSG_host_destroy(h);
194 xbt_fifo_free(msg_global->host);
195 xbt_fifo_free(msg_global->process_list);
200 /* cleanup all resources in the mailbox module */
201 MSG_mailbox_mod_exit();
203 /* initialization of the action module */
212 /** \ingroup msg_easier_life
213 * \brief A clock (in second).
215 double MSG_get_clock(void)
217 return SIMIX_get_clock();
220 unsigned long int MSG_get_sent_msg()
222 return msg_global->sent_msg;