Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
976492f9d9a3a02045ac9cc037315986213e5d38
[simgrid.git] / src / simix / libsmx.cpp
1 /* libsmx.c - public interface to simix                                       */
2 /* --------                                                                   */
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) */
5 /*                                                                            */
6 /* This is somehow the "libc" of SimGrid                                      */
7
8 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.            */
9
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. */
12
13 #include <cmath>         /* std::isfinite() */
14
15 #include <functional>
16
17 #include "mc/mc.h"
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/ConditionVariableImpl.hpp"
24 #include "src/kernel/activity/MutexImpl.hpp"
25 #include "src/mc/mc_forward.hpp"
26 #include "src/mc/mc_replay.hpp"
27 #include "src/plugins/vm/VirtualMachineImpl.hpp"
28 #include "src/simix/smx_host_private.hpp"
29 #include "xbt/ex.h"
30 #include "xbt/functional.hpp"
31
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
33
34 #include "popping_bodies.cpp"
35
36 void simcall_call(smx_actor_t actor)
37 {
38   if (actor != simix_global->maestro_process) {
39     XBT_DEBUG("Yield actor '%s' on simcall %s (%d)", actor->get_cname(), SIMIX_simcall_name(actor->simcall.call),
40               (int)actor->simcall.call);
41     SIMIX_process_yield(actor);
42   } else {
43     SIMIX_simcall_handle(&actor->simcall, 0);
44   }
45 }
46
47 /**
48  * \ingroup simix_process_management
49  * \brief Creates a synchro that executes some computation of an host.
50  *
51  * This function creates a SURF action and allocates the data necessary
52  * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
53  *
54  * \param name Name of the execution synchro to create
55  * \param flops_amount amount Computation amount (in flops)
56  * \param priority computation priority
57  * \param bound
58  * \param host host where the synchro will be executed
59  * \return A new SIMIX execution synchronization
60  */
61 smx_activity_t simcall_execution_start(const char* name, double flops_amount, double priority, double bound,
62                                        simgrid::s4u::Host* host)
63 {
64   /* checking for infinite values */
65   xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
66   xbt_assert(std::isfinite(priority), "priority is not finite!");
67
68   return simcall_BODY_execution_start(name, flops_amount, priority, bound, host);
69 }
70
71 /**
72  * \ingroup simix_process_management
73  * \brief Creates a synchro that may involve parallel computation on
74  * several hosts and communication between them.
75  *
76  * \param name Name of the execution synchro to create
77  * \param host_nb Number of hosts where the synchro will be executed
78  * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
79  * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
80  * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
81  * amount between each pair of hosts
82  * \param rate the SURF action rate
83  * \param timeout timeout
84  * \return A new SIMIX execution synchronization
85  */
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)
88 {
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);
96       }
97     }
98   }
99
100   xbt_assert(std::isfinite(rate), "rate is not finite!");
101
102   return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, rate, timeout);
103 }
104
105 /**
106  * \ingroup simix_process_management
107  * \brief Cancels an execution synchro.
108  *
109  * This functions stops the execution. It calls a surf function.
110  * \param execution The execution synchro to cancel
111  */
112 void simcall_execution_cancel(smx_activity_t execution)
113 {
114   simgrid::kernel::activity::ExecImplPtr exec =
115       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
116   if (not exec->surfAction_)
117     return;
118   simgrid::simix::kernelImmediate([exec] {
119     XBT_DEBUG("Cancel synchro %p", exec.get());
120     if (exec->surfAction_)
121       exec->surfAction_->cancel();
122   });
123 }
124
125 /**
126  * \ingroup simix_process_management
127  * \brief Changes the priority of an execution synchro.
128  *
129  * This functions changes the priority only. It calls a surf function.
130  * \param execution The execution synchro
131  * \param priority The new priority
132  */
133 void simcall_execution_set_priority(smx_activity_t execution, double priority)
134 {
135   /* checking for infinite values */
136   xbt_assert(std::isfinite(priority), "priority is not finite!");
137   simgrid::simix::kernelImmediate([execution, priority] {
138
139     simgrid::kernel::activity::ExecImplPtr exec =
140         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
141     if (exec->surfAction_)
142       exec->surfAction_->set_priority(priority);
143   });
144 }
145
146 /**
147  * \ingroup simix_process_management
148  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
149  *
150  * This functions changes the capping only. It calls a surf function.
151  * \param execution The execution synchro
152  * \param bound The new bound
153  */
154 void simcall_execution_set_bound(smx_activity_t execution, double bound)
155 {
156   simgrid::simix::kernelImmediate([execution, bound] {
157     simgrid::kernel::activity::ExecImplPtr exec =
158         boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(execution);
159     if (exec->surfAction_)
160       exec->surfAction_->set_bound(bound);
161   });
162 }
163
164 /**
165  * \ingroup simix_host_management
166  * \brief Waits for the completion of an execution synchro and destroy it.
167  *
168  * \param execution The execution synchro
169  */
170 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
171 {
172   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
173 }
174
175 e_smx_state_t simcall_execution_test(smx_activity_t execution)
176 {
177   return (e_smx_state_t)simcall_BODY_execution_test(execution);
178 }
179
180 /**
181  * \ingroup simix_process_management
182  * \brief Kills all SIMIX processes.
183  */
184 void simcall_process_killall()
185 {
186   simcall_BODY_process_killall();
187 }
188
189 /**
190  * \ingroup simix_process_management
191  * \brief Cleans up a SIMIX process.
192  * \param process poor victim (must have already been killed)
193  */
194 void simcall_process_cleanup(smx_actor_t process)
195 {
196   simcall_BODY_process_cleanup(process);
197 }
198
199 void simcall_process_join(smx_actor_t process, double timeout)
200 {
201   simcall_BODY_process_join(process, timeout);
202 }
203
204 /**
205  * \ingroup simix_process_management
206  * \brief Suspends a process.
207  *
208  * This function suspends the process by suspending the synchro
209  * it was waiting for completion.
210  *
211  * \param process a SIMIX process
212  */
213 void simcall_process_suspend(smx_actor_t process)
214 {
215   simcall_BODY_process_suspend(process);
216 }
217
218 /**
219  * \ingroup simix_process_management
220  * \brief Set the user data of a #smx_actor_t.
221  *
222  * This functions sets the user data associated to \a process.
223  * \param process SIMIX process
224  * \param data User data
225  */
226 void simcall_process_set_data(smx_actor_t process, void *data)
227 {
228   simgrid::simix::kernelImmediate([process, data] { process->setUserData(data); });
229 }
230
231 /**
232  * \ingroup simix_process_management
233  * \brief Set the kill time of a process.
234  */
235 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
236 {
237
238   if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
239     return;
240   XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, process->get_cname(), process->host->get_cname());
241   process->kill_timer = SIMIX_timer_set(kill_time, [process] {
242     simix_global->kill_process_function(process);
243     process->kill_timer=nullptr;
244   });
245 }
246
247 /**
248  * \ingroup simix_process_management
249  * \brief Add an on_exit function
250  * Add an on_exit function which will be executed when the process exits/is killed.
251  */
252 XBT_PUBLIC void simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void* data)
253 {
254   simcall_BODY_process_on_exit(process, fun, data);
255 }
256
257 /**
258  * \ingroup simix_process_management
259  * \brief Creates a new sleep SIMIX synchro.
260  *
261  * This function creates a SURF action and allocates the data necessary
262  * to create the SIMIX synchro. It can raise a host_error exception if the
263  * host crashed. The default SIMIX name of the synchro is "sleep".
264  *
265  *   \param duration Time duration of the sleep.
266  *   \return A result telling whether the sleep was successful
267  */
268 e_smx_state_t simcall_process_sleep(double duration)
269 {
270   /* checking for infinite values */
271   xbt_assert(std::isfinite(duration), "duration is not finite!");
272   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
273 }
274
275 /**
276  * \ingroup simix_comm_management
277  */
278 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
279                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
280                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
281 {
282   /* checking for infinite values */
283   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
284   xbt_assert(std::isfinite(rate), "rate is not finite!");
285   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
286
287   xbt_assert(mbox, "No rendez-vous point defined for send");
288
289   if (MC_is_active() || MC_record_replay_is_active()) {
290     /* the model-checker wants two separate simcalls */
291     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
292     comm = simcall_comm_isend(sender, mbox, task_size, rate,
293         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
294     simcall_comm_wait(comm, timeout);
295     comm = nullptr;
296   }
297   else {
298     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
299                          match_fun, copy_data_fun, data, timeout);
300   }
301 }
302
303 /**
304  * \ingroup simix_comm_management
305  */
306 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
307                                   size_t src_buff_size,
308                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
309                                   void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
310                                   void* data, int detached)
311 {
312   /* checking for infinite values */
313   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
314   xbt_assert(std::isfinite(rate), "rate is not finite!");
315
316   xbt_assert(mbox, "No rendez-vous point defined for isend");
317
318   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
319                                  src_buff_size, match_fun,
320                                  clean_fun, copy_data_fun, data, detached);
321 }
322
323 /**
324  * \ingroup simix_comm_management
325  */
326 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
327                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
328                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
329 {
330   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
331   xbt_assert(mbox, "No rendez-vous point defined for recv");
332
333   if (MC_is_active() || MC_record_replay_is_active()) {
334     /* the model-checker wants two separate simcalls */
335     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
336     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
337                               match_fun, copy_data_fun, data, rate);
338     simcall_comm_wait(comm, timeout);
339     comm = nullptr;
340   }
341   else {
342     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
343                            match_fun, copy_data_fun, data, timeout, rate);
344   }
345 }
346 /**
347  * \ingroup simix_comm_management
348  */
349 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
350                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
351                                   void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
352 {
353   xbt_assert(mbox, "No rendez-vous point defined for irecv");
354
355   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
356                                  match_fun, copy_data_fun, data, rate);
357 }
358
359 /**
360  * \ingroup simix_comm_management
361  */
362 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
363                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
364 {
365   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
366
367   return simcall_BODY_comm_iprobe(mbox, type, match_fun, data);
368 }
369
370 /**
371  * \ingroup simix_comm_management
372  */
373 void simcall_comm_cancel(smx_activity_t synchro)
374 {
375   simgrid::simix::kernelImmediate([synchro] {
376     simgrid::kernel::activity::CommImplPtr comm =
377         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
378     comm->cancel();
379   });
380 }
381
382 /**
383  * \ingroup simix_comm_management
384  */
385 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
386 {
387   return simcall_BODY_comm_waitany(comms, timeout);
388 }
389
390 /**
391  * \ingroup simix_comm_management
392  */
393 int simcall_comm_testany(smx_activity_t* comms, size_t count)
394 {
395   if (count == 0)
396     return -1;
397   return simcall_BODY_comm_testany(comms, count);
398 }
399
400 /**
401  * \ingroup simix_comm_management
402  */
403 void simcall_comm_wait(smx_activity_t comm, double timeout)
404 {
405   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
406   simcall_BODY_comm_wait(comm, timeout);
407 }
408
409 /**
410  * \brief Set the category of an synchro.
411  *
412  * This functions changes the category only. It calls a surf function.
413  * \param synchro The execution synchro
414  * \param category The tracing category
415  */
416 void simcall_set_category(smx_activity_t synchro, const char *category)
417 {
418   if (category == nullptr) {
419     return;
420   }
421   simcall_BODY_set_category(synchro, category);
422 }
423
424 /**
425  * \ingroup simix_comm_management
426  *
427  */
428 int simcall_comm_test(smx_activity_t comm)
429 {
430   return simcall_BODY_comm_test(comm);
431 }
432
433 /**
434  * \ingroup simix_synchro_management
435  *
436  */
437 smx_mutex_t simcall_mutex_init()
438 {
439   if (not simix_global) {
440     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
441     xbt_abort();
442   }
443   return simgrid::simix::kernelImmediate([] { return new simgrid::kernel::activity::MutexImpl(); });
444 }
445
446 /**
447  * \ingroup simix_synchro_management
448  *
449  */
450 void simcall_mutex_lock(smx_mutex_t mutex)
451 {
452   simcall_BODY_mutex_lock(mutex);
453 }
454
455 /**
456  * \ingroup simix_synchro_management
457  *
458  */
459 int simcall_mutex_trylock(smx_mutex_t mutex)
460 {
461   return simcall_BODY_mutex_trylock(mutex);
462 }
463
464 /**
465  * \ingroup simix_synchro_management
466  *
467  */
468 void simcall_mutex_unlock(smx_mutex_t mutex)
469 {
470   simcall_BODY_mutex_unlock(mutex);
471 }
472
473 /**
474  * \ingroup simix_synchro_management
475  *
476  */
477 smx_cond_t simcall_cond_init()
478 {
479   return simcall_BODY_cond_init();
480 }
481
482 /**
483  * \ingroup simix_synchro_management
484  *
485  */
486 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
487 {
488   simcall_BODY_cond_wait(cond, mutex);
489 }
490
491 /**
492  * \ingroup simix_synchro_management
493  *
494  */
495 void simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
496 {
497   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
498   simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
499 }
500
501 /**
502  * \ingroup simix_synchro_management
503  *
504  */
505 void simcall_sem_acquire(smx_sem_t sem)
506 {
507   simcall_BODY_sem_acquire(sem);
508 }
509
510 /**
511  * \ingroup simix_synchro_management
512  *
513  */
514 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
515 {
516   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
517   simcall_BODY_sem_acquire_timeout(sem, timeout);
518 }
519
520 sg_size_t simcall_storage_read(surf_storage_t st, sg_size_t size)
521 {
522   return simcall_BODY_storage_read(st, size);
523 }
524
525 sg_size_t simcall_storage_write(surf_storage_t st, sg_size_t size)
526 {
527   return simcall_BODY_storage_write(st, size);
528 }
529
530 void simcall_run_kernel(std::function<void()> const& code)
531 {
532   simcall_BODY_run_kernel(&code);
533 }
534
535 void simcall_run_blocking(std::function<void()> const& code)
536 {
537   simcall_BODY_run_blocking(&code);
538 }
539
540 int simcall_mc_random(int min, int max) {
541   return simcall_BODY_mc_random(min, max);
542 }
543
544 /* ************************************************************************** */
545
546 /** @brief returns a printable string representing a simcall */
547 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
548   return simcall_names[kind];
549 }
550
551 namespace simgrid {
552 namespace simix {
553
554 void unblock(smx_actor_t process)
555 {
556   xbt_assert(SIMIX_is_maestro());
557   SIMIX_simcall_answer(&process->simcall);
558 }
559
560 }
561 }