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-2017. 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/MutexImpl.hpp"
24 #include "src/mc/mc_forward.hpp"
25 #include "src/mc/mc_replay.hpp"
26 #include "src/plugins/vm/VirtualMachineImpl.hpp"
27 #include "src/simix/smx_host_private.hpp"
29 #include "xbt/functional.hpp"
31 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
33 #include "popping_bodies.cpp"
35 void simcall_call(smx_actor_t actor)
37 if (actor != simix_global->maestro_process) {
38 XBT_DEBUG("Yield actor '%s' on simcall %s (%d)", actor->getCname(), SIMIX_simcall_name(actor->simcall.call),
39 (int)actor->simcall.call);
40 SIMIX_process_yield(actor);
42 SIMIX_simcall_handle(&actor->simcall, 0);
47 * \ingroup simix_process_management
48 * \brief Creates a synchro that executes some computation of an host.
50 * This function creates a SURF action and allocates the data necessary
51 * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
53 * \param name Name of the execution synchro to create
54 * \param flops_amount amount Computation amount (in flops)
55 * \param priority computation priority
57 * \return A new SIMIX execution synchronization
59 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
60 simgrid::s4u::Host* host)
62 /* checking for infinite values */
63 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
64 xbt_assert(std::isfinite(priority), "priority is not finite!");
66 return simcall_BODY_execution_start(name, flops_amount, priority, bound, host);
70 * \ingroup simix_process_management
71 * \brief Creates a synchro that may involve parallel computation on
72 * several hosts and communication between them.
74 * \param name Name of the execution synchro to create
75 * \param host_nb Number of hosts where the synchro will be executed
76 * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
77 * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
78 * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
79 * amount between each pair of hosts
80 * \param amount the SURF action amount
81 * \param rate the SURF action rate
82 * \param timeout timeout
83 * \return A new SIMIX execution synchronization
85 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
86 double* flops_amount, double* bytes_amount, double rate, double timeout)
88 /* checking for infinite values */
89 for (int i = 0 ; i < host_nb ; ++i) {
90 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
91 if (bytes_amount != nullptr) {
92 for (int j = 0 ; j < host_nb ; ++j) {
93 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
94 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
99 xbt_assert(std::isfinite(rate), "rate is not finite!");
101 return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
105 * \ingroup simix_process_management
106 * \brief Cancels an execution synchro.
108 * This functions stops the execution. It calls a surf function.
109 * \param execution The execution synchro to cancel
111 void simcall_execution_cancel(smx_activity_t execution)
113 simgrid::kernel::activity::ExecImplPtr exec =
114 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
115 if (not exec->surfAction_)
117 simgrid::simix::kernelImmediate([exec] {
118 XBT_DEBUG("Cancel synchro %p", exec.get());
119 if (exec->surfAction_)
120 exec->surfAction_->cancel();
125 * \ingroup simix_process_management
126 * \brief Changes the priority of an execution synchro.
128 * This functions changes the priority only. It calls a surf function.
129 * \param execution The execution synchro
130 * \param priority The new priority
132 void simcall_execution_set_priority(smx_activity_t execution, double priority)
134 /* checking for infinite values */
135 xbt_assert(std::isfinite(priority), "priority is not finite!");
136 simgrid::simix::kernelImmediate([execution, priority] {
138 simgrid::kernel::activity::ExecImplPtr exec =
139 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
140 if (exec->surfAction_)
141 exec->surfAction_->setSharingWeight(priority);
146 * \ingroup simix_process_management
147 * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
149 * This functions changes the capping only. It calls a surf function.
150 * \param execution The execution synchro
151 * \param bound The new bound
153 void simcall_execution_set_bound(smx_activity_t execution, double bound)
155 simgrid::simix::kernelImmediate([execution, bound] {
156 simgrid::kernel::activity::ExecImplPtr exec =
157 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
158 if (exec->surfAction_)
159 exec->surfAction_->setBound(bound);
164 * \ingroup simix_host_management
165 * \brief Waits for the completion of an execution synchro and destroy it.
167 * \param execution The execution synchro
169 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
171 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
174 e_smx_state_t simcall_execution_test(smx_activity_t execution)
176 return (e_smx_state_t)simcall_BODY_execution_test(execution);
180 * \ingroup simix_process_management
181 * \brief Kills all SIMIX processes.
183 void simcall_process_killall(int reset_pid)
185 simcall_BODY_process_killall(reset_pid);
189 * \ingroup simix_process_management
190 * \brief Cleans up a SIMIX process.
191 * \param process poor victim (must have already been killed)
193 void simcall_process_cleanup(smx_actor_t process)
195 simcall_BODY_process_cleanup(process);
198 void simcall_process_join(smx_actor_t process, double timeout)
200 simcall_BODY_process_join(process, timeout);
204 * \ingroup simix_process_management
205 * \brief Suspends a process.
207 * This function suspends the process by suspending the synchro
208 * it was waiting for completion.
210 * \param process a SIMIX process
212 void simcall_process_suspend(smx_actor_t process)
214 simcall_BODY_process_suspend(process);
218 * \ingroup simix_process_management
219 * \brief Returns the amount of SIMIX processes in the system
221 * Maestro internal process is not counted, only user code processes are
223 int simcall_process_count()
225 return simgrid::simix::kernelImmediate(SIMIX_process_count);
229 * \ingroup simix_process_management
230 * \brief Set the user data of a #smx_actor_t.
232 * This functions sets the user data associated to \a process.
233 * \param process SIMIX process
234 * \param data User data
236 void simcall_process_set_data(smx_actor_t process, void *data)
238 simgrid::simix::kernelImmediate([process, data] { process->setUserData(data); });
242 * \ingroup simix_process_management
243 * \brief Set the kill time of a process.
245 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
248 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
250 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->getCname(), process->host->getCname());
251 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
252 simix_global->kill_process_function(process);
253 process->kill_timer=nullptr;
258 * \ingroup simix_process_management
259 * \brief Add an on_exit function
260 * Add an on_exit function which will be executed when the process exits/is killed.
262 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
264 simcall_BODY_process_on_exit(process, fun, data);
268 * \ingroup simix_process_management
269 * \brief Creates a new sleep SIMIX synchro.
271 * This function creates a SURF action and allocates the data necessary
272 * to create the SIMIX synchro. It can raise a host_error exception if the
273 * host crashed. The default SIMIX name of the synchro is "sleep".
275 * \param duration Time duration of the sleep.
276 * \return A result telling whether the sleep was successful
278 e_smx_state_t simcall_process_sleep(double duration)
280 /* checking for infinite values */
281 xbt_assert(std::isfinite(duration), "duration is not finite!");
282 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
286 * \ingroup simix_comm_management
288 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
289 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
290 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
292 /* checking for infinite values */
293 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
294 xbt_assert(std::isfinite(rate), "rate is not finite!");
295 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
297 xbt_assert(mbox, "No rendez-vous point defined for send");
299 if (MC_is_active() || MC_record_replay_is_active()) {
300 /* the model-checker wants two separate simcalls */
301 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
302 comm = simcall_comm_isend(sender, mbox, task_size, rate,
303 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
304 simcall_comm_wait(comm, timeout);
308 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
309 match_fun, copy_data_fun, data, timeout);
314 * \ingroup simix_comm_management
316 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
317 size_t src_buff_size,
318 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
319 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
320 void* data, int detached)
322 /* checking for infinite values */
323 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
324 xbt_assert(std::isfinite(rate), "rate is not finite!");
326 xbt_assert(mbox, "No rendez-vous point defined for isend");
328 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
329 src_buff_size, match_fun,
330 clean_fun, copy_data_fun, data, detached);
334 * \ingroup simix_comm_management
336 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
337 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
338 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
340 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
341 xbt_assert(mbox, "No rendez-vous point defined for recv");
343 if (MC_is_active() || MC_record_replay_is_active()) {
344 /* the model-checker wants two separate simcalls */
345 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
346 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
347 match_fun, copy_data_fun, data, rate);
348 simcall_comm_wait(comm, timeout);
352 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
353 match_fun, copy_data_fun, data, timeout, rate);
357 * \ingroup simix_comm_management
359 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
360 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
361 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
363 xbt_assert(mbox, "No rendez-vous point defined for irecv");
365 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
366 match_fun, copy_data_fun, data, rate);
370 * \ingroup simix_comm_management
372 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
373 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
375 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
377 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
381 * \ingroup simix_comm_management
383 void simcall_comm_cancel(smx_activity_t synchro)
385 simgrid::simix::kernelImmediate([synchro] {
386 simgrid::kernel::activity::CommImplPtr comm =
387 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
393 * \ingroup simix_comm_management
395 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
397 return simcall_BODY_comm_waitany(comms, timeout);
401 * \ingroup simix_comm_management
403 int simcall_comm_testany(smx_activity_t* comms, size_t count)
407 return simcall_BODY_comm_testany(comms, count);
411 * \ingroup simix_comm_management
413 void simcall_comm_wait(smx_activity_t comm, double timeout)
415 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
416 simcall_BODY_comm_wait(comm, timeout);
420 * \brief Set the category of an synchro.
422 * This functions changes the category only. It calls a surf function.
423 * \param synchro The execution synchro
424 * \param category The tracing category
426 void simcall_set_category(smx_activity_t synchro, const char *category)
428 if (category == nullptr) {
431 simcall_BODY_set_category(synchro, category);
435 * \ingroup simix_comm_management
438 int simcall_comm_test(smx_activity_t comm)
440 return simcall_BODY_comm_test(comm);
444 * \ingroup simix_synchro_management
447 smx_mutex_t simcall_mutex_init()
449 if (not simix_global) {
450 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
453 return simgrid::simix::kernelImmediate([] { return new simgrid::kernel::activity::MutexImpl(); });
457 * \ingroup simix_synchro_management
460 void simcall_mutex_lock(smx_mutex_t mutex)
462 simcall_BODY_mutex_lock(mutex);
466 * \ingroup simix_synchro_management
469 int simcall_mutex_trylock(smx_mutex_t mutex)
471 return simcall_BODY_mutex_trylock(mutex);
475 * \ingroup simix_synchro_management
478 void simcall_mutex_unlock(smx_mutex_t mutex)
480 simcall_BODY_mutex_unlock(mutex);
484 * \ingroup simix_synchro_management
487 smx_cond_t simcall_cond_init()
489 return simcall_BODY_cond_init();
493 * \ingroup simix_synchro_management
496 void simcall_cond_signal(smx_cond_t cond)
498 simcall_BODY_cond_signal(cond);
502 * \ingroup simix_synchro_management
505 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
507 simcall_BODY_cond_wait(cond, mutex);
511 * \ingroup simix_synchro_management
514 void simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
516 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
517 simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
521 * \ingroup simix_synchro_management
524 void simcall_cond_broadcast(smx_cond_t cond)
526 simcall_BODY_cond_broadcast(cond);
530 * \ingroup simix_synchro_management
533 void simcall_sem_acquire(smx_sem_t sem)
535 simcall_BODY_sem_acquire(sem);
539 * \ingroup simix_synchro_management
542 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
544 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
545 simcall_BODY_sem_acquire_timeout(sem, timeout);
548 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
550 return simcall_BODY_storage_read(st, size);
553 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
555 return simcall_BODY_storage_write(st, size);
558 void simcall_run_kernel(std::function<void()> const& code)
560 simcall_BODY_run_kernel(&code);
563 void simcall_run_blocking(std::function<void()> const& code)
565 simcall_BODY_run_blocking(&code);
568 int simcall_mc_random(int min, int max) {
569 return simcall_BODY_mc_random(min, max);
572 /* ************************************************************************** */
574 /** @brief returns a printable string representing a simcall */
575 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
576 return simcall_names[kind];
582 void unblock(smx_actor_t process)
584 xbt_assert(SIMIX_is_maestro());
585 SIMIX_simcall_answer(&process->simcall);