1 /* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "src/msg/msg_private.hpp"
9 #include "xbt/functional.hpp"
11 #define MSG_CALL(type, oldname, args)
13 /* ************************** Engine *************************** */
14 void MSG_create_environment(const char* filename)
16 simgrid_load_platform(filename);
19 void MSG_launch_application(const char* filename)
21 simgrid_load_deployment(filename);
23 msg_error_t MSG_main()
28 void MSG_function_register(const char* name, int (*code)(int, char**))
30 simgrid::kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
31 return simgrid::xbt::wrap_main(code, std::move(args));
33 simgrid::s4u::Engine::get_instance()->register_function(name, code_factory);
35 void MSG_function_register_default(int (*code)(int, char**))
37 simgrid::s4u::Engine::get_instance()->register_default(
38 [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); });
40 double MSG_get_clock()
42 return simgrid_get_clock();
45 /* ************************** Mailboxes ************************ */
46 void MSG_mailbox_set_async(const char* alias)
48 sg_mailbox_set_receiver(alias);
50 int MSG_task_listen(const char* alias)
52 return sg_mailbox_listen(alias);
55 /* ************************** Actors *************************** */
56 void MSG_process_on_exit(int_f_int_pvoid_t fun, void* data)
58 /* We can't use the sg_actor_on_exit, as the return type of the callback changed: the int in MSG is ignored and was
60 simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); });
63 int MSG_process_get_PID(const_sg_actor_t actor)
65 return sg_actor_get_pid(actor);
67 int MSG_process_get_PPID(const_sg_actor_t actor)
69 return sg_actor_get_ppid(actor);
71 msg_process_t MSG_process_from_PID(int pid)
73 return sg_actor_by_pid(pid);
75 const char* MSG_process_get_name(const_sg_actor_t actor)
77 return sg_actor_get_name(actor);
79 sg_host_t MSG_process_get_host(const_sg_actor_t actor)
81 return sg_actor_get_host(actor);
83 xbt_dict_t MSG_process_get_properties(const_sg_actor_t actor)
85 return sg_actor_get_properties(actor);
87 const char* MSG_process_get_property_value(const_sg_actor_t actor, const char* name)
89 return sg_actor_get_property_value(actor, name);
91 void MSG_process_suspend(sg_actor_t actor)
93 sg_actor_suspend(actor);
95 void MSG_process_resume(sg_actor_t actor)
97 sg_actor_resume(actor);
99 int MSG_process_is_suspended(const_sg_actor_t actor)
101 return sg_actor_is_suspended(actor);
103 void MSG_process_restart(sg_actor_t actor)
105 sg_actor_restart(actor);
107 void MSG_process_auto_restart_set(sg_actor_t actor, int auto_restart)
109 sg_actor_set_auto_restart(actor, auto_restart);
112 void MSG_process_daemonize(sg_actor_t actor)
114 sg_actor_daemonize(actor);
116 void MSG_process_migrate(sg_actor_t actor, sg_host_t host)
118 sg_actor_set_host(actor, host);
120 void MSG_process_join(const_sg_actor_t actor, double timeout)
122 sg_actor_join(actor, timeout);
124 void MSG_process_kill(sg_actor_t actor)
126 sg_actor_kill(actor);
128 void MSG_process_killall()
132 void MSG_process_set_kill_time(sg_actor_t actor, double kill_time)
134 sg_actor_set_kill_time(actor, kill_time);
136 void MSG_process_yield()
141 msg_error_t MSG_process_sleep(double duration)
144 sg_actor_sleep_for(duration);
146 } catch (const simgrid::HostFailureException&) {
147 return MSG_HOST_FAILURE;
151 /** @brief Returns the user data of a process.
153 * This function checks whether @a process is a valid pointer and returns the user data associated to this process.
155 void* MSG_process_get_data(const_sg_actor_t process)
157 xbt_assert(process != nullptr, "Invalid parameter: first parameter must not be nullptr!");
159 /* get from SIMIX the MSG process data, and then the user data */
160 return sg_actor_get_data(process);
163 /** @brief Sets the user data of a process.
165 * This function checks whether @a process is a valid pointer and sets the user data associated to this process.
167 msg_error_t MSG_process_set_data(msg_process_t process, void* data)
169 xbt_assert(process != nullptr, "Invalid parameter: first parameter must not be nullptr!");
170 sg_actor_set_data(process, data);
175 msg_process_t MSG_process_attach(const char* name, void* data, msg_host_t host, xbt_dict_t properties)
177 return sg_actor_attach(name, data, host, properties);
180 void MSG_process_detach()
184 aid_t MSG_process_self_PID()
186 return sg_actor_self_get_pid();
189 /** @brief Return the PPID of the current process.
191 * This function returns the PID of the parent of the currently running #msg_process_t.
193 aid_t MSG_process_self_PPID()
195 return sg_actor_self_get_ppid();
198 /** @brief Return the name of the current process. */
199 const char* MSG_process_self_name()
201 return sg_actor_self_get_name();
203 /** @brief Return the current process.
205 * This function returns the currently running #msg_process_t.
207 msg_process_t MSG_process_self()
209 return sg_actor_self();
212 /** @brief Take an extra reference on that process to prevent it to be garbage-collected */
213 void MSG_process_ref(const_sg_actor_t process)
215 sg_actor_ref(process);
217 /** @brief Release a reference on that process so that it can get be garbage-collected */
218 void MSG_process_unref(const_sg_actor_t process)
220 sg_actor_unref(process);
223 /* ************************** NetZones *************************** */
224 sg_netzone_t MSG_zone_get_root()
226 return sg_zone_get_root();
228 const char* MSG_zone_get_name(const_sg_netzone_t zone)
230 return sg_zone_get_name(zone);
232 sg_netzone_t MSG_zone_get_by_name(const char* name)
234 return sg_zone_get_by_name(name);
236 void MSG_zone_get_sons(const_sg_netzone_t zone, xbt_dict_t whereto)
238 return sg_zone_get_sons(zone, whereto);
240 const char* MSG_zone_get_property_value(const_sg_netzone_t zone, const char* name)
242 return sg_zone_get_property_value(zone, name);
244 void MSG_zone_set_property_value(sg_netzone_t zone, const char* name, const char* value)
246 sg_zone_set_property_value(zone, name, value);
248 void MSG_zone_get_hosts(const_sg_netzone_t zone, xbt_dynar_t whereto)
250 sg_zone_get_hosts(zone, whereto);
253 /* ************************** hosts *************************** */
254 size_t MSG_get_host_number()
256 return sg_host_count();
258 sg_host_t MSG_get_host_by_name(const char* name)
260 return sg_host_by_name(name);
262 sg_host_t MSG_host_by_name(const char* name)
264 return sg_host_by_name(name);
266 const char* MSG_host_get_name(const_sg_host_t host)
268 return sg_host_get_name(host);
270 void* MSG_host_get_data(const_sg_host_t host)
272 return sg_host_get_data(host);
274 void MSG_host_set_data(sg_host_t host, void* data)
276 return sg_host_set_data(host, data);
278 double MSG_host_get_speed(const_sg_host_t host)
280 return sg_host_get_speed(host);
282 double MSG_host_get_power_peak_at(const_sg_host_t host, int pstate_index)
284 return sg_host_get_pstate_speed(host, pstate_index);
286 int MSG_host_get_core_number(const_sg_host_t host)
288 return sg_host_core_count(host);
290 int MSG_host_get_nb_pstates(const_sg_host_t host)
292 return sg_host_get_nb_pstates(host);
294 int MSG_host_get_pstate(const_sg_host_t host)
296 return sg_host_get_pstate(host);
298 void MSG_host_set_pstate(sg_host_t host, int pstate)
300 sg_host_set_pstate(host, pstate);
302 void MSG_host_on(sg_host_t h)
306 void MSG_host_off(sg_host_t h)
310 int MSG_host_is_on(const_sg_host_t h)
312 return sg_host_is_on(h);
314 xbt_dict_t MSG_host_get_properties(const_sg_host_t host)
316 return sg_host_get_properties(host);
318 const char* MSG_host_get_property_value(const_sg_host_t host, const char* name)
320 return sg_host_get_property_value(host, name);
322 void MSG_host_set_property_value(sg_host_t host, const char* name, const char* value)
324 sg_host_set_property_value(host, name, value);
326 void MSG_host_get_process_list(const_sg_host_t host, xbt_dynar_t whereto)
328 sg_host_get_actor_list(host, whereto);
330 sg_host_t MSG_host_self()
332 return sg_host_self();
335 double MSG_host_get_load(const_sg_host_t host)
337 return sg_host_get_load(host);
339 /* ************************** Virtual Machines *************************** */
340 sg_vm_t MSG_vm_create_core(sg_host_t pm, const char* name)
342 return sg_vm_create_core(pm, name);
344 sg_vm_t MSG_vm_create_multicore(sg_host_t pm, const char* name, int coreAmount)
346 return sg_vm_create_multicore(pm, name, coreAmount);
348 int MSG_vm_is_created(const_sg_vm_t vm)
350 return sg_vm_is_created(vm);
352 int MSG_vm_is_running(const_sg_vm_t vm)
354 return sg_vm_is_running(vm);
356 int MSG_vm_is_suspended(const_sg_vm_t vm)
358 return sg_vm_is_suspended(vm);
360 const char* MSG_vm_get_name(const_sg_vm_t vm)
362 return sg_vm_get_name(vm);
364 void MSG_vm_set_ramsize(sg_vm_t vm, size_t size)
366 sg_vm_set_ramsize(vm, size);
368 size_t MSG_vm_get_ramsize(const_sg_vm_t vm)
370 return sg_vm_get_ramsize(vm);
372 sg_host_t MSG_vm_get_pm(const_sg_vm_t vm)
374 return sg_vm_get_pm(vm);
376 void MSG_vm_set_bound(sg_vm_t vm, double bound)
378 sg_vm_set_bound(vm, bound);
380 void MSG_vm_start(sg_vm_t vm)
384 void MSG_vm_suspend(sg_vm_t vm)
388 void MSG_vm_resume(sg_vm_t vm)
392 void MSG_vm_shutdown(sg_vm_t vm)
396 void MSG_vm_destroy(sg_vm_t vm)
400 /********* barriers ************/
401 sg_bar_t MSG_barrier_init(unsigned int count)
403 return sg_barrier_init(count);
406 void MSG_barrier_destroy(sg_bar_t bar)
408 sg_barrier_destroy(bar);
411 int MSG_barrier_wait(sg_bar_t bar)
413 return sg_barrier_wait(bar);
416 sg_sem_t MSG_sem_init(int initial_value)
418 return sg_sem_init(initial_value);
420 void MSG_sem_acquire(sg_sem_t sem)
424 int MSG_sem_acquire_timeout(sg_sem_t sem, double timeout)
426 return sg_sem_acquire_timeout(sem, timeout);
428 void MSG_sem_release(sg_sem_t sem)
432 int MSG_sem_get_capacity(const_sg_sem_t sem)
434 return sg_sem_get_capacity(sem);
436 void MSG_sem_destroy(const_sg_sem_t sem)
440 int MSG_sem_would_block(const_sg_sem_t sem)
442 return sg_sem_would_block(sem);