Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new style logging macros.
[simgrid.git] / src / gras / Transport / transport_plugin_sg.c
1 /* file trp (transport) - send/receive a bunch of bytes in SG realm         */
2
3 /* Note that this is only used to debug other parts of GRAS since message   */
4 /*  exchange in SG realm is implemented directly without mimicing real life */
5 /*  This would be terribly unefficient.                                     */
6
7 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "xbt/ex.h"
14
15 #include "simix/simix.h"
16 #include "gras/Msg/msg_private.h"
17 #include "gras/Transport/transport_private.h"
18 #include "gras/Virtu/virtu_sg.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg, gras_trp,
21                                 "SimGrid pseudo-transport");
22
23 /***
24  *** Prototypes 
25  ***/
26
27 /* retrieve the port record associated to a numerical port on an host */
28 static gras_sg_portrec_t find_port(gras_hostdata_t * hd, int port);
29
30 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
31                                const char*host,
32                                int port,
33                                /* OUT */ gras_socket_t sock);
34 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
35                                int port,
36                                /* OUT */ gras_socket_t sock);
37 void gras_trp_sg_socket_close(gras_socket_t sd);
38
39 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
40                                 const char *data, unsigned long int size);
41 void gras_trp_sg_chunk_send(gras_socket_t sd,
42                             const char *data,
43                             unsigned long int size, int stable_ignored);
44
45 int gras_trp_sg_chunk_recv(gras_socket_t sd,
46                            char *data, unsigned long int size);
47
48 /***
49  *** Specific plugin part
50  ***/
51 typedef struct {
52   int placeholder;              /* nothing plugin specific so far */
53 } gras_trp_sg_plug_data_t;
54
55
56 /***
57  *** Code
58  ***/
59 static gras_sg_portrec_t find_port(gras_hostdata_t * hd, int port)
60 {
61   unsigned int cpt;
62   gras_sg_portrec_t pr;
63
64   xbt_assert0(hd, "Please run gras_process_init on each process");
65
66   xbt_dynar_foreach(hd->ports, cpt, pr) {
67     if (pr->port == port)
68       return pr;
69   }
70   return NULL;
71 }
72
73 /***
74  *** Info about who's speaking
75  ***/
76 static int gras_trp_sg_my_port(gras_socket_t s) {
77   gras_trp_sg_sock_data_t sockdata = s->data;
78   if (gras_socket_im_the_server(s))
79     return sockdata->server_port;
80   else
81     return sockdata->client_port;
82 }
83 static int gras_trp_sg_peer_port(gras_socket_t s) {
84   gras_trp_sg_sock_data_t sockdata = s->data;
85   if (gras_socket_im_the_server(s))
86     return sockdata->client_port;
87   else
88     return sockdata->server_port;
89 }
90
91 static const char* gras_trp_sg_peer_name(gras_socket_t s) {
92   gras_trp_sg_sock_data_t sockdata = s->data;
93   if (gras_socket_im_the_server(s))
94     return SIMIX_host_get_name(SIMIX_req_process_get_host(sockdata->client));
95   else {
96     return SIMIX_host_get_name(SIMIX_req_process_get_host(sockdata->server));
97   }
98 }
99 static const char* gras_trp_sg_peer_proc(gras_socket_t s) {
100   THROW_UNIMPLEMENTED;
101 }
102 static void gras_trp_sg_peer_proc_set(gras_socket_t s,char *name) {
103   THROW_UNIMPLEMENTED;
104 }
105
106 void gras_trp_sg_setup(gras_trp_plugin_t plug)
107 {
108
109   plug->my_port = gras_trp_sg_my_port;
110   plug->peer_port = gras_trp_sg_peer_port;
111   plug->peer_name = gras_trp_sg_peer_name;
112   plug->peer_proc = gras_trp_sg_peer_proc;
113   plug->peer_proc_set = gras_trp_sg_peer_proc_set;
114
115   gras_trp_sg_plug_data_t *data = xbt_new(gras_trp_sg_plug_data_t, 1);
116
117   plug->data = data;
118
119   plug->socket_client = gras_trp_sg_socket_client;
120   plug->socket_server = gras_trp_sg_socket_server;
121   plug->socket_close = gras_trp_sg_socket_close;
122
123   plug->raw_send = gras_trp_sg_chunk_send_raw;
124   plug->send = gras_trp_sg_chunk_send;
125   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
126
127   plug->flush = NULL;           /* nothing cached */
128 }
129
130 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
131                                const char*host,
132                                int port,
133                                /* OUT */ gras_socket_t sock)
134 {
135
136   smx_host_t peer;
137   gras_hostdata_t *hd;
138   gras_trp_sg_sock_data_t data;
139   gras_sg_portrec_t pr;
140
141   /* make sure this socket will reach someone */
142   if (!(peer = SIMIX_req_host_get_by_name(host)))
143     THROW1(mismatch_error, 0,
144            "Can't connect to %s: no such host.\n", host);
145
146   if (!(hd = (gras_hostdata_t *) SIMIX_req_host_get_data(peer)))
147     THROW1(mismatch_error, 0,
148            "can't connect to %s: no process on this host",
149            host);
150
151   pr = find_port(hd, port);
152
153   if (pr == NULL) {
154     THROW2(mismatch_error, 0,
155            "can't connect to %s:%d, no process listen on this port",
156            host, port);
157   }
158
159   /* Ensure that the listener is expecting the kind of stuff we want to send */
160   if (pr->meas && !sock->meas) {
161     THROW2(mismatch_error, 0,
162            "can't connect to %s:%d in regular mode, the process listen "
163            "in measurement mode on this port", host,
164            port);
165   }
166   if (!pr->meas && sock->meas) {
167     THROW2(mismatch_error, 0,
168            "can't connect to %s:%d in measurement mode, the process listen "
169            "in regular mode on this port", host,
170            port);
171   }
172
173   /* create simulation data of the socket */
174   data = xbt_new0(s_gras_trp_sg_sock_data_t, 1);
175   data->client = SIMIX_process_self();
176   data->server = pr->server;
177   data->server_port = port;
178   data->client_port = gras_os_myport();
179
180   /* initialize synchronization stuff on the socket */
181   data->rdv_server = pr->rdv;
182   data->rdv_client = SIMIX_req_rdv_create(NULL);
183   data->comm_recv = SIMIX_req_comm_irecv(data->rdv_client, &data->msg, NULL, NULL, NULL);
184
185   /* connect that simulation data to the socket */
186   sock->data = data;
187   sock->incoming = 1;
188
189   XBT_DEBUG("%s (PID %d) connects in %s mode to %s:%d (rdv_ser:%p, rdv_cli:%p, comm:%p)",
190          SIMIX_req_process_get_name(SIMIX_process_self()), gras_os_getpid(),
191          sock->meas ? "meas" : "regular", host, port,
192          data->rdv_server,data->rdv_client,data->comm_recv);
193 }
194
195 void gras_trp_sg_socket_server(gras_trp_plugin_t self, int port, gras_socket_t sock)
196 {
197
198   gras_hostdata_t *hd =
199       (gras_hostdata_t *) SIMIX_host_self_get_data();
200   gras_sg_portrec_t pr;
201   gras_trp_sg_sock_data_t data;
202
203   xbt_assert0(hd, "Please run gras_process_init on each process");
204
205   sock->accepting = 1;
206
207   /* Check whether a server is already listening on that port or not */
208   pr = find_port(hd, port);
209
210   if (pr)
211     THROW2(mismatch_error, 0,
212            "can't listen on address %s:%d: port already in use.",
213            SIMIX_host_self_get_name(), port);
214
215   /* This port is free, let's take it */
216   pr = xbt_new(s_gras_sg_portrec_t, 1);
217   pr->port = port;
218   pr->meas = sock->meas;
219   pr->server = SIMIX_process_self();
220   xbt_dynar_push(hd->ports, &pr);
221   pr->rdv = SIMIX_req_rdv_create(NULL);
222
223   /* Create the socket */
224   data = xbt_new0(s_gras_trp_sg_sock_data_t, 1);
225   data->server = SIMIX_process_self();
226   data->server_port = port;
227   data->client = NULL;
228   data->rdv_server = pr->rdv;
229   data->rdv_client = NULL;
230   data->comm_recv = SIMIX_req_comm_irecv(pr->rdv, &data->msg, NULL, NULL, NULL);
231
232   sock->data = data;
233
234   XBT_VERB
235       ("'%s' (%d) ears on %s:%d%s (%p; data:%p); Here rdv: %p; Remote rdv: %p; Comm %p",
236        SIMIX_req_process_get_name(SIMIX_process_self()), gras_os_getpid(),
237        SIMIX_host_self_get_name(), port,
238        sock->meas ? " (mode meas)" : "", sock, data,
239        (data->server ==
240         SIMIX_process_self())? data->rdv_server : data->rdv_client,
241        (data->server ==
242         SIMIX_process_self())? data->rdv_client : data->rdv_server,
243        data->comm_recv);
244
245 }
246
247 void gras_trp_sg_socket_close(gras_socket_t sock)
248 {
249   gras_hostdata_t *hd =
250       (gras_hostdata_t *) SIMIX_host_self_get_data();
251   unsigned int cpt;
252   gras_sg_portrec_t pr;
253
254   XBT_IN_F(" (sock=%p)", sock);
255
256   if (!sock)
257     return;
258
259   xbt_assert0(hd, "Please run gras_process_init on each process");
260
261   gras_trp_sg_sock_data_t sockdata = sock->data;
262
263   if (sock->incoming && !sock->outgoing && sockdata->server_port >= 0) {
264     /* server mode socket. Unregister it from 'OS' tables */
265     xbt_dynar_foreach(hd->ports, cpt, pr) {
266       XBT_DEBUG("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
267       if (pr->port == sockdata->server_port) {
268         xbt_dynar_cursor_rm(hd->ports, &cpt);
269         XBT_OUT;
270         return;
271       }
272     }
273     XBT_WARN
274         ("socket_close called on the unknown incoming socket %p (port=%d)",
275          sock, sockdata->server_port);
276   }
277   if (sock->data) {
278     /* FIXME: kill the rdv point if receiver side */
279     free(sock->data);
280   }
281   XBT_OUT;
282 }
283
284 typedef struct {
285   int size;
286   void *data;
287 } sg_task_data_t;
288
289 void gras_trp_sg_chunk_send(gras_socket_t sock,
290                             const char *data,
291                             unsigned long int size, int stable_ignored)
292 {
293   gras_trp_sg_chunk_send_raw(sock, data, size);
294 }
295
296 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
297                                 const char *data, unsigned long int size)
298 {
299 #ifdef KILLME
300   char name[256];
301   static unsigned int count = 0;
302
303   smx_action_t act;             /* simix action */
304   gras_trp_procdata_t trp_remote_proc;
305   gras_msg_procdata_t msg_remote_proc;
306   gras_msg_t msg;               /* message to send */
307
308   //gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
309   xbt_assert0(sock->meas,
310               "SG chunk exchange shouldn't be used on non-measurement sockets");
311
312
313   /* creates simix action and waits its ends, waits in the sender host
314      condition */
315   /*
316   if (XBT_LOG_ISENABLED(gras_trp_sg, xbt_log_priority_debug)) {
317     smx_process_t remote_dude =
318         (sock_data->server ==
319          SIMIX_process_self())? (sock_data->client) : (sock_data->server);
320     smx_host_t remote_host = SIMIX_req_process_get_host(remote_dude);
321   }
322   */
323   //SIMIX_network_send(sock_data->rdv,size,1,-1,NULL,0,NULL,NULL);
324 #endif
325   THROW_UNIMPLEMENTED;
326 }
327
328 int gras_trp_sg_chunk_recv(gras_socket_t sock,
329                            char *data, unsigned long int size)
330 {
331   //gras_trp_sg_sock_data_t *sock_data =
332   //    (gras_trp_sg_sock_data_t *) sock->data;
333
334   //SIMIX_req_comm_recv(sock_data->rdv,-1,NULL,0,NULL);
335   THROW_UNIMPLEMENTED;
336 #ifdef KILLME
337   gras_trp_sg_sock_data_t *remote_sock_data;
338   gras_socket_t remote_socket = NULL;
339   gras_msg_t msg_got;
340   gras_msg_procdata_t msg_procdata =
341       (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
342   gras_trp_procdata_t trp_proc =
343       (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
344
345   xbt_assert0(sock->meas,
346               "SG chunk exchange shouldn't be used on non-measurement sockets");
347   xbt_queue_shift_timed(trp_proc->meas_selectable_sockets,
348                         &remote_socket, 60);
349
350   if (remote_socket == NULL) {
351     THROW0(timeout_error, 0, "Timeout");
352   }
353
354   remote_sock_data = (gras_trp_sg_sock_data_t *) remote_socket->data;
355   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue_meas);
356
357   sock_data = (gras_trp_sg_sock_data_t *) sock->data;
358
359   /* ok, I'm here, you can continue the communication */
360   SIMIX_req_cond_signal(remote_sock_data->cond);
361
362   SIMIX_req_mutex_lock(remote_sock_data->mutex);
363   /* wait for communication end */
364   SIMIX_req_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
365
366   if (msg_got->payl_size != size)
367     THROW5(mismatch_error, 0,
368            "Got %d bytes when %ld where expected (in %s->%s:%d)",
369            msg_got->payl_size, size,
370            SIMIX_req_host_get_name(sock_data->to_host),
371            SIMIX_host_self_get_name(), sock->peer_port);
372
373   if (data)
374     memcpy(data, msg_got->payl, size);
375
376   if (msg_got->payl)
377     xbt_free(msg_got->payl);
378
379   xbt_free(msg_got);
380   SIMIX_req_mutex_unlock(remote_sock_data->mutex);
381 #endif
382   return 0;
383 }