Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
803a6e6580bbe1bbe16075050ad241a0caae135f
[simgrid.git] / src / simix / smx_user.c
1 /* smx_user.c - public interface to simix                                   */
2
3 /* Copyright (c) 2010, 2011. Da SimGrid team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef _SVID_SOURCE
8 #  define _SVID_SOURCE    /* strdup() */
9 #endif
10 #ifndef _ISOC99_SOURCE
11 #  define _ISOC99_SOURCE  /* isfinite() */
12 #endif
13 #ifndef _ISO_C99_SOURCE
14 #  define _ISO_C99_SOURCE /* isfinite() */
15 #endif
16 #include <math.h>         /* isfinite() */
17
18 #include "smx_private.h"
19 #include "mc/mc.h"
20 #include "xbt/ex.h"
21
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
23
24 static const char* simcall_names[] = {
25 #undef SIMCALL_ENUM_ELEMENT
26 #define SIMCALL_ENUM_ELEMENT(x) #x /* generate strings from the enumeration values */
27 SIMCALL_LIST
28 #undef SIMCALL_ENUM_ELEMENT
29 };
30
31 /**
32  * \brief Returns a host given its name.
33  *
34  * \param name The name of the host to get
35  * \return The corresponding host
36  */
37 smx_host_t simcall_host_get_by_name(const char *name)
38 {
39   smx_simcall_t simcall = SIMIX_simcall_mine();
40
41   simcall->call = SIMCALL_HOST_GET_BY_NAME;
42   simcall->host_get_by_name.name = name;
43   SIMIX_simcall_push(simcall->issuer);
44   return simcall->host_get_by_name.result;
45 }
46
47 /**
48  * \brief Returns the name of a host.
49  *
50  * \param host A SIMIX host
51  * \return The name of this host
52  */
53 const char* simcall_host_get_name(smx_host_t host)
54 {
55   smx_simcall_t simcall = SIMIX_simcall_mine();
56
57   simcall->call = SIMCALL_HOST_GET_NAME;
58   simcall->host_get_name.host = host;
59   SIMIX_simcall_push(simcall->issuer);
60   return simcall->host_get_name.result;
61 }
62
63 /**
64  * \brief Returns a dict of the properties assigned to a host.
65  *
66  * \param host A host
67  * \return The properties of this host
68  */
69 xbt_dict_t simcall_host_get_properties(smx_host_t host)
70 {
71   smx_simcall_t simcall = SIMIX_simcall_mine();
72
73   simcall->call = SIMCALL_HOST_GET_PROPERTIES;
74   simcall->host_get_properties.host = host;
75   SIMIX_simcall_push(simcall->issuer);
76   return simcall->host_get_properties.result;
77 }
78
79 /**
80  * \brief Returns the speed of the processor.
81  *
82  * The speed returned does not take into account the current load on the machine.
83  * \param host A SIMIX host
84  * \return The speed of this host (in Mflop/s)
85  */
86 double simcall_host_get_speed(smx_host_t host)
87 {
88   smx_simcall_t simcall = SIMIX_simcall_mine();
89
90   simcall->call = SIMCALL_HOST_GET_SPEED;
91   simcall->host_get_speed.host = host;
92   SIMIX_simcall_push(simcall->issuer);
93   return simcall->host_get_speed.result;
94 }
95
96 /**
97  * \brief Returns the available speed of the processor.
98  *
99  * \return Speed currently available (in Mflop/s)
100  */
101 double simcall_host_get_available_speed(smx_host_t host)
102 {
103   smx_simcall_t simcall = SIMIX_simcall_mine();
104
105   simcall->call = SIMCALL_HOST_GET_AVAILABLE_SPEED;
106   simcall->host_get_available_speed.host = host;
107   SIMIX_simcall_push(simcall->issuer);
108   return simcall->host_get_available_speed.result;
109 }
110
111 /**
112  * \brief Returns the state of a host.
113  *
114  * Two states are possible: 1 if the host is active or 0 if it has crashed.
115  * \param host A SIMIX host
116  * \return 1 if the host is available, 0 otherwise
117  */
118 int simcall_host_get_state(smx_host_t host)
119 {
120   smx_simcall_t simcall = SIMIX_simcall_mine();
121
122   simcall->call = SIMCALL_HOST_GET_STATE;
123   simcall->host_get_state.host = host;
124   SIMIX_simcall_push(simcall->issuer);
125   return simcall->host_get_state.result;
126 }
127
128 /**
129  * \brief Returns the user data associated to a host.
130  *
131  * \param host SIMIX host
132  * \return the user data of this host
133  */
134 void* simcall_host_get_data(smx_host_t host)
135 {
136   smx_simcall_t simcall = SIMIX_simcall_mine();
137
138   simcall->call = SIMCALL_HOST_GET_DATA;
139   simcall->host_get_data.host = host;
140   SIMIX_simcall_push(simcall->issuer);
141   return simcall->host_get_data.result;
142 }
143
144 /**
145  * \brief Sets the user data associated to a host.
146  *
147  * The host must not have previous user data associated to it.
148  * \param A host SIMIX host
149  * \param data The user data to set
150  */
151 void simcall_host_set_data(smx_host_t host, void *data)
152 {
153   smx_simcall_t simcall = SIMIX_simcall_mine();
154
155   simcall->call = SIMCALL_HOST_SET_DATA;
156   simcall->host_set_data.host = host;
157   simcall->host_set_data.data = data;
158   SIMIX_simcall_push(simcall->issuer);
159 }
160
161 /** \brief Creates an action that executes some computation of an host.
162  *
163  * This function creates a SURF action and allocates the data necessary
164  * to create the SIMIX action. It can raise a host_error exception if the host crashed.
165  *
166  * \param name Name of the execution action to create
167  * \param host SIMIX host where the action will be executed
168  * \param amount Computation amount (in bytes)
169  * \return A new SIMIX execution action
170  */
171 smx_action_t simcall_host_execute(const char *name, smx_host_t host,
172                                     double computation_amount,
173                                     double priority)
174 {
175   /* checking for infinite values */
176   xbt_assert(isfinite(computation_amount), "computation_amount is not finite!");
177   xbt_assert(isfinite(priority), "priority is not finite!");
178   
179   smx_simcall_t simcall = SIMIX_simcall_mine();
180
181   simcall->call = SIMCALL_HOST_EXECUTE;
182   simcall->host_execute.name = name;
183   simcall->host_execute.host = host;
184   simcall->host_execute.computation_amount = computation_amount;
185   simcall->host_execute.priority = priority;
186   SIMIX_simcall_push(simcall->issuer);
187   return simcall->host_execute.result;
188 }
189
190 /** \brief Creates an action that may involve parallel computation on
191  * several hosts and communication between them.
192  *
193  * \param name Name of the execution action to create
194  * \param host_nb Number of hosts where the action will be executed
195  * \param host_list Array (of size host_nb) of hosts where the action will be executed
196  * \param computation_amount Array (of size host_nb) of computation amount of hosts (in bytes)
197  * \param communication_amount Array (of size host_nb * host_nb) representing the communication
198  * amount between each pair of hosts
199  * \param amount the SURF action amount
200  * \param rate the SURF action rate
201  * \return A new SIMIX execution action
202  */
203 smx_action_t simcall_host_parallel_execute(const char *name,
204                                          int host_nb,
205                                          smx_host_t *host_list,
206                                          double *computation_amount,
207                                          double *communication_amount,
208                                          double amount,
209                                          double rate)
210 {
211   int i,j;
212   /* checking for infinite values */
213   for (i = 0 ; i < host_nb ; ++i) {
214      xbt_assert(isfinite(computation_amount[i]), "computation_amount[%d] is not finite!", i);
215      for (j = 0 ; j < host_nb ; ++j) {
216         xbt_assert(isfinite(communication_amount[i + host_nb * j]), 
217              "communication_amount[%d+%d*%d] is not finite!", i, host_nb, j);
218      }   
219   }   
220  
221   xbt_assert(isfinite(amount), "amount is not finite!");
222   xbt_assert(isfinite(rate), "rate is not finite!");
223   
224   smx_simcall_t simcall = SIMIX_simcall_mine();
225
226   simcall->call = SIMCALL_HOST_PARALLEL_EXECUTE;
227   simcall->host_parallel_execute.name = name;
228   simcall->host_parallel_execute.host_nb = host_nb;
229   simcall->host_parallel_execute.host_list = host_list;
230   simcall->host_parallel_execute.computation_amount = computation_amount;
231   simcall->host_parallel_execute.communication_amount = communication_amount;
232   simcall->host_parallel_execute.amount = amount;
233   simcall->host_parallel_execute.rate = rate;
234   SIMIX_simcall_push(simcall->issuer);
235   return simcall->host_parallel_execute.result;
236 }
237
238 /**
239  * \brief Destroys an execution action.
240  *
241  * Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
242  * \param action The execution action to destroy
243  */
244 void simcall_host_execution_destroy(smx_action_t execution)
245 {
246   smx_simcall_t simcall = SIMIX_simcall_mine();
247
248   simcall->call = SIMCALL_HOST_EXECUTION_DESTROY;
249   simcall->host_execution_destroy.execution = execution;
250   SIMIX_simcall_push(simcall->issuer);
251 }
252
253 /**
254  * \brief Cancels an execution action.
255  *
256  * This functions stops the execution. It calls a surf function.
257  * \param action The execution action to cancel
258  */
259 void simcall_host_execution_cancel(smx_action_t execution)
260 {
261   smx_simcall_t simcall = SIMIX_simcall_mine();
262
263   simcall->call = SIMCALL_HOST_EXECUTION_CANCEL;
264   simcall->host_execution_cancel.execution = execution;
265   SIMIX_simcall_push(simcall->issuer);
266 }
267
268 /**
269  * \brief Returns how much of an execution action remains to be done.
270  *
271  * \param Action The execution action
272  * \return The remaining amount
273  */
274 double simcall_host_execution_get_remains(smx_action_t execution)
275 {
276   smx_simcall_t simcall = SIMIX_simcall_mine();
277
278   simcall->call = SIMCALL_HOST_EXECUTION_GET_REMAINS;
279   simcall->host_execution_get_remains.execution = execution;
280   SIMIX_simcall_push(simcall->issuer);
281   return simcall->host_execution_get_remains.result;
282 }
283
284 /**
285  * \brief Returns the state of an execution action.
286  *
287  * \param execution The execution action
288  * \return The state
289  */
290 e_smx_state_t simcall_host_execution_get_state(smx_action_t execution)
291 {
292   smx_simcall_t simcall = SIMIX_simcall_mine();
293
294   simcall->call = SIMCALL_HOST_EXECUTION_GET_STATE;
295   simcall->host_execution_get_state.execution = execution;
296   SIMIX_simcall_push(simcall->issuer);
297   return simcall->host_execution_get_state.result;
298 }
299
300 /**
301  * \brief Changes the priority of an execution action.
302  *
303  * This functions changes the priority only. It calls a surf function.
304  * \param execution The execution action
305  * \param priority The new priority
306  */
307 void simcall_host_execution_set_priority(smx_action_t execution, double priority)
308 {
309   /* checking for infinite values */
310   xbt_assert(isfinite(priority), "priority is not finite!");
311   
312   smx_simcall_t simcall = SIMIX_simcall_mine();
313
314   simcall->call = SIMCALL_HOST_EXECUTION_SET_PRIORITY;
315   simcall->host_execution_set_priority.execution = execution;
316   simcall->host_execution_set_priority.priority = priority;
317   SIMIX_simcall_push(simcall->issuer);
318 }
319
320 /**
321  * \brief Waits for the completion of an execution action and destroy it.
322  *
323  * \param execution The execution action
324  */
325 e_smx_state_t simcall_host_execution_wait(smx_action_t execution)
326 {
327   smx_simcall_t simcall = SIMIX_simcall_mine();
328
329   simcall->call = SIMCALL_HOST_EXECUTION_WAIT;
330   simcall->host_execution_wait.execution = execution;
331   SIMIX_simcall_push(simcall->issuer);
332   return simcall->host_execution_wait.result;
333 }
334
335 /**
336  * \brief Creates and runs a new SIMIX process.
337  *
338  * The structure and the corresponding thread are created and put in the list of ready processes.
339  *
340  * \param process the process created will be stored in this pointer
341  * \param name a name for the process. It is for user-level information and can be NULL.
342  * \param code the main function of the process
343  * \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.
344  * It can be retrieved with the function \ref simcall_process_get_data.
345  * \param hostname name of the host where the new agent is executed.
346  * \param kill_time time when the process is killed
347  * \param argc first argument passed to \a code
348  * \param argv second argument passed to \a code
349  * \param properties the properties of the process
350  */
351 void simcall_process_create(smx_process_t *process, const char *name,
352                               xbt_main_func_t code,
353                               void *data,
354                               const char *hostname,
355                               double kill_time,
356                               int argc, char **argv,
357                               xbt_dict_t properties)
358 {
359   smx_simcall_t simcall = SIMIX_simcall_mine();
360
361   simcall->call = SIMCALL_PROCESS_CREATE;
362   simcall->process_create.process = process;
363   simcall->process_create.name = name;
364   simcall->process_create.code = code;
365   simcall->process_create.data = data;
366   simcall->process_create.hostname = hostname;
367   simcall->process_create.kill_time = kill_time;
368   simcall->process_create.argc = argc;
369   simcall->process_create.argv = argv;
370   simcall->process_create.properties = properties;
371   SIMIX_simcall_push(simcall->issuer);
372 }
373
374 /** \brief Kills a SIMIX process.
375  *
376  * This function simply kills a  process.
377  *
378  * \param process poor victim
379  */
380 void simcall_process_kill(smx_process_t process)
381 {
382   smx_simcall_t simcall = SIMIX_simcall_mine();
383
384   simcall->call = SIMCALL_PROCESS_KILL;
385   simcall->process_kill.process = process;
386   SIMIX_simcall_push(simcall->issuer);
387 }
388
389 /** \brief Kills all SIMIX processes.
390  */
391 void simcall_process_killall(void)
392 {
393   smx_simcall_t simcall = SIMIX_simcall_mine();
394
395   simcall->call = SIMCALL_PROCESS_KILLALL;
396   SIMIX_simcall_push(simcall->issuer);
397 }
398
399 /** \brief Cleans up a SIMIX process.
400  * \param process poor victim (must have already been killed)
401  */
402 void simcall_process_cleanup(smx_process_t process)
403 {
404   smx_simcall_t simcall = SIMIX_simcall_mine();
405
406   simcall->call = SIMCALL_PROCESS_CLEANUP;
407   simcall->process_cleanup.process = process;
408   SIMIX_simcall_push(simcall->issuer);
409 }
410
411 /**
412  * \brief Migrates an agent to another location.
413  *
414  * This function changes the value of the host on which \a process is running.
415  *
416  * \param process the process to migrate
417  * \param source name of the previous host
418  * \param dest name of the new host
419  */
420 void simcall_process_change_host(smx_process_t process, smx_host_t dest)
421 {
422   smx_simcall_t simcall = SIMIX_simcall_mine();
423
424   simcall->call = SIMCALL_PROCESS_CHANGE_HOST;
425   simcall->process_change_host.process = process;
426   simcall->process_change_host.dest = dest;
427   SIMIX_simcall_push(simcall->issuer);
428 }
429
430 /**
431  * \brief Suspends a process.
432  *
433  * This function suspends the process by suspending the action
434  * it was waiting for completion.
435  *
436  * \param process a SIMIX process
437  */
438 void simcall_process_suspend(smx_process_t process)
439 {
440   xbt_assert(process, "Invalid parameters");
441
442   smx_simcall_t simcall = SIMIX_simcall_mine();
443
444   simcall->call = SIMCALL_PROCESS_SUSPEND;
445   simcall->process_suspend.process = process;
446   SIMIX_simcall_push(simcall->issuer);
447 }
448
449 /**
450  * \brief Resumes a suspended process.
451  *
452  * This function resumes a suspended process by resuming the action
453  * it was waiting for completion.
454  *
455  * \param process a SIMIX process
456  */
457 void simcall_process_resume(smx_process_t process)
458 {
459   smx_simcall_t simcall = SIMIX_simcall_mine();
460
461   simcall->call = SIMCALL_PROCESS_RESUME;
462   simcall->process_resume.process = process;
463   SIMIX_simcall_push(simcall->issuer);
464 }
465
466 /**
467  * \brief Returns the amount of SIMIX processes in the system
468  *
469  * Maestro internal process is not counted, only user code processes are
470  */
471 int simcall_process_count(void)
472 {
473   smx_simcall_t simcall = SIMIX_simcall_mine();
474
475   simcall->call = SIMCALL_PROCESS_COUNT;
476   SIMIX_simcall_push(simcall->issuer);
477   return simcall->process_count.result;
478 }
479
480 /**
481  * \brief Return the user data of a #smx_process_t.
482  * \param process a SIMIX process
483  * \return the user data of this process
484  */
485 void* simcall_process_get_data(smx_process_t process)
486 {
487   if (process == SIMIX_process_self()) {
488     /* avoid a simcall if this function is called by the process itself */
489     return SIMIX_process_get_data(process);
490   }
491
492   smx_simcall_t simcall = SIMIX_simcall_mine();
493
494   simcall->call = SIMCALL_PROCESS_GET_DATA;
495   simcall->process_get_data.process = process;
496   SIMIX_simcall_push(simcall->issuer);
497   return simcall->process_get_data.result;
498 }
499
500 /**
501  * \brief Set the user data of a #m_process_t.
502  *
503  * This functions checks whether \a process is a valid pointer or not and set the user data associated to \a process if it is possible.
504  * \param process SIMIX process
505  * \param data User data
506  */
507 void simcall_process_set_data(smx_process_t process, void *data)
508 {
509   if (process == SIMIX_process_self()) {
510     /* avoid a simcall if this function is called by the process itself */
511     SIMIX_process_self_set_data(process, data);
512   }
513   else {
514
515     smx_simcall_t simcall = SIMIX_simcall_mine();
516
517     simcall->call = SIMCALL_PROCESS_SET_DATA;
518     simcall->process_set_data.process = process;
519     simcall->process_set_data.data = data;
520     SIMIX_simcall_push(simcall->issuer);
521   }
522 }
523
524 /**
525  * \brief Set the kill time of a process.
526  *
527  * \param process a process
528  * \param kill_time a double
529  */
530 void simcall_process_set_kill_time(smx_process_t process, double kill_time)
531 {
532
533   if (kill_time > SIMIX_get_clock()) {
534     if (simix_global->kill_process_function) {
535       XBT_DEBUG("Set kill time %f for process %s(%s)",kill_time, process->name,
536           process->smx_host->name);
537       SIMIX_timer_set(kill_time, simix_global->kill_process_function, process);
538     }
539   }
540 }
541
542 /**
543  * \brief Return the location on which an agent is running.
544  *
545  * This functions checks whether \a process is a valid pointer or not and return the m_host_t corresponding to the location on which \a process is running.
546  * \param process SIMIX process
547  * \return SIMIX host
548  */
549 smx_host_t simcall_process_get_host(smx_process_t process)
550 {
551   smx_simcall_t simcall = SIMIX_simcall_mine();
552
553   simcall->call = SIMCALL_PROCESS_GET_HOST;
554   simcall->process_get_host.process = process;
555   SIMIX_simcall_push(simcall->issuer);
556   return simcall->process_get_host.result;
557 }
558
559 /**
560  * \brief Return the name of an agent.
561  *
562  * This functions checks whether \a process is a valid pointer or not and return its name.
563  * \param process SIMIX process
564  * \return The process name
565  */
566 const char* simcall_process_get_name(smx_process_t process)
567 {
568   if (process == SIMIX_process_self()) {
569     /* avoid a simcall if this function is called by the process itself */
570     return process->name;
571   }
572
573   smx_simcall_t simcall = SIMIX_simcall_mine();
574
575   simcall->call = SIMCALL_PROCESS_GET_NAME;
576   simcall->process_get_name.process = process;
577   SIMIX_simcall_push(simcall->issuer);
578   return simcall->process_get_name.result;
579 }
580
581 /**
582  * \brief Returns true if the process is suspended .
583  *
584  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
585  * \param process SIMIX process
586  * \return 1, if the process is suspended, else 0.
587  */
588 int simcall_process_is_suspended(smx_process_t process)
589 {
590   smx_simcall_t simcall = SIMIX_simcall_mine();
591
592   simcall->call = SIMCALL_PROCESS_IS_SUSPENDED;
593   simcall->process_is_suspended.process = process;
594   SIMIX_simcall_push(simcall->issuer);
595   return simcall->process_is_suspended.result;
596 }
597
598 /** \ingroup m_process_management
599  * \brief Return the properties
600  *
601  * This functions returns the properties associated with this process
602  */
603 xbt_dict_t simcall_process_get_properties(smx_process_t process)
604 {
605   smx_simcall_t simcall = SIMIX_simcall_mine();
606
607   simcall->call = SIMCALL_PROCESS_GET_PROPERTIES;
608   simcall->process_get_properties.process = process;
609   SIMIX_simcall_push(simcall->issuer);
610   return simcall->process_get_properties.result;
611 }
612 /** \ingroup m_process_management
613  * \brief Add an on_exit function
614  * Add an on_exit function which will be executed when the process exits/is killed.
615  */
616 XBT_PUBLIC(void) simcall_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
617   smx_simcall_t simcall = SIMIX_simcall_mine();
618
619   simcall->call = SIMCALL_PROCESS_ON_EXIT;
620   simcall->process_on_exit.process = process;
621   simcall->process_on_exit.fun = fun;
622   simcall->process_on_exit.data = data;
623
624   SIMIX_simcall_push(simcall->issuer);
625 }
626 /** \brief Creates a new sleep SIMIX action.
627  *
628  * This function creates a SURF action and allocates the data necessary
629  * to create the SIMIX action. It can raise a host_error exception if the
630  * host crashed. The default SIMIX name of the action is "sleep".
631  *
632  *   \param duration Time duration of the sleep.
633  *   \return A result telling whether the sleep was successful
634  */
635 e_smx_state_t simcall_process_sleep(double duration)
636 {
637   /* checking for infinite values */
638   xbt_assert(isfinite(duration), "duration is not finite!");
639   
640   smx_simcall_t simcall = SIMIX_simcall_mine();
641
642   simcall->call = SIMCALL_PROCESS_SLEEP;
643   simcall->process_sleep.duration = duration;
644   SIMIX_simcall_push(simcall->issuer);
645   return simcall->process_sleep.result;
646 }
647
648 /**
649  *  \brief Creates a new rendez-vous point
650  *  \param name The name of the rendez-vous point
651  *  \return The created rendez-vous point
652  */
653 smx_rdv_t simcall_rdv_create(const char *name)
654 {
655   smx_simcall_t simcall = SIMIX_simcall_mine();
656
657   simcall->call = SIMCALL_RDV_CREATE;
658   simcall->rdv_create.name = name;
659
660   SIMIX_simcall_push(simcall->issuer);
661   return simcall->rdv_create.result;
662 }
663
664
665 /**
666  *  \brief Destroy a rendez-vous point
667  *  \param name The rendez-vous point to destroy
668  */
669 void simcall_rdv_destroy(smx_rdv_t rdv)
670 {
671   smx_simcall_t simcall = SIMIX_simcall_mine();
672
673   simcall->call = SIMCALL_RDV_DESTROY;
674   simcall->rdv_destroy.rdv = rdv;
675
676   SIMIX_simcall_push(simcall->issuer);
677 }
678
679 smx_rdv_t simcall_rdv_get_by_name(const char *name)
680 {
681   xbt_assert(name != NULL, "Invalid parameter for simcall_rdv_get_by_name (name is NULL)");
682
683   /* FIXME: this is a horrible lost of performance, so we hack it out by
684    * skipping the simcall (for now). It works in parallel, it won't work on
685    * distributed but probably we will change MSG for that. */
686
687   /*
688   smx_simcall_t simcall = simcall_mine();
689   simcall->call = SIMCALL_RDV_GEY_BY_NAME;
690   simcall->rdv_get_by_name.name = name;
691   SIMIX_simcall_push(simcall->issuer);
692   return simcall->rdv_get_by_name.result;*/
693
694   return SIMIX_rdv_get_by_name(name);
695 }
696
697 /**
698  *  \brief Counts the number of communication actions of a given host pending
699  *         on a rendez-vous point.
700  *  \param rdv The rendez-vous point
701  *  \param host The host to be counted
702  *  \return The number of comm actions pending in the rdv
703  */
704 int simcall_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host)
705 {
706   smx_simcall_t simcall = SIMIX_simcall_mine();
707
708   simcall->call = SIMCALL_RDV_COMM_COUNT_BY_HOST;
709   simcall->rdv_comm_count_by_host.rdv = rdv;
710   simcall->rdv_comm_count_by_host.host = host;
711
712   SIMIX_simcall_push(simcall->issuer);
713   return simcall->rdv_comm_count_by_host.result;
714 }
715
716 /**
717  *  \brief returns the communication at the head of the rendez-vous
718  *  \param rdv The rendez-vous point
719  *  \return The communication or NULL if empty
720  */
721 smx_action_t simcall_rdv_get_head(smx_rdv_t rdv)
722 {
723   smx_simcall_t simcall = SIMIX_simcall_mine();
724
725   simcall->call = SIMCALL_RDV_GET_HEAD;
726   simcall->rdv_get_head.rdv = rdv;
727
728   SIMIX_simcall_push(simcall->issuer);
729   return simcall->rdv_get_head.result;
730 }
731
732 void simcall_comm_send(smx_rdv_t rdv, double task_size, double rate,
733                          void *src_buff, size_t src_buff_size,
734                          int (*match_fun)(void *, void *, smx_action_t), void *data,
735                          double timeout)
736 {
737   /* checking for infinite values */
738   xbt_assert(isfinite(task_size), "task_size is not finite!");
739   xbt_assert(isfinite(rate), "rate is not finite!");
740   xbt_assert(isfinite(timeout), "timeout is not finite!");
741   
742   xbt_assert(rdv, "No rendez-vous point defined for send");
743
744   if (MC_IS_ENABLED) {
745     /* the model-checker wants two separate simcalls */
746     smx_action_t comm = simcall_comm_isend(rdv, task_size, rate,
747         src_buff, src_buff_size, match_fun, NULL, data, 0);
748     simcall_comm_wait(comm, timeout);
749   }
750   else {
751     smx_simcall_t simcall = SIMIX_simcall_mine();
752
753     simcall->call = SIMCALL_COMM_SEND;
754     simcall->comm_send.rdv = rdv;
755     simcall->comm_send.task_size = task_size;
756     simcall->comm_send.rate = rate;
757     simcall->comm_send.src_buff = src_buff;
758     simcall->comm_send.src_buff_size = src_buff_size;
759     simcall->comm_send.match_fun = match_fun;
760     simcall->comm_send.data = data;
761     simcall->comm_send.timeout = timeout;
762
763     SIMIX_simcall_push(simcall->issuer);
764   }
765 }
766
767 smx_action_t simcall_comm_isend(smx_rdv_t rdv, double task_size, double rate,
768                               void *src_buff, size_t src_buff_size,
769                               int (*match_fun)(void *, void *, smx_action_t),
770                               void (*clean_fun)(void *),
771                               void *data,
772                               int detached)
773 {
774   /* checking for infinite values */
775   xbt_assert(isfinite(task_size), "task_size is not finite!");
776   xbt_assert(isfinite(rate), "rate is not finite!");
777   
778   xbt_assert(rdv, "No rendez-vous point defined for isend");
779
780   smx_simcall_t simcall = SIMIX_simcall_mine();
781
782   simcall->call = SIMCALL_COMM_ISEND;
783   simcall->comm_isend.rdv = rdv;
784   simcall->comm_isend.task_size = task_size;
785   simcall->comm_isend.rate = rate;
786   simcall->comm_isend.src_buff = src_buff;
787   simcall->comm_isend.src_buff_size = src_buff_size;
788   simcall->comm_isend.match_fun = match_fun;
789   simcall->comm_isend.clean_fun = clean_fun;
790   simcall->comm_isend.data = data;
791   simcall->comm_isend.detached = detached;
792
793   SIMIX_simcall_push(simcall->issuer);
794   return simcall->comm_isend.result;
795 }
796
797 void simcall_comm_recv(smx_rdv_t rdv, void *dst_buff, size_t * dst_buff_size,
798                          int (*match_fun)(void *, void *, smx_action_t), void *data, double timeout)
799 {
800   xbt_assert(isfinite(timeout), "timeout is not finite!");
801   xbt_assert(rdv, "No rendez-vous point defined for recv");
802
803   if (MC_IS_ENABLED) {
804     /* the model-checker wants two separate simcalls */
805     smx_action_t comm = simcall_comm_irecv(rdv, dst_buff, dst_buff_size,
806         match_fun, data);
807     simcall_comm_wait(comm, timeout);
808   }
809   else {
810     smx_simcall_t simcall = SIMIX_simcall_mine();
811
812     simcall->call = SIMCALL_COMM_RECV;
813     simcall->comm_recv.rdv = rdv;
814     simcall->comm_recv.dst_buff = dst_buff;
815     simcall->comm_recv.dst_buff_size = dst_buff_size;
816     simcall->comm_recv.match_fun = match_fun;
817     simcall->comm_recv.data = data;
818     simcall->comm_recv.timeout = timeout;
819
820     SIMIX_simcall_push(simcall->issuer);
821   }
822 }
823
824 smx_action_t simcall_comm_irecv(smx_rdv_t rdv, void *dst_buff, size_t * dst_buff_size,
825                                   int (*match_fun)(void *, void *, smx_action_t), void *data)
826 {
827   xbt_assert(rdv, "No rendez-vous point defined for irecv");
828
829   smx_simcall_t simcall = SIMIX_simcall_mine();
830
831   simcall->call = SIMCALL_COMM_IRECV;
832   simcall->comm_irecv.rdv = rdv;
833   simcall->comm_irecv.dst_buff = dst_buff;
834   simcall->comm_irecv.dst_buff_size = dst_buff_size;
835   simcall->comm_irecv.match_fun = match_fun;
836   simcall->comm_irecv.data = data;
837
838   SIMIX_simcall_push(simcall->issuer);
839   return simcall->comm_irecv.result;
840 }
841
842 void simcall_comm_destroy(smx_action_t comm)
843 {
844   xbt_assert(comm, "Invalid parameter");
845
846   /* FIXME remove this simcall type: comms are auto-destroyed now */
847
848   /*
849   smx_simcall_t simcall = simcall_mine();
850
851   simcall->call = SIMCALL_COMM_DESTROY;
852   simcall->comm_destroy.comm = comm;
853
854   SIMIX_simcall_push(simcall->issuer);
855   */
856 }
857
858 void simcall_comm_cancel(smx_action_t comm)
859 {
860   smx_simcall_t simcall = SIMIX_simcall_mine();
861
862   simcall->call = SIMCALL_COMM_CANCEL;
863   simcall->comm_cancel.comm = comm;
864
865   SIMIX_simcall_push(simcall->issuer);
866 }
867
868 unsigned int simcall_comm_waitany(xbt_dynar_t comms)
869 {
870   smx_simcall_t simcall = SIMIX_simcall_mine();
871
872   simcall->call = SIMCALL_COMM_WAITANY;
873   simcall->comm_waitany.comms = comms;
874
875   SIMIX_simcall_push(simcall->issuer);
876   return simcall->comm_waitany.result;
877 }
878
879 int simcall_comm_testany(xbt_dynar_t comms)
880 {
881   smx_simcall_t simcall = SIMIX_simcall_mine();
882   if (xbt_dynar_is_empty(comms))
883     return -1;
884
885   simcall->call = SIMCALL_COMM_TESTANY;
886   simcall->comm_testany.comms = comms;
887
888   SIMIX_simcall_push(simcall->issuer);
889   return simcall->comm_testany.result;
890 }
891
892 void simcall_comm_wait(smx_action_t comm, double timeout)
893 {
894   xbt_assert(isfinite(timeout), "timeout is not finite!");
895   
896   smx_simcall_t simcall = SIMIX_simcall_mine();
897
898   simcall->call = SIMCALL_COMM_WAIT;
899   simcall->comm_wait.comm = comm;
900   simcall->comm_wait.timeout = timeout;
901
902   SIMIX_simcall_push(simcall->issuer);
903 }
904
905 #ifdef HAVE_TRACING
906 /**
907  * \brief Set the category of an action.
908  *
909  * This functions changes the category only. It calls a surf function.
910  * \param execution The execution action
911  * \param category The tracing category
912  */
913 void simcall_set_category(smx_action_t action, const char *category)
914 {
915   if (category == NULL) {
916     return;
917   }
918
919   smx_simcall_t simcall = SIMIX_simcall_mine();
920
921   simcall->call = SIMCALL_SET_CATEGORY;
922   simcall->set_category.action = action;
923   simcall->set_category.category = category;
924
925   SIMIX_simcall_push(simcall->issuer);
926 }
927 #endif
928
929 int simcall_comm_test(smx_action_t comm)
930 {
931   smx_simcall_t simcall = SIMIX_simcall_mine();
932
933   simcall->call = SIMCALL_COMM_TEST;
934   simcall->comm_test.comm = comm;
935
936   SIMIX_simcall_push(simcall->issuer);
937   return simcall->comm_test.result;
938 }
939
940 double simcall_comm_get_remains(smx_action_t comm)
941 {
942   smx_simcall_t simcall = SIMIX_simcall_mine();
943
944   simcall->call = SIMCALL_COMM_GET_REMAINS;
945   simcall->comm_get_remains.comm = comm;
946
947   SIMIX_simcall_push(simcall->issuer);
948   return simcall->comm_get_remains.result;
949 }
950
951 e_smx_state_t simcall_comm_get_state(smx_action_t comm)
952 {
953   smx_simcall_t simcall = SIMIX_simcall_mine();
954
955   simcall->call = SIMCALL_COMM_GET_STATE;
956   simcall->comm_get_state.comm = comm;
957
958   SIMIX_simcall_push(simcall->issuer);
959   return simcall->comm_get_state.result;
960 }
961
962 void *simcall_comm_get_src_data(smx_action_t comm)
963 {
964   smx_simcall_t simcall = SIMIX_simcall_mine();
965
966   simcall->call = SIMCALL_COMM_GET_SRC_DATA;
967   simcall->comm_get_src_data.comm = comm;
968
969   SIMIX_simcall_push(simcall->issuer);
970   return simcall->comm_get_src_data.result;
971 }
972
973 void *simcall_comm_get_dst_data(smx_action_t comm)
974 {
975   smx_simcall_t simcall = SIMIX_simcall_mine();
976
977   simcall->call = SIMCALL_COMM_GET_DST_DATA;
978   simcall->comm_get_dst_data.comm = comm;
979
980   SIMIX_simcall_push(simcall->issuer);
981   return simcall->comm_get_dst_data.result;
982 }
983
984 smx_process_t simcall_comm_get_src_proc(smx_action_t comm)
985 {
986   smx_simcall_t simcall = SIMIX_simcall_mine();
987
988   simcall->call = SIMCALL_COMM_GET_SRC_PROC;
989   simcall->comm_get_src_proc.comm = comm;
990
991   SIMIX_simcall_push(simcall->issuer);
992   return simcall->comm_get_src_proc.result;
993 }
994
995 smx_process_t simcall_comm_get_dst_proc(smx_action_t comm)
996 {
997   smx_simcall_t simcall = SIMIX_simcall_mine();
998
999   simcall->call = SIMCALL_COMM_GET_DST_PROC;
1000   simcall->comm_get_dst_proc.comm = comm;
1001
1002   SIMIX_simcall_push(simcall->issuer);
1003   return simcall->comm_get_dst_proc.result;
1004 }
1005
1006 #ifdef HAVE_LATENCY_BOUND_TRACKING
1007 int simcall_comm_is_latency_bounded(smx_action_t comm)
1008 {
1009   smx_simcall_t simcall = SIMIX_simcall_mine();
1010
1011   simcall->call = SIMCALL_COMM_IS_LATENCY_BOUNDED;
1012   simcall->comm_is_latency_bounded.comm = comm;
1013
1014   SIMIX_simcall_push(simcall->issuer);
1015   return simcall->comm_is_latency_bounded.result;
1016 }
1017 #endif
1018
1019 smx_mutex_t simcall_mutex_init(void)
1020 {
1021   if(!simix_global) {
1022     fprintf(stderr,"You must run MSG_init or gras_init before using MSG or GRAS\n"); // I would have loved using xbt_die but I can't since it is not initialized yet... :)
1023     abort();
1024   }
1025   smx_simcall_t simcall = SIMIX_simcall_mine();
1026
1027   simcall->call = SIMCALL_MUTEX_INIT;
1028
1029   SIMIX_simcall_push(simcall->issuer);
1030   return simcall->mutex_init.result;
1031 }
1032
1033 void simcall_mutex_destroy(smx_mutex_t mutex)
1034 {
1035   smx_simcall_t simcall = SIMIX_simcall_mine();
1036
1037   simcall->call = SIMCALL_MUTEX_DESTROY;
1038   simcall->mutex_destroy.mutex = mutex;
1039
1040   SIMIX_simcall_push(simcall->issuer);
1041 }
1042
1043 void simcall_mutex_lock(smx_mutex_t mutex)
1044 {
1045   smx_simcall_t simcall = SIMIX_simcall_mine();
1046
1047   simcall->call = SIMCALL_MUTEX_LOCK;
1048   simcall->mutex_lock.mutex = mutex;
1049
1050   SIMIX_simcall_push(simcall->issuer);
1051 }
1052
1053 int simcall_mutex_trylock(smx_mutex_t mutex)
1054 {
1055   smx_simcall_t simcall = SIMIX_simcall_mine();
1056
1057   simcall->call = SIMCALL_MUTEX_TRYLOCK;
1058   simcall->mutex_trylock.mutex = mutex;
1059
1060   SIMIX_simcall_push(simcall->issuer);
1061   return simcall->mutex_trylock.result;
1062 }
1063
1064 void simcall_mutex_unlock(smx_mutex_t mutex)
1065 {
1066   smx_simcall_t simcall = SIMIX_simcall_mine();
1067
1068   simcall->call = SIMCALL_MUTEX_UNLOCK;
1069   simcall->mutex_unlock.mutex = mutex;
1070
1071   SIMIX_simcall_push(simcall->issuer);
1072 }
1073
1074
1075 smx_cond_t simcall_cond_init(void)
1076 {
1077   smx_simcall_t simcall = SIMIX_simcall_mine();
1078
1079   simcall->call = SIMCALL_COND_INIT;
1080
1081   SIMIX_simcall_push(simcall->issuer);
1082   return simcall->cond_init.result;
1083 }
1084
1085 void simcall_cond_destroy(smx_cond_t cond)
1086 {
1087   smx_simcall_t simcall = SIMIX_simcall_mine();
1088
1089   simcall->call = SIMCALL_COND_DESTROY;
1090   simcall->cond_destroy.cond = cond;
1091
1092   SIMIX_simcall_push(simcall->issuer);
1093 }
1094
1095 void simcall_cond_signal(smx_cond_t cond)
1096 {
1097   smx_simcall_t simcall = SIMIX_simcall_mine();
1098
1099   simcall->call = SIMCALL_COND_SIGNAL;
1100   simcall->cond_signal.cond = cond;
1101
1102   SIMIX_simcall_push(simcall->issuer);
1103 }
1104
1105 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
1106 {
1107   smx_simcall_t simcall = SIMIX_simcall_mine();
1108
1109   simcall->call = SIMCALL_COND_WAIT;
1110   simcall->cond_wait.cond = cond;
1111   simcall->cond_wait.mutex = mutex;
1112
1113   SIMIX_simcall_push(simcall->issuer);
1114 }
1115
1116 void simcall_cond_wait_timeout(smx_cond_t cond,
1117                                  smx_mutex_t mutex,
1118                                  double timeout)
1119 {
1120   xbt_assert(isfinite(timeout), "timeout is not finite!");
1121   
1122   smx_simcall_t simcall = SIMIX_simcall_mine();
1123
1124   simcall->call = SIMCALL_COND_WAIT_TIMEOUT;
1125   simcall->cond_wait_timeout.cond = cond;
1126   simcall->cond_wait_timeout.mutex = mutex;
1127   simcall->cond_wait_timeout.timeout = timeout;
1128
1129   SIMIX_simcall_push(simcall->issuer);
1130 }
1131
1132 void simcall_cond_broadcast(smx_cond_t cond)
1133 {
1134   smx_simcall_t simcall = SIMIX_simcall_mine();
1135
1136   simcall->call = SIMCALL_COND_BROADCAST;
1137   simcall->cond_broadcast.cond = cond;
1138
1139   SIMIX_simcall_push(simcall->issuer);
1140 }
1141
1142
1143 smx_sem_t simcall_sem_init(int capacity)
1144 {
1145   smx_simcall_t simcall = SIMIX_simcall_mine();
1146
1147   simcall->call = SIMCALL_SEM_INIT;
1148   simcall->sem_init.capacity = capacity;
1149
1150   SIMIX_simcall_push(simcall->issuer);
1151   return simcall->sem_init.result;
1152 }
1153
1154 void simcall_sem_destroy(smx_sem_t sem)
1155 {
1156   smx_simcall_t simcall = SIMIX_simcall_mine();
1157
1158   simcall->call = SIMCALL_SEM_DESTROY;
1159   simcall->sem_destroy.sem = sem;
1160
1161   SIMIX_simcall_push(simcall->issuer);
1162 }
1163
1164 void simcall_sem_release(smx_sem_t sem)
1165 {
1166   smx_simcall_t simcall = SIMIX_simcall_mine();
1167
1168   simcall->call = SIMCALL_SEM_RELEASE;
1169   simcall->sem_release.sem = sem;
1170
1171   SIMIX_simcall_push(simcall->issuer);
1172 }
1173
1174 int simcall_sem_would_block(smx_sem_t sem)
1175 {
1176   smx_simcall_t simcall = SIMIX_simcall_mine();
1177
1178   simcall->call = SIMCALL_SEM_WOULD_BLOCK;
1179   simcall->sem_would_block.sem = sem;
1180
1181   SIMIX_simcall_push(simcall->issuer);
1182   return simcall->sem_would_block.result;
1183 }
1184
1185 void simcall_sem_acquire(smx_sem_t sem)
1186 {
1187   smx_simcall_t simcall = SIMIX_simcall_mine();
1188
1189   simcall->call = SIMCALL_SEM_ACQUIRE;
1190   simcall->sem_acquire.sem = sem;
1191
1192   SIMIX_simcall_push(simcall->issuer);
1193 }
1194
1195 void simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
1196 {
1197   xbt_assert(isfinite(timeout), "timeout is not finite!");
1198   
1199   smx_simcall_t simcall = SIMIX_simcall_mine();
1200
1201   simcall->call = SIMCALL_SEM_ACQUIRE_TIMEOUT;
1202   simcall->sem_acquire_timeout.sem = sem;
1203   simcall->sem_acquire_timeout.timeout = timeout;
1204
1205   SIMIX_simcall_push(simcall->issuer);
1206 }
1207
1208 int simcall_sem_get_capacity(smx_sem_t sem)
1209 {
1210   smx_simcall_t simcall = SIMIX_simcall_mine();
1211
1212   simcall->call = SIMCALL_SEM_GET_CAPACITY;
1213   simcall->sem_get_capacity.sem = sem;
1214
1215   SIMIX_simcall_push(simcall->issuer);
1216   return simcall->sem_get_capacity.result;
1217 }
1218
1219 double simcall_file_read(void* ptr, size_t size, size_t nmemb, smx_file_t stream)
1220 {
1221   smx_simcall_t simcall = SIMIX_simcall_mine();
1222
1223   simcall->call = SIMCALL_FILE_READ;
1224   simcall->file_read.ptr = ptr;
1225   simcall->file_read.size = size;
1226   simcall->file_read.nmemb = nmemb;
1227   simcall->file_read.stream = stream;
1228   SIMIX_simcall_push(simcall->issuer);
1229
1230   return simcall->file_read.result;
1231 }
1232
1233 size_t simcall_file_write(const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
1234 {
1235   smx_simcall_t simcall = SIMIX_simcall_mine();
1236
1237   simcall->call = SIMCALL_FILE_WRITE;
1238   simcall->file_write.ptr = ptr;
1239   simcall->file_write.size = size;
1240   simcall->file_write.nmemb = nmemb;
1241   simcall->file_write.stream = stream;
1242   SIMIX_simcall_push(simcall->issuer);
1243
1244   return simcall->file_write.result;
1245 }
1246
1247 smx_file_t simcall_file_open(const char* mount, const char* path, const char* mode)
1248 {
1249   smx_simcall_t simcall = SIMIX_simcall_mine();
1250
1251   simcall->call = SIMCALL_FILE_OPEN;
1252   simcall->file_open.mount = mount;
1253   simcall->file_open.path = path;
1254   simcall->file_open.mode = mode;
1255   SIMIX_simcall_push(simcall->issuer);
1256
1257   return simcall->file_open.result;
1258 }
1259
1260 int simcall_file_close(smx_file_t fp)
1261 {
1262   smx_simcall_t simcall = SIMIX_simcall_mine();
1263
1264   simcall->call = SIMCALL_FILE_CLOSE;
1265   simcall->file_close.fp = fp;
1266   SIMIX_simcall_push(simcall->issuer);
1267
1268   return simcall->file_close.result;
1269 }
1270
1271 int simcall_file_stat(smx_file_t fd, s_file_stat_t *buf)
1272 {
1273   smx_simcall_t simcall = SIMIX_simcall_mine();
1274   simcall->call = SIMCALL_FILE_STAT;
1275   simcall->file_stat.fd = fd;
1276
1277   SIMIX_simcall_push(simcall->issuer);
1278
1279   *buf = simcall->file_stat.buf;
1280
1281   return simcall->file_stat.result;
1282 }
1283
1284 /* ************************************************************************** */
1285
1286 /** @brief returns a printable string representing a simcall */
1287 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
1288   return simcall_names[kind];
1289 }