Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improvements to the MSG port on top of SIMIX network. All MSG tests pass now.
[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 /*                           Communication Requests                           */
95 /******************************************************************************/ 
96
97 /**
98  *  \brief Creates a new communication request
99  *  \param type The type of communication (comm_send, comm_recv)
100  *  \return The new communication request
101  */  
102 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
103 {
104   /* alloc structures */
105   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
106   comm->type = type;
107   comm->cond = SIMIX_cond_init();
108   comm->refcount = 1;
109   
110   return comm;
111 }
112
113 /**
114  *  \brief Destroy a communication request
115  *  \param comm The request to be destroyed
116  */
117 void SIMIX_communication_destroy(smx_comm_t comm)
118 {
119   comm->refcount--;
120   if(comm->refcount == 0){
121     if(comm->act != NULL)
122       SIMIX_action_destroy(comm->act);
123
124     xbt_free(comm->cond);
125     xbt_free(comm);
126   }
127 }
128
129 /**
130  *  \brief Increase the number of users of the communication.
131  *  \param comm The communication request
132  *  Each communication request can be used by more than one process, so it is
133  *  necessary to know number of them at destroy time, to avoid freeing stuff that
134  *  maybe is in use by others.
135  *  \
136  */
137 static inline void SIMIX_communication_use(smx_comm_t comm)
138 {
139   comm->refcount++;
140 }
141
142 /**
143  *  \brief Start the simulation of a communication request
144  *  \param comm The communication request
145  */
146 static inline void SIMIX_communication_start(smx_comm_t comm)
147 {
148   /* If both the sender and the receiver are already there, start the communication */
149   if(comm->src_proc && comm->dst_proc){
150     DEBUG1("Starting communication %p", comm);
151     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host, 
152                                          comm->dst_proc->smx_host, NULL, 
153                                          comm->task_size, comm->rate);
154
155     /* If any of the process is suspend, create the action but stop its execution,
156        it will be restart when the sender process resume */
157     if(SIMIX_process_is_suspended(comm->src_proc) || 
158        SIMIX_process_is_suspended(comm->dst_proc)) {
159       SIMIX_action_set_priority(comm->act, 0);
160     }
161     
162     /* Add the communication as user data of the action */
163     comm->act->data = comm;
164     
165     SIMIX_register_action_to_condition(comm->act, comm->cond);
166   }else{
167     DEBUG1("Communication %p cannot be started, peer missing", comm);
168   }
169 }
170
171 /**
172  *  \brief Waits for communication completion and performs error checking
173  *  \param comm The communication
174  *  \param timeout The max amount of time to wait for the communication to finish
175  *
176  *  Throws:
177  *   - host_error if peer failed
178  *   - timeout_error if communication reached the timeout specified
179  *   - network_error if network failed or peer issued a timeout
180  */
181 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
182 {
183   xbt_ex_t e;
184
185   DEBUG1("Waiting for the completion of communication %p", comm);
186   
187   if(timeout > 0){
188     TRY{
189       SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
190     }
191     CATCH(e){
192       /* If it's a timeout then cancel the communication and signal the other peer */
193       if(e.category == timeout_error){
194         DEBUG1("Communication timeout! %p", comm);
195         if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
196           SIMIX_action_cancel(comm->act);
197         else
198           SIMIX_rdv_remove(comm->rdv, comm);
199           
200         SIMIX_cond_signal(comm->cond);
201         SIMIX_communication_destroy(comm);
202       }
203       RETHROW;
204     }
205   }else{
206     SIMIX_cond_wait(comm->cond, NULL);
207   }
208
209   DEBUG1("Communication %p complete! Let's check for errors", comm);
210   
211   /* Check for errors */
212   if(!SIMIX_host_get_state(SIMIX_host_self())){
213     SIMIX_communication_destroy(comm);
214     THROW0(host_error, 0, "Host failed");
215   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
216     SIMIX_communication_destroy(comm);
217     THROW0(network_error, 0, "Link failure");
218   }
219
220   SIMIX_unregister_action_to_condition(comm->act, comm->cond);
221 }
222
223 /**
224  *  \brief Copy the communication data from the sender's buffer to the receiver's one
225  *  \param comm The communication
226  */
227 void SIMIX_network_copy_data(smx_comm_t comm)
228 {
229   size_t src_buff_size = comm->src_buff_size;
230   size_t dst_buff_size = *comm->dst_buff_size;
231
232   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
233   dst_buff_size = MIN(dst_buff_size, src_buff_size);
234
235   /* Update the receiver's buffer size to the copied amount */
236   *comm->dst_buff_size = dst_buff_size;
237
238   memcpy(comm->dst_buff, comm->src_buff, dst_buff_size);
239
240   DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)", 
241          comm, comm->src_proc->smx_host->name, comm->dst_proc->smx_host->name,
242          dst_buff_size);
243 }
244
245 /******************************************************************************/
246 /*                        Synchronous Communication                           */
247 /******************************************************************************/
248 /*  Throws:
249  *   - host_error if peer failed
250  *   - timeout_error if communication reached the timeout specified
251  *   - network_error if network failed or peer issued a timeout
252  */
253 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate, 
254                         double timeout, void *data, size_t data_size)
255 {
256   smx_comm_t comm;
257   
258   /* Look for communication request matching our needs. 
259      If it is not found then create it and push it into the rendez-vous point */
260   comm = SIMIX_rdv_get_request(rdv, comm_recv);
261
262   if(!comm){
263     comm = SIMIX_communication_new(comm_send);
264     SIMIX_rdv_push(rdv, comm);
265   }
266
267   /* Setup the communication request */
268   comm->src_proc = SIMIX_process_self();
269   comm->task_size = task_size;
270   comm->rate = rate;
271   comm->src_buff = data;
272   comm->src_buff_size = data_size;
273
274   SIMIX_communication_start(comm);
275
276   /* Wait for communication completion */
277   SIMIX_communication_wait_for_completion(comm, timeout);
278
279   SIMIX_communication_destroy(comm);
280 }
281
282 /*  Throws:
283  *   - host_error if peer failed
284  *   - timeout_error if communication reached the timeout specified
285  *   - network_error if network failed or peer issued a timeout
286  */
287 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *data, size_t *data_size)
288 {
289   smx_comm_t comm;
290
291   /* Look for communication request matching our needs. 
292      If it is not found then create it and push it into the rendez-vous point */
293   comm = SIMIX_rdv_get_request(rdv, comm_send);
294
295   if(!comm){
296     comm = SIMIX_communication_new(comm_recv);
297     SIMIX_rdv_push(rdv, comm);
298   }
299
300   /* Setup communication request */
301   comm->dst_proc = SIMIX_process_self();
302   comm->dst_buff = data;
303   comm->dst_buff_size = data_size;
304
305   SIMIX_communication_start(comm);
306
307   /* Wait for communication completion */
308   SIMIX_communication_wait_for_completion(comm, timeout);
309
310   SIMIX_communication_destroy(comm);
311 }
312
313 /******************************************************************************/
314 /*                        Asynchronous Communication                          */
315 /******************************************************************************/
316
317 /*
318 void SIMIX_network_wait(smx_action_t comm, double timeout)
319 {
320   TO BE IMPLEMENTED
321 }
322
323 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)
324 {
325   TO BE IMPLEMENTED
326 }*/
327
328
329
330
331
332
333