Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into hypervisor
[simgrid.git] / src / msg / msg_global.c
1 /* Copyright (c) 2004-2012. The SimGrid Team.  All rights reserved.         */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "instr/instr_interface.h"
7 #include "msg_private.h"
8 #include "msg_mailbox.h"
9 #include "mc/mc.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/virtu.h"
13 #include "xbt/ex.h"             /* ex_backtrace_display */
14 #include "xbt/replay.h"
15 #include "simgrid/sg_config.h" /* Configuration mechanism of SimGrid */
16
17
18 XBT_LOG_NEW_CATEGORY(msg, "All MSG categories");
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg,
20                                 "Logging specific to MSG (kernel)");
21
22 MSG_Global_t msg_global = NULL;
23 static void MSG_exit(void);
24
25 /********************************* MSG **************************************/
26
27 /**
28  * \ingroup msg_simulation
29  * \brief Initialize MSG with less verifications
30  * You should use the MSG_init() function instead. Failing to do so may turn into PEBKAC some day. You've been warned.
31  */
32 void MSG_init_nocheck(int *argc, char **argv) {
33
34 #ifdef HAVE_TRACING
35   TRACE_global_init(argc, argv);
36 #endif
37
38   xbt_getpid = MSG_process_self_PID;
39   if (!msg_global) {
40
41     SIMIX_global_init(argc, argv);
42     
43     msg_global = xbt_new0(s_MSG_Global_t, 1);
44
45 #ifdef MSG_USE_DEPRECATED
46     msg_global->max_channel = 0;
47 #endif
48     msg_global->sent_msg = 0;
49     msg_global->task_copy_callback = NULL;
50     msg_global->process_data_cleanup = NULL;
51
52     /* initialization of the action module */
53     _MSG_action_init();
54
55     SIMIX_function_register_process_create(MSG_process_create_from_SIMIX);
56     SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX);
57
58     sg_platf_postparse_add_cb(MSG_post_create_environment);
59   }
60   
61   if(MC_is_active()){
62     /* Ignore total amount of messages sent during the simulation for heap comparison */
63     MC_ignore_heap(&(msg_global->sent_msg), sizeof(msg_global->sent_msg));
64   }
65
66 #ifdef HAVE_TRACING
67   TRACE_start();
68 #endif
69
70   XBT_DEBUG("ADD MSG LEVELS");
71   MSG_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __MSG_host_priv_free);
72
73   atexit(MSG_exit);
74 }
75
76 #ifdef MSG_USE_DEPRECATED
77
78 /* This deprecated function has to be called to fix the number of channel in the
79    simulation before creating any host. Indeed, each channel is
80    represented by a different mailbox on each #m_host_t. This
81    function can then be called only once. This function takes only one
82    parameter.
83  * \param number the number of channel in the simulation. It has to be >0
84  */
85 msg_error_t MSG_set_channel_number(int number)
86 {
87   XBT_WARN("DEPRECATED! Please use aliases instead");
88   xbt_assert((msg_global)
89               && (msg_global->max_channel == 0),
90               "Channel number already set!");
91
92   msg_global->max_channel = number;
93
94   return MSG_OK;
95 }
96
97 /* This deprecated function has to be called once the number of channel is fixed. I can't
98    figure out a reason why anyone would like to call this function but nevermind.
99  * \return the number of channel in the simulation.
100  */
101 int MSG_get_channel_number(void)
102 {
103   XBT_WARN("DEPRECATED! Please use aliases instead");
104   xbt_assert((msg_global)
105               && (msg_global->max_channel != 0),
106               "Channel number not set yet!");
107
108   return msg_global->max_channel;
109 }
110 #endif
111
112 /** \ingroup msg_simulation
113  * \brief Launch the MSG simulation
114  */
115 msg_error_t MSG_main(void)
116 {
117   /* Clean IO before the run */
118   fflush(stdout);
119   fflush(stderr);
120
121   if (MC_is_active()) {
122     MC_do_the_modelcheck_for_real();
123   } else {
124     SIMIX_run();
125   }
126   return MSG_OK;
127 }
128
129 /** \ingroup msg_simulation
130  * \brief set a configuration variable
131  *
132  * Do --help on any simgrid binary to see the list of currently existing configuration variables, and see Section @ref options.
133  *
134  * Example:
135  * MSG_config("workstation/model","KCCFLN05");
136  */
137 void MSG_config(const char *key, const char *value){
138   xbt_assert(msg_global,"ERROR: Please call MSG_init() before using MSG_config()");
139   xbt_cfg_set_as_string(_sg_cfg_set, key, value);
140 }
141
142
143 /** \ingroup msg_simulation
144  * \brief Kill all running process
145
146  * \param reset_PIDs should we reset the PID numbers. A negative
147  *   number means no reset and a positive number will be used to set the PID
148  *   of the next newly created process.
149  */
150 int MSG_process_killall(int reset_PIDs)
151 {
152   simcall_process_killall(reset_PIDs);
153
154   if (reset_PIDs > 0)
155     msg_global->session++;
156
157   return 0;
158
159 }
160
161 static void MSG_exit(void) {
162   if (msg_global==NULL)
163     return;
164
165 #ifdef HAVE_TRACING
166   TRACE_surf_resource_utilization_release();
167 #endif
168
169   /* initialization of the action module */
170   _MSG_action_exit();
171
172 #ifdef HAVE_TRACING
173   TRACE_end();
174 #endif
175
176   free(msg_global);
177   msg_global = NULL;
178 }
179
180
181 /** \ingroup msg_simulation
182  * \brief A clock (in second).
183  */
184 XBT_INLINE double MSG_get_clock(void)
185 {
186   return SIMIX_get_clock();
187 }
188
189 unsigned long int MSG_get_sent_msg()
190 {
191   return msg_global->sent_msg;
192 }
193
194 #ifdef MSG_USE_DEPRECATED
195 msg_error_t MSG_clean(void) {
196   return MSG_OK;
197 }
198 #endif