Logo AND Algorithmique Numérique Distribuée

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