Logo AND Algorithmique Numérique Distribuée

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