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) {
77 smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
78 xbt_fifo_get_first_item(rdv->comm_fifo));
80 if(comm && comm->type == type){
81 DEBUG0("Communication request found!");
82 xbt_fifo_shift(rdv->comm_fifo);
83 SIMIX_communication_use(comm);
88 DEBUG0("Communication request not found");
93 * \brief counts the number of communication requests of a given host pending
94 * on a rendez-vous point
95 * \param rdv The rendez-vous point
96 * \param host The host to be counted
97 * \return The number of comm request pending in the rdv
100 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
102 smx_comm_t comm = NULL;
103 xbt_fifo_item_t item = NULL;
106 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
107 if (comm->src_proc->smx_host == host)
115 * \brief returns the communication at the head of the rendez-vous
116 * \param rdv The rendez-vous point
117 * \return The communication or NULL if empty
119 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
121 return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
124 /** @brief adds some API-related data to the rendez-vous point */
125 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
128 /** @brief gets API-related data from the rendez-vous point */
129 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
133 /******************************************************************************/
134 /* Communication Requests */
135 /******************************************************************************/
138 * \brief Creates a new communication request
139 * \param type The type of communication (comm_send, comm_recv)
140 * \return The new communication request
142 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
144 /* alloc structures */
145 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
147 comm->sem = SIMIX_sem_init(0);
154 * \brief Destroy a communication request
155 * \param comm The request to be destroyed
157 void SIMIX_communication_destroy(smx_comm_t comm)
160 if(comm->refcount > 0)
164 SIMIX_sem_destroy(comm->sem);
169 SIMIX_action_destroy(comm->act);
173 if(comm->src_timeout){
174 SIMIX_action_destroy(comm->src_timeout);
175 comm->src_timeout = NULL;
178 if(comm->dst_timeout){
179 SIMIX_action_destroy(comm->dst_timeout);
180 comm->dst_timeout = NULL;
187 * \brief Increase the number of users of the communication.
188 * \param comm The communication request
189 * Each communication request can be used by more than one process, so it is
190 * necessary to know number of them at destroy time, to avoid freeing stuff that
191 * maybe is in use by others.
194 static inline void SIMIX_communication_use(smx_comm_t comm)
200 * \brief Start the simulation of a communication request
201 * \param comm The comm->rdv = NULL;communication request
203 static inline void SIMIX_communication_start(smx_comm_t comm)
205 /* If both the sender and the receiver are already there, start the communication */
206 if(comm->src_proc && comm->dst_proc){
207 DEBUG1("Starting communication %p", comm);
208 comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
209 comm->dst_proc->smx_host, NULL,
210 comm->task_size, comm->rate);
212 /* If any of the process is suspend, create the action but stop its execution,
213 it will be restarted when the sender process resume */
214 if(SIMIX_process_is_suspended(comm->src_proc) ||
215 SIMIX_process_is_suspended(comm->dst_proc)) {
216 SIMIX_action_set_priority(comm->act, 0);
219 /* Add the communication as user data of the action */
220 comm->act->data = comm;
222 /* The semaphore will only get signaled once, but since the first unlocked guy will
223 * release_forever() the semaphore, that will unlock the second (and any other)
224 * communication partner */
225 SIMIX_register_action_to_semaphore(comm->act, comm->sem);
230 * \brief Waits for communication completion and performs error checking
231 * \param comm The communication
232 * \param timeout The max amount of time to wait for the communication to finish
235 * - host_error if peer failed
236 * - timeout_error if communication reached the timeout specified
237 * - network_error if network failed or peer issued a timeout
239 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
241 smx_action_t act_sleep = NULL;
245 DEBUG1("Waiting for the completion of communication %p", comm);
248 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
249 if(SIMIX_process_self()==comm->src_proc)
250 comm->src_timeout = act_sleep;
252 comm->dst_timeout = act_sleep;
253 SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
254 SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
255 SIMIX_process_self()->waiting_action = act_sleep;
256 SIMIX_sem_block_onto(comm->sem);
257 SIMIX_process_self()->waiting_action = NULL;
258 SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
260 SIMIX_sem_acquire(comm->sem);
263 /* Check for timeouts */
264 if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
265 (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
266 /* Somebody did a timeout! */
267 if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
268 if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
270 if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
271 SIMIX_communication_cancel(comm);
273 SIMIX_rdv_remove(comm->rdv, comm);
275 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
276 SIMIX_sem_release_forever(comm->sem);
277 SIMIX_communication_destroy(comm);
279 THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
282 DEBUG1("Communication %p complete! Let's check for errors", comm);
284 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
285 SIMIX_sem_release_forever(comm->sem);
287 /* Check for errors other than timeouts (they are catched above) */
288 if(!SIMIX_host_get_state(SIMIX_host_self())){
290 SIMIX_rdv_remove(comm->rdv, comm);
291 SIMIX_communication_destroy(comm);
292 THROW0(host_error, 0, "Host failed");
293 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
294 SIMIX_communication_destroy(comm);
295 THROW0(network_error, 0, "Link failure");
300 * \brief Cancels a communication
301 * \brief comm The communication to cancel
303 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
306 SIMIX_action_cancel(comm->act);
310 * \brief get the amount remaining from the communication
311 * \param comm The communication
313 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
315 return SIMIX_action_get_remains(comm->act);
318 /******************************************************************************/
319 /* SIMIX_network_copy_data callbacks */
320 /******************************************************************************/
321 static void (*SIMIX_network_copy_data_callback)(smx_comm_t, size_t) = &SIMIX_network_copy_pointer_callback;
323 void SIMIX_network_set_copy_data_callback(void (*callback)(smx_comm_t, size_t)) {
324 SIMIX_network_copy_data_callback = callback;
327 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size) {
328 xbt_assert1((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)",buff_size);
329 *(void**)(comm->dst_buff) = comm->src_buff;
332 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size) {
333 memcpy(comm->dst_buff, comm->src_buff, buff_size);
337 * \brief Copy the communication data from the sender's buffer to the receiver's one
338 * \param comm The communication
340 void SIMIX_network_copy_data(smx_comm_t comm)
342 /* If there is no data to be copy then return */
343 if(!comm->src_buff || !comm->dst_buff)
346 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
347 size_t buff_size = comm->src_buff_size;
348 if (comm->dst_buff_size)
349 buff_size = MIN(buff_size,*(comm->dst_buff_size));
351 /* Update the receiver's buffer size to the copied amount */
352 if (comm->dst_buff_size)
353 *comm->dst_buff_size = buff_size;
357 DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
359 comm->src_proc->smx_host->name, comm->src_buff,
360 comm->dst_proc->smx_host->name, comm->dst_buff,
362 (*SIMIX_network_copy_data_callback)(comm, buff_size);
366 * \brief Return the user data associated to the communication
367 * \param comm The communication
368 * \return the user data
370 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
375 /******************************************************************************/
376 /* Synchronous Communication */
377 /******************************************************************************/
379 * \brief Put a send communication request in a rendez-vous point and waits for
380 * its completion (blocking)
381 * \param rdv The rendez-vous point
382 * \param task_size The size of the communication action (for surf simulation)
383 * \param rate The rate of the communication action (for surf)
384 * \param timeout The timeout used for the waiting the completion
385 * \param src_buff The source buffer containing the message to be sent
386 * \param src_buff_size The size of the source buffer
387 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
388 * \param data User data associated to the communication object
390 * - host_error if peer failed
391 * - timeout_error if communication reached the timeout specified
392 * - network_error if network failed or peer issued a timeout
394 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
395 double timeout, void *src_buff, size_t src_buff_size,
396 smx_comm_t *comm_ref, void *data)
398 *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
399 SIMIX_network_wait(*comm_ref,timeout);
403 * \brief Put a receive communication request in a rendez-vous point and waits
404 * for its completion (blocking)
405 * \param rdv The rendez-vous point
406 * \param timeout The timeout used for the waiting the completion
407 * \param dst_buff The destination buffer to copy the received message
408 * \param src_buff_size The size of the destination buffer
409 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
411 * - host_error if peer failed
412 * - timeout_error if communication reached the timeout specified
413 * - network_error if network failed or peer issued a timeout
415 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
416 size_t *dst_buff_size, smx_comm_t *comm_ref)
418 *comm_ref = SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
419 SIMIX_network_wait(*comm_ref,timeout);
422 /******************************************************************************/
423 /* Asynchronous Communication */
424 /******************************************************************************/
425 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
426 void *src_buff, size_t src_buff_size, void *data)
430 /* Look for communication request matching our needs.
431 If it is not found then create it and push it into the rendez-vous point */
432 comm = SIMIX_rdv_get_request(rdv, comm_recv);
435 comm = SIMIX_communication_new(comm_send);
436 SIMIX_rdv_push(rdv, comm);
439 /* Setup the communication request */
440 comm->src_proc = SIMIX_process_self();
441 comm->task_size = task_size;
443 comm->src_buff = src_buff;
444 comm->src_buff_size = src_buff_size;
447 SIMIX_communication_start(comm);
451 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size) {
454 /* Look for communication request matching our needs.
455 If it is not found then create it and push it into the rendez-vous point */
456 comm = SIMIX_rdv_get_request(rdv, comm_send);
459 comm = SIMIX_communication_new(comm_recv);
460 SIMIX_rdv_push(rdv, comm);
463 /* Setup communication request */
464 comm->dst_proc = SIMIX_process_self();
465 comm->dst_buff = dst_buff;
466 comm->dst_buff_size = dst_buff_size;
468 SIMIX_communication_start(comm);
472 /** @brief blocks until the communication terminates or the timeout occurs */
473 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout) {
474 /* Wait for communication completion */
475 SIMIX_communication_wait_for_completion(comm, timeout);
477 SIMIX_communication_destroy(comm);
480 /** @Returns whether the (asynchronous) communication is done yet or not */
481 XBT_INLINE int SIMIX_network_test(smx_comm_t comm) {
482 return comm->sem?SIMIX_sem_would_block(comm->sem):0;
485 /** @brief wait for the completion of any communication of a set
487 * @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
489 unsigned int SIMIX_network_waitany(xbt_dynar_t comms) {
490 xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
491 unsigned int cursor, found_comm=-1;
492 smx_comm_t comm,comm_finished=NULL;
494 xbt_dynar_foreach(comms,cursor,comm){
495 xbt_dynar_push(sems,&(comm->sem));
498 DEBUG1("Waiting for the completion of communication set %p", comms);
500 found_comm = SIMIX_sem_acquire_any(sems);
501 xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
502 xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
504 DEBUG1("Communication %p complete! Let's check for errors", comm_finished);
506 /* Make sure that everyone sleeping on that semaphore is awake,
507 * and that nobody will ever block on it */
508 SIMIX_sem_release_forever(comm_finished->sem);
510 /* Check for errors */
511 if(!SIMIX_host_get_state(SIMIX_host_self())){
512 if(comm_finished->rdv)
513 SIMIX_rdv_remove(comm_finished->rdv, comm_finished);
514 SIMIX_communication_destroy(comm_finished);
515 THROW0(host_error, 0, "Host failed");
516 } else if (SIMIX_action_get_state(comm_finished->act) == SURF_ACTION_FAILED){
517 SIMIX_communication_destroy(comm_finished);
518 THROW0(network_error, 0, "Link failure");
520 SIMIX_communication_destroy(comm_finished);