Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge conflicts
[simgrid.git] / examples / gras / replay / replay.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* This example replays a trace as produced by examples/simdag/dax, or other means
8  * This is mainly interesting when run on real platforms, to validate the results
9  * given in the simulator when running SimDag.
10  */
11
12 #include "xbt/ex.h"
13 #include "xbt/log.h"
14 #include "xbt/str.h"
15 #include "xbt/dynar.h"
16 #include "xbt/synchro.h"
17 #include "workload.h"
18 #include "gras.h"
19 #include "amok/peermanagement.h"
20 #include <stdio.h>
21
22 #include "simix/simix.h"
23
24 int master(int argc, char *argv[]);
25 int worker(int argc, char *argv[]);
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(replay, "Messages specific to this example");
28
29 static void declare_msg()
30 {
31   amok_pm_init();
32   xbt_workload_declare_datadesc();
33   gras_msgtype_declare("go", NULL);
34   gras_msgtype_declare("commands",
35                        gras_datadesc_dynar(gras_datadesc_by_name
36                                            ("xbt_workload_elm_t"),
37                                            xbt_workload_elm_free_voidp));
38   gras_msgtype_declare("chunk",
39                        gras_datadesc_by_name("xbt_workload_data_chunk_t"));
40 }
41
42 int master(int argc, char *argv[])
43 {
44
45   gras_init(&argc, argv);
46   declare_msg();
47
48
49   xbt_assert(argc == 3, "usage: replay_master tracefile port");
50   gras_socket_server(atoi(argv[2]));    /* open my master socket, even if I don't use it */
51   xbt_dynar_t peers = amok_pm_group_new("replay");      /* group of slaves */
52   xbt_peer_t peer;
53   xbt_dynar_t cmds = xbt_workload_parse_file(argv[1]);
54   xbt_workload_sort_who_date(cmds);
55   unsigned int cursor;
56   xbt_workload_elm_t cmd;
57   xbt_dict_cursor_t dict_cursor;
58
59   xbt_dict_t pals_int = xbt_dict_new();
60   xbt_dynar_foreach(cmds, cursor, cmd) {
61     int *p = xbt_dict_get_or_null(pals_int, cmd->who);
62     if (!p) {
63       p = (int *) 0xBEAF;
64       xbt_dict_set(pals_int, cmd->who, &p, NULL);
65     }
66   }
67
68   /* friends, we're ready. Come and play */
69   XBT_INFO("Wait for peers for a while. I need %d peers",
70         xbt_dict_length(pals_int));
71   while (xbt_dynar_length(peers) < xbt_dict_length(pals_int)) {
72     TRY {
73       gras_msg_handle(20);
74     }
75     CATCH_ANONYMOUS {
76       xbt_dynar_foreach(peers, cursor, peer) {
77         xbt_dict_remove(pals_int, peer->name);
78       }
79       char *peer_name;
80       void *data;
81       xbt_dict_foreach(pals_int, dict_cursor, peer_name, data) {
82         XBT_INFO("Peer %s didn't showed up", peer_name);
83       }
84       RETHROW;
85     }
86   }
87   XBT_INFO("Got my %ld peers", xbt_dynar_length(peers));
88   xbt_dict_free(&pals_int);
89
90   /* Check who came */
91   xbt_dict_t pals = xbt_dict_new();
92   gras_socket_t pal;
93   xbt_dynar_foreach(peers, cursor, peer) {
94     //XBT_INFO("%s is here",peer->name);
95     gras_socket_t sock = gras_socket_client(peer->name, peer->port);
96     xbt_dict_set(pals, peer->name, sock, NULL);
97   }
98   /* check that we have a dude for every element of the trace */
99   xbt_dynar_foreach(cmds, cursor, cmd) {
100     pal = xbt_dict_get_or_null(pals, cmd->who);
101     if (!pal) {
102       XBT_CRITICAL("Process %s didn't came! Abording!", cmd->who);
103       amok_pm_group_shutdown("replay");
104       xbt_dynar_free(&cmds);
105       gras_exit();
106       abort();
107     }
108   }
109   /* Send the commands to every pal */
110   char *pal_name;
111   xbt_dict_foreach(pals, dict_cursor, pal_name, pal) {
112     gras_msg_send(pal, "commands", &cmds);
113   }
114   XBT_INFO("Sent commands to every processes. Let them start now");
115   xbt_dict_cursor_t dict_it;
116   xbt_dict_foreach(pals, dict_it, pal_name, pal) {
117     gras_msg_send(pal, "go", NULL);
118   }
119   XBT_INFO("They should be started by now. Wait for their completion.");
120
121   for (cursor = 0; cursor < xbt_dynar_length(peers); cursor++) {
122     gras_msg_wait(-1, "go", NULL, NULL);
123   }
124
125   /* Done, exiting */
126   //amok_pm_group_shutdown("replay");
127   xbt_dynar_free(&cmds);
128   gras_exit();
129   return 0;
130 }
131
132 typedef struct {
133   xbt_dynar_t commands;
134   xbt_dict_t peers;
135   gras_socket_t mysock;
136 } s_worker_data_t, *worker_data_t;
137
138
139 static gras_socket_t get_peer_sock(char *peer)
140 {
141   worker_data_t g = gras_userdata_get();
142   gras_socket_t peer_sock = xbt_dict_get_or_null(g->peers, peer);
143   if (!peer_sock) {
144     XBT_INFO("Create a socket to %s", peer);
145     peer_sock = gras_socket_client(peer, 4000);
146     xbt_dict_set(g->peers, peer, peer_sock, NULL);      //gras_socket_close_voidp);
147   }
148   return peer_sock;
149 }
150
151 static int worker_commands_cb(gras_msg_cb_ctx_t ctx, void *payload)
152 {
153   worker_data_t g = gras_userdata_get();
154   g->commands = *(xbt_dynar_t *) payload;
155   return 0;
156 }
157
158 static void do_command(int rank, void *c)
159 {
160   xbt_workload_elm_t cmd = *(xbt_workload_elm_t *) c;
161   xbt_workload_data_chunk_t chunk;
162
163   if (cmd->action == XBT_WORKLOAD_SEND) {
164     gras_socket_t sock = get_peer_sock(cmd->str_arg);
165     chunk = xbt_workload_data_chunk_new((int) (cmd->d_arg));
166     XBT_INFO("Send %.f bytes to %s %s:%d", cmd->d_arg, cmd->str_arg,
167           gras_socket_peer_name(sock), gras_socket_peer_port(sock));
168     gras_msg_send_(sock, gras_msgtype_by_name("chunk"), &chunk);
169     XBT_INFO("Done sending %.f bytes to %s", cmd->d_arg, cmd->str_arg);
170
171   } else if (cmd->action == XBT_WORKLOAD_RECV) {
172     XBT_INFO("Recv %.f bytes from %s", cmd->d_arg, cmd->str_arg);
173     TRY {
174       gras_msg_wait(1000000, "chunk", NULL, &chunk);
175     }
176     CATCH_ANONYMOUS {
177       SIMIX_display_process_status();
178       RETHROWF("Exception while waiting for %f bytes from %s: %s",
179                cmd->d_arg, cmd->str_arg);
180     }
181     xbt_workload_data_chunk_free(chunk);
182     XBT_INFO("Done receiving %.f bytes from %s", cmd->d_arg, cmd->str_arg);
183
184   } else {
185     xbt_die("unknown command: %s", xbt_workload_elm_to_string(cmd));
186   }
187 }
188
189 int worker(int argc, char *argv[])
190 {
191   xbt_ex_t e;
192   worker_data_t globals;
193   gras_init(&argc, argv);
194   declare_msg();
195   globals = gras_userdata_new(s_worker_data_t);
196   /* Create the connexions */
197   globals->mysock = gras_socket_server(4000);   /* FIXME: shouldn't be hardcoded */
198   gras_socket_t master = NULL;
199   int connected = 0;
200
201   gras_cb_register("commands", worker_commands_cb);
202   globals->peers = xbt_dict_new();
203
204   if (gras_if_RL())
205     XBT_INFO("Sensor %s starting. Connecting to master on %s",
206           gras_os_myname(), argv[1]);
207   while (!connected) {
208     xbt_ex_t e;
209     TRY {
210       master = gras_socket_client_from_string(argv[1]);
211       connected = 1;
212     }
213     CATCH(e) {
214       if (e.category != system_error)
215         RETHROW;
216       xbt_ex_free(e);
217       XBT_INFO("Failed to connect. Retry in 0.5 second");
218       gras_os_sleep(0.5);
219     }
220   }
221   /* Join and run the group */
222   amok_pm_group_join(master, "replay", -1);
223   gras_msg_handle(60);          // command message
224   gras_msg_wait(60, "go", NULL, NULL);
225   {
226     worker_data_t g = gras_userdata_get();
227     unsigned int cursor;
228     xbt_workload_elm_t cmd;
229     const char *myname = gras_os_myname();
230     xbt_dynar_t cmd_to_go =
231         xbt_dynar_new(sizeof(xbt_workload_elm_t), NULL);
232
233     xbt_dynar_foreach(g->commands, cursor, cmd) {
234       if (!strcmp(cmd->who, myname)) {
235         char *c = xbt_workload_elm_to_string(cmd);
236         //      XBT_INFO("TODO: %s",c);
237         free(c);
238
239         switch (cmd->action) {
240         case XBT_WORKLOAD_COMPUTE:
241           /* If any communication were queued, do them in parallel */
242           if (!xbt_dynar_is_empty(cmd_to_go)) {
243             TRY {
244               xbt_dynar_dopar(cmd_to_go, do_command);
245               xbt_dynar_reset(cmd_to_go);
246             }
247             CATCH(e) {
248               SIMIX_display_process_status();
249               xbt_ex_free(e);
250             }
251             XBT_INFO("Communications all done");
252             xbt_dynar_reset(cmd_to_go);
253           }
254           XBT_INFO("Compute %.f flops", cmd->d_arg);
255           gras_cpu_burn(cmd->d_arg);
256           XBT_INFO("Done computing %.f flops", cmd->d_arg);
257           break;
258         case XBT_WORKLOAD_SEND:
259           /* Create the socket from main thread since it seems to fails when done from dopar thread */
260           get_peer_sock(cmd->str_arg);
261         case XBT_WORKLOAD_RECV:
262           /* queue communications for later realization in parallel */
263           xbt_dynar_push(cmd_to_go, &cmd);
264           break;
265         }
266       }
267     }
268     /* do in parallel any communication still queued */
269     XBT_INFO("Do %ld pending communications after end of TODO list",
270           xbt_dynar_length(cmd_to_go));
271     if (!xbt_dynar_is_empty(cmd_to_go)) {
272       xbt_dynar_dopar(cmd_to_go, do_command);
273       xbt_dynar_reset(cmd_to_go);
274     }
275   }
276
277   gras_msg_send(master, "go", NULL);
278 //  amok_pm_group_leave(master, "replay");
279
280   gras_socket_close(globals->mysock);
281   xbt_dynar_free(&(globals->commands));
282   xbt_dict_free(&(globals->peers));
283   free(globals);
284
285   gras_exit();
286   return 0;
287 }