Logo AND Algorithmique Numérique Distribuée

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