3 /* Copyright (c) 2009 Cristian Rosa.
4 All rights reserved. */
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. */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13 "Logging specific to SIMIX (network)");
15 /******************************************************************************/
16 /* Rendez-Vous Points */
17 /******************************************************************************/
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
24 smx_rdv_t SIMIX_rdv_create(const char *name)
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();
36 * \brief Destroy a rendez-vous point
37 * \param name The rendez-vous point to destroy
39 void SIMIX_rdv_destroy(smx_rdv_t rdv)
43 SIMIX_mutex_destroy(rdv->read);
44 SIMIX_mutex_destroy(rdv->write);
45 xbt_fifo_free(rdv->comm_fifo);
50 * \brief Push a communication request into a rendez-vous point
51 * \param rdv The rendez-vous point
52 * \param comm The communication request
54 static inline void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
56 xbt_fifo_push(rdv->comm_fifo, comm);
60 * \brief Remove a communication request from a rendez-vous point
61 * \param rdv The rendez-vous point
62 * \param comm The communication request
64 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
66 xbt_fifo_remove(rdv->comm_fifo, comm);
71 * \brief Checks if there is a communication request queued in a rendez-vous matching our needs
72 * \param rdv The rendez-vous with the queue
73 * \param look_for_src boolean. True: we are receiver looking for sender; False: other way round
74 * \return The communication request if found, or a newly created one otherwise.
76 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, int (filter)(smx_comm_t, void*), void *arg) {
80 /* Traverse the rendez-vous queue looking for a comm request matching the
81 filter conditions. If found return it and remove it from the list. */
82 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
83 if(filter(comm, arg)){
84 SIMIX_communication_use(comm);
85 xbt_fifo_remove_item(rdv->comm_fifo, item);
86 DEBUG1("Communication request found! %p", comm);
91 /* no relevant request found. Return NULL */
92 DEBUG0("Communication request not found");
96 /******************************************************************************/
97 /* Communication Requests */
98 /******************************************************************************/
101 * \brief Creates a new communication request
102 * \param sender The process starting the communication (by send)
103 * \param receiver The process receiving the communication (by recv)
104 * \return the communication request
106 smx_comm_t SIMIX_communication_new(smx_comm_type_t type, smx_rdv_t rdv)
108 /* alloc structures */
109 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
111 comm->cond = SIMIX_cond_init();
119 * \brief Destroy a communication request
120 * \param comm The request to be destroyed
122 void SIMIX_communication_destroy(smx_comm_t comm)
125 if(comm->refcount == 0){
126 if(comm->act != NULL)
127 SIMIX_action_destroy(comm->act);
129 xbt_free(comm->cond);
135 * \brief Increase the number of users of the communication.
136 * \param comm The communication request
137 * Each communication request can be used by more than one process, so it is
138 * necessary to know number of them at destroy time, to avoid freeing stuff that
139 * maybe is in use by others.
142 static inline void SIMIX_communication_use(smx_comm_t comm)
148 * \brief Start the simulation of a communication request
149 * \param comm The communication request
151 static inline void SIMIX_communication_start(smx_comm_t comm)
153 /* If both the sender and the receiver are already there, start the communication */
154 if(comm->src_host != NULL && comm->dst_host != NULL){
155 DEBUG1("Starting communication %p", comm);
156 comm->act = SIMIX_action_communicate(comm->src_host, comm->dst_host, NULL,
157 comm->task_size, comm->rate);
159 /* Add the communication as user data of the action */
160 comm->act->data = comm;
162 SIMIX_register_action_to_condition(comm->act, comm->cond);
164 DEBUG1("Communication %p cannot be started, peer missing", comm);
169 * \brief Waits for communication completion and performs error checking
170 * \param comm The communication
171 * \param timeout The max amount of time to wait for the communication to finish
174 * - host_error if peer failed
175 * - timeout_error if communication reached the timeout specified
176 * - network_error if network failed or peer issued a timeout
178 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
182 DEBUG1("Waiting for the completion of communication %p", comm);
186 SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
189 /* If it's a timeout then cancel the communication and signal the other peer */
190 if(e.category == timeout_error){
191 DEBUG1("Communication timeout! %p", comm);
192 if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
193 SIMIX_action_cancel(comm->act);
195 SIMIX_rdv_remove(comm->rdv, comm);
197 SIMIX_cond_signal(comm->cond);
198 SIMIX_communication_destroy(comm);
203 SIMIX_cond_wait(comm->cond, NULL);
206 DEBUG1("Communication %p complete! Let's check for errors", comm);
208 /* Check for errors */
209 if(!SIMIX_host_get_state(SIMIX_host_self())){
210 SIMIX_communication_destroy(comm);
211 THROW0(host_error, 0, "Host failed");
212 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
213 SIMIX_communication_destroy(comm);
214 THROW0(network_error, 0, "Link failure");
217 SIMIX_unregister_action_to_condition(comm->act, comm->cond);
221 * \brief Copy the communication data from the sender's buffer to the receiver's one
222 * \param comm The communication
224 void SIMIX_network_copy_data(smx_comm_t comm)
226 size_t src_buff_size = comm->src_buff_size;
227 size_t dst_buff_size = *comm->dst_buff_size;
229 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
230 dst_buff_size = MIN(dst_buff_size, src_buff_size);
232 /* Update the receiver's buffer size to the copied amount */
233 *comm->dst_buff_size = dst_buff_size;
235 memcpy(comm->dst_buff, comm->src_buff, dst_buff_size);
237 DEBUG4("Copying comm %p data from %s -> %s (%zu bytes)",
238 comm, comm->src_host->name, comm->dst_host->name, dst_buff_size);
241 /* FIXME: move to some other place */
242 int comm_filter_get(smx_comm_t comm, void *arg)
244 if(comm->type == comm_send){
245 if(arg && comm->src_host != (smx_host_t)arg)
254 int comm_filter_put(smx_comm_t comm, void *arg)
256 return comm->type == comm_recv ? TRUE : FALSE;
258 /******************************************************************************/
259 /* Synchronous Communication */
260 /******************************************************************************/
262 * - host_error if peer failed
263 * - timeout_error if communication reached the timeout specified
264 * - network_error if network failed or peer issued a timeout
266 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
267 double timeout, void *data, size_t data_size,
268 int (filter)(smx_comm_t, void *), void *arg)
272 /* Look for communication request matching our needs.
273 If it is not found then create it and push it into the rendez-vous point */
274 comm = SIMIX_rdv_get_request(rdv, filter, arg);
277 comm = SIMIX_communication_new(comm_send, rdv);
278 SIMIX_rdv_push(rdv, comm);
281 /* Setup the communication request */
282 comm->src_host = SIMIX_host_self();
283 comm->task_size = task_size;
285 comm->src_buff = data;
286 comm->src_buff_size = data_size;
288 SIMIX_communication_start(comm);
290 /* Wait for communication completion */
291 /* FIXME: if the semantic is non blocking, it shouldn't wait on the condition here */
292 SIMIX_communication_wait_for_completion(comm, timeout);
294 SIMIX_communication_destroy(comm);
298 * - host_error if peer failed
299 * - timeout_error if communication reached the timeout specified
300 * - network_error if network failed or peer issued a timeout
302 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *data,
303 size_t *data_size, int (filter)(smx_comm_t, void *), void *arg)
307 /* Look for communication request matching our needs.
308 If it is not found then create it and push it into the rendez-vous point */
309 comm = SIMIX_rdv_get_request(rdv, filter, arg);
312 comm = SIMIX_communication_new(comm_recv, rdv);
313 SIMIX_rdv_push(rdv, comm);
316 /* Setup communication request */
317 comm->dst_host = SIMIX_host_self();
318 comm->dst_buff = data;
319 comm->dst_buff_size = data_size;
321 SIMIX_communication_start(comm);
323 /* Wait for communication completion */
324 /* FIXME: if the semantic is non blocking, it shouldn't wait on the condition here */
325 SIMIX_communication_wait_for_completion(comm, timeout);
327 SIMIX_communication_destroy(comm);
330 /******************************************************************************/
331 /* Asynchronous Communication */
332 /******************************************************************************/
335 void SIMIX_network_wait(smx_action_t comm, double timeout)
338 SIMIX_cond_wait_timeout(rdv_cond, rdv_comm_mutex, timeout - start_time);
340 SIMIX_cond_wait(rdv_cond, rdv_comm_mutex);
345 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)
347 if(SIMIX_action_get_state (comm) == SURF_ACTION_DONE){
350 return SIMIX_action_get_state (comm) == SURF_ACTION_DONE ? TRUE : FALSE;