Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
temporarly reduce the example size until it gets debugged in SG
[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 (sockdata->rdv_client == NULL) /* Master socket, I'm server */
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 (sockdata->server == SIMIX_process_self())
86     return sockdata->client_port;
87   else
88     return sockdata->server_port;
89 }
90 static const char* gras_trp_sg_peer_name(gras_socket_t s) {
91   gras_trp_sg_sock_data_t sockdata = s->data;
92   if (sockdata->server == SIMIX_process_self())
93     return SIMIX_host_get_name(SIMIX_process_get_host(sockdata->client));
94   else
95     return SIMIX_host_get_name(SIMIX_process_get_host(sockdata->server));
96 }
97 static const char* gras_trp_sg_peer_proc(gras_socket_t s) {
98   THROW_UNIMPLEMENTED;
99 }
100 static void gras_trp_sg_peer_proc_set(gras_socket_t s,char *name) {
101   THROW_UNIMPLEMENTED;
102 }
103
104 void gras_trp_sg_setup(gras_trp_plugin_t plug)
105 {
106
107   plug->my_port = gras_trp_sg_my_port;
108   plug->peer_port = gras_trp_sg_peer_port;
109   plug->peer_name = gras_trp_sg_peer_name;
110   plug->peer_proc = gras_trp_sg_peer_proc;
111   plug->peer_proc_set = gras_trp_sg_peer_proc_set;
112
113   gras_trp_sg_plug_data_t *data = xbt_new(gras_trp_sg_plug_data_t, 1);
114
115   plug->data = data;
116
117   plug->socket_client = gras_trp_sg_socket_client;
118   plug->socket_server = gras_trp_sg_socket_server;
119   plug->socket_close = gras_trp_sg_socket_close;
120
121   plug->raw_send = gras_trp_sg_chunk_send_raw;
122   plug->send = gras_trp_sg_chunk_send;
123   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
124
125   plug->flush = NULL;           /* nothing cached */
126 }
127
128 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
129                                const char*host,
130                                int port,
131                                /* OUT */ gras_socket_t sock)
132 {
133
134   smx_host_t peer;
135   gras_hostdata_t *hd;
136   gras_trp_sg_sock_data_t data;
137   gras_sg_portrec_t pr;
138
139   /* make sure this socket will reach someone */
140   if (!(peer = SIMIX_host_get_by_name(host)))
141     THROW1(mismatch_error, 0,
142            "Can't connect to %s: no such host.\n", host);
143
144   if (!(hd = (gras_hostdata_t *) SIMIX_host_get_data(peer)))
145     THROW1(mismatch_error, 0,
146            "can't connect to %s: no process on this host",
147            host);
148
149   pr = find_port(hd, port);
150
151   if (pr == NULL) {
152     THROW2(mismatch_error, 0,
153            "can't connect to %s:%d, no process listen on this port",
154            host, port);
155   }
156
157   /* Ensure that the listener is expecting the kind of stuff we want to send */
158   if (pr->meas && !sock->meas) {
159     THROW2(mismatch_error, 0,
160            "can't connect to %s:%d in regular mode, the process listen "
161            "in measurement mode on this port", host,
162            port);
163   }
164   if (!pr->meas && sock->meas) {
165     THROW2(mismatch_error, 0,
166            "can't connect to %s:%d in measurement mode, the process listen "
167            "in regular mode on this port", host,
168            port);
169   }
170
171   /* create simulation data of the socket */
172   data = xbt_new(s_gras_trp_sg_sock_data_t, 1);
173   data->client = SIMIX_process_self();
174   data->server = pr->server;
175   data->server_port = port;
176   data->client_port = gras_os_myport();
177
178   /* initialize synchronization stuff on the socket */
179   data->rdv_server = pr->rdv;
180   data->rdv_client = SIMIX_rdv_create(NULL);
181   data->comm_recv = SIMIX_network_irecv(data->rdv_client, NULL, 0);
182
183   /* connect that simulation data to the socket */
184   sock->data = data;
185   sock->incoming = 1;
186
187   DEBUG8("%s (PID %d) connects in %s mode to %s:%d (rdv_ser:%p, rdv_cli:%p, comm:%p)",
188          SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
189          sock->meas ? "meas" : "regular", host, port,
190          data->rdv_server,data->rdv_client,data->comm_recv);
191 }
192
193 void gras_trp_sg_socket_server(gras_trp_plugin_t self, int port, gras_socket_t sock)
194 {
195
196   gras_hostdata_t *hd =
197       (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
198   gras_sg_portrec_t pr;
199   gras_trp_sg_sock_data_t data;
200
201   xbt_assert0(hd, "Please run gras_process_init on each process");
202
203   sock->accepting = 1;
204
205   /* Check whether a server is already listening on that port or not */
206   pr = find_port(hd, port);
207
208   if (pr)
209     THROW2(mismatch_error, 0,
210            "can't listen on address %s:%d: port already in use.",
211            SIMIX_host_get_name(SIMIX_host_self()), port);
212
213   /* This port is free, let's take it */
214   pr = xbt_new(s_gras_sg_portrec_t, 1);
215   pr->port = port;
216   pr->meas = sock->meas;
217   pr->server = SIMIX_process_self();
218   pr->rdv = SIMIX_rdv_create(NULL);
219   xbt_dynar_push(hd->ports, &pr);
220
221   /* Create the socket */
222   data = xbt_new(s_gras_trp_sg_sock_data_t, 1);
223   data->server = SIMIX_process_self();
224   data->server_port = port;
225   data->client = NULL;
226   data->rdv_server = pr->rdv;
227   data->rdv_client = NULL;
228   data->comm_recv = SIMIX_network_irecv(pr->rdv, NULL, 0);
229
230   sock->data = data;
231
232   VERB10
233       ("'%s' (%d) ears on %s:%d%s (%p; data:%p); Here rdv: %p; Remote rdv: %p; Comm %p",
234        SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
235        SIMIX_host_get_name(SIMIX_host_self()), port,
236        sock->meas ? " (mode meas)" : "", sock, data,
237        (data->server ==
238         SIMIX_process_self())? data->rdv_server : data->rdv_client,
239        (data->server ==
240         SIMIX_process_self())? data->rdv_client : data->rdv_server,
241        data->comm_recv);
242
243 }
244
245 void gras_trp_sg_socket_close(gras_socket_t sock)
246 {
247   gras_hostdata_t *hd =
248       (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
249   unsigned int cpt;
250   gras_sg_portrec_t pr;
251
252   XBT_IN1(" (sock=%p)", sock);
253
254   if (!sock)
255     return;
256
257   xbt_assert0(hd, "Please run gras_process_init on each process");
258
259   gras_trp_sg_sock_data_t sockdata = sock->data;
260
261   if (sock->incoming && !sock->outgoing && sockdata->server_port >= 0) {
262     /* server mode socket. Unregister it from 'OS' tables */
263     xbt_dynar_foreach(hd->ports, cpt, pr) {
264       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
265       if (pr->port == sockdata->server_port) {
266         xbt_dynar_cursor_rm(hd->ports, &cpt);
267         XBT_OUT;
268         return;
269       }
270     }
271     WARN2
272         ("socket_close called on the unknown incoming socket %p (port=%d)",
273          sock, sockdata->server_port);
274   }
275   if (sock->data) {
276     /* FIXME: kill the rdv point if receiver side */
277     free(sock->data);
278   }
279   XBT_OUT;
280 }
281
282 typedef struct {
283   int size;
284   void *data;
285 } sg_task_data_t;
286
287 void gras_trp_sg_chunk_send(gras_socket_t sock,
288                             const char *data,
289                             unsigned long int size, int stable_ignored)
290 {
291   gras_trp_sg_chunk_send_raw(sock, data, size);
292 }
293
294 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
295                                 const char *data, unsigned long int size)
296 {
297 #ifdef KILLME
298   char name[256];
299   static unsigned int count = 0;
300
301   smx_action_t act;             /* simix action */
302   gras_trp_procdata_t trp_remote_proc;
303   gras_msg_procdata_t msg_remote_proc;
304   gras_msg_t msg;               /* message to send */
305
306   //gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
307   xbt_assert0(sock->meas,
308               "SG chunk exchange shouldn't be used on non-measurement sockets");
309
310
311   /* creates simix action and waits its ends, waits in the sender host
312      condition */
313   /*
314   if (XBT_LOG_ISENABLED(gras_trp_sg, xbt_log_priority_debug)) {
315     smx_process_t remote_dude =
316         (sock_data->server ==
317          SIMIX_process_self())? (sock_data->client) : (sock_data->server);
318     smx_host_t remote_host = SIMIX_process_get_host(remote_dude);
319   }
320   */
321   //SIMIX_network_send(sock_data->rdv,size,1,-1,NULL,0,NULL,NULL);
322 #endif
323   THROW_UNIMPLEMENTED;
324 }
325
326 int gras_trp_sg_chunk_recv(gras_socket_t sock,
327                            char *data, unsigned long int size)
328 {
329   //gras_trp_sg_sock_data_t *sock_data =
330   //    (gras_trp_sg_sock_data_t *) sock->data;
331
332   //SIMIX_network_recv(sock_data->rdv,-1,NULL,0,NULL);
333   THROW_UNIMPLEMENTED;
334 #ifdef KILLME
335   gras_trp_sg_sock_data_t *remote_sock_data;
336   gras_socket_t remote_socket = NULL;
337   gras_msg_t msg_got;
338   gras_msg_procdata_t msg_procdata =
339       (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
340   gras_trp_procdata_t trp_proc =
341       (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
342
343   xbt_assert0(sock->meas,
344               "SG chunk exchange shouldn't be used on non-measurement sockets");
345   xbt_queue_shift_timed(trp_proc->meas_selectable_sockets,
346                         &remote_socket, 60);
347
348   if (remote_socket == NULL) {
349     THROW0(timeout_error, 0, "Timeout");
350   }
351
352   remote_sock_data = (gras_trp_sg_sock_data_t *) remote_socket->data;
353   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue_meas);
354
355   sock_data = (gras_trp_sg_sock_data_t *) sock->data;
356
357   /* ok, I'm here, you can continue the communication */
358   SIMIX_cond_signal(remote_sock_data->cond);
359
360   SIMIX_mutex_lock(remote_sock_data->mutex);
361   /* wait for communication end */
362   SIMIX_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
363
364   if (msg_got->payl_size != size)
365     THROW5(mismatch_error, 0,
366            "Got %d bytes when %ld where expected (in %s->%s:%d)",
367            msg_got->payl_size, size,
368            SIMIX_host_get_name(sock_data->to_host),
369            SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
370
371   if (data)
372     memcpy(data, msg_got->payl, size);
373
374   if (msg_got->payl)
375     xbt_free(msg_got->payl);
376
377   xbt_free(msg_got);
378   SIMIX_mutex_unlock(remote_sock_data->mutex);
379 #endif
380   return 0;
381 }