From: mquinson Date: Tue, 5 May 2009 15:59:49 +0000 (+0000) Subject: first draft of the action runner X-Git-Tag: SVN~1365 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/5f7e9bf9cd6ea2c2cfb9246613c03d667d66fd79 first draft of the action runner git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@6270 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/.gitignore b/.gitignore index cba30f55c6..1bb333841e 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/examples/msg/Makefile.am b/examples/msg/Makefile.am index b95deda8db..4198ad671f 100644 --- a/examples/msg/Makefile.am +++ b/examples/msg/Makefile.am @@ -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 index 0000000000..38f6c22842 --- /dev/null +++ b/examples/msg/actions/actions.c @@ -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 +#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 index 0000000000..238a110528 --- /dev/null +++ b/examples/msg/actions/actions.txt @@ -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 index 0000000000..e854ed0369 --- /dev/null +++ b/examples/msg/actions/actions_deployment.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/examples/msg/actions/runme b/examples/msg/actions/runme new file mode 100644 index 0000000000..5810192304 --- /dev/null +++ b/examples/msg/actions/runme @@ -0,0 +1,6 @@ +#! /bin/sh + +set -e +make -C ../../../src +make -C .. +./actions ../small_platform.xml actions_deployment.xml actions.txt diff --git a/include/msg/msg.h b/include/msg/msg.h index 05142be44f..36a2336651 100644 --- a/include/msg/msg.h +++ b/include/msg/msg.h @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 93ff77b29b..3f42ae4c49 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/msg/global.c b/src/msg/global.c index 090574b722..329b1bd76c 100644 --- a/src/msg/global.c +++ b/src/msg/global.c @@ -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 index 0000000000..f2b1e812a7 --- /dev/null +++ b/src/msg/msg_actions.c @@ -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; +} diff --git a/src/msg/private.h b/src/msg/private.h index f72dd97e3e..562b6b0dc8 100644 --- a/src/msg/private.h +++ b/src/msg/private.h @@ -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