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() */
18 #include <xbt/functional.hpp>
20 #include <simgrid/s4u/VirtualMachine.hpp>
21 #include <simgrid/simix/blocking_simcall.hpp>
24 #include "smx_private.h"
25 #include "src/kernel/activity/SynchroComm.hpp"
26 #include "src/mc/mc_forward.hpp"
27 #include "src/mc/mc_replay.h"
28 #include "src/plugins/vm/VirtualMachineImpl.hpp"
29 #include "src/simix/smx_host_private.h"
32 #include <simgrid/simix.hpp>
34 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
36 #include "popping_bodies.cpp"
38 void simcall_call(smx_actor_t actor)
40 if (actor != simix_global->maestro_process) {
41 XBT_DEBUG("Yield actor '%s' on simcall %s (%d)", actor->cname(), SIMIX_simcall_name(actor->simcall.call),
42 (int)actor->simcall.call);
43 SIMIX_process_yield(actor);
45 SIMIX_simcall_handle(&actor->simcall, 0);
50 * \ingroup simix_process_management
51 * \brief Creates a synchro that executes some computation of an host.
53 * This function creates a SURF action and allocates the data necessary
54 * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
56 * \param name Name of the execution synchro to create
57 * \param flops_amount amount Computation amount (in flops)
58 * \param priority computation priority
60 * \return A new SIMIX execution synchronization
62 smx_activity_t simcall_execution_start(const char *name,
64 double priority, double bound)
66 /* checking for infinite values */
67 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
68 xbt_assert(std::isfinite(priority), "priority is not finite!");
70 return simcall_BODY_execution_start(name, flops_amount, priority, bound);
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 amount the SURF action amount
85 * \param rate the SURF action rate
86 * \param timeout timeout
87 * \return A new SIMIX execution synchronization
89 smx_activity_t simcall_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list,
90 double* flops_amount, double* bytes_amount, double amount, double rate,
94 /* checking for infinite values */
95 for (i = 0 ; i < host_nb ; ++i) {
96 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
97 if (bytes_amount != nullptr) {
98 for (j = 0 ; j < host_nb ; ++j) {
99 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
100 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
105 xbt_assert(std::isfinite(amount), "amount is not finite!");
106 xbt_assert(std::isfinite(rate), "rate is not finite!");
108 return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, amount, rate,
113 * \ingroup simix_process_management
114 * \brief Cancels an execution synchro.
116 * This functions stops the execution. It calls a surf function.
117 * \param execution The execution synchro to cancel
119 void simcall_execution_cancel(smx_activity_t execution)
121 simcall_BODY_execution_cancel(execution);
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!");
137 simcall_BODY_execution_set_priority(execution, priority);
141 * \ingroup simix_process_management
142 * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
144 * This functions changes the capping only. It calls a surf function.
145 * \param execution The execution synchro
146 * \param bound The new bound
148 void simcall_execution_set_bound(smx_activity_t execution, double bound)
150 simcall_BODY_execution_set_bound(execution, bound);
154 * \ingroup simix_host_management
155 * \brief Waits for the completion of an execution synchro and destroy it.
157 * \param execution The execution synchro
159 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
161 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
165 * \ingroup simix_process_management
166 * \brief Kills a SIMIX process.
168 * This function simply kills a process.
170 * \param process poor victim
172 void simcall_process_kill(smx_actor_t process)
174 simcall_BODY_process_kill(process);
178 * \ingroup simix_process_management
179 * \brief Kills all SIMIX processes.
181 void simcall_process_killall(int reset_pid)
183 simcall_BODY_process_killall(reset_pid);
187 * \ingroup simix_process_management
188 * \brief Cleans up a SIMIX process.
189 * \param process poor victim (must have already been killed)
191 void simcall_process_cleanup(smx_actor_t process)
193 simcall_BODY_process_cleanup(process);
197 * \ingroup simix_process_management
198 * \brief Migrates an agent to another location.
200 * This function changes the value of the host on which \a process is running.
202 * \param process the process to migrate
203 * \param dest name of the new host
205 void simcall_process_set_host(smx_actor_t process, sg_host_t dest)
207 simcall_BODY_process_set_host(process, dest);
210 void simcall_process_join(smx_actor_t process, double timeout)
212 simcall_BODY_process_join(process, timeout);
216 * \ingroup simix_process_management
217 * \brief Suspends a process.
219 * This function suspends the process by suspending the synchro
220 * it was waiting for completion.
222 * \param process a SIMIX process
224 void simcall_process_suspend(smx_actor_t process)
226 simcall_BODY_process_suspend(process);
230 * \ingroup simix_process_management
231 * \brief Resumes a suspended process.
233 * This function resumes a suspended process by resuming the synchro
234 * it was waiting for completion.
236 * \param process a SIMIX process
238 void simcall_process_resume(smx_actor_t process)
240 simcall_BODY_process_resume(process);
244 * \ingroup simix_process_management
245 * \brief Returns the amount of SIMIX processes in the system
247 * Maestro internal process is not counted, only user code processes are
249 int simcall_process_count()
251 return simgrid::simix::kernelImmediate(SIMIX_process_count);
255 * \ingroup simix_process_management
256 * \brief Set the user data of a #smx_actor_t.
258 * This functions sets the user data associated to \a process.
259 * \param process SIMIX process
260 * \param data User data
262 void simcall_process_set_data(smx_actor_t process, void *data)
264 simgrid::simix::kernelImmediate(std::bind(SIMIX_process_set_data, process, data));
268 * \ingroup simix_process_management
269 * \brief Set the kill time of a process.
271 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
274 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
276 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->cname(), process->host->cname());
277 process->kill_timer = SIMIX_timer_set(kill_time, [=] {
278 simix_global->kill_process_function(process);
279 process->kill_timer=nullptr;
283 * \ingroup simix_process_management
284 * \brief Get the kill time of a process (or 0 if unset).
286 double simcall_process_get_kill_time(smx_actor_t process) {
287 return SIMIX_timer_get_date(process->kill_timer);
291 * \ingroup simix_process_management
292 * \brief Returns true if the process is suspended .
294 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
295 * \param process SIMIX process
296 * \return 1, if the process is suspended, else 0.
298 int simcall_process_is_suspended(smx_actor_t process)
300 return simcall_BODY_process_is_suspended(process);
304 * \ingroup simix_process_management
305 * \brief Return the properties
307 * This functions returns the properties associated with this process
309 xbt_dict_t simcall_process_get_properties(smx_actor_t process)
311 return SIMIX_process_get_properties(process);
314 * \ingroup simix_process_management
315 * \brief Add an on_exit function
316 * Add an on_exit function which will be executed when the process exits/is killed.
318 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
320 simcall_BODY_process_on_exit(process, fun, data);
323 * \ingroup simix_process_management
324 * \brief Sets the process to be auto-restarted or not by SIMIX when its host comes back up.
325 * Will restart the process when the host comes back up if auto_restart is set to 1.
328 XBT_PUBLIC(void) simcall_process_auto_restart_set(smx_actor_t process, int auto_restart)
330 simcall_BODY_process_auto_restart_set(process, auto_restart);
334 * \ingroup simix_process_management
335 * \brief Restarts the process, killing it and starting it again from scratch.
337 XBT_PUBLIC(smx_actor_t) simcall_process_restart(smx_actor_t process)
339 return (smx_actor_t) simcall_BODY_process_restart(process);
342 * \ingroup simix_process_management
343 * \brief Creates a new sleep SIMIX synchro.
345 * This function creates a SURF action and allocates the data necessary
346 * to create the SIMIX synchro. It can raise a host_error exception if the
347 * host crashed. The default SIMIX name of the synchro is "sleep".
349 * \param duration Time duration of the sleep.
350 * \return A result telling whether the sleep was successful
352 e_smx_state_t simcall_process_sleep(double duration)
354 /* checking for infinite values */
355 xbt_assert(std::isfinite(duration), "duration is not finite!");
356 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
360 * \ingroup simix_comm_management
362 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate,
363 void *src_buff, size_t src_buff_size,
364 int (*match_fun)(void *, void *, smx_activity_t),
365 void (*copy_data_fun)(smx_activity_t, void*, size_t), void *data,
368 /* checking for infinite values */
369 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
370 xbt_assert(std::isfinite(rate), "rate is not finite!");
371 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
373 xbt_assert(mbox, "No rendez-vous point defined for send");
375 if (MC_is_active() || MC_record_replay_is_active()) {
376 /* the model-checker wants two separate simcalls */
377 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
378 comm = simcall_comm_isend(sender, mbox, task_size, rate,
379 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
380 simcall_comm_wait(comm, timeout);
384 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
385 match_fun, copy_data_fun, data, timeout);
390 * \ingroup simix_comm_management
392 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate,
393 void *src_buff, size_t src_buff_size,
394 int (*match_fun)(void *, void *, smx_activity_t),
395 void (*clean_fun)(void *),
396 void (*copy_data_fun)(smx_activity_t, void*, size_t),
400 /* checking for infinite values */
401 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
402 xbt_assert(std::isfinite(rate), "rate is not finite!");
404 xbt_assert(mbox, "No rendez-vous point defined for isend");
406 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
407 src_buff_size, match_fun,
408 clean_fun, copy_data_fun, data, detached);
412 * \ingroup simix_comm_management
414 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void *dst_buff, size_t * dst_buff_size,
415 int (*match_fun)(void *, void *, smx_activity_t),
416 void (*copy_data_fun)(smx_activity_t, void*, size_t),
417 void *data, double timeout, double rate)
419 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
420 xbt_assert(mbox, "No rendez-vous point defined for recv");
422 if (MC_is_active() || MC_record_replay_is_active()) {
423 /* the model-checker wants two separate simcalls */
424 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
425 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
426 match_fun, copy_data_fun, data, rate);
427 simcall_comm_wait(comm, timeout);
431 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
432 match_fun, copy_data_fun, data, timeout, rate);
436 * \ingroup simix_comm_management
438 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void *dst_buff, size_t *dst_buff_size,
439 int (*match_fun)(void *, void *, smx_activity_t),
440 void (*copy_data_fun)(smx_activity_t, void*, size_t),
441 void *data, double rate)
443 xbt_assert(mbox, "No rendez-vous point defined for irecv");
445 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
446 match_fun, copy_data_fun, data, rate);
450 * \ingroup simix_comm_management
452 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type, int src, int tag,
453 int (*match_fun)(void *, void *, smx_activity_t), void *data)
455 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
457 return simcall_BODY_comm_iprobe(mbox, type, src, tag, match_fun, data);
461 * \ingroup simix_comm_management
463 void simcall_comm_cancel(smx_activity_t synchro)
465 simgrid::simix::kernelImmediate([synchro]{
466 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
472 * \ingroup simix_comm_management
474 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
476 return simcall_BODY_comm_waitany(comms, timeout);
480 * \ingroup simix_comm_management
482 int simcall_comm_testany(smx_activity_t* comms, size_t count)
486 return simcall_BODY_comm_testany(comms, count);
490 * \ingroup simix_comm_management
492 void simcall_comm_wait(smx_activity_t comm, double timeout)
494 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
495 simcall_BODY_comm_wait(comm, timeout);
499 * \brief Set the category of an synchro.
501 * This functions changes the category only. It calls a surf function.
502 * \param synchro The execution synchro
503 * \param category The tracing category
505 void simcall_set_category(smx_activity_t synchro, const char *category)
507 if (category == nullptr) {
510 simcall_BODY_set_category(synchro, category);
514 * \ingroup simix_comm_management
517 int simcall_comm_test(smx_activity_t comm)
519 return simcall_BODY_comm_test(comm);
523 * \ingroup simix_synchro_management
526 smx_mutex_t simcall_mutex_init()
529 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
532 return simcall_BODY_mutex_init();
536 * \ingroup simix_synchro_management
539 void simcall_mutex_lock(smx_mutex_t mutex)
541 simcall_BODY_mutex_lock(mutex);
545 * \ingroup simix_synchro_management
548 int simcall_mutex_trylock(smx_mutex_t mutex)
550 return simcall_BODY_mutex_trylock(mutex);
554 * \ingroup simix_synchro_management
557 void simcall_mutex_unlock(smx_mutex_t mutex)
559 simcall_BODY_mutex_unlock(mutex);
563 * \ingroup simix_synchro_management
566 smx_cond_t simcall_cond_init()
568 return simcall_BODY_cond_init();
572 * \ingroup simix_synchro_management
575 void simcall_cond_signal(smx_cond_t cond)
577 simcall_BODY_cond_signal(cond);
581 * \ingroup simix_synchro_management
584 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
586 simcall_BODY_cond_wait(cond, mutex);
590 * \ingroup simix_synchro_management
593 void simcall_cond_wait_timeout(smx_cond_t cond,
597 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
598 simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
602 * \ingroup simix_synchro_management
605 void simcall_cond_broadcast(smx_cond_t cond)
607 simcall_BODY_cond_broadcast(cond);
611 * \ingroup simix_synchro_management
614 smx_sem_t simcall_sem_init(int capacity)
616 return simcall_BODY_sem_init(capacity);
620 * \ingroup simix_synchro_management
623 void simcall_sem_release(smx_sem_t sem)
625 simcall_BODY_sem_release(sem);
629 * \ingroup simix_synchro_management
632 int simcall_sem_would_block(smx_sem_t sem)
634 return simcall_BODY_sem_would_block(sem);
638 * \ingroup simix_synchro_management
641 void simcall_sem_acquire(smx_sem_t sem)
643 simcall_BODY_sem_acquire(sem);
647 * \ingroup simix_synchro_management
650 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
652 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
653 simcall_BODY_sem_acquire_timeout(sem, timeout);
657 * \ingroup simix_synchro_management
660 int simcall_sem_get_capacity(smx_sem_t sem)
662 return simcall_BODY_sem_get_capacity(sem);
666 * \ingroup simix_file_management
669 sg_size_t simcall_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
671 return simcall_BODY_file_read(fd, size, host);
675 * \ingroup simix_file_management
678 sg_size_t simcall_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
680 return simcall_BODY_file_write(fd, size, host);
684 * \ingroup simix_file_management
687 smx_file_t simcall_file_open(const char* fullpath, sg_host_t host)
689 return simcall_BODY_file_open(fullpath, host);
693 * \ingroup simix_file_management
696 int simcall_file_close(smx_file_t fd, sg_host_t host)
698 return simcall_BODY_file_close(fd, host);
702 * \ingroup simix_file_management
705 int simcall_file_unlink(smx_file_t fd, sg_host_t host)
707 return simcall_BODY_file_unlink(fd, host);
711 * \ingroup simix_file_management
714 sg_size_t simcall_file_get_size(smx_file_t fd){
715 return simcall_BODY_file_get_size(fd);
719 * \ingroup simix_file_management
722 sg_size_t simcall_file_tell(smx_file_t fd){
723 return simcall_BODY_file_tell(fd);
727 * \ingroup simix_file_management
730 xbt_dynar_t simcall_file_get_info(smx_file_t fd)
732 return simcall_BODY_file_get_info(fd);
736 * \ingroup simix_file_management
739 int simcall_file_seek(smx_file_t fd, sg_offset_t offset, int origin){
740 return simcall_BODY_file_seek(fd, offset, origin);
744 * \ingroup simix_file_management
745 * \brief Move a file to another location on the *same mount point*.
748 int simcall_file_move(smx_file_t fd, const char* fullpath)
750 return simcall_BODY_file_move(fd, fullpath);
754 * \ingroup simix_storage_management
755 * \brief Returns the free space size on a given storage element.
756 * \param storage a storage
757 * \return Return the free space size on a given storage element (as sg_size_t)
759 sg_size_t simcall_storage_get_free_size (smx_storage_t storage){
760 return simcall_BODY_storage_get_free_size(storage);
764 * \ingroup simix_storage_management
765 * \brief Returns the used space size on a given storage element.
766 * \param storage a storage
767 * \return Return the used space size on a given storage element (as sg_size_t)
769 sg_size_t simcall_storage_get_used_size (smx_storage_t storage){
770 return simcall_BODY_storage_get_used_size(storage);
774 * \ingroup simix_storage_management
775 * \brief Returns a dict of the properties assigned to a storage element.
777 * \param storage A storage element
778 * \return The properties of this storage element
780 xbt_dict_t simcall_storage_get_properties(smx_storage_t storage)
782 return simcall_BODY_storage_get_properties(storage);
786 * \ingroup simix_storage_management
787 * \brief Returns a dict containing the content of a storage element.
789 * \param storage A storage element
790 * \return The content of this storage element as a dict (full path file => size)
792 xbt_dict_t simcall_storage_get_content(smx_storage_t storage)
794 return simcall_BODY_storage_get_content(storage);
797 void simcall_run_kernel(std::function<void()> const& code)
799 simcall_BODY_run_kernel(&code);
802 void simcall_run_blocking(std::function<void()> const& code)
804 simcall_BODY_run_blocking(&code);
807 int simcall_mc_random(int min, int max) {
808 return simcall_BODY_mc_random(min, max);
811 /* ************************************************************************** */
813 /** @brief returns a printable string representing a simcall */
814 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
815 return simcall_names[kind];
821 void unblock(smx_actor_t process)
823 xbt_assert(SIMIX_is_maestro());
824 SIMIX_simcall_answer(&process->simcall);