Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Delay the copy of the data of the communication actions until one of the peers calls...
[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 /* Pimple to get an histogram of message sizes in the simulation */
13 xbt_dict_t msg_sizes = NULL;
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
16                                 "Logging specific to SIMIX (network)");
17
18 /******************************************************************************/
19 /*                           Rendez-Vous Points                               */
20 /******************************************************************************/ 
21
22 /**
23  *  \brief Creates a new rendez-vous point
24  *  \param name The name of the rendez-vous point
25  *  \return The created rendez-vous point
26  */
27 smx_rdv_t SIMIX_rdv_create(const char *name)
28 {
29   smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
30   rdv->name = name ? xbt_strdup(name) : NULL;
31   rdv->read = SIMIX_mutex_init();
32   rdv->write = SIMIX_mutex_init();
33   rdv->comm_fifo = xbt_fifo_new();
34
35   return rdv;
36 }
37
38 /**
39  *  \brief Destroy a rendez-vous point
40  *  \param name The rendez-vous point to destroy
41  */
42 void SIMIX_rdv_destroy(smx_rdv_t rdv)
43 {
44   if(rdv->name)
45     xbt_free(rdv->name);
46   SIMIX_mutex_destroy(rdv->read);
47   SIMIX_mutex_destroy(rdv->write);
48   xbt_fifo_free(rdv->comm_fifo);
49   xbt_free(rdv);
50 }
51
52 /**
53  *  \brief Push a communication request into a rendez-vous point
54  *  \param rdv The rendez-vous point
55  *  \param comm The communication request
56  */
57 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
58 {
59   xbt_fifo_push(rdv->comm_fifo, comm);
60   comm->rdv = rdv;
61 }
62
63 /**
64  *  \brief Remove a communication request from a rendez-vous point
65  *  \param rdv The rendez-vous point
66  *  \param comm The communication request
67  */
68 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
69 {
70   xbt_fifo_remove(rdv->comm_fifo, comm);
71   comm->rdv = NULL;
72 }
73   
74 /**
75  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
76  *  \param type The type of communication we are looking for (comm_send, comm_recv)
77  *  \return The communication request if found, NULL otherwise.
78  */
79 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type) {
80   smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
81                                   xbt_fifo_get_first_item(rdv->comm_fifo));
82
83   if(comm && comm->type == type){
84     DEBUG0("Communication request found!");
85     xbt_fifo_shift(rdv->comm_fifo);
86     SIMIX_communication_use(comm);
87     comm->rdv = NULL;
88     return comm;
89   }
90
91   DEBUG0("Communication request not found");
92   return NULL;
93 }
94
95 /**
96  *  \brief counts the number of communication requests of a given host pending
97  *         on a rendez-vous point
98  *  \param rdv The rendez-vous point
99  *  \param host The host to be counted
100  *  \return The number of comm request pending in the rdv
101  */
102 int 
103 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
104 {
105   smx_comm_t comm = NULL;
106   xbt_fifo_item_t item = NULL;
107   int count = 0;
108
109   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
110     if (comm->src_proc->smx_host == host)
111       count++;
112   }
113
114   return count;
115 }
116
117 /**
118  *  \brief returns the communication at the head of the rendez-vous
119  *  \param rdv The rendez-vous point
120  *  \return The communication or NULL if empty
121  */
122 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
123 {
124   return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
125 }
126
127 /** @brief adds some API-related data to the rendez-vous point */
128 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
129   rdv->data=data;
130 }
131 /** @brief gets API-related data from the rendez-vous point */
132 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
133   return rdv->data;
134 }
135
136 /******************************************************************************/
137 /*                           Communication Requests                           */
138 /******************************************************************************/ 
139
140 /**
141  *  \brief Creates a new communication request
142  *  \param type The type of communication (comm_send, comm_recv)
143  *  \return The new communication request
144  */  
145 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
146 {
147   /* alloc structures */
148   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
149   comm->type = type;
150   comm->sem = SIMIX_sem_init(0);
151   comm->refcount = 1;
152   
153   return comm;
154 }
155
156 /**
157  *  \brief Destroy a communication request
158  *  \param comm The request to be destroyed
159  */
160 void SIMIX_communication_destroy(smx_comm_t comm)
161 {
162   comm->refcount--;
163   if(comm->refcount > 0)
164     return;
165
166   if(comm->sem){
167     SIMIX_sem_destroy(comm->sem);
168     comm->sem = NULL;
169   }
170   
171   if(comm->act){
172     SIMIX_action_destroy(comm->act);
173     comm->act = NULL;
174   }
175
176   if(comm->src_timeout){
177     SIMIX_action_destroy(comm->src_timeout);
178     comm->src_timeout = NULL;
179   }
180
181   if(comm->dst_timeout){
182       SIMIX_action_destroy(comm->dst_timeout);
183       comm->dst_timeout = NULL;
184     }
185
186   xbt_free(comm);
187 }
188
189 /**
190  *  \brief Increase the number of users of the communication.
191  *  \param comm The communication request
192  *  Each communication request can be used by more than one process, so it is
193  *  necessary to know number of them at destroy time, to avoid freeing stuff that
194  *  maybe is in use by others.
195  *  \
196  */
197 static inline void SIMIX_communication_use(smx_comm_t comm)
198 {
199   comm->refcount++;
200 }
201
202 /**
203  *  \brief Start the simulation of a communication request
204  *  \param comm The communication request
205  */
206 static inline void SIMIX_communication_start(smx_comm_t comm)
207 {
208   /* If both the sender and the receiver are already there, start the communication */
209   if(comm->src_proc && comm->dst_proc){
210     DEBUG1("Starting communication %p", comm);
211     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host, 
212                                          comm->dst_proc->smx_host, NULL, 
213                                          comm->task_size, comm->rate);
214 #ifdef HAVE_TRACING
215     TRACE_smx_action_communicate (comm->act, comm->src_proc);
216 #endif
217
218     /* If any of the process is suspend, create the action but stop its execution,
219        it will be restarted when the sender process resume */
220     if(SIMIX_process_is_suspended(comm->src_proc) || 
221        SIMIX_process_is_suspended(comm->dst_proc)) {
222       SIMIX_action_suspend(comm->act);
223     }
224     
225     /* Add the communication as user data of the action */
226     comm->act->data = comm;
227
228     /* The semaphore will only get signaled once, but since the first unlocked guy will
229      * release_forever() the semaphore, that will unlock the second (and any other)
230      * communication partner */
231     SIMIX_register_action_to_semaphore(comm->act, comm->sem);
232   }
233 }
234
235 /**
236  *  \brief Waits for communication completion and performs error checking
237  *  \param comm The communication
238  *  \param timeout The max amount of time to wait for the communication to finish
239  *
240  *  Throws:
241  *   - host_error if local peer failed
242  *   - timeout_error if communication reached the timeout specified (either because of local peer or remote peer)
243  *   - network_error if network failed or remote peer failed
244  */
245 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
246 {
247   smx_action_t act_sleep = NULL;
248   int src_timeout = 0;
249   int dst_timeout = 0;
250
251   DEBUG1("Waiting for the completion of communication %p", comm);
252   
253   if (timeout >= 0) {
254     act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
255                 if(SIMIX_process_self()==comm->src_proc)
256                         comm->src_timeout = act_sleep;
257                 else
258                         comm->dst_timeout = act_sleep;
259     SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
260     SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
261     SIMIX_process_self()->waiting_action = act_sleep;
262     SIMIX_sem_block_onto(comm->sem);
263     SIMIX_process_self()->waiting_action = NULL;
264     SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
265   } else {
266     SIMIX_sem_acquire(comm->sem);
267   }
268
269   /* Check for timeouts */
270   if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
271       (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
272                         /* Somebody did a timeout! */
273     if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
274     if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
275
276     if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
277       SIMIX_communication_cancel(comm);
278     else if (comm->rdv)
279       SIMIX_rdv_remove(comm->rdv, comm);
280
281     /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
282     SIMIX_sem_release_forever(comm->sem);
283     SIMIX_communication_destroy(comm);
284
285     THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
286   }
287
288   DEBUG1("Communication %p complete! Let's check for errors", comm);
289
290   /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
291   SIMIX_sem_release_forever(comm->sem);
292   
293   /* Check for errors other than timeouts (they are catched above) */
294   if (!SIMIX_host_get_state(SIMIX_host_self())){
295     if(comm->rdv)
296       SIMIX_rdv_remove(comm->rdv, comm);
297     SIMIX_communication_destroy(comm);
298     THROW0(host_error, 0, "Host failed");
299   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
300     SIMIX_communication_destroy(comm);
301     THROW0(network_error, 0, "Link failure");
302   } else if (!SIMIX_host_get_state(SIMIX_process_get_host(comm->dst_proc)) ||
303       !SIMIX_host_get_state(SIMIX_process_get_host(comm->src_proc))) {
304     /* We test both src&dst because we dunno who we are today, and we already tested myself above.
305      *    So, at the end, we test the remote peer only
306      * Moreover, we have to test it because if the remote peer fails, the action comm->act is not done nor failed.
307      *    In that case, we got awaken by the little endless actions created in the SIMIX_sem_acquire(comm->sem)
308      *    at the beginning of this function. */
309     SIMIX_communication_destroy(comm);
310     THROW0(network_error, 0, "Remote peer failed");
311
312   }
313   /* Copy network data */
314   SIMIX_network_copy_data(comm);  
315
316   SIMIX_communication_destroy(comm);
317 }
318
319 /**
320  *  \brief Cancels a communication
321  *  \brief comm The communication to cancel
322  */
323 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
324 {
325   if (comm->act)
326     SIMIX_action_cancel(comm->act);
327 }
328
329 /**
330  *  \brief get the amount remaining from the communication
331  *  \param comm The communication
332  */
333 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
334 {
335   return SIMIX_action_get_remains(comm->act);
336 }  
337
338 /******************************************************************************/
339 /*                    SIMIX_network_copy_data callbacks                       */
340 /******************************************************************************/
341 static void (*SIMIX_network_copy_data_callback)(smx_comm_t, size_t) = &SIMIX_network_copy_pointer_callback;
342
343 void SIMIX_network_set_copy_data_callback(void (*callback)(smx_comm_t, size_t)) {
344   SIMIX_network_copy_data_callback = callback;
345 }
346
347 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size) {
348   xbt_assert1((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)",buff_size);
349   *(void**)(comm->dst_buff) = comm->src_buff;
350 }
351
352 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size) {
353   memcpy(comm->dst_buff, comm->src_buff, buff_size);
354 }
355
356 /**
357  *  \brief Copy the communication data from the sender's buffer to the receiver's one
358  *  \param comm The communication
359  */
360 void SIMIX_network_copy_data(smx_comm_t comm)
361 {
362   size_t buff_size = comm->src_buff_size;
363
364   /* If there is no data to be copy then return */
365   if(!comm->src_buff || !comm->dst_buff)
366     return;
367
368   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
369       comm,
370       comm->src_proc->smx_host->name, comm->src_buff,
371       comm->dst_proc->smx_host->name, comm->dst_buff,
372       buff_size);
373
374   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
375   if (comm->dst_buff_size)
376     buff_size = MIN(buff_size,*(comm->dst_buff_size));
377   
378   /* Update the receiver's buffer size to the copied amount */
379   if (comm->dst_buff_size)
380     *comm->dst_buff_size = buff_size;
381
382   if(buff_size == 0)
383     return;
384   (*SIMIX_network_copy_data_callback)(comm, buff_size);
385
386   /* Set the buffers to null so we copy data only once */
387   /* (this function might be called from both communication ends)*/
388   comm->src_buff = NULL;
389   comm->dst_buff = NULL;
390   
391   /* pimple to display the message sizes */
392   {
393     if (msg_sizes == NULL)
394       msg_sizes = xbt_dict_new();
395     uintptr_t casted_size = comm->task_size;
396     uintptr_t amount = xbt_dicti_get(msg_sizes, casted_size);
397     amount++;
398
399     xbt_dicti_set(msg_sizes,casted_size, amount);
400   }
401 }
402 #include "xbt.h"
403 /* pimple to display the message sizes */
404 void SIMIX_message_sizes_output(const char *filename) {
405   FILE * out = fopen(filename,"w");
406   xbt_assert1(out,"Cannot open file %s",filename);
407   uintptr_t key,data;
408   xbt_dict_cursor_t cursor;
409   xbt_dict_foreach(msg_sizes,cursor,key,data) {
410     fprintf(out,"%zu %zu\n",key,data);
411   }
412   fclose(out);
413 }
414
415 /**
416  *  \brief Return the user data associated to the communication
417  *  \param comm The communication
418  *  \return the user data
419  */
420 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
421 {
422   return comm->data;
423 }
424
425 /******************************************************************************/
426 /*                        Synchronous Communication                           */
427 /******************************************************************************/
428 /**
429  *  \brief Put a send communication request in a rendez-vous point and waits for
430  *         its completion (blocking)
431  *  \param rdv The rendez-vous point
432  *  \param task_size The size of the communication action (for surf simulation)
433  *  \param rate The rate of the communication action (for surf)
434  *  \param timeout The timeout used for the waiting the completion 
435  *  \param src_buff The source buffer containing the message to be sent
436  *  \param src_buff_size The size of the source buffer
437  *  \param comm_ref The communication object used for the send  (useful if someone else wants to cancel this communication afterward)
438  *  \param data User data associated to the communication object
439  *  Throws:
440  *   - host_error if peer failed
441  *   - timeout_error if communication reached the timeout specified
442  *   - network_error if network failed or peer issued a timeout
443  */
444 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
445                         double timeout, void *src_buff, size_t src_buff_size,
446                         smx_comm_t *comm_ref, void *data)
447 {
448   *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
449   SIMIX_network_wait(*comm_ref,timeout);
450 }
451
452 /**
453  *  \brief Put a receive communication request in a rendez-vous point and waits
454  *         for its completion (blocking)
455  *  \param rdv The rendez-vous point
456  *  \param timeout The timeout used for the waiting the completion 
457  *  \param dst_buff The destination buffer to copy the received message
458  *  \param src_buff_size The size of the destination buffer
459  *  \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
460  *  Throws:
461  *   - host_error if peer failed
462  *   - timeout_error if communication reached the timeout specified
463  *   - network_error if network failed or peer issued a timeout
464  */
465 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
466                         size_t *dst_buff_size, smx_comm_t *comm_ref)
467 {
468   *comm_ref = SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
469   SIMIX_network_wait(*comm_ref,timeout);
470 }
471
472 /******************************************************************************/
473 /*                        Asynchronous Communication                          */
474 /******************************************************************************/
475 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
476     void *src_buff, size_t src_buff_size, void *data)
477 {
478   smx_comm_t comm;
479   mc_transition_t trans=NULL;
480
481   if (_surf_do_model_check) {
482     /* Let's intercept the communication and control it from the model-checker */
483     trans = MC_create_transition(mc_isend, SIMIX_process_self(), rdv, NULL);
484     SIMIX_process_yield();
485   }
486   
487   /* Look for communication request matching our needs.
488      If it is not found then create it and push it into the rendez-vous point */
489   comm = SIMIX_rdv_get_request(rdv, comm_recv);
490
491   if(!comm){
492     comm = SIMIX_communication_new(comm_send);
493     SIMIX_rdv_push(rdv, comm);
494   }
495
496   /* Setup the communication request */
497   comm->src_proc = SIMIX_process_self();
498   comm->task_size = task_size;
499   comm->rate = rate;
500   comm->src_buff = src_buff;
501   comm->src_buff_size = src_buff_size;
502   comm->data = data;
503
504   /* Associate the simix communication to the mc transition */
505   if (_surf_do_model_check)
506     MC_transition_set_comm(trans, comm);
507   
508   SIMIX_communication_start(comm);
509   return comm;
510 }
511
512 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size) {
513   smx_comm_t comm;
514   mc_transition_t trans=NULL;
515   
516   if (_surf_do_model_check) {
517     /* Let's intercept the communication and control it from the model-checker */
518     trans = MC_create_transition(mc_irecv, SIMIX_process_self(), rdv, NULL);
519     SIMIX_process_yield();
520   }
521   
522   /* Look for communication request matching our needs.
523      If it is not found then create it and push it into the rendez-vous point */
524   comm = SIMIX_rdv_get_request(rdv, comm_send);
525
526   if(!comm){
527     comm = SIMIX_communication_new(comm_recv);
528     SIMIX_rdv_push(rdv, comm);
529   }
530
531   /* Setup communication request */
532   comm->dst_proc = SIMIX_process_self();
533   comm->dst_buff = dst_buff;
534   comm->dst_buff_size = dst_buff_size;
535
536   /* Associate the simix communication to the mc transition */
537   if (_surf_do_model_check)
538     MC_transition_set_comm(trans, comm);
539  
540   SIMIX_communication_start(comm);
541   return comm;
542 }
543
544 /** @brief blocks until the communication terminates or the timeout occurs */
545 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout) {
546   if (_surf_do_model_check) {
547     /* Let's intercept the communication and control it from the model-checker */
548     MC_create_transition(mc_wait, SIMIX_process_self(), comm->rdv, comm);
549     SIMIX_process_yield();
550   }
551   /* Wait for communication completion */
552   SIMIX_communication_wait_for_completion(comm, timeout);
553 }
554
555 /** @Returns whether the (asynchronous) communication is done yet or not */
556 XBT_INLINE int SIMIX_network_test(smx_comm_t comm) {
557   if (_surf_do_model_check) {
558     /* Let's intercept the communication and control it from the model-checker */
559     MC_create_transition(mc_test, SIMIX_process_self(), comm->rdv, comm);
560     SIMIX_process_yield();
561   }
562
563   /* Copy data if the communication is done */
564   if(comm->sem && !SIMIX_sem_would_block(comm->sem)){
565     /* Copy network data */
566     SIMIX_network_copy_data(comm);
567     return TRUE;
568   }
569   return FALSE;
570 }
571
572 /** @brief wait for the completion of any communication of a set
573  *
574  *  @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
575  */
576 unsigned int SIMIX_network_waitany(xbt_dynar_t comms) {
577   xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
578   unsigned int cursor, found_comm=-1;
579   smx_comm_t comm,comm_finished=NULL;
580
581   if (_surf_do_model_check) {
582     /* Let's intercept the communication and control it from the model-checker */
583     MC_create_transition(mc_waitany, SIMIX_process_self(), NULL, NULL);
584     SIMIX_process_yield();
585   }
586   
587   xbt_dynar_foreach(comms,cursor,comm)
588     xbt_dynar_push(sems,&(comm->sem));
589
590   DEBUG1("Waiting for the completion of communication set %p", comms);
591
592   found_comm = SIMIX_sem_acquire_any(sems);
593   xbt_dynar_free_container(&sems);
594   xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
595   xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
596
597   /* Check for errors and cleanup the comm */
598   SIMIX_communication_wait_for_completion(comm_finished,-1);
599   return found_comm;
600 }