Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'smpi'
[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   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   gras_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 = SIMIX_req_comm_waitany(comms);
165
166   /* retrieve the message sent in that communication */
167   sock = xbt_dynar_get_as(trp_proc->sockets, got, gras_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       SIMIX_req_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(gras_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
213   XBT_VERB("Send msg %s (%s) to rdv %p sock %p",
214       msgtype->name,  e_gras_msg_kind_names[kind], target_rdv, sock);
215
216   if (kind == e_gras_msg_kind_rpcerror) {
217     /* error on remote host, careful, payload is an exception */
218     msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
219     msg->payl = xbt_malloc(msg->payl_size);
220     whole_payload_size =
221         gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"), payload,
222                              msg->payl);
223   } else if (kind == e_gras_msg_kind_rpcanswer) {
224     msg->payl_size = gras_datadesc_size(msgtype->answer_type);
225     if (msg->payl_size)
226       msg->payl = xbt_malloc(msg->payl_size);
227     else
228       msg->payl = NULL;
229
230     if (msgtype->answer_type)
231       whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
232                                                 payload, msg->payl);
233   } else {
234     msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
235     msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
236     if (msgtype->ctn_type)
237       whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
238                                                 payload, msg->payl);
239   }
240
241   comm = SIMIX_req_comm_isend(target_rdv, whole_payload_size, -1, msg, sizeof(void *), NULL,NULL, msg, 0);
242   SIMIX_req_comm_wait(comm, -1);
243
244   XBT_VERB("Message sent (and received)");
245
246 }