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 xbt_dict_t latency_limited_dict = NULL;
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
17 "Logging specific to SIMIX (network)");
19 /******************************************************************************/
20 /* Rendez-Vous Points */
21 /******************************************************************************/
24 * \brief Creates a new rendez-vous point
25 * \param name The name of the rendez-vous point
26 * \return The created rendez-vous point
28 smx_rdv_t SIMIX_rdv_create(const char *name)
30 smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
31 rdv->name = name ? xbt_strdup(name) : NULL;
32 rdv->read = SIMIX_mutex_init();
33 rdv->write = SIMIX_mutex_init();
34 rdv->comm_fifo = xbt_fifo_new();
40 * \brief Destroy a rendez-vous point
41 * \param name The rendez-vous point to destroy
43 void SIMIX_rdv_destroy(smx_rdv_t rdv)
47 SIMIX_mutex_destroy(rdv->read);
48 SIMIX_mutex_destroy(rdv->write);
49 xbt_fifo_free(rdv->comm_fifo);
54 * \brief Push a communication request into a rendez-vous point
55 * \param rdv The rendez-vous point
56 * \param comm The communication request
58 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
60 xbt_fifo_push(rdv->comm_fifo, comm);
65 * \brief Remove a communication request from a rendez-vous point
66 * \param rdv The rendez-vous point
67 * \param comm The communication request
69 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
71 xbt_fifo_remove(rdv->comm_fifo, comm);
76 * \brief Checks if there is a communication request queued in a rendez-vous matching our needs
77 * \param type The type of communication we are looking for (comm_send, comm_recv)
78 * \return The communication request if found, NULL otherwise.
80 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type) {
81 smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
82 xbt_fifo_get_first_item(rdv->comm_fifo));
84 if(comm && comm->type == type){
85 DEBUG0("Communication request found!");
86 xbt_fifo_shift(rdv->comm_fifo);
87 SIMIX_communication_use(comm);
92 DEBUG0("Communication request not found");
97 * \brief counts the number of communication requests of a given host pending
98 * on a rendez-vous point
99 * \param rdv The rendez-vous point
100 * \param host The host to be counted
101 * \return The number of comm request pending in the rdv
104 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
106 smx_comm_t comm = NULL;
107 xbt_fifo_item_t item = NULL;
110 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
111 if (comm->src_proc->smx_host == host)
119 * \brief returns the communication at the head of the rendez-vous
120 * \param rdv The rendez-vous point
121 * \return The communication or NULL if empty
123 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
125 return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
128 /** @brief adds some API-related data to the rendez-vous point */
129 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
132 /** @brief gets API-related data from the rendez-vous point */
133 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
137 /******************************************************************************/
138 /* Communication Requests */
139 /******************************************************************************/
142 * \brief Creates a new communication request
143 * \param type The type of communication (comm_send, comm_recv)
144 * \return The new communication request
146 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
148 /* alloc structures */
149 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
151 comm->sem = SIMIX_sem_init(0);
158 * \brief Destroy a communication request
159 * \param comm The request to be destroyed
161 void SIMIX_communication_destroy(smx_comm_t comm)
163 VERB2("Destroy communication %p; refcount initially %d",comm,comm->refcount);
165 //save is latency limited flag to use afterwards
166 if (latency_limited_dict == NULL)
167 latency_limited_dict = xbt_dict_new();
168 DEBUG2("adding key %p with latency limited value %d to the dict", comm, SIMIX_action_is_latency_bounded(comm->act));
169 xbt_dicti_set(latency_limited_dict, (uintptr_t)comm, SIMIX_action_is_latency_bounded(comm->act));
172 if(comm->refcount > 0)
176 SIMIX_sem_destroy(comm->sem);
181 SIMIX_action_destroy(comm->act);
185 if(comm->src_timeout){
186 SIMIX_action_destroy(comm->src_timeout);
187 comm->src_timeout = NULL;
190 if(comm->dst_timeout){
191 SIMIX_action_destroy(comm->dst_timeout);
192 comm->dst_timeout = NULL;
201 * \brief Increase the number of users of the communication.
202 * \param comm The communication request
203 * Each communication request can be used by more than one process, so it is
204 * necessary to know number of them at destroy time, to avoid freeing stuff that
205 * maybe is in use by others.
208 static XBT_INLINE void SIMIX_communication_use(smx_comm_t comm)
214 * \brief Start the simulation of a communication request
215 * \param comm The communication request
217 static XBT_INLINE void SIMIX_communication_start(smx_comm_t comm)
219 /* If both the sender and the receiver are already there, start the communication */
220 if(comm->src_proc && comm->dst_proc){
221 DEBUG1("Starting communication %p", comm);
222 comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
223 comm->dst_proc->smx_host, NULL,
224 comm->task_size, comm->rate);
226 TRACE_smx_action_communicate (comm->act, comm->src_proc);
229 /* If any of the process is suspend, create the action but stop its execution,
230 it will be restarted when the sender process resume */
231 if(SIMIX_process_is_suspended(comm->src_proc) ||
232 SIMIX_process_is_suspended(comm->dst_proc)) {
233 SIMIX_action_suspend(comm->act);
236 /* Add the communication as user data of the action */
237 comm->act->data = comm;
239 /* The semaphore will only get signaled once, but since the first unlocked guy will
240 * release_forever() the semaphore, that will unlock the second (and any other)
241 * communication partner */
242 SIMIX_register_action_to_semaphore(comm->act, comm->sem);
247 * \brief Performs error checking and cleanup
248 * \param comm The communication
250 static XBT_INLINE void SIMIX_communication_cleanup(smx_comm_t comm)
252 DEBUG1("Checking errors and cleaning communication %p", comm);
254 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
255 SIMIX_sem_release_forever(comm->sem);
257 /* Check for errors other than timeouts */
258 if (!SIMIX_host_get_state(SIMIX_host_self())){
260 SIMIX_rdv_remove(comm->rdv, comm);
261 SIMIX_communication_destroy(comm);
262 THROW0(host_error, 0, "Host failed");
263 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
264 SIMIX_communication_destroy(comm);
265 THROW0(network_error, 0, "Link failure");
266 } else if (!SIMIX_host_get_state(SIMIX_process_get_host(comm->dst_proc)) ||
267 !SIMIX_host_get_state(SIMIX_process_get_host(comm->src_proc))) {
268 /* We test both src&dst because we dunno who we are today, and we already tested myself above.
269 * So, at the end, we test the remote peer only
270 * Moreover, we have to test it because if the remote peer fails, the action comm->act is not done nor failed.
271 * In that case, we got awaken by the little endless actions created in the SIMIX_sem_acquire(comm->sem)
272 * at the beginning of this function. */
273 SIMIX_communication_destroy(comm);
274 THROW0(network_error, 0, "Remote peer failed");
277 /* Copy network data */
278 SIMIX_network_copy_data(comm);
280 SIMIX_communication_destroy(comm);
284 * \brief Waits for communication completion
285 * \param comm The communication
286 * \param timeout The max amount of time to wait for the communication to finish
289 * - host_error if local peer failed
290 * - timeout_error if communication reached the timeout specified (either because of local peer or remote peer)
291 * - network_error if network failed or remote peer failed
293 static XBT_INLINE void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
295 smx_action_t act_sleep = NULL;
299 DEBUG1("Waiting for the completion of communication %p", comm);
302 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
303 if(SIMIX_process_self()==comm->src_proc)
304 comm->src_timeout = act_sleep;
306 comm->dst_timeout = act_sleep;
307 SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
308 SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
309 SIMIX_process_self()->waiting_action = act_sleep;
310 SIMIX_sem_block_onto(comm->sem);
311 SIMIX_process_self()->waiting_action = NULL;
312 SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
314 SIMIX_sem_acquire(comm->sem);
317 /* Check for timeouts */
318 if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
319 (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
320 /* Somebody did a timeout! */
321 if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
322 if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
324 if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
325 SIMIX_communication_cancel(comm);
327 SIMIX_rdv_remove(comm->rdv, comm);
329 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
330 SIMIX_sem_release_forever(comm->sem);
331 SIMIX_communication_destroy(comm);
333 THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
336 DEBUG1("Communication %p complete!", comm);
337 SIMIX_communication_cleanup(comm);
341 * \brief Cancels a communication
342 * \brief comm The communication to cancel
344 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
347 SIMIX_action_cancel(comm->act);
351 * \brief get the amount remaining from the communication
352 * \param comm The communication
354 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
356 DEBUG1("calling SIMIX_action_get_remains(%p)", comm->act);
357 return SIMIX_action_get_remains(comm->act);
361 * \brief verify if communication is latency bounded
362 * \param comm The communication
364 XBT_INLINE int SIMIX_communication_is_latency_bounded(smx_comm_t comm)
366 //try to find comm on the list of finished flows
369 xbt_dict_cursor_t cursor;
370 xbt_dict_foreach(latency_limited_dict,cursor,key,data) {
371 DEBUG2("comparing key=%p with comm=%p", (void*)key, (void*)comm);
372 if((void*)comm == (void*)key){
373 DEBUG2("key %p found, return value latency limited value %d", (void*)key, (int)data);
378 DEBUG1("calling SIMIX_action_is_latency_bounded(%p)", comm->act);
379 return SIMIX_action_is_latency_bounded(comm->act);
382 /******************************************************************************/
383 /* SIMIX_network_copy_data callbacks */
384 /******************************************************************************/
385 static void (*SIMIX_network_copy_data_callback)(smx_comm_t, size_t) = &SIMIX_network_copy_pointer_callback;
387 void SIMIX_network_set_copy_data_callback(void (*callback)(smx_comm_t, size_t)) {
388 SIMIX_network_copy_data_callback = callback;
391 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size) {
392 xbt_assert1((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)",buff_size);
393 *(void**)(comm->dst_buff) = comm->src_buff;
396 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size) {
397 memcpy(comm->dst_buff, comm->src_buff, buff_size);
401 * \brief Copy the communication data from the sender's buffer to the receiver's one
402 * \param comm The communication
404 void SIMIX_network_copy_data(smx_comm_t comm)
406 size_t buff_size = comm->src_buff_size;
407 uintptr_t casted_size = 0;
408 uintptr_t amount = 0;
409 /* If there is no data to be copy then return */
410 if(!comm->src_buff || !comm->dst_buff || comm->copied == 1)
413 DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
415 comm->src_proc->smx_host->name, comm->src_buff,
416 comm->dst_proc->smx_host->name, comm->dst_buff,
419 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
420 if (comm->dst_buff_size)
421 buff_size = MIN(buff_size,*(comm->dst_buff_size));
423 /* Update the receiver's buffer size to the copied amount */
424 if (comm->dst_buff_size)
425 *comm->dst_buff_size = buff_size;
429 (*SIMIX_network_copy_data_callback)(comm, buff_size);
431 /* Set the copied flag so we copy data only once */
432 /* (this function might be called from both communication ends)*/
435 /* pimple to display the message sizes */
437 if (msg_sizes == NULL)
438 msg_sizes = xbt_dict_new();
439 casted_size = comm->task_size;
440 amount = xbt_dicti_get(msg_sizes, casted_size);
443 xbt_dicti_set(msg_sizes,casted_size, amount);
447 /* pimple to display the message sizes */
448 void SIMIX_message_sizes_output(const char *filename) {
451 xbt_dict_cursor_t cursor;
453 out = fopen(filename,"w");
454 xbt_assert1(out,"Cannot open file %s",filename);
456 xbt_dict_foreach(msg_sizes,cursor,key,data) {
457 fprintf(out,"%zu %zu\n",key,data);
463 * \brief Return the user data associated to the communication
464 * \param comm The communication
465 * \return the user data
467 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
472 XBT_PUBLIC(void *) SIMIX_communication_get_src_buf(smx_comm_t comm)
474 return comm->src_buff;
476 XBT_PUBLIC(void *) SIMIX_communication_get_dst_buf(smx_comm_t comm)
478 return comm->dst_buff;
480 XBT_PUBLIC(size_t) SIMIX_communication_get_src_buf_size(smx_comm_t comm)
482 return comm->src_buff_size;
484 XBT_PUBLIC(size_t) SIMIX_communication_get_dst_buf_size(smx_comm_t comm)
486 return *(comm->dst_buff_size);
489 /******************************************************************************/
490 /* Synchronous Communication */
491 /******************************************************************************/
493 * \brief Put a send communication request in a rendez-vous point and waits for
494 * its completion (blocking)
495 * \param rdv The rendez-vous point
496 * \param task_size The size of the communication action (for surf simulation)
497 * \param rate The rate of the communication action (for surf)
498 * \param timeout The timeout used for the waiting the completion
499 * \param src_buff The source buffer containing the message to be sent
500 * \param src_buff_size The size of the source buffer
501 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
502 * \param data User data associated to the communication object
504 * - host_error if peer failed
505 * - timeout_error if communication reached the timeout specified
506 * - network_error if network failed or peer issued a timeout
508 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
509 double timeout, void *src_buff, size_t src_buff_size,
510 smx_comm_t *comm_ref, void *data)
512 *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
513 SIMIX_network_wait(*comm_ref,timeout);
517 * \brief Put a receive communication request in a rendez-vous point and waits
518 * for its completion (blocking)
519 * \param rdv The rendez-vous point
520 * \param timeout The timeout used for the waiting the completion
521 * \param dst_buff The destination buffer to copy the received message
522 * \param src_buff_size The size of the destination buffer
523 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
525 * - host_error if peer failed
526 * - timeout_error if communication reached the timeout specified
527 * - network_error if network failed or peer issued a timeout
529 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
530 size_t *dst_buff_size, smx_comm_t *comm_ref)
532 *comm_ref = (smx_comm_t) SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
533 SIMIX_network_wait(*comm_ref,timeout);
536 /******************************************************************************/
537 /* Asynchronous Communication */
538 /******************************************************************************/
539 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
540 void *src_buff, size_t src_buff_size, void *data)
544 /*If running in model-checking mode then intercept the communication action*/
546 if (_surf_do_model_check)
547 MC_trans_intercept_isend(rdv);
549 /* Look for communication request matching our needs.
550 If it is not found then create it and push it into the rendez-vous point */
551 comm = SIMIX_rdv_get_request(rdv, comm_recv);
554 comm = SIMIX_communication_new(comm_send);
555 SIMIX_rdv_push(rdv, comm);
558 /* Setup the communication request */
559 comm->src_proc = SIMIX_process_self();
560 comm->task_size = task_size;
562 comm->src_buff = src_buff;
563 comm->src_buff_size = src_buff_size;
566 SIMIX_communication_start(comm);
570 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size)
574 /*If running in model-checking mode then intercept the communication action*/
576 if (_surf_do_model_check)
577 MC_trans_intercept_irecv(rdv);
579 /* Look for communication request matching our needs.
580 * If it is not found then create it and push it into the rendez-vous point
582 comm = SIMIX_rdv_get_request(rdv, comm_send);
585 comm = SIMIX_communication_new(comm_recv);
586 SIMIX_rdv_push(rdv, comm);
589 /* Setup communication request */
590 comm->dst_proc = SIMIX_process_self();
591 comm->dst_buff = dst_buff;
592 comm->dst_buff_size = dst_buff_size;
594 SIMIX_communication_start(comm);
598 /** @brief blocks until the communication terminates or the timeout occurs */
599 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout)
601 /*If running in model-checking mode then intercept the communication action*/
603 if (_surf_do_model_check)
604 MC_trans_intercept_wait(comm);
606 /* Wait for communication completion */
607 SIMIX_communication_wait_for_completion(comm, timeout);
610 /** @Returns whether the (asynchronous) communication is done yet or not */
611 XBT_INLINE int SIMIX_network_test(smx_comm_t comm)
613 /*If running in model-checking mode then intercept the communication action*/
615 if (_surf_do_model_check)
616 MC_trans_intercept_test(comm);
619 /* Copy data if the communication is done */
620 if(comm->sem && !SIMIX_sem_would_block(comm->sem)){
621 /* Copy network data */
622 SIMIX_network_copy_data(comm);
628 /** @brief wait for the completion of any communication of a set
630 * @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
632 unsigned int SIMIX_network_waitany(xbt_dynar_t comms)
634 xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
635 unsigned int cursor, found_comm=-1;
636 smx_comm_t comm,comm_finished=NULL;
638 /*If running in model-checking mode then intercept the communication action*/
640 if (_surf_do_model_check)
641 MC_trans_intercept_waitany(comms);
643 xbt_dynar_foreach(comms,cursor,comm)
644 xbt_dynar_push(sems,&(comm->sem));
646 DEBUG1("Waiting for the completion of communication set %p", comms);
648 found_comm = SIMIX_sem_acquire_any(sems);
649 xbt_dynar_free_container(&sems);
650 xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
651 xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
653 DEBUG2("Communication %p of communication set %p finished", comm_finished, comms);
655 /* let the regular code deal with the communication end (errors checking and cleanup).
656 * A bit of useless work will be done, but that's good for source factorization */
657 SIMIX_sem_release_forever(comm_finished->sem);
658 SIMIX_communication_wait_for_completion(comm_finished, -1);