Logo AND Algorithmique Numérique Distribuée

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