1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
12 /* Pimple to get an histogram of message sizes in the simulation */
13 xbt_dict_t msg_sizes = NULL;
14 #ifdef HAVE_LATENCY_BOUND_TRACKING
15 xbt_dict_t latency_limited_dict = NULL;
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
19 "Logging specific to SIMIX (network)");
21 /******************************************************************************/
22 /* Rendez-Vous Points */
23 /******************************************************************************/
26 * \brief Creates a new rendez-vous point
27 * \param name The name of the rendez-vous point
28 * \return The created rendez-vous point
30 smx_rdv_t SIMIX_rdv_create(const char *name)
32 smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
33 rdv->name = name ? xbt_strdup(name) : NULL;
34 rdv->read = SIMIX_mutex_init();
35 rdv->write = SIMIX_mutex_init();
36 rdv->comm_fifo = xbt_fifo_new();
42 * \brief Destroy a rendez-vous point
43 * \param name The rendez-vous point to destroy
45 void SIMIX_rdv_destroy(smx_rdv_t rdv)
49 SIMIX_mutex_destroy(rdv->read);
50 SIMIX_mutex_destroy(rdv->write);
51 xbt_fifo_free(rdv->comm_fifo);
56 * \brief Push a communication request into a rendez-vous point
57 * \param rdv The rendez-vous point
58 * \param comm The communication request
60 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
62 xbt_fifo_push(rdv->comm_fifo, comm);
67 * \brief Remove a communication request from a rendez-vous point
68 * \param rdv The rendez-vous point
69 * \param comm The communication request
71 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
73 xbt_fifo_remove(rdv->comm_fifo, comm);
78 * \brief Checks if there is a communication request queued in a rendez-vous matching our needs
79 * \param type The type of communication we are looking for (comm_send, comm_recv)
80 * \return The communication request if found, NULL otherwise.
82 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type)
84 smx_comm_t comm = (smx_comm_t)
85 xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
87 if (comm && comm->type == type) {
88 DEBUG0("Communication request found!");
89 xbt_fifo_shift(rdv->comm_fifo);
90 SIMIX_communication_use(comm);
95 DEBUG0("Communication request not found");
100 * \brief counts the number of communication requests of a given host pending
101 * on a rendez-vous point
102 * \param rdv The rendez-vous point
103 * \param host The host to be counted
104 * \return The number of comm request pending in the rdv
106 int SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
108 smx_comm_t comm = NULL;
109 xbt_fifo_item_t item = NULL;
112 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
113 if (comm->src_proc->smx_host == host)
121 * \brief returns the communication at the head of the rendez-vous
122 * \param rdv The rendez-vous point
123 * \return The communication or NULL if empty
125 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
128 xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
131 /** @brief adds some API-related data to the rendez-vous point */
132 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv, void *data)
137 /** @brief gets API-related data from the rendez-vous point */
138 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv)
143 /******************************************************************************/
144 /* Communication Requests */
145 /******************************************************************************/
148 * \brief Creates a new communication request
149 * \param type The type of communication (comm_send, comm_recv)
150 * \return The new communication request
152 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
154 /* alloc structures */
155 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
157 comm->sem = SIMIX_sem_init(0);
164 * \brief Destroy a communication request
165 * \param comm The request to be destroyed
167 void SIMIX_communication_destroy(smx_comm_t comm)
169 VERB2("Destroy communication %p; refcount initially %d", comm,
172 #ifdef HAVE_LATENCY_BOUND_TRACKING
173 //save is latency limited flag to use afterwards
174 if (latency_limited_dict == NULL) {
175 latency_limited_dict = xbt_dict_new();
178 DEBUG2("adding key %p with latency limited value %d to the dict", comm,
179 SIMIX_action_is_latency_bounded(comm->act));
180 xbt_dicti_set(latency_limited_dict, (uintptr_t) comm,
181 SIMIX_action_is_latency_bounded(comm->act));
186 if (comm->refcount > 0)
188 VERB2("Really free communication %p; refcount is now %d", comm,
192 SIMIX_sem_destroy(comm->sem);
197 SIMIX_action_destroy(comm->act);
201 if (comm->src_timeout) {
202 SIMIX_action_destroy(comm->src_timeout);
203 comm->src_timeout = NULL;
206 if (comm->dst_timeout) {
207 SIMIX_action_destroy(comm->dst_timeout);
208 comm->dst_timeout = NULL;
217 * \brief Increase the number of users of the communication.
218 * \param comm The communication request
219 * Each communication request can be used by more than one process, so it is
220 * necessary to know number of them at destroy time, to avoid freeing stuff that
221 * maybe is in use by others.
224 static XBT_INLINE void SIMIX_communication_use(smx_comm_t comm)
230 * \brief Start the simulation of a communication request
231 * \param comm The communication request
233 static XBT_INLINE void SIMIX_communication_start(smx_comm_t comm)
235 /* If both the sender and the receiver are already there, start the communication */
236 if (comm->src_proc && comm->dst_proc) {
237 DEBUG1("Starting communication %p", comm);
238 comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
239 comm->dst_proc->smx_host, NULL,
240 comm->task_size, comm->rate);
242 TRACE_smx_action_communicate(comm->act, comm->src_proc);
243 TRACE_surf_action(comm->act->surf_action, comm->act->category);
246 /* If any of the process is suspend, create the action but stop its execution,
247 it will be restarted when the sender process resume */
248 if (SIMIX_process_is_suspended(comm->src_proc) ||
249 SIMIX_process_is_suspended(comm->dst_proc)) {
250 SIMIX_action_suspend(comm->act);
253 /* Add the communication as user data of the action */
254 comm->act->data = comm;
256 /* The semaphore will only get signaled once, but since the first unlocked guy will
257 * release_forever() the semaphore, that will unlock the second (and any other)
258 * communication partner */
259 SIMIX_register_action_to_semaphore(comm->act, comm->sem);
264 * \brief Performs error checking and cleanup
265 * \param comm The communication
267 static XBT_INLINE void SIMIX_communication_cleanup(smx_comm_t comm)
269 DEBUG1("Checking errors and cleaning communication %p", comm);
271 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
272 SIMIX_sem_release_forever(comm->sem);
274 /* Check for errors other than timeouts */
275 if (!SIMIX_host_get_state(SIMIX_host_self())) {
277 SIMIX_rdv_remove(comm->rdv, comm);
278 SIMIX_communication_destroy(comm);
279 THROW0(host_error, 0, "Host failed");
280 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED) {
281 SIMIX_communication_destroy(comm);
282 THROW0(network_error, 0, "Link failure");
283 } else if (!SIMIX_host_get_state(SIMIX_process_get_host(comm->dst_proc))
285 !SIMIX_host_get_state(SIMIX_process_get_host(comm->src_proc)))
287 /* We test both src&dst because we dunno who we are today, and we already tested myself above.
288 * So, at the end, we test the remote peer only
289 * Moreover, we have to test it because if the remote peer fails, the action comm->act is not done nor failed.
290 * In that case, we got awaken by the little endless actions created in the SIMIX_sem_acquire(comm->sem)
291 * at the beginning of this function. */
292 SIMIX_communication_destroy(comm);
293 THROW0(network_error, 0, "Remote peer failed");
296 /* Copy network data */
297 SIMIX_network_copy_data(comm);
299 SIMIX_communication_destroy(comm);
303 * \brief Waits for communication completion
304 * \param comm The communication
305 * \param timeout The max amount of time to wait for the communication to finish
308 * - host_error if local peer failed
309 * - timeout_error if communication reached the timeout specified (either because of local peer or remote peer)
310 * - network_error if network failed or remote peer failed
312 static XBT_INLINE void SIMIX_communication_wait_for_completion(smx_comm_t
317 smx_action_t act_sleep = NULL;
321 DEBUG1("Waiting for the completion of communication %p", comm);
324 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
325 if (SIMIX_process_self() == comm->src_proc)
326 comm->src_timeout = act_sleep;
328 comm->dst_timeout = act_sleep;
329 SIMIX_action_set_name(act_sleep,
331 ("Timeout for comm %p and wait on semaphore %p (max_duration:%f)",
332 comm, comm->sem, timeout));
333 SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
334 SIMIX_process_self()->waiting_action = act_sleep;
335 SIMIX_sem_block_onto(comm->sem);
336 SIMIX_process_self()->waiting_action = NULL;
337 SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
339 SIMIX_sem_acquire(comm->sem);
342 /* Check for timeouts */
343 if ((src_timeout = ((comm->src_timeout)
344 && (SIMIX_action_get_state(comm->src_timeout) ==
346 || (dst_timeout = ((comm->dst_timeout)
347 && (SIMIX_action_get_state(comm->dst_timeout) ==
348 SURF_ACTION_DONE)))) {
349 /* Somebody did a timeout! */
351 DEBUG1("Communication timeout from the src! %p", comm);
353 DEBUG1("Communication timeout from the dst! %p", comm);
356 && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
357 SIMIX_communication_cancel(comm);
359 SIMIX_rdv_remove(comm->rdv, comm);
361 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
362 SIMIX_sem_release_forever(comm->sem);
363 SIMIX_communication_destroy(comm);
365 THROW1(timeout_error, 0, "Communication timeouted because of %s",
366 src_timeout ? "the source" : "the destination");
369 DEBUG1("Communication %p complete!", comm);
370 SIMIX_communication_cleanup(comm);
374 * \brief Cancels a communication
375 * \brief comm The communication to cancel
377 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
380 SIMIX_action_cancel(comm->act);
384 * \brief get the amount remaining from the communication
385 * \param comm The communication
387 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
389 DEBUG1("calling SIMIX_action_get_remains(%p)", comm->act);
390 return SIMIX_action_get_remains(comm->act);
393 #ifdef HAVE_LATENCY_BOUND_TRACKING
395 * \brief verify if communication is latency bounded
396 * \param comm The communication
398 XBT_INLINE int SIMIX_communication_is_latency_bounded(smx_comm_t comm)
400 //try to find comm on the list of finished flows
403 xbt_dict_cursor_t cursor;
404 xbt_dict_foreach(latency_limited_dict, cursor, key, data) {
405 DEBUG2("comparing key=%p with comm=%p", (void *) key, (void *) comm);
406 if ((void *) comm == (void *) key) {
407 DEBUG2("key %p found, return value latency limited value %d",
408 (void *) key, (int) data);
413 DEBUG1("calling SIMIX_action_is_latency_bounded(%p)", comm->act);
414 return SIMIX_action_is_latency_bounded(comm->act);
418 /******************************************************************************/
419 /* SIMIX_network_copy_data callbacks */
420 /******************************************************************************/
421 static void (*SIMIX_network_copy_data_callback) (smx_comm_t, size_t) =
422 &SIMIX_network_copy_pointer_callback;
425 SIMIX_network_set_copy_data_callback(void (*callback) (smx_comm_t, size_t))
427 SIMIX_network_copy_data_callback = callback;
430 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size)
432 xbt_assert1((buff_size == sizeof(void *)),
433 "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
434 *(void **) (comm->dst_buff) = comm->src_buff;
437 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size)
439 memcpy(comm->dst_buff, comm->src_buff, buff_size);
443 * \brief Copy the communication data from the sender's buffer to the receiver's one
444 * \param comm The communication
446 void SIMIX_network_copy_data(smx_comm_t comm)
448 size_t buff_size = comm->src_buff_size;
449 uintptr_t casted_size = 0;
450 uintptr_t amount = 0;
451 /* If there is no data to be copy then return */
452 if (!comm->src_buff || !comm->dst_buff || comm->copied == 1)
455 DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
457 comm->src_proc->smx_host->name, comm->src_buff,
458 comm->dst_proc->smx_host->name, comm->dst_buff, buff_size);
460 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
461 if (comm->dst_buff_size)
462 buff_size = MIN(buff_size, *(comm->dst_buff_size));
464 /* Update the receiver's buffer size to the copied amount */
465 if (comm->dst_buff_size)
466 *comm->dst_buff_size = buff_size;
470 (*SIMIX_network_copy_data_callback) (comm, buff_size);
472 /* Set the copied flag so we copy data only once */
473 /* (this function might be called from both communication ends) */
476 /* pimple to display the message sizes */
478 if (msg_sizes == NULL)
479 msg_sizes = xbt_dict_new();
480 casted_size = comm->task_size;
481 amount = xbt_dicti_get(msg_sizes, casted_size);
484 xbt_dicti_set(msg_sizes, casted_size, amount);
489 /* pimple to display the message sizes */
490 void SIMIX_message_sizes_output(const char *filename)
494 xbt_dict_cursor_t cursor;
496 out = fopen(filename, "w");
497 xbt_assert1(out, "Cannot open file %s", filename);
499 xbt_dict_foreach(msg_sizes, cursor, key, data) {
500 fprintf(out, "%zu %zu\n", key, data);
506 * \brief Return the user data associated to the communication
507 * \param comm The communication
508 * \return the user data
510 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
515 XBT_PUBLIC(void *) SIMIX_communication_get_src_buf(smx_comm_t comm)
517 return comm->src_buff;
520 XBT_PUBLIC(void *) SIMIX_communication_get_dst_buf(smx_comm_t comm)
522 return comm->dst_buff;
525 XBT_PUBLIC(size_t) SIMIX_communication_get_src_buf_size(smx_comm_t comm)
527 return comm->src_buff_size;
530 XBT_PUBLIC(size_t) SIMIX_communication_get_dst_buf_size(smx_comm_t comm)
532 return *(comm->dst_buff_size);
535 /******************************************************************************/
536 /* Synchronous Communication */
537 /******************************************************************************/
539 * \brief Put a send communication request in a rendez-vous point and waits for
540 * its completion (blocking)
541 * \param rdv The rendez-vous point
542 * \param task_size The size of the communication action (for surf simulation)
543 * \param rate The rate of the communication action (for surf)
544 * \param timeout The timeout used for the waiting the completion
545 * \param src_buff The source buffer containing the message to be sent
546 * \param src_buff_size The size of the source buffer
547 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
548 * \param data User data associated to the communication object
550 * - host_error if peer failed
551 * - timeout_error if communication reached the timeout specified
552 * - network_error if network failed or peer issued a timeout
554 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size,
555 double rate, double timeout,
556 void *src_buff, size_t src_buff_size,
557 smx_comm_t * comm_ref, void *data)
561 SIMIX_network_isend(rdv, task_size, rate, src_buff, src_buff_size,
564 SIMIX_network_wait(*comm_ref, timeout);
567 SIMIX_communication_destroy(*comm_ref);
575 * \brief Put a receive communication request in a rendez-vous point and waits
576 * for its completion (blocking)
577 * \param rdv The rendez-vous point
578 * \param timeout The timeout used for the waiting the completion
579 * \param dst_buff The destination buffer to copy the received message
580 * \param src_buff_size The size of the destination buffer
581 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
583 * - host_error if peer failed
584 * - timeout_error if communication reached the timeout specified
585 * - network_error if network failed or peer issued a timeout
587 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout,
588 void *dst_buff, size_t * dst_buff_size,
589 smx_comm_t * comm_ref)
593 (smx_comm_t) SIMIX_network_irecv(rdv, dst_buff, dst_buff_size);
595 SIMIX_network_wait(*comm_ref, timeout);
598 SIMIX_communication_destroy(*comm_ref);
605 /******************************************************************************/
606 /* Asynchronous Communication */
607 /******************************************************************************/
608 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size,
609 double rate, void *src_buff,
610 size_t src_buff_size, void *data)
614 /*If running in model-checking mode then intercept the communication action */
616 if (_surf_do_model_check)
617 MC_trans_intercept_isend(rdv);
619 /* Look for communication request matching our needs.
620 If it is not found then create it and push it into the rendez-vous point */
621 comm = SIMIX_rdv_get_request(rdv, comm_recv);
624 comm = SIMIX_communication_new(comm_send);
625 SIMIX_rdv_push(rdv, comm);
628 /* Setup the communication request */
629 comm->src_proc = SIMIX_process_self();
630 comm->task_size = task_size;
632 comm->src_buff = src_buff;
633 comm->src_buff_size = src_buff_size;
636 SIMIX_communication_start(comm);
640 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff,
641 size_t * dst_buff_size)
645 /*If running in model-checking mode then intercept the communication action */
647 if (_surf_do_model_check)
648 MC_trans_intercept_irecv(rdv);
650 /* Look for communication request matching our needs.
651 * If it is not found then create it and push it into the rendez-vous point
653 comm = SIMIX_rdv_get_request(rdv, comm_send);
656 comm = SIMIX_communication_new(comm_recv);
657 SIMIX_rdv_push(rdv, comm);
660 /* Setup communication request */
661 comm->dst_proc = SIMIX_process_self();
662 comm->dst_buff = dst_buff;
663 comm->dst_buff_size = dst_buff_size;
665 SIMIX_communication_start(comm);
669 /** @brief blocks until the communication terminates or the timeout occurs */
670 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout)
672 /*If running in model-checking mode then intercept the communication action */
674 if (_surf_do_model_check)
675 MC_trans_intercept_wait(comm);
677 SIMIX_communication_use(comm);
678 /* Wait for communication completion */
679 SIMIX_communication_wait_for_completion(comm, timeout);
682 /** @Returns whether the (asynchronous) communication is done yet or not */
683 XBT_INLINE int SIMIX_network_test(smx_comm_t comm)
685 /*If running in model-checking mode then intercept the communication action */
687 if (_surf_do_model_check)
688 MC_trans_intercept_test(comm);
691 /* Copy data if the communication is done */
692 if (comm->sem && !SIMIX_sem_would_block(comm->sem)) {
693 /* Copy network data */
694 SIMIX_network_copy_data(comm);
700 /** @brief wait for the completion of any communication of a set
702 * @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
704 unsigned int SIMIX_network_waitany(xbt_dynar_t comms)
706 xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t), NULL);
707 unsigned int cursor, found_comm = -1;
708 smx_comm_t comm, comm_finished = NULL;
710 /*If running in model-checking mode then intercept the communication action */
712 if (_surf_do_model_check)
713 MC_trans_intercept_waitany(comms);
715 xbt_dynar_foreach(comms, cursor, comm)
716 xbt_dynar_push(sems, &(comm->sem));
718 DEBUG1("Waiting for the completion of communication set %p", comms);
720 found_comm = SIMIX_sem_acquire_any(sems);
721 xbt_dynar_free_container(&sems);
722 xbt_assert0(found_comm != -1,
723 "Cannot find which communication finished");
724 xbt_dynar_get_cpy(comms, found_comm, &comm_finished);
726 DEBUG2("Communication %p of communication set %p finished",
727 comm_finished, comms);
729 /* let the regular code deal with the communication end (errors checking and cleanup).
730 * A bit of useless work will be done, but that's good for source factorization */
731 SIMIX_sem_release_forever(comm_finished->sem);
732 SIMIX_communication_use(comm_finished);
733 SIMIX_communication_wait_for_completion(comm_finished, -1);