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/IoImpl.hpp"
19 #include "src/kernel/activity/MutexImpl.hpp"
20 #include "src/mc/mc_replay.hpp"
21 #include "src/plugins/vm/VirtualMachineImpl.hpp"
22 #include "src/simix/smx_host_private.hpp"
23 #include "src/simix/smx_io_private.hpp"
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
27 #include "popping_bodies.cpp"
30 * @ingroup simix_process_management
31 * @brief Creates a synchro that executes some computation of a host.
33 * This function creates a SURF action and allocates the data necessary
34 * to create the SIMIX synchro. It can raise a HostFailureException exception if the host crashed.
36 * @param name Name of the execution synchro to create
37 * @param category Tracing category
38 * @param flops_amount amount Computation amount (in flops)
39 * @param priority computation priority
40 * @param bound Maximal speed for this execution (in flops) or -1 if no limit
41 * @param host host where the synchro will be executed
42 * @return A new SIMIX execution synchronization
44 smx_activity_t simcall_execution_start(std::string name, std::string category, double flops_amount, double priority,
45 double bound, simgrid::s4u::Host* host)
47 /* checking for infinite values */
48 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
49 xbt_assert(std::isfinite(priority), "priority is not finite!");
51 return simgrid::simix::simcall([name, category, flops_amount, priority, bound, host] {
52 return SIMIX_execution_start(name, category, flops_amount, priority, bound, host);
57 * @ingroup simix_process_management
58 * @brief Creates a synchro that may involve parallel computation on
59 * several hosts and communication between them.
61 * @param name Name of the execution synchro to create
62 * @param host_nb Number of hosts where the synchro will be executed
63 * @param host_list Array (of size host_nb) of hosts where the synchro will be executed
64 * @param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
65 * @param bytes_amount Array (of size host_nb * host_nb) representing the communication
66 * amount between each pair of hosts
67 * @param rate the SURF action rate
68 * @param timeout timeout
69 * @return A new SIMIX execution synchronization
71 smx_activity_t simcall_execution_parallel_start(std::string name, int host_nb, sg_host_t* host_list,
72 double* flops_amount, double* bytes_amount, double rate, double timeout)
74 /* checking for infinite values */
75 for (int i = 0 ; i < host_nb ; ++i) {
76 if (flops_amount != nullptr)
77 xbt_assert(std::isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
78 if (bytes_amount != nullptr) {
79 for (int j = 0 ; j < host_nb ; ++j) {
80 xbt_assert(std::isfinite(bytes_amount[i + host_nb * j]),
81 "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
86 xbt_assert(std::isfinite(rate), "rate is not finite!");
88 return simgrid::simix::simcall([name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout] {
89 return SIMIX_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
94 * @ingroup simix_process_management
95 * @brief Cancels an execution synchro.
97 * This functions stops the execution. It calls a surf function.
98 * @param execution The execution synchro to cancel
100 void simcall_execution_cancel(smx_activity_t execution)
102 simgrid::kernel::activity::ExecImplPtr exec =
103 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
104 if (exec->surf_action_ == nullptr) // FIXME: One test fails if I remove this, but I don't get why...
106 simgrid::simix::simcall([exec] { exec->cancel(); });
110 * @ingroup simix_process_management
111 * @brief Changes the priority of an execution synchro.
113 * This functions changes the priority only. It calls a surf function.
114 * @param execution The execution synchro
115 * @param priority The new priority
117 void simcall_execution_set_priority(smx_activity_t execution, double priority)
119 /* checking for infinite values */
120 xbt_assert(std::isfinite(priority), "priority is not finite!");
121 simgrid::simix::simcall([execution, priority] {
123 simgrid::kernel::activity::ExecImplPtr exec =
124 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
125 exec->set_priority(priority);
130 * @ingroup simix_process_management
131 * @brief Changes the capping (the maximum CPU utilization) of an execution synchro.
133 * This functions changes the capping only. It calls a surf function.
134 * @param execution The execution synchro
135 * @param bound The new bound
137 void simcall_execution_set_bound(smx_activity_t execution, double bound)
139 simgrid::simix::simcall([execution, bound] {
140 simgrid::kernel::activity::ExecImplPtr exec =
141 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
142 exec->set_bound(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);
157 e_smx_state_t simcall_execution_test(smx_activity_t execution)
159 return (e_smx_state_t)simcall_BODY_execution_test(execution);
162 void simcall_process_join(smx_actor_t process, double timeout)
164 simcall_BODY_process_join(process, timeout);
168 * @ingroup simix_process_management
169 * @brief Suspends a process.
171 * This function suspends the process by suspending the synchro
172 * it was waiting for completion.
174 * @param process a SIMIX process
176 void simcall_process_suspend(smx_actor_t process)
178 simcall_BODY_process_suspend(process);
182 * @ingroup simix_process_management
183 * @brief Set the user data of a #smx_actor_t.
185 * This functions sets the user data associated to @a process.
186 * @param process SIMIX process
187 * @param data User data
189 void simcall_process_set_data(smx_actor_t process, void *data)
191 simgrid::simix::simcall([process, data] { process->set_user_data(data); });
195 * @ingroup simix_process_management
196 * @brief Set the kill time of a process.
198 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
201 if (kill_time <= SIMIX_get_clock())
203 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host_->get_cname());
204 process->kill_timer = SIMIX_timer_set(kill_time, [process] {
205 SIMIX_process_kill(process, nullptr);
206 process->kill_timer=nullptr;
211 * @ingroup simix_process_management
212 * @brief Creates a new sleep SIMIX synchro.
214 * This function creates a SURF action and allocates the data necessary
215 * to create the SIMIX synchro. It can raise a HostFailureException if the
216 * host crashed. The default SIMIX name of the synchro is "sleep".
218 * @param duration Time duration of the sleep.
219 * @return A result telling whether the sleep was successful
221 e_smx_state_t simcall_process_sleep(double duration)
223 /* checking for infinite values */
224 xbt_assert(std::isfinite(duration), "duration is not finite!");
225 return (e_smx_state_t) simcall_BODY_process_sleep(duration);
229 * @ingroup simix_comm_management
231 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
232 size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
233 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
235 /* checking for infinite values */
236 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
237 xbt_assert(std::isfinite(rate), "rate is not finite!");
238 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
240 xbt_assert(mbox, "No rendez-vous point defined for send");
242 if (MC_is_active() || MC_record_replay_is_active()) {
243 /* the model-checker wants two separate simcalls */
244 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
245 comm = simcall_comm_isend(sender, mbox, task_size, rate,
246 src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
247 simcall_comm_wait(comm, timeout);
251 simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
252 match_fun, copy_data_fun, data, timeout);
257 * @ingroup simix_comm_management
259 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
260 size_t src_buff_size,
261 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
262 void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
263 void* data, int detached)
265 /* checking for infinite values */
266 xbt_assert(std::isfinite(task_size), "task_size is not finite!");
267 xbt_assert(std::isfinite(rate), "rate is not finite!");
269 xbt_assert(mbox, "No rendez-vous point defined for isend");
271 return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
272 src_buff_size, match_fun,
273 clean_fun, copy_data_fun, data, detached);
277 * @ingroup simix_comm_management
279 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
280 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
281 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
283 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
284 xbt_assert(mbox, "No rendez-vous point defined for recv");
286 if (MC_is_active() || MC_record_replay_is_active()) {
287 /* the model-checker wants two separate simcalls */
288 smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
289 comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
290 match_fun, copy_data_fun, data, rate);
291 simcall_comm_wait(comm, timeout);
295 simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
296 match_fun, copy_data_fun, data, timeout, rate);
300 * @ingroup simix_comm_management
302 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
303 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
304 void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
306 xbt_assert(mbox, "No rendez-vous point defined for irecv");
308 return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
309 match_fun, copy_data_fun, data, rate);
313 * @ingroup simix_comm_management
315 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
316 int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
318 xbt_assert(mbox, "No rendez-vous point defined for iprobe");
320 return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
324 * @ingroup simix_comm_management
326 void simcall_comm_cancel(smx_activity_t synchro)
328 simgrid::simix::simcall([synchro] {
329 simgrid::kernel::activity::CommImplPtr comm =
330 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
336 * @ingroup simix_comm_management
338 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
340 return simcall_BODY_comm_waitany(comms, timeout);
344 * @ingroup simix_comm_management
346 int simcall_comm_testany(smx_activity_t* comms, size_t count)
350 return simcall_BODY_comm_testany(comms, count);
354 * @ingroup simix_comm_management
356 void simcall_comm_wait(smx_activity_t comm, double timeout)
358 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
359 simcall_BODY_comm_wait(comm, timeout);
363 * @brief Set the category of an synchro.
365 * This functions changes the category only. It calls a surf function.
366 * @param synchro The execution synchro
367 * @param category The tracing category
369 void simcall_set_category(smx_activity_t synchro, std::string category)
371 if (category.empty()) {
374 simgrid::simix::simcall([synchro, category] { SIMIX_set_category(synchro, category); });
378 * @ingroup simix_comm_management
381 int simcall_comm_test(smx_activity_t comm)
383 return simcall_BODY_comm_test(comm);
387 * @ingroup simix_synchro_management
390 smx_mutex_t simcall_mutex_init()
392 if (not simix_global) {
393 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
396 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
400 * @ingroup simix_synchro_management
403 void simcall_mutex_lock(smx_mutex_t mutex)
405 simcall_BODY_mutex_lock(mutex);
409 * @ingroup simix_synchro_management
412 int simcall_mutex_trylock(smx_mutex_t mutex)
414 return simcall_BODY_mutex_trylock(mutex);
418 * @ingroup simix_synchro_management
421 void simcall_mutex_unlock(smx_mutex_t mutex)
423 simcall_BODY_mutex_unlock(mutex);
427 * @ingroup simix_synchro_management
430 smx_cond_t simcall_cond_init()
432 return simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
436 * @ingroup simix_synchro_management
439 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
441 simcall_BODY_cond_wait(cond, mutex);
445 * @ingroup simix_synchro_management
448 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
450 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
451 return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
455 * @ingroup simix_synchro_management
458 void simcall_sem_acquire(smx_sem_t sem)
460 simcall_BODY_sem_acquire(sem);
464 * @ingroup simix_synchro_management
467 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
469 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
470 return simcall_BODY_sem_acquire_timeout(sem, timeout);
473 e_smx_state_t simcall_io_wait(smx_activity_t io)
475 return (e_smx_state_t)simcall_BODY_io_wait(io);
478 void simcall_run_kernel(std::function<void()> const& code)
480 simcall_BODY_run_kernel(&code);
483 void simcall_run_blocking(std::function<void()> const& code)
485 simcall_BODY_run_blocking(&code);
488 int simcall_mc_random(int min, int max) {
489 return simcall_BODY_mc_random(min, max);
492 /* ************************************************************************** */
494 /** @brief returns a printable string representing a simcall */
495 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
496 return simcall_names[kind];
502 void unblock(smx_actor_t process)
504 xbt_assert(SIMIX_is_maestro());
505 SIMIX_simcall_answer(&process->simcall);