Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correct some bugs
[simgrid.git] / src / gras_simix / Msg / gras_simix_sg_msg.c
1 /* $Id$ */
2
3 /* messaging - Function related to messaging code specific to SG            */
4
5 /* Copyright (c) 2003-2005 Martin Quinson. All rights reserved.             */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/ex.h"
11
12 #include "gras_simix/Virtu/gras_simix_virtu_sg.h"
13
14 #include "gras_simix/Msg/gras_simix_msg_private.h"
15
16 #include "gras_simix/DataDesc/gras_simix_datadesc_interface.h"
17 #include "gras_simix/Transport/gras_simix_transport_interface.h" /* gras_trp_chunk_send/recv */
18 #include "gras_simix/Transport/gras_simix_transport_private.h" /* sock->data */
19
20 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
21 XBT_LOG_DEFAULT_CATEGORY(gras_msg);
22
23 typedef void* gras_trp_bufdata_;
24
25 void gras_msg_send_ext(gras_socket_t   sock,
26                      e_gras_msg_kind_t kind,
27                        unsigned long int ID,
28                        gras_msgtype_t  msgtype,
29                        void           *payload) {
30
31         smx_action_t act; /* simix action */
32         gras_trp_sg_sock_data_t *sock_data; 
33         gras_hostdata_t *hd;
34         gras_trp_procdata_t trp_remote_proc;
35         gras_msg_procdata_t msg_remote_proc;
36         gras_msg_t msg; /* message to send */
37   int whole_payload_size=0; /* msg->payload_size is used to memcpy the payload. 
38                                This is used to report the load onto the simulator. It also counts the size of pointed stuff */
39
40         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
41         hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
42
43   xbt_assert1(!gras_socket_is_meas(sock), 
44               "Asked to send a message on the measurement socket %p", sock);
45         
46         /* got the mutex my port */
47         DEBUG1("Sock port %d",sock->port);
48         SIMIX_mutex_lock(hd->mutex_port[sock->port]);
49
50         /*initialize gras message */
51         msg = xbt_new(s_gras_msg_t,1);
52         msg->expe = sock;
53         msg->kind = kind;
54         msg->type = msgtype;
55         msg->ID = ID;
56   if (kind == e_gras_msg_kind_rpcerror) {
57      /* error on remote host, carfull, payload is an exception */
58     msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t"));
59     msg->payl=xbt_malloc(msg->payl_size);
60     whole_payload_size = gras_datadesc_copy(gras_datadesc_by_name("ex_t"),
61                                             payload,msg->payl);
62   } else if (kind == e_gras_msg_kind_rpcanswer) {
63     msg->payl_size=gras_datadesc_size(msgtype->answer_type);
64     msg->payl=xbt_malloc(msg->payl_size);
65     if (msgtype->answer_type)
66       whole_payload_size = gras_datadesc_copy(msgtype->answer_type,
67                                               payload, msg->payl);
68   } else {
69     msg->payl_size=gras_datadesc_size(msgtype->ctn_type);
70     msg->payl=msg->payl_size?xbt_malloc(msg->payl_size):NULL;
71     if (msgtype->ctn_type)
72       whole_payload_size = gras_datadesc_copy(msgtype->ctn_type,
73                                               payload, msg->payl);
74   }
75         /* put message on msg_queue */
76         msg_remote_proc = (gras_msg_procdata_t)gras_libdata_by_name_from_remote("gras_msg",sock_data->to_process);
77         xbt_fifo_push(msg_remote_proc->msg_to_receive_queue,msg);
78         
79         /* wake-up the receiver */
80         trp_remote_proc = (gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sock_data->to_process);
81         xbt_fifo_push(trp_remote_proc->active_socket,sock);
82
83         SIMIX_cond_signal(trp_remote_proc->cond);
84
85         /* wait for the receiver */
86         SIMIX_cond_wait(hd->cond_port[sock->port], hd->mutex_port[sock->port]);
87
88         /* creates simix action and waits its ends, waits in the sender host condition*/
89         act = SIMIX_action_communicate(sock_data->to_host, SIMIX_host_self(),msgtype->name, msg->payl_size, -1);
90         SIMIX_register_action_to_condition(act,hd->cond_port[sock->port]);
91         SIMIX_register_condition_to_action(act,hd->cond_port[sock->port]);
92
93   VERB5("Sending to %s(%s) a message type '%s' kind '%s' ID %lu",
94         SIMIX_host_get_name(sock_data->to_host),SIMIX_process_get_name(sock_data->to_process),
95         msg->type->name,e_gras_msg_kind_names[msg->kind],       msg->ID);
96         
97         SIMIX_cond_wait(hd->cond_port[sock->port], hd->mutex_port[sock->port]);
98         /* error treatmeant */
99
100         /* cleanup structures */
101         SIMIX_action_destroy(act);
102         SIMIX_mutex_unlock(hd->mutex_port[sock->port]);
103
104         VERB0("Message sent");
105
106 /*
107   if (XBT_LOG_ISENABLED(gras_msg,xbt_log_priority_verbose)) {
108      asprintf(&name,"type:'%s';kind:'%s';ID %lu from %s:%d to %s:%d",
109               msg->type->name, e_gras_msg_kind_names[msg->kind], msg->ID,
110               gras_os_myname(),gras_os_myport(),
111               gras_socket_peer_name(sock), gras_socket_peer_port(sock));
112      task=MSG_task_create(name,0,
113                           ((double)whole_payload_size),msg);
114      free(name);
115   } else {
116      task=MSG_task_create(msg->type->name,0,
117                           ((double)whole_payload_size),msg);
118   }
119    
120         sock->bufdata = msg;
121         SIMIX_cond_signal(gras_libdata_by_name_from_remote("trp", sock->data->to_process)->cond);
122   DEBUG1("Prepare to send a message to %s",
123          MSG_host_get_name (sock_data->to_host));
124   if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) 
125     THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60");
126
127   VERB5("Sent to %s(%d) a message type '%s' kind '%s' ID %lu",
128         MSG_host_get_name(sock_data->to_host),sock_data->to_PID,
129         msg->type->name,
130         e_gras_msg_kind_names[msg->kind],
131         msg->ID);
132 */
133 }
134 /*
135  * receive the next message on the given socket.  
136  */
137 void
138 gras_msg_recv(gras_socket_t    sock,
139               gras_msg_t       msg) {
140
141         gras_trp_sg_sock_data_t *sock_data; 
142         gras_hostdata_t *remote_hd;
143   gras_msg_t msg_got;
144         gras_msg_procdata_t msg_procdata = (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
145
146   xbt_assert1(!gras_socket_is_meas(sock), 
147               "Asked to receive a message on the measurement socket %p", sock);
148
149   xbt_assert0(msg,"msg is an out parameter of gras_msg_recv...");
150
151         sock_data = (gras_trp_sg_sock_data_t *)sock->data;
152         DEBUG3("Remote host %s, Remote Port: %d Local port %d", SIMIX_host_get_name(sock_data->to_host), sock->peer_port, sock->port);
153         remote_hd = (gras_hostdata_t *)SIMIX_host_get_data(sock_data->to_host);
154
155         if (xbt_fifo_size(msg_procdata->msg_to_receive_queue) == 0 ) {
156                 THROW_IMPOSSIBLE;
157         }
158         DEBUG1("Size msg_to_receive buffer: %d", xbt_fifo_size(msg_procdata->msg_to_receive_queue));
159   msg_got = xbt_fifo_shift(msg_procdata->msg_to_receive_queue);
160
161         SIMIX_mutex_lock(remote_hd->mutex_port[sock->peer_port]);
162 /* ok, I'm here, you can continuate the communication */
163         SIMIX_cond_signal(remote_hd->cond_port[sock->peer_port]);
164
165 /* wait for communication end */
166         SIMIX_cond_wait(remote_hd->cond_port[sock->peer_port],remote_hd->mutex_port[sock->peer_port]);
167
168         msg_got->expe= msg->expe;
169   memcpy(msg,msg_got,sizeof(s_gras_msg_t));
170         xbt_free(msg_got);
171         SIMIX_mutex_unlock(remote_hd->mutex_port[sock->peer_port]);
172
173         VERB3("Received a message type '%s' kind '%s' ID %lu",// from %s",
174         msg->type->name,
175         e_gras_msg_kind_names[msg->kind],
176         msg->ID);
177 }