1 /* libsmx.c - public interface to simix */
3 /* These functions are the only ones that are visible from the higher levels */
4 /* (most of them simply add some documentation to the generated simcall body) */
6 /* This is somehow the "libc" of SimGrid */
8 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved. */
10 /* This program is free software; you can redistribute it and/or modify it
11 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <cmath> /* std::isfinite() */
18 #include "simgrid/s4u/VirtualMachine.hpp"
19 #include "simgrid/simix.hpp"
20 #include "simgrid/simix/blocking_simcall.hpp"
21 #include "smx_private.hpp"
22 #include "src/kernel/activity/CommImpl.hpp"
23 #include "src/kernel/activity/ConditionVariableImpl.hpp"
24 #include "src/kernel/activity/MutexImpl.hpp"
25 #include "src/mc/mc_forward.hpp"
26 #include "src/mc/mc_replay.hpp"
27 #include "src/plugins/vm/VirtualMachineImpl.hpp"
28 #include "src/simix/smx_host_private.hpp"
30 #include "xbt/functional.hpp"
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
34 #include "popping_bodies.cpp"
36 void simcall_call(smx_actor_t actor)
38 if (actor != simix_global->maestro_process) {
39 XBT_DEBUG("Yield actor '%s' on simcall %s (%d)", actor->get_cname(), SIMIX_simcall_name(actor->simcall.call),
40 (int)actor->simcall.call);
41 SIMIX_process_yield(actor);
43 SIMIX_simcall_handle(&actor->simcall, 0);
48 * \ingroup simix_process_management
49 * \brief Creates a synchro that executes some computation of an host.
51 * This function creates a SURF action and allocates the data necessary
52 * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
54 * \param name Name of the execution synchro to create
55 * \param flops_amount amount Computation amount (in flops)
56 * \param priority computation priority
58 * \param host host where the synchro will be executed
59 * \return A new SIMIX execution synchronization
61 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
62 simgrid::s4u::Host* host)
64 /* checking for infinite values */
65 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
66 xbt_assert(std::isfinite(priority), "priority is not finite!");
68 return simgrid::simix::kernelImmediate([name, flops_amount, priority, bound, host] {
69 return SIMIX_execution_start(name, flops_amount, priority, bound, host);
74 * \ingroup simix_process_management
75 * \brief Creates a synchro that may involve parallel computation on
76 * several hosts and communication between them.
78 * \param name Name of the execution synchro to create
79 * \param host_nb Number of hosts where the synchro will be executed
80 * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
81 * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
82 * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
83 * amount between each pair of hosts
84 * \param rate the SURF action rate
85 * \param timeout timeout
86 * \return A new SIMIX execution synchronization
88 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
89 double* flops_amount, double* bytes_amount, double rate, double timeout)
91 /* checking for infinite values */
92 for (int i = 0 ; i < host_nb ; ++i) {
93 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
94 if (bytes_amount != nullptr) {
95 for (int j = 0 ; j < host_nb ; ++j) {
96 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
97 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
102 xbt_assert(std::isfinite(rate), "rate is not finite!");
104 return simgrid::simix::kernelImmediate([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
105 return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
110 * \ingroup simix_process_management
111 * \brief Cancels an execution synchro.
113 * This functions stops the execution. It calls a surf function.
114 * \param execution The execution synchro to cancel
116 void simcall_execution_cancel(smx_activity_t execution)
118 simgrid::kernel::activity::ExecImplPtr exec =
119 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
120 if (exec->surf_action_ == nullptr) // FIXME: One test fails if I remove this, but I don't get why...
122 simgrid::simix::kernelImmediate([exec] {
128 * \ingroup simix_process_management
129 * \brief Changes the priority of an execution synchro.
131 * This functions changes the priority only. It calls a surf function.
132 * \param execution The execution synchro
133 * \param priority The new priority
135 void simcall_execution_set_priority(smx_activity_t execution, double priority)
137 /* checking for infinite values */
138 xbt_assert(std::isfinite(priority), "priority is not finite!");
139 simgrid::simix::kernelImmediate([execution, priority] {
141 simgrid::kernel::activity::ExecImplPtr exec =
142 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
143 exec->set_priority(priority);
148 * \ingroup simix_process_management
149 * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
151 * This functions changes the capping only. It calls a surf function.
152 * \param execution The execution synchro
153 * \param bound The new bound
155 void simcall_execution_set_bound(smx_activity_t execution, double bound)
157 simgrid::simix::kernelImmediate([execution, bound] {
158 simgrid::kernel::activity::ExecImplPtr exec =
159 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
160 exec->set_bound(bound);
165 * \ingroup simix_host_management
166 * \brief Waits for the completion of an execution synchro and destroy it.
168 * \param execution The execution synchro
170 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
172 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
175 e_smx_state_t simcall_execution_test(smx_activity_t execution)
177 return (e_smx_state_t)simcall_BODY_execution_test(execution);
180 void simcall_process_join(smx_actor_t process, double timeout)
182 simcall_BODY_process_join(process, timeout);
186 * \ingroup simix_process_management
187 * \brief Suspends a process.
189 * This function suspends the process by suspending the synchro
190 * it was waiting for completion.
192 * \param process a SIMIX process
194 void simcall_process_suspend(smx_actor_t process)
196 simcall_BODY_process_suspend(process);
200 * \ingroup simix_process_management
201 * \brief Set the user data of a #smx_actor_t.
203 * This functions sets the user data associated to \a process.
204 * \param process SIMIX process
205 * \param data User data
207 void simcall_process_set_data(smx_actor_t process, void *data)
209 simgrid::simix::kernelImmediate([process, data] { process->setUserData(data); });
213 * \ingroup simix_process_management
214 * \brief Set the kill time of a process.
216 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
219 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
221 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host->get_cname());
222 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
223 simix_global->kill_process_function(process);
224 process->kill_timer=nullptr;
229 * \ingroup simix_process_management
230 * \brief Creates a new sleep SIMIX synchro.
232 * This function creates a SURF action and allocates the data necessary
233 * to create the SIMIX synchro. It can raise a host_error exception if the
234 * host crashed. The default SIMIX name of the synchro is "sleep".
236 * \param duration Time duration of the sleep.
237 * \return A result telling whether the sleep was successful
239 e_smx_state_t simcall_process_sleep(double duration)
241 /* checking for infinite values */
242 xbt_assert(std::isfinite(duration), "duration is not finite!");
243 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
247 * \ingroup simix_comm_management
249 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
250 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
251 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
253 /* checking for infinite values */
254 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
255 xbt_assert(std::isfinite(rate), "rate is not finite!");
256 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
258 xbt_assert(mbox, "No rendez-vous point defined for send");
260 if (MC_is_active() || MC_record_replay_is_active()) {
261 /* the model-checker wants two separate simcalls */
262 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
263 comm = simcall_comm_isend(sender, mbox, task_size, rate,
264 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
265 simcall_comm_wait(comm, timeout);
269 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
270 match_fun, copy_data_fun, data, timeout);
275 * \ingroup simix_comm_management
277 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
278 size_t src_buff_size,
279 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
280 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
281 void* data, int detached)
283 /* checking for infinite values */
284 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
285 xbt_assert(std::isfinite(rate), "rate is not finite!");
287 xbt_assert(mbox, "No rendez-vous point defined for isend");
289 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
290 src_buff_size, match_fun,
291 clean_fun, copy_data_fun, data, detached);
295 * \ingroup simix_comm_management
297 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
298 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
299 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
301 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
302 xbt_assert(mbox, "No rendez-vous point defined for recv");
304 if (MC_is_active() || MC_record_replay_is_active()) {
305 /* the model-checker wants two separate simcalls */
306 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
307 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
308 match_fun, copy_data_fun, data, rate);
309 simcall_comm_wait(comm, timeout);
313 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
314 match_fun, copy_data_fun, data, timeout, rate);
318 * \ingroup simix_comm_management
320 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
321 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
322 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
324 xbt_assert(mbox, "No rendez-vous point defined for irecv");
326 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
327 match_fun, copy_data_fun, data, rate);
331 * \ingroup simix_comm_management
333 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
334 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
336 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
338 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
342 * \ingroup simix_comm_management
344 void simcall_comm_cancel(smx_activity_t synchro)
346 simgrid::simix::kernelImmediate([synchro] {
347 simgrid::kernel::activity::CommImplPtr comm =
348 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
354 * \ingroup simix_comm_management
356 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
358 return simcall_BODY_comm_waitany(comms, timeout);
362 * \ingroup simix_comm_management
364 int simcall_comm_testany(smx_activity_t* comms, size_t count)
368 return simcall_BODY_comm_testany(comms, count);
372 * \ingroup simix_comm_management
374 void simcall_comm_wait(smx_activity_t comm, double timeout)
376 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
377 simcall_BODY_comm_wait(comm, timeout);
381 * \brief Set the category of an synchro.
383 * This functions changes the category only. It calls a surf function.
384 * \param synchro The execution synchro
385 * \param category The tracing category
387 void simcall_set_category(smx_activity_t synchro, const char *category)
389 if (category == nullptr) {
392 simgrid::simix::kernelImmediate([synchro, category] { SIMIX_set_category(synchro, category); });
396 * \ingroup simix_comm_management
399 int simcall_comm_test(smx_activity_t comm)
401 return simcall_BODY_comm_test(comm);
405 * \ingroup simix_synchro_management
408 smx_mutex_t simcall_mutex_init()
410 if (not simix_global) {
411 fprintf(stderr,"You must run MSG_init before using MSG\n"); // We can't use xbt_die since we may get there before the initialization
414 return simgrid::simix::kernelImmediate([] { return new simgrid::kernel::activity::MutexImpl(); });
418 * \ingroup simix_synchro_management
421 void simcall_mutex_lock(smx_mutex_t mutex)
423 simcall_BODY_mutex_lock(mutex);
427 * \ingroup simix_synchro_management
430 int simcall_mutex_trylock(smx_mutex_t mutex)
432 return simcall_BODY_mutex_trylock(mutex);
436 * \ingroup simix_synchro_management
439 void simcall_mutex_unlock(smx_mutex_t mutex)
441 simcall_BODY_mutex_unlock(mutex);
445 * \ingroup simix_synchro_management
448 smx_cond_t simcall_cond_init()
450 return simgrid::simix::kernelImmediate([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
454 * \ingroup simix_synchro_management
457 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
459 simcall_BODY_cond_wait(cond, mutex);
463 * \ingroup simix_synchro_management
466 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
468 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
469 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
473 * \ingroup simix_synchro_management
476 void simcall_sem_acquire(smx_sem_t sem)
478 simcall_BODY_sem_acquire(sem);
482 * \ingroup simix_synchro_management
485 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
487 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
488 return simcall_BODY_sem_acquire_timeout(sem, timeout);
491 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
493 return simcall_BODY_storage_read(st, size);
496 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
498 return simcall_BODY_storage_write(st, size);
501 void simcall_run_kernel(std::function<void()> const& code)
503 simcall_BODY_run_kernel(&code);
506 void simcall_run_blocking(std::function<void()> const& code)
508 simcall_BODY_run_blocking(&code);
511 int simcall_mc_random(int min, int max) {
512 return simcall_BODY_mc_random(min, max);
515 /* ************************************************************************** */
517 /** @brief returns a printable string representing a simcall */
518 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
519 return simcall_names[kind];
525 void unblock(smx_actor_t process)
527 xbt_assert(SIMIX_is_maestro());
528 SIMIX_simcall_answer(&process->simcall);