Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't pass NULL to xbt_die.
[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 #include "xbt/xbt_socket_private.h" /* FIXME */
11 #include "xbt/datadesc.h"
12 #include "xbt/datadesc/datadesc_interface.h" /* FIXME */
13 #include "gras/Virtu/virtu_sg.h"
14 #include "gras/Msg/msg_private.h"
15 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
16 #include "gras/Transport/transport_private.h"   /* sock->data */
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
19
20 typedef void *gras_trp_bufdata_;
21 #include "simix/datatypes.h"
22 #include "simix/smx_private.h"
23
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.
26  * Sorry, Mt.
27  */
28 typedef struct {
29   xbt_thread_t listener;
30 } *fake_gras_msg_listener_t;
31 typedef struct {
32   smx_process_t s_process;
33 } *fake_xbt_thread_t;
34
35 int gras_socket_im_the_server(xbt_socket_t sock)
36 {
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   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
46   if (sock_data->server == SIMIX_process_self()) {
47     XBT_VERB("I am the server");
48     return 1;
49   }
50   if (sock_data->client == SIMIX_process_self()) {
51     XBT_VERB("I am the client");
52     return 0;
53   }
54   XBT_VERB("I am neither the client nor the server, probably a listener");
55
56   /* neither the client nor the server. Check their respective listeners */
57   pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->server));
58   l = pd->listener;
59   if (l) {
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       XBT_VERB("I am the listener of the server");
64       return 1;
65     }
66   }
67
68   if (sock_data->client) {
69     pd = ((gras_procdata_t*)SIMIX_process_get_data(sock_data->client));
70     l = pd->listener;
71     if (l) {
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         XBT_VERB("I am the listener of the client");
76         return 0;
77       }
78     }
79   }
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   XBT_INFO("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   XBT_INFO("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");
94
95 }
96
97 gras_msg_t gras_msg_recv_any(void)
98 {
99   gras_trp_procdata_t trp_proc =
100       (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
101   gras_msg_t msg;
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;
105   int got = 0;
106   xbt_socket_t sock = NULL;
107   gras_trp_sg_sock_data_t sock_data;
108   xbt_dynar_foreach(trp_proc->sockets, cursor, sock) {
109     sock_data = (gras_trp_sg_sock_data_t) sock->data;
110
111
112     XBT_DEBUG
113         ("Consider socket %p (data:%p; Here rdv: %p; Remote rdv: %p; Comm %p) to get a message",
114          sock, sock_data,
115          gras_socket_im_the_server(sock)?
116              sock_data->rdv_server : sock_data->rdv_client,
117          gras_socket_im_the_server(sock)?
118              sock_data->rdv_client : sock_data->rdv_server,
119          sock_data->comm_recv);
120
121
122     /* If the following assert fails in some valid conditions, we need to
123      * change the code downward looking for the socket again.
124      *
125      * For now it relies on the facts (A) that sockets and comms are aligned
126      *                                (B) every sockets has a posted irecv in comms
127      *
128      * This is not trivial because we need that alignment to hold after the waitany(), so
129      * after other processes get scheduled.
130      *
131      * I cannot think of conditions where they get desynchronized (A violated) as long as
132      *    1) only the listener calls that function
133      *    2) Nobody but the listener removes sockets from that set (in main listener loop)
134      *    3) New sockets are added at the end, and signified ASAP to the listener (by awaking him)
135      * The throw bellow ensures that B is never violated without failing out loudly.
136      *
137      * We cannot search by comparing the comm object pointer that object got
138      *    freed by the waiting process (down in smx_network, in
139      *    comm_wait_for_completion or comm_cleanup). So, actually, we could
140      *    use that pointer since that's a dangling pointer, but no one changes it.
141      * I still feel unconfortable with using dangling pointers, even if that would
142      *    let the code work even if A and/or B are violated, provided that
143      *    (C) the new irecv is never posted before we return from waitany to that function.
144      *
145      * Another approach, robust to B violation would be to retraverse the socks dynar with
146      *    an iterator, incremented only when the socket has a comm. And we've the right socket
147      *    when that iterator is equal to "got", the result of waitany. Not needed if B holds.
148      */
149     xbt_assert(sock_data->comm_recv,
150                 "Comm_recv of socket %p is empty; please report that nasty bug",
151                 sock);
152     /* End of paranoia */
153
154     XBT_VERB("Consider receiving messages from on comm_recv %p rdv:%p (other rdv:%p)",
155           sock_data->comm_recv,
156           gras_socket_im_the_server(sock)?
157               sock_data->rdv_server : sock_data->rdv_client,
158           gras_socket_im_the_server(sock)?
159               sock_data->rdv_client : sock_data->rdv_server);
160     xbt_dynar_push(comms, &(sock_data->comm_recv));
161   }
162   XBT_VERB("Wait on %ld 'sockets'", xbt_dynar_length(comms));
163   /* Wait for the end of any of these communications */
164   got = simcall_comm_waitany(comms);
165
166   /* retrieve the message sent in that communication */
167   sock = xbt_dynar_get_as(trp_proc->sockets, got, xbt_socket_t);
168   sock_data = (gras_trp_sg_sock_data_t) sock->data;
169   msg = sock_data->msg;
170   XBT_VERB("Got something. Communication over rdv_server=%p, rdv_client=%p",
171       sock_data->rdv_server,sock_data->rdv_client);
172
173   /* Reinstall a waiting communication on that rdv */
174 /*  xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
175     sock_data = (gras_trp_sg_sock_data_t) sock->data;
176     if (sock_data->comm_recv && sock_data->comm_recv == comm)
177       break;
178   }
179   */
180   sock_data->comm_recv =
181       simcall_comm_irecv(gras_socket_im_the_server(sock) ?
182                           sock_data->rdv_server : sock_data->rdv_client,
183                           &sock_data->msg, NULL, NULL, NULL);
184
185   return msg;
186 }
187
188
189 void gras_msg_send_ext(xbt_socket_t sock,
190                        e_gras_msg_kind_t kind,
191                        unsigned long int ID,
192                        gras_msgtype_t msgtype, void *payload)
193 {
194   int whole_payload_size = 0;   /* msg->payload_size is used to memcpy the payload.
195                                    This is used to report the load onto the simulator. It also counts the size of pointed stuff */
196   gras_msg_t msg;               /* message to send */
197   smx_action_t comm;
198   gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
199
200   smx_rdv_t target_rdv =
201       (sock_data->server == SIMIX_process_self())?
202           sock_data->rdv_client :
203           sock_data->rdv_server;
204
205   /*initialize gras message */
206   msg = xbt_new(s_gras_msg_t, 1);
207   sock->refcount++;
208   msg->expe = sock;
209   msg->kind = kind;
210   msg->type = msgtype;
211   msg->ID = ID;
212   XBT_PUBLIC(xbt_datadesc_type_t) xbt_datadesc_by_id(long int code);
213
214   /* to debug */
215   XBT_PUBLIC(void) xbt_datadesc_type_dump(const xbt_datadesc_type_t ddt);
216   XBT_PUBLIC(const char *) xbt_datadesc_arch_name(int code);
217
218   /* compare two data type description */
219   XBT_PUBLIC(int)
220   xbt_datadesc_type_cmp(const xbt_datadesc_type_t d1,
221                          const xbt_datadesc_type_t d2);
222
223   /* Access function */
224   XBT_PUBLIC(int) xbt_datadesc_size(xbt_datadesc_type_t type);
225   /* Described data exchanges: direct use */
226   XBT_PUBLIC(int) xbt_datadesc_memcpy(xbt_datadesc_type_t type, void *src,
227                                        void *dst);
228   XBT_PUBLIC(void) xbt_datadesc_send(xbt_socket_t sock,
229                                       xbt_datadesc_type_t type, void *src);
230   XBT_PUBLIC(void) xbt_datadesc_recv(xbt_socket_t sock,
231                                       xbt_datadesc_type_t type, int r_arch,
232                                       void *dst);
233
234   /* Described data exchanges: IDL compilation FIXME: not implemented*/
235   void xbt_datadesc_gen_cpy(xbt_datadesc_type_t type, void *src,
236                              void **dst);
237   void xbt_datadesc_gen_send(xbt_socket_t sock,
238                               xbt_datadesc_type_t type, void *src);
239   void xbt_datadesc_gen_recv(xbt_socket_t sock,
240                               xbt_datadesc_type_t type, int r_arch,
241                               void *dst);
242
243
244   XBT_VERB("Send msg %s (%s) to rdv %p sock %p",
245       msgtype->name,  e_gras_msg_kind_names[kind], target_rdv, sock);
246
247   if (kind == e_gras_msg_kind_rpcerror) {
248     /* error on remote host, careful, payload is an exception */
249     msg->payl_size = xbt_datadesc_size(xbt_datadesc_by_name("ex_t"));
250     msg->payl = xbt_malloc(msg->payl_size);
251     whole_payload_size =
252         xbt_datadesc_memcpy(xbt_datadesc_by_name("ex_t"), payload,
253                              msg->payl);
254   } else if (kind == e_gras_msg_kind_rpcanswer) {
255     msg->payl_size = xbt_datadesc_size(msgtype->answer_type);
256     if (msg->payl_size)
257       msg->payl = xbt_malloc(msg->payl_size);
258     else
259       msg->payl = NULL;
260
261     if (msgtype->answer_type)
262       whole_payload_size = xbt_datadesc_memcpy(msgtype->answer_type,
263                                                 payload, msg->payl);
264   } else {
265     msg->payl_size = xbt_datadesc_size(msgtype->ctn_type);
266     msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
267     if (msgtype->ctn_type)
268       whole_payload_size = xbt_datadesc_memcpy(msgtype->ctn_type,
269                                                 payload, msg->payl);
270   }
271
272   comm = simcall_comm_isend(target_rdv, whole_payload_size, -1, msg, sizeof(void *), NULL,NULL, msg, 0);
273   simcall_comm_wait(comm, -1);
274
275   XBT_VERB("Message sent (and received)");
276
277 }