1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_S4U_ACTOR_HPP
7 #define SIMGRID_S4U_ACTOR_HPP
10 #include <simgrid/simix.h>
19 /** @brief Simulation Agent
21 * An actor may be defined as a code executing in a location (host).
23 * All actors should be started from the XML deployment file (using the @link{s4u::Engine::loadDeployment()}),
24 * even if you can also start new actors directly.
25 * Separating the deployment in the XML from the logic in the code is a good habit as it makes your simulation easier
26 * to adapt to new settings.
28 * The code that you define for a given actor should be placed in the main method that is virtual.
29 * For example, a Worker actor should be declared as follows:
32 * #include "s4u/actor.hpp"
34 * class Worker : simgrid::s4u::Actor {
36 * int main(int argc, char **argv) {
37 * printf("Hello s4u");
43 XBT_PUBLIC_CLASS Actor {
45 Actor(smx_process_t smx_proc);
47 Actor(const char*name, s4u::Host *host, int argc, char **argv);
48 Actor(const char*name, s4u::Host *host, int argc, char **argv, double killTime);
51 /** The main method of your agent */
52 virtual int main(int argc, char **argv);
54 /** The Actor that is currently running */
55 static Actor *current();
56 /** Retrieves the actor that have the given PID (or NULL if not existing) */
57 static Actor *byPid(int pid);
59 /** Retrieves the name of that actor */
61 /** Retrieves the host on which that actor is running */
63 /** Retrieves the PID of that actor */
66 /** If set to true, the actor will automatically restart when its host reboots */
67 void setAutoRestart(bool autorestart);
68 /** Sets the time at which that actor should be killed */
69 void setKillTime(double time);
70 /** Retrieves the time at which that actor will be killed (or -1 if not set) */
73 /** Ask kindly to all actors to die. Only the issuer will survive. */
74 static void killAll();
75 /** Ask the actor to die.
77 * It will only notice your request when doing a simcall next time (a communication or similar).
78 * SimGrid sometimes have issues when you kill actors that are currently communicating and such. We are working on it to fix the issues.
82 /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
83 void sleep(double duration);
85 /** Block the actor, computing the given amount of flops */
86 e_smx_state_t execute(double flop);
88 /** Block the actor until it gets a message from the given mailbox.
90 * See \ref Comm for the full communication API (including non blocking communications).
92 void *recv(Mailbox &chan);
94 /** Block the actor until it delivers a message of the given simulated size to the given mailbox
96 * See \ref Comm for the full communication API (including non blocking communications).
98 void send(Mailbox &chan, void*payload, size_t simulatedSize);
101 smx_process_t getInferior() {return p_smx_process;}
103 smx_process_t p_smx_process;
105 }} // namespace simgrid::s4u
107 #endif /* SIMGRID_S4U_ACTOR_HPP */
111 public abstract class Actor implements Runnable {
112 /** Suspends the process. See {@link #resume()} to resume it afterward */
113 public native void suspend();
114 /** Resume a process that was suspended by {@link #suspend()}. */
115 public native void resume();
116 /** Tests if a process is suspended. */
117 public native boolean isSuspended();
120 * Returns the value of a given process property.
122 public native String getProperty(String name);
126 * Migrates a process to another host.
128 * @param host The host where to migrate the process.
131 public native void migrate(Host host);
133 public native void exit();
135 * This static method returns the current amount of processes running
137 * @return The count of the running processes
139 public native static int getCount();