From 087d11a0d22c93b0d9289684ceca2a8ed1db7e89 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Wed, 22 Jul 2015 13:31:24 +0200 Subject: [PATCH] basic grounding of S4U, the next interface of SimGrid --- buildtools/Cmake/DefinePackages.cmake | 11 ++ include/simgrid/s4u.h | 13 ++ include/simgrid/s4u/engine.hpp | 49 ++++++++ include/simgrid/s4u/host.hpp | 164 ++++++++++++++++++++++++++ include/simgrid/s4u/process.hpp | 134 +++++++++++++++++++++ src/s4u/s4u_engine.cpp | 43 +++++++ src/s4u/s4u_host.cpp | 56 +++++++++ src/s4u/s4u_process.cpp | 100 ++++++++++++++++ 8 files changed, 570 insertions(+) create mode 100644 include/simgrid/s4u.h create mode 100644 include/simgrid/s4u/engine.hpp create mode 100644 include/simgrid/s4u/host.hpp create mode 100644 include/simgrid/s4u/process.hpp create mode 100644 src/s4u/s4u_engine.cpp create mode 100644 src/s4u/s4u_host.cpp create mode 100644 src/s4u/s4u_process.cpp diff --git a/buildtools/Cmake/DefinePackages.cmake b/buildtools/Cmake/DefinePackages.cmake index dcb025e62e..4d5527e653 100644 --- a/buildtools/Cmake/DefinePackages.cmake +++ b/buildtools/Cmake/DefinePackages.cmake @@ -389,6 +389,12 @@ else() src/simix/smx_context_boost.cpp) endif() +set(S4U_SRC + src/s4u/s4u_engine.cpp + src/s4u/s4u_host.cpp + src/s4u/s4u_process.cpp +) + set(SIMGRID_SRC src/simgrid/sg_config.c src/simgrid/host.cpp @@ -688,6 +694,10 @@ set(headers_to_install include/simgrid/simix.h include/simgrid/host.h include/simgrid/link.h + include/simgrid/s4u/s4u/engine.hpp + include/simgrid/s4u/host.hpp + include/simgrid/s4u/process.hpp + include/simgrid/s4u.h include/smpi/mpi.h include/smpi/smpi.h include/smpi/smpi_cocci.h @@ -785,6 +795,7 @@ endif() set(simgrid_sources ${BINDINGS_SRC} ${GTNETS_USED} + ${S4U_SRC} ${MSG_SRC} ${SIMDAG_SRC} ${SIMGRID_SRC} diff --git a/include/simgrid/s4u.h b/include/simgrid/s4u.h new file mode 100644 index 0000000000..d360a3ce0f --- /dev/null +++ b/include/simgrid/s4u.h @@ -0,0 +1,13 @@ +/* Copyright (c) 2004-2015. 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. */ + +#ifndef SIMGRID_S4U_S4U_H +#define SIMGRID_S4U_S4U_H + +#include "simgrid/s4u/engine.hpp" +#include "simgrid/s4u/host.hpp" +#include "simgrid/s4u/process.hpp" + +#endif /* SIMGRID_S4U_S4U_H */ diff --git a/include/simgrid/s4u/engine.hpp b/include/simgrid/s4u/engine.hpp new file mode 100644 index 0000000000..7c2f9aff3c --- /dev/null +++ b/include/simgrid/s4u/engine.hpp @@ -0,0 +1,49 @@ +/* Copyright (c) 2006-2015. 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. */ + +#ifndef SIMGRID_S4U_ENGINE_HPP +#define SIMGRID_S4U_ENGINE_HPP + +namespace simgrid { +namespace s4u { +/** @brief Simulation engine + * + * This class is an interface to the simulation engine. + */ +class Engine { +public: + /** Constructor, taking the command line parameters of your main function */ + Engine(int *argc, char **argv); + + /** @brief Load a platform file describing the environment + * + * The environment is either a XML file following the simgrid.dtd formalism, or a lua file. + * Some examples can be found in the directory examples/platforms. + */ + void loadPlatform(const char *platf); + + /** Registers the main function of a process that will be launched from the deployment file */ + void register_function(const char*name, int (*code)(int,char**)); + + /** Registers a function as the default main function of processes + * + * It will be used as fallback when the function requested from the deployment file was not registered. + * It is used for trace-based simulations (see examples/msg/actions). + */ + void register_default(int (*code)(int,char**)); + + /** @brief Load a deployment file and launch the processes that it contains */ + void loadDeployment(const char *deploy); + + /** @brief Run the simulation */ + void run(); + + /** @brief Retrieve the simulation time */ + static double getClock(); + +}; +}} // namespace simgrid::sgo + +#endif /* SIMGRID_S4U_ENGINE_HPP */ diff --git a/include/simgrid/s4u/host.hpp b/include/simgrid/s4u/host.hpp new file mode 100644 index 0000000000..54399ece84 --- /dev/null +++ b/include/simgrid/s4u/host.hpp @@ -0,0 +1,164 @@ +/* Copyright (c) 2006-2015. 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. */ + +#ifndef SIMGRID_S4U_HOST_HPP +#define SIMGRID_S4U_HOST_HPP + +#include + +#include "simgrid/simix.h" + +namespace simgrid { +namespace s4u { + +class Process; + +/** @brief Simulated machine that can host some processes + * + * It represents some physical resource with computing and networking capabilities. + * + * All hosts are automatically created during the call of the method + * @link{simgrid::s4u::Engine::loadPlatform()}. + * You cannot create a host yourself. + * + * You can retrieve a particular host using @link{simgrid::s4u::Host.byName()}, + * and processes can retrieve the host on which they run using @link{simgrid::s4u::Host.current()}. + */ +class Host { +private: + Host(const char *name); +public: + /** Retrieves an host from its name. */ + static s4u::Host *byName(std::string name); + /** Retrieves the host on which the current process is running */ + static s4u::Host *current(); + + const char* getName(); + + /** Turn sthat host on if it was previously off + * + * All processes on that host which were marked autorestart will be restarted automatically. + * This call does nothing if the host is already on. + */ + void turnOn(); + /** Turns that host off. All processes are forcefully stopped. */ + void turnOff(); + /** Returns if that host is currently up and running */ + bool isOn(); + + + /** Allows to store user data on that host */ + void setData(void *data) {p_userdata = data;} + /** Retrieves the previously stored data */ + void* getData() {return p_userdata;} + +protected: + friend Process; + sg_host_t getInferior() {return p_sghost;} +private: + void*p_userdata=NULL; + sg_host_t p_sghost; + static boost::unordered_map *hosts; +}; + +}} // namespace simgrid::s4u + +#endif /* SIMGRID_S4U_HOST_HPP */ + +#if 0 +/* Bindings to the MSG hosts */ + +/* Copyright (c) 2006-2014. 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. */ + +package org.simgrid.msg; + +import org.simgrid.msg.Storage; + +/* +Host jacquelin; + +try { + jacquelin = Host.getByName("Jacquelin"); +} catch(HostNotFoundException e) { + System.err.println(e.toString()); +} +... +\endverbatim + * + */ +public class Host { + /** + * This static method returns all of the hosts of the installed platform. + * + * @return An array containing all the hosts installed. + * + */ + public native static Host[] all(); + + /** + * This static method sets a mailbox to receive in asynchronous mode. + * + * All messages sent to this mailbox will be transferred to + * the receiver without waiting for the receive call. + * The receive call will still be necessary to use the received data. + * If there is a need to receive some messages asynchronously, and some not, + * two different mailboxes should be used. + * + * @param mailboxName The name of the mailbox + */ + public static native void setAsyncMailbox(String mailboxName); + + /** + * This method returns the number of tasks currently running on a host. + * The external load (comming from an availability trace) is not taken in account. + * + * @return The number of tasks currently running on a host. + */ + public native int getLoad(); + + /** + * This method returns the speed of the processor of a host, + * regardless of the current load of the machine. + * + * @return The speed of the processor of the host in flops. + * + */ + public native double getSpeed(); + + /** + * This method returns the number of core of a host. + * + * @return The speed of the processor of the host in flops. + * + */ + public native double getCoreNumber(); + + /** + * Returns the value of a given host property (set from the platform file). + */ + public native String getProperty(String name); + + /** + * Change the value of a given host property. + */ + public native void setProperty(String name, String value); + + /** This methods returns the list of mount point names on an host + * @return An array containing all mounted storages on the host + */ + public native Storage[] getMountedStorage(); + + /** This methods returns the list of storages attached to an host + * @return An array containing all storages (name) attached to the host + */ + public native String[] getAttachedStorage(); + + +} +#endif diff --git a/include/simgrid/s4u/process.hpp b/include/simgrid/s4u/process.hpp new file mode 100644 index 0000000000..bfe5c49f72 --- /dev/null +++ b/include/simgrid/s4u/process.hpp @@ -0,0 +1,134 @@ +/* Copyright (c) 2006-2015. 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. */ + +#ifndef SIMGRID_S4U_PROCESS_HPP +#define SIMGRID_S4U_PROCESS_HPP + +#include "simgrid/simix.h" + +namespace simgrid { +namespace s4u { + +class Host; + +/** @brief Simulation Agent + * + * A process may be defined as a code executing in a location (host). + * + * All processes should be started from the XML deployment file (using the @link{s4u::Engine::loadDeployment()}), + * even if you can also start new processes directly. + * Separating the deployment in the XML from the logic in the code is a good habit as it makes your simulation easier + * to adapt to new settings. + * + * The code that you define for a given process should be placed in the main method that is virtual. + * For example, a Worker process should be declared as follows: + * + * \verbatim + * #include "s4u/process.hpp" + * + * class Worker : simgrid::s4u::Process { + * + * int main(int argc, char **argv) { + * printf("Hello s4u"); + * } + * }; + * \endverbatim + * + */ +class Process { +public: + Process(const char*procname, s4u::Host *host, int argc, char **argv); + Process(const char*procname, s4u::Host *host, int argc, char **argv, double killTime); + virtual ~Process() {} + + /** The main method of your agent */ + virtual int main(int argc, char **argv)=0; + + /** The process that is currently running */ + static Process *current(); + /** Retrieves the process that have the given PID (or NULL if not existing) */ + static Process *byPid(int pid); + + /** Retrieves the name of that process */ + const char*getName(); + /** Retrieves the host on which that process is running */ + s4u::Host *getHost(); + /** Retrieves the PID of that process */ + int getPid(); + + /** If set to true, the process will automatically restart when its host reboots */ + void setAutoRestart(bool autorestart); + /** Sets the time at which that process should be killed */ + void setKillTime(double time); + /** Retrieves the time at which that process will be killed (or -1 if not set) */ + double getKillTime(); + + /** Ask kindly to all processes to die. Only the issuer will survive. */ + static void killAll(); + /** Ask the process to die. + * + * It will only notice your request when doing a simcall next time (a communication or similar). + * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues. + */ + void kill(); + + /** Block the process sleeping for that amount of seconds (may throws hostFailure) */ + void sleep(double duration); + + /** Block the process, computing the given amount of flops */ + void execute(double flop); + + /** Block the process until it gets a message from the given mailbox */ + //void* recv(const char *mailbox); + + /** Block the process until it gets a string message (to be freed after use) from the given mailbox */ + char *recvstr(const char* mailbox); + + /** Block the process until it delivers a string message (that will be copied) to the given mailbox */ + void sendstr(const char*mailbox, const char*msg); + +protected: + smx_process_t getInferior() {return p_smx_process;} +private: + smx_process_t p_smx_process; +}; +}} // namespace simgrid::s4u + +#endif /* SIMGRID_S4U_PROCESS_HPP */ + +#if 0 + +public abstract class Process implements Runnable { + /** Suspends the process. See {@link #resume()} to resume it afterward */ + public native void suspend(); + /** Resume a process that was suspended by {@link #suspend()}. */ + public native void resume(); + /** Tests if a process is suspended. */ + public native boolean isSuspended(); + + /** + * Returns the value of a given process property. + */ + public native String getProperty(String name); + + + /** + * Migrates a process to another host. + * + * @param host The host where to migrate the process. + * + */ + public native void migrate(Host host); + + public native void exit(); + /** + * This static method returns the current amount of processes running + * + * @return The count of the running processes + */ + public native static int getCount(); + +} +#endif diff --git a/src/s4u/s4u_engine.cpp b/src/s4u/s4u_engine.cpp new file mode 100644 index 0000000000..43e2475b21 --- /dev/null +++ b/src/s4u/s4u_engine.cpp @@ -0,0 +1,43 @@ +/* s4u::Engine Simulation Engine and global functions. */ + +/* Copyright (c) 2006-2015. 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 "simgrid/simix.h" +#include "mc/mc.h" +#include "simgrid/s4u/engine.hpp" + + +using namespace simgrid; + +double s4u::Engine::getClock() { + return SIMIX_get_clock(); +} + +s4u::Engine::Engine(int *argc, char **argv) { + SIMIX_global_init(argc, argv); +} + +void s4u::Engine::loadPlatform(const char *platf) { + SIMIX_create_environment(platf); +} + +void s4u::Engine::register_function(const char*name, int (*code)(int,char**)) { + SIMIX_function_register(name,code); +} +void s4u::Engine::register_default(int (*code)(int,char**)) { + SIMIX_function_register_default(code); +} +void s4u::Engine::loadDeployment(const char *deploy) { + SIMIX_launch_application(deploy); +} + +void s4u::Engine::run() { + if (MC_is_active()) { + MC_run(); + } else { + SIMIX_run(); + } +} diff --git a/src/s4u/s4u_host.cpp b/src/s4u/s4u_host.cpp new file mode 100644 index 0000000000..c9204c3959 --- /dev/null +++ b/src/s4u/s4u_host.cpp @@ -0,0 +1,56 @@ +/* Copyright (c) 2006-2014. 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 "xbt/log.h" +#include "msg/msg_private.h" +#include "simix/smx_process_private.h" + +#include "simgrid/s4u/host.hpp" + +using namespace simgrid; + +boost::unordered_map *simgrid::s4u::Host::hosts + = new boost::unordered_map(); + +s4u::Host::Host(const char*name) { + p_sghost = sg_host_by_name(name); + if (p_sghost==NULL) + xbt_die("No such host: %s",name); //FIXME: raise an exception +} + +s4u::Host *s4u::Host::byName(std::string name) { + s4u::Host * res = NULL; + try { + res = hosts->at(name); + } catch (std::out_of_range& e) {} + + if (res==NULL) { + res = new s4u::Host(name.c_str()); + hosts->insert({name,res}); + } + return res; +} +s4u::Host *s4u::Host::current(){ + smx_process_t smx_proc = SIMIX_process_self(); + if (smx_proc == NULL) + xbt_die("Cannot call Host::current() from the maestro context"); + + return s4u::Host::byName(SIMIX_host_get_name(SIMIX_process_get_host(smx_proc))); +} + +const char* s4u::Host::getName() { + return sg_host_name(p_sghost); +} + +void s4u::Host::turnOn() { + simcall_host_on(p_sghost); +} +void s4u::Host::turnOff() { + simcall_host_off(p_sghost); +} +bool s4u::Host::isOn() { + return simcall_host_get_state(p_sghost); +} diff --git a/src/s4u/s4u_process.cpp b/src/s4u/s4u_process.cpp new file mode 100644 index 0000000000..608c74855a --- /dev/null +++ b/src/s4u/s4u_process.cpp @@ -0,0 +1,100 @@ +/* Copyright (c) 2006-2014. 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 "xbt/log.h" +#include "msg/msg_private.h" +#include "msg/msg_mailbox.h" + +#include "simgrid/s4u/host.hpp" +#include "simgrid/s4u/process.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_process,"S4U processes"); + +/* C main function of a process, running this->main */ +static int s4u_process_runner(int argc, char **argv) { + + smx_process_t smx_proc = SIMIX_process_self(); + simgrid::s4u::Process *proc = (simgrid::s4u::Process*) SIMIX_process_self_get_data(smx_proc); + int res= proc->main(argc,argv); + return res; +} + + + +using namespace simgrid; + +s4u::Process::Process(const char *procname, s4u::Host *host, int argc, char **argv) + : s4u::Process::Process(procname,host, argc,argv, -1) { +} +s4u::Process::Process(const char *procname, s4u::Host *host, int argc, char **argv, double killTime) { + p_smx_process = simcall_process_create(procname, s4u_process_runner, this, host->getName(), killTime, argc, argv, NULL/*properties*/,0); + + xbt_assert(p_smx_process,"Cannot create the process"); +// TRACE_msg_process_create(procname, simcall_process_get_PID(p_smx_process), host->getInferior()); +// simcall_process_on_exit(p_smx_process,(int_f_pvoid_pvoid_t)TRACE_msg_process_kill,p_smx_process); +} + +s4u::Process *s4u::Process::current() { + smx_process_t smx_proc = SIMIX_process_self(); + return (simgrid::s4u::Process*) SIMIX_process_self_get_data(smx_proc); +} +s4u::Process *s4u::Process::byPid(int pid) { + return (simgrid::s4u::Process*) SIMIX_process_self_get_data(SIMIX_process_from_PID(pid)); +} + +void s4u::Process::setAutoRestart(bool autorestart) { + simcall_process_auto_restart_set(p_smx_process,autorestart); +} + +s4u::Host *s4u::Process::getHost() { + return s4u::Host::byName(sg_host_name(simcall_process_get_host(p_smx_process))); +} +const char* s4u::Process::getName() { + return simcall_process_get_name(p_smx_process); +} +int s4u::Process::getPid(){ + return simcall_process_get_PID(p_smx_process); +} + +void s4u::Process::setKillTime(double time) { + simcall_process_set_kill_time(p_smx_process,time); +} +double s4u::Process::getKillTime() { + return simcall_process_get_kill_time(p_smx_process); +} +void s4u::Process::killAll() { + simcall_process_killall(1); +} +void s4u::Process::kill() { + simcall_process_kill(p_smx_process); +} + +void s4u::Process::sleep(double duration) { + simcall_process_sleep(duration); +} + +void s4u::Process::execute(double flops) { + simcall_process_execute(NULL,flops,1.0/*priority*/,0./*bound*/, 0L/*affinity*/); +} + +char *s4u::Process::recvstr(const char* mailbox) { + char *res=NULL; + size_t res_size=sizeof(res); + smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox); + + simcall_comm_recv(rdv,&res,&res_size,NULL,NULL,NULL,-1 /* timeout */,-1 /*rate*/); + + return res; +} +void s4u::Process::sendstr(const char* mailbox, const char*msg) { + char *msg_cpy=xbt_strdup(msg); + smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox); + smx_synchro_t comm = simcall_comm_isend(p_smx_process, rdv, strlen(msg), + -1/*rate*/, msg_cpy, sizeof(void *), + NULL, NULL, NULL,NULL/*data*/, 0); + simcall_comm_wait(comm, -1/*timeout*/); +} + -- 2.20.1