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 amount, double rate,
90 /* checking for infinite values */
91 for (int i = 0 ; i < host_nb ; ++i) {
92 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
93 if (bytes_amount != nullptr) {
94 for (int j = 0 ; j < host_nb ; ++j) {
95 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
96 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
101 xbt_assert(std::isfinite(amount), "amount is not finite!");
102 xbt_assert(std::isfinite(rate), "rate is not finite!");
104 return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, amount, rate,
109 * \ingroup simix_process_management
110 * \brief Cancels an execution synchro.
112 * This functions stops the execution. It calls a surf function.
113 * \param execution The execution synchro to cancel
115 void simcall_execution_cancel(smx_activity_t execution)
117 simcall_BODY_execution_cancel(execution);
121 * \ingroup simix_process_management
122 * \brief Changes the priority of an execution synchro.
124 * This functions changes the priority only. It calls a surf function.
125 * \param execution The execution synchro
126 * \param priority The new priority
128 void simcall_execution_set_priority(smx_activity_t execution, double priority)
130 /* checking for infinite values */
131 xbt_assert(std::isfinite(priority), "priority is not finite!");
133 simcall_BODY_execution_set_priority(execution, priority);
137 * \ingroup simix_process_management
138 * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
140 * This functions changes the capping only. It calls a surf function.
141 * \param execution The execution synchro
142 * \param bound The new bound
144 void simcall_execution_set_bound(smx_activity_t execution, double bound)
146 simcall_BODY_execution_set_bound(execution, bound);
150 * \ingroup simix_host_management
151 * \brief Waits for the completion of an execution synchro and destroy it.
153 * \param execution The execution synchro
155 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
157 return (e_smx_state_t) simcall_BODY_execution_wait(execution);
161 * \ingroup simix_process_management
162 * \brief Kills a SIMIX process.
164 * This function simply kills a process.
166 * \param process poor victim
168 void simcall_process_kill(smx_actor_t process)
170 simcall_BODY_process_kill(process);
174 * \ingroup simix_process_management
175 * \brief Kills all SIMIX processes.
177 void simcall_process_killall(int reset_pid)
179 simcall_BODY_process_killall(reset_pid);
183 * \ingroup simix_process_management
184 * \brief Cleans up a SIMIX process.
185 * \param process poor victim (must have already been killed)
187 void simcall_process_cleanup(smx_actor_t process)
189 simcall_BODY_process_cleanup(process);
193 * \ingroup simix_process_management
194 * \brief Migrates an agent to another location.
196 * This function changes the value of the host on which \a process is running.
198 * \param process the process to migrate
199 * \param dest name of the new host
201 void simcall_process_set_host(smx_actor_t process, sg_host_t dest)
203 simcall_BODY_process_set_host(process, dest);
206 void simcall_process_join(smx_actor_t process, double timeout)
208 simcall_BODY_process_join(process, timeout);
212 * \ingroup simix_process_management
213 * \brief Suspends a process.
215 * This function suspends the process by suspending the synchro
216 * it was waiting for completion.
218 * \param process a SIMIX process
220 void simcall_process_suspend(smx_actor_t process)
222 simcall_BODY_process_suspend(process);
226 * \ingroup simix_process_management
227 * \brief Resumes a suspended process.
229 * This function resumes a suspended process by resuming the synchro
230 * it was waiting for completion.
232 * \param process a SIMIX process
234 void simcall_process_resume(smx_actor_t process)
236 simcall_BODY_process_resume(process);
240 * \ingroup simix_process_management
241 * \brief Returns the amount of SIMIX processes in the system
243 * Maestro internal process is not counted, only user code processes are
245 int simcall_process_count()
247 return simgrid::simix::kernelImmediate(SIMIX_process_count);
251 * \ingroup simix_process_management
252 * \brief Set the user data of a #smx_actor_t.
254 * This functions sets the user data associated to \a process.
255 * \param process SIMIX process
256 * \param data User data
258 void simcall_process_set_data(smx_actor_t process, void *data)
260 simgrid::simix::kernelImmediate(std::bind(SIMIX_process_set_data, process, data));
264 * \ingroup simix_process_management
265 * \brief Set the kill time of a process.
267 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
270 if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
272 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->cname(), process->host->cname());
273 process->kill_timer = SIMIX_timer_set(kill_time, [=] {
274 simix_global->kill_process_function(process);
275 process->kill_timer=nullptr;
279 * \ingroup simix_process_management
280 * \brief Get the kill time of a process (or 0 if unset).
282 double simcall_process_get_kill_time(smx_actor_t process) {
283 return SIMIX_timer_get_date(process->kill_timer);
287 * \ingroup simix_process_management
288 * \brief Returns true if the process is suspended .
290 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
291 * \param process SIMIX process
292 * \return 1, if the process is suspended, else 0.
294 int simcall_process_is_suspended(smx_actor_t process)
296 return simcall_BODY_process_is_suspended(process);
300 * \ingroup simix_process_management
301 * \brief Return the properties
303 * This functions returns the properties associated with this process
305 xbt_dict_t simcall_process_get_properties(smx_actor_t process)
307 return SIMIX_process_get_properties(process);
310 * \ingroup simix_process_management
311 * \brief Add an on_exit function
312 * Add an on_exit function which will be executed when the process exits/is killed.
314 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
316 simcall_BODY_process_on_exit(process, fun, data);
319 * \ingroup simix_process_management
320 * \brief Sets the process to be auto-restarted or not by SIMIX when its host comes back up.
321 * Will restart the process when the host comes back up if auto_restart is set to 1.
324 XBT_PUBLIC(void) simcall_process_auto_restart_set(smx_actor_t process, int auto_restart)
326 simcall_BODY_process_auto_restart_set(process, auto_restart);
330 * \ingroup simix_process_management
331 * \brief Restarts the process, killing it and starting it again from scratch.
333 XBT_PUBLIC(smx_actor_t) simcall_process_restart(smx_actor_t process)
335 return (smx_actor_t) simcall_BODY_process_restart(process);
338 * \ingroup simix_process_management
339 * \brief Creates a new sleep SIMIX synchro.
341 * This function creates a SURF action and allocates the data necessary
342 * to create the SIMIX synchro. It can raise a host_error exception if the
343 * host crashed. The default SIMIX name of the synchro is "sleep".
345 * \param duration Time duration of the sleep.
346 * \return A result telling whether the sleep was successful
348 e_smx_state_t simcall_process_sleep(double duration)
350 /* checking for infinite values */
351 xbt_assert(std::isfinite(duration), "duration is not finite!");
352 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
356 * \ingroup simix_comm_management
358 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate,
359 void *src_buff, size_t src_buff_size,
360 int (*match_fun)(void *, void *, smx_activity_t),
361 void (*copy_data_fun)(smx_activity_t, void*, size_t), void *data,
364 /* checking for infinite values */
365 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
366 xbt_assert(std::isfinite(rate), "rate is not finite!");
367 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
369 xbt_assert(mbox, "No rendez-vous point defined for send");
371 if (MC_is_active() || MC_record_replay_is_active()) {
372 /* the model-checker wants two separate simcalls */
373 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
374 comm = simcall_comm_isend(sender, mbox, task_size, rate,
375 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
376 simcall_comm_wait(comm, timeout);
380 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
381 match_fun, copy_data_fun, data, timeout);
386 * \ingroup simix_comm_management
388 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate,
389 void *src_buff, size_t src_buff_size,
390 int (*match_fun)(void *, void *, smx_activity_t),
391 void (*clean_fun)(void *),
392 void (*copy_data_fun)(smx_activity_t, void*, size_t),
396 /* checking for infinite values */
397 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
398 xbt_assert(std::isfinite(rate), "rate is not finite!");
400 xbt_assert(mbox, "No rendez-vous point defined for isend");
402 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
403 src_buff_size, match_fun,
404 clean_fun, copy_data_fun, data, detached);
408 * \ingroup simix_comm_management
410 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void *dst_buff, size_t * dst_buff_size,
411 int (*match_fun)(void *, void *, smx_activity_t),
412 void (*copy_data_fun)(smx_activity_t, void*, size_t),
413 void *data, double timeout, double rate)
415 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
416 xbt_assert(mbox, "No rendez-vous point defined for recv");
418 if (MC_is_active() || MC_record_replay_is_active()) {
419 /* the model-checker wants two separate simcalls */
420 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
421 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
422 match_fun, copy_data_fun, data, rate);
423 simcall_comm_wait(comm, timeout);
427 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
428 match_fun, copy_data_fun, data, timeout, rate);
432 * \ingroup simix_comm_management
434 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void *dst_buff, size_t *dst_buff_size,
435 int (*match_fun)(void *, void *, smx_activity_t),
436 void (*copy_data_fun)(smx_activity_t, void*, size_t),
437 void *data, double rate)
439 xbt_assert(mbox, "No rendez-vous point defined for irecv");
441 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
442 match_fun, copy_data_fun, data, rate);
446 * \ingroup simix_comm_management
448 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type, int src, int tag,
449 int (*match_fun)(void *, void *, smx_activity_t), void *data)
451 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
453 return simcall_BODY_comm_iprobe(mbox, type, src, tag, match_fun, data);
457 * \ingroup simix_comm_management
459 void simcall_comm_cancel(smx_activity_t synchro)
461 simgrid::simix::kernelImmediate([synchro] {
462 simgrid::kernel::activity::CommImpl* comm = static_cast<simgrid::kernel::activity::CommImpl*>(synchro);
468 * \ingroup simix_comm_management
470 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
472 return simcall_BODY_comm_waitany(comms, timeout);
476 * \ingroup simix_comm_management
478 int simcall_comm_testany(smx_activity_t* comms, size_t count)
482 return simcall_BODY_comm_testany(comms, count);
486 * \ingroup simix_comm_management
488 void simcall_comm_wait(smx_activity_t comm, double timeout)
490 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
491 simcall_BODY_comm_wait(comm, timeout);
495 * \brief Set the category of an synchro.
497 * This functions changes the category only. It calls a surf function.
498 * \param synchro The execution synchro
499 * \param category The tracing category
501 void simcall_set_category(smx_activity_t synchro, const char *category)
503 if (category == nullptr) {
506 simcall_BODY_set_category(synchro, category);
510 * \ingroup simix_comm_management
513 int simcall_comm_test(smx_activity_t comm)
515 return simcall_BODY_comm_test(comm);
519 * \ingroup simix_synchro_management
522 smx_mutex_t simcall_mutex_init()
524 if (not simix_global) {
525 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
528 return simcall_BODY_mutex_init();
532 * \ingroup simix_synchro_management
535 void simcall_mutex_lock(smx_mutex_t mutex)
537 simcall_BODY_mutex_lock(mutex);
541 * \ingroup simix_synchro_management
544 int simcall_mutex_trylock(smx_mutex_t mutex)
546 return simcall_BODY_mutex_trylock(mutex);
550 * \ingroup simix_synchro_management
553 void simcall_mutex_unlock(smx_mutex_t mutex)
555 simcall_BODY_mutex_unlock(mutex);
559 * \ingroup simix_synchro_management
562 smx_cond_t simcall_cond_init()
564 return simcall_BODY_cond_init();
568 * \ingroup simix_synchro_management
571 void simcall_cond_signal(smx_cond_t cond)
573 simcall_BODY_cond_signal(cond);
577 * \ingroup simix_synchro_management
580 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
582 simcall_BODY_cond_wait(cond, mutex);
586 * \ingroup simix_synchro_management
589 void simcall_cond_wait_timeout(smx_cond_t cond,
593 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
594 simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
598 * \ingroup simix_synchro_management
601 void simcall_cond_broadcast(smx_cond_t cond)
603 simcall_BODY_cond_broadcast(cond);
607 * \ingroup simix_synchro_management
610 smx_sem_t simcall_sem_init(int capacity)
612 return simcall_BODY_sem_init(capacity);
616 * \ingroup simix_synchro_management
619 void simcall_sem_release(smx_sem_t sem)
621 simcall_BODY_sem_release(sem);
625 * \ingroup simix_synchro_management
628 int simcall_sem_would_block(smx_sem_t sem)
630 return simcall_BODY_sem_would_block(sem);
634 * \ingroup simix_synchro_management
637 void simcall_sem_acquire(smx_sem_t sem)
639 simcall_BODY_sem_acquire(sem);
643 * \ingroup simix_synchro_management
646 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
648 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
649 simcall_BODY_sem_acquire_timeout(sem, timeout);
653 * \ingroup simix_synchro_management
656 int simcall_sem_get_capacity(smx_sem_t sem)
658 return simcall_BODY_sem_get_capacity(sem);
662 * \ingroup simix_file_management
665 sg_size_t simcall_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
667 return simcall_BODY_file_read(fd, size, host);
671 * \ingroup simix_file_management
674 sg_size_t simcall_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
676 return simcall_BODY_file_write(fd, size, host);
680 * \ingroup simix_file_management
683 smx_file_t simcall_file_open(const char* fullpath, sg_host_t host)
685 return simcall_BODY_file_open(fullpath, host);
689 * \ingroup simix_file_management
692 int simcall_file_close(smx_file_t fd, sg_host_t host)
694 return simcall_BODY_file_close(fd, host);
698 * \ingroup simix_file_management
701 int simcall_file_unlink(smx_file_t fd, sg_host_t host)
703 return simcall_BODY_file_unlink(fd, host);
707 * \ingroup simix_file_management
710 sg_size_t simcall_file_get_size(smx_file_t fd){
711 return simcall_BODY_file_get_size(fd);
715 * \ingroup simix_file_management
718 sg_size_t simcall_file_tell(smx_file_t fd){
719 return simcall_BODY_file_tell(fd);
723 * \ingroup simix_file_management
726 xbt_dynar_t simcall_file_get_info(smx_file_t fd)
728 return simcall_BODY_file_get_info(fd);
732 * \ingroup simix_file_management
735 int simcall_file_seek(smx_file_t fd, sg_offset_t offset, int origin){
736 return simcall_BODY_file_seek(fd, offset, origin);
740 * \ingroup simix_file_management
741 * \brief Move a file to another location on the *same mount point*.
744 int simcall_file_move(smx_file_t fd, const char* fullpath)
746 return simcall_BODY_file_move(fd, fullpath);
749 void simcall_run_kernel(std::function<void()> const& code)
751 simcall_BODY_run_kernel(&code);
754 void simcall_run_blocking(std::function<void()> const& code)
756 simcall_BODY_run_blocking(&code);
759 int simcall_mc_random(int min, int max) {
760 return simcall_BODY_mc_random(min, max);
763 /* ************************************************************************** */
765 /** @brief returns a printable string representing a simcall */
766 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
767 return simcall_names[kind];
773 void unblock(smx_actor_t process)
775 xbt_assert(SIMIX_is_maestro());
776 SIMIX_simcall_answer(&process->simcall);