Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics...
[simgrid.git] / src / gras_simix / Transport / gras_simix_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_simix/Msg/gras_simix_msg_private.h"
17 #include "gras_simix_transport_private.h"
18 #include "gras_simix/Virtu/gras_simix_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_hostdata_t *local_hd;
101   gras_trp_sg_sock_data_t *data;
102   gras_sg_portrec_t pr;
103         int i;
104
105   /* make sure this socket will reach someone */
106   if (!(peer=SIMIX_host_get_by_name(sock->peer_name))) 
107     THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
108
109   if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer))) 
110     THROW1(mismatch_error,0,
111            "can't connect to %s: no process on this host",
112            sock->peer_name);
113   
114   TRY {
115     find_port(hd,sock->peer_port,&pr);
116   } CATCH(e) {
117     if (e.category == mismatch_error) {
118       xbt_ex_free(e);
119       THROW2(mismatch_error,0,
120              "can't connect to %s:%d, no process listen on this port",
121              sock->peer_name,sock->peer_port);
122     } 
123     RETHROW;
124   }
125
126   if (pr.meas && !sock->meas) {
127     THROW2(mismatch_error,0,
128            "can't connect to %s:%d in regular mode, the process listen "
129            "in meas mode on this port",sock->peer_name,sock->peer_port);
130   }
131   if (!pr.meas && sock->meas) {
132     THROW2(mismatch_error,0,
133            "can't connect to %s:%d in meas mode, the process listen "
134            "in regular mode on this port",sock->peer_name,sock->peer_port);
135   }
136   /* create the socket */
137   data = xbt_new(gras_trp_sg_sock_data_t,1);
138   data->from_process = SIMIX_process_self();
139         data->to_process         = pr.process;
140   data->to_host      = peer;
141
142         /* searches for a free port on this host */
143         local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
144         for (i=1;i<65536;i++) {
145                 if (local_hd->cond_port[i] == NULL)
146                         break;
147         }
148         if (i == 65536) THROW0(system_error,0,"No port free");
149         sock->port = i;
150         local_hd->cond_port[i] = SIMIX_cond_init();
151         local_hd->mutex_port[i] = SIMIX_mutex_init();
152
153   sock->data = data;
154   sock->incoming = 1;
155
156   DEBUG5("%s (PID %ld) connects in %s mode to %s:%d",
157           SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
158           sock->meas?"meas":"regular",
159          sock->peer_name,sock->peer_port);
160 }
161
162 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
163                                gras_socket_t sock){
164
165   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
166   //gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
167   gras_sg_portrec_t pr;
168   gras_trp_sg_sock_data_t *data;
169   volatile int found;
170   
171   const char *host=SIMIX_host_get_name(SIMIX_host_self());
172
173   xbt_ex_t e;
174
175   xbt_assert0(hd,"Please run gras_process_init on each process");
176
177   sock->accepting = 0; /* no such nuisance in SG */
178   found = 0;
179   TRY {
180     find_port(hd,sock->port,&pr);
181     found = 1;
182   } CATCH(e) {
183     if (e.category == mismatch_error)
184       xbt_ex_free(e);
185     else
186       RETHROW;
187   }
188    
189   if (found) 
190     THROW2(mismatch_error,0,
191            "can't listen on address %s:%d: port already in use.",
192            host,sock->port);
193
194   pr.port   = sock->port;
195   pr.meas    = sock->meas;
196         pr.process = SIMIX_process_self();
197   xbt_dynar_push(hd->ports,&pr);
198
199         hd->cond_port[sock->port] = SIMIX_cond_init();
200         hd->mutex_port[sock->port] = SIMIX_mutex_init();
201   
202   /* Create the socket */
203   data = xbt_new(gras_trp_sg_sock_data_t,1);
204   data->from_process     = SIMIX_process_self();
205   data->to_process       = NULL;
206         data->to_host      = SIMIX_host_self();
207   
208   sock->data = data;
209
210   VERB6("'%s' (%ld) ears on %s:%d%s (%p)",
211     SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
212     host,sock->port,sock->meas? " (mode meas)":"",sock);
213
214 }
215
216 void gras_trp_sg_socket_close(gras_socket_t sock){
217         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
218         gras_socket_t sock_iter;
219         int cursor;
220         int found;
221   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
222   int cpt;
223   gras_sg_portrec_t pr; 
224
225   XBT_IN1(" (sock=%p)",sock);
226    
227   if (!sock) return;
228
229   xbt_assert0(hd,"Please run gras_process_init on each process");
230
231   if (sock->data)
232     free(sock->data);
233
234         /* search for a socket in the list that is using the mutex and condition. It can happen because we create 2 sockets to communicate (incomming and outgoing) */
235         found = 0;
236         xbt_dynar_foreach(sockets,cursor,sock_iter) {
237                 if (sock_iter->port == sock->port) {
238                         found = 1;
239                         break;
240                 }
241         }
242         /* if not found, it is the last socket opened in this port and we can free the mutex and condition */
243         if (!found) {
244                 SIMIX_cond_destroy(hd->cond_port[sock->port]);
245                 hd->cond_port[sock->port] = NULL;
246                 SIMIX_mutex_destroy(hd->mutex_port[sock->port]);
247                 hd->mutex_port[sock->port] = NULL;
248         }
249
250
251   if (sock->incoming && !sock->outgoing && sock->port >= 0) {
252     /* server mode socket. Unregister it from 'OS' tables */
253     xbt_dynar_foreach(hd->ports, cpt, pr) {
254       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
255       if (pr.port == sock->port) {
256                                 xbt_dynar_cursor_rm(hd->ports, &cpt);
257                                 XBT_OUT;
258                                 return;
259       }
260     }
261     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
262           sock,sock->port);
263   }
264   XBT_OUT;
265
266 }
267
268 typedef struct {
269   int size;
270   void *data;
271 } sg_task_data_t;
272
273 void gras_trp_sg_chunk_send(gras_socket_t sock,
274                             const char *data,
275                             unsigned long int size,
276                             int stable_ignored) {
277   gras_trp_sg_chunk_send_raw(sock,data,size);
278 }
279
280 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
281                                 const char *data,
282                                 unsigned long int size) {
283   char name[256];
284   static unsigned int count=0;
285
286         smx_action_t act; /* simix action */
287         gras_trp_sg_sock_data_t *sock_data; 
288         gras_hostdata_t *hd;
289         gras_hostdata_t *remote_hd;
290         gras_trp_procdata_t trp_remote_proc;
291         gras_msg_procdata_t msg_remote_proc;
292         gras_msg_t msg; /* message to send */
293
294         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
295         hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
296         remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
297
298   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
299
300
301   sprintf(name,"Chunk[%d]",count++);
302         /*initialize gras message */
303         msg = xbt_new(s_gras_msg_t,1);
304         msg->expe = sock;
305         msg->payl_size=size;
306
307   if (data) {
308     msg->payl=(void*)xbt_malloc(size);
309     memcpy(msg->payl,data,size);
310   } else {
311     msg->payl = NULL;
312   }
313
314         /* put message on msg_queue */
315         msg_remote_proc = (gras_msg_procdata_t)gras_libdata_by_name_from_remote("gras_msg",sock_data->to_process);
316         xbt_fifo_push(msg_remote_proc->msg_to_receive_queue,msg);
317         
318         /* wake-up the receiver */
319         trp_remote_proc = (gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sock_data->to_process);
320         SIMIX_cond_signal(trp_remote_proc->cond);
321
322         SIMIX_mutex_lock(remote_hd->mutex_port[sock->peer_port]);
323         /* wait for the receiver */
324         SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port], remote_hd->mutex_port[sock->peer_port]);
325
326         /* creates simix action and waits its ends, waits in the sender host condition*/
327   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
328          name, SIMIX_host_get_name(SIMIX_host_self()),
329          SIMIX_host_get_name(sock_data->to_host), sock->peer_port,size);
330
331         act = SIMIX_action_communicate(sock_data->to_host, SIMIX_host_self(),name, size, -1);
332         SIMIX_register_action_to_condition(act,remote_hd->cond_port[sock->peer_port]);
333         SIMIX_register_condition_to_action(act,remote_hd->cond_port[sock->peer_port]);
334
335         SIMIX_host_get_name(sock_data->to_host),SIMIX_process_get_name(sock_data->to_process),
336         
337         SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port], remote_hd->mutex_port[sock->peer_port]);
338         /* error treatmeant */
339
340         /* cleanup structures */
341         SIMIX_action_destroy(act);
342         SIMIX_mutex_unlock(remote_hd->mutex_port[sock->peer_port]);
343         SIMIX_cond_signal(remote_hd->cond_port[sock->peer_port]);
344                                 /*
345   m_task_t task=NULL;
346   char name[256];
347   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
348   sg_task_data_t *task_data;
349   
350   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
351
352   sprintf(name,"Chunk[%d]",count++);
353
354   task_data=xbt_new(sg_task_data_t,1);
355   task_data->size = size;
356   if (data) {
357     task_data->data=(void*)xbt_malloc(size);
358     memcpy(task_data->data,data,size);
359   } else {
360     task_data->data = NULL;
361   }
362
363   task=MSG_task_create(name,0,((double)size),task_data);
364
365   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
366          name, MSG_host_get_name(MSG_host_self()),
367          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
368   if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) {
369     THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
370   }*/
371 }
372
373 int gras_trp_sg_chunk_recv(gras_socket_t sock,
374                             char *data,
375                             unsigned long int size){
376   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
377         gras_trp_sg_sock_data_t *sock_data; 
378         gras_hostdata_t *remote_hd;
379         gras_hostdata_t *local_hd;
380   gras_msg_t msg_got;
381         gras_msg_procdata_t msg_procdata = (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
382
383   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
384         
385         SIMIX_mutex_lock(pd->mutex);
386         if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
387                 SIMIX_cond_wait_timeout(pd->cond,pd->mutex,60);
388         }
389         if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
390                 THROW0(timeout_error,0,"Timeout");
391         }
392         SIMIX_mutex_unlock(pd->mutex);
393
394         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
395         DEBUG3("Remote host %s, Remote Port: %d Local port %d", SIMIX_host_get_name(sock_data->to_host), sock->peer_port, sock->port);
396         remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
397         local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
398
399
400
401   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
402
403         SIMIX_mutex_lock(local_hd->mutex_port[sock->port]);
404 /* ok, I'm here, you can continue the communication */
405         SIMIX_cond_signal(local_hd->cond_port[sock->port]);
406
407 /* wait for communication end */
408         SIMIX_cond_wait(local_hd->cond_port[sock->port],local_hd->mutex_port[sock->port]);
409
410
411   if (msg_got->payl_size != size)
412     THROW5(mismatch_error,0,
413            "Got %d bytes when %ld where expected (in %s->%s:%d)",
414            msg_got->payl_size, size,
415            SIMIX_host_get_name(sock_data->to_host),
416            SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
417   if (data) {
418     memcpy(data,msg_got->payl,size);
419         }
420         if (msg_got->payl)
421                 xbt_free(msg_got->payl);        
422         xbt_free(msg_got);
423         SIMIX_cond_wait(local_hd->cond_port[sock->port],local_hd->mutex_port[sock->port]);
424         SIMIX_mutex_unlock(local_hd->mutex_port[sock->port]);
425
426                                         /*
427   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
428
429   m_task_t task=NULL;
430   sg_task_data_t *task_data;
431   gras_trp_sg_sock_data_t *sock_data = sock->data;
432
433   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
434   XBT_IN;
435   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
436          MSG_host_get_name(sock_data->to_host),
437          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
438   if (MSG_task_get_with_time_out(&task, 
439                                  (sock->meas ? pd->measChan : pd->chan),
440                                  60) != MSG_OK)
441     THROW0(system_error,0,"Error in MSG_task_get()");
442   DEBUG1("Got chuck %s",MSG_task_get_name(task));
443
444   task_data = MSG_task_get_data(task);
445   if (task_data->size != size)
446     THROW5(mismatch_error,0,
447            "Got %d bytes when %ld where expected (in %s->%s:%d)",
448            task_data->size, size,
449            MSG_host_get_name(sock_data->to_host),
450            MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
451   if (data) 
452     memcpy(data,task_data->data,size);
453   if (task_data->data)
454     free(task_data->data);
455   free(task_data);
456
457   if (MSG_task_destroy(task) != MSG_OK)
458     THROW0(system_error,0,"Error in MSG_task_destroy()");
459
460   XBT_OUT;
461   return size;*/
462         return 0;
463 }
464