Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix: do not use the action state to determine the result of TEST requests when...
[simgrid.git] / src / simix / smx_network.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/log.h"
9 #include "mc/mc.h"
10 #include "xbt/dict.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13                                 "Logging specific to SIMIX (network)");
14
15 static xbt_dict_t rdv_points = NULL;
16
17 static XBT_INLINE void SIMIX_comm_start(smx_action_t action);
18 static void SIMIX_comm_finish(smx_action_t action);
19 static void SIMIX_waitany_req_remove_from_actions(smx_req_t req);
20 static void SIMIX_comm_copy_data(smx_action_t comm);
21 static smx_action_t SIMIX_comm_new(e_smx_comm_type_t type);
22 static XBT_INLINE void SIMIX_comm_wait_for_completion(smx_action_t comm,
23                                                       double timeout);
24 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm);
25 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm);
26 static smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
27                                                                                   int (*match_fun)(void *, void *), void *);
28 static void SIMIX_rdv_free(void *data);
29
30 void SIMIX_network_init(void)
31 {
32   rdv_points = xbt_dict_new();
33 }
34
35 void SIMIX_network_exit(void)
36 {
37   xbt_dict_free(&rdv_points);
38 }
39
40 /******************************************************************************/
41 /*                           Rendez-Vous Points                               */
42 /******************************************************************************/
43
44 smx_rdv_t SIMIX_rdv_create(const char *name)
45 {
46   /* two processes may have pushed the same rdv_create request at the same time */
47   smx_rdv_t rdv = name ? xbt_dict_get_or_null(rdv_points, name) : NULL;
48
49   if (!rdv) {
50     rdv = xbt_new0(s_smx_rvpoint_t, 1);
51     rdv->name = name ? xbt_strdup(name) : NULL;
52     rdv->comm_fifo = xbt_fifo_new();
53
54     if (rdv->name)
55       xbt_dict_set(rdv_points, rdv->name, rdv, SIMIX_rdv_free);
56   }
57   return rdv;
58 }
59
60 void SIMIX_rdv_destroy(smx_rdv_t rdv)
61 {
62   if (rdv->name)
63     xbt_dict_remove(rdv_points, rdv->name);
64 }
65
66 void SIMIX_rdv_free(void *data)
67 {
68   smx_rdv_t rdv = (smx_rdv_t) data;
69   if (rdv->name)
70     xbt_free(rdv->name);
71   xbt_fifo_free(rdv->comm_fifo);
72   xbt_free(rdv);  
73 }
74
75 smx_rdv_t SIMIX_rdv_get_by_name(const char *name)
76 {
77   return xbt_dict_get_or_null(rdv_points, name);
78 }
79
80 int SIMIX_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host)
81 {
82   smx_action_t comm = NULL;
83   xbt_fifo_item_t item = NULL;
84   int count = 0;
85
86   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_action_t) {
87     if (comm->comm.src_proc->smx_host == host)
88       count++;
89   }
90
91   return count;
92 }
93
94 smx_action_t SIMIX_rdv_get_head(smx_rdv_t rdv)
95 {
96   return xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
97 }
98
99 /**
100  *  \brief Push a communication request into a rendez-vous point
101  *  \param rdv The rendez-vous point
102  *  \param comm The communication request
103  */
104 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm)
105 {
106   xbt_fifo_push(rdv->comm_fifo, comm);
107   comm->comm.rdv = rdv;
108 }
109
110 /**
111  *  \brief Remove a communication request from a rendez-vous point
112  *  \param rdv The rendez-vous point
113  *  \param comm The communication request
114  */
115 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm)
116 {
117   xbt_fifo_remove(rdv->comm_fifo, comm);
118   comm->comm.rdv = NULL;
119 }
120
121 /**
122  *  \brief Checks if there is a communication action queued in a rendez-vous matching our needs
123  *  \param type The type of communication we are looking for (comm_send, comm_recv)
124  *  \return The communication action if found, NULL otherwise
125  */
126 smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
127                                    int (*match_fun)(void *, void *), void *data)
128 {
129   smx_action_t action;
130   xbt_fifo_item_t item;
131   void* req_data = NULL;
132
133   xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t){
134     if (action->comm.type == SIMIX_COMM_SEND) {
135       req_data = action->comm.src_data;
136     } else if (action->comm.type == SIMIX_COMM_RECEIVE) {
137       req_data = action->comm.dst_data;
138     }
139     if (action->comm.type == type && (!match_fun || match_fun(data, req_data))) {
140       DEBUG1("Found a matching communication action %p", action);
141       xbt_fifo_remove_item(rdv->comm_fifo, item);
142       xbt_fifo_free_item(item);
143       action->comm.refcount++;
144       action->comm.rdv = NULL;
145       return action;
146     }
147     DEBUG3("Sorry, communication action %p does not match our needs:"
148            " its type is %d but we are looking for a comm of type %d",
149            action, action->comm.type, type);
150   }
151   DEBUG0("No matching communication action found");
152   return NULL;
153 }
154
155 /******************************************************************************/
156 /*                            Comunication Actions                            */
157 /******************************************************************************/
158
159 /**
160  *  \brief Creates a new comunicate action
161  *  \param type The type of request (comm_send, comm_recv)
162  *  \return The new comunicate action
163  */
164 smx_action_t SIMIX_comm_new(e_smx_comm_type_t type)
165 {
166   smx_action_t act;
167
168   /* alloc structures */
169   act = xbt_new0(s_smx_action_t, 1);
170   act->type = SIMIX_ACTION_COMMUNICATE;
171   act->state = SIMIX_WAITING;
172   act->request_list = xbt_fifo_new();
173
174   /* set communication */
175   act->comm.type = type;
176   act->comm.refcount = 1;
177
178 #ifdef HAVE_LATENCY_BOUND_TRACKING
179   //initialize with unknown value
180   act->latency_limited = -1;
181 #endif
182
183 #ifdef HAVE_TRACING
184   act->category = NULL;
185 #endif
186
187   DEBUG1("Create communicate action %p", act);
188
189   return act;
190 }
191
192 /**
193  *  \brief Destroy a communicate action
194  *  \param action The communicate action to be destroyed
195  */
196 void SIMIX_comm_destroy(smx_action_t action)
197 {
198   DEBUG2("Destroy action %p (refcount:%d)", action, action->comm.refcount);
199
200   if (action->comm.refcount <= 0)
201     xbt_die(bprintf("the refcount of comm %p is already 0 before decreasing it. That's a bug!",action));
202
203   action->comm.refcount--;
204   if (action->comm.refcount > 0)
205     return;
206   DEBUG2("Really free communication %p; refcount is now %d", action,
207         action->comm.refcount);
208
209 #ifdef HAVE_LATENCY_BOUND_TRACKING
210     action->latency_limited = SIMIX_comm_is_latency_bounded( action ) ;
211 #endif
212
213 #ifdef HAVE_TRACING
214   TRACE_smx_action_destroy(action);
215 #endif
216
217   if (action->name)
218     xbt_free(action->name);
219
220   xbt_fifo_free(action->request_list);
221
222   SIMIX_comm_destroy_internal_actions(action);
223
224   xbt_free(action);
225 }
226
227 void SIMIX_comm_destroy_internal_actions(smx_action_t action)
228 {
229   if (action->comm.surf_comm){
230 #ifdef HAVE_LATENCY_BOUND_TRACKING
231     action->latency_limited = SIMIX_comm_is_latency_bounded(action);
232 #endif
233     action->comm.surf_comm->model_type->action_unref(action->comm.surf_comm);
234     action->comm.surf_comm = NULL;
235   }
236
237   if (action->comm.src_timeout){
238     action->comm.src_timeout->model_type->action_unref(action->comm.src_timeout);
239     action->comm.src_timeout = NULL;
240   }
241
242   if (action->comm.dst_timeout){
243     action->comm.dst_timeout->model_type->action_unref(action->comm.dst_timeout);
244     action->comm.dst_timeout = NULL;
245   }
246 }
247
248 smx_action_t SIMIX_comm_isend(smx_process_t src_proc, smx_rdv_t rdv,
249                               double task_size, double rate,
250                               void *src_buff, size_t src_buff_size,
251                               int (*match_fun)(void *, void *), void *data)
252 {
253   smx_action_t action;
254
255   /* Look for communication request matching our needs.
256      If it is not found then create it and push it into the rendez-vous point */
257   action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_RECEIVE, match_fun, data);
258
259   if (!action) {
260     action = SIMIX_comm_new(SIMIX_COMM_SEND);
261     SIMIX_rdv_push(rdv, action);
262   } else {
263     action->state = SIMIX_READY;
264     action->comm.type = SIMIX_COMM_READY;
265   }
266
267   /* Setup the communication request */
268   action->comm.src_proc = src_proc;
269   action->comm.task_size = task_size;
270   action->comm.rate = rate;
271   action->comm.src_buff = src_buff;
272   action->comm.src_buff_size = src_buff_size;
273   action->comm.src_data = data;
274
275   if (MC_IS_ENABLED) {
276     action->state = SIMIX_RUNNING;
277     return action;
278   }
279
280   SIMIX_comm_start(action);
281   return action;
282 }
283
284 smx_action_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_rdv_t rdv,
285                       void *dst_buff, size_t *dst_buff_size,
286                       int (*match_fun)(void *, void *), void *data)
287 {
288   smx_action_t action;
289
290   /* Look for communication request matching our needs.
291    * If it is not found then create it and push it into the rendez-vous point
292    */
293   action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
294
295   if (!action) {
296     action = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
297     SIMIX_rdv_push(rdv, action);
298   } else {
299     action->state = SIMIX_READY;
300     action->comm.type = SIMIX_COMM_READY;
301   }
302
303   /* Setup communication request */
304   action->comm.dst_proc = dst_proc;
305   action->comm.dst_buff = dst_buff;
306   action->comm.dst_buff_size = dst_buff_size;
307   action->comm.dst_data = data;
308
309   if (MC_IS_ENABLED) {
310     action->state = SIMIX_RUNNING;
311     return action;
312   }
313
314   SIMIX_comm_start(action);
315   return action;
316 }
317
318 void SIMIX_pre_comm_wait(smx_req_t req, int idx)
319 {
320   smx_action_t action = req->comm_wait.comm;
321   double timeout = req->comm_wait.timeout;
322   surf_action_t sleep;
323
324   /* Associate this request to the action */
325   xbt_fifo_push(action->request_list, req);
326   req->issuer->waiting_action = action;
327
328   if (MC_IS_ENABLED){
329     if(idx == 0){
330       action->state = SIMIX_DONE;
331     }else{
332       /* If we reached this point, the wait request must have a timeout */
333       /* Otherwise it shouldn't be enabled and executed by the MC */
334       if(timeout == -1)
335         THROW_IMPOSSIBLE;
336
337       if(action->comm.src_proc == req->issuer)
338         action->state = SIMIX_SRC_TIMEOUT;
339       else
340         action->state = SIMIX_DST_TIMEOUT;
341     }
342
343     SIMIX_comm_finish(action);
344     return;
345   }
346
347   /* If the action has already finish perform the error handling, */
348   /* otherwise set up a waiting timeout on the right side         */
349   if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
350     SIMIX_comm_finish(action);
351   } else { /* if (timeout >= 0) { we need a surf sleep action even when there is no timeout, otherwise surf won't tell us when the host fails */
352     sleep = surf_workstation_model->extension.workstation.sleep(req->issuer->smx_host->host, timeout);
353     surf_workstation_model->action_data_set(sleep, action);
354
355     if (req->issuer == action->comm.src_proc)
356       action->comm.src_timeout = sleep;
357     else
358       action->comm.dst_timeout = sleep;
359   }
360 }
361
362 void SIMIX_pre_comm_test(smx_req_t req)
363 {
364   smx_action_t action = req->comm_test.comm;
365
366   if(MC_IS_ENABLED){
367     req->comm_test.result = action->comm.src_proc && action->comm.dst_proc;
368     if(req->comm_test.result){
369       action->state = SIMIX_DONE;
370       xbt_fifo_push(action->request_list, req);
371       SIMIX_comm_finish(action);
372     }else{
373       SIMIX_request_answer(req);
374     }
375     return;
376   }
377
378   req->comm_test.result = (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING);
379   if (req->comm_test.result) {
380     xbt_fifo_push(action->request_list, req);
381     SIMIX_comm_finish(action);
382   } else {
383     SIMIX_request_answer(req);
384   }
385 }
386
387 void SIMIX_pre_comm_testany(smx_req_t req, int idx)
388 {
389   unsigned int cursor;
390   smx_action_t action;
391   xbt_dynar_t actions = req->comm_testany.comms;
392   req->comm_testany.result = -1;
393
394   if (MC_IS_ENABLED){
395     if(idx == -1){
396       SIMIX_request_answer(req);
397     }else{
398       action = xbt_dynar_get_as(actions, idx, smx_action_t);
399       xbt_fifo_push(action->request_list, req);
400       action->state = SIMIX_DONE;
401       SIMIX_comm_finish(action);
402     }
403     return;
404   }
405
406   xbt_dynar_foreach(req->comm_testany.comms,cursor,action) {
407     if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
408       req->comm_testany.result = cursor;
409       xbt_fifo_push(action->request_list, req);
410       SIMIX_comm_finish(action);
411       return;
412     }
413   }
414   SIMIX_request_answer(req);
415 }
416
417 void SIMIX_pre_comm_waitany(smx_req_t req, int idx)
418 {
419   smx_action_t action;
420   unsigned int cursor = 0;
421   xbt_dynar_t actions = req->comm_waitany.comms;
422
423   if (MC_IS_ENABLED){
424     action = xbt_dynar_get_as(actions, idx, smx_action_t);
425     xbt_fifo_push(action->request_list, req);
426     req->comm_waitany.result = idx;
427     action->state = SIMIX_DONE;
428     SIMIX_comm_finish(action);
429     return;
430   }
431
432   xbt_dynar_foreach(actions, cursor, action){
433     /* Associate this request to the action */
434     xbt_fifo_push(action->request_list, req);
435     if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING){
436       SIMIX_comm_finish(action);
437       break;
438     }
439   }
440 }
441
442 void SIMIX_waitany_req_remove_from_actions(smx_req_t req)
443 {
444   smx_action_t action;
445   unsigned int cursor = 0;
446   xbt_dynar_t actions = req->comm_waitany.comms;
447
448   xbt_dynar_foreach(actions, cursor, action){
449     xbt_fifo_remove(action->request_list, req);
450   }
451 }
452
453 /**
454  *  \brief Start the simulation of a communication request
455  *  \param action The communication action
456  */
457 static XBT_INLINE void SIMIX_comm_start(smx_action_t action)
458 {
459   /* If both the sender and the receiver are already there, start the communication */
460   if (action->state == SIMIX_READY) {
461     smx_host_t sender = action->comm.src_proc->smx_host;
462     smx_host_t receiver = action->comm.dst_proc->smx_host;
463
464     DEBUG3("Starting communication %p from '%s' to '%s'", action,
465            SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
466
467     action->comm.surf_comm = surf_workstation_model->extension.workstation.
468         communicate(sender->host, receiver->host, action->comm.task_size, action->comm.rate);
469
470     surf_workstation_model->action_data_set(action->comm.surf_comm, action);
471
472     action->state = SIMIX_RUNNING;
473
474 #ifdef HAVE_TRACING
475     TRACE_smx_action_communicate(action, action->comm.src_proc);
476 #endif
477
478     /* If a link is failed, detect it immediately */
479     if (surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED) {
480       DEBUG2("Communication from '%s' to '%s' failed to start because of a link failure",
481           SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
482       action->state = SIMIX_LINK_FAILURE;
483       SIMIX_comm_destroy_internal_actions(action);
484     }
485
486     /* If any of the process is suspend, create the action but stop its execution,
487        it will be restarted when the sender process resume */
488     if (SIMIX_process_is_suspended(action->comm.src_proc) ||
489         SIMIX_process_is_suspended(action->comm.dst_proc)) {
490       /* FIXME: check what should happen with the action state */
491       surf_workstation_model->suspend(action->comm.surf_comm);
492     }
493   }
494 }
495
496 void SIMIX_comm_finish(smx_action_t action)
497 {
498   smx_req_t req;
499
500   while ((req = xbt_fifo_shift(action->request_list))) {
501
502     /* If a waitany request is waiting for this action to finish, then remove
503        it from the other actions in the waitany list. Afterwards, get the
504        position of the actual action in the waitany request's actions dynar and
505        return it as the result of the call */
506     if (req->call == REQ_COMM_WAITANY) {
507       SIMIX_waitany_req_remove_from_actions(req);
508       if(!MC_IS_ENABLED)
509         req->comm_waitany.result = xbt_dynar_search(req->comm_waitany.comms, &action);
510     }
511
512     /* If the action is still in a rendez-vous point then remove from it */
513     if (action->comm.rdv)
514       SIMIX_rdv_remove(action->comm.rdv, action);
515
516     DEBUG1("SIMIX_comm_finish: action state = %d", action->state);
517
518     /* Check out for errors */
519     switch (action->state) {
520
521       case SIMIX_DONE:
522         DEBUG1("Communication %p complete!", action);
523         SIMIX_comm_copy_data(action);
524         break;
525
526       case SIMIX_SRC_TIMEOUT:
527         TRY {
528           THROW0(timeout_error, 0, "Communication timeouted because of sender");
529         }
530         CATCH(req->issuer->running_ctx->exception) {
531           req->issuer->doexception = 1;
532         }
533         break;
534
535       case SIMIX_DST_TIMEOUT:
536         TRY {
537           THROW0(timeout_error, 0, "Communication timeouted because of receiver");
538         }
539         CATCH(req->issuer->running_ctx->exception) {
540           req->issuer->doexception = 1;
541         }
542         break;
543
544       case SIMIX_SRC_HOST_FAILURE:
545         TRY {
546           if (req->issuer == action->comm.src_proc)
547             THROW0(host_error, 0, "Host failed");
548           else
549             THROW0(network_error, 0, "Remote peer failed");
550         }
551         CATCH(req->issuer->running_ctx->exception) {
552           req->issuer->doexception = 1;
553         }
554         break;
555
556       case SIMIX_DST_HOST_FAILURE:
557         TRY {
558           if (req->issuer == action->comm.dst_proc)
559             THROW0(host_error, 0, "Host failed");
560           else
561             THROW0(network_error, 0, "Remote peer failed");
562         }
563         CATCH(req->issuer->running_ctx->exception) {
564           req->issuer->doexception = 1;
565         }
566         break;
567
568       case SIMIX_LINK_FAILURE:
569         TRY {
570           DEBUG5("Link failure in action %p between '%s' and '%s': posting an exception to the issuer: %s (%p)",
571               action, action->comm.src_proc->smx_host->name, action->comm.dst_proc->smx_host->name,
572               req->issuer->name, req->issuer);
573           THROW0(network_error, 0, "Link failure");
574         }
575         CATCH(req->issuer->running_ctx->exception) {
576           req->issuer->doexception = 1;
577         }
578         break;
579
580       default:
581         THROW_IMPOSSIBLE;
582     }
583
584     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
585     if (req->issuer->doexception) {
586       if (req->call == REQ_COMM_WAITANY) {
587         req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_waitany.comms, &action);
588       }
589       else if (req->call == REQ_COMM_TESTANY) {
590         req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_testany.comms, &action);
591       }
592     }
593
594     req->issuer->waiting_action = NULL;
595     SIMIX_request_answer(req);
596   }
597 }
598
599 void SIMIX_post_comm(smx_action_t action)
600 {
601   /* Update action state */
602   if (action->comm.src_timeout &&
603      surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_DONE)
604      action->state = SIMIX_SRC_TIMEOUT;
605   else if (action->comm.dst_timeout &&
606           surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_DONE)
607      action->state = SIMIX_DST_TIMEOUT;
608   else if (action->comm.src_timeout &&
609           surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_FAILED)
610      action->state = SIMIX_SRC_HOST_FAILURE;
611   else if (action->comm.dst_timeout &&
612           surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_FAILED)
613      action->state = SIMIX_DST_HOST_FAILURE;
614   else if (action->comm.surf_comm &&
615           surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED)
616      action->state = SIMIX_LINK_FAILURE;
617   else
618     action->state = SIMIX_DONE;
619
620   DEBUG1("SIMIX_post_comm: action state = %d", action->state);
621
622   /* After this point the surf actions associated with the simix communicate
623      action are no longer needed, thus we delete them. */
624   SIMIX_comm_destroy_internal_actions(action);
625
626   /* If there are requests associated with the action, then answer them */
627   if (xbt_fifo_size(action->request_list))
628     SIMIX_comm_finish(action);
629 }
630
631 void SIMIX_comm_cancel(smx_action_t action)
632 {
633   /* If the action is a waiting state means that it is still in a rdv */
634   /* so remove from it and delete it */
635   if (action->state == SIMIX_WAITING) {
636     SIMIX_rdv_remove(action->comm.rdv, action);
637     action->state = SIMIX_FAILED;
638   } else {
639     /* When running the MC there are no surf actions */
640     if(!MC_IS_ENABLED)
641       surf_workstation_model->action_cancel(action->comm.surf_comm);
642   }
643 }
644
645 void SIMIX_comm_suspend(smx_action_t action)
646 {
647   /*FIXME: shall we suspend also the timeout actions? */
648   surf_workstation_model->suspend(action->comm.surf_comm);
649 }
650
651 void SIMIX_comm_resume(smx_action_t action)
652 {
653   /*FIXME: check what happen with the timeouts */
654   surf_workstation_model->resume(action->comm.surf_comm);
655 }
656
657
658 /************* Action Getters **************/
659
660 /**
661  *  \brief get the amount remaining from the communication
662  *  \param action The communication
663  */
664 double SIMIX_comm_get_remains(smx_action_t action)
665 {
666   double remains;
667
668   switch (action->state) {
669
670     case SIMIX_RUNNING:
671       remains = surf_workstation_model->get_remains(action->comm.surf_comm);
672       break;
673
674     case SIMIX_WAITING:
675     case SIMIX_READY:
676       remains = 0; /*FIXME: check what should be returned */
677       break;
678
679     default:
680       remains = 0; /*FIXME: is this correct? */
681       break;
682   }
683   return remains;
684 }
685
686 e_smx_state_t SIMIX_comm_get_state(smx_action_t action)
687 {
688   return action->state;
689 }
690
691 /**
692  *  \brief Return the user data associated to the sender of the communication
693  *  \param action The communication
694  *  \return the user data
695  */
696 void* SIMIX_comm_get_src_data(smx_action_t action)
697 {
698   return action->comm.src_data;
699 }
700
701 /**
702  *  \brief Return the user data associated to the receiver of the communication
703  *  \param action The communication
704  *  \return the user data
705  */
706 void* SIMIX_comm_get_dst_data(smx_action_t action)
707 {
708   return action->comm.dst_data;
709 }
710
711 void* SIMIX_comm_get_src_buff(smx_action_t action)
712 {
713   return action->comm.src_buff;
714 }
715
716 void* SIMIX_comm_get_dst_buff(smx_action_t action)
717 {
718   return action->comm.dst_buff;
719 }
720
721 size_t SIMIX_comm_get_src_buff_size(smx_action_t action)
722 {
723   return action->comm.src_buff_size;
724 }
725
726 size_t SIMIX_comm_get_dst_buff_size(smx_action_t action)
727 {
728   size_t buff_size;
729
730   if (action->comm.dst_buff_size)
731     buff_size = *(action->comm.dst_buff_size);
732   else
733     buff_size = 0;
734
735   return buff_size;
736 }
737
738 smx_process_t SIMIX_comm_get_src_proc(smx_action_t action)
739 {
740   return action->comm.src_proc;
741 }
742
743 smx_process_t SIMIX_comm_get_dst_proc(smx_action_t action)
744 {
745   return action->comm.dst_proc;
746 }
747
748 #ifdef HAVE_LATENCY_BOUND_TRACKING
749 /**
750  *  \brief verify if communication is latency bounded
751  *  \param comm The communication
752  */
753 XBT_INLINE int SIMIX_comm_is_latency_bounded(smx_action_t action)
754 {
755   if (action->comm.surf_comm){
756       DEBUG1("Getting latency limited for surf_action (%p)", action->comm.surf_comm);
757       action->latency_limited = surf_workstation_model->get_latency_limited(action->comm.surf_comm);
758       DEBUG1("Action limited is %d", action->latency_limited);
759   }
760   return action->latency_limited;
761 }
762 #endif
763
764 /******************************************************************************/
765 /*                    SIMIX_comm_copy_data callbacks                       */
766 /******************************************************************************/
767 static void (*SIMIX_comm_copy_data_callback) (smx_action_t, size_t) =
768     &SIMIX_comm_copy_pointer_callback;
769
770 void
771 SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, size_t))
772 {
773   SIMIX_comm_copy_data_callback = callback;
774 }
775
776 void SIMIX_comm_copy_pointer_callback(smx_action_t comm, size_t buff_size)
777 {
778   xbt_assert1((buff_size == sizeof(void *)),
779               "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
780   *(void **) (comm->comm.dst_buff) = comm->comm.src_buff;
781 }
782
783 void SIMIX_comm_copy_buffer_callback(smx_action_t comm, size_t buff_size)
784 {
785   memcpy(comm->comm.dst_buff, comm->comm.src_buff, buff_size);
786 }
787
788 /**
789  *  \brief Copy the communication data from the sender's buffer to the receiver's one
790  *  \param comm The communication
791  */
792 void SIMIX_comm_copy_data(smx_action_t comm)
793 {
794   size_t buff_size = comm->comm.src_buff_size;
795   /* If there is no data to be copy then return */
796   if (!comm->comm.src_buff || !comm->comm.dst_buff || comm->comm.copied == 1)
797     return;
798
799   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
800          comm,
801          comm->comm.src_proc->smx_host->name, comm->comm.src_buff,
802          comm->comm.dst_proc->smx_host->name, comm->comm.dst_buff, buff_size);
803
804   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
805   if (comm->comm.dst_buff_size)
806     buff_size = MIN(buff_size, *(comm->comm.dst_buff_size));
807
808   /* Update the receiver's buffer size to the copied amount */
809   if (comm->comm.dst_buff_size)
810     *comm->comm.dst_buff_size = buff_size;
811
812   if (buff_size == 0)
813     return;
814
815   (*SIMIX_comm_copy_data_callback) (comm, buff_size);
816
817   /* Set the copied flag so we copy data only once */
818   /* (this function might be called from both communication ends) */
819   comm->comm.copied = 1;
820 }