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 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
16 //#include "msg/msg.h"
18 #include "gras_simix_transport_private.h"
19 #include "gras_simix/Virtu/gras_simix_virtu_sg.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg,gras_trp,"SimGrid pseudo-transport");
27 /* retrieve the port record associated to a numerical port on an host */
28 static void find_port(gras_hostdata_t *hd, int port, gras_sg_portrec_t *hpd);
31 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
32 /* OUT */ gras_socket_t sock);
33 void 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 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
39 unsigned long int size);
40 void gras_trp_sg_chunk_send(gras_socket_t sd,
42 unsigned long int size,
45 int gras_trp_sg_chunk_recv(gras_socket_t sd,
47 unsigned long int size);
50 *** Specific plugin part
53 int placeholder; /* nothing plugin specific so far */
54 } gras_trp_sg_plug_data_t;
60 static void find_port(gras_hostdata_t *hd, int port,
61 gras_sg_portrec_t *hpd) {
65 xbt_assert0(hd,"Please run gras_process_init on each process");
67 xbt_dynar_foreach(hd->ports, cpt, pr) {
68 if (pr.port == port) {
69 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
73 THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
78 gras_trp_sg_setup(gras_trp_plugin_t plug) {
80 gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
84 plug->socket_client = gras_trp_sg_socket_client;
85 plug->socket_server = gras_trp_sg_socket_server;
86 plug->socket_close = gras_trp_sg_socket_close;
88 plug->raw_send = gras_trp_sg_chunk_send_raw;
89 plug->send = gras_trp_sg_chunk_send;
90 plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
92 plug->flush = NULL; /* nothing cached */
95 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
96 /* OUT */ gras_socket_t sock){
101 gras_hostdata_t *local_hd;
102 gras_trp_sg_sock_data_t *data;
103 gras_sg_portrec_t pr;
106 /* make sure this socket will reach someone */
107 if (!(peer=SIMIX_host_get_by_name(sock->peer_name)))
108 THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
110 if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer)))
111 THROW1(mismatch_error,0,
112 "can't connect to %s: no process on this host",
116 find_port(hd,sock->peer_port,&pr);
118 if (e.category == mismatch_error) {
120 THROW2(mismatch_error,0,
121 "can't connect to %s:%d, no process listen on this port",
122 sock->peer_name,sock->peer_port);
127 if (pr.meas && !sock->meas) {
128 THROW2(mismatch_error,0,
129 "can't connect to %s:%d in regular mode, the process listen "
130 "in meas mode on this port",sock->peer_name,sock->peer_port);
132 if (!pr.meas && sock->meas) {
133 THROW2(mismatch_error,0,
134 "can't connect to %s:%d in meas mode, the process listen "
135 "in regular mode on this port",sock->peer_name,sock->peer_port);
137 /* create the socket */
138 data = xbt_new(gras_trp_sg_sock_data_t,1);
139 data->from_process = SIMIX_process_self();
140 data->to_process = pr.process;
141 data->to_host = peer;
143 /* searches for a free port on this host */
144 local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
145 for (i=1;i<65536;i++) {
146 if (local_hd->cond_port[i] == NULL)
149 if (i == 65536) THROW0(system_error,0,"No port free");
151 local_hd->cond_port[i] = SIMIX_cond_init();
152 local_hd->mutex_port[i] = SIMIX_mutex_init();
157 DEBUG5("%s (PID %ld) connects in %s mode to %s:%d",
158 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
159 sock->meas?"meas":"regular",
160 sock->peer_name,sock->peer_port);
163 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
166 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
167 //gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
168 gras_sg_portrec_t pr;
169 gras_trp_sg_sock_data_t *data;
172 const char *host=SIMIX_host_get_name(SIMIX_host_self());
176 xbt_assert0(hd,"Please run gras_process_init on each process");
178 sock->accepting = 0; /* no such nuisance in SG */
181 find_port(hd,sock->port,&pr);
184 if (e.category == mismatch_error)
191 THROW2(mismatch_error,0,
192 "can't listen on address %s:%d: port already in use.",
195 pr.port = sock->port;
196 pr.meas = sock->meas;
197 pr.process = SIMIX_process_self();
198 xbt_dynar_push(hd->ports,&pr);
200 hd->cond_port[sock->port] = SIMIX_cond_init();
201 hd->mutex_port[sock->port] = SIMIX_mutex_init();
203 /* Create the socket */
204 data = xbt_new(gras_trp_sg_sock_data_t,1);
205 data->from_process = SIMIX_process_self();
206 data->to_process = NULL;
207 data->to_host = SIMIX_host_self();
211 VERB6("'%s' (%ld) ears on %s:%d%s (%p)",
212 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
213 host,sock->port,sock->meas? " (mode meas)":"",sock);
217 void gras_trp_sg_socket_close(gras_socket_t sock){
219 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
221 gras_sg_portrec_t pr;
223 XBT_IN1(" (sock=%p)",sock);
227 xbt_assert0(hd,"Please run gras_process_init on each process");
233 SIMIX_cond_destroy(hd->cond_port[sock->port]);
234 hd->cond_port[sock->port] = NULL;
235 SIMIX_mutex_destroy(hd->mutex_port[sock->port]);
236 hd->mutex_port[sock->port] = NULL;
238 if (sock->incoming && !sock->outgoing && sock->port >= 0) {
239 /* server mode socket. Unregister it from 'OS' tables */
240 xbt_dynar_foreach(hd->ports, cpt, pr) {
241 DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
242 if (pr.port == sock->port) {
243 xbt_dynar_cursor_rm(hd->ports, &cpt);
248 WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
260 void gras_trp_sg_chunk_send(gras_socket_t sock,
262 unsigned long int size,
263 int stable_ignored) {
264 gras_trp_sg_chunk_send_raw(sock,data,size);
267 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
269 unsigned long int size) {
272 static unsigned int count=0;
274 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
275 sg_task_data_t *task_data;
277 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
279 sprintf(name,"Chunk[%d]",count++);
281 task_data=xbt_new(sg_task_data_t,1);
282 task_data->size = size;
284 task_data->data=(void*)xbt_malloc(size);
285 memcpy(task_data->data,data,size);
287 task_data->data = NULL;
290 task=MSG_task_create(name,0,((double)size),task_data);
292 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
293 name, MSG_host_get_name(MSG_host_self()),
294 MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
295 if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) {
296 THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
300 int gras_trp_sg_chunk_recv(gras_socket_t sock,
302 unsigned long int size){
304 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
307 sg_task_data_t *task_data;
308 gras_trp_sg_sock_data_t *sock_data = sock->data;
310 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
312 DEBUG4("recv chunk on %s -> %s:%d (size=%ld)",
313 MSG_host_get_name(sock_data->to_host),
314 MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
315 if (MSG_task_get_with_time_out(&task,
316 (sock->meas ? pd->measChan : pd->chan),
318 THROW0(system_error,0,"Error in MSG_task_get()");
319 DEBUG1("Got chuck %s",MSG_task_get_name(task));
321 task_data = MSG_task_get_data(task);
322 if (task_data->size != size)
323 THROW5(mismatch_error,0,
324 "Got %d bytes when %ld where expected (in %s->%s:%d)",
325 task_data->size, size,
326 MSG_host_get_name(sock_data->to_host),
327 MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
329 memcpy(data,task_data->data,size);
331 free(task_data->data);
334 if (MSG_task_destroy(task) != MSG_OK)
335 THROW0(system_error,0,"Error in MSG_task_destroy()");