Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first draft of the action runner
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 5 May 2009 15:59:49 +0000 (15:59 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 5 May 2009 15:59:49 +0000 (15:59 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@6270 48e7efb5-ca39-0410-a469-dd3cf9ba447f

.gitignore
examples/msg/Makefile.am
examples/msg/actions/actions.c [new file with mode: 0644]
examples/msg/actions/actions.txt [new file with mode: 0644]
examples/msg/actions/actions_deployment.xml [new file with mode: 0644]
examples/msg/actions/runme [new file with mode: 0644]
include/msg/msg.h
src/Makefile.am
src/msg/global.c
src/msg/msg_actions.c [new file with mode: 0644]
src/msg/private.h

index cba30f5..1bb3338 100644 (file)
@@ -2,6 +2,7 @@ checkall.log
 TAGS
 *.mk
 *.l[oa]
+*.loT
 *.[oa]
 *.class
 .libs
@@ -64,6 +65,7 @@ examples/gras/spawn/spawn_child
 examples/gras/spawn/spawn_father
 examples/gras/synchro/synchro_philosopher
 examples/gras/timer/timer_client
+examples/msg/actions/actions
 examples/msg/gtnets/gtnets
 examples/msg/masterslave/masterslave_bypass
 examples/msg/masterslave/masterslave_failure
index b95deda..4198ad6 100644 (file)
@@ -80,7 +80,8 @@ noinst_PROGRAMS = sendrecv/sendrecv \
                   parallel_task/parallel_task \
                   parallel_task/test_ptask \
                   priority/priority \
-                  properties/msg_prop
+                  properties/msg_prop \
+                  actions/actions
 
 if HAVE_GTNETS
   noinst_PROGRAMS += gtnets/gtnets gtnets/gtnets_kayo
@@ -91,6 +92,10 @@ endif
 properties_msg_prop_SOURCES = properties/msg_prop.c
 properties_msg_prop_LDADD   = $(top_builddir)/src/libsimgrid.la
 
+# actions example
+actions_actions_SOURCES = actions/actions.c
+actions_actions_LDADD   = $(top_builddir)/src/libsimgrid.la
+
 
 # sendrecv simple example
 sendrecv_sendrecv_SOURCES = sendrecv/sendrecv.c
diff --git a/examples/msg/actions/actions.c b/examples/msg/actions/actions.c
new file mode 100644 (file)
index 0000000..38f6c22
--- /dev/null
@@ -0,0 +1,62 @@
+/*     $Id$     */
+
+/* Copyright (c) 2009. 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. */
+
+#include <stdio.h>
+#include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
+#include "xbt.h" /* calloc, printf */
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
+
+/* My actions */
+static void send(xbt_dynar_t action) {
+       INFO1("Send: %s",xbt_str_join(action," "));
+}
+static void recv(xbt_dynar_t action) {
+       INFO1("Recv: %s",xbt_str_join(action," "));
+}
+static void sleep(xbt_dynar_t action) {
+       INFO1("Recv: %s",xbt_str_join(action," "));
+}
+static void compute(xbt_dynar_t action) {
+       INFO1("compute: %s",xbt_str_join(action," "));
+}
+
+/** Main function */
+int main(int argc, char *argv[]){
+       MSG_error_t res = MSG_OK;
+
+       /* Check the given arguments */
+       MSG_global_init(&argc,argv);
+       if (argc < 4) {
+               printf ("Usage: %s platform_file deployment_file action_files\n",argv[0]);
+               printf ("example: %s msg_platform.xml msg_deployment.xml actions\n",argv[0]);
+               exit(1);
+       }
+
+       /*  Simulation setting */
+       MSG_create_environment(argv[1]);
+
+       /* No need to register functions as in classical MSG programs: the actions get started anyway */
+       MSG_launch_application(argv[2]);
+
+       /*   Action registration */
+       MSG_action_register("send", send);
+       MSG_action_register("recv", recv);
+       MSG_action_register("sleep", sleep);
+       MSG_action_register("compute", sleep);
+
+       /* Actually do the simulation using MSG_action_trace_run */
+       res = MSG_action_trace_run(argv[3]);
+
+       INFO1("Simulation time %g",MSG_get_clock());
+       MSG_clean();
+
+       if(res==MSG_OK)
+               return 0;
+       else
+               return 1;
+} /* end_of_main */
diff --git a/examples/msg/actions/actions.txt b/examples/msg/actions/actions.txt
new file mode 100644 (file)
index 0000000..238a110
--- /dev/null
@@ -0,0 +1,5 @@
+# sample action file
+tutu send toto 12
+toto recv any
+tutu sleep 12
+toto compute 12
diff --git a/examples/msg/actions/actions_deployment.xml b/examples/msg/actions/actions_deployment.xml
new file mode 100644 (file)
index 0000000..e854ed0
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version='1.0'?>
+<!DOCTYPE platform SYSTEM "simgrid.dtd">
+<platform version="2">
+  <process host="Tremblay" function="toto"/>
+  <process host="Jupiter" function="tutu"/>
+</platform>
diff --git a/examples/msg/actions/runme b/examples/msg/actions/runme
new file mode 100644 (file)
index 0000000..5810192
--- /dev/null
@@ -0,0 +1,6 @@
+#! /bin/sh
+
+set -e
+make -C ../../../src
+make -C ..
+./actions ../small_platform.xml actions_deployment.xml actions.txt
index 05142be..36a2336 100644 (file)
@@ -176,5 +176,12 @@ MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t* task, m_host_t host, d
 XBT_PUBLIC(MSG_error_t)
 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task, double timeout);
 
+/************************** Action handling **********************************/
+typedef void (*msg_action_fun)(xbt_dynar_t args);
+XBT_PUBLIC(void) MSG_action_register(const char*action_name, msg_action_fun function);
+XBT_PUBLIC(void) MSG_action_unregister(const char*action_name);
+MSG_error_t MSG_action_trace_run(char*path);
+
+
 SG_END_DECL()
 #endif
index 93ff77b..3f42ae4 100644 (file)
@@ -217,7 +217,8 @@ SMPI_SRC= \
 
 MSG_SRC=  msg/msg_config.c \
   msg/task.c msg/host.c msg/m_process.c msg/gos.c \
-  msg/global.c msg/environment.c msg/deployment.c msg/msg_mailbox.c
+  msg/global.c msg/environment.c msg/deployment.c msg/msg_mailbox.c \
+  msg/msg_actions.c
 
 JMSG_C_SRC = \
   xbt/xbt_context_java.c \
index 090574b..329b1bd 100644 (file)
@@ -71,6 +71,9 @@ void MSG_global_init(int *argc, char **argv)
        /* initialization of the mailbox module */
        MSG_mailbox_mod_init();
 
+       /* initialization of the action module */
+       _MSG_action_init();
+
     SIMIX_function_register_process_create(_MSG_process_create_from_SIMIX);
     SIMIX_function_register_process_cleanup(__MSG_process_cleanup);
     SIMIX_function_register_process_kill(_MSG_process_kill_from_SIMIX);
diff --git a/src/msg/msg_actions.c b/src/msg/msg_actions.c
new file mode 100644 (file)
index 0000000..f2b1e81
--- /dev/null
@@ -0,0 +1,95 @@
+/* Copyright (c) 2009 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. */
+
+#include "msg/private.h"
+#include "xbt/str.h"
+#include "xbt/dynar.h"
+
+static xbt_dict_t action_funs;
+static xbt_dynar_t action_list;
+
+
+/** \ingroup msg_actions
+ * \brief Registers a function to handle a kind of action
+ *
+ * Registers a function to handle a kind of action
+ * This table is then used by #MSG_action_trace_run
+ *
+ * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
+ *
+ * \param name the reference name of the action.
+ * \param code the function; prototype given by the type: void...(xbt_dynar_t action)
+ */
+void MSG_action_register(const char*action_name, msg_action_fun function) {
+       xbt_dict_set(action_funs,action_name,function,NULL);
+}
+/** \ingroup msg_actions
+ * \brief Unregisters a function, which handled a kind of action
+ *
+ * \param name the reference name of the action.
+ */
+void MSG_action_unregister(const char*action_name) {
+       xbt_dict_remove(action_funs,action_name);
+}
+
+static int MSG_action_runner(int argc, char* argv[]) {
+       xbt_dynar_t evt;
+       unsigned int cursor;
+
+       xbt_dynar_foreach(action_list,cursor,evt) {
+               if (!strcmp(xbt_dynar_get_as(evt,0,char*),argv[0])) {
+                       msg_action_fun function = xbt_dict_get(action_funs,xbt_dynar_get_as(evt,1,char*));
+                       (*function)(evt);
+               }
+       }
+
+       return 0;
+}
+
+void _MSG_action_init() {
+       action_funs = xbt_dict_new();
+       action_list = xbt_dynar_new(sizeof(xbt_dynar_t),xbt_dynar_free_voidp);
+       MSG_function_register_default(MSG_action_runner);
+}
+
+/** \ingroup msg_actions
+ * \brief A trace loader
+ *
+ *  Load a trace file containing actions, and execute them.
+ */
+MSG_error_t MSG_action_trace_run(char*path) {
+       MSG_error_t res;
+
+    FILE * fp;
+    char * line = NULL;
+    size_t len = 0;
+    ssize_t read;
+
+    xbt_dynar_t evt;
+
+    fp = fopen(path, "r");
+    xbt_assert2(fp != NULL,"Cannot open %s: %s",path,strerror(errno));
+
+    while ((read = getline(&line, &len, fp)) != -1) {
+       char *comment = strchr(line,'#');
+       if (comment!=NULL)
+               *comment='\0';
+       xbt_str_trim(line,NULL);
+       if (line[0]=='\0')
+               continue;
+       evt = xbt_str_split_quoted(line);
+       xbt_dynar_push(action_list,&evt);
+    }
+
+    if (line)
+        free(line);
+    fclose(fp);
+
+       res = MSG_main();
+       xbt_dynar_free(&action_list);
+       action_list = xbt_dynar_new(sizeof(xbt_dynar_t),xbt_dynar_free_voidp);
+
+       return res;
+}
index f72dd97..562b6b0 100644 (file)
@@ -124,5 +124,7 @@ void *_MSG_process_create_from_SIMIX(const char *name,
                                     char * hostname, int argc, char **argv, xbt_dict_t properties);
 void _MSG_process_kill_from_SIMIX(void *p);
 
+void _MSG_action_init(void);
+
 SG_END_DECL()
 #endif