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/MutexImpl.hpp"
19 #include "src/mc/mc_replay.hpp"
20 #include "src/plugins/vm/VirtualMachineImpl.hpp"
21 #include "src/simix/smx_host_private.hpp"
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
25 #include "popping_bodies.cpp"
28 * @ingroup simix_process_management
29 * @brief Creates a synchro that executes some computation of an host.
31 * This function creates a SURF action and allocates the data necessary
32 * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
34 * @param name Name of the execution synchro to create
35 * @param flops_amount amount Computation amount (in flops)
36 * @param priority computation priority
38 * @param host host where the synchro will be executed
39 * @return A new SIMIX execution synchronization
41 smx_activity_t simcall_execution_start(std::string name, std::string category, double flops_amount, double priority,
42 double bound, simgrid::s4u::Host* host)
44 /* checking for infinite values */
45 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
46 xbt_assert(std::isfinite(priority), "priority is not finite!");
48 return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
49 return SIMIX_execution_start(name, category, flops_amount, priority, bound, host);
54 * @ingroup simix_process_management
55 * @brief Creates a synchro that may involve parallel computation on
56 * several hosts and communication between them.
58 * @param name Name of the execution synchro to create
59 * @param host_nb Number of hosts where the synchro will be executed
60 * @param host_list Array (of size host_nb) of hosts where the synchro will be executed
61 * @param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
62 * @param bytes_amount Array (of size host_nb * host_nb) representing the communication
63 * amount between each pair of hosts
64 * @param rate the SURF action rate
65 * @param timeout timeout
66 * @return A new SIMIX execution synchronization
68 smx_activity_t simcall_execution_parallel_start(std::string name, int host_nb, sg_host_t* host_list,
69 double* flops_amount, double* bytes_amount, double rate, double timeout)
71 /* checking for infinite values */
72 for (int i = 0 ; i < host_nb ; ++i) {
73 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
74 if (bytes_amount != nullptr) {
75 for (int j = 0 ; j < host_nb ; ++j) {
76 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
77 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
82 xbt_assert(std::isfinite(rate), "rate is not finite!");
84 return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
85 return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
90 * @ingroup simix_process_management
91 * @brief Cancels an execution synchro.
93 * This functions stops the execution. It calls a surf function.
94 * @param execution The execution synchro to cancel
96 void simcall_execution_cancel(smx_activity_t execution)
98 simgrid::kernel::activity::ExecImplPtr exec =
99 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
100 if (exec->surf_action_ == nullptr) // FIXME: One test fails if I remove this, but I don't get why...
102 simgrid::simix::simcall([exec] { exec->cancel(); });
106 * @ingroup simix_process_management
107 * @brief Changes the priority of an execution synchro.
109 * This functions changes the priority only. It calls a surf function.
110 * @param execution The execution synchro
111 * @param priority The new priority
113 void simcall_execution_set_priority(smx_activity_t execution, double priority)
115 /* checking for infinite values */
116 xbt_assert(std::isfinite(priority), "priority is not finite!");
117 simgrid::simix::simcall([execution, priority] {
119 simgrid::kernel::activity::ExecImplPtr exec =
120 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
121 exec->set_priority(priority);
126 * @ingroup simix_process_management
127 * @brief Changes the capping (the maximum CPU utilization) of an execution synchro.
129 * This functions changes the capping only. It calls a surf function.
130 * @param execution The execution synchro
131 * @param bound The new bound
133 void simcall_execution_set_bound(smx_activity_t execution, double bound)
135 simgrid::simix::simcall([execution, bound] {
136 simgrid::kernel::activity::ExecImplPtr exec =
137 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
138 exec->set_bound(bound);
143 * @ingroup simix_host_management
144 * @brief Waits for the completion of an execution synchro and destroy it.
146 * @param execution The execution synchro
148 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
150 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
153 e_smx_state_t simcall_execution_test(smx_activity_t execution)
155 return (e_smx_state_t)simcall_BODY_execution_test(execution);
158 void simcall_process_join(smx_actor_t process, double timeout)
160 simcall_BODY_process_join(process, timeout);
164 * @ingroup simix_process_management
165 * @brief Suspends a process.
167 * This function suspends the process by suspending the synchro
168 * it was waiting for completion.
170 * @param process a SIMIX process
172 void simcall_process_suspend(smx_actor_t process)
174 simcall_BODY_process_suspend(process);
178 * @ingroup simix_process_management
179 * @brief Set the user data of a #smx_actor_t.
181 * This functions sets the user data associated to @a process.
182 * @param process SIMIX process
183 * @param data User data
185 void simcall_process_set_data(smx_actor_t process, void *data)
187 simgrid::simix::simcall([process, data] { process->set_user_data(data); });
191 * @ingroup simix_process_management
192 * @brief Set the kill time of a process.
194 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
197 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
199 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host_->get_cname());
200 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
201 simix_global->kill_process_function(process);
202 process->kill_timer=nullptr;
207 * @ingroup simix_process_management
208 * @brief Creates a new sleep SIMIX synchro.
210 * This function creates a SURF action and allocates the data necessary
211 * to create the SIMIX synchro. It can raise a host_error exception if the
212 * host crashed. The default SIMIX name of the synchro is "sleep".
214 * @param duration Time duration of the sleep.
215 * @return A result telling whether the sleep was successful
217 e_smx_state_t simcall_process_sleep(double duration)
219 /* checking for infinite values */
220 xbt_assert(std::isfinite(duration), "duration is not finite!");
221 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
225 * @ingroup simix_comm_management
227 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
228 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
229 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
231 /* checking for infinite values */
232 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
233 xbt_assert(std::isfinite(rate), "rate is not finite!");
234 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
236 xbt_assert(mbox, "No rendez-vous point defined for send");
238 if (MC_is_active() || MC_record_replay_is_active()) {
239 /* the model-checker wants two separate simcalls */
240 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
241 comm = simcall_comm_isend(sender, mbox, task_size, rate,
242 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
243 simcall_comm_wait(comm, timeout);
247 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
248 match_fun, copy_data_fun, data, timeout);
253 * @ingroup simix_comm_management
255 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
256 size_t src_buff_size,
257 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
258 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
259 void* data, int detached)
261 /* checking for infinite values */
262 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
263 xbt_assert(std::isfinite(rate), "rate is not finite!");
265 xbt_assert(mbox, "No rendez-vous point defined for isend");
267 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
268 src_buff_size, match_fun,
269 clean_fun, copy_data_fun, data, detached);
273 * @ingroup simix_comm_management
275 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
276 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
277 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
279 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
280 xbt_assert(mbox, "No rendez-vous point defined for recv");
282 if (MC_is_active() || MC_record_replay_is_active()) {
283 /* the model-checker wants two separate simcalls */
284 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
285 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
286 match_fun, copy_data_fun, data, rate);
287 simcall_comm_wait(comm, timeout);
291 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
292 match_fun, copy_data_fun, data, timeout, rate);
296 * @ingroup simix_comm_management
298 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
299 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
300 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
302 xbt_assert(mbox, "No rendez-vous point defined for irecv");
304 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
305 match_fun, copy_data_fun, data, rate);
309 * @ingroup simix_comm_management
311 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
312 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
314 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
316 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
320 * @ingroup simix_comm_management
322 void simcall_comm_cancel(smx_activity_t synchro)
324 simgrid::simix::simcall([synchro] {
325 simgrid::kernel::activity::CommImplPtr comm =
326 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
332 * @ingroup simix_comm_management
334 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
336 return simcall_BODY_comm_waitany(comms, timeout);
340 * @ingroup simix_comm_management
342 int simcall_comm_testany(smx_activity_t* comms, size_t count)
346 return simcall_BODY_comm_testany(comms, count);
350 * @ingroup simix_comm_management
352 void simcall_comm_wait(smx_activity_t comm, double timeout)
354 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
355 simcall_BODY_comm_wait(comm, timeout);
359 * @brief Set the category of an synchro.
361 * This functions changes the category only. It calls a surf function.
362 * @param synchro The execution synchro
363 * @param category The tracing category
365 void simcall_set_category(smx_activity_t synchro, std::string category)
367 if (category.empty()) {
370 simgrid::simix::simcall([synchro, category] { SIMIX_set_category(synchro, category); });
374 * @ingroup simix_comm_management
377 int simcall_comm_test(smx_activity_t comm)
379 return simcall_BODY_comm_test(comm);
383 * @ingroup simix_synchro_management
386 smx_mutex_t simcall_mutex_init()
388 if (not simix_global) {
389 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
392 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
396 * @ingroup simix_synchro_management
399 void simcall_mutex_lock(smx_mutex_t mutex)
401 simcall_BODY_mutex_lock(mutex);
405 * @ingroup simix_synchro_management
408 int simcall_mutex_trylock(smx_mutex_t mutex)
410 return simcall_BODY_mutex_trylock(mutex);
414 * @ingroup simix_synchro_management
417 void simcall_mutex_unlock(smx_mutex_t mutex)
419 simcall_BODY_mutex_unlock(mutex);
423 * @ingroup simix_synchro_management
426 smx_cond_t simcall_cond_init()
428 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
432 * @ingroup simix_synchro_management
435 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
437 simcall_BODY_cond_wait(cond, mutex);
441 * @ingroup simix_synchro_management
444 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
446 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
447 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
451 * @ingroup simix_synchro_management
454 void simcall_sem_acquire(smx_sem_t sem)
456 simcall_BODY_sem_acquire(sem);
460 * @ingroup simix_synchro_management
463 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
465 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
466 return simcall_BODY_sem_acquire_timeout(sem, timeout);
469 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
471 return simcall_BODY_storage_read(st, size);
474 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
476 return simcall_BODY_storage_write(st, size);
479 void simcall_run_kernel(std::function<void()> const& code)
481 simcall_BODY_run_kernel(&code);
484 void simcall_run_blocking(std::function<void()> const& code)
486 simcall_BODY_run_blocking(&code);
489 int simcall_mc_random(int min, int max) {
490 return simcall_BODY_mc_random(min, max);
493 /* ************************************************************************** */
495 /** @brief returns a printable string representing a simcall */
496 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
497 return simcall_names[kind];
503 void unblock(smx_actor_t process)
505 xbt_assert(SIMIX_is_maestro());
506 SIMIX_simcall_answer(&process->simcall);