Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c88f4b97ba4e284f905b3ca820b48b580a5a1d22
[simgrid.git] / include / simgrid / msg.h
1 /* Copyright (c) 2004-2018. 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 #ifndef SIMGRID_MSG_H
7 #define SIMGRID_MSG_H
8
9 #include <simgrid/actor.h>
10 #include <simgrid/forward.h>
11 #include <simgrid/host.h>
12 #include <simgrid/instr.h>
13 #include <simgrid/plugins/live_migration.h>
14 #include <simgrid/storage.h>
15 #include <simgrid/vm.h>
16 #include <simgrid/zone.h>
17 #include <xbt/base.h>
18 #include <xbt/dict.h>
19 #include <xbt/dynar.h>
20
21 #ifdef __cplusplus
22 #include <map>
23 #include <simgrid/simix.h>
24 namespace simgrid {
25 namespace msg {
26 class Comm;
27 }
28 }
29 typedef simgrid::msg::Comm sg_msg_Comm;
30 #else
31 typedef struct msg_Comm sg_msg_Comm;
32 #endif
33
34 SG_BEGIN_DECL()
35
36 /* *************************** Network Zones ******************************** */
37 #define msg_as_t msg_netzone_t /* portability macro */
38 typedef sg_netzone_t msg_netzone_t;
39
40 XBT_PUBLIC msg_netzone_t MSG_zone_get_root();
41 XBT_PUBLIC const char* MSG_zone_get_name(msg_netzone_t zone);
42 XBT_PUBLIC msg_netzone_t MSG_zone_get_by_name(const char* name);
43 XBT_PUBLIC void MSG_zone_get_sons(msg_netzone_t zone, xbt_dict_t whereto);
44 XBT_PUBLIC const char* MSG_zone_get_property_value(msg_netzone_t zone, const char* name);
45 XBT_PUBLIC void MSG_zone_set_property_value(msg_netzone_t zone, const char* name, char* value);
46 XBT_PUBLIC void MSG_zone_get_hosts(msg_netzone_t zone, xbt_dynar_t whereto);
47
48 /* ******************************** Hosts ************************************ */
49 typedef sg_host_t msg_host_t;
50
51 XBT_PUBLIC size_t MSG_get_host_number();
52 XBT_PUBLIC sg_host_t MSG_get_host_by_name(const char* name);
53 XBT_PUBLIC sg_host_t MSG_host_by_name(const char* name);
54
55 XBT_PUBLIC xbt_dynar_t MSG_hosts_as_dynar();
56
57 XBT_PUBLIC const char* MSG_host_get_name(sg_host_t host);
58 XBT_PUBLIC void* MSG_host_get_data(sg_host_t host);
59 XBT_PUBLIC void MSG_host_set_data(sg_host_t host, void* data);
60 XBT_PUBLIC xbt_dict_t MSG_host_get_mounted_storage_list(sg_host_t host);
61 XBT_PUBLIC xbt_dynar_t MSG_host_get_attached_storage_lists(sg_host_t host);
62 XBT_PUBLIC double MSG_host_get_speed(sg_host_t host);
63 XBT_PUBLIC double MSG_host_get_power_peak_at(sg_host_t host, int pstate_index);
64 XBT_PUBLIC int MSG_host_get_core_number(sg_host_t host);
65 XBT_PUBLIC int MSG_host_get_nb_pstates(sg_host_t host);
66 XBT_PUBLIC int MSG_host_get_pstate(sg_host_t host);
67 XBT_PUBLIC void MSG_host_set_pstate(sg_host_t host, int pstate);
68 XBT_PUBLIC void MSG_host_on(sg_host_t h);
69 XBT_PUBLIC void MSG_host_off(sg_host_t h);
70 XBT_PUBLIC int MSG_host_is_on(sg_host_t h);
71 XBT_PUBLIC int MSG_host_is_off(sg_host_t h);
72 XBT_PUBLIC xbt_dict_t MSG_host_get_properties(sg_host_t host);
73 XBT_PUBLIC const char* MSG_host_get_property_value(sg_host_t host, const char* name);
74 XBT_PUBLIC void MSG_host_set_property_value(sg_host_t host, const char* name, const char* value);
75 XBT_PUBLIC void MSG_host_get_process_list(sg_host_t host, xbt_dynar_t whereto);
76
77 XBT_PUBLIC sg_host_t MSG_host_self();
78
79 XBT_ATTRIB_DEPRECATED_v320("Use MSG_host_get_speed(): v3.20 will drop MSG_host_get_current_power_peak() "
80                            "completely.") static inline double MSG_host_get_current_power_peak(msg_host_t host)
81 {
82   return sg_host_speed(host);
83 }
84
85 /* ******************************** VMs ************************************* */
86 typedef sg_vm_t msg_vm_t;
87
88 XBT_ATTRIB_DEPRECATED_v322("Use sg_vm_create_migratable() from the live migration plugin: "
89                            "v3.22 will drop MSG_vm_create() completely.") XBT_PUBLIC sg_vm_t
90     MSG_vm_create(sg_host_t ind_pm, const char* name, int coreAmount, int ramsize, int mig_netspeed, int dp_intensity);
91
92 XBT_PUBLIC msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name);
93 XBT_PUBLIC msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount);
94
95 XBT_PUBLIC int MSG_vm_is_created(msg_vm_t vm);
96 XBT_PUBLIC int MSG_vm_is_running(msg_vm_t vm);
97 XBT_PUBLIC int MSG_vm_is_suspended(msg_vm_t vm);
98
99 XBT_PUBLIC const char* MSG_vm_get_name(msg_vm_t vm);
100 XBT_PUBLIC void MSG_vm_set_ramsize(msg_vm_t vm, size_t size);
101 XBT_PUBLIC size_t MSG_vm_get_ramsize(msg_vm_t vm);
102 XBT_PUBLIC msg_host_t MSG_vm_get_pm(msg_vm_t vm);
103 XBT_PUBLIC void MSG_vm_set_bound(msg_vm_t vm, double bound);
104
105 XBT_PUBLIC void MSG_vm_start(msg_vm_t vm);
106 XBT_PUBLIC void MSG_vm_suspend(msg_vm_t vm);
107 XBT_PUBLIC void MSG_vm_resume(msg_vm_t vm);
108 XBT_PUBLIC void MSG_vm_shutdown(msg_vm_t vm);
109 XBT_PUBLIC void MSG_vm_destroy(msg_vm_t vm);
110
111 /* ******************************** Storage ********************************* */
112 typedef sg_storage_t msg_storage_t;
113
114 XBT_PUBLIC const char* MSG_storage_get_name(msg_storage_t storage);
115 XBT_PUBLIC msg_storage_t MSG_storage_get_by_name(const char* name);
116 XBT_PUBLIC xbt_dict_t MSG_storage_get_properties(msg_storage_t storage);
117 XBT_PUBLIC void MSG_storage_set_property_value(msg_storage_t storage, const char* name, const char* value);
118 XBT_PUBLIC const char* MSG_storage_get_property_value(msg_storage_t storage, const char* name);
119 XBT_PUBLIC xbt_dynar_t MSG_storages_as_dynar();
120 XBT_PUBLIC void MSG_storage_set_data(msg_storage_t storage, void* data);
121 XBT_PUBLIC void* MSG_storage_get_data(msg_storage_t storage);
122 XBT_PUBLIC const char* MSG_storage_get_host(msg_storage_t storage);
123 XBT_PUBLIC sg_size_t MSG_storage_read(msg_storage_t storage, sg_size_t size);
124 XBT_PUBLIC sg_size_t MSG_storage_write(msg_storage_t storage, sg_size_t size);
125
126 /* ******************************** Actor/process *************************** */
127 typedef sg_actor_t msg_process_t;
128
129 XBT_PUBLIC int MSG_process_get_PID(msg_process_t process);
130 XBT_PUBLIC int MSG_process_get_PPID(msg_process_t process);
131 XBT_PUBLIC const char* MSG_process_get_name(msg_process_t process);
132 XBT_PUBLIC sg_host_t MSG_process_get_host(msg_process_t process);
133
134 /*property handlers*/
135 XBT_PUBLIC xbt_dict_t MSG_process_get_properties(msg_process_t process);
136 XBT_PUBLIC const char* MSG_process_get_property_value(msg_process_t process, const char* name);
137
138 XBT_PUBLIC void MSG_process_suspend(msg_process_t process);
139 XBT_PUBLIC void MSG_process_resume(msg_process_t process);
140 XBT_PUBLIC int MSG_process_is_suspended(msg_process_t process);
141 XBT_PUBLIC void MSG_process_restart(msg_process_t process);
142 XBT_PUBLIC void MSG_process_daemonize(msg_process_t process);
143
144 /* ******************************** File ************************************ */
145 typedef sg_file_t msg_file_t;
146 XBT_PUBLIC_DATA int sg_storage_max_file_descriptors;
147
148 /**
149  * \brief @brief Communication action.
150  * \ingroup msg_task_usage
151  *
152  * Object representing an ongoing communication between processes. Such beast is usually obtained by using #MSG_task_isend, #MSG_task_irecv or friends.
153  */
154 typedef sg_msg_Comm* msg_comm_t;
155
156 /* ******************************** Task ************************************ */
157
158 typedef struct s_simdata_task_t* simdata_task_t;
159
160 typedef struct msg_task {
161   char* name;             /**< @brief task name if any */
162   simdata_task_t simdata; /**< @brief simulator data */
163   void* data;             /**< @brief user data */
164   long long int counter;  /* task unique identifier for instrumentation */
165   char* category;         /* task category for instrumentation */
166 } s_msg_task_t;
167
168 /** @brief Task datatype.
169     @ingroup m_task_management
170
171     A <em>task</em> may then be defined by a <em>computing
172     amount</em>, a <em>message size</em> and some <em>private
173     data</em>.
174  */
175
176 typedef struct msg_task* msg_task_t;
177
178 /** \brief Default value for an uninitialized #msg_task_t.
179     \ingroup m_task_management
180 */
181 #define MSG_TASK_UNINITIALIZED NULL
182
183 /** @brief Return code of most MSG functions
184     @ingroup msg_simulation
185     @{ */
186 /* Keep these code as binary values: java bindings manipulate | of these values */
187 typedef enum {
188   MSG_OK = 0,                 /**< @brief Everything is right. Keep on going this way ! */
189   MSG_TIMEOUT = 1,            /**< @brief nothing good happened before the timer you provided elapsed */
190   MSG_TRANSFER_FAILURE = 2,   /**< @brief There has been a problem during you task
191       transfer. Either the network is down or the remote host has been
192       shutdown. */
193   MSG_HOST_FAILURE = 4,       /**< @brief System shutdown. The host on which you are
194       running has just been rebooted. Free your datastructures and
195       return now !*/
196   MSG_TASK_CANCELED = 8      /**< @brief Canceled task. This task has been canceled by somebody!*/
197 } msg_error_t;
198 /** @} */
199
200 /************************** Global ******************************************/
201 XBT_PUBLIC void MSG_config(const char* key, const char* value);
202 /** \ingroup msg_simulation
203  *  \brief Initialize the MSG internal data.
204  *  \hideinitializer
205  *
206  *  It also check that the link-time and compile-time versions of SimGrid do
207  *  match, so you should use this version instead of the #MSG_init_nocheck
208  *  function that does the same initializations, but without this check.
209  *
210  *  We allow to link against compiled versions that differ in the patch level.
211  */
212 #define MSG_init(argc,argv)  do {                                                          \
213   sg_version_check(SIMGRID_VERSION_MAJOR,SIMGRID_VERSION_MINOR,SIMGRID_VERSION_PATCH);\
214     MSG_init_nocheck(argc,argv);                                                        \
215   } while (0)
216
217 XBT_PUBLIC void MSG_init_nocheck(int* argc, char** argv);
218 XBT_PUBLIC msg_error_t MSG_main();
219 XBT_PUBLIC void MSG_function_register(const char* name, xbt_main_func_t code);
220 XBT_PUBLIC void MSG_function_register_default(xbt_main_func_t code);
221 XBT_PUBLIC void MSG_create_environment(const char* file);
222 XBT_PUBLIC void MSG_launch_application(const char* file);
223 /*Bypass the parser */
224 XBT_PUBLIC void MSG_set_function(const char* host_id, const char* function_name, xbt_dynar_t arguments);
225
226 XBT_PUBLIC double MSG_get_clock();
227 XBT_PUBLIC unsigned long int MSG_get_sent_msg();
228
229 /************************** Process handling *********************************/
230 XBT_PUBLIC msg_process_t MSG_process_create(const char* name, xbt_main_func_t code, void* data, msg_host_t host);
231 XBT_PUBLIC msg_process_t MSG_process_create_with_arguments(const char* name, xbt_main_func_t code, void* data,
232                                                            msg_host_t host, int argc, char** argv);
233 XBT_PUBLIC msg_process_t MSG_process_create_with_environment(const char* name, xbt_main_func_t code, void* data,
234                                                              msg_host_t host, int argc, char** argv,
235                                                              xbt_dict_t properties);
236
237 XBT_PUBLIC msg_process_t MSG_process_attach(const char* name, void* data, msg_host_t host, xbt_dict_t properties);
238 XBT_PUBLIC void MSG_process_detach();
239
240 XBT_PUBLIC void MSG_process_kill(msg_process_t process);
241 XBT_PUBLIC int MSG_process_killall();
242 XBT_PUBLIC msg_error_t MSG_process_migrate(msg_process_t process, msg_host_t host);
243 XBT_PUBLIC void MSG_process_yield();
244
245 XBT_PUBLIC void MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup);
246 XBT_PUBLIC msg_process_t MSG_process_from_PID(int PID);
247 XBT_PUBLIC int MSG_process_self_PID();
248 XBT_PUBLIC int MSG_process_self_PPID();
249 XBT_PUBLIC const char* MSG_process_self_name();
250 XBT_PUBLIC msg_process_t MSG_process_self();
251 XBT_PUBLIC xbt_dynar_t MSG_processes_as_dynar();
252 XBT_PUBLIC int MSG_process_get_number();
253
254 XBT_PUBLIC msg_error_t MSG_process_set_kill_time(msg_process_t process, double kill_time);
255
256 XBT_PUBLIC void* MSG_process_get_data(msg_process_t process);
257 XBT_PUBLIC msg_error_t MSG_process_set_data(msg_process_t process, void* data);
258
259 XBT_PUBLIC void MSG_process_on_exit(int_f_pvoid_pvoid_t fun, void* data);
260 XBT_PUBLIC void MSG_process_auto_restart_set(msg_process_t process, int auto_restart);
261
262 XBT_PUBLIC void MSG_process_ref(msg_process_t process);
263 XBT_PUBLIC void MSG_process_unref(msg_process_t process);
264
265 /************************** Task handling ************************************/
266 XBT_PUBLIC msg_task_t MSG_task_create(const char* name, double flops_amount, double bytes_amount, void* data);
267 XBT_PUBLIC msg_task_t MSG_parallel_task_create(const char* name, int host_nb, const msg_host_t* host_list,
268                                                double* flops_amount, double* bytes_amount, void* data);
269 XBT_PUBLIC void* MSG_task_get_data(msg_task_t task);
270 XBT_PUBLIC void MSG_task_set_data(msg_task_t task, void* data);
271 XBT_PUBLIC void MSG_task_set_copy_callback(void (*callback)(msg_task_t task, msg_process_t src, msg_process_t dst));
272 XBT_PUBLIC msg_process_t MSG_task_get_sender(msg_task_t task);
273 XBT_PUBLIC msg_host_t MSG_task_get_source(msg_task_t task);
274 XBT_PUBLIC const char* MSG_task_get_name(msg_task_t task);
275 XBT_PUBLIC void MSG_task_set_name(msg_task_t task, const char* name);
276 XBT_PUBLIC msg_error_t MSG_task_cancel(msg_task_t task);
277 XBT_PUBLIC msg_error_t MSG_task_destroy(msg_task_t task);
278
279 XBT_PUBLIC msg_error_t MSG_task_execute(msg_task_t task);
280 XBT_PUBLIC msg_error_t MSG_parallel_task_execute(msg_task_t task);
281 XBT_PUBLIC msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout);
282 XBT_PUBLIC void MSG_task_set_priority(msg_task_t task, double priority);
283 XBT_PUBLIC void MSG_task_set_bound(msg_task_t task, double bound);
284
285 XBT_PUBLIC msg_error_t MSG_process_join(msg_process_t process, double timeout);
286 XBT_PUBLIC msg_error_t MSG_process_sleep(double nb_sec);
287
288 XBT_PUBLIC void MSG_task_set_flops_amount(msg_task_t task, double flops_amount);
289 XBT_PUBLIC double MSG_task_get_flops_amount(msg_task_t task);
290 XBT_PUBLIC double MSG_task_get_remaining_work_ratio(msg_task_t task);
291 XBT_PUBLIC void MSG_task_set_bytes_amount(msg_task_t task, double bytes_amount);
292
293 XBT_PUBLIC double MSG_task_get_remaining_communication(msg_task_t task);
294 XBT_PUBLIC double MSG_task_get_bytes_amount(msg_task_t task);
295
296 XBT_PUBLIC msg_error_t MSG_task_receive_ext(msg_task_t* task, const char* alias, double timeout, msg_host_t host);
297
298 XBT_PUBLIC msg_error_t MSG_task_receive_with_timeout(msg_task_t* task, const char* alias, double timeout);
299
300 XBT_PUBLIC msg_error_t MSG_task_receive(msg_task_t* task, const char* alias);
301 #define MSG_task_recv(t,a) MSG_task_receive(t,a)
302
303 XBT_PUBLIC msg_error_t MSG_task_receive_ext_bounded(msg_task_t* task, const char* alias, double timeout,
304                                                     msg_host_t host, double rate);
305
306 XBT_PUBLIC msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout,
307                                                              double rate);
308 XBT_PUBLIC msg_error_t MSG_task_receive_bounded(msg_task_t* task, const char* alias, double rate);
309 #define MSG_task_recv_bounded(t,a,r) MSG_task_receive_bounded(t,a,r)
310
311 XBT_PUBLIC msg_comm_t MSG_task_isend(msg_task_t task, const char* alias);
312 XBT_PUBLIC msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char* alias, double maxrate);
313 XBT_ATTRIB_DEPRECATED_v320(
314     "This function will be removed from SimGrid v3.20. If you really need this function, please speak up quickly.")
315     XBT_PUBLIC msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char* alias,
316                                                        int (*match_fun)(void*, void*, void*), void* match_data);
317
318 XBT_PUBLIC void MSG_task_dsend(msg_task_t task, const char* alias, void_f_pvoid_t cleanup);
319 XBT_PUBLIC void MSG_task_dsend_bounded(msg_task_t task, const char* alias, void_f_pvoid_t cleanup, double maxrate);
320 XBT_PUBLIC msg_comm_t MSG_task_irecv(msg_task_t* task, const char* alias);
321 XBT_PUBLIC msg_comm_t MSG_task_irecv_bounded(msg_task_t* task, const char* alias, double rate);
322 XBT_PUBLIC int MSG_comm_test(msg_comm_t comm);
323 XBT_PUBLIC int MSG_comm_testany(xbt_dynar_t comms);
324 XBT_PUBLIC void MSG_comm_destroy(msg_comm_t comm);
325 XBT_PUBLIC msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout);
326 XBT_PUBLIC void MSG_comm_waitall(msg_comm_t* comm, int nb_elem, double timeout);
327 XBT_PUBLIC int MSG_comm_waitany(xbt_dynar_t comms);
328 XBT_PUBLIC msg_task_t MSG_comm_get_task(msg_comm_t comm);
329 XBT_PUBLIC msg_error_t MSG_comm_get_status(msg_comm_t comm);
330
331 XBT_PUBLIC int MSG_task_listen(const char* alias);
332 XBT_PUBLIC msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char* alias, double timeout);
333 XBT_PUBLIC msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char* alias, double timeout,
334                                                           double maxrate);
335 XBT_PUBLIC msg_error_t MSG_task_send(msg_task_t task, const char* alias);
336 XBT_PUBLIC msg_error_t MSG_task_send_bounded(msg_task_t task, const char* alias, double rate);
337 XBT_PUBLIC int MSG_task_listen_from(const char* alias);
338 XBT_PUBLIC void MSG_task_set_category(msg_task_t task, const char* category);
339 XBT_PUBLIC const char* MSG_task_get_category(msg_task_t task);
340
341 /************************** Mailbox handling ************************************/
342
343 /* @brief MSG_mailbox_set_async - set a mailbox as eager
344  * Sets the mailbox to a permanent receiver mode. Messages sent to this mailbox will then be sent just after the send
345  * is issued, without waiting for the corresponding receive.
346  * This call should be done before issuing any receive, and on the receiver's side only
347  * @param alias    The alias of the mailbox to modify.
348  */
349 XBT_PUBLIC void MSG_mailbox_set_async(const char* alias);
350
351 /************************** Action handling **********************************/
352 XBT_PUBLIC msg_error_t MSG_action_trace_run(char* path);
353 XBT_PUBLIC void MSG_action_init();
354 XBT_PUBLIC void MSG_action_exit();
355
356 /** @brief Opaque type representing a semaphore
357  *  @ingroup msg_synchro
358  *  @hideinitializer
359  */
360 typedef struct s_smx_sem_t* msg_sem_t; // Yeah that's a rename of the smx_sem_t which doesnt require smx_sem_t to be
361                                        // declared here
362 XBT_PUBLIC msg_sem_t MSG_sem_init(int initial_value);
363 XBT_PUBLIC void MSG_sem_acquire(msg_sem_t sem);
364 XBT_PUBLIC msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout);
365 XBT_PUBLIC void MSG_sem_release(msg_sem_t sem);
366 XBT_PUBLIC int MSG_sem_get_capacity(msg_sem_t sem);
367 XBT_PUBLIC void MSG_sem_destroy(msg_sem_t sem);
368 XBT_PUBLIC int MSG_sem_would_block(msg_sem_t sem);
369
370 /** @brief Opaque type representing a barrier identifier
371  *  @ingroup msg_synchro
372  *  @hideinitializer
373  */
374
375 #define MSG_BARRIER_SERIAL_PROCESS -1
376 typedef struct s_msg_bar_t* msg_bar_t;
377 XBT_PUBLIC msg_bar_t MSG_barrier_init(unsigned int count);
378 XBT_PUBLIC void MSG_barrier_destroy(msg_bar_t bar);
379 XBT_PUBLIC int MSG_barrier_wait(msg_bar_t bar);
380
381 /* ****************************************************************************************** */
382 /* Used only by the bindings -- unclean pimple, please ignore if you're not writing a binding */
383 XBT_PUBLIC smx_context_t MSG_process_get_smx_ctx(msg_process_t process);
384
385 SG_END_DECL()
386
387 #ifdef __cplusplus
388 XBT_PUBLIC msg_process_t MSG_process_create_from_stdfunc(const char* name, std::function<void()> code, void* data,
389                                                          msg_host_t host,
390                                                          std::map<std::string, std::string>* properties);
391 #endif
392
393 #endif