Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Port GRAS to smx_network infrastructure. Kinda working, but not tested enough yet
[simgrid.git] / src / gras / Msg / sg_msg.c
1 /* messaging - Function related to messaging code specific to SG            */
2
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/ex.h"
10
11 #include "gras/Virtu/virtu_sg.h"
12
13 #include "gras/Msg/msg_private.h"
14
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 */
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_msg);
20
21 typedef void *gras_trp_bufdata_;
22
23 gras_msg_t gras_msg_recv_any(void) {
24   gras_trp_procdata_t trp_proc =
25       (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
26   gras_msg_t msg;
27   /* Build a dynar of all communications I could get something from */
28   xbt_dynar_t comms = xbt_dynar_new(sizeof(smx_comm_t),NULL);
29   unsigned int cursor = 0;
30   int got = 0;
31   smx_comm_t comm = NULL;
32   gras_socket_t sock = NULL;
33   gras_trp_sg_sock_data_t sock_data;
34   xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
35     sock_data = (gras_trp_sg_sock_data_t) sock->data;
36
37
38     DEBUG5("Consider socket %p (data:%p; Here rdv: %p; Remote rdv: %p; Comm %p) to get a message",
39           sock,sock_data,
40           (sock_data->server==SIMIX_process_self())?sock_data->rdv_server:sock_data->rdv_client,
41           (sock_data->server==SIMIX_process_self())?sock_data->rdv_client:sock_data->rdv_server,
42               sock_data->comm_recv);
43
44
45     /* The following assert fails in some valid conditions, we need to
46      * change the code downward looking for the socket again.
47      *
48      * For now it relies on the facts (A) that sockets and comms are aligned
49      *                                (B) every sockets has a posted irecv in comms
50      *
51      * This is not trivial because we need that alignment to hold after the waitany(), so
52      * after other processes get scheduled.
53      *
54      * I cannot think of conditions where they get desynchronized (A violated) as long as
55      *    1) only the listener calls that function
56      *    2) Nobody but the listener removes sockets from that set (in main listener loop)
57      *    3) New sockets are added at the end, and signified ASAP to the listener (by awaking him)
58      * The throw bellow ensures that B is never violated without failing out loudly.
59      *
60      * We cannot search by comparing the comm object pointer that object got
61      *    freed by the waiting process (down in smx_network, in
62      *    comm_wait_for_completion or comm_cleanup). So, actually, we could
63      *    use that pointer since that's a dangling pointer, but no one changes it.
64      * I still feel unconfortable with using dangling pointers, even if that would
65      *    let the code work even if A and/or B are violated, provided that
66      *    (C) the new irecv is never posted before we return from waitany to that function.
67      *
68      * Another approach, robust to B violation would be to retraverse the socks dynar with
69      *    an iterator, incremented only when the socket has a comm. And we've the right socket
70      *    when that iterator is equal to "got", the result of waitany. Not needed if B holds.
71      */
72     xbt_assert1(sock_data->comm_recv,
73         "Comm_recv of socket %p is empty; please report that nasty bug",sock);
74     /* End of paranoia */
75
76     VERB3("Copy comm_recv %p rdv:%p (other rdv:%p)",
77         sock_data->comm_recv,
78         (sock_data->server==SIMIX_process_self())?sock_data->rdv_server:sock_data->rdv_client,
79         (sock_data->server==SIMIX_process_self())?sock_data->rdv_client:sock_data->rdv_server);
80     xbt_dynar_push(comms,&(sock_data->comm_recv));
81   }
82   VERB1("Wait on %ld 'sockets'",xbt_dynar_length(comms));
83   /* Wait for the end of any of these communications */
84   got = SIMIX_network_waitany(comms);
85
86   /* retrieve the message sent in that communication */
87   xbt_dynar_get_cpy(comms,got,&(comm));
88   msg=SIMIX_communication_get_data(comm);
89   VERB1("Got something. Communication %p's over",comm);
90
91   /* Reinstall a waiting communication on that rdv */
92   /* Get the sock again
93    * For that, we use the fact that */
94   sock=xbt_dynar_get_as(trp_proc->sockets,got,gras_socket_t);
95 /*  xbt_dynar_foreach(trp_proc->sockets,cursor,sock) {
96     sock_data = (gras_trp_sg_sock_data_t) sock->data;
97     if (sock_data->comm_recv && sock_data->comm_recv == comm)
98       break;
99   }
100   */
101   sock_data = (gras_trp_sg_sock_data_t) sock->data;
102   sock_data->comm_recv = SIMIX_network_irecv(
103       sock_data->rdv_server!=NULL?
104       //(sock_data->server==SIMIX_process_self())?
105                                                 sock_data->rdv_server
106                                                :sock_data->rdv_client,
107       NULL,0);
108
109   return msg;
110 }
111
112
113 void gras_msg_send_ext(gras_socket_t sock,
114                        e_gras_msg_kind_t kind,
115                        unsigned long int ID,
116                        gras_msgtype_t msgtype, void *payload)
117 {
118   int whole_payload_size = 0;   /* msg->payload_size is used to memcpy the payload.
119                                    This is used to report the load onto the simulator. It also counts the size of pointed stuff */
120   gras_msg_t msg;               /* message to send */
121   smx_comm_t comm;
122   gras_trp_sg_sock_data_t sock_data = (gras_trp_sg_sock_data_t) sock->data;
123
124   smx_rdv_t target_rdv = (sock_data->server==SIMIX_process_self())?sock_data->rdv_client
125                                                                   :sock_data->rdv_server;
126
127   /*initialize gras message */
128   msg = xbt_new(s_gras_msg_t, 1);
129   msg->expe = sock;
130   msg->kind = kind;
131   msg->type = msgtype;
132   msg->ID = ID;
133
134   VERB2("Send msg %s to rdv %p", msgtype->name,target_rdv);
135
136   if (kind == e_gras_msg_kind_rpcerror) {
137     /* error on remote host, careful, payload is an exception */
138     msg->payl_size = gras_datadesc_size(gras_datadesc_by_name("ex_t"));
139     msg->payl = xbt_malloc(msg->payl_size);
140     whole_payload_size = gras_datadesc_memcpy(gras_datadesc_by_name("ex_t"),
141                                               payload, msg->payl);
142   } else if (kind == e_gras_msg_kind_rpcanswer) {
143     msg->payl_size = gras_datadesc_size(msgtype->answer_type);
144     if (msg->payl_size)
145       msg->payl = xbt_malloc(msg->payl_size);
146     else
147       msg->payl = NULL;
148
149     if (msgtype->answer_type)
150       whole_payload_size = gras_datadesc_memcpy(msgtype->answer_type,
151                                                 payload, msg->payl);
152   } else {
153     msg->payl_size = gras_datadesc_size(msgtype->ctn_type);
154     msg->payl = msg->payl_size ? xbt_malloc(msg->payl_size) : NULL;
155     if (msgtype->ctn_type)
156       whole_payload_size = gras_datadesc_memcpy(msgtype->ctn_type,
157                                                 payload, msg->payl);
158   }
159
160   SIMIX_network_send(target_rdv, whole_payload_size,-1,-1,&msg,sizeof(void*),&comm,msg);
161
162   VERB0("Message sent");
163
164 }
165