Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
another move to FileImpl
[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-2015. The SimGrid Team.
9  * All rights reserved.                                                     */
10
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. */
13
14 #include <cmath>         /* std::isfinite() */
15
16 #include <functional>
17
18 #include "mc/mc.h"
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"
28 #include "xbt/ex.h"
29 #include "xbt/functional.hpp"
30
31 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
32
33 #include "popping_bodies.cpp"
34
35 void simcall_call(smx_actor_t actor)
36 {
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);
41   } else {
42     SIMIX_simcall_handle(&actor->simcall, 0);
43   }
44 }
45
46 /**
47  * \ingroup simix_process_management
48  * \brief Creates a synchro that executes some computation of an host.
49  *
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.
52  *
53  * \param name Name of the execution synchro to create
54  * \param flops_amount amount Computation amount (in flops)
55  * \param priority computation priority
56  * \param bound
57  * \return A new SIMIX execution synchronization
58  */
59 smx_activity_t simcall_execution_start(const char *name,
60                                     double flops_amount,
61                                     double priority, double bound)
62 {
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!");
66
67   return simcall_BODY_execution_start(name, flops_amount, priority, bound);
68 }
69
70 /**
71  * \ingroup simix_process_management
72  * \brief Creates a synchro that may involve parallel computation on
73  * several hosts and communication between them.
74  *
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
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 amount, double rate,
88                                                 double timeout)
89 {
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);
97       }
98     }
99   }
100
101   xbt_assert(std::isfinite(amount), "amount is not finite!");
102   xbt_assert(std::isfinite(rate), "rate is not finite!");
103
104   return simcall_BODY_execution_parallel_start(name, host_nb, host_list, flops_amount, bytes_amount, amount, rate,
105                                                timeout);
106 }
107
108 /**
109  * \ingroup simix_process_management
110  * \brief Cancels an execution synchro.
111  *
112  * This functions stops the execution. It calls a surf function.
113  * \param execution The execution synchro to cancel
114  */
115 void simcall_execution_cancel(smx_activity_t execution)
116 {
117   simcall_BODY_execution_cancel(execution);
118 }
119
120 /**
121  * \ingroup simix_process_management
122  * \brief Changes the priority of an execution synchro.
123  *
124  * This functions changes the priority only. It calls a surf function.
125  * \param execution The execution synchro
126  * \param priority The new priority
127  */
128 void simcall_execution_set_priority(smx_activity_t execution, double priority)
129 {
130   /* checking for infinite values */
131   xbt_assert(std::isfinite(priority), "priority is not finite!");
132
133   simcall_BODY_execution_set_priority(execution, priority);
134 }
135
136 /**
137  * \ingroup simix_process_management
138  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
139  *
140  * This functions changes the capping only. It calls a surf function.
141  * \param execution The execution synchro
142  * \param bound The new bound
143  */
144 void simcall_execution_set_bound(smx_activity_t execution, double bound)
145 {
146   simcall_BODY_execution_set_bound(execution, bound);
147 }
148
149 /**
150  * \ingroup simix_host_management
151  * \brief Waits for the completion of an execution synchro and destroy it.
152  *
153  * \param execution The execution synchro
154  */
155 e_smx_state_t simcall_execution_wait(smx_activity_t execution)
156 {
157   return (e_smx_state_t) simcall_BODY_execution_wait(execution);
158 }
159
160 /**
161  * \ingroup simix_process_management
162  * \brief Kills a SIMIX process.
163  *
164  * This function simply kills a  process.
165  *
166  * \param process poor victim
167  */
168 void simcall_process_kill(smx_actor_t process)
169 {
170   simcall_BODY_process_kill(process);
171 }
172
173 /**
174  * \ingroup simix_process_management
175  * \brief Kills all SIMIX processes.
176  */
177 void simcall_process_killall(int reset_pid)
178 {
179   simcall_BODY_process_killall(reset_pid);
180 }
181
182 /**
183  * \ingroup simix_process_management
184  * \brief Cleans up a SIMIX process.
185  * \param process poor victim (must have already been killed)
186  */
187 void simcall_process_cleanup(smx_actor_t process)
188 {
189   simcall_BODY_process_cleanup(process);
190 }
191
192 /**
193  * \ingroup simix_process_management
194  * \brief Migrates an agent to another location.
195  *
196  * This function changes the value of the host on which \a process is running.
197  *
198  * \param process the process to migrate
199  * \param dest name of the new host
200  */
201 void simcall_process_set_host(smx_actor_t process, sg_host_t dest)
202 {
203   simcall_BODY_process_set_host(process, dest);
204 }
205
206 void simcall_process_join(smx_actor_t process, double timeout)
207 {
208   simcall_BODY_process_join(process, timeout);
209 }
210
211 /**
212  * \ingroup simix_process_management
213  * \brief Suspends a process.
214  *
215  * This function suspends the process by suspending the synchro
216  * it was waiting for completion.
217  *
218  * \param process a SIMIX process
219  */
220 void simcall_process_suspend(smx_actor_t process)
221 {
222   simcall_BODY_process_suspend(process);
223 }
224
225 /**
226  * \ingroup simix_process_management
227  * \brief Resumes a suspended process.
228  *
229  * This function resumes a suspended process by resuming the synchro
230  * it was waiting for completion.
231  *
232  * \param process a SIMIX process
233  */
234 void simcall_process_resume(smx_actor_t process)
235 {
236   simcall_BODY_process_resume(process);
237 }
238
239 /**
240  * \ingroup simix_process_management
241  * \brief Returns the amount of SIMIX processes in the system
242  *
243  * Maestro internal process is not counted, only user code processes are
244  */
245 int simcall_process_count()
246 {
247   return simgrid::simix::kernelImmediate(SIMIX_process_count);
248 }
249
250 /**
251  * \ingroup simix_process_management
252  * \brief Set the user data of a #smx_actor_t.
253  *
254  * This functions sets the user data associated to \a process.
255  * \param process SIMIX process
256  * \param data User data
257  */
258 void simcall_process_set_data(smx_actor_t process, void *data)
259 {
260   simgrid::simix::kernelImmediate(std::bind(SIMIX_process_set_data, process, data));
261 }
262
263 /**
264  * \ingroup simix_process_management
265  * \brief Set the kill time of a process.
266  */
267 void simcall_process_set_kill_time(smx_actor_t process, double kill_time)
268 {
269
270   if (kill_time <= SIMIX_get_clock() || simix_global->kill_process_function == nullptr)
271     return;
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;
276   });
277 }
278 /**
279  * \ingroup simix_process_management
280  * \brief Get the kill time of a process (or 0 if unset).
281  */
282 double simcall_process_get_kill_time(smx_actor_t process) {
283   return SIMIX_timer_get_date(process->kill_timer);
284 }
285
286 /**
287  * \ingroup simix_process_management
288  * \brief Returns true if the process is suspended .
289  *
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.
293  */
294 int simcall_process_is_suspended(smx_actor_t process)
295 {
296   return simcall_BODY_process_is_suspended(process);
297 }
298
299 /**
300  * \ingroup simix_process_management
301  * \brief Return the properties
302  *
303  * This functions returns the properties associated with this process
304  */
305 xbt_dict_t simcall_process_get_properties(smx_actor_t process)
306 {
307   return SIMIX_process_get_properties(process);
308 }
309 /**
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.
313  */
314 XBT_PUBLIC(void) simcall_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data)
315 {
316   simcall_BODY_process_on_exit(process, fun, data);
317 }
318 /**
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.
322  */
323
324 XBT_PUBLIC(void) simcall_process_auto_restart_set(smx_actor_t process, int auto_restart)
325 {
326   simcall_BODY_process_auto_restart_set(process, auto_restart);
327 }
328
329 /**
330  * \ingroup simix_process_management
331  * \brief Restarts the process, killing it and starting it again from scratch.
332  */
333 XBT_PUBLIC(smx_actor_t) simcall_process_restart(smx_actor_t process)
334 {
335   return (smx_actor_t) simcall_BODY_process_restart(process);
336 }
337 /**
338  * \ingroup simix_process_management
339  * \brief Creates a new sleep SIMIX synchro.
340  *
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".
344  *
345  *   \param duration Time duration of the sleep.
346  *   \return A result telling whether the sleep was successful
347  */
348 e_smx_state_t simcall_process_sleep(double duration)
349 {
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);
353 }
354
355 /**
356  * \ingroup simix_comm_management
357  */
358 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
359                        size_t src_buff_size, int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
360                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout)
361 {
362   /* checking for infinite values */
363   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
364   xbt_assert(std::isfinite(rate), "rate is not finite!");
365   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
366
367   xbt_assert(mbox, "No rendez-vous point defined for send");
368
369   if (MC_is_active() || MC_record_replay_is_active()) {
370     /* the model-checker wants two separate simcalls */
371     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
372     comm = simcall_comm_isend(sender, mbox, task_size, rate,
373         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
374     simcall_comm_wait(comm, timeout);
375     comm = nullptr;
376   }
377   else {
378     simcall_BODY_comm_send(sender, mbox, task_size, rate, src_buff, src_buff_size,
379                          match_fun, copy_data_fun, data, timeout);
380   }
381 }
382
383 /**
384  * \ingroup simix_comm_management
385  */
386 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
387                                   size_t src_buff_size,
388                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
389                                   void (*clean_fun)(void*), void (*copy_data_fun)(smx_activity_t, void*, size_t),
390                                   void* data, int detached)
391 {
392   /* checking for infinite values */
393   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
394   xbt_assert(std::isfinite(rate), "rate is not finite!");
395
396   xbt_assert(mbox, "No rendez-vous point defined for isend");
397
398   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, src_buff,
399                                  src_buff_size, match_fun,
400                                  clean_fun, copy_data_fun, data, detached);
401 }
402
403 /**
404  * \ingroup simix_comm_management
405  */
406 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
407                        int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
408                        void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double timeout, double rate)
409 {
410   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
411   xbt_assert(mbox, "No rendez-vous point defined for recv");
412
413   if (MC_is_active() || MC_record_replay_is_active()) {
414     /* the model-checker wants two separate simcalls */
415     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
416     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
417                               match_fun, copy_data_fun, data, rate);
418     simcall_comm_wait(comm, timeout);
419     comm = nullptr;
420   }
421   else {
422     simcall_BODY_comm_recv(receiver, mbox, dst_buff, dst_buff_size,
423                            match_fun, copy_data_fun, data, timeout, rate);
424   }
425 }
426 /**
427  * \ingroup simix_comm_management
428  */
429 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
430                                   int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
431                                   void (*copy_data_fun)(smx_activity_t, void*, size_t), void* data, double rate)
432 {
433   xbt_assert(mbox, "No rendez-vous point defined for irecv");
434
435   return simcall_BODY_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
436                                  match_fun, copy_data_fun, data, rate);
437 }
438
439 /**
440  * \ingroup simix_comm_management
441  */
442 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type, int src, int tag,
443                                    int (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
444 {
445   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
446
447   return simcall_BODY_comm_iprobe(mbox, type, src, tag, match_fun, data);
448 }
449
450 /**
451  * \ingroup simix_comm_management
452  */
453 void simcall_comm_cancel(smx_activity_t synchro)
454 {
455   simgrid::simix::kernelImmediate([synchro] {
456     simgrid::kernel::activity::CommImplPtr comm =
457         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
458     comm->cancel();
459   });
460 }
461
462 /**
463  * \ingroup simix_comm_management
464  */
465 unsigned int simcall_comm_waitany(xbt_dynar_t comms, double timeout)
466 {
467   return simcall_BODY_comm_waitany(comms, timeout);
468 }
469
470 /**
471  * \ingroup simix_comm_management
472  */
473 int simcall_comm_testany(smx_activity_t* comms, size_t count)
474 {
475   if (count == 0)
476     return -1;
477   return simcall_BODY_comm_testany(comms, count);
478 }
479
480 /**
481  * \ingroup simix_comm_management
482  */
483 void simcall_comm_wait(smx_activity_t comm, double timeout)
484 {
485   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
486   simcall_BODY_comm_wait(comm, timeout);
487 }
488
489 /**
490  * \brief Set the category of an synchro.
491  *
492  * This functions changes the category only. It calls a surf function.
493  * \param synchro The execution synchro
494  * \param category The tracing category
495  */
496 void simcall_set_category(smx_activity_t synchro, const char *category)
497 {
498   if (category == nullptr) {
499     return;
500   }
501   simcall_BODY_set_category(synchro, category);
502 }
503
504 /**
505  * \ingroup simix_comm_management
506  *
507  */
508 int simcall_comm_test(smx_activity_t comm)
509 {
510   return simcall_BODY_comm_test(comm);
511 }
512
513 /**
514  * \ingroup simix_synchro_management
515  *
516  */
517 smx_mutex_t simcall_mutex_init()
518 {
519   if (not simix_global) {
520     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
521     xbt_abort();
522   }
523   return simcall_BODY_mutex_init();
524 }
525
526 /**
527  * \ingroup simix_synchro_management
528  *
529  */
530 void simcall_mutex_lock(smx_mutex_t mutex)
531 {
532   simcall_BODY_mutex_lock(mutex);
533 }
534
535 /**
536  * \ingroup simix_synchro_management
537  *
538  */
539 int simcall_mutex_trylock(smx_mutex_t mutex)
540 {
541   return simcall_BODY_mutex_trylock(mutex);
542 }
543
544 /**
545  * \ingroup simix_synchro_management
546  *
547  */
548 void simcall_mutex_unlock(smx_mutex_t mutex)
549 {
550   simcall_BODY_mutex_unlock(mutex);
551 }
552
553 /**
554  * \ingroup simix_synchro_management
555  *
556  */
557 smx_cond_t simcall_cond_init()
558 {
559   return simcall_BODY_cond_init();
560 }
561
562 /**
563  * \ingroup simix_synchro_management
564  *
565  */
566 void simcall_cond_signal(smx_cond_t cond)
567 {
568   simcall_BODY_cond_signal(cond);
569 }
570
571 /**
572  * \ingroup simix_synchro_management
573  *
574  */
575 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
576 {
577   simcall_BODY_cond_wait(cond, mutex);
578 }
579
580 /**
581  * \ingroup simix_synchro_management
582  *
583  */
584 void simcall_cond_wait_timeout(smx_cond_t cond,
585                                  smx_mutex_t mutex,
586                                  double timeout)
587 {
588   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
589   simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
590 }
591
592 /**
593  * \ingroup simix_synchro_management
594  *
595  */
596 void simcall_cond_broadcast(smx_cond_t cond)
597 {
598   simcall_BODY_cond_broadcast(cond);
599 }
600
601 /**
602  * \ingroup simix_synchro_management
603  *
604  */
605 smx_sem_t simcall_sem_init(int capacity)
606 {
607   return simcall_BODY_sem_init(capacity);
608 }
609
610 /**
611  * \ingroup simix_synchro_management
612  *
613  */
614 void simcall_sem_release(smx_sem_t sem)
615 {
616   simcall_BODY_sem_release(sem);
617 }
618
619 /**
620  * \ingroup simix_synchro_management
621  *
622  */
623 int simcall_sem_would_block(smx_sem_t sem)
624 {
625   return simcall_BODY_sem_would_block(sem);
626 }
627
628 /**
629  * \ingroup simix_synchro_management
630  *
631  */
632 void simcall_sem_acquire(smx_sem_t sem)
633 {
634   simcall_BODY_sem_acquire(sem);
635 }
636
637 /**
638  * \ingroup simix_synchro_management
639  *
640  */
641 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
642 {
643   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
644   simcall_BODY_sem_acquire_timeout(sem, timeout);
645 }
646
647 /**
648  * \ingroup simix_synchro_management
649  *
650  */
651 int simcall_sem_get_capacity(smx_sem_t sem)
652 {
653   return simcall_BODY_sem_get_capacity(sem);
654 }
655
656 /**
657  * \ingroup simix_file_management
658  *
659  */
660 sg_size_t simcall_file_read(surf_file_t fd, sg_size_t size, sg_host_t host)
661 {
662   return simcall_BODY_file_read(fd, size, host);
663 }
664
665 /**
666  * \ingroup simix_file_management
667  *
668  */
669 sg_size_t simcall_file_write(surf_file_t fd, sg_size_t size, sg_host_t host)
670 {
671   return simcall_BODY_file_write(fd, size, host);
672 }
673
674 /**
675  * \ingroup simix_file_management
676  * \brief
677  */
678 surf_file_t simcall_file_open(const char* mount, const char* path, sg_storage_t st)
679 {
680   return simcall_BODY_file_open(mount, path, st);
681 }
682
683 /**
684  * \ingroup simix_file_management
685  *
686  */
687 int simcall_file_close(surf_file_t fd, sg_host_t host)
688 {
689   return simcall_BODY_file_close(fd, host);
690 }
691
692 void simcall_run_kernel(std::function<void()> const& code)
693 {
694   simcall_BODY_run_kernel(&code);
695 }
696
697 void simcall_run_blocking(std::function<void()> const& code)
698 {
699   simcall_BODY_run_blocking(&code);
700 }
701
702 int simcall_mc_random(int min, int max) {
703   return simcall_BODY_mc_random(min, max);
704 }
705
706 /* ************************************************************************** */
707
708 /** @brief returns a printable string representing a simcall */
709 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
710   return simcall_names[kind];
711 }
712
713 namespace simgrid {
714 namespace simix {
715
716 void unblock(smx_actor_t process)
717 {
718   xbt_assert(SIMIX_is_maestro());
719   SIMIX_simcall_answer(&process->simcall);
720 }
721
722 }
723 }