Logo AND Algorithmique Numérique Distribuée

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