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 "gras_simix/Msg/gras_simix_msg_private.h"
17 #include "gras_simix_transport_private.h"
18 #include "gras_simix/Virtu/gras_simix_virtu_sg.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg,gras_trp,"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, gras_sg_portrec_t *hpd);
30 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
31 /* OUT */ gras_socket_t sock);
32 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
33 /* OUT */ gras_socket_t sock);
34 void gras_trp_sg_socket_close(gras_socket_t sd);
36 void gras_trp_sg_chunk_send_raw(gras_socket_t sd,
38 unsigned long int size);
39 void gras_trp_sg_chunk_send(gras_socket_t sd,
41 unsigned long int size,
44 int gras_trp_sg_chunk_recv(gras_socket_t sd,
46 unsigned long int size);
49 *** Specific plugin part
52 int placeholder; /* nothing plugin specific so far */
53 } gras_trp_sg_plug_data_t;
59 static void find_port(gras_hostdata_t *hd, int port,
60 gras_sg_portrec_t *hpd) {
64 xbt_assert0(hd,"Please run gras_process_init on each process");
66 xbt_dynar_foreach(hd->ports, cpt, pr) {
67 if (pr.port == port) {
68 memcpy(hpd,&pr,sizeof(gras_sg_portrec_t));
72 THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port);
77 gras_trp_sg_setup(gras_trp_plugin_t plug) {
79 gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1);
83 plug->socket_client = gras_trp_sg_socket_client;
84 plug->socket_server = gras_trp_sg_socket_server;
85 plug->socket_close = gras_trp_sg_socket_close;
87 plug->raw_send = gras_trp_sg_chunk_send_raw;
88 plug->send = gras_trp_sg_chunk_send;
89 plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv;
91 plug->flush = NULL; /* nothing cached */
94 void gras_trp_sg_socket_client(gras_trp_plugin_t self,
95 /* OUT */ gras_socket_t sock){
100 gras_hostdata_t *local_hd;
101 gras_trp_sg_sock_data_t *data;
102 gras_sg_portrec_t pr;
105 /* make sure this socket will reach someone */
106 if (!(peer=SIMIX_host_get_by_name(sock->peer_name)))
107 THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name);
109 if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer)))
110 THROW1(mismatch_error,0,
111 "can't connect to %s: no process on this host",
115 find_port(hd,sock->peer_port,&pr);
117 if (e.category == mismatch_error) {
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);
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);
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);
136 /* create the socket */
137 data = xbt_new(gras_trp_sg_sock_data_t,1);
138 data->from_process = SIMIX_process_self();
139 data->to_process = pr.process;
140 data->to_host = peer;
142 /* searches for a free port on this host */
143 local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
144 for (i=1;i<65536;i++) {
145 if (local_hd->cond_port[i] == NULL)
148 if (i == 65536) THROW0(system_error,0,"No port free");
150 local_hd->cond_port[i] = SIMIX_cond_init();
151 local_hd->mutex_port[i] = SIMIX_mutex_init();
156 DEBUG5("%s (PID %ld) connects in %s mode to %s:%d",
157 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
158 sock->meas?"meas":"regular",
159 sock->peer_name,sock->peer_port);
162 void gras_trp_sg_socket_server(gras_trp_plugin_t self,
165 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
166 //gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
167 gras_sg_portrec_t pr;
168 gras_trp_sg_sock_data_t *data;
171 const char *host=SIMIX_host_get_name(SIMIX_host_self());
175 xbt_assert0(hd,"Please run gras_process_init on each process");
177 sock->accepting = 0; /* no such nuisance in SG */
180 find_port(hd,sock->port,&pr);
183 if (e.category == mismatch_error)
190 THROW2(mismatch_error,0,
191 "can't listen on address %s:%d: port already in use.",
194 pr.port = sock->port;
195 pr.meas = sock->meas;
196 pr.process = SIMIX_process_self();
197 xbt_dynar_push(hd->ports,&pr);
199 hd->cond_port[sock->port] = SIMIX_cond_init();
200 hd->mutex_port[sock->port] = SIMIX_mutex_init();
202 /* Create the socket */
203 data = xbt_new(gras_trp_sg_sock_data_t,1);
204 data->from_process = SIMIX_process_self();
205 data->to_process = NULL;
206 data->to_host = SIMIX_host_self();
210 VERB6("'%s' (%ld) ears on %s:%d%s (%p)",
211 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(),
212 host,sock->port,sock->meas? " (mode meas)":"",sock);
216 void gras_trp_sg_socket_close(gras_socket_t sock){
217 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
218 gras_socket_t sock_iter;
221 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
223 gras_sg_portrec_t pr;
225 XBT_IN1(" (sock=%p)",sock);
229 xbt_assert0(hd,"Please run gras_process_init on each process");
234 /* search for a socket in the list that is using the mutex and condition. It can happen because we create 2 sockets to communicate (incomming and outgoing) */
236 xbt_dynar_foreach(sockets,cursor,sock_iter) {
237 if (sock_iter->port == sock->port) {
242 /* if not found, it is the last socket opened in this port and we can free the mutex and condition */
244 SIMIX_cond_destroy(hd->cond_port[sock->port]);
245 hd->cond_port[sock->port] = NULL;
246 SIMIX_mutex_destroy(hd->mutex_port[sock->port]);
247 hd->mutex_port[sock->port] = NULL;
251 if (sock->incoming && !sock->outgoing && sock->port >= 0) {
252 /* server mode socket. Unregister it from 'OS' tables */
253 xbt_dynar_foreach(hd->ports, cpt, pr) {
254 DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports));
255 if (pr.port == sock->port) {
256 xbt_dynar_cursor_rm(hd->ports, &cpt);
261 WARN2("socket_close called on the unknown incoming socket %p (port=%d)",
273 void gras_trp_sg_chunk_send(gras_socket_t sock,
275 unsigned long int size,
276 int stable_ignored) {
277 gras_trp_sg_chunk_send_raw(sock,data,size);
280 void gras_trp_sg_chunk_send_raw(gras_socket_t sock,
282 unsigned long int size) {
284 static unsigned int count=0;
286 smx_action_t act; /* simix action */
287 gras_trp_sg_sock_data_t *sock_data;
289 gras_hostdata_t *remote_hd;
290 gras_trp_procdata_t trp_remote_proc;
291 gras_msg_procdata_t msg_remote_proc;
292 gras_msg_t msg; /* message to send */
294 sock_data = (gras_trp_sg_sock_data_t *)sock->data;
295 hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
296 remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
298 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
301 sprintf(name,"Chunk[%d]",count++);
302 /*initialize gras message */
303 msg = xbt_new(s_gras_msg_t,1);
308 msg->payl=(void*)xbt_malloc(size);
309 memcpy(msg->payl,data,size);
314 /* put message on msg_queue */
315 msg_remote_proc = (gras_msg_procdata_t)gras_libdata_by_name_from_remote("gras_msg",sock_data->to_process);
316 xbt_fifo_push(msg_remote_proc->msg_to_receive_queue,msg);
318 /* wake-up the receiver */
319 trp_remote_proc = (gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sock_data->to_process);
320 SIMIX_cond_signal(trp_remote_proc->cond);
322 SIMIX_mutex_lock(remote_hd->mutex_port[sock->peer_port]);
323 /* wait for the receiver */
324 SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port], remote_hd->mutex_port[sock->peer_port]);
326 /* creates simix action and waits its ends, waits in the sender host condition*/
327 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
328 name, SIMIX_host_get_name(SIMIX_host_self()),
329 SIMIX_host_get_name(sock_data->to_host), sock->peer_port,size);
331 act = SIMIX_action_communicate(sock_data->to_host, SIMIX_host_self(),name, size, -1);
332 SIMIX_register_action_to_condition(act,remote_hd->cond_port[sock->peer_port]);
333 SIMIX_register_condition_to_action(act,remote_hd->cond_port[sock->peer_port]);
335 SIMIX_host_get_name(sock_data->to_host),SIMIX_process_get_name(sock_data->to_process),
337 SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port], remote_hd->mutex_port[sock->peer_port]);
338 /* error treatmeant */
340 /* cleanup structures */
341 SIMIX_action_destroy(act);
342 SIMIX_mutex_unlock(remote_hd->mutex_port[sock->peer_port]);
343 SIMIX_cond_signal(remote_hd->cond_port[sock->peer_port]);
347 gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data;
348 sg_task_data_t *task_data;
350 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
352 sprintf(name,"Chunk[%d]",count++);
354 task_data=xbt_new(sg_task_data_t,1);
355 task_data->size = size;
357 task_data->data=(void*)xbt_malloc(size);
358 memcpy(task_data->data,data,size);
360 task_data->data = NULL;
363 task=MSG_task_create(name,0,((double)size),task_data);
365 DEBUG5("send chunk %s from %s to %s:%d (size=%ld)",
366 name, MSG_host_get_name(MSG_host_self()),
367 MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size);
368 if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) {
369 THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
373 int gras_trp_sg_chunk_recv(gras_socket_t sock,
375 unsigned long int size){
376 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
377 gras_trp_sg_sock_data_t *sock_data;
378 gras_hostdata_t *remote_hd;
379 gras_hostdata_t *local_hd;
381 gras_msg_procdata_t msg_procdata = (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
383 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
385 SIMIX_mutex_lock(pd->mutex);
386 if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
387 SIMIX_cond_wait_timeout(pd->cond,pd->mutex,60);
389 if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
390 THROW0(timeout_error,0,"Timeout");
392 SIMIX_mutex_unlock(pd->mutex);
394 sock_data = (gras_trp_sg_sock_data_t *)sock->data;
395 DEBUG3("Remote host %s, Remote Port: %d Local port %d", SIMIX_host_get_name(sock_data->to_host), sock->peer_port, sock->port);
396 remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
397 local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
401 msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
403 SIMIX_mutex_lock(local_hd->mutex_port[sock->port]);
404 /* ok, I'm here, you can continue the communication */
405 SIMIX_cond_signal(local_hd->cond_port[sock->port]);
407 /* wait for communication end */
408 SIMIX_cond_wait(local_hd->cond_port[sock->port],local_hd->mutex_port[sock->port]);
411 if (msg_got->payl_size != size)
412 THROW5(mismatch_error,0,
413 "Got %d bytes when %ld where expected (in %s->%s:%d)",
414 msg_got->payl_size, size,
415 SIMIX_host_get_name(sock_data->to_host),
416 SIMIX_host_get_name(SIMIX_host_self()), sock->peer_port);
418 memcpy(data,msg_got->payl,size);
421 xbt_free(msg_got->payl);
423 SIMIX_cond_wait(local_hd->cond_port[sock->port],local_hd->mutex_port[sock->port]);
424 SIMIX_mutex_unlock(local_hd->mutex_port[sock->port]);
427 gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
430 sg_task_data_t *task_data;
431 gras_trp_sg_sock_data_t *sock_data = sock->data;
433 xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets");
435 DEBUG4("recv chunk on %s -> %s:%d (size=%ld)",
436 MSG_host_get_name(sock_data->to_host),
437 MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size);
438 if (MSG_task_get_with_time_out(&task,
439 (sock->meas ? pd->measChan : pd->chan),
441 THROW0(system_error,0,"Error in MSG_task_get()");
442 DEBUG1("Got chuck %s",MSG_task_get_name(task));
444 task_data = MSG_task_get_data(task);
445 if (task_data->size != size)
446 THROW5(mismatch_error,0,
447 "Got %d bytes when %ld where expected (in %s->%s:%d)",
448 task_data->size, size,
449 MSG_host_get_name(sock_data->to_host),
450 MSG_host_get_name(MSG_host_self()), sock_data->to_chan);
452 memcpy(data,task_data->data,size);
454 free(task_data->data);
457 if (MSG_task_destroy(task) != MSG_OK)
458 THROW0(system_error,0,"Error in MSG_task_destroy()");