Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improved and tested docummentation.
[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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13                                 "Logging specific to SIMIX (network)");
14
15 /******************************************************************************/
16 /*                           Rendez-Vous Points                               */
17 /******************************************************************************/ 
18
19 /**
20  *  \brief Creates a new rendez-vous point
21  *  \param name The name of the rendez-vous point
22  *  \return The created rendez-vous point
23  */
24 smx_rdv_t SIMIX_rdv_create(const char *name)
25 {
26   smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
27   rdv->name = name ? xbt_strdup(name) : NULL;
28   rdv->read = SIMIX_mutex_init();
29   rdv->write = SIMIX_mutex_init();
30   rdv->comm_fifo = xbt_fifo_new();
31
32   return rdv;
33 }
34
35 /**
36  *  \brief Destroy a rendez-vous point
37  *  \param name The rendez-vous point to destroy
38  */
39 void SIMIX_rdv_destroy(smx_rdv_t rdv)
40 {
41   if(rdv->name)
42     xbt_free(rdv->name);
43   SIMIX_mutex_destroy(rdv->read);
44   SIMIX_mutex_destroy(rdv->write);
45   xbt_fifo_free(rdv->comm_fifo);
46   xbt_free(rdv);
47 }
48
49 /**
50  *  \brief Push a communication request into a rendez-vous point
51  *  \param rdv The rendez-vous point
52  *  \param comm The communication request
53  */
54 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
55 {
56   xbt_fifo_push(rdv->comm_fifo, comm);
57   comm->rdv = rdv;
58 }
59
60 /**
61  *  \brief Remove a communication request from a rendez-vous point
62  *  \param rdv The rendez-vous point
63  *  \param comm The communication request
64  */
65 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
66 {
67   xbt_fifo_remove(rdv->comm_fifo, comm);
68   comm->rdv = NULL;
69 }
70   
71 /**
72  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
73  *  \param type The type of communication we are looking for (comm_send, comm_recv)
74  *  \return The communication request if found, NULL otherwise.
75  */
76 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type)
77 {
78   smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
79                                   xbt_fifo_get_first_item(rdv->comm_fifo));
80
81   if(comm && comm->type == type){
82     DEBUG0("Communication request found!");
83     xbt_fifo_shift(rdv->comm_fifo);
84     SIMIX_communication_use(comm);
85     return comm;
86   }
87
88   /* no relevant request found. Return NULL */
89   DEBUG0("Communication request not found");
90   return NULL;
91 }
92
93 /**
94  *  \brief counts the number of communication requests of a given host pending
95  *         on a rendez-vous point
96  *  \param rdv The rendez-vous point
97  *  \param host The host to be counted
98  *  \return The number of comm request pending in the rdv
99  */
100 int 
101 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
102 {
103   smx_comm_t comm = NULL;
104   xbt_fifo_item_t item = NULL;
105   int count = 0;
106
107   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
108     if (comm->src_proc->smx_host == host)
109       count++;
110   }
111
112   return count;
113 }
114
115 /**
116  *  \brief returns the communication at the head of the rendez-vous
117  *  \param rdv The rendez-vous point
118  *  \return The communication or NULL if empty
119  */
120 smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
121 {
122   return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
123 }
124
125
126 /******************************************************************************/
127 /*                           Communication Requests                           */
128 /******************************************************************************/ 
129
130 /**
131  *  \brief Creates a new communication request
132  *  \param type The type of communication (comm_send, comm_recv)
133  *  \return The new communication request
134  */  
135 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
136 {
137   /* alloc structures */
138   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
139   comm->type = type;
140   comm->cond = SIMIX_cond_init();
141   comm->refcount = 1;
142   
143   return comm;
144 }
145
146 /**
147  *  \brief Destroy a communication request
148  *  \param comm The request to be destroyed
149  */
150 void SIMIX_communication_destroy(smx_comm_t comm)
151 {
152   comm->refcount--;
153   if(comm->refcount == 0){
154     if(comm->act != NULL)
155       SIMIX_action_destroy(comm->act);
156
157     xbt_free(comm->cond);
158     xbt_free(comm);
159   }
160 }
161
162 /**
163  *  \brief Increase the number of users of the communication.
164  *  \param comm The communication request
165  *  Each communication request can be used by more than one process, so it is
166  *  necessary to know number of them at destroy time, to avoid freeing stuff that
167  *  maybe is in use by others.
168  *  \
169  */
170 static inline void SIMIX_communication_use(smx_comm_t comm)
171 {
172   comm->refcount++;
173 }
174
175 /**
176  *  \brief Start the simulation of a communication request
177  *  \param comm The communication request
178  */
179 static inline void SIMIX_communication_start(smx_comm_t comm)
180 {
181   /* If both the sender and the receiver are already there, start the communication */
182   if(comm->src_proc && comm->dst_proc){
183     DEBUG1("Starting communication %p", comm);
184     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host, 
185                                          comm->dst_proc->smx_host, NULL, 
186                                          comm->task_size, comm->rate);
187
188     /* If any of the process is suspend, create the action but stop its execution,
189        it will be restart when the sender process resume */
190     if(SIMIX_process_is_suspended(comm->src_proc) || 
191        SIMIX_process_is_suspended(comm->dst_proc)) {
192       SIMIX_action_set_priority(comm->act, 0);
193     }
194     
195     /* Add the communication as user data of the action */
196     comm->act->data = comm;
197     
198     SIMIX_register_action_to_condition(comm->act, comm->cond);
199   }
200 }
201
202 /**
203  *  \brief Waits for communication completion and performs error checking
204  *  \param comm The communication
205  *  \param timeout The max amount of time to wait for the communication to finish
206  *
207  *  Throws:
208  *   - host_error if peer failed
209  *   - timeout_error if communication reached the timeout specified
210  *   - network_error if network failed or peer issued a timeout
211  */
212 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
213 {
214   xbt_ex_t e;
215
216   DEBUG1("Waiting for the completion of communication %p", comm);
217   
218   if(timeout > 0){
219     TRY{
220       SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
221     }
222     CATCH(e){
223       /* If there is a timeout then cancel the communication if it is running or 
224          remove it from the rendez-vous otherwise. Then signal the other peer,
225          destroy the communication and retrow the exception. */
226       if(e.category == timeout_error){
227         DEBUG1("Communication timeout! %p", comm);
228         if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
229           SIMIX_communication_cancel(comm);
230         else
231           SIMIX_rdv_remove(comm->rdv, comm);
232           
233         SIMIX_cond_signal(comm->cond);
234         SIMIX_communication_destroy(comm);
235       }
236       RETHROW;
237     }
238   }else{
239     SIMIX_cond_wait(comm->cond, NULL);
240   }
241
242   DEBUG1("Communication %p complete! Let's check for errors", comm);
243   
244   /* Check for errors other than timeouts (they are catched above) */
245   if(!SIMIX_host_get_state(SIMIX_host_self())){
246     SIMIX_communication_destroy(comm);
247     THROW0(host_error, 0, "Host failed");
248   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
249     SIMIX_communication_destroy(comm);
250     THROW0(network_error, 0, "Link failure");
251   }
252
253   SIMIX_unregister_action_to_condition(comm->act, comm->cond);
254 }
255
256 /**
257  *  \brief Cancels a communication
258  *  \brief comm The communication to cancel
259  */
260 void SIMIX_communication_cancel(smx_comm_t comm)
261 {
262   SIMIX_action_cancel(comm->act);
263 }
264
265 /**
266  *  \brief get the amount remaining from the communication
267  *  \param comm The communication
268  */
269 double SIMIX_communication_get_remains(smx_comm_t comm)
270 {
271   return SIMIX_action_get_remains(comm->act);
272 }  
273
274 /**
275  *  \brief Copy the communication data from the sender's buffer to the receiver's one
276  *  \param comm The communication
277  */
278 void SIMIX_network_copy_data(smx_comm_t comm)
279 {
280   /* If there is no data to be copy then return */
281   if(!comm->src_buff || !comm->dst_buff)
282     return;
283   
284   size_t src_buff_size = comm->src_buff_size;
285   size_t dst_buff_size = *comm->dst_buff_size;
286   
287   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
288   dst_buff_size = MIN(dst_buff_size, src_buff_size);
289   
290   /* Update the receiver's buffer size to the copied amount */
291   *comm->dst_buff_size = dst_buff_size;
292
293   if(dst_buff_size == 0)
294     return;
295
296   memcpy(comm->dst_buff, comm->src_buff, dst_buff_size);
297
298   DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)", 
299          comm, comm->src_proc->smx_host->name, comm->dst_proc->smx_host->name,
300          dst_buff_size);
301 }
302
303 /**
304  *  \brief Return the user data associated to the communication
305  *  \param comm The communication
306  *  \return the user data
307  */
308 void *SIMIX_communication_get_data(smx_comm_t comm)
309 {
310   return comm->data;
311 }
312
313 /******************************************************************************/
314 /*                        Synchronous Communication                           */
315 /******************************************************************************/
316 /**
317  *  \brief Put a send communication request in a rendez-vous point and waits for
318  *         its completion (blocking)
319  *  \param rdv The rendez-vous point
320  *  \param task_size The size of the communication action (for surf simulation)
321  *  \param rate The rate of the communication action (for surf)
322  *  \param timeout The timeout used for the waiting the completion 
323  *  \param src_buff The source buffer containing the message to be sent
324  *  \param src_buff_size The size of the source buffer
325  *  \param comm_ref The communication object used for the send
326  *  \param data User data associated to the communication object
327  *  Throws:
328  *   - host_error if peer failed
329  *   - timeout_error if communication reached the timeout specified
330  *   - network_error if network failed or peer issued a timeout
331  */
332 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate, 
333                         double timeout, void *src_buff, size_t src_buff_size,
334                         smx_comm_t *comm_ref, void *data)
335 {
336   smx_comm_t comm;
337   
338   /* Look for communication request matching our needs. 
339      If it is not found then create it and push it into the rendez-vous point */
340   comm = SIMIX_rdv_get_request(rdv, comm_recv);
341
342   if(!comm){
343     comm = SIMIX_communication_new(comm_send);
344     SIMIX_rdv_push(rdv, comm);
345   }
346
347   /* Update the communication reference with the comm to be used */
348   *comm_ref = comm;
349   
350   /* Setup the communication request */
351   comm->src_proc = SIMIX_process_self();
352   comm->task_size = task_size;
353   comm->rate = rate;
354   comm->src_buff = src_buff;
355   comm->src_buff_size = src_buff_size;
356   comm->data = data;
357
358   SIMIX_communication_start(comm);
359
360   /* Wait for communication completion */
361   SIMIX_communication_wait_for_completion(comm, timeout);
362
363   SIMIX_communication_destroy(comm);
364 }
365
366 /**
367  *  \brief Put a receive communication request in a rendez-vous point and waits
368  *         for its completion (blocking)
369  *  \param rdv The rendez-vous point
370  *  \param timeout The timeout used for the waiting the completion 
371  *  \param dst_buff The destination buffer to copy the received message
372  *  \param src_buff_size The size of the destination buffer
373  *  \param comm_ref The communication object used for the send
374  *  Throws:
375  *   - host_error if peer failed
376  *   - timeout_error if communication reached the timeout specified
377  *   - network_error if network failed or peer issued a timeout
378  */
379 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff, 
380                         size_t *dst_buff_size, smx_comm_t *comm_ref)
381 {
382   smx_comm_t comm;
383
384   /* Look for communication request matching our needs. 
385      If it is not found then create it and push it into the rendez-vous point */
386   comm = SIMIX_rdv_get_request(rdv, comm_send);
387
388   if(!comm){
389     comm = SIMIX_communication_new(comm_recv);
390     SIMIX_rdv_push(rdv, comm);
391   }
392
393   /* Update the communication reference with the comm to be used */
394   *comm_ref = comm;
395  
396   /* Setup communication request */
397   comm->dst_proc = SIMIX_process_self();
398   comm->dst_buff = dst_buff;
399   comm->dst_buff_size = dst_buff_size;
400
401   SIMIX_communication_start(comm);
402
403   /* Wait for communication completion */
404   SIMIX_communication_wait_for_completion(comm, timeout);
405
406   SIMIX_communication_destroy(comm);
407 }
408
409 /******************************************************************************/
410 /*                        Asynchronous Communication                          */
411 /******************************************************************************/
412
413 /*
414 void SIMIX_network_wait(smx_action_t comm, double timeout)
415 {
416   TO BE IMPLEMENTED
417 }
418
419 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)
420 {
421   TO BE IMPLEMENTED
422 }*/
423
424
425
426
427
428
429