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;
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
16 "Logging specific to SIMIX (network)");
18 /******************************************************************************/
19 /* Rendez-Vous Points */
20 /******************************************************************************/
23 * \brief Creates a new rendez-vous point
24 * \param name The name of the rendez-vous point
25 * \return The created rendez-vous point
27 smx_rdv_t SIMIX_rdv_create(const char *name)
29 smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
30 rdv->name = name ? xbt_strdup(name) : NULL;
31 rdv->read = SIMIX_mutex_init();
32 rdv->write = SIMIX_mutex_init();
33 rdv->comm_fifo = xbt_fifo_new();
39 * \brief Destroy a rendez-vous point
40 * \param name The rendez-vous point to destroy
42 void SIMIX_rdv_destroy(smx_rdv_t rdv)
46 SIMIX_mutex_destroy(rdv->read);
47 SIMIX_mutex_destroy(rdv->write);
48 xbt_fifo_free(rdv->comm_fifo);
53 * \brief Push a communication request into a rendez-vous point
54 * \param rdv The rendez-vous point
55 * \param comm The communication request
57 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
59 xbt_fifo_push(rdv->comm_fifo, comm);
64 * \brief Remove a communication request from a rendez-vous point
65 * \param rdv The rendez-vous point
66 * \param comm The communication request
68 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
70 xbt_fifo_remove(rdv->comm_fifo, comm);
75 * \brief Checks if there is a communication request queued in a rendez-vous matching our needs
76 * \param type The type of communication we are looking for (comm_send, comm_recv)
77 * \return The communication request if found, NULL otherwise.
79 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type) {
80 smx_comm_t comm = (smx_comm_t)xbt_fifo_get_item_content(
81 xbt_fifo_get_first_item(rdv->comm_fifo));
83 if(comm && comm->type == type){
84 DEBUG0("Communication request found!");
85 xbt_fifo_shift(rdv->comm_fifo);
86 SIMIX_communication_use(comm);
91 DEBUG0("Communication request not found");
96 * \brief counts the number of communication requests of a given host pending
97 * on a rendez-vous point
98 * \param rdv The rendez-vous point
99 * \param host The host to be counted
100 * \return The number of comm request pending in the rdv
103 SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
105 smx_comm_t comm = NULL;
106 xbt_fifo_item_t item = NULL;
109 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
110 if (comm->src_proc->smx_host == host)
118 * \brief returns the communication at the head of the rendez-vous
119 * \param rdv The rendez-vous point
120 * \return The communication or NULL if empty
122 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
124 return (smx_comm_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
127 /** @brief adds some API-related data to the rendez-vous point */
128 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv,void *data) {
131 /** @brief gets API-related data from the rendez-vous point */
132 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv) {
136 /******************************************************************************/
137 /* Communication Requests */
138 /******************************************************************************/
141 * \brief Creates a new communication request
142 * \param type The type of communication (comm_send, comm_recv)
143 * \return The new communication request
145 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
147 /* alloc structures */
148 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
150 comm->sem = SIMIX_sem_init(0);
157 * \brief Destroy a communication request
158 * \param comm The request to be destroyed
160 void SIMIX_communication_destroy(smx_comm_t comm)
162 VERB2("Destroy communication %p; refcount initially %d",comm,comm->refcount);
164 if(comm->refcount > 0)
168 SIMIX_sem_destroy(comm->sem);
173 SIMIX_action_destroy(comm->act);
177 if(comm->src_timeout){
178 SIMIX_action_destroy(comm->src_timeout);
179 comm->src_timeout = NULL;
182 if(comm->dst_timeout){
183 SIMIX_action_destroy(comm->dst_timeout);
184 comm->dst_timeout = NULL;
191 * \brief Increase the number of users of the communication.
192 * \param comm The communication request
193 * Each communication request can be used by more than one process, so it is
194 * necessary to know number of them at destroy time, to avoid freeing stuff that
195 * maybe is in use by others.
198 static XBT_INLINE void SIMIX_communication_use(smx_comm_t comm)
204 * \brief Start the simulation of a communication request
205 * \param comm The communication request
207 static XBT_INLINE void SIMIX_communication_start(smx_comm_t comm)
209 /* If both the sender and the receiver are already there, start the communication */
210 if(comm->src_proc && comm->dst_proc){
211 DEBUG1("Starting communication %p", comm);
212 comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
213 comm->dst_proc->smx_host, NULL,
214 comm->task_size, comm->rate);
216 TRACE_smx_action_communicate (comm->act, comm->src_proc);
219 /* If any of the process is suspend, create the action but stop its execution,
220 it will be restarted when the sender process resume */
221 if(SIMIX_process_is_suspended(comm->src_proc) ||
222 SIMIX_process_is_suspended(comm->dst_proc)) {
223 SIMIX_action_suspend(comm->act);
226 /* Add the communication as user data of the action */
227 comm->act->data = comm;
229 /* The semaphore will only get signaled once, but since the first unlocked guy will
230 * release_forever() the semaphore, that will unlock the second (and any other)
231 * communication partner */
232 SIMIX_register_action_to_semaphore(comm->act, comm->sem);
237 * \brief Performs error checking and cleanup
238 * \param comm The communication
240 static XBT_INLINE void SIMIX_communication_cleanup(smx_comm_t comm)
242 DEBUG1("Checking errors and cleaning communication %p", comm);
244 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
245 SIMIX_sem_release_forever(comm->sem);
247 /* Check for errors other than timeouts */
248 if (!SIMIX_host_get_state(SIMIX_host_self())){
250 SIMIX_rdv_remove(comm->rdv, comm);
251 SIMIX_communication_destroy(comm);
252 THROW0(host_error, 0, "Host failed");
253 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
254 SIMIX_communication_destroy(comm);
255 THROW0(network_error, 0, "Link failure");
256 } else if (!SIMIX_host_get_state(SIMIX_process_get_host(comm->dst_proc)) ||
257 !SIMIX_host_get_state(SIMIX_process_get_host(comm->src_proc))) {
258 /* We test both src&dst because we dunno who we are today, and we already tested myself above.
259 * So, at the end, we test the remote peer only
260 * Moreover, we have to test it because if the remote peer fails, the action comm->act is not done nor failed.
261 * In that case, we got awaken by the little endless actions created in the SIMIX_sem_acquire(comm->sem)
262 * at the beginning of this function. */
263 SIMIX_communication_destroy(comm);
264 THROW0(network_error, 0, "Remote peer failed");
267 /* Copy network data */
268 SIMIX_network_copy_data(comm);
270 SIMIX_communication_destroy(comm);
274 * \brief Waits for communication completion
275 * \param comm The communication
276 * \param timeout The max amount of time to wait for the communication to finish
279 * - host_error if local peer failed
280 * - timeout_error if communication reached the timeout specified (either because of local peer or remote peer)
281 * - network_error if network failed or remote peer failed
283 static XBT_INLINE void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
285 smx_action_t act_sleep = NULL;
289 DEBUG1("Waiting for the completion of communication %p", comm);
292 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
293 if(SIMIX_process_self()==comm->src_proc)
294 comm->src_timeout = act_sleep;
296 comm->dst_timeout = act_sleep;
297 SIMIX_action_set_name(act_sleep,bprintf("Timeout for comm %p and wait on semaphore %p (max_duration:%f)", comm, comm->sem,timeout));
298 SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
299 SIMIX_process_self()->waiting_action = act_sleep;
300 SIMIX_sem_block_onto(comm->sem);
301 SIMIX_process_self()->waiting_action = NULL;
302 SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
304 SIMIX_sem_acquire(comm->sem);
307 /* Check for timeouts */
308 if ((src_timeout = ((comm->src_timeout) && (SIMIX_action_get_state(comm->src_timeout) == SURF_ACTION_DONE))) ||
309 (dst_timeout = ((comm->dst_timeout) && (SIMIX_action_get_state(comm->dst_timeout) == SURF_ACTION_DONE))) ) {
310 /* Somebody did a timeout! */
311 if (src_timeout) DEBUG1("Communication timeout from the src! %p", comm);
312 if (dst_timeout) DEBUG1("Communication timeout from the dst! %p", comm);
314 if(comm->act && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
315 SIMIX_communication_cancel(comm);
317 SIMIX_rdv_remove(comm->rdv, comm);
319 /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
320 SIMIX_sem_release_forever(comm->sem);
321 SIMIX_communication_destroy(comm);
323 THROW1(timeout_error, 0, "Communication timeouted because of %s",src_timeout?"the source":"the destination");
326 DEBUG1("Communication %p complete!", comm);
327 SIMIX_communication_cleanup(comm);
331 * \brief Cancels a communication
332 * \brief comm The communication to cancel
334 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
337 SIMIX_action_cancel(comm->act);
341 * \brief get the amount remaining from the communication
342 * \param comm The communication
344 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
346 return SIMIX_action_get_remains(comm->act);
349 /******************************************************************************/
350 /* SIMIX_network_copy_data callbacks */
351 /******************************************************************************/
352 static void (*SIMIX_network_copy_data_callback)(smx_comm_t, size_t) = &SIMIX_network_copy_pointer_callback;
354 void SIMIX_network_set_copy_data_callback(void (*callback)(smx_comm_t, size_t)) {
355 SIMIX_network_copy_data_callback = callback;
358 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size) {
359 xbt_assert1((buff_size == sizeof(void*)), "Cannot copy %zu bytes: must be sizeof(void*)",buff_size);
360 *(void**)(comm->dst_buff) = comm->src_buff;
363 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size) {
364 memcpy(comm->dst_buff, comm->src_buff, buff_size);
368 * \brief Copy the communication data from the sender's buffer to the receiver's one
369 * \param comm The communication
371 void SIMIX_network_copy_data(smx_comm_t comm)
373 size_t buff_size = comm->src_buff_size;
374 uintptr_t casted_size = 0;
375 uintptr_t amount = 0;
376 /* If there is no data to be copy then return */
377 if(!comm->src_buff || !comm->dst_buff || comm->copied == 1)
380 DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
382 comm->src_proc->smx_host->name, comm->src_buff,
383 comm->dst_proc->smx_host->name, comm->dst_buff,
386 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
387 if (comm->dst_buff_size)
388 buff_size = MIN(buff_size,*(comm->dst_buff_size));
390 /* Update the receiver's buffer size to the copied amount */
391 if (comm->dst_buff_size)
392 *comm->dst_buff_size = buff_size;
396 (*SIMIX_network_copy_data_callback)(comm, buff_size);
398 /* Set the copied flag so we copy data only once */
399 /* (this function might be called from both communication ends)*/
402 /* pimple to display the message sizes */
404 if (msg_sizes == NULL)
405 msg_sizes = xbt_dict_new();
406 casted_size = comm->task_size;
407 amount = xbt_dicti_get(msg_sizes, casted_size);
410 xbt_dicti_set(msg_sizes,casted_size, amount);
414 /* pimple to display the message sizes */
415 void SIMIX_message_sizes_output(const char *filename) {
418 xbt_dict_cursor_t cursor;
420 out = fopen(filename,"w");
421 xbt_assert1(out,"Cannot open file %s",filename);
423 xbt_dict_foreach(msg_sizes,cursor,key,data) {
424 fprintf(out,"%zu %zu\n",key,data);
430 * \brief Return the user data associated to the communication
431 * \param comm The communication
432 * \return the user data
434 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
439 XBT_PUBLIC(void *) SIMIX_communication_get_src_buf(smx_comm_t comm)
441 return comm->src_buff;
443 XBT_PUBLIC(void *) SIMIX_communication_get_dst_buf(smx_comm_t comm)
445 return comm->dst_buff;
447 XBT_PUBLIC(size_t) SIMIX_communication_get_src_buf_size(smx_comm_t comm)
449 return comm->src_buff_size;
451 XBT_PUBLIC(size_t) SIMIX_communication_get_dst_buf_size(smx_comm_t comm)
453 return *(comm->dst_buff_size);
456 /******************************************************************************/
457 /* Synchronous Communication */
458 /******************************************************************************/
460 * \brief Put a send communication request in a rendez-vous point and waits for
461 * its completion (blocking)
462 * \param rdv The rendez-vous point
463 * \param task_size The size of the communication action (for surf simulation)
464 * \param rate The rate of the communication action (for surf)
465 * \param timeout The timeout used for the waiting the completion
466 * \param src_buff The source buffer containing the message to be sent
467 * \param src_buff_size The size of the source buffer
468 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
469 * \param data User data associated to the communication object
471 * - host_error if peer failed
472 * - timeout_error if communication reached the timeout specified
473 * - network_error if network failed or peer issued a timeout
475 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate,
476 double timeout, void *src_buff, size_t src_buff_size,
477 smx_comm_t *comm_ref, void *data)
479 *comm_ref = SIMIX_network_isend(rdv,task_size,rate,src_buff,src_buff_size,data);
480 SIMIX_network_wait(*comm_ref,timeout);
484 * \brief Put a receive communication request in a rendez-vous point and waits
485 * for its completion (blocking)
486 * \param rdv The rendez-vous point
487 * \param timeout The timeout used for the waiting the completion
488 * \param dst_buff The destination buffer to copy the received message
489 * \param src_buff_size The size of the destination buffer
490 * \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
492 * - host_error if peer failed
493 * - timeout_error if communication reached the timeout specified
494 * - network_error if network failed or peer issued a timeout
496 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void *dst_buff,
497 size_t *dst_buff_size, smx_comm_t *comm_ref)
499 *comm_ref = (smx_comm_t) SIMIX_network_irecv(rdv,dst_buff,dst_buff_size);
500 SIMIX_network_wait(*comm_ref,timeout);
503 /******************************************************************************/
504 /* Asynchronous Communication */
505 /******************************************************************************/
506 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size, double rate,
507 void *src_buff, size_t src_buff_size, void *data)
511 /*If running in model-checking mode then intercept the communication action*/
513 if (_surf_do_model_check)
514 MC_trans_intercept_isend(rdv);
516 /* Look for communication request matching our needs.
517 If it is not found then create it and push it into the rendez-vous point */
518 comm = SIMIX_rdv_get_request(rdv, comm_recv);
521 comm = SIMIX_communication_new(comm_send);
522 SIMIX_rdv_push(rdv, comm);
525 /* Setup the communication request */
526 comm->src_proc = SIMIX_process_self();
527 comm->task_size = task_size;
529 comm->src_buff = src_buff;
530 comm->src_buff_size = src_buff_size;
533 SIMIX_communication_start(comm);
537 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff, size_t *dst_buff_size)
541 /*If running in model-checking mode then intercept the communication action*/
543 if (_surf_do_model_check)
544 MC_trans_intercept_irecv(rdv);
546 /* Look for communication request matching our needs.
547 * If it is not found then create it and push it into the rendez-vous point
549 comm = SIMIX_rdv_get_request(rdv, comm_send);
552 comm = SIMIX_communication_new(comm_recv);
553 SIMIX_rdv_push(rdv, comm);
556 /* Setup communication request */
557 comm->dst_proc = SIMIX_process_self();
558 comm->dst_buff = dst_buff;
559 comm->dst_buff_size = dst_buff_size;
561 SIMIX_communication_start(comm);
565 /** @brief blocks until the communication terminates or the timeout occurs */
566 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout)
568 /*If running in model-checking mode then intercept the communication action*/
570 if (_surf_do_model_check)
571 MC_trans_intercept_wait(comm);
573 /* Wait for communication completion */
574 SIMIX_communication_wait_for_completion(comm, timeout);
577 /** @Returns whether the (asynchronous) communication is done yet or not */
578 XBT_INLINE int SIMIX_network_test(smx_comm_t comm)
580 /*If running in model-checking mode then intercept the communication action*/
582 if (_surf_do_model_check)
583 MC_trans_intercept_test(comm);
586 /* Copy data if the communication is done */
587 if(comm->sem && !SIMIX_sem_would_block(comm->sem)){
588 /* Copy network data */
589 SIMIX_network_copy_data(comm);
595 /** @brief wait for the completion of any communication of a set
597 * @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
599 unsigned int SIMIX_network_waitany(xbt_dynar_t comms)
601 xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t),NULL);
602 unsigned int cursor, found_comm=-1;
603 smx_comm_t comm,comm_finished=NULL;
605 /*If running in model-checking mode then intercept the communication action*/
607 if (_surf_do_model_check)
608 MC_trans_intercept_waitany(comms);
610 xbt_dynar_foreach(comms,cursor,comm)
611 xbt_dynar_push(sems,&(comm->sem));
613 DEBUG1("Waiting for the completion of communication set %p", comms);
615 found_comm = SIMIX_sem_acquire_any(sems);
616 xbt_dynar_free_container(&sems);
617 xbt_assert0(found_comm!=-1,"Cannot find which communication finished");
618 xbt_dynar_get_cpy(comms,found_comm,&comm_finished);
620 DEBUG2("Communication %p of communication set %p finished", comm_finished, comms);
622 /* let the regular code deal with the communication end (errors checking and cleanup).
623 * A bit of useless work will be done, but that's good for source factorization */
624 SIMIX_sem_release_forever(comm_finished->sem);
625 SIMIX_communication_wait_for_completion(comm_finished, -1);