SimGrid 3.6.2
Scalable simulation of distributed systems
Some timer games

This example fools around with the GRAS timers (Timers). It is mainly a regression test, since it uses almost all timer features.

The main program registers a repetititive task and a delayed one, and then loops until the still_to_do variables of its globals reach 0. The delayed task set it to 5, and the repetititive one decrease it each time. Here is an example of output:

Initialize GRAS
 Initialize XBT
 [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
 [1108335471] Programming the delayed_action for after 2.000000 sec
 [1108335471] Have a rest
 [1108335472] Canceling the delayed_action.
 [1108335472] Re-programming the delayed_action for after 2.000000 sec
 [1108335472] Repetitive_action has nothing to do yet
 [1108335473] Repetitive_action has nothing to do yet
 [1108335473] delayed_action setting globals->still_to_do to 5
 [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
 [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
 [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
 [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
 [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
 Exiting GRAS

Source code:

1. Declarations and headers

#include "gras.h"

XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test");

#define REPEAT_INTERVAL 1.0
#define DELAY_INTERVAL 2.0
#define LOOP_COUNT 5

typedef struct {
  int still_to_do;
} my_globals;

2. Source code of the delayed action

static void repetitive_action(void)
{
  my_globals *globals = (my_globals *) gras_userdata_get();

  /* Stop if nothing to do yet */
  if (globals->still_to_do <= 0) {
    XBT_INFO("Repetitive_action has nothing to do yet");
    return;
  }

  if (globals->still_to_do == 1) {
    /* Unregister myself if I'm done */
    gras_timer_cancel_repeat(REPEAT_INTERVAL, repetitive_action);
  }

  XBT_INFO
      ("repetitive_action decrementing globals->still_to_do. New value: %d",
       globals->still_to_do - 1);

  globals->still_to_do--;       /* should be the last line of the action since value=0 stops the program */
}                               /* end_of_repetitive_action */

3. Source code of the repetitive action

static void delayed_action(void)
{
  my_globals *globals = (my_globals *) gras_userdata_get();

  XBT_INFO("delayed_action setting globals->still_to_do to %d", LOOP_COUNT);

  globals->still_to_do = LOOP_COUNT;
}                               /* end_of_delayed_action */

4. Source code of main function

int client(int argc, char *argv[])
{
  my_globals *globals;

  gras_init(&argc, argv);
  globals = gras_userdata_new(my_globals);
  globals->still_to_do = -1;

  XBT_INFO("Programming the repetitive_action with a frequency of %f sec",
        REPEAT_INTERVAL);
  gras_timer_repeat(REPEAT_INTERVAL, repetitive_action);

  XBT_INFO("Programming the delayed_action for after %f sec", DELAY_INTERVAL);
  gras_timer_delay(REPEAT_INTERVAL, delayed_action);

  XBT_INFO("Have a rest");
  gras_os_sleep(DELAY_INTERVAL / 2.0);

  XBT_INFO("Canceling the delayed_action.");
  gras_timer_cancel_delay(REPEAT_INTERVAL, delayed_action);

  XBT_INFO("Re-programming the delayed_action for after %f sec",
        DELAY_INTERVAL);
  gras_timer_delay(REPEAT_INTERVAL, delayed_action);

  while (globals->still_to_do == -1 ||  /* Before delayed action runs */
         globals->still_to_do >
         0 /* after delayed_action, and not enough repetitive_action */ ) {

    XBT_DEBUG("Prepare to handle messages for 5 sec (still_to_do=%d)",
           globals->still_to_do);
    gras_msg_handle(5.0);
  }
  gras_exit();
  xbt_free(globals);
  return 0;
}                               /* end_of_client */


Back to the main Simgrid Documentation page The version of Simgrid documented here is v3.6.2.
Documentation of other versions can be found in their respective archive files (directory doc/html).
Generated for SimGridAPI by doxygen