Logo AND Algorithmique Numérique Distribuée

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