1 /* file trp (transport) - send/receive a bunch of bytes in SG realm */
3 /* Note that this is only used to debug other parts of GRAS since message */
4 /* exchange in SG realm is implemented directly without mimicing real life */
5 /* This would be terribly unefficient. */
7 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
8 * All rights reserved. */
10 /* This program is free software; you can redistribute it and/or modify it
11 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "gras/Msg/msg_private.h"
16 #include "gras/Transport/transport_private.h"
17 #include "gras/Virtu/virtu_sg.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg, gras_trp,
20 "SimGrid pseudo-transport");
26 /* retrieve the port record associated to a numerical port on an host */
27 static void find_port(gras_hostdata_t * hd, int port,
28 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,
38 const char *data, unsigned long int size);
39 void gras_trp_sg_chunk_send(gras_socket_t sd,
41 unsigned long int size, int stable_ignored);
43 int gras_trp_sg_chunk_recv(gras_socket_t sd,
44 char *data, unsigned long int size);
47 *** Specific plugin part
50 int placeholder; /* nothing plugin specific so far */
51 } gras_trp_sg_plug_data_t;
57 static void find_port(gras_hostdata_t * hd, int port, gras_sg_portrec_t * hpd)
62 xbt_assert0(hd, "Please run gras_process_init on each process");
64 xbt_dynar_foreach(hd->ports, cpt, pr) {
65 if (pr.port == port) {
66 memcpy(hpd, &pr, sizeof(gras_sg_portrec_t));
70 THROW1(mismatch_error, 0, "Unable to find any portrec for port #%d", port);
74 void gras_trp_sg_setup(gras_trp_plugin_t plug)
77 gras_trp_sg_plug_data_t *data = xbt_new(gras_trp_sg_plug_data_t, 1);
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;
85 plug->raw_send = gras_trp_sg_chunk_send_raw;
86 plug->send = gras_trp_sg_chunk_send;
87 plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
89 plug->flush = NULL; /* nothing cached */
92 void 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 = SIMIX_host_get_by_name(sock->peer_name)))
104 THROW1(mismatch_error, 0,
105 "Can't connect to %s: no such host.\n", sock->peer_name);
107 if (!(hd = (gras_hostdata_t *) SIMIX_host_get_data(peer)))
108 THROW1(mismatch_error, 0,
109 "can't connect to %s: no process on this host", sock->peer_name);
112 find_port(hd, sock->peer_port, &pr);
115 if (e.category == mismatch_error) {
117 THROW2(mismatch_error, 0,
118 "can't connect to %s:%d, no process listen on this port",
119 sock->peer_name, sock->peer_port);
124 if (pr.meas && !sock->meas) {
125 THROW2(mismatch_error, 0,
126 "can't connect to %s:%d in regular mode, the process listen "
127 "in measurement mode on this port", sock->peer_name,
130 if (!pr.meas && sock->meas) {
131 THROW2(mismatch_error, 0,
132 "can't connect to %s:%d in measurement mode, the process listen "
133 "in regular mode on this port", sock->peer_name, sock->peer_port);
135 /* create the socket */
136 data = xbt_new(gras_trp_sg_sock_data_t, 1);
137 data->from_process = SIMIX_process_self();
138 data->to_process = pr.process;
139 data->to_host = peer;
141 /* initialize mutex and condition of the socket */
142 data->mutex = SIMIX_mutex_init();
143 data->cond = SIMIX_cond_init();
144 data->to_socket = pr.socket;
149 DEBUG5("%s (PID %d) connects in %s mode to %s:%d",
150 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
151 sock->meas ? "meas" : "regular", sock->peer_name, sock->peer_port);
154 void gras_trp_sg_socket_server(gras_trp_plugin_t self, gras_socket_t sock)
157 gras_hostdata_t *hd =
158 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
159 gras_sg_portrec_t pr;
160 gras_trp_sg_sock_data_t *data;
163 const char *host = SIMIX_host_get_name(SIMIX_host_self());
167 xbt_assert0(hd, "Please run gras_process_init on each process");
169 sock->accepting = 0; /* no such nuisance in SG */
172 find_port(hd, sock->port, &pr);
175 if (e.category == mismatch_error)
182 THROW2(mismatch_error, 0,
183 "can't listen on address %s:%d: port already in use.",
186 pr.port = sock->port;
187 pr.meas = sock->meas;
189 pr.process = SIMIX_process_self();
190 xbt_dynar_push(hd->ports, &pr);
192 /* Create the socket */
193 data = xbt_new(gras_trp_sg_sock_data_t, 1);
194 data->from_process = SIMIX_process_self();
195 data->to_process = NULL;
196 data->to_host = SIMIX_host_self();
198 data->cond = SIMIX_cond_init();
199 data->mutex = SIMIX_mutex_init();
203 VERB6("'%s' (%d) ears on %s:%d%s (%p)",
204 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
205 host, sock->port, sock->meas ? " (mode meas)" : "", sock);
209 void gras_trp_sg_socket_close(gras_socket_t sock)
211 gras_hostdata_t *hd =
212 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
214 gras_sg_portrec_t pr;
216 XBT_IN1(" (sock=%p)", sock);
221 xbt_assert0(hd, "Please run gras_process_init on each process");
224 SIMIX_cond_destroy(((gras_trp_sg_sock_data_t *) sock->data)->cond);
225 SIMIX_mutex_destroy(((gras_trp_sg_sock_data_t *) sock->data)->mutex);
229 if (sock->incoming && !sock->outgoing && sock->port >= 0) {
230 /* server mode socket. Unregister it from 'OS' tables */
231 xbt_dynar_foreach(hd->ports, cpt, pr) {
232 DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
233 if (pr.port == sock->port) {
234 xbt_dynar_cursor_rm(hd->ports, &cpt);
239 WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
250 void gras_trp_sg_chunk_send(gras_socket_t sock,
252 unsigned long int size, int stable_ignored)
254 gras_trp_sg_chunk_send_raw(sock, data, size);
257 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
258 const char *data, unsigned long int size)
261 static unsigned int count = 0;
263 smx_action_t act; /* simix action */
264 gras_trp_sg_sock_data_t *sock_data;
265 gras_trp_procdata_t trp_remote_proc;
266 gras_msg_procdata_t msg_remote_proc;
267 gras_msg_t msg; /* message to send */
269 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
271 xbt_assert0(sock->meas,
272 "SG chunk exchange shouldn't be used on non-measurement sockets");
274 SIMIX_mutex_lock(sock_data->mutex);
275 sprintf(name, "Chunk[%d]", count++);
276 /*initialize gras message */
277 msg = xbt_new(s_gras_msg_t, 1);
279 msg->payl_size = size;
282 msg->payl = (void *) xbt_malloc(size);
283 memcpy(msg->payl, data, size);
289 /* put his socket on the selectable socket queue */
290 trp_remote_proc = (gras_trp_procdata_t)
291 gras_libdata_by_name_from_remote("gras_trp", sock_data->to_process);
292 xbt_queue_push(trp_remote_proc->meas_selectable_sockets, &sock);
294 /* put message on msg_queue */
295 msg_remote_proc = (gras_msg_procdata_t)
296 gras_libdata_by_name_from_remote("gras_msg", sock_data->to_process);
298 xbt_fifo_push(msg_remote_proc->msg_to_receive_queue_meas, msg);
300 /* wait for the receiver */
301 SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
303 /* creates simix action and waits its ends, waits in the sender host
305 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
306 name, SIMIX_host_get_name(SIMIX_host_self()),
307 SIMIX_host_get_name(sock_data->to_host), sock->peer_port, size);
309 act = SIMIX_action_communicate(SIMIX_host_self(), sock_data->to_host,
311 SIMIX_register_action_to_condition(act, sock_data->cond);
312 SIMIX_cond_wait(sock_data->cond, sock_data->mutex);
313 SIMIX_unregister_action_to_condition(act, sock_data->cond);
314 /* error treatmeant (FIXME) */
316 /* cleanup structures */
317 SIMIX_action_destroy(act);
319 SIMIX_mutex_unlock(sock_data->mutex);
322 int gras_trp_sg_chunk_recv(gras_socket_t sock,
323 char *data, unsigned long int size)
325 gras_trp_sg_sock_data_t *sock_data;
326 gras_trp_sg_sock_data_t *remote_sock_data;
327 gras_socket_t remote_socket = NULL;
329 gras_msg_procdata_t msg_procdata =
330 (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
331 gras_trp_procdata_t trp_proc =
332 (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
334 xbt_assert0(sock->meas,
335 "SG chunk exchange shouldn't be used on non-measurement sockets");
336 xbt_queue_shift_timed(trp_proc->meas_selectable_sockets,
339 if (remote_socket == NULL) {
340 THROW0(timeout_error, 0, "Timeout");
343 remote_sock_data = (gras_trp_sg_sock_data_t *) remote_socket->data;
344 msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue_meas);
346 sock_data = (gras_trp_sg_sock_data_t *) sock->data;
348 /* ok, I'm here, you can continue the communication */
349 SIMIX_cond_signal(remote_sock_data->cond);
351 SIMIX_mutex_lock(remote_sock_data->mutex);
352 /* wait for communication end */
353 SIMIX_cond_wait(remote_sock_data->cond, remote_sock_data->mutex);
355 if (msg_got->payl_size != size)
356 THROW5(mismatch_error, 0,
357 "Got %d bytes when %ld where expected (in %s->%s:%d)",
358 msg_got->payl_size, size,
359 SIMIX_host_get_name(sock_data->to_host),
360 SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
363 memcpy(data, msg_got->payl, size);
366 xbt_free(msg_got->payl);
369 SIMIX_mutex_unlock(remote_sock_data->mutex);