Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't use XBT_CIN, since I didn't manage to implement it correctly
[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 void hexa_print(unsigned char *data, int size);   /* in gras.c */
26
27 /* retrieve the port record associated to a numerical port on an host */
28 static xbt_error_t find_port(gras_hostdata_t *hd, int port,
29                               gras_sg_portrec_t *hpd);
30
31
32 xbt_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
33                                        /* OUT */ gras_socket_t sock);
34 xbt_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 xbt_error_t gras_trp_sg_chunk_send(gras_socket_t sd,
39                                    const char *data,
40                                    unsigned long int size);
41
42 xbt_error_t gras_trp_sg_chunk_recv(gras_socket_t sd,
43                                    char *data,
44                                    unsigned 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 xbt_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   xbt_assert0(hd,"Please run gras_process_init on each process");
63   
64   xbt_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 xbt_error_t
75 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
76
77   gras_trp_sg_plug_data_t *data=xbt_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 xbt_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
94                                        /* OUT */ gras_socket_t sock){
95
96   xbt_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.meas && !sock->meas) {
124     RAISE2(mismatch_error,
125            "can't connect to %s:%d in regular mode, the process listen "
126            "in meas mode on this port",sock->peer_name,sock->peer_port);
127   }
128   if (!pr.meas && sock->meas) {
129     RAISE2(mismatch_error,
130            "can't connect to %s:%d in meas 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 = xbt_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->meas?"meas":"regular",
147          sock->peer_name,sock->peer_port,data->to_PID);
148
149   return no_error;
150 }
151
152 xbt_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
153                                        gras_socket_t sock){
154
155   xbt_error_t errcode;
156
157   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
158   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
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   xbt_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     break;
175
176   case mismatch_error: /* Port not used so far. Do it */
177     pr.tochan = sock->meas ? pd->measChan : pd->chan;
178     pr.port   = sock->port;
179     pr.meas    = sock->meas;
180     xbt_dynar_push(hd->ports,&pr);
181     break;
182     
183   default:
184     return errcode;
185   }
186   
187   /* Create the socket */
188   data = xbt_new(gras_trp_sg_sock_data_t,1);
189   data->from_PID     = -1;
190   data->to_PID       = MSG_process_self_PID();
191   data->to_host      = MSG_host_self();
192   data->to_chan      = pd->chan;
193   
194   sock->data = data;
195
196   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
197     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
198     host,sock->port,sock->meas? " (mode meas)":"",sock);
199
200   return no_error;
201 }
202
203 void gras_trp_sg_socket_close(gras_socket_t sock){
204   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
205   int cpt;
206   
207   gras_sg_portrec_t pr;
208
209   XBT_IN1(" (sock=%p)",sock);
210    
211   if (!sock) return;
212   xbt_assert0(hd,"Please run gras_process_init on each process");
213
214   if (sock->data)
215     free(sock->data);
216
217   if (sock->incoming && sock->port >= 0) {
218     /* server mode socket. Unregister it from 'OS' tables */
219     xbt_dynar_foreach(hd->ports, cpt, pr) {
220       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
221       if (pr.port == sock->port) {
222         xbt_dynar_cursor_rm(hd->ports, &cpt);
223         XBT_OUT;
224         return;
225       }
226     }
227     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
228           sock,sock->port);
229   }
230   XBT_OUT;
231 }
232
233 typedef struct {
234   int size;
235   void *data;
236 } sg_task_data_t;
237
238 xbt_error_t gras_trp_sg_chunk_send(gras_socket_t sock,
239                                    const char *data,
240                                    unsigned long int size) {
241   m_task_t task=NULL;
242   static unsigned int count=0;
243   char name[256];
244   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
245   sg_task_data_t *task_data;
246   
247   sprintf(name,"Chunk[%d]",count++);
248
249   task_data=xbt_new(sg_task_data_t,1);
250   task_data->data=(void*)xbt_malloc(size);
251   task_data->size = size;
252   memcpy(task_data->data,data,size);
253
254   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
255
256   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
257          name, MSG_host_get_name(MSG_host_self()),
258          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
259   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
260     RAISE0(system_error,"Problem during the MSG_task_put");
261   }
262
263   return no_error;
264 }
265
266 xbt_error_t gras_trp_sg_chunk_recv(gras_socket_t sock,
267                                    char *data,
268                                    unsigned long int size){
269   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
270
271   m_task_t task=NULL;
272   sg_task_data_t *task_data;
273   gras_trp_sg_sock_data_t *sock_data = sock->data;
274
275   XBT_IN;
276   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
277          MSG_host_get_name(sock_data->to_host),
278          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
279   if (MSG_task_get(&task, (sock->meas ? pd->measChan : pd->chan)) != MSG_OK)
280     RAISE0(unknown_error,"Error in MSG_task_get()");
281   DEBUG1("Got chuck %s",MSG_task_get_name(task));
282
283   task_data = MSG_task_get_data(task);
284   if (size != -1) {     
285      if (task_data->size != size)
286        RAISE5(mismatch_error,
287               "Got %d bytes when %ld where expected (in %s->%s:%d)",
288               task_data->size, size,
289               MSG_host_get_name(sock_data->to_host),
290               MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
291      memcpy(data,task_data->data,size);
292   } else {
293      /* damn, the size is embeeded at the begining of the chunk */
294      int netsize;
295      
296      memcpy((char*)&netsize,task_data->data,4);
297      DEBUG1("netsize embeeded = %d",netsize);
298
299      memcpy(data,task_data->data,netsize+4);
300   }
301   free(task_data->data);
302   free(task_data);
303
304   if (MSG_task_destroy(task) != MSG_OK)
305     RAISE0(unknown_error,"Error in MSG_task_destroy()");
306
307   XBT_OUT;
308   return no_error;
309 }
310
311 #if 0
312 /* Data exchange over measurement sockets */
313 xbt_error_t gras_socket_meas_exchange(gras_socket_t peer,
314                                       int sender,
315                                       unsigned int timeout,
316                                       unsigned long int expSize,
317                                       unsigned long int msgSize) {
318   unsigned int bytesTotal;
319   static unsigned int count=0;
320   m_task_t task=NULL;
321   char name[256];
322   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)peer->data;
323   
324   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
325   double startTime;
326   
327   startTime=gras_os_time(); /* used only in sender mode */
328
329   for(bytesTotal = 0;  bytesTotal < expSize;  bytesTotal += msgSize) {
330
331     if (sender) {
332     
333       sprintf(name,"meas data[%d]",count++);
334       
335       task=MSG_task_create(name,0,((double)msgSize)/(1024.0*1024.0),NULL);
336
337       DEBUG5("%f:%s: gras_socket_meas_send(%f %s -> %s) BEGIN",
338              gras_os_time(), MSG_process_get_name(MSG_process_self()),
339              ((double)msgSize)/(1024.0*1024.0),
340              MSG_host_get_name( MSG_host_self()), peer->peer_name);
341
342       if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK)
343         RAISE0(system_error,"Problem during the MSG_task_put()");
344                
345       DEBUG5("%f:%s: gras_socket_meas_send(%f %s -> %s) END",
346              gras_os_time(), MSG_process_get_name(MSG_process_self()),
347              ((double)msgSize)/(1024.0*1024.0),
348              MSG_host_get_name( MSG_host_self()), peer->peer_name);
349                    
350     } else { /* we are receiver, simulate a select */
351       
352       task=NULL;
353       DEBUG2("%f:%s: gras_socket_meas_recv() BEGIN\n",
354              gras_os_time(), MSG_process_get_name(MSG_process_self()));
355       do {
356         if (MSG_task_Iprobe((m_channel_t) pd->measChan)) {
357           if (MSG_task_get(&task, (m_channel_t) pd->measChan) != MSG_OK) {
358             fprintf(stderr,"GRAS: Error in MSG_task_get()\n");
359             return unknown_error;
360           }
361           
362           if (MSG_task_destroy(task) != MSG_OK) {
363             fprintf(stderr,"GRAS: Error in MSG_task_destroy()\n");
364             return unknown_error;
365           }
366           
367           DEBUG2("%f:%s: gras_socket_meas_recv() END\n",
368                  gras_os_time(), MSG_process_get_name(MSG_process_self()));
369           break;
370         } else {
371           MSG_process_sleep(0.0001);
372         }
373         
374       } while (gras_os_time() - startTime < timeout);
375       
376       if (gras_os_time() - startTime > timeout)
377         return timeout_error;
378     } /* receiver part */
379   } /* foreach msg */
380
381   return no_error;
382 }
383 #endif