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 * This section details how to run trace-driven simulations. It is very handy when you want to test an algorithm or
16 * protocol that does nothing unless it receives some events from outside. For example, a P2P protocol reacts to
17 * requests from the user, but does nothing if there is no such event.
19 * In such situations, SimGrid allows you to write your protocol in a 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 directories for details on how to do it.
25 * - <b>Communication: actions-comm/actions-comm.c</b>. This example comes with a set of event handlers reproducing
26 * some classical communication primitives (synchronous and asynchronous send/receive, broadcast, barrier, ...).
29 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
30 int communicator_size = 0;
32 static void action_Isend(const char *const *action);
35 int last_Irecv_sender_id;
37 xbt_dynar_t isends; /* of msg_comm_t */
38 /* Used to implement irecv+wait */
39 xbt_dynar_t irecvs; /* of msg_comm_t */
40 xbt_dynar_t tasks; /* of msg_task_t */
41 } s_process_globals_t, *process_globals_t;
44 static double parse_double(const char *string)
49 value = strtod(string, &endptr);
51 THROWF(unknown_error, 0, "%s is not a double", string);
55 #define ACT_DEBUG(...) \
56 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
57 char *NAME = xbt_str_join_array(action, " "); \
58 XBT_DEBUG(__VA_ARGS__); \
62 static void log_action(const char *const *action, double date)
64 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
65 char *name = xbt_str_join_array(action, " ");
66 XBT_VERB("%s %f", name, date);
71 static void asynchronous_cleanup(void)
73 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
75 /* Destroy any isend which correspond to completed communications */
78 while ((found = MSG_comm_testany(globals->isends)) != -1) {
79 xbt_dynar_remove_at(globals->isends, found, &comm);
80 MSG_comm_destroy(comm);
85 static void action_send(const char *const *action)
88 const char *size_str = action[3];
89 double size = parse_double(size_str);
90 double clock = MSG_get_clock();
92 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
94 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
98 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
101 log_action(action, MSG_get_clock() - clock);
102 asynchronous_cleanup();
105 static void action_Isend(const char *const *action)
108 const char *size = action[3];
109 double clock = MSG_get_clock();
110 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
112 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
113 msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
114 xbt_dynar_push(globals->isends, &comm);
116 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
117 log_action(action, MSG_get_clock() - clock);
118 asynchronous_cleanup();
121 static void action_recv(const char *const *action)
123 char mailbox_name[250];
124 msg_task_t task = NULL;
125 double clock = MSG_get_clock();
127 sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
129 ACT_DEBUG("Receiving: %s", NAME);
130 msg_error_t res = MSG_task_receive(&task, mailbox_name);
131 log_action(action, MSG_get_clock() - clock);
134 MSG_task_destroy(task);
136 asynchronous_cleanup();
139 static void action_Irecv(const char *const *action)
142 double clock = MSG_get_clock();
143 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
145 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
147 sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
149 xbt_dynar_push(globals->tasks, &t);
150 msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
151 xbt_dynar_push(globals->irecvs, &c);
153 log_action(action, MSG_get_clock() - clock);
154 asynchronous_cleanup();
157 static void action_wait(const char *const *action)
159 msg_task_t task = NULL;
161 double clock = MSG_get_clock();
162 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
164 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
165 xbt_str_join_array(action, " "));
167 ACT_DEBUG("Entering %s", NAME);
168 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
169 MSG_comm_wait(comm, -1);
170 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
171 MSG_comm_destroy(comm);
172 MSG_task_destroy(task);
174 log_action(action, MSG_get_clock() - clock);
177 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
178 static void action_barrier(const char *const *action)
180 static smx_mutex_t mutex = NULL;
181 static smx_cond_t cond = NULL;
182 static int processes_arrived_sofar = 0;
184 if (mutex == NULL) { // first arriving on the barrier
185 mutex = simcall_mutex_init();
186 cond = simcall_cond_init();
187 processes_arrived_sofar = 0;
189 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
191 simcall_mutex_lock(mutex);
192 if (++processes_arrived_sofar == communicator_size) {
193 simcall_cond_broadcast(cond);
194 simcall_mutex_unlock(mutex);
196 simcall_cond_wait(cond, mutex);
197 simcall_mutex_unlock(mutex);
200 ACT_DEBUG("Exiting barrier: %s", NAME);
202 processes_arrived_sofar--;
203 if (!processes_arrived_sofar) {
204 SIMIX_cond_destroy(cond);
205 SIMIX_mutex_destroy(mutex);
210 static void action_bcast(const char *const *action)
213 char *bcast_identifier;
215 double comm_size = parse_double(action[2]);
216 msg_task_t task = NULL;
217 const char *process_name;
218 double clock = MSG_get_clock();
220 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
222 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
224 process_name = MSG_process_get_name(MSG_process_self());
226 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
228 if (!strcmp(process_name, "p0")) {
229 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
231 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
233 for (i = 1; i < communicator_size; i++) {
234 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
235 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
237 MSG_comm_waitall(comms, communicator_size - 1, -1);
238 for (i = 1; i < communicator_size; i++)
239 MSG_comm_destroy(comms[i - 1]);
242 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
244 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
245 MSG_task_receive(&task, mailbox);
246 MSG_task_destroy(task);
247 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
250 log_action(action, MSG_get_clock() - clock);
251 xbt_free(bcast_identifier);
254 static void action_comm_size(const char *const *action)
256 const char *size = action[2];
257 double clock = MSG_get_clock();
259 communicator_size = parse_double(size);
260 log_action(action, MSG_get_clock() - clock);
263 static void action_compute(const char *const *action)
265 const char *amount = action[2];
266 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
267 double clock = MSG_get_clock();
269 ACT_DEBUG("Entering %s", NAME);
270 MSG_task_execute(task);
271 MSG_task_destroy(task);
272 log_action(action, MSG_get_clock() - clock);
275 static void action_init(const char *const *action)
277 XBT_DEBUG("Initialize the counters");
278 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
279 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
280 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
281 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
282 MSG_process_set_data(MSG_process_self(), globals);
285 static void action_finalize(const char *const *action)
287 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
289 asynchronous_cleanup();
290 xbt_dynar_free_container(&(globals->isends));
291 xbt_dynar_free_container(&(globals->irecvs));
292 xbt_dynar_free_container(&(globals->tasks));
298 int main(int argc, char *argv[])
300 msg_error_t res = MSG_OK;
302 /* Check the given arguments */
303 MSG_init(&argc, argv);
304 /* Explicit initialization of the action module is required now*/
308 "Usage: %s platform_file deployment_file [action_files]\n"
309 "\t# if all actions are in the same file\n"
310 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
311 "\t# if actions are in separate files, specified in deployment\n"
312 "\tExample: %s msg_platform.xml msg_deployment.xml ",
313 argv[0],argv[0],argv[0]);
315 MSG_create_environment(argv[1]);
316 MSG_launch_application(argv[2]);
318 /* Action registration */
319 xbt_replay_action_register("init", action_init);
320 xbt_replay_action_register("finalize", action_finalize);
321 xbt_replay_action_register("comm_size", action_comm_size);
322 xbt_replay_action_register("send", action_send);
323 xbt_replay_action_register("Isend", action_Isend);
324 xbt_replay_action_register("recv", action_recv);
325 xbt_replay_action_register("Irecv", action_Irecv);
326 xbt_replay_action_register("wait", action_wait);
327 xbt_replay_action_register("barrier", action_barrier);
328 xbt_replay_action_register("bcast", action_bcast);
329 xbt_replay_action_register("compute", action_compute);
331 /* Actually do the simulation using MSG_action_trace_run */
332 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
334 XBT_INFO("Simulation time %g", MSG_get_clock());
336 /* Explicit finalization of the action module is required now*/
339 return res != MSG_OK;