Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Re-import the simple-token example (was named tokenS before)
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Wed, 12 Jul 2006 12:40:02 +0000 (12:40 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Wed, 12 Jul 2006 12:40:02 +0000 (12:40 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2544 48e7efb5-ca39-0410-a469-dd3cf9ba447f

examples/gras/mutual-exclusion/simple-token/Makefile.am [new file with mode: 0644]
examples/gras/mutual-exclusion/simple-token/make_deployment.pl [new file with mode: 0755]
examples/gras/mutual-exclusion/simple-token/run.sh [new file with mode: 0755]
examples/gras/mutual-exclusion/simple-token/simple-token.c [new file with mode: 0644]
examples/gras/mutual-exclusion/simple-token/simple-token.xml [new file with mode: 0644]
examples/gras/mutual-exclusion/simple-token/test_rl.in [new file with mode: 0755]
examples/gras/mutual-exclusion/simple-token/test_sg.in [new file with mode: 0755]

diff --git a/examples/gras/mutual-exclusion/simple-token/Makefile.am b/examples/gras/mutual-exclusion/simple-token/Makefile.am
new file mode 100644 (file)
index 0000000..0a1a5b7
--- /dev/null
@@ -0,0 +1,30 @@
+INCLUDES= -I$(top_srcdir)/include
+TESTS= test_rl test_sg
+EXTRA_DIST=tokenS_deployment.xml $(TESTS)
+
+# AUTOMAKE variable definition
+noinst_PROGRAMS=tokenS_node tokenS_simulator
+
+tokenS_simulator_SOURCES=  _tokenS_simulator.c tokenS.c
+tokenS_simulator_LDADD=    $(top_builddir)/src/libsimgrid.la
+
+tokenS_node_SOURCES=       _tokenS_node.c tokenS.c
+tokenS_node_LDADD=         $(top_builddir)/src/libgras.la
+
+# cleanup temps
+CLEANFILES = _tokenS_simulator.c _tokenS_node.c _tokenS_node.c \
+             tokenS.Makefile.local tokenS.Makefile.remote tokenS.deploy.sh 
+
+MOSTLYCLEANFILES = tokenS.trace
+
+# generate temps
+NAME=tokenS
+PROCESSES=node
+
+$(foreach proc, $(PROCESSES), _$(NAME)_$(proc).c) _$(NAME)_simulator.c: $(srcdir)/$(NAME)_deployment.xml $(top_srcdir)/tools/gras/gras_stub_generator@EXEEXT@
+       [ x@EXEEXT@ = x ] || exenv=wine; $$exenv $(top_srcdir)/tools/gras/gras_stub_generator@EXEEXT@ $(NAME) $(srcdir)/$(NAME)_deployment.xml >/dev/null
+
+$(top_srcdir)/tools/gras/gras_stub_generator@EXEEXT@:
+       make -C   $(top_srcdir)/tools/gras/ gras_stub_generator@EXEEXT@
+
+include $(top_srcdir)/acmacro/dist-files.mk
diff --git a/examples/gras/mutual-exclusion/simple-token/make_deployment.pl b/examples/gras/mutual-exclusion/simple-token/make_deployment.pl
new file mode 100755 (executable)
index 0000000..5e69deb
--- /dev/null
@@ -0,0 +1,50 @@
+#! /usr/bin/perl
+
+use strict;
+
+my $input    = shift @ARGV || die "Usage: $ARGV[0] platform_file.xml nb_host\n";
+my $nb_hosts = shift @ARGV || die "Usage: $ARGV[0] platform_file.xml nb_host\n";
+
+my @host;
+
+open IN,$input || die "Cannot open $input: $!\n";
+
+while (<IN>) {
+  next unless /<cpu name="([^"]*)"/; # "
+  
+  push @host, $1;
+}
+
+# map { print "$_\n" } @host;
+
+print "<?xml version='1.0'?>\n";
+print "<!DOCTYPE platform_description SYSTEM \"surfxml.dtd\">\n";
+print "<platform_description version=\"1\">\n\n";
+
+
+my $it_port=4000;
+my $it_host=0;
+
+for (my $i=0; $i<$nb_hosts -1; $i++) {
+
+  print "  <process host=\"".$host[$it_host]."\" function=\"node\">\n";
+  print "    <argument value=\"". $it_port ."\"/>     <!-- port on which I am listening -->\n";
+  $it_host ++;
+  if ($it_host == scalar @host) {
+    $it_host=0;
+    $it_port++;
+  }
+  print "    <argument value=\"". $host[ $it_host ]."\"/>   <!-- peer (successor) host -->\n";
+  print "    <argument value=\"".($it_port)."\"/>     <!-- port on which peer is listening -->\n";
+  print "  </process>\n\n";
+                
+}
+
+print "  <process host=\"".$host[$it_host]."\" function=\"node\">\n";
+print "    <argument value=\"". $it_port ."\"/>     <!-- port on which I am listening -->\n";
+print "    <argument value=\"". $host[ 0 ]."\"/>   <!-- peer (successor) host -->\n";
+print "    <argument value=\"4000\"/>     <!-- port on which peer is listening -->\n";
+print "    <argument value=\"--create-token\"/>   <!-- I'm first client, ie I have to create the token -->\n";
+print "  </process>\n\n";
+
+print "</platform_description>\n";
diff --git a/examples/gras/mutual-exclusion/simple-token/run.sh b/examples/gras/mutual-exclusion/simple-token/run.sh
new file mode 100755 (executable)
index 0000000..f8aa0bd
--- /dev/null
@@ -0,0 +1,30 @@
+#! /bin/bash
+
+#
+# USAGE: run.sh  plaform  nb_host 
+#
+# This script takes a platform file and a number of hosts as argument.
+#
+# It generates the right deployment platform and run the experiment, 
+#  only showing the last line of the run, showing the resulting time.
+
+plat=$1
+nb_host=$2
+
+set -e
+
+if [ -z $plat -o -z $nb_host ] ; then
+  # invalid argument. Display the comment at the script begining & exit
+  grep '^#\(\([^!]\)\|$\)' $0 | sed 's/# *//' >&2
+  exit 1
+fi
+if ! [ -e $plat ] ; then
+  echo "Platform file not found" >&2
+  exit 1
+fi
+
+echo "Generating the deployment"
+./make_deployment.pl $plat $nb_host > tmp_deployment_$nb_host
+echo "Running the experiment"
+./tokenS_simulator $plat tmp_deployment_$nb_host 2>&1 |tee run.log|grep "Congrat"
+rm tmp_deployment_$nb_host
diff --git a/examples/gras/mutual-exclusion/simple-token/simple-token.c b/examples/gras/mutual-exclusion/simple-token/simple-token.c
new file mode 100644 (file)
index 0000000..06b0762
--- /dev/null
@@ -0,0 +1,188 @@
+/* $Id$ */
+
+/* stoken - simple/static token ring                                        */
+
+/* Copyright (c) 2005 Alexandre Colucci.                                    */
+/* Copyright (c) 2005 Martin Quinson.                                       */
+/* 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. */
+#include "gras.h"
+#define NBLOOPS 3
+/*#define NBLOOPS 30000*/
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(SimpleToken,"Messages specific to this example");
+
+/* Function prototypes */
+int node (int argc,char *argv[]);
+
+
+/* **********************************************************************
+ * Node code
+ * **********************************************************************/
+
+/* Global private data */
+typedef struct {
+  gras_socket_t sock; /* server socket on which I hear */
+  int remaining_loop; /* number of loops to do until done */
+  int create;        /* whether I have to create the token */
+  gras_socket_t tosuccessor; /* how to connect to the successor on ring */
+} node_data_t;
+
+
+/* Callback function */
+static int node_cb_stoken_handler(gras_msg_cb_ctx_t ctx, void *payload) {
+  
+  xbt_ex_t e;
+  
+  /* 1. Get the payload into the msg variable, and retrieve my caller */
+  int msg=*(int*)payload;
+  gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
+
+
+  /* 2. Retrieve the node's state (globals) */
+  node_data_t *globals=(node_data_t*)gras_userdata_get();
+  /* 3. Log which predecessor connected */
+  int supersteps = 1; /* only log every superstep loop */
+  if (NBLOOPS >= 1000) {
+    supersteps = 100;
+  } else if (NBLOOPS >= 100) {
+    supersteps = 10;
+  } else if (NBLOOPS <=10) {
+    supersteps = 1;
+  }
+  if (globals->create && (! (globals->remaining_loop % supersteps))) {
+    INFO1("Begin a new loop. Still to do: %d", globals->remaining_loop);
+  } else if (! (globals->remaining_loop % supersteps)) {
+    VERB3("Got token(%d) from %s remaining_loop=%d", 
+         msg, gras_socket_peer_name(expeditor),globals->remaining_loop);
+  }
+  
+  /* 4. If the right shouldn't be stopped yet */
+  if (globals->remaining_loop > 0) {
+    msg += 1;
+     
+    DEBUG3("Send token(%d) to %s:%d", msg, 
+          gras_socket_peer_name(globals->tosuccessor),
+          gras_socket_peer_port(globals->tosuccessor));
+     
+    /* 5. Send the token as payload of a stoken message to the successor */
+    TRY {
+      gras_msg_send(globals->tosuccessor, 
+                   gras_msgtype_by_name("stoken"), &msg);
+     
+    /* 6. Deal with errors */
+    } CATCH(e) {
+      gras_socket_close(globals->sock);
+      RETHROW0("Unable to forward token: %s");
+    }
+  
+  }
+             
+  /* DO NOT CLOSE THE expeditor SOCKET since the client socket is
+     reused by our predecessor.
+     Closing this side would thus create troubles */
+  
+  /* 7. Decrease the remaining_loop integer. */
+  globals->remaining_loop -= 1;
+   
+  /* 8. Repport the hop number to the user at the end */
+  if (globals->remaining_loop == -1 && globals->create) {
+    INFO1("Shut down the token-ring. There was %d hops.",msg);
+  }
+
+  /* 9. Tell GRAS that we consummed this message */
+  return 1;
+} /* end_of_node_cb_stoken_handler */
+
+int node (int argc,char *argv[]) {
+  node_data_t *globals;
+  
+  const char *host;
+  int   myport;
+  int   peerport;
+    
+  xbt_ex_t e;
+  
+  /* 1. Init the GRAS infrastructure and declare my globals */
+  gras_init(&argc,argv);
+  globals=gras_userdata_new(node_data_t);
+  
+  
+  /* 2. Get the successor's address. The command line overrides
+        defaults when specified */
+  host = "127.0.0.1";
+  myport = 4000;
+  peerport = 4000;
+  if (argc >= 4) {
+    myport=atoi(argv[1]);
+    host=argv[2];
+    peerport=atoi(argv[3]);
+  }
+
+  /* 3. Save successor's address in global var */
+  globals->remaining_loop=NBLOOPS;
+  globals->create = 0;
+  globals->tosuccessor = NULL;
+
+  if (!gras_os_getpid() % 100)
+  INFO4("Launch node %ld (successor on %s:%d; listening on %d)",
+       gras_os_getpid(), host,peerport, myport);
+
+  /* 4. Create my master socket for listening */
+  globals->sock = gras_socket_server(myport);
+  gras_os_sleep(1.0); /* Make sure all server sockets are created */
+
+  /* 5. Create socket to the successor on the ring */
+  DEBUG2("Connect to my successor on %s:%d",host,peerport);
+
+  globals->tosuccessor = gras_socket_client(host,peerport);
+  
+  /* 6. Register the known messages.  */
+  gras_msgtype_declare("stoken", gras_datadesc_by_name("int"));
+   
+  /* 7. Register my callback */
+  gras_cb_register(gras_msgtype_by_name("stoken"),&node_cb_stoken_handler);  
+
+  /* 8. One node has to create the token at startup. 
+        It's specified by a command line argument */
+  if (argc >= 5 && !strncmp("--create-token", argv[4],strlen(argv[4])))
+    globals->create=1;
+
+  if (globals->create) {
+    int token = 0;
+            
+    globals->remaining_loop = NBLOOPS - 1;
+      
+    INFO3("Create the token (with value %d) and send it to %s:%d",
+         token, host, peerport);
+
+    TRY {        
+      gras_msg_send(globals->tosuccessor,
+                 gras_msgtype_by_name("stoken"), &token);
+    } CATCH(e) {
+      RETHROW0("Unable to send the freshly created token: %s");
+    }
+  } 
+  
+  /* 8. Wait up to 10 seconds for an incomming message to handle */
+  while (globals->remaining_loop > (globals->create ? -1 : 0)) {
+    gras_msg_handle(10.0);
+  
+    DEBUG1("looping (remaining_loop=%d)", globals->remaining_loop);
+  }
+
+  gras_os_sleep(1.0); /* FIXME: if the sender quited, receive fail */
+
+  /* 9. Free the allocated resources, and shut GRAS down */ 
+  gras_socket_close(globals->sock);
+  gras_socket_close(globals->tosuccessor);
+  free(globals);
+  gras_exit();
+  
+  return 0;
+} /* end_of_node */
diff --git a/examples/gras/mutual-exclusion/simple-token/simple-token.xml b/examples/gras/mutual-exclusion/simple-token/simple-token.xml
new file mode 100644 (file)
index 0000000..78d0418
--- /dev/null
@@ -0,0 +1,31 @@
+<?xml version='1.0'?>
+<!DOCTYPE platform_description SYSTEM "surfxml.dtd">
+<platform_description version="1">
+
+ <process host="Tremblay" function="node">
+  <argument value="4000"/>                           <!-- port on which I am listening -->
+  <argument value="Fafard"/><argument value="4000"/> <!-- peer (successor) host name and port-->
+  <argument value="--create-token"/>                 <!-- I'm first client, ie I have to create the token -->
+ </process>
+ <process host="Fafard" function="node">
+  <argument value="4000"/>                           <!-- port on which I am listening -->
+  <argument value="Jupiter"/><argument value="4000"/><!-- peer (successor) host name and port-->
+ </process>
+ <process host="Jupiter" function="node">
+  <argument value="4000"/>                            <!-- port on which I am listening -->
+  <argument value="Ginette"/><argument value="4000"/> <!-- peer (successor) host name and port-->
+ </process>
+ <process host="Ginette" function="node">
+  <argument value="4000"/>                            <!-- port on which I am listening -->
+  <argument value="Bourassa"/><argument value="4000"/><!-- peer (successor) host name and port-->
+ </process>
+ <process host="Bourassa" function="node">
+  <argument value="4000"/>                            <!-- port on which I am listening -->
+  <argument value="Tremblay"/><argument value="4000"/><!-- peer (successor) host name and port-->
+ </process>
+</platform_description>
diff --git a/examples/gras/mutual-exclusion/simple-token/test_rl.in b/examples/gras/mutual-exclusion/simple-token/test_rl.in
new file mode 100755 (executable)
index 0000000..d381ced
--- /dev/null
@@ -0,0 +1,17 @@
+#! @BASH@ -e
+if [ x@EXEEXT@ = x ] ; then 
+#  exenv="libtool --mode=execute valgrind --db-attach=yes --run-libc-freeres=no "
+#  exenv="libtool --mode=execute valgrind --run-libc-freeres=no "
+  exenv=$SG_TEST_EXENV
+else
+  exenv=wine
+fi
+    
+$exenv ./tokenS_node@EXEEXT@ 4000 127.0.0.1 4010 --create-token $@ &
+# $exenv ./tokenS_node@EXEEXT@ 4010 127.0.0.1 4000 $@ &
+# exit 0
+$exenv ./tokenS_node@EXEEXT@ 4010 127.0.0.1 4020 $@ &
+$exenv ./tokenS_node@EXEEXT@ 4020 127.0.0.1 4030 $@ &
+$exenv ./tokenS_node@EXEEXT@ 4030 127.0.0.1 4040 $@ &
+$exenv ./tokenS_node@EXEEXT@ 4040 127.0.0.1 4050 $@ &
+$exenv ./tokenS_node@EXEEXT@ 4050 127.0.0.1 4000 $@ 
diff --git a/examples/gras/mutual-exclusion/simple-token/test_sg.in b/examples/gras/mutual-exclusion/simple-token/test_sg.in
new file mode 100755 (executable)
index 0000000..d04f283
--- /dev/null
@@ -0,0 +1,10 @@
+#! @BASH@
+if [ x@EXEEXT@ = x ] ; then 
+#  exenv="libtool --mode=execute valgrind --run-libc-freeres=no --show-reachable=yes "
+  exenv=$SG_TEST_EXENV
+else
+  exenv=wine
+fi
+
+exec $exenv ./tokenS_simulator@EXEEXT@ @top_srcdir@/examples/msg/small_platform.xml @srcdir@/tokenS_deployment.xml $@
+