Logo AND Algorithmique Numérique Distribuée

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