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_;
22 #include "simix/datatypes.h"
24 /* Yeah, the following is awfull, breaking the encapsulation of at least 3 modules
25 * at the same time, but I'm tracking this bug since too long now, I want it dead. now.
29 xbt_thread_t listener;
30 } *fake_gras_msg_listener_t;
32 smx_process_t s_process;
35 int gras_socket_im_the_server(gras_socket_t sock) {
36 gras_trp_sg_sock_data_t sock_data = sock->data;
38 gras_msg_listener_t l;
39 xbt_thread_t listener_thread;
40 smx_process_t server_listener_process=NULL;
41 smx_process_t client_listener_process = NULL;
43 XBT_VERB("Am I the server of socket %p (client = %p, server = %p) ? process self: %p", sock, sock_data->client, sock_data->server, SIMIX_process_self());
45 if (sock_data->server == SIMIX_process_self()) {
46 XBT_VERB("I am the server");
49 if (sock_data->client == SIMIX_process_self()) {
50 XBT_VERB("I am the client");
53 XBT_VERB("I am neither the client nor the server, probably a listener");
55 /* neither the client nor the server. Check their respective listeners */
56 pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->server));
59 listener_thread = ((fake_gras_msg_listener_t)l)->listener;
60 server_listener_process = ((fake_xbt_thread_t)listener_thread)->s_process;
61 if (server_listener_process == SIMIX_process_self()) {
62 XBT_VERB("I am the listener of the server");
67 if (sock_data->client) {
68 pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->client));
71 listener_thread = ((fake_gras_msg_listener_t)l)->listener;
72 client_listener_process = ((fake_xbt_thread_t)listener_thread)->s_process;
73 if (client_listener_process == SIMIX_process_self()) {
74 XBT_VERB("I am the listener of the client");
79 /* THAT'S BAD! I should be either client or server of the sockets I get messages on!! */
80 /* This is where the bug is visible. Try to die as loudly as possible */
81 xbt_backtrace_display_current();
82 ((char*)sock)[sizeof(*sock)+1] = '0'; /* Try to make valgrind angry to see where that damn socket comes from */
83 if(system(bprintf("cat /proc/%d/maps 1>&2",getpid()))){}
84 XBT_INFO("I'm not the client in socket %p (comm:%p, rdvser=%p, rdvcli=%p) to %s, that's %s",
85 sock,sock_data->comm_recv,sock_data->rdv_server,sock_data->rdv_client,
86 SIMIX_host_get_name(SIMIX_process_get_host(sock_data->server)),
87 sock_data->client?SIMIX_host_get_name(SIMIX_process_get_host(sock_data->client)):"(no client)");
88 XBT_INFO("server:%s (%p) server_listener=%p client:%s (%p) client_listener=%p, I'm %p",
89 SIMIX_host_get_name(SIMIX_process_get_host(sock_data->server)), sock_data->server,server_listener_process,
90 sock_data->client?SIMIX_host_get_name(SIMIX_process_get_host(sock_data->client)):"(no client)", sock_data->client,client_listener_process,
91 SIMIX_process_self());
92 xbt_die("Bailing out after finding that damn bug");
96 gras_msg_t gras_msg_recv_any(void)
98 gras_trp_procdata_t trp_proc =
99 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
101 /* Build a dynar of all communications I could get something from */
102 xbt_dynar_t comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
103 unsigned int cursor = 0;
105 gras_socket_t sock = NULL;
106 gras_trp_sg_sock_data_t sock_data;
107 xbt_dynar_foreach(trp_proc->sockets, cursor, sock) {
108 sock_data = (gras_trp_sg_sock_data_t) sock->data;
112 ("Consider socket %p (data:%p; Here rdv: %p; Remote rdv: %p; Comm %p) to get a message",
114 gras_socket_im_the_server(sock)?
115 sock_data->rdv_server : sock_data->rdv_client,
116 gras_socket_im_the_server(sock)?
117 sock_data->rdv_client : sock_data->rdv_server,
118 sock_data->comm_recv);
121 /* If the following assert fails in some valid conditions, we need to
122 * change the code downward looking for the socket again.
124 * For now it relies on the facts (A) that sockets and comms are aligned
125 * (B) every sockets has a posted irecv in comms
127 * This is not trivial because we need that alignment to hold after the waitany(), so
128 * after other processes get scheduled.
130 * I cannot think of conditions where they get desynchronized (A violated) as long as
131 * 1) only the listener calls that function
132 * 2) Nobody but the listener removes sockets from that set (in main listener loop)
133 * 3) New sockets are added at the end, and signified ASAP to the listener (by awaking him)
134 * The throw bellow ensures that B is never violated without failing out loudly.
136 * We cannot search by comparing the comm object pointer that object got
137 * freed by the waiting process (down in smx_network, in
138 * comm_wait_for_completion or comm_cleanup). So, actually, we could
139 * use that pointer since that's a dangling pointer, but no one changes it.
140 * I still feel unconfortable with using dangling pointers, even if that would
141 * let the code work even if A and/or B are violated, provided that
142 * (C) the new irecv is never posted before we return from waitany to that function.
144 * Another approach, robust to B violation would be to retraverse the socks dynar with
145 * an iterator, incremented only when the socket has a comm. And we've the right socket
146 * when that iterator is equal to "got", the result of waitany. Not needed if B holds.
148 xbt_assert(sock_data->comm_recv,
149 "Comm_recv of socket %p is empty; please report that nasty bug",
151 /* End of paranoia */
153 XBT_VERB("Consider receiving messages from on comm_recv %p rdv:%p (other rdv:%p)",
154 sock_data->comm_recv,
155 gras_socket_im_the_server(sock)?
156 sock_data->rdv_server : sock_data->rdv_client,
157 gras_socket_im_the_server(sock)?
158 sock_data->rdv_client : sock_data->rdv_server);
159 xbt_dynar_push(comms, &(sock_data->comm_recv));
161 XBT_VERB("Wait on %ld 'sockets'", xbt_dynar_length(comms));
162 /* Wait for the end of any of these communications */
163 got = SIMIX_req_comm_waitany(comms);
165 /* retrieve the message sent in that communication */
166 sock = xbt_dynar_get_as(trp_proc->sockets, got, gras_socket_t);
167 sock_data = (gras_trp_sg_sock_data_t) sock->data;
168 msg = sock_data->msg;
169 XBT_VERB("Got something. Communication over rdv_server=%p, rdv_client=%p",
170 sock_data->rdv_server,sock_data->rdv_client);
172 /* Reinstall a waiting communication on that rdv */
173 /* xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
174 sock_data = (gras_trp_sg_sock_data_t) sock->data;
175 if (sock_data->comm_recv && sock_data->comm_recv == comm)
179 sock_data->comm_recv =
180 SIMIX_req_comm_irecv(gras_socket_im_the_server(sock) ?
181 sock_data->rdv_server : sock_data->rdv_client,
182 &sock_data->msg, NULL, NULL, NULL);
188 void gras_msg_send_ext(gras_socket_t sock,
189 e_gras_msg_kind_t kind,
190 unsigned long int ID,
191 gras_msgtype_t msgtype, void *payload)
193 int whole_payload_size = 0; /* msg->payload_size is used to memcpy the payload.
194 This is used to report the load onto the simulator. It also counts the size of pointed stuff */
195 gras_msg_t msg; /* message to send */
197 gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
199 smx_rdv_t target_rdv =
200 (sock_data->server == SIMIX_process_self())?
201 sock_data->rdv_client :
202 sock_data->rdv_server;
204 /*initialize gras message */
205 msg = xbt_new(s_gras_msg_t, 1);
212 XBT_VERB("Send msg %s (%s) to rdv %p sock %p",
213 msgtype->name, e_gras_msg_kind_names[kind], target_rdv, sock);
215 if (kind == e_gras_msg_kind_rpcerror) {
216 /* error on remote host, careful, payload is an exception */
217 msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
218 msg->payl = xbt_malloc(msg->payl_size);
220 gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"), payload,
222 } else if (kind == e_gras_msg_kind_rpcanswer) {
223 msg->payl_size = gras_datadesc_size(msgtype->answer_type);
225 msg->payl = xbt_malloc(msg->payl_size);
229 if (msgtype->answer_type)
230 whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
233 msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
234 msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
235 if (msgtype->ctn_type)
236 whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
240 comm = SIMIX_req_comm_isend(target_rdv, whole_payload_size, -1, msg, sizeof(void *), NULL,NULL, msg, 0);
241 SIMIX_req_comm_wait(comm, -1);
243 XBT_VERB("Message sent (and received)");