Logo AND Algorithmique Numérique Distribuée

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