1 /* messaging - Function related to messaging code specific to SG */
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "gras/Virtu/virtu_sg.h"
13 #include "gras/Msg/msg_private.h"
15 #include "gras/DataDesc/datadesc_interface.h"
16 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
17 #include "gras/Transport/transport_private.h" /* sock->data */
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
21 typedef void *gras_trp_bufdata_;
23 gras_msg_t gras_msg_recv_any(void)
25 gras_trp_procdata_t trp_proc =
26 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
28 /* Build a dynar of all communications I could get something from */
29 xbt_dynar_t comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
30 unsigned int cursor = 0;
32 smx_comm_t comm = NULL;
33 gras_socket_t sock = NULL;
34 gras_trp_sg_sock_data_t sock_data;
35 xbt_dynar_foreach(trp_proc->sockets, cursor, sock) {
36 sock_data = (gras_trp_sg_sock_data_t) sock->data;
40 ("Consider socket %p (data:%p; Here rdv: %p; Remote rdv: %p; Comm %p) to get a message",
43 SIMIX_process_self())? sock_data->
44 rdv_server : sock_data->rdv_client,
46 SIMIX_process_self())? sock_data->
47 rdv_client : sock_data->rdv_server, sock_data->comm_recv);
50 /* The following assert fails in some valid conditions, we need to
51 * change the code downward looking for the socket again.
53 * For now it relies on the facts (A) that sockets and comms are aligned
54 * (B) every sockets has a posted irecv in comms
56 * This is not trivial because we need that alignment to hold after the waitany(), so
57 * after other processes get scheduled.
59 * I cannot think of conditions where they get desynchronized (A violated) as long as
60 * 1) only the listener calls that function
61 * 2) Nobody but the listener removes sockets from that set (in main listener loop)
62 * 3) New sockets are added at the end, and signified ASAP to the listener (by awaking him)
63 * The throw bellow ensures that B is never violated without failing out loudly.
65 * We cannot search by comparing the comm object pointer that object got
66 * freed by the waiting process (down in smx_network, in
67 * comm_wait_for_completion or comm_cleanup). So, actually, we could
68 * use that pointer since that's a dangling pointer, but no one changes it.
69 * I still feel unconfortable with using dangling pointers, even if that would
70 * let the code work even if A and/or B are violated, provided that
71 * (C) the new irecv is never posted before we return from waitany to that function.
73 * Another approach, robust to B violation would be to retraverse the socks dynar with
74 * an iterator, incremented only when the socket has a comm. And we've the right socket
75 * when that iterator is equal to "got", the result of waitany. Not needed if B holds.
77 xbt_assert1(sock_data->comm_recv,
78 "Comm_recv of socket %p is empty; please report that nasty bug",
82 VERB3("Copy comm_recv %p rdv:%p (other rdv:%p)",
85 SIMIX_process_self())? sock_data->
86 rdv_server : sock_data->rdv_client,
88 SIMIX_process_self())? sock_data->
89 rdv_client : sock_data->rdv_server);
90 xbt_dynar_push(comms, &(sock_data->comm_recv));
92 VERB1("Wait on %ld 'sockets'", xbt_dynar_length(comms));
93 /* Wait for the end of any of these communications */
94 got = SIMIX_network_waitany(comms);
96 /* retrieve the message sent in that communication */
97 xbt_dynar_get_cpy(comms, got, &(comm));
98 msg = SIMIX_communication_get_data(comm);
99 VERB1("Got something. Communication %p's over", comm);
101 /* Reinstall a waiting communication on that rdv */
102 /* Get the sock again
103 * For that, we use the fact that */
104 sock = xbt_dynar_get_as(trp_proc->sockets, got, gras_socket_t);
105 /* xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
106 sock_data = (gras_trp_sg_sock_data_t) sock->data;
107 if (sock_data->comm_recv && sock_data->comm_recv == comm)
111 sock_data = (gras_trp_sg_sock_data_t) sock->data;
112 sock_data->comm_recv =
113 SIMIX_network_irecv(sock_data->rdv_server != NULL ?
114 //(sock_data->server==SIMIX_process_self())?
115 sock_data->rdv_server
116 : sock_data->rdv_client, NULL, 0);
122 void gras_msg_send_ext(gras_socket_t sock,
123 e_gras_msg_kind_t kind,
124 unsigned long int ID,
125 gras_msgtype_t msgtype, void *payload)
127 int whole_payload_size = 0; /* msg->payload_size is used to memcpy the payload.
128 This is used to report the load onto the simulator. It also counts the size of pointed stuff */
129 gras_msg_t msg; /* message to send */
131 gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
133 smx_rdv_t target_rdv =
134 (sock_data->server ==
135 SIMIX_process_self())? sock_data->
136 rdv_client : sock_data->rdv_server;
138 /*initialize gras message */
139 msg = xbt_new(s_gras_msg_t, 1);
145 VERB2("Send msg %s to rdv %p", msgtype->name, target_rdv);
147 if (kind == e_gras_msg_kind_rpcerror) {
148 /* error on remote host, careful, payload is an exception */
149 msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
150 msg->payl = xbt_malloc(msg->payl_size);
152 gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"), payload,
154 } else if (kind == e_gras_msg_kind_rpcanswer) {
155 msg->payl_size = gras_datadesc_size(msgtype->answer_type);
157 msg->payl = xbt_malloc(msg->payl_size);
161 if (msgtype->answer_type)
162 whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
165 msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
166 msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
167 if (msgtype->ctn_type)
168 whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
172 SIMIX_network_send(target_rdv, whole_payload_size, -1, -1, &msg,
173 sizeof(void *), &comm, msg);
175 VERB0("Message sent");