1 /* messaging - Function related to messaging code specific to SG */
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
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. */
11 #include "gras/Virtu/virtu_sg.h"
13 #include "gras/Msg/msg_private.h"
15 #include "gras/DataDesc/datadesc_interface.h"
16 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
17 #include "gras/Transport/transport_private.h" /* sock->data */
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
21 typedef void *gras_trp_bufdata_;
23 gras_msg_t gras_msg_recv_any(void) {
24 gras_trp_procdata_t trp_proc =
25 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
27 /* Build a dynar of all communications I could get something from */
28 xbt_dynar_t comms = xbt_dynar_new(sizeof(smx_comm_t),NULL);
33 gras_trp_sg_sock_data_t *sock_data;
34 xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
35 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
36 if (sock_data->comm_recv) {
37 INFO2("Copy %p of size %lu",sock_data->comm_recv,(unsigned long int)sizeof(smx_comm_t));
38 xbt_dynar_push(comms,&(sock_data->comm_recv));
41 VERB1("Wait on %ld 'sockets'",xbt_dynar_length(comms));
42 /* Wait for the end of any of these communications */
43 got = SIMIX_network_waitany(comms);
45 /* retrieve the message sent in that communication */
46 xbt_dynar_get_cpy(comms,got,&(comm));
47 msg=SIMIX_communication_get_data(comm);
48 VERB1("Got something. Communication %p's over",comm);
50 /* Reinstall a waiting communication on that rdv */
51 /* Get the sock again */
52 xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
53 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
54 if (sock_data->comm_recv && sock_data->comm_recv == comm)
57 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
58 sock_data->comm_recv = SIMIX_network_irecv(
59 sock_data->im_server?sock_data->rdv_server:sock_data->rdv_client,
61 SIMIX_communication_destroy(comm);
67 void gras_msg_send_ext(gras_socket_t sock,
68 e_gras_msg_kind_t kind,
70 gras_msgtype_t msgtype, void *payload)
72 int whole_payload_size = 0; /* msg->payload_size is used to memcpy the payload.
73 This is used to report the load onto the simulator. It also counts the size of pointed stuff */
74 gras_msg_t msg; /* message to send */
76 gras_trp_sg_sock_data_t *sock_data = NULL;
77 /*initialize gras message */
78 msg = xbt_new(s_gras_msg_t, 1);
83 if (kind == e_gras_msg_kind_rpcerror) {
84 /* error on remote host, careful, payload is an exception */
85 msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
86 msg->payl = xbt_malloc(msg->payl_size);
87 whole_payload_size = gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"),
89 } else if (kind == e_gras_msg_kind_rpcanswer) {
90 msg->payl_size = gras_datadesc_size(msgtype->answer_type);
92 msg->payl = xbt_malloc(msg->payl_size);
96 if (msgtype->answer_type)
97 whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
100 msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
101 msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
102 if (msgtype->ctn_type)
103 whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
106 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
108 SIMIX_network_send(sock_data->im_server ? sock_data->rdv_client : sock_data->rdv_client,
109 whole_payload_size,-1,-1,&msg,sizeof(void*),&comm,msg);
112 smx_action_t act; /* simix action */
114 gras_trp_procdata_t trp_remote_proc;
115 gras_msg_procdata_t msg_remote_proc;
117 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
119 hd = (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
121 xbt_assert1(!gras_socket_is_meas(sock),
122 "Asked to send a message on the measurement socket %p", sock);
125 /* put the selectable socket on the queue */
126 trp_remote_proc = (gras_trp_procdata_t)
127 gras_libdata_by_name_from_remote("gras_trp", sock_data->to_process);
129 xbt_queue_push(trp_remote_proc->msg_selectable_sockets, &sock);
131 /* put message on msg_queue */
132 msg_remote_proc = (gras_msg_procdata_t)
133 gras_libdata_by_name_from_remote("gras_msg", sock_data->to_process);
134 xbt_fifo_push(msg_remote_proc->msg_to_receive_queue, msg);
136 /* wait for the receiver */
137 SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
139 /* creates simix action and waits its ends, waits in the sender host
141 act = SIMIX_action_communicate(SIMIX_host_self(),
142 sock_data->to_host, msgtype->name,
143 (double) whole_payload_size, -1);
144 SIMIX_register_action_to_condition(act, sock_data->cond);
146 VERB5("Sending to %s(%s) a message type '%s' kind '%s' ID %lu",
147 SIMIX_host_get_name(sock_data->to_host),
148 SIMIX_process_get_name(sock_data->to_process),
149 msg->type->name, e_gras_msg_kind_names[msg->kind], msg->ID);
151 SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
152 SIMIX_unregister_action_to_condition(act, sock_data->cond);
153 /* error treatmeant (FIXME) */
155 /* cleanup structures */
156 SIMIX_action_destroy(act);
157 SIMIX_mutex_unlock(sock_data->mutex);
159 VERB0("Message sent");
165 * receive the next message on the given socket.
167 void gras_msg_recv(gras_socket_t sock, gras_msg_t msg)
170 gras_trp_sg_sock_data_t *sock_data =
171 (gras_trp_sg_sock_data_t *) sock->data;
173 size_t size_got = sizeof(void*);
175 xbt_assert1(!gras_socket_is_meas(sock),
176 "Asked to receive a message on the measurement socket %p",
179 SIMIX_network_recv(sock_data->rdv,-1,&msg_got,&size_got,NULL);
181 gras_trp_sg_sock_data_t *remote_sock_data;
182 gras_hostdata_t *remote_hd;
183 gras_msg_procdata_t msg_procdata =
184 (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
186 xbt_assert0(msg, "msg is an out parameter of gras_msg_recv...");
188 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
190 ((gras_trp_sg_sock_data_t *) sock->data)->to_socket->data;
191 DEBUG3("Remote host %s, Remote Port: %d Local port %d",
192 SIMIX_host_get_name(sock_data->to_host), sock->peer_port,
194 remote_hd = (gras_hostdata_t *) SIMIX_host_get_data(sock_data->to_host);
196 if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0) {
199 DEBUG1("Size msg_to_receive buffer: %d",
200 xbt_fifo_size(msg_procdata->msg_to_receive_queue));
201 msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
203 SIMIX_mutex_lock(remote_sock_data->mutex);
204 /* ok, I'm here, you can continuate the communication */
205 SIMIX_cond_signal(remote_sock_data->cond);
207 /* wait for communication end */
208 SIMIX_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
210 msg_got->expe = msg->expe;
211 memcpy(msg, msg_got, sizeof(s_gras_msg_t));
213 SIMIX_mutex_unlock(remote_sock_data->mutex);
215 VERB3("Received a message type '%s' kind '%s' ID %lu", // from %s",
216 msg->type->name, e_gras_msg_kind_names[msg->kind], msg->ID);