3 /* file trp (transport) - send/receive a bunch of bytes in SG realm */
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. */
9 /* Authors: Martin Quinson */
10 /* Copyright (C) 2004 Martin Quinson. */
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. */
17 #include "transport_private.h"
18 #include "gras/Virtu/virtu_sg.h"
20 XBT_LOG_EXTERNAL_CATEGORY(transport);
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_sg,transport,"SimGrid pseudo-transport");
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);
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);
37 xbt_error_t gras_trp_sg_chunk_send(gras_socket_t sd,
41 xbt_error_t gras_trp_sg_chunk_recv(gras_socket_t sd,
46 *** Specific plugin part
49 int placeholder; /* nothing plugin specific so far */
50 } gras_trp_sg_plug_data_t;
56 static xbt_error_t find_port(gras_hostdata_t *hd, int port,
57 gras_sg_portrec_t *hpd) {
61 xbt_assert0(hd,"Please run gras_process_init on each process");
63 xbt_dynar_foreach(hd->ports, cpt, pr) {
64 if (pr.port == port) {
65 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
69 return mismatch_error;
74 gras_trp_sg_setup(gras_trp_plugin_t *plug) {
76 gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
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;
84 plug->chunk_send = gras_trp_sg_chunk_send;
85 plug->chunk_recv = gras_trp_sg_chunk_recv;
87 plug->flush = NULL; /* nothing cached */
92 xbt_error_t gras_trp_sg_socket_client(gras_trp_plugin_t *self,
93 /* OUT */ gras_socket_t sock){
99 gras_trp_sg_sock_data_t *data;
100 gras_sg_portrec_t pr;
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;
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",
112 errcode = find_port(hd,sock->peer_port,&pr);
113 if (errcode != no_error && errcode != mismatch_error)
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);
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);
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);
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;
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);
151 xbt_error_t gras_trp_sg_socket_server(gras_trp_plugin_t *self,
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;
161 const char *host=MSG_host_get_name(MSG_host_self());
163 xbt_assert0(hd,"Please run gras_process_init on each process");
165 sock->accepting = 0; /* no such nuisance in SG */
167 errcode = find_port(hd,sock->port,&pr);
169 case no_error: /* Port already used... */
170 RAISE2(mismatch_error,
171 "can't listen on address %s:%d: port already in use\n.",
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;
179 xbt_dynar_push(hd->ports,&pr);
186 /* Create the socket */
187 data = xbt_new(gras_trp_sg_sock_data_t,1);
189 data->to_PID = MSG_process_self_PID();
190 data->to_host = MSG_host_self();
191 data->to_chan = pd->chan;
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);
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());
206 gras_sg_portrec_t pr;
209 xbt_assert0(hd,"Please run gras_process_init on each process");
212 xbt_free(sock->data);
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);
223 WARN0("socket_close called on an unknown socket");
232 xbt_error_t gras_trp_sg_chunk_send(gras_socket_t sock,
236 static unsigned int count=0;
238 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
239 sg_task_data_t *task_data;
241 sprintf(name,"Chunk[%d]",count++);
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);
248 task=MSG_task_create(name,0,((double)size)/(1024.0*1024.0),task_data);
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");
260 xbt_error_t gras_trp_sg_chunk_recv(gras_socket_t sock,
263 gras_procdata_t *pd=gras_procdata_get();
266 sg_task_data_t *task_data;
267 gras_trp_sg_sock_data_t *sock_data = sock->data;
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()");
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);
287 if (MSG_task_destroy(task) != MSG_OK)
288 RAISE0(unknown_error,"Error in MSG_task_destroy()");
294 /* Data exchange over raw sockets */
295 xbt_error_t gras_socket_raw_exchange(gras_socket_t peer,
297 unsigned int timeout,
298 unsigned long int expSize,
299 unsigned long int msgSize) {
300 unsigned int bytesTotal;
301 static unsigned int count=0;
304 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)peer->data;
306 gras_procdata_t *pd=gras_procdata_get();
309 startTime=gras_os_time(); /* used only in sender mode */
311 for(bytesTotal = 0; bytesTotal < expSize; bytesTotal += msgSize) {
315 sprintf(name,"Raw data[%d]",count++);
317 task=MSG_task_create(name,0,((double)msgSize)/(1024.0*1024.0),NULL);
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);
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()");
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);
332 } else { /* we are receiver, simulate a select */
335 DEBUG2("%f:%s: gras_socket_raw_recv() BEGIN\n",
336 gras_os_time(), MSG_process_get_name(MSG_process_self()));
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;
344 if (MSG_task_destroy(task) != MSG_OK) {
345 fprintf(stderr,"GRAS: Error in MSG_task_destroy()\n");
346 return unknown_error;
349 DEBUG2("%f:%s: gras_socket_raw_recv() END\n",
350 gras_os_time(), MSG_process_get_name(MSG_process_self()));
353 MSG_process_sleep(0.0001);
356 } while (gras_os_time() - startTime < timeout);
358 if (gras_os_time() - startTime > timeout)
359 return timeout_error;
360 } /* receiver part */