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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13 "Logging specific to SIMIX (network)");
15 static xbt_dict_t rdv_points = NULL;
17 static XBT_INLINE void SIMIX_comm_start(smx_action_t action);
18 static void SIMIX_comm_finish(smx_action_t action);
19 static void SIMIX_waitany_req_remove_from_actions(smx_req_t req);
20 static void SIMIX_comm_copy_data(smx_action_t comm);
21 static smx_action_t SIMIX_comm_new(e_smx_comm_type_t type);
22 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm);
23 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm);
24 static smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
25 int (*match_fun)(void *, void *), void *);
26 static void SIMIX_rdv_free(void *data);
28 void SIMIX_network_init(void)
30 rdv_points = xbt_dict_new();
33 void SIMIX_network_exit(void)
35 xbt_dict_free(&rdv_points);
38 /******************************************************************************/
39 /* Rendez-Vous Points */
40 /******************************************************************************/
42 smx_rdv_t SIMIX_rdv_create(const char *name)
44 /* two processes may have pushed the same rdv_create request at the same time */
45 smx_rdv_t rdv = name ? xbt_dict_get_or_null(rdv_points, name) : NULL;
48 rdv = xbt_new0(s_smx_rvpoint_t, 1);
49 rdv->name = name ? xbt_strdup(name) : NULL;
50 rdv->comm_fifo = xbt_fifo_new();
53 xbt_dict_set(rdv_points, rdv->name, rdv, SIMIX_rdv_free);
58 void SIMIX_rdv_destroy(smx_rdv_t rdv)
61 xbt_dict_remove(rdv_points, rdv->name);
64 void SIMIX_rdv_free(void *data)
66 smx_rdv_t rdv = (smx_rdv_t) data;
69 xbt_fifo_free(rdv->comm_fifo);
73 smx_rdv_t SIMIX_rdv_get_by_name(const char *name)
75 return xbt_dict_get_or_null(rdv_points, name);
78 int SIMIX_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host)
80 smx_action_t comm = NULL;
81 xbt_fifo_item_t item = NULL;
84 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_action_t) {
85 if (comm->comm.src_proc->smx_host == host)
92 smx_action_t SIMIX_rdv_get_head(smx_rdv_t rdv)
94 return xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
98 * \brief Push a communication request into a rendez-vous point
99 * \param rdv The rendez-vous point
100 * \param comm The communication request
102 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm)
104 xbt_fifo_push(rdv->comm_fifo, comm);
105 comm->comm.rdv = rdv;
109 * \brief Remove a communication request from a rendez-vous point
110 * \param rdv The rendez-vous point
111 * \param comm The communication request
113 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm)
115 xbt_fifo_remove(rdv->comm_fifo, comm);
116 comm->comm.rdv = NULL;
120 * \brief Checks if there is a communication action queued in a rendez-vous matching our needs
121 * \param type The type of communication we are looking for (comm_send, comm_recv)
122 * \return The communication action if found, NULL otherwise
124 smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
125 int (*match_fun)(void *, void *), void *data)
128 xbt_fifo_item_t item;
129 void* req_data = NULL;
131 xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t){
132 if (action->comm.type == SIMIX_COMM_SEND) {
133 req_data = action->comm.src_data;
134 } else if (action->comm.type == SIMIX_COMM_RECEIVE) {
135 req_data = action->comm.dst_data;
137 if (action->comm.type == type && (!match_fun || match_fun(data, req_data))) {
138 DEBUG1("Found a matching communication action %p", action);
139 xbt_fifo_remove_item(rdv->comm_fifo, item);
140 xbt_fifo_free_item(item);
141 action->comm.refcount++;
142 action->comm.rdv = NULL;
145 DEBUG3("Sorry, communication action %p does not match our needs:"
146 " its type is %d but we are looking for a comm of type %d",
147 action, action->comm.type, type);
149 DEBUG0("No matching communication action found");
153 /******************************************************************************/
154 /* Comunication Actions */
155 /******************************************************************************/
158 * \brief Creates a new comunicate action
159 * \param type The type of request (comm_send, comm_recv)
160 * \return The new comunicate action
162 smx_action_t SIMIX_comm_new(e_smx_comm_type_t type)
166 /* alloc structures */
167 act = xbt_new0(s_smx_action_t, 1);
168 act->type = SIMIX_ACTION_COMMUNICATE;
169 act->state = SIMIX_WAITING;
170 act->request_list = xbt_fifo_new();
172 /* set communication */
173 act->comm.type = type;
174 act->comm.refcount = 1;
176 #ifdef HAVE_LATENCY_BOUND_TRACKING
177 //initialize with unknown value
178 act->latency_limited = -1;
182 act->category = NULL;
185 DEBUG1("Create communicate action %p", act);
191 * \brief Destroy a communicate action
192 * \param action The communicate action to be destroyed
194 void SIMIX_comm_destroy(smx_action_t action)
196 DEBUG2("Destroy action %p (refcount:%d)", action, action->comm.refcount);
198 if (action->comm.refcount <= 0)
199 xbt_die(bprintf("the refcount of comm %p is already 0 before decreasing it. That's a bug!",action));
201 action->comm.refcount--;
202 if (action->comm.refcount > 0)
204 DEBUG2("Really free communication %p; refcount is now %d", action,
205 action->comm.refcount);
207 #ifdef HAVE_LATENCY_BOUND_TRACKING
208 action->latency_limited = SIMIX_comm_is_latency_bounded( action ) ;
212 TRACE_smx_action_destroy(action);
216 xbt_free(action->name);
218 xbt_fifo_free(action->request_list);
220 SIMIX_comm_destroy_internal_actions(action);
225 void SIMIX_comm_destroy_internal_actions(smx_action_t action)
227 if (action->comm.surf_comm){
228 #ifdef HAVE_LATENCY_BOUND_TRACKING
229 action->latency_limited = SIMIX_comm_is_latency_bounded(action);
231 action->comm.surf_comm->model_type->action_unref(action->comm.surf_comm);
232 action->comm.surf_comm = NULL;
235 if (action->comm.src_timeout){
236 action->comm.src_timeout->model_type->action_unref(action->comm.src_timeout);
237 action->comm.src_timeout = NULL;
240 if (action->comm.dst_timeout){
241 action->comm.dst_timeout->model_type->action_unref(action->comm.dst_timeout);
242 action->comm.dst_timeout = NULL;
246 smx_action_t SIMIX_comm_isend(smx_process_t src_proc, smx_rdv_t rdv,
247 double task_size, double rate,
248 void *src_buff, size_t src_buff_size,
249 int (*match_fun)(void *, void *), void *data)
253 /* Look for communication request matching our needs.
254 If it is not found then create it and push it into the rendez-vous point */
255 action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_RECEIVE, match_fun, data);
258 action = SIMIX_comm_new(SIMIX_COMM_SEND);
259 SIMIX_rdv_push(rdv, action);
261 action->state = SIMIX_READY;
262 action->comm.type = SIMIX_COMM_READY;
265 /* Setup the communication request */
266 action->comm.src_proc = src_proc;
267 action->comm.task_size = task_size;
268 action->comm.rate = rate;
269 action->comm.src_buff = src_buff;
270 action->comm.src_buff_size = src_buff_size;
271 action->comm.src_data = data;
274 action->state = SIMIX_RUNNING;
278 SIMIX_comm_start(action);
282 smx_action_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_rdv_t rdv,
283 void *dst_buff, size_t *dst_buff_size,
284 int (*match_fun)(void *, void *), void *data)
288 /* Look for communication request matching our needs.
289 * If it is not found then create it and push it into the rendez-vous point
291 action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
294 action = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
295 SIMIX_rdv_push(rdv, action);
297 action->state = SIMIX_READY;
298 action->comm.type = SIMIX_COMM_READY;
301 /* Setup communication request */
302 action->comm.dst_proc = dst_proc;
303 action->comm.dst_buff = dst_buff;
304 action->comm.dst_buff_size = dst_buff_size;
305 action->comm.dst_data = data;
308 action->state = SIMIX_RUNNING;
312 SIMIX_comm_start(action);
316 void SIMIX_pre_comm_wait(smx_req_t req, int idx)
318 smx_action_t action = req->comm_wait.comm;
319 double timeout = req->comm_wait.timeout;
322 /* Associate this request to the action */
323 xbt_fifo_push(action->request_list, req);
324 req->issuer->waiting_action = action;
328 action->state = SIMIX_DONE;
330 /* If we reached this point, the wait request must have a timeout */
331 /* Otherwise it shouldn't be enabled and executed by the MC */
335 if(action->comm.src_proc == req->issuer)
336 action->state = SIMIX_SRC_TIMEOUT;
338 action->state = SIMIX_DST_TIMEOUT;
341 SIMIX_comm_finish(action);
345 /* If the action has already finish perform the error handling, */
346 /* otherwise set up a waiting timeout on the right side */
347 if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
348 SIMIX_comm_finish(action);
349 } else { /* if (timeout >= 0) { we need a surf sleep action even when there is no timeout, otherwise surf won't tell us when the host fails */
350 sleep = surf_workstation_model->extension.workstation.sleep(req->issuer->smx_host->host, timeout);
351 surf_workstation_model->action_data_set(sleep, action);
353 if (req->issuer == action->comm.src_proc)
354 action->comm.src_timeout = sleep;
356 action->comm.dst_timeout = sleep;
360 void SIMIX_pre_comm_test(smx_req_t req)
362 smx_action_t action = req->comm_test.comm;
365 req->comm_test.result = action->comm.src_proc && action->comm.dst_proc;
366 if(req->comm_test.result){
367 action->state = SIMIX_DONE;
368 xbt_fifo_push(action->request_list, req);
369 SIMIX_comm_finish(action);
371 SIMIX_request_answer(req);
376 req->comm_test.result = (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING);
377 if (req->comm_test.result) {
378 xbt_fifo_push(action->request_list, req);
379 SIMIX_comm_finish(action);
381 SIMIX_request_answer(req);
385 void SIMIX_pre_comm_testany(smx_req_t req, int idx)
389 xbt_dynar_t actions = req->comm_testany.comms;
390 req->comm_testany.result = -1;
394 SIMIX_request_answer(req);
396 action = xbt_dynar_get_as(actions, idx, smx_action_t);
397 req->comm_testany.result = idx;
398 xbt_fifo_push(action->request_list, req);
399 action->state = SIMIX_DONE;
400 SIMIX_comm_finish(action);
405 xbt_dynar_foreach(req->comm_testany.comms,cursor,action) {
406 if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
407 req->comm_testany.result = cursor;
408 xbt_fifo_push(action->request_list, req);
409 SIMIX_comm_finish(action);
413 SIMIX_request_answer(req);
416 void SIMIX_pre_comm_waitany(smx_req_t req, int idx)
419 unsigned int cursor = 0;
420 xbt_dynar_t actions = req->comm_waitany.comms;
423 action = xbt_dynar_get_as(actions, idx, smx_action_t);
424 xbt_fifo_push(action->request_list, req);
425 req->comm_waitany.result = idx;
426 action->state = SIMIX_DONE;
427 SIMIX_comm_finish(action);
431 xbt_dynar_foreach(actions, cursor, action){
432 /* Associate this request to the action */
433 xbt_fifo_push(action->request_list, req);
434 if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING){
435 SIMIX_comm_finish(action);
441 void SIMIX_waitany_req_remove_from_actions(smx_req_t req)
444 unsigned int cursor = 0;
445 xbt_dynar_t actions = req->comm_waitany.comms;
447 xbt_dynar_foreach(actions, cursor, action){
448 xbt_fifo_remove(action->request_list, req);
453 * \brief Start the simulation of a communication request
454 * \param action The communication action
456 static XBT_INLINE void SIMIX_comm_start(smx_action_t action)
458 /* If both the sender and the receiver are already there, start the communication */
459 if (action->state == SIMIX_READY) {
460 smx_host_t sender = action->comm.src_proc->smx_host;
461 smx_host_t receiver = action->comm.dst_proc->smx_host;
463 DEBUG3("Starting communication %p from '%s' to '%s'", action,
464 SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
466 action->comm.surf_comm = surf_workstation_model->extension.workstation.
467 communicate(sender->host, receiver->host, action->comm.task_size, action->comm.rate);
469 surf_workstation_model->action_data_set(action->comm.surf_comm, action);
471 action->state = SIMIX_RUNNING;
474 TRACE_smx_action_communicate(action, action->comm.src_proc);
477 /* If a link is failed, detect it immediately */
478 if (surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED) {
479 DEBUG2("Communication from '%s' to '%s' failed to start because of a link failure",
480 SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
481 action->state = SIMIX_LINK_FAILURE;
482 SIMIX_comm_destroy_internal_actions(action);
485 /* If any of the process is suspend, create the action but stop its execution,
486 it will be restarted when the sender process resume */
487 if (SIMIX_process_is_suspended(action->comm.src_proc) ||
488 SIMIX_process_is_suspended(action->comm.dst_proc)) {
489 /* FIXME: check what should happen with the action state */
490 surf_workstation_model->suspend(action->comm.surf_comm);
495 void SIMIX_comm_finish(smx_action_t action)
499 while ((req = xbt_fifo_shift(action->request_list))) {
501 /* If a waitany request is waiting for this action to finish, then remove
502 it from the other actions in the waitany list. Afterwards, get the
503 position of the actual action in the waitany request's actions dynar and
504 return it as the result of the call */
505 if (req->call == REQ_COMM_WAITANY) {
506 SIMIX_waitany_req_remove_from_actions(req);
508 req->comm_waitany.result = xbt_dynar_search(req->comm_waitany.comms, &action);
511 /* If the action is still in a rendez-vous point then remove from it */
512 if (action->comm.rdv)
513 SIMIX_rdv_remove(action->comm.rdv, action);
515 DEBUG1("SIMIX_comm_finish: action state = %d", action->state);
517 /* Check out for errors */
518 switch (action->state) {
521 DEBUG1("Communication %p complete!", action);
522 SIMIX_comm_copy_data(action);
525 case SIMIX_SRC_TIMEOUT:
527 THROW0(timeout_error, 0, "Communication timeouted because of sender");
529 CATCH(req->issuer->running_ctx->exception) {
530 req->issuer->doexception = 1;
534 case SIMIX_DST_TIMEOUT:
536 THROW0(timeout_error, 0, "Communication timeouted because of receiver");
538 CATCH(req->issuer->running_ctx->exception) {
539 req->issuer->doexception = 1;
543 case SIMIX_SRC_HOST_FAILURE:
545 if (req->issuer == action->comm.src_proc)
546 THROW0(host_error, 0, "Host failed");
548 THROW0(network_error, 0, "Remote peer failed");
550 CATCH(req->issuer->running_ctx->exception) {
551 req->issuer->doexception = 1;
555 case SIMIX_DST_HOST_FAILURE:
557 if (req->issuer == action->comm.dst_proc)
558 THROW0(host_error, 0, "Host failed");
560 THROW0(network_error, 0, "Remote peer failed");
562 CATCH(req->issuer->running_ctx->exception) {
563 req->issuer->doexception = 1;
567 case SIMIX_LINK_FAILURE:
569 DEBUG5("Link failure in action %p between '%s' and '%s': posting an exception to the issuer: %s (%p)",
570 action, action->comm.src_proc->smx_host->name, action->comm.dst_proc->smx_host->name,
571 req->issuer->name, req->issuer);
572 THROW0(network_error, 0, "Link failure");
574 CATCH(req->issuer->running_ctx->exception) {
575 req->issuer->doexception = 1;
583 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
584 if (req->issuer->doexception) {
585 if (req->call == REQ_COMM_WAITANY) {
586 req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_waitany.comms, &action);
588 else if (req->call == REQ_COMM_TESTANY) {
589 req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_testany.comms, &action);
593 req->issuer->waiting_action = NULL;
594 SIMIX_request_answer(req);
598 void SIMIX_post_comm(smx_action_t action)
600 /* Update action state */
601 if (action->comm.src_timeout &&
602 surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_DONE)
603 action->state = SIMIX_SRC_TIMEOUT;
604 else if (action->comm.dst_timeout &&
605 surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_DONE)
606 action->state = SIMIX_DST_TIMEOUT;
607 else if (action->comm.src_timeout &&
608 surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_FAILED)
609 action->state = SIMIX_SRC_HOST_FAILURE;
610 else if (action->comm.dst_timeout &&
611 surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_FAILED)
612 action->state = SIMIX_DST_HOST_FAILURE;
613 else if (action->comm.surf_comm &&
614 surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED)
615 action->state = SIMIX_LINK_FAILURE;
617 action->state = SIMIX_DONE;
619 DEBUG1("SIMIX_post_comm: action state = %d", action->state);
621 /* After this point the surf actions associated with the simix communicate
622 action are no longer needed, thus we delete them. */
623 SIMIX_comm_destroy_internal_actions(action);
625 /* If there are requests associated with the action, then answer them */
626 if (xbt_fifo_size(action->request_list))
627 SIMIX_comm_finish(action);
630 void SIMIX_comm_cancel(smx_action_t action)
632 /* If the action is a waiting state means that it is still in a rdv */
633 /* so remove from it and delete it */
634 if (action->state == SIMIX_WAITING) {
635 SIMIX_rdv_remove(action->comm.rdv, action);
636 action->state = SIMIX_FAILED;
638 /* When running the MC there are no surf actions */
640 surf_workstation_model->action_cancel(action->comm.surf_comm);
644 void SIMIX_comm_suspend(smx_action_t action)
646 /*FIXME: shall we suspend also the timeout actions? */
647 surf_workstation_model->suspend(action->comm.surf_comm);
650 void SIMIX_comm_resume(smx_action_t action)
652 /*FIXME: check what happen with the timeouts */
653 surf_workstation_model->resume(action->comm.surf_comm);
657 /************* Action Getters **************/
660 * \brief get the amount remaining from the communication
661 * \param action The communication
663 double SIMIX_comm_get_remains(smx_action_t action)
667 switch (action->state) {
670 remains = surf_workstation_model->get_remains(action->comm.surf_comm);
675 remains = 0; /*FIXME: check what should be returned */
679 remains = 0; /*FIXME: is this correct? */
685 e_smx_state_t SIMIX_comm_get_state(smx_action_t action)
687 return action->state;
691 * \brief Return the user data associated to the sender of the communication
692 * \param action The communication
693 * \return the user data
695 void* SIMIX_comm_get_src_data(smx_action_t action)
697 return action->comm.src_data;
701 * \brief Return the user data associated to the receiver of the communication
702 * \param action The communication
703 * \return the user data
705 void* SIMIX_comm_get_dst_data(smx_action_t action)
707 return action->comm.dst_data;
710 void* SIMIX_comm_get_src_buff(smx_action_t action)
712 return action->comm.src_buff;
715 void* SIMIX_comm_get_dst_buff(smx_action_t action)
717 return action->comm.dst_buff;
720 size_t SIMIX_comm_get_src_buff_size(smx_action_t action)
722 return action->comm.src_buff_size;
725 size_t SIMIX_comm_get_dst_buff_size(smx_action_t action)
729 if (action->comm.dst_buff_size)
730 buff_size = *(action->comm.dst_buff_size);
737 smx_process_t SIMIX_comm_get_src_proc(smx_action_t action)
739 return action->comm.src_proc;
742 smx_process_t SIMIX_comm_get_dst_proc(smx_action_t action)
744 return action->comm.dst_proc;
747 #ifdef HAVE_LATENCY_BOUND_TRACKING
749 * \brief verify if communication is latency bounded
750 * \param comm The communication
752 XBT_INLINE int SIMIX_comm_is_latency_bounded(smx_action_t action)
754 if (action->comm.surf_comm){
755 DEBUG1("Getting latency limited for surf_action (%p)", action->comm.surf_comm);
756 action->latency_limited = surf_workstation_model->get_latency_limited(action->comm.surf_comm);
757 DEBUG1("Action limited is %d", action->latency_limited);
759 return action->latency_limited;
763 /******************************************************************************/
764 /* SIMIX_comm_copy_data callbacks */
765 /******************************************************************************/
766 static void (*SIMIX_comm_copy_data_callback) (smx_action_t, size_t) =
767 &SIMIX_comm_copy_pointer_callback;
770 SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, size_t))
772 SIMIX_comm_copy_data_callback = callback;
775 void SIMIX_comm_copy_pointer_callback(smx_action_t comm, size_t buff_size)
777 xbt_assert1((buff_size == sizeof(void *)),
778 "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
779 *(void **) (comm->comm.dst_buff) = comm->comm.src_buff;
782 void SIMIX_comm_copy_buffer_callback(smx_action_t comm, size_t buff_size)
784 memcpy(comm->comm.dst_buff, comm->comm.src_buff, buff_size);
788 * \brief Copy the communication data from the sender's buffer to the receiver's one
789 * \param comm The communication
791 void SIMIX_comm_copy_data(smx_action_t comm)
793 size_t buff_size = comm->comm.src_buff_size;
794 /* If there is no data to be copy then return */
795 if (!comm->comm.src_buff || !comm->comm.dst_buff || comm->comm.copied == 1)
798 DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
800 comm->comm.src_proc->smx_host->name, comm->comm.src_buff,
801 comm->comm.dst_proc->smx_host->name, comm->comm.dst_buff, buff_size);
803 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
804 if (comm->comm.dst_buff_size)
805 buff_size = MIN(buff_size, *(comm->comm.dst_buff_size));
807 /* Update the receiver's buffer size to the copied amount */
808 if (comm->comm.dst_buff_size)
809 *comm->comm.dst_buff_size = buff_size;
814 (*SIMIX_comm_copy_data_callback) (comm, buff_size);
816 /* Set the copied flag so we copy data only once */
817 /* (this function might be called from both communication ends) */
818 comm->comm.copied = 1;