Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix possible crashes and leaks with dsends during processes cleanup
[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   xbt_free(rdv->name);
67   xbt_fifo_free(rdv->comm_fifo);
68   xbt_free(rdv);  
69 }
70
71 smx_rdv_t SIMIX_rdv_get_by_name(const char *name)
72 {
73   return xbt_dict_get_or_null(rdv_points, name);
74 }
75
76 int SIMIX_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host)
77 {
78   smx_action_t comm = NULL;
79   xbt_fifo_item_t item = NULL;
80   int count = 0;
81
82   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_action_t) {
83     if (comm->comm.src_proc->smx_host == host)
84       count++;
85   }
86
87   return count;
88 }
89
90 smx_action_t SIMIX_rdv_get_head(smx_rdv_t rdv)
91 {
92   return xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
93 }
94
95 /**
96  *  \brief Push a communication request into a rendez-vous point
97  *  \param rdv The rendez-vous point
98  *  \param comm The communication request
99  */
100 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm)
101 {
102   xbt_fifo_push(rdv->comm_fifo, comm);
103   comm->comm.rdv = rdv;
104 }
105
106 /**
107  *  \brief Remove a communication request from a rendez-vous point
108  *  \param rdv The rendez-vous point
109  *  \param comm The communication request
110  */
111 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm)
112 {
113   xbt_fifo_remove(rdv->comm_fifo, comm);
114   comm->comm.rdv = NULL;
115 }
116
117 /**
118  *  \brief Wrapper to SIMIX_rdv_get_request
119  */
120 smx_action_t SIMIX_comm_get_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data) {
121    return SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
122 }
123
124 /**
125  *  \brief Checks if there is a communication action queued in a rendez-vous matching our needs
126  *  \param type The type of communication we are looking for (comm_send, comm_recv)
127  *  \return The communication action if found, NULL otherwise
128  */
129 smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
130                                    int (*match_fun)(void *, void *), void *data)
131 {
132   // FIXME rewrite this function by using SIMIX_rdv_has_send/recv_match
133   smx_action_t action;
134   xbt_fifo_item_t item;
135   void* req_data = NULL;
136
137   xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t) {
138     if (action->comm.type == SIMIX_COMM_SEND) {
139       req_data = action->comm.src_data;
140     } else if (action->comm.type == SIMIX_COMM_RECEIVE) {
141       req_data = action->comm.dst_data;
142     }
143     if (action->comm.type == type && (!match_fun || match_fun(data, req_data))) {
144       XBT_DEBUG("Found a matching communication action %p", action);
145       xbt_fifo_remove_item(rdv->comm_fifo, item);
146       xbt_fifo_free_item(item);
147       action->comm.refcount++;
148       action->comm.rdv = NULL;
149       return action;
150     }
151     XBT_DEBUG("Sorry, communication action %p does not match our needs:"
152            " its type is %d but we are looking for a comm of type %d",
153            action, action->comm.type, type);
154   }
155   XBT_DEBUG("No matching communication action found");
156   return NULL;
157 }
158
159 /**
160  *  \brief Checks if there is a send communication action
161  *  queued in a rendez-vous matching our needs.
162  *  \return 1 if found, 0 otherwise
163  */
164 int SIMIX_comm_has_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data) {
165
166   smx_action_t action;
167   xbt_fifo_item_t item;
168
169   xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t){
170     if (action->comm.type == SIMIX_COMM_SEND
171         && (!match_fun || match_fun(data, action->comm.src_data))) {
172       XBT_DEBUG("Found a matching communication action %p", action);
173       return 1;
174     }
175   }
176   XBT_DEBUG("No matching communication action found");
177   return 0;
178 }
179
180 /**
181  *  \brief Checks if there is a recv communication action
182  *  queued in a rendez-vous matching our needs.
183  *  \return 1 if found, 0 otherwise
184  */
185 int SIMIX_comm_has_recv_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data) {
186
187   smx_action_t action;
188   xbt_fifo_item_t item;
189
190   xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t) {
191     if (action->comm.type == SIMIX_COMM_RECEIVE
192         && (!match_fun || match_fun(data, action->comm.dst_data))) {
193       XBT_DEBUG("Found a matching communication action %p", action);
194       return 1;
195     }
196   }
197   XBT_DEBUG("No matching communication action found");
198   return 0;
199 }
200
201 /******************************************************************************/
202 /*                            Comunication Actions                            */
203 /******************************************************************************/
204
205 /**
206  *  \brief Creates a new comunicate action
207  *  \param type The type of request (comm_send, comm_recv)
208  *  \return The new comunicate action
209  */
210 smx_action_t SIMIX_comm_new(e_smx_comm_type_t type)
211 {
212   smx_action_t act;
213
214   /* alloc structures */
215   act = xbt_mallocator_get(simix_global->action_mallocator);
216
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), state: %d",
246       action, action->comm.refcount, action->state);
247
248   xbt_assert(action->comm.refcount > 0,
249       "The refcount of comm %p is already 0 before decreasing it. "
250       "That's a bug!", action);
251
252   action->comm.refcount--;
253   if (action->comm.refcount > 0)
254     return;
255   XBT_DEBUG("Really free communication %p; refcount is now %d", action,
256         action->comm.refcount);
257
258 #ifdef HAVE_LATENCY_BOUND_TRACKING
259     action->latency_limited = SIMIX_comm_is_latency_bounded( action ) ;
260 #endif
261
262   xbt_free(action->name);
263   SIMIX_comm_destroy_internal_actions(action);
264
265   if (action->comm.detached && action->state != SIMIX_DONE) {
266     /* the communication has failed and was detached:
267      * we have to free the buffer */
268     ((void_f_pvoid_t) action->comm.src_data)(action->comm.src_buff);
269   }
270
271   xbt_mallocator_release(simix_global->action_mallocator, action);
272 }
273
274 void SIMIX_comm_destroy_internal_actions(smx_action_t action)
275 {
276   if (action->comm.surf_comm){
277 #ifdef HAVE_LATENCY_BOUND_TRACKING
278     action->latency_limited = SIMIX_comm_is_latency_bounded(action);
279 #endif
280     action->comm.surf_comm->model_type->action_unref(action->comm.surf_comm);
281     action->comm.surf_comm = NULL;
282   }
283
284   if (action->comm.src_timeout){
285     action->comm.src_timeout->model_type->action_unref(action->comm.src_timeout);
286     action->comm.src_timeout = NULL;
287   }
288
289   if (action->comm.dst_timeout){
290     action->comm.dst_timeout->model_type->action_unref(action->comm.dst_timeout);
291     action->comm.dst_timeout = NULL;
292   }
293 }
294
295 smx_action_t SIMIX_comm_isend(smx_process_t src_proc, smx_rdv_t rdv,
296                               double task_size, double rate,
297                               void *src_buff, size_t src_buff_size,
298                               int (*match_fun)(void *, void *), void *data,
299                               int detached)
300 {
301   smx_action_t action;
302
303   /* Look for communication request matching our needs.
304      If it is not found then create it and push it into the rendez-vous point */
305   action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_RECEIVE, match_fun, data);
306
307   if (!action) {
308     action = SIMIX_comm_new(SIMIX_COMM_SEND);
309     SIMIX_rdv_push(rdv, action);
310   } else {
311     action->state = SIMIX_READY;
312     action->comm.type = SIMIX_COMM_READY;
313   }
314   xbt_fifo_push(src_proc->comms, action);
315
316   /* if the communication action is detached then decrease the refcount
317    * by one, so it will be eliminated by the receiver's destroy call */
318   if (detached) {
319     action->comm.detached = 1;
320     action->comm.refcount--;
321   }
322
323   /* Setup the communication request */
324   action->comm.src_proc = src_proc;
325   action->comm.task_size = task_size;
326   action->comm.rate = rate;
327   action->comm.src_buff = src_buff;
328   action->comm.src_buff_size = src_buff_size;
329   action->comm.src_data = data;
330
331   if (MC_IS_ENABLED) {
332     action->state = SIMIX_RUNNING;
333     return action;
334   }
335
336   SIMIX_comm_start(action);
337   return action;
338 }
339
340 smx_action_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_rdv_t rdv,
341                       void *dst_buff, size_t *dst_buff_size,
342                       int (*match_fun)(void *, void *), void *data)
343 {
344   smx_action_t action;
345
346   /* Look for communication request matching our needs.
347    * If it is not found then create it and push it into the rendez-vous point
348    */
349   action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
350
351   if (!action) {
352     action = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
353     SIMIX_rdv_push(rdv, action);
354   } else {
355     action->state = SIMIX_READY;
356     action->comm.type = SIMIX_COMM_READY;
357   }
358   xbt_fifo_push(dst_proc->comms, action);
359
360   /* Setup communication request */
361   action->comm.dst_proc = dst_proc;
362   action->comm.dst_buff = dst_buff;
363   action->comm.dst_buff_size = dst_buff_size;
364   action->comm.dst_data = data;
365
366   if (MC_IS_ENABLED) {
367     action->state = SIMIX_RUNNING;
368     return action;
369   }
370
371   SIMIX_comm_start(action);
372   return action;
373 }
374
375 void SIMIX_pre_comm_wait(smx_req_t req, smx_action_t action, double timeout, int idx)
376 {
377   /* the request may be a wait, a send or a recv */
378   surf_action_t sleep;
379
380   /* Associate this request to the action */
381   xbt_fifo_push(action->request_list, req);
382   req->issuer->waiting_action = action;
383
384   if (MC_IS_ENABLED) {
385     if (idx == 0) {
386       action->state = SIMIX_DONE;
387     } else {
388       /* If we reached this point, the wait request must have a timeout */
389       /* Otherwise it shouldn't be enabled and executed by the MC */
390       if (timeout == -1)
391         THROW_IMPOSSIBLE;
392
393       if (action->comm.src_proc == req->issuer)
394         action->state = SIMIX_SRC_TIMEOUT;
395       else
396         action->state = SIMIX_DST_TIMEOUT;
397     }
398
399     SIMIX_comm_finish(action);
400     return;
401   }
402
403   /* If the action has already finish perform the error handling, */
404   /* otherwise set up a waiting timeout on the right side         */
405   if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
406     SIMIX_comm_finish(action);
407   } 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 */
408     sleep = surf_workstation_model->extension.workstation.sleep(req->issuer->smx_host->host, timeout);
409     surf_workstation_model->action_data_set(sleep, action);
410
411     if (req->issuer == action->comm.src_proc)
412       action->comm.src_timeout = sleep;
413     else
414       action->comm.dst_timeout = sleep;
415   }
416 }
417
418 void SIMIX_pre_comm_test(smx_req_t req)
419 {
420   smx_action_t action = req->comm_test.comm;
421
422   if(MC_IS_ENABLED){
423     req->comm_test.result = action->comm.src_proc && action->comm.dst_proc;
424     if(req->comm_test.result){
425       action->state = SIMIX_DONE;
426       xbt_fifo_push(action->request_list, req);
427       SIMIX_comm_finish(action);
428     }else{
429       SIMIX_request_answer(req);
430     }
431     return;
432   }
433
434   req->comm_test.result = (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING);
435   if (req->comm_test.result) {
436     xbt_fifo_push(action->request_list, req);
437     SIMIX_comm_finish(action);
438   } else {
439     SIMIX_request_answer(req);
440   }
441 }
442
443 void SIMIX_pre_comm_testany(smx_req_t req, int idx)
444 {
445   unsigned int cursor;
446   smx_action_t action;
447   xbt_dynar_t actions = req->comm_testany.comms;
448   req->comm_testany.result = -1;
449
450   if (MC_IS_ENABLED){
451     if(idx == -1){
452       SIMIX_request_answer(req);
453     }else{
454       action = xbt_dynar_get_as(actions, idx, smx_action_t);
455       req->comm_testany.result = idx;
456       xbt_fifo_push(action->request_list, req);
457       action->state = SIMIX_DONE;
458       SIMIX_comm_finish(action);
459     }
460     return;
461   }
462
463   xbt_dynar_foreach(req->comm_testany.comms,cursor,action) {
464     if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
465       req->comm_testany.result = cursor;
466       xbt_fifo_push(action->request_list, req);
467       SIMIX_comm_finish(action);
468       return;
469     }
470   }
471   SIMIX_request_answer(req);
472 }
473
474 void SIMIX_pre_comm_waitany(smx_req_t req, int idx)
475 {
476   smx_action_t action;
477   unsigned int cursor = 0;
478   xbt_dynar_t actions = req->comm_waitany.comms;
479
480   if (MC_IS_ENABLED){
481     action = xbt_dynar_get_as(actions, idx, smx_action_t);
482     xbt_fifo_push(action->request_list, req);
483     req->comm_waitany.result = idx;
484     action->state = SIMIX_DONE;
485     SIMIX_comm_finish(action);
486     return;
487   }
488
489   xbt_dynar_foreach(actions, cursor, action){
490     /* Associate this request to the action */
491     xbt_fifo_push(action->request_list, req);
492     if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING){
493       SIMIX_comm_finish(action);
494       break;
495     }
496   }
497 }
498
499 void SIMIX_waitany_req_remove_from_actions(smx_req_t req)
500 {
501   smx_action_t action;
502   unsigned int cursor = 0;
503   xbt_dynar_t actions = req->comm_waitany.comms;
504
505   xbt_dynar_foreach(actions, cursor, action){
506     xbt_fifo_remove(action->request_list, req);
507   }
508 }
509
510 /**
511  *  \brief Start the simulation of a communication request
512  *  \param action The communication action
513  */
514
515 XBT_INLINE void SIMIX_comm_start(smx_action_t action)
516 {
517   /* If both the sender and the receiver are already there, start the communication */
518   if (action->state == SIMIX_READY) {
519
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     /* If a link is failed, detect it immediately */
534     if (surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED) {
535       XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure",
536           SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
537       action->state = SIMIX_LINK_FAILURE;
538       SIMIX_comm_destroy_internal_actions(action);
539     }
540
541     /* If any of the process is suspend, create the action but stop its execution,
542        it will be restarted when the sender process resume */
543     if (SIMIX_process_is_suspended(action->comm.src_proc) ||
544         SIMIX_process_is_suspended(action->comm.dst_proc)) {
545       /* FIXME: check what should happen with the action state */
546       surf_workstation_model->suspend(action->comm.surf_comm);
547     }
548   }
549 }
550
551 /**
552  * \brief Answers the SIMIX requests associated to a communication action.
553  * \param action a finished communication action
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           THROWF(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           THROWF(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             THROWF(host_error, 0, "Host failed");
608           else
609             THROWF(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             THROWF(host_error, 0, "Host failed");
620           else
621             THROWF(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,
632               action->comm.src_proc ? action->comm.src_proc->smx_host->name : NULL,
633               action->comm.dst_proc ? action->comm.dst_proc->smx_host->name : NULL,
634               req->issuer->name, req->issuer);
635           THROWF(network_error, 0, "Link failure");
636         }
637         CATCH(req->issuer->running_ctx->exception) {
638           req->issuer->doexception = 1;
639         }
640         break;
641
642       case SIMIX_CANCELED:
643         TRY {
644           if (req->issuer == action->comm.dst_proc) {
645             THROWF(cancel_error, 0, "Communication canceled by the sender");
646           }
647           else {
648             THROWF(cancel_error, 0, "Communication canceled by the receiver");
649           }
650         }
651         CATCH(req->issuer->running_ctx->exception) {
652           req->issuer->doexception = 1;
653         }
654         break;
655
656       default:
657         xbt_die("Unexpected action state in SIMIX_comm_finish: %d", action->state);
658     }
659
660     /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
661     if (req->issuer->doexception) {
662       if (req->call == REQ_COMM_WAITANY) {
663         req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_waitany.comms, &action);
664       }
665       else if (req->call == REQ_COMM_TESTANY) {
666         req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_testany.comms, &action);
667       }
668     }
669
670     req->issuer->waiting_action = NULL;
671     SIMIX_request_answer(req);
672     destroy_count++;
673   }
674
675   while (destroy_count-- > 0)
676     SIMIX_comm_destroy(action);
677 }
678
679 /**
680  * \brief This function is called when a Surf communication action is finished.
681  * \param action the corresponding Simix communication
682  */
683 void SIMIX_post_comm(smx_action_t action)
684 {
685   /* Update action state */
686   if (action->comm.src_timeout &&
687      surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_DONE)
688      action->state = SIMIX_SRC_TIMEOUT;
689   else if (action->comm.dst_timeout &&
690           surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_DONE)
691      action->state = SIMIX_DST_TIMEOUT;
692   else if (action->comm.src_timeout &&
693           surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_FAILED)
694      action->state = SIMIX_SRC_HOST_FAILURE;
695   else if (action->comm.dst_timeout &&
696           surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_FAILED)
697      action->state = SIMIX_DST_HOST_FAILURE;
698   else if (action->comm.surf_comm &&
699           surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED)
700      action->state = SIMIX_LINK_FAILURE;
701   else
702     action->state = SIMIX_DONE;
703
704   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
705       action, action->state, action->comm.src_proc, action->comm.dst_proc, action->comm.detached);
706
707   /* destroy the surf actions associated with the Simix communication */
708   SIMIX_comm_destroy_internal_actions(action);
709
710   /* remove the communication action from the list of pending communications
711    * of both processes (if they still exist) */
712   if (action->comm.src_proc) {
713     xbt_fifo_remove(action->comm.src_proc->comms, action);
714   }
715   if (action->comm.dst_proc) {
716     xbt_fifo_remove(action->comm.dst_proc->comms, action);
717   }
718
719   /* if there are requests associated with the action, then answer them */
720   if (xbt_fifo_size(action->request_list)) {
721     SIMIX_comm_finish(action);
722   }
723 }
724
725 void SIMIX_comm_cancel(smx_action_t action)
726 {
727   /* if the action is a waiting state means that it is still in a rdv */
728   /* so remove from it and delete it */
729   if (action->state == SIMIX_WAITING) {
730     SIMIX_rdv_remove(action->comm.rdv, action);
731     action->state = SIMIX_CANCELED;
732   }
733   else if (!MC_IS_ENABLED /* when running the MC there are no surf actions */
734       && (action->state == SIMIX_READY || action->state == SIMIX_RUNNING)) {
735
736     surf_workstation_model->action_cancel(action->comm.surf_comm);
737   }
738 }
739
740 void SIMIX_comm_suspend(smx_action_t action)
741 {
742   /*FIXME: shall we suspend also the timeout actions? */
743   surf_workstation_model->suspend(action->comm.surf_comm);
744 }
745
746 void SIMIX_comm_resume(smx_action_t action)
747 {
748   /*FIXME: check what happen with the timeouts */
749   surf_workstation_model->resume(action->comm.surf_comm);
750 }
751
752
753 /************* Action Getters **************/
754
755 /**
756  *  \brief get the amount remaining from the communication
757  *  \param action The communication
758  */
759 double SIMIX_comm_get_remains(smx_action_t action)
760 {
761   double remains;
762
763   if(!action){
764       return 0;
765   }
766
767   switch (action->state) {
768
769     case SIMIX_RUNNING:
770       remains = surf_workstation_model->get_remains(action->comm.surf_comm);
771       break;
772
773     case SIMIX_WAITING:
774     case SIMIX_READY:
775       remains = 0; /*FIXME: check what should be returned */
776       break;
777
778     default:
779       remains = 0; /*FIXME: is this correct? */
780       break;
781   }
782   return remains;
783 }
784
785 e_smx_state_t SIMIX_comm_get_state(smx_action_t action)
786 {
787   return action->state;
788 }
789
790 /**
791  *  \brief Return the user data associated to the sender of the communication
792  *  \param action The communication
793  *  \return the user data
794  */
795 void* SIMIX_comm_get_src_data(smx_action_t action)
796 {
797   return action->comm.src_data;
798 }
799
800 /**
801  *  \brief Return the user data associated to the receiver of the communication
802  *  \param action The communication
803  *  \return the user data
804  */
805 void* SIMIX_comm_get_dst_data(smx_action_t action)
806 {
807   return action->comm.dst_data;
808 }
809
810 smx_process_t SIMIX_comm_get_src_proc(smx_action_t action)
811 {
812   return action->comm.src_proc;
813 }
814
815 smx_process_t SIMIX_comm_get_dst_proc(smx_action_t action)
816 {
817   return action->comm.dst_proc;
818 }
819
820 #ifdef HAVE_LATENCY_BOUND_TRACKING
821 /**
822  *  \brief verify if communication is latency bounded
823  *  \param comm The communication
824  */
825 XBT_INLINE int SIMIX_comm_is_latency_bounded(smx_action_t action)
826 {
827   if(!action){
828       return 0;
829   }
830   if (action->comm.surf_comm){
831       XBT_DEBUG("Getting latency limited for surf_action (%p)", action->comm.surf_comm);
832       action->latency_limited = surf_workstation_model->get_latency_limited(action->comm.surf_comm);
833       XBT_DEBUG("Action limited is %d", action->latency_limited);
834   }
835   return action->latency_limited;
836 }
837 #endif
838
839 /******************************************************************************/
840 /*                    SIMIX_comm_copy_data callbacks                       */
841 /******************************************************************************/
842 static void (*SIMIX_comm_copy_data_callback) (smx_action_t, size_t) =
843     &SIMIX_comm_copy_pointer_callback;
844
845 void
846 SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, size_t))
847 {
848   SIMIX_comm_copy_data_callback = callback;
849 }
850
851 void SIMIX_comm_copy_pointer_callback(smx_action_t comm, size_t buff_size)
852 {
853   xbt_assert((buff_size == sizeof(void *)),
854               "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
855   *(void **) (comm->comm.dst_buff) = comm->comm.src_buff;
856 }
857
858 void SIMIX_comm_copy_buffer_callback(smx_action_t comm, size_t buff_size)
859 {
860   memcpy(comm->comm.dst_buff, comm->comm.src_buff, buff_size);
861 }
862
863 /**
864  *  \brief Copy the communication data from the sender's buffer to the receiver's one
865  *  \param comm The communication
866  */
867 void SIMIX_comm_copy_data(smx_action_t comm)
868 {
869   size_t buff_size = comm->comm.src_buff_size;
870   /* If there is no data to be copy then return */
871   if (!comm->comm.src_buff || !comm->comm.dst_buff || comm->comm.copied == 1)
872     return;
873
874   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
875          comm,
876          comm->comm.src_proc->smx_host->name, comm->comm.src_buff,
877          comm->comm.dst_proc->smx_host->name, comm->comm.dst_buff, buff_size);
878
879   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
880   if (comm->comm.dst_buff_size)
881     buff_size = MIN(buff_size, *(comm->comm.dst_buff_size));
882
883   /* Update the receiver's buffer size to the copied amount */
884   if (comm->comm.dst_buff_size)
885     *comm->comm.dst_buff_size = buff_size;
886
887   if (buff_size == 0)
888     return;
889
890   (*SIMIX_comm_copy_data_callback) (comm, buff_size);
891
892   /* Set the copied flag so we copy data only once */
893   /* (this function might be called from both communication ends) */
894   comm->comm.copied = 1;
895 }