Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sg_* functions callable from simdag
[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 <functional>
15
16 #include "src/mc/mc_replay.h"
17 #include "smx_private.h"
18 #include "src/mc/mc_forward.h"
19 #include "xbt/ex.h"
20 #include <math.h>         /* isfinite() */
21 #include "mc/mc.h"
22 #include "src/simix/smx_host_private.h"
23 #include "src/simix/smx_private.hpp"
24
25 #include <simgrid/simix.hpp>
26
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
28
29 #include "popping_bodies.cpp"
30
31 void simcall_call(smx_process_t process)
32 {
33   if (process != simix_global->maestro_process) {
34     XBT_DEBUG("Yield process '%s' on simcall %s (%d)", process->name,
35               SIMIX_simcall_name(process->simcall.call), (int)process->simcall.call);
36     SIMIX_process_yield(process);
37   } else {
38     SIMIX_simcall_handle(&process->simcall, 0);
39   }
40 }
41
42 // ***** Host simcalls
43 // Those functions are replaced by methods on the Host object.
44
45 /** \ingroup simix_host_management
46  * \deprecated */
47 xbt_swag_t simcall_host_get_process_list(sg_host_t host)
48 {
49   return host->getProcessList();
50 }
51
52 /** \ingroup simix_host_management
53  * \deprecated */
54 double simcall_host_get_current_power_peak(sg_host_t host)
55 {
56   return host->getCurrentPowerPeak();
57 }
58
59 /** \ingroup simix_host_management
60  * \deprecated */
61 double simcall_host_get_power_peak_at(sg_host_t host, int pstate_index)
62 {
63   return host->getPowerPeakAt(pstate_index);
64 }
65
66 /** \deprecated */
67 void simcall_host_get_params(sg_host_t vm, vm_params_t params)
68 {
69   vm->getParams(params);
70 }
71
72 /** \deprecated */
73 void simcall_host_set_params(sg_host_t vm, vm_params_t params)
74 {
75   vm->setParams(params);
76 }
77
78 /** \ingroup simix_storage_management
79  *  \deprecated */
80 xbt_dict_t simcall_host_get_mounted_storage_list(sg_host_t host)
81 {
82   return host->getMountedStorageList();
83 }
84
85 /** \ingroup simix_storage_management
86  *  \deprecated */
87 xbt_dynar_t simcall_host_get_attached_storage_list(sg_host_t host)
88 {
89   return host->getAttachedStorageList();
90 }
91
92 // ***** Other simcalls
93
94 /**
95  * \ingroup simix_host_management
96  * \brief Returns a dict of the properties assigned to a router or AS.
97  *
98  * \param name The name of the router or AS
99  * \return The properties
100  */
101 xbt_dict_t simcall_asr_get_properties(const char *name)
102 {
103   return simcall_BODY_asr_get_properties(name);
104 }
105
106 /**
107  * \ingroup simix_process_management
108  * \brief Creates a synchro that executes some computation of an host.
109  *
110  * This function creates a SURF action and allocates the data necessary
111  * to create the SIMIX synchro. It can raise a host_error exception if the host crashed.
112  *
113  * \param name Name of the execution synchro to create
114  * \param flops_amount amount Computation amount (in flops)
115  * \param priority computation priority
116  * \param bound
117  * \param affinity_mask
118  * \return A new SIMIX execution synchronization
119  */
120 smx_synchro_t simcall_process_execute(const char *name,
121                                     double flops_amount,
122                                     double priority, double bound, unsigned long affinity_mask)
123 {
124   /* checking for infinite values */
125   xbt_assert(isfinite(flops_amount), "flops_amount is not finite!");
126   xbt_assert(isfinite(priority), "priority is not finite!");
127
128   return simcall_BODY_process_execute(name, flops_amount, priority, bound, affinity_mask);
129 }
130
131 /**
132  * \ingroup simix_process_management
133  * \brief Creates a synchro that may involve parallel computation on
134  * several hosts and communication between them.
135  *
136  * \param name Name of the execution synchro to create
137  * \param host_nb Number of hosts where the synchro will be executed
138  * \param host_list Array (of size host_nb) of hosts where the synchro will be executed
139  * \param flops_amount Array (of size host_nb) of computation amount of hosts (in bytes)
140  * \param bytes_amount Array (of size host_nb * host_nb) representing the communication
141  * amount between each pair of hosts
142  * \param amount the SURF action amount
143  * \param rate the SURF action rate
144  * \return A new SIMIX execution synchronization
145  */
146 smx_synchro_t simcall_process_parallel_execute(const char *name,
147                                          int host_nb,
148                                          sg_host_t *host_list,
149                                          double *flops_amount,
150                                          double *bytes_amount,
151                                          double amount,
152                                          double rate)
153 {
154   int i,j;
155   /* checking for infinite values */
156   for (i = 0 ; i < host_nb ; ++i) {
157      xbt_assert(isfinite(flops_amount[i]), "flops_amount[%d] is not finite!", i);
158      for (j = 0 ; j < host_nb ; ++j) {
159         xbt_assert(isfinite(bytes_amount[i + host_nb * j]),
160              "bytes_amount[%d+%d*%d] is not finite!", i, host_nb, j);
161      }
162   }
163
164   xbt_assert(isfinite(amount), "amount is not finite!");
165   xbt_assert(isfinite(rate), "rate is not finite!");
166
167   return simcall_BODY_process_parallel_execute(name, host_nb, host_list,
168                                             flops_amount,
169                                             bytes_amount,
170                                             amount, rate);
171
172 }
173
174 /**
175  * \ingroup simix_process_management
176  * \brief Destroys an execution synchro.
177  *
178  * Destroys a synchro, freeing its memory. This function cannot be called if there are a conditional waiting for it.
179  * \param execution The execution synchro to destroy
180  */
181 void simcall_process_execution_destroy(smx_synchro_t execution)
182 {
183   simcall_BODY_process_execution_destroy(execution);
184 }
185
186 /**
187  * \ingroup simix_process_management
188  * \brief Cancels an execution synchro.
189  *
190  * This functions stops the execution. It calls a surf function.
191  * \param execution The execution synchro to cancel
192  */
193 void simcall_process_execution_cancel(smx_synchro_t execution)
194 {
195   simcall_BODY_process_execution_cancel(execution);
196 }
197
198 /**
199  * \ingroup simix_process_management
200  * \brief Returns how much of an execution synchro remains to be done.
201  *
202  * \param execution The execution synchro
203  * \return The remaining amount
204  */
205 double simcall_process_execution_get_remains(smx_synchro_t execution)
206 {
207   return simcall_BODY_process_execution_get_remains(execution);
208 }
209
210 /**
211  * \ingroup simix_process_management
212  * \brief Returns the state of an execution synchro.
213  *
214  * \param execution The execution synchro
215  * \return The state
216  */
217 e_smx_state_t simcall_process_execution_get_state(smx_synchro_t execution)
218 {
219   return simcall_BODY_process_execution_get_state(execution);
220 }
221
222 /**
223  * \ingroup simix_process_management
224  * \brief Changes the priority of an execution synchro.
225  *
226  * This functions changes the priority only. It calls a surf function.
227  * \param execution The execution synchro
228  * \param priority The new priority
229  */
230 void simcall_process_execution_set_priority(smx_synchro_t execution, double priority)
231 {
232   /* checking for infinite values */
233   xbt_assert(isfinite(priority), "priority is not finite!");
234
235   simcall_BODY_process_execution_set_priority(execution, priority);
236 }
237
238 /**
239  * \ingroup simix_process_management
240  * \brief Changes the capping (the maximum CPU utilization) of an execution synchro.
241  *
242  * This functions changes the capping only. It calls a surf function.
243  * \param execution The execution synchro
244  * \param bound The new bound
245  */
246 void simcall_process_execution_set_bound(smx_synchro_t execution, double bound)
247 {
248   simcall_BODY_process_execution_set_bound(execution, bound);
249 }
250
251 /**
252  * \ingroup simix_process_management
253  * \brief Changes the CPU affinity of an execution synchro.
254  *
255  * This functions changes the CPU affinity of an execution synchro. See taskset(1) on Linux.
256  * \param execution The execution synchro
257  * \param host Host
258  * \param mask Affinity mask
259  */
260 void simcall_process_execution_set_affinity(smx_synchro_t execution, sg_host_t host, unsigned long mask)
261 {
262   simcall_BODY_process_execution_set_affinity(execution, host, mask);
263 }
264
265 /**
266  * \ingroup simix_host_management
267  * \brief Waits for the completion of an execution synchro and destroy it.
268  *
269  * \param execution The execution synchro
270  */
271 e_smx_state_t simcall_process_execution_wait(smx_synchro_t execution)
272 {
273   return (e_smx_state_t) simcall_BODY_process_execution_wait(execution);
274 }
275
276
277 /**
278  * \ingroup simix_vm_management
279  * \brief Create a VM on the given physical host.
280  *
281  * \param name VM name
282  * \param host Physical host
283  *
284  * \return The host object of the VM
285  */
286 void* simcall_vm_create(const char *name, sg_host_t phys_host)
287 {
288   return simgrid::simix::kernel(std::bind(SIMIX_vm_create, name, phys_host));
289 }
290
291 /**
292  * \ingroup simix_vm_management
293  * \brief Start the given VM to the given physical host
294  *
295  * \param vm VM
296  */
297 void simcall_vm_start(sg_host_t vm)
298 {
299   return simgrid::simix::kernel(std::bind(SIMIX_vm_start, vm));
300 }
301
302 /**
303  * \ingroup simix_vm_management
304  * \brief Get the state of the given VM
305  *
306  * \param vm VM
307  * \return The state of the VM
308  */
309 int simcall_vm_get_state(sg_host_t vm)
310 {
311   return simgrid::simix::kernel(std::bind(SIMIX_vm_get_state, vm));
312 }
313
314 /**
315  * \ingroup simix_vm_management
316  * \brief Get the name of the physical host on which the given VM runs.
317  *
318  * \param vm VM
319  * \return The name of the physical host
320  */
321 void *simcall_vm_get_pm(sg_host_t vm)
322 {
323   return simgrid::simix::kernel(std::bind(SIMIX_vm_get_pm, vm));
324 }
325
326 void simcall_vm_set_bound(sg_host_t vm, double bound)
327 {
328   simgrid::simix::kernel(std::bind(SIMIX_vm_set_bound, vm, bound));
329 }
330
331 void simcall_vm_set_affinity(sg_host_t vm, sg_host_t pm, unsigned long mask)
332 {
333   simgrid::simix::kernel(std::bind(SIMIX_vm_set_affinity, vm, pm, mask));
334 }
335
336 /**
337  * \ingroup simix_vm_management
338  * \brief Migrate the given VM to the given physical host
339  *
340  * \param vm VM
341  * \param host Destination physical host
342  */
343 void simcall_vm_migrate(sg_host_t vm, sg_host_t host)
344 {
345   return simgrid::simix::kernel(std::bind(SIMIX_vm_migrate, vm, host));
346 }
347
348 /**
349  * \ingroup simix_vm_management
350  * \brief Suspend the given VM
351  *
352  * \param vm VM
353  */
354 void simcall_vm_suspend(sg_host_t vm)
355 {
356   simcall_BODY_vm_suspend(vm);
357 }
358
359 /**
360  * \ingroup simix_vm_management
361  * \brief Resume the given VM
362  *
363  * \param vm VM
364  */
365 void simcall_vm_resume(sg_host_t vm)
366 {
367   simcall_BODY_vm_resume(vm);
368 }
369
370 /**
371  * \ingroup simix_vm_management
372  * \brief Save the given VM
373  *
374  * \param vm VM
375  */
376 void simcall_vm_save(sg_host_t vm)
377 {
378   simcall_BODY_vm_save(vm);
379 }
380
381 /**
382  * \ingroup simix_vm_management
383  * \brief Restore the given VM
384  *
385  * \param vm VM
386  */
387 void simcall_vm_restore(sg_host_t vm)
388 {
389   simcall_BODY_vm_restore(vm);
390 }
391
392 /**
393  * \ingroup simix_vm_management
394  * \brief Shutdown the given VM
395  *
396  * \param vm VM
397  */
398 void simcall_vm_shutdown(sg_host_t vm)
399 {
400   simcall_BODY_vm_shutdown(vm);
401 }
402
403 /**
404  * \ingroup simix_vm_management
405  * \brief Destroy the given VM
406  *
407  * \param vm VM
408  */
409 void simcall_vm_destroy(sg_host_t vm)
410 {
411   simgrid::simix::kernel(std::bind(SIMIX_vm_destroy, vm));
412 }
413
414 /**
415  * \ingroup simix_vm_management
416  * \brief Encompassing simcall to prevent the removal of the src or the dst node at the end of a VM migration
417  *  The simcall actually invokes the following calls: 
418  *     simcall_vm_set_affinity(vm, src_pm, 0); 
419  *     simcall_vm_migrate(vm, dst_pm); 
420  *     simcall_vm_resume(vm);
421  *
422  * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
423  *
424  * \param vm VM to migrate
425  * \param src_pm  Source physical host
426  * \param dst_pmt Destination physical host
427  */
428 void simcall_vm_migratefrom_resumeto(sg_host_t vm, sg_host_t src_pm, sg_host_t dst_pm)
429 {
430   simgrid::simix::kernel(std::bind(
431     SIMIX_vm_migratefrom_resumeto, vm, src_pm, dst_pm));
432 }
433
434 /**
435  * \ingroup simix_process_management
436  * \brief Creates and runs a new SIMIX process.
437  *
438  * The structure and the corresponding thread are created and put in the list of ready processes.
439  *
440  * \param name a name for the process. It is for user-level information and can be NULL.
441  * \param code the main function of the process
442  * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can be NULL.
443  * It can be retrieved with the function \ref simcall_process_get_data.
444  * \param hostname name of the host where the new agent is executed.
445  * \param kill_time time when the process is killed
446  * \param argc first argument passed to \a code
447  * \param argv second argument passed to \a code
448  * \param properties the properties of the process
449  * \param auto_restart either it is autorestarting or not.
450  */
451 smx_process_t simcall_process_create(const char *name,
452                               xbt_main_func_t code,
453                               void *data,
454                               const char *hostname,
455                               double kill_time,
456                               int argc, char **argv,
457                               xbt_dict_t properties,
458                               int auto_restart)
459 {
460   return (smx_process_t) simcall_BODY_process_create(name, code, data, hostname,
461                               kill_time, argc, argv, properties,
462                               auto_restart);
463 }
464
465 /**
466  * \ingroup simix_process_management
467  * \brief Kills a SIMIX process.
468  *
469  * This function simply kills a  process.
470  *
471  * \param process poor victim
472  */
473 void simcall_process_kill(smx_process_t process)
474 {
475   simcall_BODY_process_kill(process);
476 }
477
478 /**
479  * \ingroup simix_process_management
480  * \brief Kills all SIMIX processes.
481  */
482 void simcall_process_killall(int reset_pid)
483 {
484   simcall_BODY_process_killall(reset_pid);
485 }
486
487 /**
488  * \ingroup simix_process_management
489  * \brief Cleans up a SIMIX process.
490  * \param process poor victim (must have already been killed)
491  */
492 void simcall_process_cleanup(smx_process_t process)
493 {
494   simcall_BODY_process_cleanup(process);
495 }
496
497 /**
498  * \ingroup simix_process_management
499  * \brief Migrates an agent to another location.
500  *
501  * This function changes the value of the host on which \a process is running.
502  *
503  * \param process the process to migrate
504  * \param dest name of the new host
505  */
506 void simcall_process_set_host(smx_process_t process, sg_host_t dest)
507 {
508   simcall_BODY_process_set_host(process, dest);
509 }
510
511 void simcall_process_join(smx_process_t process, double timeout)
512 {
513   simcall_BODY_process_join(process, timeout);
514 }
515
516 /**
517  * \ingroup simix_process_management
518  * \brief Suspends a process.
519  *
520  * This function suspends the process by suspending the synchro
521  * it was waiting for completion.
522  *
523  * \param process a SIMIX process
524  */
525 void simcall_process_suspend(smx_process_t process)
526 {
527   xbt_assert(process, "Invalid parameters");
528
529   simcall_BODY_process_suspend(process);
530 }
531
532 /**
533  * \ingroup simix_process_management
534  * \brief Resumes a suspended process.
535  *
536  * This function resumes a suspended process by resuming the synchro
537  * it was waiting for completion.
538  *
539  * \param process a SIMIX process
540  */
541 void simcall_process_resume(smx_process_t process)
542 {
543   simcall_BODY_process_resume(process);
544 }
545
546 /**
547  * \ingroup simix_process_management
548  * \brief Returns the amount of SIMIX processes in the system
549  *
550  * Maestro internal process is not counted, only user code processes are
551  */
552 int simcall_process_count(void)
553 {
554   return simgrid::simix::kernel(SIMIX_process_count);
555 }
556
557 /**
558  * \ingroup simix_process_management
559  * \brief Return the PID of a #smx_process_t.
560  * \param process a SIMIX process
561  * \return the PID of this process
562  */
563 int simcall_process_get_PID(smx_process_t process)
564 {
565   return SIMIX_process_get_PID(process);
566 }
567
568 /**
569  * \ingroup simix_process_management
570  * \brief Return the parent PID of a #smx_process_t.
571  * \param process a SIMIX process
572  * \return the PID of this process parenrt
573  */
574 int simcall_process_get_PPID(smx_process_t process)
575 {
576   return SIMIX_process_get_PPID(process);
577 }
578
579 /**
580  * \ingroup simix_process_management
581  * \brief Return the user data of a #smx_process_t.
582  * \param process a SIMIX process
583  * \return the user data of this process
584  */
585 void* simcall_process_get_data(smx_process_t process)
586 {
587   return SIMIX_process_get_data(process);
588 }
589
590 /**
591  * \ingroup simix_process_management
592  * \brief Set the user data of a #smx_process_t.
593  *
594  * This functions sets the user data associated to \a process.
595  * \param process SIMIX process
596  * \param data User data
597  */
598 void simcall_process_set_data(smx_process_t process, void *data)
599 {
600   simgrid::simix::kernel(std::bind(SIMIX_process_set_data, process, data));
601 }
602
603 static void kill_process(void* arg)
604 {
605   simix_global->kill_process_function((smx_process_t) arg);
606 }
607
608 /**
609  * \ingroup simix_process_management
610  * \brief Set the kill time of a process.
611  */
612 void simcall_process_set_kill_time(smx_process_t process, double kill_time)
613 {
614
615   if (kill_time > SIMIX_get_clock()) {
616     if (simix_global->kill_process_function) {
617       XBT_DEBUG("Set kill time %f for process %s(%s)",kill_time, process->name,
618           sg_host_get_name(process->host));
619       process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
620     }
621   }
622 }
623 /**
624  * \ingroup simix_process_management
625  * \brief Get the kill time of a process (or 0 if unset).
626  */
627 double simcall_process_get_kill_time(smx_process_t process) {
628         return SIMIX_timer_get_date(process->kill_timer);
629 }
630
631 /**
632  * \ingroup simix_process_management
633  * \brief Return the location on which an agent is running.
634  *
635  * This functions returns the sg_host_t corresponding to the location on which
636  * \a process is running.
637  * \param process SIMIX process
638  * \return SIMIX host
639  */
640 sg_host_t simcall_process_get_host(smx_process_t process)
641 {
642   return SIMIX_process_get_host(process);
643 }
644
645 /**
646  * \ingroup simix_process_management
647  * \brief Return the name of an agent.
648  *
649  * This functions checks whether \a process is a valid pointer or not and return its name.
650  * \param process SIMIX process
651  * \return The process name
652  */
653 const char* simcall_process_get_name(smx_process_t process)
654 {
655   return SIMIX_process_get_name(process);
656 }
657
658 /**
659  * \ingroup simix_process_management
660  * \brief Returns true if the process is suspended .
661  *
662  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
663  * \param process SIMIX process
664  * \return 1, if the process is suspended, else 0.
665  */
666 int simcall_process_is_suspended(smx_process_t process)
667 {
668   return simcall_BODY_process_is_suspended(process);
669 }
670
671 /**
672  * \ingroup simix_process_management
673  * \brief Return the properties
674  *
675  * This functions returns the properties associated with this process
676  */
677 xbt_dict_t simcall_process_get_properties(smx_process_t process)
678 {
679   return SIMIX_process_get_properties(process);
680 }
681 /**
682  * \ingroup simix_process_management
683  * \brief Add an on_exit function
684  * Add an on_exit function which will be executed when the process exits/is killed.
685  */
686 XBT_PUBLIC(void) simcall_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data)
687 {
688   simcall_BODY_process_on_exit(process, fun, data);
689 }
690 /**
691  * \ingroup simix_process_management
692  * \brief Sets the process to be auto-restarted or not by SIMIX when its host comes back up.
693  * Will restart the process when the host comes back up if auto_restart is set to 1.
694  */
695
696 XBT_PUBLIC(void) simcall_process_auto_restart_set(smx_process_t process, int auto_restart)
697 {
698   simcall_BODY_process_auto_restart_set(process, auto_restart);
699 }
700
701 /**
702  * \ingroup simix_process_management
703  * \brief Restarts the process, killing it and starting it again from scratch.
704  */
705 XBT_PUBLIC(smx_process_t) simcall_process_restart(smx_process_t process)
706 {
707   return (smx_process_t) simcall_BODY_process_restart(process);
708 }
709 /**
710  * \ingroup simix_process_management
711  * \brief Creates a new sleep SIMIX synchro.
712  *
713  * This function creates a SURF action and allocates the data necessary
714  * to create the SIMIX synchro. It can raise a host_error exception if the
715  * host crashed. The default SIMIX name of the synchro is "sleep".
716  *
717  *   \param duration Time duration of the sleep.
718  *   \return A result telling whether the sleep was successful
719  */
720 e_smx_state_t simcall_process_sleep(double duration)
721 {
722   /* checking for infinite values */
723   xbt_assert(isfinite(duration), "duration is not finite!");
724   return (e_smx_state_t) simcall_BODY_process_sleep(duration);
725 }
726
727 /**
728  *  \ingroup simix_rdv_management
729  *  \brief Creates a new rendez-vous point
730  *  \param name The name of the rendez-vous point
731  *  \return The created rendez-vous point
732  */
733 smx_rdv_t simcall_rdv_create(const char *name)
734 {
735   return simcall_BODY_rdv_create(name);
736 }
737
738
739 /**
740  *  \ingroup simix_rdv_management
741  *  \brief Destroy a rendez-vous point
742  *  \param rdv The rendez-vous point to destroy
743  */
744 void simcall_rdv_destroy(smx_rdv_t rdv)
745 {
746   simcall_BODY_rdv_destroy(rdv);
747 }
748 /**
749  *  \ingroup simix_rdv_management
750  *  \brief Returns a rendez-vous point knowing its name
751  */
752 smx_rdv_t simcall_rdv_get_by_name(const char *name)
753 {
754   xbt_assert(name != NULL, "Invalid parameter for simcall_rdv_get_by_name (name is NULL)");
755
756   /* FIXME: this is a horrible loss of performance, so we hack it out by
757    * skipping the simcall (for now). It works in parallel, it won't work on
758    * distributed but probably we will change MSG for that. */
759
760   return SIMIX_rdv_get_by_name(name);
761 }
762
763 /**
764  *  \ingroup simix_rdv_management
765  *  \brief Counts the number of communication synchros of a given host pending
766  *         on a rendez-vous point.
767  *  \param rdv The rendez-vous point
768  *  \param host The host to be counted
769  *  \return The number of comm synchros pending in the rdv
770  */
771 int simcall_rdv_comm_count_by_host(smx_rdv_t rdv, sg_host_t host)
772 {
773   return simcall_BODY_rdv_comm_count_by_host(rdv, host);
774 }
775
776 /**
777  *  \ingroup simix_rdv_management
778  *  \brief returns the communication at the head of the rendez-vous
779  *  \param rdv The rendez-vous point
780  *  \return The communication or NULL if empty
781  */
782 smx_synchro_t simcall_rdv_get_head(smx_rdv_t rdv)
783 {
784   return simcall_BODY_rdv_get_head(rdv);
785 }
786
787 void simcall_rdv_set_receiver(smx_rdv_t rdv, smx_process_t process)
788 {
789   simcall_BODY_rdv_set_receiver(rdv, process);
790 }
791
792 smx_process_t simcall_rdv_get_receiver(smx_rdv_t rdv)
793 {
794   return simcall_BODY_rdv_get_receiver(rdv);
795 }
796
797 /**
798  * \ingroup simix_comm_management
799  */
800 void simcall_comm_send(smx_process_t sender, smx_rdv_t rdv, double task_size, double rate,
801                          void *src_buff, size_t src_buff_size,
802                          int (*match_fun)(void *, void *, smx_synchro_t),
803                          void (*copy_data_fun)(smx_synchro_t, void*, size_t), void *data,
804                          double timeout)
805 {
806   /* checking for infinite values */
807   xbt_assert(isfinite(task_size), "task_size is not finite!");
808   xbt_assert(isfinite(rate), "rate is not finite!");
809   xbt_assert(isfinite(timeout), "timeout is not finite!");
810
811   xbt_assert(rdv, "No rendez-vous point defined for send");
812
813   if (MC_is_active() || MC_record_replay_is_active()) {
814     /* the model-checker wants two separate simcalls */
815     smx_synchro_t comm = NULL; /* MC needs the comm to be set to NULL during the simcall */
816     comm = simcall_comm_isend(sender, rdv, task_size, rate,
817         src_buff, src_buff_size, match_fun, NULL, copy_data_fun, data, 0);
818     simcall_comm_wait(comm, timeout);
819     comm = NULL;
820   }
821   else {
822     simcall_BODY_comm_send(sender, rdv, task_size, rate, src_buff, src_buff_size,
823                          match_fun, copy_data_fun, data, timeout);
824   }
825 }
826
827 /**
828  * \ingroup simix_comm_management
829  */
830 smx_synchro_t simcall_comm_isend(smx_process_t sender, smx_rdv_t rdv, double task_size, double rate,
831                               void *src_buff, size_t src_buff_size,
832                               int (*match_fun)(void *, void *, smx_synchro_t),
833                               void (*clean_fun)(void *),
834                               void (*copy_data_fun)(smx_synchro_t, void*, size_t),
835                               void *data,
836                               int detached)
837 {
838   /* checking for infinite values */
839   xbt_assert(isfinite(task_size), "task_size is not finite!");
840   xbt_assert(isfinite(rate), "rate is not finite!");
841
842   xbt_assert(rdv, "No rendez-vous point defined for isend");
843
844   return simcall_BODY_comm_isend(sender, rdv, task_size, rate, src_buff,
845                                  src_buff_size, match_fun,
846                                  clean_fun, copy_data_fun, data, detached);
847 }
848
849 /**
850  * \ingroup simix_comm_management
851  */
852 void simcall_comm_recv(smx_process_t receiver, smx_rdv_t rdv, void *dst_buff, size_t * dst_buff_size,
853                        int (*match_fun)(void *, void *, smx_synchro_t),
854                        void (*copy_data_fun)(smx_synchro_t, void*, size_t),
855                        void *data, double timeout, double rate)
856 {
857   xbt_assert(isfinite(timeout), "timeout is not finite!");
858   xbt_assert(rdv, "No rendez-vous point defined for recv");
859
860   if (MC_is_active() || MC_record_replay_is_active()) {
861     /* the model-checker wants two separate simcalls */
862     smx_synchro_t comm = NULL; /* MC needs the comm to be set to NULL during the simcall */
863     comm = simcall_comm_irecv(receiver, rdv, dst_buff, dst_buff_size,
864                               match_fun, copy_data_fun, data, rate);
865     simcall_comm_wait(comm, timeout);
866     comm = NULL;
867   }
868   else {
869     simcall_BODY_comm_recv(receiver, rdv, dst_buff, dst_buff_size,
870                            match_fun, copy_data_fun, data, timeout, rate);
871   }
872 }
873 /**
874  * \ingroup simix_comm_management
875  */
876 smx_synchro_t simcall_comm_irecv(smx_process_t receiver, smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size,
877                                 int (*match_fun)(void *, void *, smx_synchro_t),
878                                 void (*copy_data_fun)(smx_synchro_t, void*, size_t),
879                                 void *data, double rate)
880 {
881   xbt_assert(rdv, "No rendez-vous point defined for irecv");
882
883   return simcall_BODY_comm_irecv(receiver, rdv, dst_buff, dst_buff_size,
884                                  match_fun, copy_data_fun, data, rate);
885 }
886
887 /**
888  * \ingroup simix_comm_management
889  */
890 smx_synchro_t simcall_comm_iprobe(smx_rdv_t rdv, int type, int src, int tag,
891                                 int (*match_fun)(void *, void *, smx_synchro_t), void *data)
892 {
893   xbt_assert(rdv, "No rendez-vous point defined for iprobe");
894
895   return simcall_BODY_comm_iprobe(rdv, type, src, tag, match_fun, data);
896 }
897
898 /**
899  * \ingroup simix_comm_management
900  */
901 void simcall_comm_cancel(smx_synchro_t comm)
902 {
903   simcall_BODY_comm_cancel(comm);
904 }
905
906 /**
907  * \ingroup simix_comm_management
908  */
909 unsigned int simcall_comm_waitany(xbt_dynar_t comms)
910 {
911   return simcall_BODY_comm_waitany(comms);
912 }
913
914 /**
915  * \ingroup simix_comm_management
916  */
917 int simcall_comm_testany(xbt_dynar_t comms)
918 {
919   if (xbt_dynar_is_empty(comms))
920     return -1;
921   return simcall_BODY_comm_testany(comms);
922 }
923
924 /**
925  * \ingroup simix_comm_management
926  */
927 void simcall_comm_wait(smx_synchro_t comm, double timeout)
928 {
929   xbt_assert(isfinite(timeout), "timeout is not finite!");
930   simcall_BODY_comm_wait(comm, timeout);
931 }
932
933 /**
934  * \brief Set the category of an synchro.
935  *
936  * This functions changes the category only. It calls a surf function.
937  * \param execution The execution synchro
938  * \param category The tracing category
939  */
940 void simcall_set_category(smx_synchro_t synchro, const char *category)
941 {
942   if (category == NULL) {
943     return;
944   }
945   simcall_BODY_set_category(synchro, category);
946 }
947
948 /**
949  * \ingroup simix_comm_management
950  *
951  */
952 int simcall_comm_test(smx_synchro_t comm)
953 {
954   return simcall_BODY_comm_test(comm);
955 }
956
957 /**
958  * \ingroup simix_comm_management
959  *
960  */
961 double simcall_comm_get_remains(smx_synchro_t comm)
962 {
963   return simcall_BODY_comm_get_remains(comm);
964 }
965
966 /**
967  * \ingroup simix_comm_management
968  *
969  */
970 e_smx_state_t simcall_comm_get_state(smx_synchro_t comm)
971 {
972   return simcall_BODY_comm_get_state(comm);
973 }
974
975 /**
976  * \ingroup simix_comm_management
977  *
978  */
979 void *simcall_comm_get_src_data(smx_synchro_t comm)
980 {
981   return simcall_BODY_comm_get_src_data(comm);
982 }
983
984 /**
985  * \ingroup simix_comm_management
986  *
987  */
988 void *simcall_comm_get_dst_data(smx_synchro_t comm)
989 {
990   return simcall_BODY_comm_get_dst_data(comm);
991 }
992
993 /**
994  * \ingroup simix_comm_management
995  *
996  */
997 smx_process_t simcall_comm_get_src_proc(smx_synchro_t comm)
998 {
999   return simcall_BODY_comm_get_src_proc(comm);
1000 }
1001
1002 /**
1003  * \ingroup simix_comm_management
1004  *
1005  */
1006 smx_process_t simcall_comm_get_dst_proc(smx_synchro_t comm)
1007 {
1008   return simcall_BODY_comm_get_dst_proc(comm);
1009 }
1010
1011 #ifdef HAVE_LATENCY_BOUND_TRACKING
1012 int simcall_comm_is_latency_bounded(smx_synchro_t comm)
1013 {
1014   return simcall_BODY_comm_is_latency_bounded(comm);
1015 }
1016 #endif
1017
1018 /**
1019  * \ingroup simix_synchro_management
1020  *
1021  */
1022 smx_mutex_t simcall_mutex_init(void)
1023 {
1024   if(!simix_global) {
1025     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
1026     xbt_abort();
1027   }
1028   return simcall_BODY_mutex_init();
1029 }
1030
1031 /**
1032  * \ingroup simix_synchro_management
1033  *
1034  */
1035 void simcall_mutex_destroy(smx_mutex_t mutex)
1036 {
1037   simcall_BODY_mutex_destroy(mutex);
1038 }
1039
1040 /**
1041  * \ingroup simix_synchro_management
1042  *
1043  */
1044 void simcall_mutex_lock(smx_mutex_t mutex)
1045 {
1046   simcall_BODY_mutex_lock(mutex);
1047 }
1048
1049 /**
1050  * \ingroup simix_synchro_management
1051  *
1052  */
1053 int simcall_mutex_trylock(smx_mutex_t mutex)
1054 {
1055   return simcall_BODY_mutex_trylock(mutex);
1056 }
1057
1058 /**
1059  * \ingroup simix_synchro_management
1060  *
1061  */
1062 void simcall_mutex_unlock(smx_mutex_t mutex)
1063 {
1064   simcall_BODY_mutex_unlock(mutex);
1065 }
1066
1067 /**
1068  * \ingroup simix_synchro_management
1069  *
1070  */
1071 smx_cond_t simcall_cond_init(void)
1072 {
1073   return simcall_BODY_cond_init();
1074 }
1075
1076 /**
1077  * \ingroup simix_synchro_management
1078  *
1079  */
1080 void simcall_cond_destroy(smx_cond_t cond)
1081 {
1082   simcall_BODY_cond_destroy(cond);
1083 }
1084
1085 /**
1086  * \ingroup simix_synchro_management
1087  *
1088  */
1089 void simcall_cond_signal(smx_cond_t cond)
1090 {
1091   simcall_BODY_cond_signal(cond);
1092 }
1093
1094 /**
1095  * \ingroup simix_synchro_management
1096  *
1097  */
1098 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
1099 {
1100   simcall_BODY_cond_wait(cond, mutex);
1101 }
1102
1103 /**
1104  * \ingroup simix_synchro_management
1105  *
1106  */
1107 void simcall_cond_wait_timeout(smx_cond_t cond,
1108                                  smx_mutex_t mutex,
1109                                  double timeout)
1110 {
1111   xbt_assert(isfinite(timeout), "timeout is not finite!");
1112   simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
1113 }
1114
1115 /**
1116  * \ingroup simix_synchro_management
1117  *
1118  */
1119 void simcall_cond_broadcast(smx_cond_t cond)
1120 {
1121   simcall_BODY_cond_broadcast(cond);
1122 }
1123
1124 /**
1125  * \ingroup simix_synchro_management
1126  *
1127  */
1128 smx_sem_t simcall_sem_init(int capacity)
1129 {
1130   return simcall_BODY_sem_init(capacity);
1131 }
1132
1133 /**
1134  * \ingroup simix_synchro_management
1135  *
1136  */
1137 void simcall_sem_destroy(smx_sem_t sem)
1138 {
1139   simcall_BODY_sem_destroy(sem);
1140 }
1141
1142 /**
1143  * \ingroup simix_synchro_management
1144  *
1145  */
1146 void simcall_sem_release(smx_sem_t sem)
1147 {
1148   simcall_BODY_sem_release(sem);
1149 }
1150
1151 /**
1152  * \ingroup simix_synchro_management
1153  *
1154  */
1155 int simcall_sem_would_block(smx_sem_t sem)
1156 {
1157   return simcall_BODY_sem_would_block(sem);
1158 }
1159
1160 /**
1161  * \ingroup simix_synchro_management
1162  *
1163  */
1164 void simcall_sem_acquire(smx_sem_t sem)
1165 {
1166   simcall_BODY_sem_acquire(sem);
1167 }
1168
1169 /**
1170  * \ingroup simix_synchro_management
1171  *
1172  */
1173 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
1174 {
1175   xbt_assert(isfinite(timeout), "timeout is not finite!");
1176   simcall_BODY_sem_acquire_timeout(sem, timeout);
1177 }
1178
1179 /**
1180  * \ingroup simix_synchro_management
1181  *
1182  */
1183 int simcall_sem_get_capacity(smx_sem_t sem)
1184 {
1185   return simcall_BODY_sem_get_capacity(sem);
1186 }
1187
1188 /**
1189  * \ingroup simix_file_management
1190  *
1191  */
1192 sg_size_t simcall_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
1193 {
1194   return simcall_BODY_file_read(fd, size, host);
1195 }
1196
1197 /**
1198  * \ingroup simix_file_management
1199  *
1200  */
1201 sg_size_t simcall_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
1202 {
1203   return simcall_BODY_file_write(fd, size, host);
1204 }
1205
1206 /**
1207  * \ingroup simix_file_management
1208  * \brief
1209  */
1210 smx_file_t simcall_file_open(const char* fullpath, sg_host_t host)
1211 {
1212   return simcall_BODY_file_open(fullpath, host);
1213 }
1214
1215 /**
1216  * \ingroup simix_file_management
1217  *
1218  */
1219 int simcall_file_close(smx_file_t fd, sg_host_t host)
1220 {
1221   return simcall_BODY_file_close(fd, host);
1222 }
1223
1224 /**
1225  * \ingroup simix_file_management
1226  *
1227  */
1228 int simcall_file_unlink(smx_file_t fd, sg_host_t host)
1229 {
1230   return simcall_BODY_file_unlink(fd, host);
1231 }
1232
1233 /**
1234  * \ingroup simix_file_management
1235  *
1236  */
1237 sg_size_t simcall_file_get_size(smx_file_t fd){
1238   return simcall_BODY_file_get_size(fd);
1239 }
1240
1241 /**
1242  * \ingroup simix_file_management
1243  *
1244  */
1245 sg_size_t simcall_file_tell(smx_file_t fd){
1246   return simcall_BODY_file_tell(fd);
1247 }
1248
1249 /**
1250  * \ingroup simix_file_management
1251  *
1252  */
1253 xbt_dynar_t simcall_file_get_info(smx_file_t fd)
1254 {
1255   return simcall_BODY_file_get_info(fd);
1256 }
1257
1258 /**
1259  * \ingroup simix_file_management
1260  *
1261  */
1262 int simcall_file_seek(smx_file_t fd, sg_offset_t offset, int origin){
1263   return simcall_BODY_file_seek(fd, offset, origin);
1264 }
1265
1266 /**
1267  * \ingroup simix_file_management
1268  * \brief Move a file to another location on the *same mount point*.
1269  *
1270  */
1271 int simcall_file_move(smx_file_t fd, const char* fullpath)
1272 {
1273   return simcall_BODY_file_move(fd, fullpath);
1274 }
1275
1276 /**
1277  * \ingroup simix_storage_management
1278  * \brief Returns the free space size on a given storage element.
1279  * \param storage a storage
1280  * \return Return the free space size on a given storage element (as sg_size_t)
1281  */
1282 sg_size_t simcall_storage_get_free_size (smx_storage_t storage){
1283   return simcall_BODY_storage_get_free_size(storage);
1284 }
1285
1286 /**
1287  * \ingroup simix_storage_management
1288  * \brief Returns the used space size on a given storage element.
1289  * \param storage a storage
1290  * \return Return the used space size on a given storage element (as sg_size_t)
1291  */
1292 sg_size_t simcall_storage_get_used_size (smx_storage_t storage){
1293   return simcall_BODY_storage_get_used_size(storage);
1294 }
1295
1296 /**
1297  * \ingroup simix_storage_management
1298  * \brief Returns a dict of the properties assigned to a storage element.
1299  *
1300  * \param storage A storage element
1301  * \return The properties of this storage element
1302  */
1303 xbt_dict_t simcall_storage_get_properties(smx_storage_t storage)
1304 {
1305   return simcall_BODY_storage_get_properties(storage);
1306 }
1307
1308 /**
1309  * \ingroup simix_storage_management
1310  * \brief Returns a dict containing the content of a storage element.
1311  *
1312  * \param storage A storage element
1313  * \return The content of this storage element as a dict (full path file => size)
1314  */
1315 xbt_dict_t simcall_storage_get_content(smx_storage_t storage)
1316 {
1317   return simcall_BODY_storage_get_content(storage);
1318 }
1319
1320 void simcall_run_kernel(std::function<void()> const& code)
1321 {
1322   return simcall_BODY_run_kernel((void*) &code);
1323 }
1324
1325 #ifdef HAVE_MC
1326
1327 void *simcall_mc_snapshot(void) {
1328   return simcall_BODY_mc_snapshot();
1329 }
1330
1331 int simcall_mc_compare_snapshots(void *s1, void *s2) {
1332   return simcall_BODY_mc_compare_snapshots((simgrid::mc::Snapshot*)s1, (simgrid::mc::Snapshot*)s2);
1333 }
1334
1335 #endif /* HAVE_MC */
1336
1337 int simcall_mc_random(int min, int max) {
1338   return simcall_BODY_mc_random(min, max);
1339 }
1340
1341 /* ************************************************************************** */
1342
1343 /** @brief returns a printable string representing a simcall */
1344 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
1345   return simcall_names[kind];
1346 }