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-2015. The SimGrid Team.
9 * All rights reserved. */
11 /* This program is free software; you can redistribute it and/or modify it
12 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include <cmath> /* std::isfinite() */
19 #include "simgrid/s4u/VirtualMachine.hpp"
20 #include "simgrid/simix.hpp"
21 #include "simgrid/simix/blocking_simcall.hpp"
22 #include "smx_private.h"
23 #include "src/kernel/activity/CommImpl.hpp"
24 #include "src/mc/mc_forward.hpp"
25 #include "src/mc/mc_replay.h"
26 #include "src/plugins/vm/VirtualMachineImpl.hpp"
27 #include "src/simix/smx_host_private.h"
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->cname(), 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,
61 double priority, double bound)
63 /* checking for infinite values */
64 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
65 xbt_assert(std::isfinite(priority), "priority is not finite!");
67 return simcall_BODY_execution_start(name, flops_amount, priority, bound);
71 * \ingroup simix_process_management
72 * \brief Creates a synchro that may involve parallel computation on
73 * several hosts and communication between them.
75 * \param name Name of the execution synchro to create
76 * \param host_nb Number of hosts where the synchro will be executed
77 * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
78 * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
79 * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
80 * amount between each pair of hosts
81 * \param amount the SURF action amount
82 * \param rate the SURF action rate
83 * \param timeout timeout
84 * \return A new SIMIX execution synchronization
86 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
87 double* flops_amount, double* bytes_amount, double rate, double timeout)
89 /* checking for infinite values */
90 for (int i = 0 ; i < host_nb ; ++i) {
91 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
92 if (bytes_amount != nullptr) {
93 for (int j = 0 ; j < host_nb ; ++j) {
94 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
95 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
100 xbt_assert(std::isfinite(rate), "rate is not finite!");
102 return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
106 * \ingroup simix_process_management
107 * \brief Cancels an execution synchro.
109 * This functions stops the execution. It calls a surf function.
110 * \param execution The execution synchro to cancel
112 void simcall_execution_cancel(smx_activity_t execution)
114 simcall_BODY_execution_cancel(execution);
118 * \ingroup simix_process_management
119 * \brief Changes the priority of an execution synchro.
121 * This functions changes the priority only. It calls a surf function.
122 * \param execution The execution synchro
123 * \param priority The new priority
125 void simcall_execution_set_priority(smx_activity_t execution, double priority)
127 /* checking for infinite values */
128 xbt_assert(std::isfinite(priority), "priority is not finite!");
130 simcall_BODY_execution_set_priority(execution, priority);
134 * \ingroup simix_process_management
135 * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
137 * This functions changes the capping only. It calls a surf function.
138 * \param execution The execution synchro
139 * \param bound The new bound
141 void simcall_execution_set_bound(smx_activity_t execution, double bound)
143 simcall_BODY_execution_set_bound(execution, bound);
147 * \ingroup simix_host_management
148 * \brief Waits for the completion of an execution synchro and destroy it.
150 * \param execution The execution synchro
152 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
154 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
158 * \ingroup simix_process_management
159 * \brief Kills all SIMIX processes.
161 void simcall_process_killall(int reset_pid)
163 simcall_BODY_process_killall(reset_pid);
167 * \ingroup simix_process_management
168 * \brief Cleans up a SIMIX process.
169 * \param process poor victim (must have already been killed)
171 void simcall_process_cleanup(smx_actor_t process)
173 simcall_BODY_process_cleanup(process);
176 void simcall_process_join(smx_actor_t process, double timeout)
178 simcall_BODY_process_join(process, timeout);
182 * \ingroup simix_process_management
183 * \brief Suspends a process.
185 * This function suspends the process by suspending the synchro
186 * it was waiting for completion.
188 * \param process a SIMIX process
190 void simcall_process_suspend(smx_actor_t process)
192 simcall_BODY_process_suspend(process);
196 * \ingroup simix_process_management
197 * \brief Returns the amount of SIMIX processes in the system
199 * Maestro internal process is not counted, only user code processes are
201 int simcall_process_count()
203 return simgrid::simix::kernelImmediate(SIMIX_process_count);
207 * \ingroup simix_process_management
208 * \brief Set the user data of a #smx_actor_t.
210 * This functions sets the user data associated to \a process.
211 * \param process SIMIX process
212 * \param data User data
214 void simcall_process_set_data(smx_actor_t process, void *data)
216 simgrid::simix::kernelImmediate([process, data] { process->setUserData(data); });
220 * \ingroup simix_process_management
221 * \brief Set the kill time of a process.
223 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
226 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
228 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->cname(), process->host->getCname());
229 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
230 simix_global->kill_process_function(process);
231 process->kill_timer=nullptr;
236 * \ingroup simix_process_management
237 * \brief Add an on_exit function
238 * Add an on_exit function which will be executed when the process exits/is killed.
240 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
242 simcall_BODY_process_on_exit(process, fun, data);
246 * \ingroup simix_process_management
247 * \brief Creates a new sleep SIMIX synchro.
249 * This function creates a SURF action and allocates the data necessary
250 * to create the SIMIX synchro. It can raise a host_error exception if the
251 * host crashed. The default SIMIX name of the synchro is "sleep".
253 * \param duration Time duration of the sleep.
254 * \return A result telling whether the sleep was successful
256 e_smx_state_t simcall_process_sleep(double duration)
258 /* checking for infinite values */
259 xbt_assert(std::isfinite(duration), "duration is not finite!");
260 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
264 * \ingroup simix_comm_management
266 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
267 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
268 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
270 /* checking for infinite values */
271 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
272 xbt_assert(std::isfinite(rate), "rate is not finite!");
273 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
275 xbt_assert(mbox, "No rendez-vous point defined for send");
277 if (MC_is_active() || MC_record_replay_is_active()) {
278 /* the model-checker wants two separate simcalls */
279 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
280 comm = simcall_comm_isend(sender, mbox, task_size, rate,
281 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
282 simcall_comm_wait(comm, timeout);
286 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
287 match_fun, copy_data_fun, data, timeout);
292 * \ingroup simix_comm_management
294 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
295 size_t src_buff_size,
296 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
297 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
298 void* data, int detached)
300 /* checking for infinite values */
301 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
302 xbt_assert(std::isfinite(rate), "rate is not finite!");
304 xbt_assert(mbox, "No rendez-vous point defined for isend");
306 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
307 src_buff_size, match_fun,
308 clean_fun, copy_data_fun, data, detached);
312 * \ingroup simix_comm_management
314 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
315 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
316 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
318 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
319 xbt_assert(mbox, "No rendez-vous point defined for recv");
321 if (MC_is_active() || MC_record_replay_is_active()) {
322 /* the model-checker wants two separate simcalls */
323 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
324 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
325 match_fun, copy_data_fun, data, rate);
326 simcall_comm_wait(comm, timeout);
330 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
331 match_fun, copy_data_fun, data, timeout, rate);
335 * \ingroup simix_comm_management
337 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
338 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
339 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
341 xbt_assert(mbox, "No rendez-vous point defined for irecv");
343 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
344 match_fun, copy_data_fun, data, rate);
348 * \ingroup simix_comm_management
350 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
351 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
353 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
355 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
359 * \ingroup simix_comm_management
361 void simcall_comm_cancel(smx_activity_t synchro)
363 simgrid::simix::kernelImmediate([synchro] {
364 simgrid::kernel::activity::CommImplPtr comm =
365 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
371 * \ingroup simix_comm_management
373 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
375 return simcall_BODY_comm_waitany(comms, timeout);
379 * \ingroup simix_comm_management
381 int simcall_comm_testany(smx_activity_t* comms, size_t count)
385 return simcall_BODY_comm_testany(comms, count);
389 * \ingroup simix_comm_management
391 void simcall_comm_wait(smx_activity_t comm, double timeout)
393 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
394 simcall_BODY_comm_wait(comm, timeout);
398 * \brief Set the category of an synchro.
400 * This functions changes the category only. It calls a surf function.
401 * \param synchro The execution synchro
402 * \param category The tracing category
404 void simcall_set_category(smx_activity_t synchro, const char *category)
406 if (category == nullptr) {
409 simcall_BODY_set_category(synchro, category);
413 * \ingroup simix_comm_management
416 int simcall_comm_test(smx_activity_t comm)
418 return simcall_BODY_comm_test(comm);
422 * \ingroup simix_synchro_management
425 smx_mutex_t simcall_mutex_init()
427 if (not simix_global) {
428 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
431 return simgrid::simix::kernelImmediate([] { return new simgrid::simix::MutexImpl(); });
435 * \ingroup simix_synchro_management
438 void simcall_mutex_lock(smx_mutex_t mutex)
440 simcall_BODY_mutex_lock(mutex);
444 * \ingroup simix_synchro_management
447 int simcall_mutex_trylock(smx_mutex_t mutex)
449 return simcall_BODY_mutex_trylock(mutex);
453 * \ingroup simix_synchro_management
456 void simcall_mutex_unlock(smx_mutex_t mutex)
458 simcall_BODY_mutex_unlock(mutex);
462 * \ingroup simix_synchro_management
465 smx_cond_t simcall_cond_init()
467 return simcall_BODY_cond_init();
471 * \ingroup simix_synchro_management
474 void simcall_cond_signal(smx_cond_t cond)
476 simcall_BODY_cond_signal(cond);
480 * \ingroup simix_synchro_management
483 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
485 simcall_BODY_cond_wait(cond, mutex);
489 * \ingroup simix_synchro_management
492 void simcall_cond_wait_timeout(smx_cond_t cond,
496 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
497 simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
501 * \ingroup simix_synchro_management
504 void simcall_cond_broadcast(smx_cond_t cond)
506 simcall_BODY_cond_broadcast(cond);
510 * \ingroup simix_synchro_management
513 void simcall_sem_acquire(smx_sem_t sem)
515 simcall_BODY_sem_acquire(sem);
519 * \ingroup simix_synchro_management
522 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
524 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
525 simcall_BODY_sem_acquire_timeout(sem, timeout);
529 * \ingroup simix_file_management
532 sg_size_t simcall_file_read(surf_file_t fd, sg_size_t size)
534 return simcall_BODY_file_read(fd, size);
538 * \ingroup simix_file_management
541 sg_size_t simcall_file_write(surf_file_t fd, sg_size_t size)
543 return simcall_BODY_file_write(fd, size);
546 void simcall_run_kernel(std::function<void()> const& code)
548 simcall_BODY_run_kernel(&code);
551 void simcall_run_blocking(std::function<void()> const& code)
553 simcall_BODY_run_blocking(&code);
556 int simcall_mc_random(int min, int max) {
557 return simcall_BODY_mc_random(min, max);
560 /* ************************************************************************** */
562 /** @brief returns a printable string representing a simcall */
563 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
564 return simcall_names[kind];
570 void unblock(smx_actor_t process)
572 xbt_assert(SIMIX_is_maestro());
573 SIMIX_simcall_answer(&process->simcall);