Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use snprintf instead of sprintf
[simgrid.git] / examples / msg / actions-comm / actions-comm.c
index 0d2d646..126936b 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (c) 2009-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -8,25 +7,6 @@
 #include "simgrid/simix.h"      /* semaphores for the barrier */
 #include <xbt/replay.h>
 
-/** @addtogroup MSG_examples
- *
- *  @section MSG_ex_actions Trace driven simulations
- * 
- *  The <b>actions/actions.c</b> example demonstrates how to run trace-driven simulations. It is very handy when you
- *  want to test an algorithm or protocol that does nothing unless it receives some events from outside. For example,
- *  a P2P protocol reacts to requests from the user, but does nothing if there is no such event.
- * 
- *  In such situations, SimGrid allows to write your protocol in your C file, and the events to react to in a separate
- *  text file. Declare a function handling each of the events that you want to accept in your trace files, register
- *  them using \ref xbt_replay_action_register in your main, and then use \ref MSG_action_trace_run to launch the
- *  simulation. You can either have one trace file containing all your events, or a file per simulated process. Check
- *  the tesh files in the example directory for details on how to do it.
- *
- *  This example uses this approach to replay MPI-like traces. It comes with a set of event handlers reproducing MPI
- *  events. This is somehow similar to SMPI, yet differently implemented. This code should probably be changed to use
- *  SMPI internals instead, but wasn't, so far.
- */
-
 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
 int communicator_size = 0;
 
@@ -35,8 +15,6 @@ static void action_Isend(const char *const *action);
 typedef struct {
   int last_Irecv_sender_id;
   int bcast_counter;
-  int reduce_counter;
-  int allReduce_counter;
   xbt_dynar_t isends;           /* of msg_comm_t */
   /* Used to implement irecv+wait */
   xbt_dynar_t irecvs;           /* of msg_comm_t */
@@ -92,7 +70,7 @@ static void action_send(const char *const *action)
   double size = parse_double(size_str);
   double clock = MSG_get_clock();
 
-  sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
+  snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
 
   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
   if (size < 65536) {
@@ -112,7 +90,7 @@ static void action_Isend(const char *const *action)
   double clock = MSG_get_clock();
   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
 
-  sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
+  snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
   msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
   xbt_dynar_push(globals->isends, &comm);
 
@@ -127,7 +105,7 @@ static void action_recv(const char *const *action)
   msg_task_t task = NULL;
   double clock = MSG_get_clock();
 
-  sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
+  snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
 
   ACT_DEBUG("Receiving: %s", NAME);
   msg_error_t res = MSG_task_receive(&task, mailbox_name);
@@ -147,7 +125,7 @@ static void action_Irecv(const char *const *action)
 
   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
 
-  sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
+  snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
   msg_task_t t = NULL;
   xbt_dynar_push(globals->tasks, &t);
   msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
@@ -203,7 +181,7 @@ static void action_barrier(const char *const *action)
   ACT_DEBUG("Exiting barrier: %s", NAME);
 
   processes_arrived_sofar--;
-  if (!processes_arrived_sofar) {
+  if (processes_arrived_sofar<=0) {
     SIMIX_cond_destroy(cond);
     SIMIX_mutex_destroy(mutex);
     mutex = NULL;
@@ -212,39 +190,36 @@ static void action_barrier(const char *const *action)
 
 static void action_bcast(const char *const *action)
 {
-  int i;
-  char *bcast_identifier;
   char mailbox[80];
   double comm_size = parse_double(action[2]);
   msg_task_t task = NULL;
-  const char *process_name;
   double clock = MSG_get_clock();
 
   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
 
   xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
 
-  process_name = MSG_process_get_name(MSG_process_self());
+  const char * process_name = MSG_process_get_name(MSG_process_self());
 
-  bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
+  char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
 
   if (!strcmp(process_name, "p0")) {
     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
 
     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
 
-    for (i = 1; i < communicator_size; i++) {
-      sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
+    for (int i = 1; i < communicator_size; i++) {
+      snprintf(mailbox,79, "%s_p0_p%d", bcast_identifier, i);
       comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
     }
     MSG_comm_waitall(comms, communicator_size - 1, -1);
-    for (i = 1; i < communicator_size; i++)
+    for (int i = 1; i < communicator_size; i++)
       MSG_comm_destroy(comms[i - 1]);
     xbt_free(comms);
 
     XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
   } else {
-    sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
+    snprintf(mailbox,79, "%s_p0_%s", bcast_identifier, process_name);
     MSG_task_receive(&task, mailbox);
     MSG_task_destroy(task);
     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
@@ -297,7 +272,6 @@ static void action_finalize(const char *const *action)
   }
 }
 
-/** Main function */
 int main(int argc, char *argv[])
 {
   msg_error_t res = MSG_OK;
@@ -336,8 +310,7 @@ int main(int argc, char *argv[])
 
   XBT_INFO("Simulation time %g", MSG_get_clock());
 
-  /* Explicit finalization of the action module is required now*/
-  MSG_action_exit();
+  MSG_action_exit(); /* Explicit finalization of the action module */
 
   return res != MSG_OK;
 }