1 /* Copyright (c) 2009-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/msg.h"
8 #include "simgrid/simix.h" /* semaphores for the barrier */
9 #include <xbt/replay.h>
11 /** @addtogroup MSG_examples
13 * @section MSG_ex_actions Trace driven simulations
15 * The <b>actions/actions.c</b> example demonstrates how to run trace-driven simulations. It is very handy when you
16 * want to test an algorithm or protocol that does nothing unless it receives some events from outside. For example,
17 * a P2P protocol reacts to requests from the user, but does nothing if there is no such event.
19 * In such situations, SimGrid allows to write your protocol in your C file, and the events to react to in a separate
20 * text file. Declare a function handling each of the events that you want to accept in your trace files, register
21 * them using \ref xbt_replay_action_register in your main, and then use \ref MSG_action_trace_run to launch the
22 * simulation. You can either have one trace file containing all your events, or a file per simulated process. Check
23 * the tesh files in the example directory for details on how to do it.
25 * This example uses this approach to replay MPI-like traces. It comes with a set of event handlers reproducing MPI
26 * events. This is somehow similar to SMPI, yet differently implemented. This code should probably be changed to use
27 * SMPI internals instead, but wasn't, so far.
30 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
31 int communicator_size = 0;
33 static void action_Isend(const char *const *action);
36 int last_Irecv_sender_id;
38 xbt_dynar_t isends; /* of msg_comm_t */
39 /* Used to implement irecv+wait */
40 xbt_dynar_t irecvs; /* of msg_comm_t */
41 xbt_dynar_t tasks; /* of msg_task_t */
42 } s_process_globals_t, *process_globals_t;
45 static double parse_double(const char *string)
50 value = strtod(string, &endptr);
52 THROWF(unknown_error, 0, "%s is not a double", string);
56 #define ACT_DEBUG(...) \
57 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
58 char *NAME = xbt_str_join_array(action, " "); \
59 XBT_DEBUG(__VA_ARGS__); \
63 static void log_action(const char *const *action, double date)
65 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
66 char *name = xbt_str_join_array(action, " ");
67 XBT_VERB("%s %f", name, date);
72 static void asynchronous_cleanup(void)
74 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
76 /* Destroy any isend which correspond to completed communications */
79 while ((found = MSG_comm_testany(globals->isends)) != -1) {
80 xbt_dynar_remove_at(globals->isends, found, &comm);
81 MSG_comm_destroy(comm);
86 static void action_send(const char *const *action)
89 const char *size_str = action[3];
90 double size = parse_double(size_str);
91 double clock = MSG_get_clock();
93 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
95 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
99 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
102 log_action(action, MSG_get_clock() - clock);
103 asynchronous_cleanup();
106 static void action_Isend(const char *const *action)
109 const char *size = action[3];
110 double clock = MSG_get_clock();
111 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
113 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
114 msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
115 xbt_dynar_push(globals->isends, &comm);
117 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
118 log_action(action, MSG_get_clock() - clock);
119 asynchronous_cleanup();
122 static void action_recv(const char *const *action)
124 char mailbox_name[250];
125 msg_task_t task = NULL;
126 double clock = MSG_get_clock();
128 sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
130 ACT_DEBUG("Receiving: %s", NAME);
131 msg_error_t res = MSG_task_receive(&task, mailbox_name);
132 log_action(action, MSG_get_clock() - clock);
135 MSG_task_destroy(task);
137 asynchronous_cleanup();
140 static void action_Irecv(const char *const *action)
143 double clock = MSG_get_clock();
144 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
146 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
148 sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
150 xbt_dynar_push(globals->tasks, &t);
151 msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
152 xbt_dynar_push(globals->irecvs, &c);
154 log_action(action, MSG_get_clock() - clock);
155 asynchronous_cleanup();
158 static void action_wait(const char *const *action)
160 msg_task_t task = NULL;
162 double clock = MSG_get_clock();
163 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
165 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
166 xbt_str_join_array(action, " "));
168 ACT_DEBUG("Entering %s", NAME);
169 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
170 MSG_comm_wait(comm, -1);
171 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
172 MSG_comm_destroy(comm);
173 MSG_task_destroy(task);
175 log_action(action, MSG_get_clock() - clock);
178 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
179 static void action_barrier(const char *const *action)
181 static smx_mutex_t mutex = NULL;
182 static smx_cond_t cond = NULL;
183 static int processes_arrived_sofar = 0;
185 if (mutex == NULL) { // first arriving on the barrier
186 mutex = simcall_mutex_init();
187 cond = simcall_cond_init();
188 processes_arrived_sofar = 0;
190 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
192 simcall_mutex_lock(mutex);
193 if (++processes_arrived_sofar == communicator_size) {
194 simcall_cond_broadcast(cond);
195 simcall_mutex_unlock(mutex);
197 simcall_cond_wait(cond, mutex);
198 simcall_mutex_unlock(mutex);
201 ACT_DEBUG("Exiting barrier: %s", NAME);
203 processes_arrived_sofar--;
204 if (!processes_arrived_sofar) {
205 SIMIX_cond_destroy(cond);
206 SIMIX_mutex_destroy(mutex);
211 static void action_bcast(const char *const *action)
214 char *bcast_identifier;
216 double comm_size = parse_double(action[2]);
217 msg_task_t task = NULL;
218 const char *process_name;
219 double clock = MSG_get_clock();
221 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
223 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
225 process_name = MSG_process_get_name(MSG_process_self());
227 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
229 if (!strcmp(process_name, "p0")) {
230 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
232 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
234 for (i = 1; i < communicator_size; i++) {
235 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
236 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
238 MSG_comm_waitall(comms, communicator_size - 1, -1);
239 for (i = 1; i < communicator_size; i++)
240 MSG_comm_destroy(comms[i - 1]);
243 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
245 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
246 MSG_task_receive(&task, mailbox);
247 MSG_task_destroy(task);
248 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
251 log_action(action, MSG_get_clock() - clock);
252 xbt_free(bcast_identifier);
255 static void action_comm_size(const char *const *action)
257 const char *size = action[2];
258 double clock = MSG_get_clock();
260 communicator_size = parse_double(size);
261 log_action(action, MSG_get_clock() - clock);
264 static void action_compute(const char *const *action)
266 const char *amount = action[2];
267 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
268 double clock = MSG_get_clock();
270 ACT_DEBUG("Entering %s", NAME);
271 MSG_task_execute(task);
272 MSG_task_destroy(task);
273 log_action(action, MSG_get_clock() - clock);
276 static void action_init(const char *const *action)
278 XBT_DEBUG("Initialize the counters");
279 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
280 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
281 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
282 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
283 MSG_process_set_data(MSG_process_self(), globals);
286 static void action_finalize(const char *const *action)
288 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
290 asynchronous_cleanup();
291 xbt_dynar_free_container(&(globals->isends));
292 xbt_dynar_free_container(&(globals->irecvs));
293 xbt_dynar_free_container(&(globals->tasks));
299 int main(int argc, char *argv[])
301 msg_error_t res = MSG_OK;
303 /* Check the given arguments */
304 MSG_init(&argc, argv);
305 /* Explicit initialization of the action module is required now*/
309 "Usage: %s platform_file deployment_file [action_files]\n"
310 "\t# if all actions are in the same file\n"
311 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
312 "\t# if actions are in separate files, specified in deployment\n"
313 "\tExample: %s msg_platform.xml msg_deployment.xml ",
314 argv[0],argv[0],argv[0]);
316 MSG_create_environment(argv[1]);
317 MSG_launch_application(argv[2]);
319 /* Action registration */
320 xbt_replay_action_register("init", action_init);
321 xbt_replay_action_register("finalize", action_finalize);
322 xbt_replay_action_register("comm_size", action_comm_size);
323 xbt_replay_action_register("send", action_send);
324 xbt_replay_action_register("Isend", action_Isend);
325 xbt_replay_action_register("recv", action_recv);
326 xbt_replay_action_register("Irecv", action_Irecv);
327 xbt_replay_action_register("wait", action_wait);
328 xbt_replay_action_register("barrier", action_barrier);
329 xbt_replay_action_register("bcast", action_bcast);
330 xbt_replay_action_register("compute", action_compute);
332 /* Actually do the simulation using MSG_action_trace_run */
333 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
335 XBT_INFO("Simulation time %g", MSG_get_clock());
337 /* Explicit finalization of the action module is required now*/
340 return res != MSG_OK;