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. */
14 #include "simgrid/simix/blocking_simcall.hpp"
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/activity/ConditionVariableImpl.hpp"
17 #include "src/kernel/activity/ExecImpl.hpp"
18 #include "src/kernel/activity/IoImpl.hpp"
19 #include "src/kernel/activity/MutexImpl.hpp"
20 #include "src/mc/mc_replay.hpp"
21 #include "src/plugins/vm/VirtualMachineImpl.hpp"
22 #include "src/simix/smx_host_private.hpp"
23 #include "src/simix/smx_io_private.hpp"
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
27 #include "popping_bodies.cpp"
30 * @ingroup simix_process_management
31 * @brief Creates a synchro that executes some computation of an host.
33 * This function creates a SURF action and allocates the data necessary
34 * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
36 * @param name Name of the execution synchro to create
37 * @param flops_amount amount Computation amount (in flops)
38 * @param priority computation priority
40 * @param host host where the synchro will be executed
41 * @return A new SIMIX execution synchronization
43 smx_activity_t simcall_execution_start(std::string name, std::string category, double flops_amount, double priority,
44 double bound, simgrid::s4u::Host* host)
46 /* checking for infinite values */
47 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
48 xbt_assert(std::isfinite(priority), "priority is not finite!");
50 return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
51 return SIMIX_execution_start(name, category, flops_amount, priority, bound, host);
56 * @ingroup simix_process_management
57 * @brief Creates a synchro that may involve parallel computation on
58 * several hosts and communication between them.
60 * @param name Name of the execution synchro to create
61 * @param host_nb Number of hosts where the synchro will be executed
62 * @param host_list Array (of size host_nb) of hosts where the synchro will be executed
63 * @param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
64 * @param bytes_amount Array (of size host_nb * host_nb) representing the communication
65 * amount between each pair of hosts
66 * @param rate the SURF action rate
67 * @param timeout timeout
68 * @return A new SIMIX execution synchronization
70 smx_activity_t simcall_execution_parallel_start(std::string name, int host_nb, sg_host_t* host_list,
71 double* flops_amount, double* bytes_amount, double rate, double timeout)
73 /* checking for infinite values */
74 for (int i = 0 ; i < host_nb ; ++i) {
75 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
76 if (bytes_amount != nullptr) {
77 for (int j = 0 ; j < host_nb ; ++j) {
78 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
79 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
84 xbt_assert(std::isfinite(rate), "rate is not finite!");
86 return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
87 return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
92 * @ingroup simix_process_management
93 * @brief Cancels an execution synchro.
95 * This functions stops the execution. It calls a surf function.
96 * @param execution The execution synchro to cancel
98 void simcall_execution_cancel(smx_activity_t execution)
100 simgrid::kernel::activity::ExecImplPtr exec =
101 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
102 if (exec->surf_action_ == nullptr) // FIXME: One test fails if I remove this, but I don't get why...
104 simgrid::simix::simcall([exec] { exec->cancel(); });
108 * @ingroup simix_process_management
109 * @brief Changes the priority of an execution synchro.
111 * This functions changes the priority only. It calls a surf function.
112 * @param execution The execution synchro
113 * @param priority The new priority
115 void simcall_execution_set_priority(smx_activity_t execution, double priority)
117 /* checking for infinite values */
118 xbt_assert(std::isfinite(priority), "priority is not finite!");
119 simgrid::simix::simcall([execution, priority] {
121 simgrid::kernel::activity::ExecImplPtr exec =
122 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
123 exec->set_priority(priority);
128 * @ingroup simix_process_management
129 * @brief Changes the capping (the maximum CPU utilization) of an execution synchro.
131 * This functions changes the capping only. It calls a surf function.
132 * @param execution The execution synchro
133 * @param bound The new bound
135 void simcall_execution_set_bound(smx_activity_t execution, double bound)
137 simgrid::simix::simcall([execution, bound] {
138 simgrid::kernel::activity::ExecImplPtr exec =
139 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
140 exec->set_bound(bound);
145 * @ingroup simix_host_management
146 * @brief Waits for the completion of an execution synchro and destroy it.
148 * @param execution The execution synchro
150 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
152 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
155 e_smx_state_t simcall_execution_test(smx_activity_t execution)
157 return (e_smx_state_t)simcall_BODY_execution_test(execution);
160 void simcall_process_join(smx_actor_t process, double timeout)
162 simcall_BODY_process_join(process, timeout);
166 * @ingroup simix_process_management
167 * @brief Suspends a process.
169 * This function suspends the process by suspending the synchro
170 * it was waiting for completion.
172 * @param process a SIMIX process
174 void simcall_process_suspend(smx_actor_t process)
176 simcall_BODY_process_suspend(process);
180 * @ingroup simix_process_management
181 * @brief Set the user data of a #smx_actor_t.
183 * This functions sets the user data associated to @a process.
184 * @param process SIMIX process
185 * @param data User data
187 void simcall_process_set_data(smx_actor_t process, void *data)
189 simgrid::simix::simcall([process, data] { process->set_user_data(data); });
193 * @ingroup simix_process_management
194 * @brief Set the kill time of a process.
196 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
199 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
201 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host_->get_cname());
202 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
203 simix_global->kill_process_function(process);
204 process->kill_timer=nullptr;
209 * @ingroup simix_process_management
210 * @brief Creates a new sleep SIMIX synchro.
212 * This function creates a SURF action and allocates the data necessary
213 * to create the SIMIX synchro. It can raise a host_error exception if the
214 * host crashed. The default SIMIX name of the synchro is "sleep".
216 * @param duration Time duration of the sleep.
217 * @return A result telling whether the sleep was successful
219 e_smx_state_t simcall_process_sleep(double duration)
221 /* checking for infinite values */
222 xbt_assert(std::isfinite(duration), "duration is not finite!");
223 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
227 * @ingroup simix_comm_management
229 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
230 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
231 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
233 /* checking for infinite values */
234 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
235 xbt_assert(std::isfinite(rate), "rate is not finite!");
236 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
238 xbt_assert(mbox, "No rendez-vous point defined for send");
240 if (MC_is_active() || MC_record_replay_is_active()) {
241 /* the model-checker wants two separate simcalls */
242 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
243 comm = simcall_comm_isend(sender, mbox, task_size, rate,
244 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
245 simcall_comm_wait(comm, timeout);
249 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
250 match_fun, copy_data_fun, data, timeout);
255 * @ingroup simix_comm_management
257 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
258 size_t src_buff_size,
259 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
260 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
261 void* data, int detached)
263 /* checking for infinite values */
264 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
265 xbt_assert(std::isfinite(rate), "rate is not finite!");
267 xbt_assert(mbox, "No rendez-vous point defined for isend");
269 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
270 src_buff_size, match_fun,
271 clean_fun, copy_data_fun, data, detached);
275 * @ingroup simix_comm_management
277 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
278 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
279 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
281 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
282 xbt_assert(mbox, "No rendez-vous point defined for recv");
284 if (MC_is_active() || MC_record_replay_is_active()) {
285 /* the model-checker wants two separate simcalls */
286 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
287 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
288 match_fun, copy_data_fun, data, rate);
289 simcall_comm_wait(comm, timeout);
293 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
294 match_fun, copy_data_fun, data, timeout, rate);
298 * @ingroup simix_comm_management
300 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
301 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
302 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
304 xbt_assert(mbox, "No rendez-vous point defined for irecv");
306 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
307 match_fun, copy_data_fun, data, rate);
311 * @ingroup simix_comm_management
313 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
314 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
316 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
318 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
322 * @ingroup simix_comm_management
324 void simcall_comm_cancel(smx_activity_t synchro)
326 simgrid::simix::simcall([synchro] {
327 simgrid::kernel::activity::CommImplPtr comm =
328 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
334 * @ingroup simix_comm_management
336 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
338 return simcall_BODY_comm_waitany(comms, timeout);
342 * @ingroup simix_comm_management
344 int simcall_comm_testany(smx_activity_t* comms, size_t count)
348 return simcall_BODY_comm_testany(comms, count);
352 * @ingroup simix_comm_management
354 void simcall_comm_wait(smx_activity_t comm, double timeout)
356 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
357 simcall_BODY_comm_wait(comm, timeout);
361 * @brief Set the category of an synchro.
363 * This functions changes the category only. It calls a surf function.
364 * @param synchro The execution synchro
365 * @param category The tracing category
367 void simcall_set_category(smx_activity_t synchro, std::string category)
369 if (category.empty()) {
372 simgrid::simix::simcall([synchro, category] { SIMIX_set_category(synchro, category); });
376 * @ingroup simix_comm_management
379 int simcall_comm_test(smx_activity_t comm)
381 return simcall_BODY_comm_test(comm);
385 * @ingroup simix_synchro_management
388 smx_mutex_t simcall_mutex_init()
390 if (not simix_global) {
391 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
394 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
398 * @ingroup simix_synchro_management
401 void simcall_mutex_lock(smx_mutex_t mutex)
403 simcall_BODY_mutex_lock(mutex);
407 * @ingroup simix_synchro_management
410 int simcall_mutex_trylock(smx_mutex_t mutex)
412 return simcall_BODY_mutex_trylock(mutex);
416 * @ingroup simix_synchro_management
419 void simcall_mutex_unlock(smx_mutex_t mutex)
421 simcall_BODY_mutex_unlock(mutex);
425 * @ingroup simix_synchro_management
428 smx_cond_t simcall_cond_init()
430 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
434 * @ingroup simix_synchro_management
437 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
439 simcall_BODY_cond_wait(cond, mutex);
443 * @ingroup simix_synchro_management
446 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
448 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
449 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
453 * @ingroup simix_synchro_management
456 void simcall_sem_acquire(smx_sem_t sem)
458 simcall_BODY_sem_acquire(sem);
462 * @ingroup simix_synchro_management
465 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
467 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
468 return simcall_BODY_sem_acquire_timeout(sem, timeout);
471 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
473 return simcall_BODY_storage_read(st, size);
476 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
478 return simcall_BODY_storage_write(st, size);
481 void simcall_run_kernel(std::function<void()> const& code)
483 simcall_BODY_run_kernel(&code);
486 void simcall_run_blocking(std::function<void()> const& code)
488 simcall_BODY_run_blocking(&code);
491 int simcall_mc_random(int min, int max) {
492 return simcall_BODY_mc_random(min, max);
495 /* ************************************************************************** */
497 /** @brief returns a printable string representing a simcall */
498 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
499 return simcall_names[kind];
505 void unblock(smx_actor_t process)
507 xbt_assert(SIMIX_is_maestro());
508 SIMIX_simcall_answer(&process->simcall);