Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more debuging
[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 "msg/msg.h"
15
16 #include "transport_private.h"
17 #include "gras/Virtu/virtu_sg.h"
18
19 XBT_LOG_EXTERNAL_CATEGORY(transport);
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-transport");
21
22 /***
23  *** Prototypes 
24  ***/
25 /* retrieve the port record associated to a numerical port on an host */
26 static xbt_error_t find_port(gras_hostdata_t *hd, int port,
27                               gras_sg_portrec_t *hpd);
28
29
30 xbt_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
31                                        /* OUT */ gras_socket_t sock);
32 xbt_error_t 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 xbt_error_t gras_trp_sg_chunk_send(gras_socket_t sd,
37                                     const char *data,
38                                     long int size);
39
40 xbt_error_t gras_trp_sg_chunk_recv(gras_socket_t sd,
41                                     char *data,
42                                     long int size);
43
44 /***
45  *** Specific plugin part
46  ***/
47 typedef struct {
48   int placeholder; /* nothing plugin specific so far */
49 } gras_trp_sg_plug_data_t;
50
51
52 /***
53  *** Code
54  ***/
55 static xbt_error_t find_port(gras_hostdata_t *hd, int port,
56                               gras_sg_portrec_t *hpd) {
57   int cpt;
58   gras_sg_portrec_t pr;
59
60   xbt_assert0(hd,"Please run gras_process_init on each process");
61   
62   xbt_dynar_foreach(hd->ports, cpt, pr) {
63     if (pr.port == port) {
64       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
65       return no_error;
66     }
67   }
68   return mismatch_error;
69 }
70
71
72 xbt_error_t
73 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
74
75   gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
76
77   plug->data      = data; 
78
79   plug->socket_client = gras_trp_sg_socket_client;
80   plug->socket_server = gras_trp_sg_socket_server;
81   plug->socket_close  = gras_trp_sg_socket_close;
82
83   plug->chunk_send    = gras_trp_sg_chunk_send;
84   plug->chunk_recv    = gras_trp_sg_chunk_recv;
85
86   plug->flush         = NULL; /* nothing cached */
87
88   return no_error;
89 }
90
91 xbt_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
92                                        /* OUT */ gras_socket_t sock){
93
94   xbt_error_t errcode;
95
96   m_host_t peer;
97   gras_hostdata_t *hd;
98   gras_trp_sg_sock_data_t *data;
99   gras_sg_portrec_t pr;
100
101   /* make sure this socket will reach someone */
102   if (!(peer=MSG_get_host_by_name(sock->peer_name))) {
103       fprintf(stderr,"GRAS: can't connect to %s: no such host.\n",sock->peer_name);
104       return mismatch_error;
105   }
106   if (!(hd=(gras_hostdata_t *)MSG_host_get_data(peer))) {
107     RAISE1(mismatch_error,
108            "can't connect to %s: no process on this host",
109            sock->peer_name);
110   }
111   errcode = find_port(hd,sock->peer_port,&pr);
112   if (errcode != no_error && errcode != mismatch_error) 
113     return errcode;
114
115   if (errcode == mismatch_error) {
116     RAISE2(mismatch_error,
117            "can't connect to %s:%d, no process listen on this port",
118            sock->peer_name,sock->peer_port);
119   } 
120
121   if (pr.raw && !sock->raw) {
122     RAISE2(mismatch_error,
123            "can't connect to %s:%d in regular mode, the process listen "
124            "in raw mode on this port",sock->peer_name,sock->peer_port);
125   }
126   if (!pr.raw && sock->raw) {
127     RAISE2(mismatch_error,
128            "can't connect to %s:%d in raw mode, the process listen "
129            "in regular mode on this port",sock->peer_name,sock->peer_port);
130   }
131
132   /* create the socket */
133   data = xbt_new(gras_trp_sg_sock_data_t,1);
134   data->from_PID     = MSG_process_self_PID();
135   data->to_PID       = hd->proc[ pr.tochan ];
136   data->to_host      = peer;
137   data->to_chan      = pr.tochan;
138   
139   sock->data = data;
140   sock->incoming = 1;
141
142   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
143           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
144           sock->raw?"RAW":"regular",
145          sock->peer_name,sock->peer_port,data->to_PID);
146
147   return no_error;
148 }
149
150 xbt_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
151                                        gras_socket_t sock){
152
153   xbt_error_t errcode;
154
155   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
156   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
157   gras_sg_portrec_t pr;
158   gras_trp_sg_sock_data_t *data;
159   
160   const char *host=MSG_host_get_name(MSG_host_self());
161
162   xbt_assert0(hd,"Please run gras_process_init on each process");
163
164   sock->accepting = 0; /* no such nuisance in SG */
165
166   errcode = find_port(hd,sock->port,&pr);
167   switch (errcode) {
168   case no_error: /* Port already used... */
169     RAISE2(mismatch_error,
170            "can't listen on address %s:%d: port already in use\n.",
171            host,sock->port);
172     break;
173
174   case mismatch_error: /* Port not used so far. Do it */
175     pr.tochan = sock->raw ? pd->rawChan : pd->chan;
176     pr.port   = sock->port;
177     pr.raw    = sock->raw;
178     xbt_dynar_push(hd->ports,&pr);
179     break;
180     
181   default:
182     return errcode;
183   }
184   
185   /* Create the socket */
186   data = xbt_new(gras_trp_sg_sock_data_t,1);
187   data->from_PID     = -1;
188   data->to_PID       = MSG_process_self_PID();
189   data->to_host      = MSG_host_self();
190   data->to_chan      = pd->chan;
191   
192   sock->data = data;
193
194   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
195     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
196     host,sock->port,sock->raw? " (mode RAW)":"",sock);
197
198   return no_error;
199 }
200
201 void gras_trp_sg_socket_close(gras_socket_t sock){
202   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
203   int cpt;
204   
205   gras_sg_portrec_t pr;
206
207   XBT_IN1(" (sock=%p)",sock);
208    
209   if (!sock) return;
210   xbt_assert0(hd,"Please run gras_process_init on each process");
211
212   if (sock->data)
213     free(sock->data);
214
215   if (sock->incoming) {
216     /* server mode socket. Un register it from 'OS' tables */
217     xbt_dynar_foreach(hd->ports, cpt, pr) {
218       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
219       if (pr.port == sock->port) {
220         xbt_dynar_cursor_rm(hd->ports, &cpt);
221         XBT_OUT;
222         return;
223       }
224     }
225     WARN0("socket_close called on an unknown socket");
226   }
227   XBT_OUT;
228 }
229
230 typedef struct {
231   int size;
232   void *data;
233 } sg_task_data_t;
234
235 xbt_error_t gras_trp_sg_chunk_send(gras_socket_t sock,
236                                     const char *data,
237                                     long int size) {
238   m_task_t task=NULL;
239   static unsigned int count=0;
240   char name[256];
241   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
242   sg_task_data_t *task_data;
243   
244   sprintf(name,"Chunk[%d]",count++);
245
246   task_data=xbt_new(sg_task_data_t,1);
247   task_data->data=(void*)xbt_malloc(size);
248   task_data->size = size;
249   memcpy(task_data->data,data,size);
250
251   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
252
253   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
254          name, MSG_host_get_name(MSG_host_self()),
255          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
256   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
257     RAISE0(system_error,"Problem during the MSG_task_put");
258   }
259
260   return no_error;
261 }
262
263 xbt_error_t gras_trp_sg_chunk_recv(gras_socket_t sock,
264                                     char *data,
265                                     long int size){
266   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
267
268   m_task_t task=NULL;
269   sg_task_data_t *task_data;
270   gras_trp_sg_sock_data_t *sock_data = sock->data;
271
272   XBT_IN;
273   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
274          MSG_host_get_name(sock_data->to_host),
275          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
276   if (MSG_task_get(&task, (sock->raw ? pd->rawChan : pd->chan)) != MSG_OK)
277     RAISE0(unknown_error,"Error in MSG_task_get()");
278   DEBUG1("Got chuck %s",MSG_task_get_name(task));
279
280   task_data = MSG_task_get_data(task);
281   if (task_data->size != size)
282     RAISE5(mismatch_error,
283            "Got %d bytes when %ld where expected (in %s->%s:%d)",
284            task_data->size, size,
285            MSG_host_get_name(sock_data->to_host),
286            MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
287   memcpy(data,task_data->data,size);
288   free(task_data->data);
289   free(task_data);
290
291   if (MSG_task_destroy(task) != MSG_OK)
292     RAISE0(unknown_error,"Error in MSG_task_destroy()");
293
294   XBT_OUT;
295   return no_error;
296 }
297
298 /* Data exchange over raw sockets */
299 xbt_error_t gras_socket_raw_exchange(gras_socket_t peer,
300                                       int sender,
301                                       unsigned int timeout,
302                                       unsigned long int expSize,
303                                       unsigned long int msgSize) {
304   unsigned int bytesTotal;
305   static unsigned int count=0;
306   m_task_t task=NULL;
307   char name[256];
308   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)peer->data;
309   
310   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
311   double startTime;
312   
313   startTime=gras_os_time(); /* used only in sender mode */
314
315   for(bytesTotal = 0;  bytesTotal < expSize;  bytesTotal += msgSize) {
316
317     if (sender) {
318     
319       sprintf(name,"Raw data[%d]",count++);
320       
321       task=MSG_task_create(name,0,((double)msgSize)/(1024.0*1024.0),NULL);
322
323       DEBUG5("%f:%s: gras_socket_raw_send(%f %s -> %s) BEGIN",
324              gras_os_time(), MSG_process_get_name(MSG_process_self()),
325              ((double)msgSize)/(1024.0*1024.0),
326              MSG_host_get_name( MSG_host_self()), peer->peer_name);
327
328       if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK)
329         RAISE0(system_error,"Problem during the MSG_task_put()");
330                
331       DEBUG5("%f:%s: gras_socket_raw_send(%f %s -> %s) END",
332              gras_os_time(), MSG_process_get_name(MSG_process_self()),
333              ((double)msgSize)/(1024.0*1024.0),
334              MSG_host_get_name( MSG_host_self()), peer->peer_name);
335                    
336     } else { /* we are receiver, simulate a select */
337       
338       task=NULL;
339       DEBUG2("%f:%s: gras_socket_raw_recv() BEGIN\n",
340              gras_os_time(), MSG_process_get_name(MSG_process_self()));
341       do {
342         if (MSG_task_Iprobe((m_channel_t) pd->rawChan)) {
343           if (MSG_task_get(&task, (m_channel_t) pd->rawChan) != MSG_OK) {
344             fprintf(stderr,"GRAS: Error in MSG_task_get()\n");
345             return unknown_error;
346           }
347           
348           if (MSG_task_destroy(task) != MSG_OK) {
349             fprintf(stderr,"GRAS: Error in MSG_task_destroy()\n");
350             return unknown_error;
351           }
352           
353           DEBUG2("%f:%s: gras_socket_raw_recv() END\n",
354                  gras_os_time(), MSG_process_get_name(MSG_process_self()));
355           break;
356         } else {
357           MSG_process_sleep(0.0001);
358         }
359         
360       } while (gras_os_time() - startTime < timeout);
361       
362       if (gras_os_time() - startTime > timeout)
363         return timeout_error;
364     } /* receiver part */
365   } /* foreach msg */
366
367   return no_error;
368 }