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"
23 #include "simix/private.h"
25 /* Yeah, the following is awfull, breaking the encapsulation of at least 3 modules
26 * at the same time, but I'm tracking this bug since too long now, I want it dead. now.
30 xbt_thread_t listener;
31 } *fake_gras_msg_listener_t;
33 smx_process_t s_process;
36 int gras_socket_im_the_server(gras_socket_t sock) {
37 gras_trp_sg_sock_data_t sock_data = sock->data;
39 gras_msg_listener_t l;
40 xbt_thread_t listener_thread;
41 smx_process_t server_listener_process=NULL;
42 smx_process_t client_listener_process = NULL;
44 VERB4("Am I the server of socket %p (client = %p, server = %p) ? process self: %p", sock, sock_data->client, sock_data->server, SIMIX_process_self());
46 if (sock_data->server == SIMIX_process_self()) {
47 VERB0("I am the server");
50 if (sock_data->client == SIMIX_process_self()) {
51 VERB0("I am the client");
54 VERB0("I am neither the client nor the server, probably a listener");
56 /* neither the client nor the server. Check their respective listeners */
57 pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->server));
60 listener_thread = ((fake_gras_msg_listener_t)l)->listener;
61 server_listener_process = ((fake_xbt_thread_t)listener_thread)->s_process;
62 if (server_listener_process == SIMIX_process_self()) {
63 VERB0("I am the listener of the server");
68 if (sock_data->client) {
69 pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->client));
72 listener_thread = ((fake_gras_msg_listener_t)l)->listener;
73 client_listener_process = ((fake_xbt_thread_t)listener_thread)->s_process;
74 if (client_listener_process == SIMIX_process_self()) {
75 VERB0("I am the listener of the client");
80 /* THAT'S BAD! I should be either client or server of the sockets I get messages on!! */
81 /* This is where the bug is visible. Try to die as loudly as possible */
82 xbt_backtrace_display_current();
83 ((char*)sock)[sizeof(*sock)+1] = '0'; /* Try to make valgrind angry to see where that damn socket comes from */
84 if(system(bprintf("cat /proc/%d/maps 1>&2",getpid()))){}
85 INFO6("I'm not the client in socket %p (comm:%p, rdvser=%p, rdvcli=%p) to %s, that's %s",
86 sock,sock_data->comm_recv,sock_data->rdv_server,sock_data->rdv_client,
87 SIMIX_host_get_name(SIMIX_process_get_host(sock_data->server)),
88 sock_data->client?SIMIX_host_get_name(SIMIX_process_get_host(sock_data->client)):"(no client)");
89 INFO7("server:%s (%p) server_listener=%p client:%s (%p) client_listener=%p, I'm %p",
90 SIMIX_host_get_name(SIMIX_process_get_host(sock_data->server)), sock_data->server,server_listener_process,
91 sock_data->client?SIMIX_host_get_name(SIMIX_process_get_host(sock_data->client)):"(no client)", sock_data->client,client_listener_process,
92 SIMIX_process_self());
93 xbt_die("Bailing out after finding that damn bug");
97 gras_msg_t gras_msg_recv_any(void)
99 gras_trp_procdata_t trp_proc =
100 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
102 /* Build a dynar of all communications I could get something from */
103 xbt_dynar_t comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
104 unsigned int cursor = 0;
106 smx_action_t comm = NULL;
107 gras_socket_t sock = NULL;
108 gras_trp_sg_sock_data_t sock_data;
109 xbt_dynar_foreach(trp_proc->sockets, cursor, sock) {
110 sock_data = (gras_trp_sg_sock_data_t) sock->data;
114 ("Consider socket %p (data:%p; Here rdv: %p; Remote rdv: %p; Comm %p) to get a message",
116 gras_socket_im_the_server(sock)?
117 sock_data->rdv_server : sock_data->rdv_client,
118 gras_socket_im_the_server(sock)?
119 sock_data->rdv_client : sock_data->rdv_server,
120 sock_data->comm_recv);
123 /* If the following assert fails in some valid conditions, we need to
124 * change the code downward looking for the socket again.
126 * For now it relies on the facts (A) that sockets and comms are aligned
127 * (B) every sockets has a posted irecv in comms
129 * This is not trivial because we need that alignment to hold after the waitany(), so
130 * after other processes get scheduled.
132 * I cannot think of conditions where they get desynchronized (A violated) as long as
133 * 1) only the listener calls that function
134 * 2) Nobody but the listener removes sockets from that set (in main listener loop)
135 * 3) New sockets are added at the end, and signified ASAP to the listener (by awaking him)
136 * The throw bellow ensures that B is never violated without failing out loudly.
138 * We cannot search by comparing the comm object pointer that object got
139 * freed by the waiting process (down in smx_network, in
140 * comm_wait_for_completion or comm_cleanup). So, actually, we could
141 * use that pointer since that's a dangling pointer, but no one changes it.
142 * I still feel unconfortable with using dangling pointers, even if that would
143 * let the code work even if A and/or B are violated, provided that
144 * (C) the new irecv is never posted before we return from waitany to that function.
146 * Another approach, robust to B violation would be to retraverse the socks dynar with
147 * an iterator, incremented only when the socket has a comm. And we've the right socket
148 * when that iterator is equal to "got", the result of waitany. Not needed if B holds.
150 xbt_assert1(sock_data->comm_recv,
151 "Comm_recv of socket %p is empty; please report that nasty bug",
153 /* End of paranoia */
155 VERB3("Consider receiving messages from on comm_recv %p rdv:%p (other rdv:%p)",
156 sock_data->comm_recv,
157 gras_socket_im_the_server(sock)?
158 sock_data->rdv_server : sock_data->rdv_client,
159 gras_socket_im_the_server(sock)?
160 sock_data->rdv_client : sock_data->rdv_server);
161 xbt_dynar_push(comms, &(sock_data->comm_recv));
163 VERB1("Wait on %ld 'sockets'", xbt_dynar_length(comms));
164 /* Wait for the end of any of these communications */
165 got = SIMIX_req_comm_waitany(comms);
167 /* retrieve the message sent in that communication */
168 xbt_dynar_get_cpy(comms, got, &(comm));
169 msg = SIMIX_req_comm_get_src_data(comm);
170 sock = xbt_dynar_get_as(trp_proc->sockets, got, gras_socket_t);
171 sock_data = (gras_trp_sg_sock_data_t) sock->data;
172 VERB3("Got something. Communication %p's over rdv_server=%p, rdv_client=%p",
173 comm,sock_data->rdv_server,sock_data->rdv_client);
174 SIMIX_req_comm_destroy(comm);
176 /* Reinstall a waiting communication on that rdv */
177 /* xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
178 sock_data = (gras_trp_sg_sock_data_t) sock->data;
179 if (sock_data->comm_recv && sock_data->comm_recv == comm)
183 sock_data->comm_recv =
184 SIMIX_req_comm_irecv(gras_socket_im_the_server(sock) ?
185 sock_data->rdv_server : sock_data->rdv_client,
186 NULL, 0, NULL, NULL);
192 void gras_msg_send_ext(gras_socket_t sock,
193 e_gras_msg_kind_t kind,
194 unsigned long int ID,
195 gras_msgtype_t msgtype, void *payload)
197 int whole_payload_size = 0; /* msg->payload_size is used to memcpy the payload.
198 This is used to report the load onto the simulator. It also counts the size of pointed stuff */
199 gras_msg_t msg; /* message to send */
201 gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
203 smx_rdv_t target_rdv =
204 (sock_data->server == SIMIX_process_self())?
205 sock_data->rdv_client :
206 sock_data->rdv_server;
208 /*initialize gras message */
209 msg = xbt_new(s_gras_msg_t, 1);
216 VERB4("Send msg %s (%s) to rdv %p sock %p",
217 msgtype->name, e_gras_msg_kind_names[kind], target_rdv, sock);
219 if (kind == e_gras_msg_kind_rpcerror) {
220 /* error on remote host, careful, payload is an exception */
221 msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
222 msg->payl = xbt_malloc(msg->payl_size);
224 gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"), payload,
226 } else if (kind == e_gras_msg_kind_rpcanswer) {
227 msg->payl_size = gras_datadesc_size(msgtype->answer_type);
229 msg->payl = xbt_malloc(msg->payl_size);
233 if (msgtype->answer_type)
234 whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
237 msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
238 msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
239 if (msgtype->ctn_type)
240 whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
244 comm = SIMIX_req_comm_isend(target_rdv, whole_payload_size, -1, &msg, sizeof(void *), NULL, msg, 0);
245 SIMIX_req_comm_wait(comm, -1);
247 VERB0("Message sent (and received)");