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);
61 * \brief Remove a communication request from a rendez-vous point
62 * \param rdv The rendez-vous point
63 * \param comm The communication request
65 static inline void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
67 xbt_fifo_remove(rdv->comm_fifo, comm);
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.
76 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type)
78 smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
79 xbt_fifo_get_first_item(rdv->comm_fifo));
81 if(comm && comm->type == type){
82 DEBUG0("Communication request found!");
83 xbt_fifo_shift(rdv->comm_fifo);
84 SIMIX_communication_use(comm);
88 /* no relevant request found. Return NULL */
89 DEBUG0("Communication request not found");
93 /******************************************************************************/
94 /* Communication Requests */
95 /******************************************************************************/
98 * \brief Creates a new communication request
99 * \param type The type of communication (comm_send, comm_recv)
100 * \return The new communication request
102 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
104 /* alloc structures */
105 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
107 comm->cond = SIMIX_cond_init();
114 * \brief Destroy a communication request
115 * \param comm The request to be destroyed
117 void SIMIX_communication_destroy(smx_comm_t comm)
120 if(comm->refcount == 0){
121 if(comm->act != NULL)
122 SIMIX_action_destroy(comm->act);
124 xbt_free(comm->cond);
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.
137 static inline void SIMIX_communication_use(smx_comm_t comm)
143 * \brief Start the simulation of a communication request
144 * \param comm The communication request
146 static inline void SIMIX_communication_start(smx_comm_t comm)
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);
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);
162 /* Add the communication as user data of the action */
163 comm->act->data = comm;
165 SIMIX_register_action_to_condition(comm->act, comm->cond);
167 DEBUG1("Communication %p cannot be started, peer missing", comm);
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
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
181 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
185 DEBUG1("Waiting for the completion of communication %p", comm);
189 SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
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);
198 SIMIX_rdv_remove(comm->rdv, comm);
200 SIMIX_cond_signal(comm->cond);
201 SIMIX_communication_destroy(comm);
206 SIMIX_cond_wait(comm->cond, NULL);
209 DEBUG1("Communication %p complete! Let's check for errors", comm);
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");
220 SIMIX_unregister_action_to_condition(comm->act, comm->cond);
224 * \brief Copy the communication data from the sender's buffer to the receiver's one
225 * \param comm The communication
227 void SIMIX_network_copy_data(smx_comm_t comm)
229 size_t src_buff_size = comm->src_buff_size;
230 size_t dst_buff_size = *comm->dst_buff_size;
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);
235 /* Update the receiver's buffer size to the copied amount */
236 *comm->dst_buff_size = dst_buff_size;
238 memcpy(comm->dst_buff, comm->src_buff, dst_buff_size);
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,
245 /******************************************************************************/
246 /* Synchronous Communication */
247 /******************************************************************************/
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
253 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
254 double timeout, void *data, size_t data_size)
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);
263 comm = SIMIX_communication_new(comm_send);
264 SIMIX_rdv_push(rdv, comm);
267 /* Setup the communication request */
268 comm->src_proc = SIMIX_process_self();
269 comm->task_size = task_size;
271 comm->src_buff = data;
272 comm->src_buff_size = data_size;
274 SIMIX_communication_start(comm);
276 /* Wait for communication completion */
277 SIMIX_communication_wait_for_completion(comm, timeout);
279 SIMIX_communication_destroy(comm);
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
287 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *data, size_t *data_size)
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);
296 comm = SIMIX_communication_new(comm_recv);
297 SIMIX_rdv_push(rdv, comm);
300 /* Setup communication request */
301 comm->dst_proc = SIMIX_process_self();
302 comm->dst_buff = data;
303 comm->dst_buff_size = data_size;
305 SIMIX_communication_start(comm);
307 /* Wait for communication completion */
308 SIMIX_communication_wait_for_completion(comm, timeout);
310 SIMIX_communication_destroy(comm);
313 /******************************************************************************/
314 /* Asynchronous Communication */
315 /******************************************************************************/
318 void SIMIX_network_wait(smx_action_t comm, double timeout)
323 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)