Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
92bea15ff36dd31620324b6250c8690006b57ebe
[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 "xbt/ex.h" 
15
16 #include "msg/msg.h"
17
18 #include "transport_private.h"
19 #include "gras/Virtu/virtu_sg.h"
20
21 XBT_LOG_EXTERNAL_CATEGORY(transport);
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-transport");
23
24 /***
25  *** Prototypes 
26  ***/
27
28 /* retrieve the port record associated to a numerical port on an host */
29 static void find_port(gras_hostdata_t *hd, int port, gras_sg_portrec_t *hpd);
30
31
32 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
33                                /* OUT */ gras_socket_t sock);
34 void 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 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
39                                 const char *data,
40                                 unsigned long int size);
41 void gras_trp_sg_chunk_send(gras_socket_t sd,
42                             const char *data,
43                             unsigned long int size,
44                             int stable_ignored);
45
46 int gras_trp_sg_chunk_recv(gras_socket_t sd,
47                             char *data,
48                             unsigned long int size);
49
50 /***
51  *** Specific plugin part
52  ***/
53 typedef struct {
54   int placeholder; /* nothing plugin specific so far */
55 } gras_trp_sg_plug_data_t;
56
57
58 /***
59  *** Code
60  ***/
61 static void find_port(gras_hostdata_t *hd, int port,
62                       gras_sg_portrec_t *hpd) {
63   int cpt;
64   gras_sg_portrec_t pr;
65
66   xbt_assert0(hd,"Please run gras_process_init on each process");
67   
68   xbt_dynar_foreach(hd->ports, cpt, pr) {
69     if (pr.port == port) {
70       memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
71       return;
72     }
73   }
74   THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
75 }
76
77
78 void
79 gras_trp_sg_setup(gras_trp_plugin_t plug) {
80
81   gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
82
83   plug->data      = data; 
84
85   plug->socket_client = gras_trp_sg_socket_client;
86   plug->socket_server = gras_trp_sg_socket_server;
87   plug->socket_close  = gras_trp_sg_socket_close;
88
89   plug->raw_send = gras_trp_sg_chunk_send_raw;
90   plug->send = gras_trp_sg_chunk_send;
91   plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
92
93   plug->flush         = NULL; /* nothing cached */
94 }
95
96 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
97                                /* OUT */ gras_socket_t sock){
98   xbt_ex_t e;
99
100   m_host_t peer;
101   gras_hostdata_t *hd;
102   gras_trp_sg_sock_data_t *data;
103   gras_sg_portrec_t pr;
104
105   /* make sure this socket will reach someone */
106   if (!(peer=MSG_get_host_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 *)MSG_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
137   /* create the socket */
138   data = xbt_new(gras_trp_sg_sock_data_t,1);
139   data->from_PID     = MSG_process_self_PID();
140   data->to_PID       = hd->proc[ pr.tochan ];
141   data->to_host      = peer;
142   data->to_chan      = pr.tochan;
143   
144   sock->data = data;
145   sock->incoming = 1;
146
147   DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)",
148           MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
149           sock->meas?"meas":"regular",
150          sock->peer_name,sock->peer_port,data->to_PID);
151 }
152
153 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
154                                gras_socket_t sock){
155
156   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
157   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
158   gras_sg_portrec_t pr;
159   gras_trp_sg_sock_data_t *data;
160   int found;
161   
162   const char *host=MSG_host_get_name(MSG_host_self());
163
164   xbt_ex_t e;
165
166   xbt_assert0(hd,"Please run gras_process_init on each process");
167
168   sock->accepting = 0; /* no such nuisance in SG */
169
170   found = 0;
171   TRY {
172     find_port(hd,sock->port,&pr);
173     found = 1;
174   } CATCH(e) {
175     if (e.category == mismatch_error)
176       xbt_ex_free(&e);
177     else
178       RETHROW;
179   }
180    
181   if (found) 
182     THROW2(mismatch_error,0,
183            "can't listen on address %s:%d: port already in use.",
184            host,sock->port);
185
186   pr.tochan = sock->meas ? pd->measChan : pd->chan;
187   pr.port   = sock->port;
188   pr.meas    = sock->meas;
189   xbt_dynar_push(hd->ports,&pr);
190   
191   /* Create the socket */
192   data = xbt_new(gras_trp_sg_sock_data_t,1);
193   data->from_PID     = -1;
194   data->to_PID       = MSG_process_self_PID();
195   data->to_host      = MSG_host_self();
196   data->to_chan      = pd->chan;
197   
198   sock->data = data;
199
200   VERB6("'%s' (%d) ears on %s:%d%s (%p)",
201     MSG_process_get_name(MSG_process_self()), MSG_process_self_PID(),
202     host,sock->port,sock->meas? " (mode meas)":"",sock);
203 }
204
205 void gras_trp_sg_socket_close(gras_socket_t sock){
206   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
207   int cpt;
208   
209   gras_sg_portrec_t pr;
210
211   XBT_IN1(" (sock=%p)",sock);
212    
213   if (!sock) return;
214   xbt_assert0(hd,"Please run gras_process_init on each process");
215
216   if (sock->data)
217     free(sock->data);
218
219   if (sock->incoming && sock->port >= 0) {
220     /* server mode socket. Unregister it from 'OS' tables */
221     xbt_dynar_foreach(hd->ports, cpt, pr) {
222       DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
223       if (pr.port == sock->port) {
224         xbt_dynar_cursor_rm(hd->ports, &cpt);
225         XBT_OUT;
226         return;
227       }
228     }
229     WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
230           sock,sock->port);
231   }
232   XBT_OUT;
233 }
234
235 typedef struct {
236   int size;
237   void *data;
238 } sg_task_data_t;
239
240 void gras_trp_sg_chunk_send(gras_socket_t sock,
241                             const char *data,
242                             unsigned long int size,
243                             int stable_ignored) {
244   gras_trp_sg_chunk_send_raw(sock,data,size);
245 }
246
247 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
248                                 const char *data,
249                                 unsigned long int size) {
250   m_task_t task=NULL;
251   static unsigned int count=0;
252   char name[256];
253   gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
254   sg_task_data_t *task_data;
255   
256   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
257
258   sprintf(name,"Chunk[%d]",count++);
259
260   task_data=xbt_new(sg_task_data_t,1);
261   task_data->size = size;
262   if (data) {
263     task_data->data=(void*)xbt_malloc(size);
264     memcpy(task_data->data,data,size);
265   } else {
266     task_data->data = NULL;
267   }
268
269   task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
270
271   DEBUG5("send chunk %s from %s to  %s:%d (size=%ld)",
272          name, MSG_host_get_name(MSG_host_self()),
273          MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
274   if (MSG_task_put(task, sock_data->to_host,sock_data->to_chan) != MSG_OK) {
275     THROW0(system_error,0,"Problem during the MSG_task_put");
276   }
277 }
278
279 int gras_trp_sg_chunk_recv(gras_socket_t sock,
280                             char *data,
281                             unsigned long int size){
282   gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
283
284   m_task_t task=NULL;
285   sg_task_data_t *task_data;
286   gras_trp_sg_sock_data_t *sock_data = sock->data;
287
288   xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
289   XBT_IN;
290   DEBUG4("recv chunk on %s ->  %s:%d (size=%ld)",
291          MSG_host_get_name(sock_data->to_host),
292          MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
293   if (MSG_task_get_with_time_out(&task, 
294                                  (sock->meas ? pd->measChan : pd->chan),
295                                  60) != MSG_OK)
296     THROW0(system_error,0,"Error in MSG_task_get()");
297   DEBUG1("Got chuck %s",MSG_task_get_name(task));
298
299   task_data = MSG_task_get_data(task);
300   if (task_data->size != size)
301     THROW5(mismatch_error,0,
302            "Got %d bytes when %ld where expected (in %s->%s:%d)",
303            task_data->size, size,
304            MSG_host_get_name(sock_data->to_host),
305            MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
306   if (data) 
307     memcpy(data,task_data->data,size);
308   if (task_data->data)
309     free(task_data->data);
310   free(task_data);
311
312   if (MSG_task_destroy(task) != MSG_OK)
313     THROW0(system_error,0,"Error in MSG_task_destroy()");
314
315   XBT_OUT;
316   return size;
317 }
318