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 /* If 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("Consider receiving messages from on 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 sock = xbt_dynar_get_as(trp_proc->sockets, got, gras_socket_t);
100 sock_data = (gras_trp_sg_sock_data_t) sock->data;
101 VERB3("Got something. Communication %p's over rdv_server=%p, rdv_client=%p",
102 comm,sock_data->rdv_server,sock_data->rdv_client);
103 SIMIX_communication_destroy(comm);
105 /* Reinstall a waiting communication on that rdv */
106 /* xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
107 sock_data = (gras_trp_sg_sock_data_t) sock->data;
108 if (sock_data->comm_recv && sock_data->comm_recv == comm)
112 sock_data->comm_recv =
113 SIMIX_network_irecv(sock_data->rdv_server != NULL ?
114 sock_data->rdv_server
115 : sock_data->rdv_client, NULL, 0);
121 void gras_msg_send_ext(gras_socket_t sock,
122 e_gras_msg_kind_t kind,
123 unsigned long int ID,
124 gras_msgtype_t msgtype, void *payload)
126 int whole_payload_size = 0; /* msg->payload_size is used to memcpy the payload.
127 This is used to report the load onto the simulator. It also counts the size of pointed stuff */
128 gras_msg_t msg; /* message to send */
130 gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
132 smx_rdv_t target_rdv =
133 (sock_data->server ==
134 SIMIX_process_self())? sock_data->
135 rdv_client : sock_data->rdv_server;
137 /*initialize gras message */
138 msg = xbt_new(s_gras_msg_t, 1);
145 VERB4("Send msg %s (%s) to rdv %p sock %p",
146 msgtype->name, e_gras_msg_kind_names[kind], target_rdv, sock);
148 if (kind == e_gras_msg_kind_rpcerror) {
149 /* error on remote host, careful, payload is an exception */
150 msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
151 msg->payl = xbt_malloc(msg->payl_size);
153 gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"), payload,
155 } else if (kind == e_gras_msg_kind_rpcanswer) {
156 msg->payl_size = gras_datadesc_size(msgtype->answer_type);
158 msg->payl = xbt_malloc(msg->payl_size);
162 if (msgtype->answer_type)
163 whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
166 msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
167 msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
168 if (msgtype->ctn_type)
169 whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
173 SIMIX_network_send(target_rdv, whole_payload_size, -1, -1, &msg,
174 sizeof(void *), &comm, msg);
176 VERB0("Message sent");