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);
89 /* no relevant request found. Return NULL */
90 DEBUG0("Communication request not found");
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
102 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
104 smx_comm_t comm = NULL;
105 xbt_fifo_item_t item = NULL;
108 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
109 if (comm->src_proc->smx_host == host)
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
121 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
123 return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
126 /** @brief adds some API-related data to the rendez-vous point */
127 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
130 /** @brief gets API-related data from the rendez-vous point */
131 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
135 /******************************************************************************/
136 /* Communication Requests */
137 /******************************************************************************/
140 * \brief Creates a new communication request
141 * \param type The type of communication (comm_send, comm_recv)
142 * \return The new communication request
144 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
146 /* alloc structures */
147 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
149 comm->sem = SIMIX_sem_init(0);
156 * \brief Destroy a communication request
157 * \param comm The request to be destroyed
159 void SIMIX_communication_destroy(smx_comm_t comm)
162 if(comm->refcount > 0)
166 SIMIX_sem_destroy(comm->sem);
171 SIMIX_action_destroy(comm->act);
175 if(comm->src_timeout){
176 SIMIX_action_destroy(comm->src_timeout);
177 comm->src_timeout = NULL;
180 if(comm->dst_timeout){
181 SIMIX_action_destroy(comm->dst_timeout);
182 comm->dst_timeout = NULL;
189 * \brief Increase the number of users of the communication.
190 * \param comm The communication request
191 * Each communication request can be used by more than one process, so it is
192 * necessary to know number of them at destroy time, to avoid freeing stuff that
193 * maybe is in use by others.
196 static inline void SIMIX_communication_use(smx_comm_t comm)
202 * \brief Start the simulation of a communication request
203 * \param comm The comm->rdv = NULL;communication request
205 static inline void SIMIX_communication_start(smx_comm_t comm)
207 /* If both the sender and the receiver are already there, start the communication */
208 if(comm->src_proc && comm->dst_proc){
209 DEBUG1("Starting communication %p", comm);
210 comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
211 comm->dst_proc->smx_host, NULL,
212 comm->task_size, comm->rate);
214 /* If any of the process is suspend, create the action but stop its execution,
215 it will be restarted when the sender process resume */
216 if(SIMIX_process_is_suspended(comm->src_proc) ||
217 SIMIX_process_is_suspended(comm->dst_proc)) {
218 SIMIX_action_set_priority(comm->act, 0);
221 /* Add the communication as user data of the action */
222 comm->act->data = comm;
224 /* The semaphore will only get signaled once, but since the first unlocked guy will
225 * release_forever() the semaphore, that will unlock the second (and any other)
226 * communication partner */
227 SIMIX_register_action_to_semaphore(comm->act, comm->sem);
232 * \brief Waits for communication completion and performs error checking
233 * \param comm The communication
234 * \param timeout The max amount of time to wait for the communication to finish
237 * - host_error if peer failed
238 * - timeout_error if communication reached the timeout specified
239 * - network_error if network failed or peer issued a timeout
241 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
243 smx_action_t act_sleep = NULL;
247 DEBUG1("Waiting for the completion of communication %p", comm);
250 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
251 if(SIMIX_process_self()==comm->src_proc)
252 comm->src_timeout = act_sleep;
254 comm->dst_timeout = act_sleep;
255 SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
256 SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
257 SIMIX_process_self()->waiting_action = act_sleep;
258 SIMIX_sem_block_onto(comm->sem);
259 SIMIX_process_self()->waiting_action = NULL;
260 SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
262 SIMIX_sem_acquire(comm->sem);
265 /* Check for timeouts */
266 if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
267 (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
268 /* Somebody did a timeout! */
269 if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
270 if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
272 if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
273 SIMIX_communication_cancel(comm);
275 SIMIX_rdv_remove(comm->rdv, comm);
277 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
278 SIMIX_sem_release_forever(comm->sem);
279 SIMIX_communication_destroy(comm);
281 THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
284 DEBUG1("Communication %p complete! Let's check for errors", comm);
286 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
287 SIMIX_sem_release_forever(comm->sem);
289 /* Check for errors other than timeouts (they are catched above) */
290 if(!SIMIX_host_get_state(SIMIX_host_self())){
292 SIMIX_rdv_remove(comm->rdv, comm);
293 SIMIX_communication_destroy(comm);
294 THROW0(host_error, 0, "Host failed");
295 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
296 SIMIX_communication_destroy(comm);
297 THROW0(network_error, 0, "Link failure");
302 * \brief Cancels a communication
303 * \brief comm The communication to cancel
305 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
308 SIMIX_action_cancel(comm->act);
312 * \brief get the amount remaining from the communication
313 * \param comm The communication
315 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
317 return SIMIX_action_get_remains(comm->act);
321 * \brief Copy the communication data from the sender's buffer to the receiver's one
322 * \param comm The communication
324 void SIMIX_network_copy_data(smx_comm_t comm)
326 /* If there is no data to be copy then return */
327 if(!comm->src_buff || !comm->dst_buff)
330 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
331 size_t buff_size = comm->src_buff_size;
332 if (comm->dst_buff_size)
333 buff_size = MIN(buff_size,*(comm->dst_buff_size));
335 /* Update the receiver's buffer size to the copied amount */
336 if (comm->dst_buff_size)
337 *comm->dst_buff_size = buff_size;
342 #ifdef HAVE_RUBY /* FIXME: KILLME */
343 INFO6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
345 comm->src_proc->smx_host->name, comm->src_buff,
346 comm->dst_proc->smx_host->name, comm->dst_buff,
349 DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
351 comm->src_proc->smx_host->name, comm->src_buff,
352 comm->dst_proc->smx_host->name, comm->dst_buff,
356 memcpy(comm->dst_buff, comm->src_buff, buff_size);
360 * \brief Return the user data associated to the communication
361 * \param comm The communication
362 * \return the user data
364 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
369 /******************************************************************************/
370 /* Synchronous Communication */
371 /******************************************************************************/
373 * \brief Put a send communication request in a rendez-vous point and waits for
374 * its completion (blocking)
375 * \param rdv The rendez-vous point
376 * \param task_size The size of the communication action (for surf simulation)
377 * \param rate The rate of the communication action (for surf)
378 * \param timeout The timeout used for the waiting the completion
379 * \param src_buff The source buffer containing the message to be sent
380 * \param src_buff_size The size of the source buffer
381 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
382 * \param data User data associated to the communication object
384 * - host_error if peer failed
385 * - timeout_error if communication reached the timeout specified
386 * - network_error if network failed or peer issued a timeout
388 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
389 double timeout, void *src_buff, size_t src_buff_size,
390 smx_comm_t *comm_ref, void *data)
392 *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
393 SIMIX_network_wait(*comm_ref,timeout);
397 * \brief Put a receive communication request in a rendez-vous point and waits
398 * for its completion (blocking)
399 * \param rdv The rendez-vous point
400 * \param timeout The timeout used for the waiting the completion
401 * \param dst_buff The destination buffer to copy the received message
402 * \param src_buff_size The size of the destination buffer
403 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
405 * - host_error if peer failed
406 * - timeout_error if communication reached the timeout specified
407 * - network_error if network failed or peer issued a timeout
409 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
410 size_t *dst_buff_size, smx_comm_t *comm_ref)
412 *comm_ref = SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
413 SIMIX_network_wait(*comm_ref,timeout);
416 /******************************************************************************/
417 /* Asynchronous Communication */
418 /******************************************************************************/
419 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
420 void *src_buff, size_t src_buff_size, void *data)
424 /* Look for communication request matching our needs.
425 If it is not found then create it and push it into the rendez-vous point */
426 comm = SIMIX_rdv_get_request(rdv, comm_recv);
429 comm = SIMIX_communication_new(comm_send);
430 SIMIX_rdv_push(rdv, comm);
433 /* Setup the communication request */
434 comm->src_proc = SIMIX_process_self();
435 comm->task_size = task_size;
437 comm->src_buff = src_buff;
438 comm->src_buff_size = src_buff_size;
441 SIMIX_communication_start(comm);
445 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size) {
448 /* Look for communication request matching our needs.
449 If it is not found then create it and push it into the rendez-vous point */
450 comm = SIMIX_rdv_get_request(rdv, comm_send);
453 comm = SIMIX_communication_new(comm_recv);
454 SIMIX_rdv_push(rdv, comm);
457 /* Setup communication request */
458 comm->dst_proc = SIMIX_process_self();
459 comm->dst_buff = dst_buff;
460 comm->dst_buff_size = dst_buff_size;
462 SIMIX_communication_start(comm);
466 /** @brief blocks until the communication terminates or the timeout occurs */
467 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout) {
468 /* Wait for communication completion */
469 SIMIX_communication_wait_for_completion(comm, timeout);
471 SIMIX_communication_destroy(comm);
474 /** @Returns whether the (asynchronous) communication is done yet or not */
475 XBT_INLINE int SIMIX_network_test(smx_comm_t comm) {
476 return comm->sem?SIMIX_sem_would_block(comm->sem):0;
479 /** @brief wait for the completion of any communication of a set
481 * @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
483 unsigned int SIMIX_network_waitany(xbt_dynar_t comms) {
484 xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
485 unsigned int cursor, found_comm=-1;
486 smx_comm_t comm,comm_finished=NULL;
488 xbt_dynar_foreach(comms,cursor,comm){
489 xbt_dynar_push(sems,&(comm->sem));
492 DEBUG1("Waiting for the completion of communication set %p", comms);
494 found_comm = SIMIX_sem_acquire_any(sems);
495 xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
496 xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
498 DEBUG1("Communication %p complete! Let's check for errors", comm_finished);
500 /* Make sure that everyone sleeping on that semaphore is awake,
501 * and that nobody will ever block on it */
502 SIMIX_sem_release_forever(comm_finished->sem);
504 /* Check for errors */
505 if(!SIMIX_host_get_state(SIMIX_host_self())){
506 if(comm_finished->rdv)
507 SIMIX_rdv_remove(comm_finished->rdv, comm_finished);
508 SIMIX_communication_destroy(comm_finished);
509 THROW0(host_error, 0, "Host failed");
510 } else if (SIMIX_action_get_state(comm_finished->act) == SURF_ACTION_FAILED){
511 SIMIX_communication_destroy(comm_finished);
512 THROW0(network_error, 0, "Link failure");