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"
9 #include "xbt/sysdep.h"
11 #include "xbt/virtu.h"
12 #include "xbt/ex.h" /* ex_backtrace_display */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
16 "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);
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);
46 /** \ingroup msg_simulation
47 * \brief Initialize some MSG internal data.
49 void MSG_global_init(int *argc, char **argv)
52 TRACE_global_init(argc, argv);
55 xbt_getpid = MSG_process_self_PID;
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);
63 SIMIX_global_init(argc, argv);
65 msg_global = xbt_new0(s_MSG_Global_t, 1);
68 msg_global->process_list = xbt_swag_new(xbt_swag_offset(p, process_list_hookup));
69 msg_global->host = xbt_fifo_new();
70 msg_global->max_channel = 0;
72 msg_global->sent_msg = 0;
74 /* initialization of the action module */
77 SIMIX_function_register_process_create(_MSG_process_create_from_SIMIX);
78 SIMIX_function_register_process_cleanup(__MSG_process_cleanup);
79 SIMIX_function_register_process_kill(_MSG_process_kill_from_SIMIX);
86 /** \defgroup m_channel_management Understanding channels
87 * \brief This section briefly describes the channel notion of MSG
90 /** @addtogroup m_channel_management
91 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
94 * For convenience, the simulator provides the notion of channel
95 * that is close to the tag notion in MPI. A channel is not a
96 * socket. It doesn't need to be opened neither closed. It rather
97 * corresponds to the ports opened on the different machines.
101 /** \ingroup m_channel_management
102 * \brief Set the number of channel in the simulation.
104 * This function has to be called to fix the number of channel in the
105 simulation before creating any host. Indeed, each channel is
106 represented by a different mailbox on each #m_host_t. This
107 function can then be called only once. This function takes only one
109 * \param number the number of channel in the simulation. It has to be >0
111 MSG_error_t MSG_set_channel_number(int number)
113 xbt_assert0((msg_global)
114 && (msg_global->max_channel == 0),
115 "Channel number already set!");
117 msg_global->max_channel = number;
122 /** \ingroup m_channel_management
123 * \brief Return the number of channel in the simulation.
125 * This function has to be called once the number of channel is fixed. I can't
126 figure out a reason why anyone would like to call this function but nevermind.
127 * \return the number of channel in the simulation.
129 int MSG_get_channel_number(void)
131 xbt_assert0((msg_global)
132 && (msg_global->max_channel != 0),
133 "Channel number not set yet!");
135 return msg_global->max_channel;
138 /** \ingroup msg_simulation
139 * \brief Launch the MSG simulation
141 MSG_error_t MSG_main(void)
143 /* Clean IO before the run */
156 /** \ingroup msg_simulation
157 * \brief Kill all running process
159 * \param reset_PIDs should we reset the PID numbers. A negative
160 * number means no reset and a positive number will be used to set the PID
161 * of the next newly created process.
163 int MSG_process_killall(int reset_PIDs)
165 m_process_t p = NULL;
166 m_process_t self = MSG_process_self();
168 while ((p = xbt_swag_extract(msg_global->process_list))) {
173 if (reset_PIDs > 0) {
174 msg_global->PID = reset_PIDs;
175 msg_global->session++;
178 return msg_global->PID;
182 /** \ingroup msg_simulation
183 * \brief Clean the MSG simulation
185 MSG_error_t MSG_clean(void)
187 xbt_fifo_item_t i = NULL;
189 m_process_t p = NULL;
192 TRACE_surf_release();
195 while ((p = xbt_swag_extract(msg_global->process_list))) {
199 xbt_fifo_foreach(msg_global->host, i, h, m_host_t) {
200 __MSG_host_destroy(h);
202 xbt_fifo_free(msg_global->host);
203 xbt_swag_free(msg_global->process_list);
208 /* initialization of the action module */
221 /** \ingroup msg_easier_life
222 * \brief A clock (in second).
224 XBT_INLINE double MSG_get_clock(void)
226 return SIMIX_get_clock();
229 unsigned long int MSG_get_sent_msg()
231 return msg_global->sent_msg;