Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
If it is a win plateform we have to use wenvironment against environment.
[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 "gras/Msg/msg_private.h"
16 #include "gras/Transport/transport_private.h"
17 #include "gras/Virtu/virtu_sg.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg, gras_trp,
20                                 "SimGrid pseudo-transport");
21
22 /***
23  *** Prototypes 
24  ***/
25
26 /* retrieve the port record associated to a numerical port on an host */
27 static void find_port(gras_hostdata_t * hd, int port,
28                       gras_sg_portrec_t * hpd);
29
30
31 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
32                                /* OUT */ gras_socket_t sock);
33 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
34                                /* OUT */ gras_socket_t sock);
35 void gras_trp_sg_socket_close(gras_socket_t sd);
36
37 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
38                                 const char *data, unsigned long int size);
39 void gras_trp_sg_chunk_send(gras_socket_t sd,
40                             const char *data,
41                             unsigned long int size, int stable_ignored);
42
43 int gras_trp_sg_chunk_recv(gras_socket_t sd,
44                            char *data, unsigned long int size);
45
46 /***
47  *** Specific plugin part
48  ***/
49 typedef struct {
50   int placeholder;              /* nothing plugin specific so far */
51 } gras_trp_sg_plug_data_t;
52
53
54 /***
55  *** Code
56  ***/
57 static void find_port(gras_hostdata_t * hd, int port, gras_sg_portrec_t * hpd)
58 {
59   unsigned int cpt;
60   gras_sg_portrec_t pr;
61
62   xbt_assert0(hd, "Please run gras_process_init on each process");
63
64   xbt_dynar_foreach(hd->ports, cpt, pr) {
65     if (pr.port == port) {
66       memcpy(hpd, &pr, sizeof(gras_sg_portrec_t));
67       return;
68     }
69   }
70   THROW1(mismatch_error, 0, "Unable to find any portrec for port #%d", port);
71 }
72
73
74 void gras_trp_sg_setup(gras_trp_plugin_t plug)
75 {
76
77   gras_trp_sg_plug_data_t *data = xbt_new(gras_trp_sg_plug_data_t, 1);
78
79   plug->data = data;
80
81   plug->socket_client = gras_trp_sg_socket_client;
82   plug->socket_server = gras_trp_sg_socket_server;
83   plug->socket_close = gras_trp_sg_socket_close;
84
85   plug->raw_send = gras_trp_sg_chunk_send_raw;
86   plug->send = gras_trp_sg_chunk_send;
87   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
88
89   plug->flush = NULL;           /* nothing cached */
90 }
91
92 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
93                                /* OUT */ gras_socket_t sock)
94 {
95   xbt_ex_t e;
96
97   smx_host_t peer;
98   gras_hostdata_t *hd;
99   gras_trp_sg_sock_data_t *data;
100   gras_sg_portrec_t pr;
101
102   /* make sure this socket will reach someone */
103   if (!(peer = SIMIX_host_get_by_name(sock->peer_name)))
104     THROW1(mismatch_error, 0,
105            "Can't connect to %s: no such host.\n", sock->peer_name);
106
107   if (!(hd = (gras_hostdata_t *) SIMIX_host_get_data(peer)))
108     THROW1(mismatch_error, 0,
109            "can't connect to %s: no process on this host", sock->peer_name);
110
111   TRY {
112     find_port(hd, sock->peer_port, &pr);
113   }
114   CATCH(e) {
115     if (e.category == mismatch_error) {
116       xbt_ex_free(e);
117       THROW2(mismatch_error, 0,
118              "can't connect to %s:%d, no process listen on this port",
119              sock->peer_name, sock->peer_port);
120     }
121     RETHROW;
122   }
123
124   if (pr.meas && !sock->meas) {
125     THROW2(mismatch_error, 0,
126            "can't connect to %s:%d in regular mode, the process listen "
127            "in measurement mode on this port", sock->peer_name,
128            sock->peer_port);
129   }
130   if (!pr.meas && sock->meas) {
131     THROW2(mismatch_error, 0,
132            "can't connect to %s:%d in measurement mode, the process listen "
133            "in regular mode on this port", sock->peer_name, sock->peer_port);
134   }
135   /* create the socket */
136   data = xbt_new(gras_trp_sg_sock_data_t, 1);
137   data->from_process = SIMIX_process_self();
138   data->to_process = pr.process;
139   data->to_host = peer;
140
141   /* initialize mutex and condition of the socket */
142   data->rdv_server = pr.rdv;
143   data->rdv_client = SIMIX_rdv_create(NULL);
144   data->im_server = 0;
145
146   sock->data = data;
147   sock->incoming = 1;
148
149   DEBUG5("%s (PID %d) connects in %s mode to %s:%d",
150          SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
151          sock->meas ? "meas" : "regular", sock->peer_name, sock->peer_port);
152 }
153
154 void gras_trp_sg_socket_server(gras_trp_plugin_t self, gras_socket_t sock)
155 {
156
157   gras_hostdata_t *hd =
158     (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
159   gras_sg_portrec_t pr;
160   gras_trp_sg_sock_data_t *data;
161   volatile int found;
162
163   const char *host = SIMIX_host_get_name(SIMIX_host_self());
164
165   xbt_ex_t e;
166
167   xbt_assert0(hd, "Please run gras_process_init on each process");
168
169   sock->accepting = 0;          /* no such nuisance in SG */
170   found = 0;
171   TRY {
172     find_port(hd, sock->port, &pr);
173     found = 1;
174   } CATCH(e) {
175     if (e.category == mismatch_error)
176       xbt_ex_free(e);
177     else
178       RETHROW;
179   }
180
181   if (found)
182     THROW2(mismatch_error, 0,
183            "can't listen on address %s:%d: port already in use.",
184            host, sock->port);
185
186   pr.port = sock->port;
187   pr.meas = sock->meas;
188   pr.process = SIMIX_process_self();
189   pr.rdv = SIMIX_rdv_create(NULL);
190   xbt_dynar_push(hd->ports, &pr);
191
192   /* Create the socket */
193   data = xbt_new(gras_trp_sg_sock_data_t, 1);
194   data->from_process = SIMIX_process_self();
195   data->to_process = NULL;
196   data->to_host = SIMIX_host_self();
197   data->rdv_server = pr.rdv;
198   data->rdv_client = NULL;
199   data->im_server = 0;
200   data->comm_recv = SIMIX_network_irecv(pr.rdv,NULL,0);
201   INFO1("Comm %p",data->comm_recv);
202
203   sock->data = data;
204
205   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
206         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
207         host, sock->port, sock->meas ? " (mode meas)" : "", sock);
208
209 }
210
211 void gras_trp_sg_socket_close(gras_socket_t sock)
212 {
213   gras_hostdata_t *hd =
214     (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
215   unsigned int cpt;
216   gras_sg_portrec_t pr;
217
218   XBT_IN1(" (sock=%p)", sock);
219
220   if (!sock)
221     return;
222
223   xbt_assert0(hd, "Please run gras_process_init on each process");
224
225   if (sock->data) {
226     /* FIXME: kill the rdv point if receiver side */
227     free(sock->data);
228   }
229
230   if (sock->incoming && !sock->outgoing && sock->port >= 0) {
231     /* server mode socket. Unregister it from 'OS' tables */
232     xbt_dynar_foreach(hd->ports, cpt, pr) {
233       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
234       if (pr.port == sock->port) {
235         xbt_dynar_cursor_rm(hd->ports, &cpt);
236         XBT_OUT;
237         return;
238       }
239     }
240     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
241           sock, sock->port);
242   }
243   XBT_OUT;
244 }
245
246 typedef struct {
247   int size;
248   void *data;
249 } sg_task_data_t;
250
251 void gras_trp_sg_chunk_send(gras_socket_t sock,
252                             const char *data,
253                             unsigned long int size, int stable_ignored)
254 {
255   gras_trp_sg_chunk_send_raw(sock, data, size);
256 }
257
258 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
259                                 const char *data, unsigned long int size)
260 {
261 #ifdef KILLME
262   char name[256];
263   static unsigned int count = 0;
264
265   smx_action_t act;             /* simix action */
266   gras_trp_procdata_t trp_remote_proc;
267   gras_msg_procdata_t msg_remote_proc;
268   gras_msg_t msg;               /* message to send */
269 #endif
270
271   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *) sock->data;
272   xbt_assert0(sock->meas,
273               "SG chunk exchange shouldn't be used on non-measurement sockets");
274
275
276   /* creates simix action and waits its ends, waits in the sender host
277      condition */
278   DEBUG4("send chunk from %s to  %s:%d (size=%ld)",
279          SIMIX_host_get_name(SIMIX_host_self()),
280          SIMIX_host_get_name(sock_data->to_host), sock->peer_port, size);
281   //SIMIX_network_send(sock_data->rdv,size,1,-1,NULL,0,NULL,NULL);
282   THROW_UNIMPLEMENTED;
283 }
284
285 int gras_trp_sg_chunk_recv(gras_socket_t sock,
286                            char *data, unsigned long int size)
287 {
288   //gras_trp_sg_sock_data_t *sock_data =
289   //    (gras_trp_sg_sock_data_t *) sock->data;
290
291   //SIMIX_network_recv(sock_data->rdv,-1,NULL,0,NULL);
292   THROW_UNIMPLEMENTED;
293 #ifdef KILLME
294   gras_trp_sg_sock_data_t *remote_sock_data;
295   gras_socket_t remote_socket = NULL;
296   gras_msg_t msg_got;
297   gras_msg_procdata_t msg_procdata =
298     (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
299   gras_trp_procdata_t trp_proc =
300     (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
301
302   xbt_assert0(sock->meas,
303               "SG chunk exchange shouldn't be used on non-measurement sockets");
304   xbt_queue_shift_timed(trp_proc->meas_selectable_sockets,
305                         &remote_socket, 60);
306
307   if (remote_socket == NULL) {
308     THROW0(timeout_error, 0, "Timeout");
309   }
310
311   remote_sock_data = (gras_trp_sg_sock_data_t *) remote_socket->data;
312   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue_meas);
313
314   sock_data = (gras_trp_sg_sock_data_t *) sock->data;
315
316   /* ok, I'm here, you can continue the communication */
317   SIMIX_cond_signal(remote_sock_data->cond);
318
319   SIMIX_mutex_lock(remote_sock_data->mutex);
320   /* wait for communication end */
321   SIMIX_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
322
323   if (msg_got->payl_size != size)
324     THROW5(mismatch_error, 0,
325            "Got %d bytes when %ld where expected (in %s->%s:%d)",
326            msg_got->payl_size, size,
327            SIMIX_host_get_name(sock_data->to_host),
328            SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
329
330   if (data)
331     memcpy(data, msg_got->payl, size);
332
333   if (msg_got->payl)
334     xbt_free(msg_got->payl);
335
336   xbt_free(msg_got);
337   SIMIX_mutex_unlock(remote_sock_data->mutex);
338 #endif
339   return 0;
340 }