Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Integrate Bruno's work on SIMIX onto main stream. Tests are broken, but it looks...
[simgrid.git] / src / gras / Transport / transport_plugin_sg.c
1 /* $Id$ */
2
3 /* file trp (transport) - send/receive a bunch of bytes in SG realm         */
4
5 /* Note that this is only used to debug other parts of GRAS since message   */
6 /*  exchange in SG realm is implemented directly without mimicing real life */
7 /*  This would be terribly unefficient.                                     */
8
9 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
10
11 /* This program is free software; you can redistribute it and/or modify it
12  * under the terms of the license (GNU LGPL) which comes with this package. */
13
14 #include "xbt/ex.h" 
15
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,"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, gras_sg_portrec_t *hpd);
28
29
30 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
31                                /* OUT */ gras_socket_t sock);
32 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
33                                /* OUT */ gras_socket_t sock);
34 void gras_trp_sg_socket_close(gras_socket_t sd);
35
36 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
37                                 const char *data,
38                                 unsigned long int size);
39 void gras_trp_sg_chunk_send(gras_socket_t sd,
40                             const char *data,
41                             unsigned long int size,
42                             int stable_ignored);
43
44 int gras_trp_sg_chunk_recv(gras_socket_t sd,
45                             char *data,
46                             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 void find_port(gras_hostdata_t *hd, int port,
60                       gras_sg_portrec_t *hpd) {
61   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       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
69       return;
70     }
71   }
72   THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
73 }
74
75
76 void
77 gras_trp_sg_setup(gras_trp_plugin_t plug) {
78
79   gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
80
81   plug->data      = data; 
82
83   plug->socket_client = gras_trp_sg_socket_client;
84   plug->socket_server = gras_trp_sg_socket_server;
85   plug->socket_close  = gras_trp_sg_socket_close;
86
87   plug->raw_send = gras_trp_sg_chunk_send_raw;
88   plug->send = gras_trp_sg_chunk_send;
89   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
90
91   plug->flush         = NULL; /* nothing cached */
92 }
93
94 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
95                                /* OUT */ gras_socket_t sock){
96   xbt_ex_t e;
97
98   smx_host_t peer;
99   gras_hostdata_t *hd;
100   gras_trp_sg_sock_data_t *data;
101   gras_sg_portrec_t pr;
102
103   /* make sure this socket will reach someone */
104   if (!(peer=SIMIX_host_get_by_name(sock->peer_name))) 
105     THROW1(mismatch_error,0,"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",
110            sock->peer_name);
111   
112   TRY {
113     find_port(hd,sock->peer_port,&pr);
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 meas mode on this port",sock->peer_name,sock->peer_port);
128   }
129   if (!pr.meas && sock->meas) {
130     THROW2(mismatch_error,0,
131            "can't connect to %s:%d in meas mode, the process listen "
132            "in regular mode on this port",sock->peer_name,sock->peer_port);
133   }
134   /* create the socket */
135   data = xbt_new(gras_trp_sg_sock_data_t,1);
136   data->from_process = SIMIX_process_self();
137         data->to_process         = pr.process;
138   data->to_host      = peer;
139
140         /* initialize mutex and condition of the socket */
141         data->mutex = SIMIX_mutex_init();
142         data->cond = SIMIX_cond_init();
143         data->to_socket = pr.socket; 
144
145   sock->data = data;
146   sock->incoming = 1;
147
148   DEBUG5("%s (PID %ld) connects in %s mode to %s:%d",
149           SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
150           sock->meas?"meas":"regular",
151          sock->peer_name,sock->peer_port);
152 }
153
154 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
155                                gras_socket_t sock){
156
157   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
158   gras_sg_portrec_t pr;
159   gras_trp_sg_sock_data_t *data;
160   volatile int found;
161   
162   const char *host=SIMIX_host_get_name(SIMIX_host_self());
163
164   xbt_ex_t e;
165
166   xbt_assert0(hd,"Please run gras_process_init on each process");
167
168   sock->accepting = 0; /* no such nuisance in SG */
169   found = 0;
170   TRY {
171     find_port(hd,sock->port,&pr);
172     found = 1;
173   } CATCH(e) {
174     if (e.category == mismatch_error)
175       xbt_ex_free(e);
176     else
177       RETHROW;
178   }
179    
180   if (found) 
181     THROW2(mismatch_error,0,
182            "can't listen on address %s:%d: port already in use.",
183            host,sock->port);
184
185   pr.port   = sock->port;
186   pr.meas    = sock->meas;
187         pr.socket = sock;
188         pr.process = SIMIX_process_self();
189   xbt_dynar_push(hd->ports,&pr);
190   
191   /* Create the socket */
192   data = xbt_new(gras_trp_sg_sock_data_t,1);
193   data->from_process     = SIMIX_process_self();
194   data->to_process       = NULL;
195         data->to_host      = SIMIX_host_self();
196   
197         data->cond = SIMIX_cond_init();
198         data->mutex = SIMIX_mutex_init();
199
200   sock->data = data;
201
202   VERB6("'%s' (%ld) ears on %s:%d%s (%p)",
203     SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
204     host,sock->port,sock->meas? " (mode meas)":"",sock);
205
206 }
207
208 void gras_trp_sg_socket_close(gras_socket_t sock){
209   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
210   int cpt;
211   gras_sg_portrec_t pr; 
212
213   XBT_IN1(" (sock=%p)",sock);
214    
215   if (!sock) return;
216
217   xbt_assert0(hd,"Please run gras_process_init on each process");
218
219   if (sock->data) {
220         SIMIX_cond_destroy(((gras_trp_sg_sock_data_t*)sock->data)->cond);
221                 SIMIX_mutex_destroy(((gras_trp_sg_sock_data_t*)sock->data)->mutex);
222                 free(sock->data);
223         }
224
225   if (sock->incoming && !sock->outgoing && sock->port >= 0) {
226     /* server mode socket. Unregister it from 'OS' tables */
227     xbt_dynar_foreach(hd->ports, cpt, pr) {
228       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
229       if (pr.port == sock->port) {
230                                 xbt_dynar_cursor_rm(hd->ports, &cpt);
231                                 XBT_OUT;
232                                 return;
233       }
234     }
235     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
236           sock,sock->port);
237   }
238   XBT_OUT;
239
240 }
241
242 typedef struct {
243   int size;
244   void *data;
245 } sg_task_data_t;
246
247 void gras_trp_sg_chunk_send(gras_socket_t sock,
248                             const char *data,
249                             unsigned long int size,
250                             int stable_ignored) {
251   gras_trp_sg_chunk_send_raw(sock,data,size);
252 }
253
254 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
255                                 const char *data,
256                                 unsigned long int size) {
257   char name[256];
258   static unsigned int count=0;
259
260         smx_action_t act; /* simix action */
261         gras_trp_sg_sock_data_t *sock_data; 
262         gras_trp_sg_sock_data_t *remote_sock_data; 
263         gras_hostdata_t *hd;
264         gras_hostdata_t *remote_hd;
265         gras_trp_procdata_t trp_remote_proc;
266         gras_msg_procdata_t msg_remote_proc;
267         gras_msg_t msg; /* message to send */
268
269         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
270         remote_sock_data = ((gras_trp_sg_sock_data_t *)sock->data)->to_socket->data;
271         hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
272         remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
273
274   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
275
276
277   sprintf(name,"Chunk[%d]",count++);
278         /*initialize gras message */
279         msg = xbt_new(s_gras_msg_t,1);
280         msg->expe = sock;
281         msg->payl_size=size;
282
283   if (data) {
284     msg->payl=(void*)xbt_malloc(size);
285     memcpy(msg->payl,data,size);
286   } else {
287     msg->payl = NULL;
288   }
289
290         /* put message on msg_queue */
291         msg_remote_proc = (gras_msg_procdata_t)gras_libdata_by_name_from_remote("gras_msg",sock_data->to_process);
292         xbt_fifo_push(msg_remote_proc->msg_to_receive_queue,msg);
293         
294         /* wake-up the receiver */
295         trp_remote_proc = (gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sock_data->to_process);
296         SIMIX_cond_signal(trp_remote_proc->cond);
297
298         SIMIX_mutex_lock(remote_sock_data->mutex);
299         //SIMIX_mutex_lock(remote_hd->mutex_port[sock->peer_port]);
300         /* wait for the receiver */
301         SIMIX_cond_wait(remote_sock_data->cond,remote_sock_data->mutex);
302         //SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port], remote_hd->mutex_port[sock->peer_port]);
303
304         /* creates simix action and waits its ends, waits in the sender host condition*/
305   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
306          name, SIMIX_host_get_name(SIMIX_host_self()),
307          SIMIX_host_get_name(sock_data->to_host), sock->peer_port,size);
308
309         act = SIMIX_action_communicate(SIMIX_host_self(), sock_data->to_host, name, size, -1);
310         /*
311         SIMIX_register_action_to_condition(act,remote_hd->cond_port[sock->peer_port]);
312         SIMIX_register_condition_to_action(act,remote_hd->cond_port[sock->peer_port]);
313         */
314         SIMIX_register_action_to_condition(act,remote_sock_data->cond);
315         SIMIX_register_condition_to_action(act,remote_sock_data->cond);
316
317         SIMIX_host_get_name(sock_data->to_host),SIMIX_process_get_name(sock_data->to_process),
318         
319         SIMIX_cond_wait(remote_sock_data->cond,remote_sock_data->mutex);
320         //SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port], remote_hd->mutex_port[sock->peer_port]);
321         /* error treatmeant */
322
323         /* cleanup structures */
324         SIMIX_action_destroy(act);
325
326         SIMIX_mutex_unlock(remote_sock_data->mutex);
327         //SIMIX_mutex_unlock(remote_hd->mutex_port[sock->peer_port]);
328         SIMIX_cond_signal(remote_sock_data->cond);
329         //SIMIX_cond_signal(remote_hd->cond_port[sock->peer_port]);
330 }
331
332 int gras_trp_sg_chunk_recv(gras_socket_t sock,
333                             char *data,
334                             unsigned long int size){
335   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
336         gras_trp_sg_sock_data_t *sock_data; 
337         gras_hostdata_t *remote_hd;
338         gras_hostdata_t *local_hd;
339   gras_msg_t msg_got;
340         gras_msg_procdata_t msg_procdata = (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
341
342   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
343         
344         SIMIX_mutex_lock(pd->mutex);
345         if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
346                 SIMIX_cond_wait_timeout(pd->cond,pd->mutex,60);
347         }
348         if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
349                 THROW0(timeout_error,0,"Timeout");
350         }
351         SIMIX_mutex_unlock(pd->mutex);
352
353         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
354         DEBUG3("Remote host %s, Remote Port: %d Local port %d", SIMIX_host_get_name(sock_data->to_host), sock->peer_port, sock->port);
355         remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
356         local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
357
358
359
360   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
361
362         SIMIX_mutex_lock(sock_data->mutex);
363         //SIMIX_mutex_lock(local_hd->mutex_port[sock->port]);
364 /* ok, I'm here, you can continue the communication */
365         SIMIX_cond_signal(sock_data->cond);
366         //SIMIX_cond_signal(local_hd->cond_port[sock->port]);
367
368 /* wait for communication end */
369         SIMIX_cond_wait(sock_data->cond,sock_data->mutex);
370         //SIMIX_cond_wait(local_hd->cond_port[sock->port],local_hd->mutex_port[sock->port]);
371
372
373   if (msg_got->payl_size != size)
374     THROW5(mismatch_error,0,
375            "Got %d bytes when %ld where expected (in %s->%s:%d)",
376            msg_got->payl_size, size,
377            SIMIX_host_get_name(sock_data->to_host),
378            SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
379   if (data) {
380     memcpy(data,msg_got->payl,size);
381         }
382         if (msg_got->payl)
383                 xbt_free(msg_got->payl);        
384         xbt_free(msg_got);
385         SIMIX_cond_wait(sock_data->cond,sock_data->mutex);
386         SIMIX_mutex_unlock(sock_data->mutex);
387         //SIMIX_mutex_unlock(local_hd->mutex_port[sock->port]);
388         /*
389         SIMIX_cond_wait(local_hd->cond_port[sock->port],local_hd->mutex_port[sock->port]);
390         SIMIX_mutex_unlock(local_hd->mutex_port[sock->port]);
391         */
392         return 0;
393 }
394