Logo AND Algorithmique Numérique Distribuée

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