Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Permit to compile with full flags.
[simgrid.git] / src / gras / Msg / sg_msg.c
1 /* messaging - Function related to messaging code specific to SG            */
2
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/ex.h"
10
11 #include "gras/Virtu/virtu_sg.h"
12
13 #include "gras/Msg/msg_private.h"
14
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 */
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
20
21 typedef void *gras_trp_bufdata_;
22 #include "simix/datatypes.h"
23 #include "simix/private.h"
24
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.
27  * Sorry, Mt.
28  */
29 typedef struct {
30   xbt_thread_t listener;
31 } *fake_gras_msg_listener_t;
32 typedef struct {
33   smx_process_t s_process;
34 } *fake_xbt_thread_t;
35
36 int gras_socket_im_the_server(gras_socket_t sock) {
37   gras_trp_sg_sock_data_t sock_data = sock->data;
38   gras_procdata_t* pd;
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;
43
44
45   if (sock_data->server == SIMIX_process_self())
46     return 1;
47   if (sock_data->client == SIMIX_process_self())
48     return 0;
49
50   /* neither the client nor the server. Check their respective listeners */
51   pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->server));
52   l = pd->listener;
53   if (l) {
54     listener_thread = ((fake_gras_msg_listener_t)l)->listener;
55     server_listener_process = ((fake_xbt_thread_t)listener_thread)->s_process;
56     if (server_listener_process == SIMIX_process_self())
57       return 1;
58   }
59
60   if (sock_data->client) {
61     pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->client));
62     l = pd->listener;
63     if (l) {
64       listener_thread = ((fake_gras_msg_listener_t)l)->listener;
65       client_listener_process = ((fake_xbt_thread_t)listener_thread)->s_process;
66       if (client_listener_process == SIMIX_process_self())
67         return 0;
68     }
69   }
70   /* THAT'S BAD! I should be either client or server of the sockets I get messages on!! */
71   /* This is where the bug is visible. Try to die as loudly as possible */
72   xbt_backtrace_display_current();
73   ((char*)sock)[sizeof(*sock)+1] = '0'; /* Try to make valgrind angry to see where that damn socket comes from */
74   if(system(bprintf("cat /proc/%d/maps 1>&2",getpid()))){}
75   INFO6("I'm not the client in socket %p (comm:%p, rdvser=%p, rdvcli=%p) to %s, that's %s",
76       sock,sock_data->comm_recv,sock_data->rdv_server,sock_data->rdv_client,
77       SIMIX_host_get_name(SIMIX_process_get_host(sock_data->server)),
78       sock_data->client?SIMIX_host_get_name(SIMIX_process_get_host(sock_data->client)):"(no client)");
79   INFO7("server:%s (%p) server_listener=%p client:%s (%p) client_listener=%p, I'm %p",
80       SIMIX_host_get_name(SIMIX_process_get_host(sock_data->server)), sock_data->server,server_listener_process,
81       sock_data->client?SIMIX_host_get_name(SIMIX_process_get_host(sock_data->client)):"(no client)", sock_data->client,client_listener_process,
82           SIMIX_process_self());
83   xbt_die("Bailing out after finding that damn bug");
84
85 }
86
87 gras_msg_t gras_msg_recv_any(void)
88 {
89   gras_trp_procdata_t trp_proc =
90       (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
91   gras_msg_t msg;
92   /* Build a dynar of all communications I could get something from */
93   xbt_dynar_t comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
94   unsigned int cursor = 0;
95   int got = 0;
96   smx_comm_t comm = NULL;
97   gras_socket_t sock = NULL;
98   gras_trp_sg_sock_data_t sock_data;
99   xbt_dynar_foreach(trp_proc->sockets, cursor, sock) {
100     sock_data = (gras_trp_sg_sock_data_t) sock->data;
101
102
103     DEBUG5
104         ("Consider socket %p (data:%p; Here rdv: %p; Remote rdv: %p; Comm %p) to get a message",
105          sock, sock_data,
106          gras_socket_im_the_server(sock)?
107              sock_data->rdv_server : sock_data->rdv_client,
108          gras_socket_im_the_server(sock)?
109              sock_data->rdv_client : sock_data->rdv_server,
110          sock_data->comm_recv);
111
112
113     /* If the following assert fails in some valid conditions, we need to
114      * change the code downward looking for the socket again.
115      *
116      * For now it relies on the facts (A) that sockets and comms are aligned
117      *                                (B) every sockets has a posted irecv in comms
118      *
119      * This is not trivial because we need that alignment to hold after the waitany(), so
120      * after other processes get scheduled.
121      *
122      * I cannot think of conditions where they get desynchronized (A violated) as long as
123      *    1) only the listener calls that function
124      *    2) Nobody but the listener removes sockets from that set (in main listener loop)
125      *    3) New sockets are added at the end, and signified ASAP to the listener (by awaking him)
126      * The throw bellow ensures that B is never violated without failing out loudly.
127      *
128      * We cannot search by comparing the comm object pointer that object got
129      *    freed by the waiting process (down in smx_network, in
130      *    comm_wait_for_completion or comm_cleanup). So, actually, we could
131      *    use that pointer since that's a dangling pointer, but no one changes it.
132      * I still feel unconfortable with using dangling pointers, even if that would
133      *    let the code work even if A and/or B are violated, provided that
134      *    (C) the new irecv is never posted before we return from waitany to that function.
135      *
136      * Another approach, robust to B violation would be to retraverse the socks dynar with
137      *    an iterator, incremented only when the socket has a comm. And we've the right socket
138      *    when that iterator is equal to "got", the result of waitany. Not needed if B holds.
139      */
140     xbt_assert1(sock_data->comm_recv,
141                 "Comm_recv of socket %p is empty; please report that nasty bug",
142                 sock);
143     /* End of paranoia */
144
145     VERB4("Consider receiving messages from on comm_recv %p (%s) rdv:%p (other rdv:%p)",
146           sock_data->comm_recv, sock_data->comm_recv->type == comm_send? "send":"recv",
147           gras_socket_im_the_server(sock)?
148               sock_data->rdv_server : sock_data->rdv_client,
149           gras_socket_im_the_server(sock)?
150               sock_data->rdv_client : sock_data->rdv_server);
151     xbt_dynar_push(comms, &(sock_data->comm_recv));
152   }
153   VERB1("Wait on %ld 'sockets'", xbt_dynar_length(comms));
154   /* Wait for the end of any of these communications */
155   got = SIMIX_network_waitany(comms);
156
157   /* retrieve the message sent in that communication */
158   xbt_dynar_get_cpy(comms, got, &(comm));
159   msg = SIMIX_communication_get_data(comm);
160   sock = xbt_dynar_get_as(trp_proc->sockets, got, gras_socket_t);
161   sock_data = (gras_trp_sg_sock_data_t) sock->data;
162   VERB3("Got something. Communication %p's over rdv_server=%p, rdv_client=%p",
163       comm,sock_data->rdv_server,sock_data->rdv_client);
164   SIMIX_communication_destroy(comm);
165
166   /* Reinstall a waiting communication on that rdv */
167 /*  xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
168     sock_data = (gras_trp_sg_sock_data_t) sock->data;
169     if (sock_data->comm_recv && sock_data->comm_recv == comm)
170       break;
171   }
172   */
173   sock_data->comm_recv =
174       SIMIX_network_irecv(gras_socket_im_the_server(sock) ?
175                           sock_data->rdv_server : sock_data->rdv_client,
176                           NULL, 0);
177
178   return msg;
179 }
180
181
182 void gras_msg_send_ext(gras_socket_t sock,
183                        e_gras_msg_kind_t kind,
184                        unsigned long int ID,
185                        gras_msgtype_t msgtype, void *payload)
186 {
187   int whole_payload_size = 0;   /* msg->payload_size is used to memcpy the payload.
188                                    This is used to report the load onto the simulator. It also counts the size of pointed stuff */
189   gras_msg_t msg;               /* message to send */
190   smx_comm_t comm;
191   gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
192
193   smx_rdv_t target_rdv =
194       (sock_data->server == SIMIX_process_self())?
195           sock_data->rdv_client :
196           sock_data->rdv_server;
197
198   /*initialize gras message */
199   msg = xbt_new(s_gras_msg_t, 1);
200   sock->refcount++;
201   msg->expe = sock;
202   msg->kind = kind;
203   msg->type = msgtype;
204   msg->ID = ID;
205
206   VERB4("Send msg %s (%s) to rdv %p sock %p",
207       msgtype->name,  e_gras_msg_kind_names[kind], target_rdv, sock);
208
209   if (kind == e_gras_msg_kind_rpcerror) {
210     /* error on remote host, careful, payload is an exception */
211     msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
212     msg->payl = xbt_malloc(msg->payl_size);
213     whole_payload_size =
214         gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"), payload,
215                              msg->payl);
216   } else if (kind == e_gras_msg_kind_rpcanswer) {
217     msg->payl_size = gras_datadesc_size(msgtype->answer_type);
218     if (msg->payl_size)
219       msg->payl = xbt_malloc(msg->payl_size);
220     else
221       msg->payl = NULL;
222
223     if (msgtype->answer_type)
224       whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
225                                                 payload, msg->payl);
226   } else {
227     msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
228     msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
229     if (msgtype->ctn_type)
230       whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
231                                                 payload, msg->payl);
232   }
233
234   SIMIX_network_send(target_rdv, whole_payload_size, -1, -1, &msg,
235                      sizeof(void *), &comm, msg);
236
237   VERB0("Message sent (and received)");
238
239 }