Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert getCname to get_cname
[simgrid.git] / src / s4u / s4u_actor.cpp
1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "xbt/log.h"
7
8 #include "simgrid/s4u/Actor.hpp"
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Exec.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13
14 #include "src/kernel/context/Context.hpp"
15 #include "src/simix/smx_private.hpp"
16
17 #include <sstream>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
20
21 namespace simgrid {
22 namespace s4u {
23
24 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onCreation;
25 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onDestruction;
26
27 // ***** Actor creation *****
28 ActorPtr Actor::self()
29 {
30   smx_context_t self_context = SIMIX_context_self();
31   if (self_context == nullptr)
32     return simgrid::s4u::ActorPtr();
33
34   return self_context->process()->iface();
35 }
36
37 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
38 {
39   simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
40   return actor->iface();
41 }
42
43 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
44 {
45   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
46   simgrid::simix::ActorCode code = factory(std::move(args));
47   simgrid::kernel::actor::ActorImpl* actor  = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
48   return actor->iface();
49 }
50
51 void intrusive_ptr_add_ref(Actor* actor)
52 {
53   intrusive_ptr_add_ref(actor->pimpl_);
54 }
55 void intrusive_ptr_release(Actor* actor)
56 {
57   intrusive_ptr_release(actor->pimpl_);
58 }
59
60 // ***** Actor methods *****
61
62 void Actor::join() {
63   simcall_process_join(this->pimpl_, -1);
64 }
65
66 void Actor::join(double timeout)
67 {
68   simcall_process_join(this->pimpl_, timeout);
69 }
70
71 void Actor::setAutoRestart(bool autorestart) {
72   simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
73 }
74
75 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
76 {
77   simcall_process_on_exit(pimpl_, fun, data);
78 }
79
80 /** @brief Moves the actor to another host
81  *
82  * If the actor is currently blocked on an execution activity, the activity is also
83  * migrated to the new host. If it's blocked on another kind of activity, an error is
84  * raised as the mandated code is not written yet. Please report that bug if you need it.
85  *
86  * Asynchronous activities started by the actor are not migrated automatically, so you have
87  * to take care of this yourself (only you knows which ones should be migrated).
88  */
89 void Actor::migrate(Host* new_host)
90 {
91   std::string key;
92   simgrid::instr::LinkType* link = nullptr;
93   if (TRACE_actor_is_enabled()) {
94     static long long int counter = 0;
95
96     key = std::to_string(counter);
97     counter++;
98
99     // start link
100     container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
101     link                        = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
102     link->startEvent(actor_container, "M", key);
103
104     // destroy existing container of this process
105     actor_container->removeFromParent();
106   }
107
108   simgrid::simix::kernelImmediate([this, new_host]() {
109     if (pimpl_->waiting_synchro != nullptr) {
110       // The actor is blocked on an activity. If it's an exec, migrate it too.
111       // FIXME: implement the migration of other kind of activities
112       simgrid::kernel::activity::ExecImplPtr exec =
113           boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
114       xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
115       exec->migrate(new_host);
116     }
117     SIMIX_process_change_host(this->pimpl_, new_host);
118   });
119
120   if (TRACE_actor_is_enabled()) {
121     // create new container on the new_host location
122     simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
123     // end link
124     link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
125   }
126 }
127
128 s4u::Host* Actor::getHost()
129 {
130   return this->pimpl_->host;
131 }
132
133 void Actor::daemonize()
134 {
135   simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
136 }
137
138 bool Actor::isDaemon()
139 {
140   return this->pimpl_->isDaemon();
141 }
142
143 const simgrid::xbt::string& Actor::get_name() const
144 {
145   return this->pimpl_->get_name();
146 }
147
148 const char* Actor::get_cname() const
149 {
150   return this->pimpl_->get_cname();
151 }
152
153 aid_t Actor::getPid()
154 {
155   return this->pimpl_->pid;
156 }
157
158 aid_t Actor::getPpid()
159 {
160   return this->pimpl_->ppid;
161 }
162
163 void Actor::suspend()
164 {
165   if (TRACE_actor_is_enabled())
166     simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
167
168   simcall_process_suspend(pimpl_);
169 }
170
171 void Actor::resume()
172 {
173   simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
174   if (TRACE_actor_is_enabled())
175     simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
176 }
177
178 int Actor::isSuspended()
179 {
180   return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
181 }
182
183 void Actor::setKillTime(double time) {
184   simcall_process_set_kill_time(pimpl_,time);
185 }
186
187 /** \brief Get the kill time of an actor(or 0 if unset). */
188 double Actor::getKillTime()
189 {
190   return SIMIX_timer_get_date(pimpl_->kill_timer);
191 }
192
193 void Actor::kill(aid_t pid)
194 {
195   smx_actor_t killer  = SIMIX_process_self();
196   smx_actor_t process = SIMIX_process_from_PID(pid);
197   if(process != nullptr) {
198     simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
199   } else {
200     std::ostringstream oss;
201     oss << "kill: (" << pid << ") - No such actor" << std::endl;
202     throw std::runtime_error(oss.str());
203   }
204 }
205
206 void Actor::kill() {
207   smx_actor_t process = SIMIX_process_self();
208   simgrid::simix::kernelImmediate(
209       [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
210 }
211
212 smx_actor_t Actor::getImpl()
213 {
214   return pimpl_;
215 }
216
217 // ***** Static functions *****
218
219 ActorPtr Actor::byPid(aid_t pid)
220 {
221   smx_actor_t process = SIMIX_process_from_PID(pid);
222   if (process != nullptr)
223     return process->iface();
224   else
225     return ActorPtr();
226 }
227
228 void Actor::killAll()
229 {
230   simcall_process_killall();
231 }
232
233 std::map<std::string, std::string>* Actor::getProperties()
234 {
235   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
236 }
237
238 /** Retrieve the property value (or nullptr if not set) */
239 const char* Actor::getProperty(const char* key)
240 {
241   return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
242 }
243
244 void Actor::setProperty(const char* key, const char* value)
245 {
246   simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
247 }
248
249 Actor* Actor::restart()
250 {
251   return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
252 }
253
254 // ***** this_actor *****
255
256 namespace this_actor {
257
258 /** Returns true if run from the kernel mode, and false if run from a real actor
259  *
260  * Everything that is run out of any actor (simulation setup before the engine is run,
261  * computing the model evolutions as a result to the actors' action, etc) is run in
262  * kernel mode, just as in any operating systems.
263  *
264  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
265  * because it is the one scheduling when the others should move or wait.
266  */
267 bool isMaestro()
268 {
269   smx_actor_t process = SIMIX_process_self();
270   return process == nullptr || process == simix_global->maestro_process;
271 }
272
273 void sleep_for(double duration)
274 {
275   if (duration > 0)
276     simcall_process_sleep(duration);
277 }
278
279 void yield()
280 {
281   simgrid::simix::kernelImmediate([] { /* do nothing*/ });
282 }
283
284 XBT_PUBLIC void sleep_until(double timeout)
285 {
286   double now = SIMIX_get_clock();
287   if (timeout > now)
288     simcall_process_sleep(timeout - now);
289 }
290
291 void execute(double flops)
292 {
293   smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
294   simcall_execution_wait(s);
295 }
296
297 void execute(double flops, double priority)
298 {
299   smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
300   simcall_execution_wait(s);
301 }
302
303 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
304 {
305   smx_activity_t s =
306       simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
307   simcall_execution_wait(s);
308 }
309
310 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
311 {
312   smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
313   simcall_execution_wait(s);
314 }
315
316 ExecPtr exec_init(double flops_amount)
317 {
318   ExecPtr res        = ExecPtr(new Exec());
319   res->host_         = getHost();
320   res->flops_amount_ = flops_amount;
321   res->setRemains(flops_amount);
322   return res;
323 }
324
325 ExecPtr exec_async(double flops)
326 {
327   ExecPtr res = exec_init(flops);
328   res->start();
329   return res;
330 }
331
332 aid_t getPid()
333 {
334   return SIMIX_process_self()->pid;
335 }
336
337 aid_t getPpid()
338 {
339   return SIMIX_process_self()->ppid;
340 }
341
342 std::string get_name()
343 {
344   return SIMIX_process_self()->get_name();
345 }
346
347 const char* get_cname()
348 {
349   return SIMIX_process_self()->get_cname();
350 }
351
352 Host* getHost()
353 {
354   return SIMIX_process_self()->host;
355 }
356
357 void suspend()
358 {
359   if (TRACE_actor_is_enabled())
360     instr::Container::byName(get_name() + "-" + std::to_string(getPid()))
361         ->getState("ACTOR_STATE")
362         ->pushEvent("suspend");
363   simcall_process_suspend(SIMIX_process_self());
364 }
365
366 void resume()
367 {
368   smx_actor_t process = SIMIX_process_self();
369   simgrid::simix::kernelImmediate([process] { process->resume(); });
370
371   if (TRACE_actor_is_enabled())
372     instr::Container::byName(get_name() + "-" + std::to_string(getPid()))->getState("ACTOR_STATE")->popEvent();
373 }
374
375 bool isSuspended()
376 {
377   smx_actor_t process = SIMIX_process_self();
378   return simgrid::simix::kernelImmediate([process] { return process->suspended; });
379 }
380
381 void kill()
382 {
383   smx_actor_t process = SIMIX_process_self();
384   simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
385 }
386
387 void onExit(int_f_pvoid_pvoid_t fun, void* data)
388 {
389   simcall_process_on_exit(SIMIX_process_self(), fun, data);
390 }
391
392 /** @brief Moves the current actor to another host
393  *
394  * @see simgrid::s4u::Actor::migrate() for more information
395  */
396 void migrate(Host* new_host)
397 {
398   SIMIX_process_self()->iface()->migrate(new_host);
399 }
400
401 } // namespace this_actor
402 } // namespace s4u
403 } // namespace simgrid
404
405 /* **************************** Public C interface *************************** */
406
407 /** \ingroup m_actor_management
408  * \brief Returns the process ID of \a actor.
409  *
410  * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
411  */
412 int sg_actor_get_PID(sg_actor_t actor)
413 {
414   /* Do not raise an exception here: this function is called by the logs
415    * and the exceptions, so it would be called back again and again */
416   if (actor == nullptr || actor->getImpl() == nullptr)
417     return 0;
418   return actor->getPid();
419 }
420
421 /** \ingroup m_actor_management
422  * \brief Returns the process ID of the parent of \a actor.
423  *
424  * This function checks whether \a actor is a valid pointer and return its parent's PID.
425  * Returns -1 if the actor has not been created by any other actor.
426  */
427 int sg_actor_get_PPID(sg_actor_t actor)
428 {
429   return actor->getPpid();
430 }
431
432 /** \ingroup m_actor_management
433  * \brief Return the name of an actor.
434  */
435 const char* sg_actor_get_name(sg_actor_t actor)
436 {
437   return actor->get_cname();
438 }
439
440 sg_host_t sg_actor_get_host(sg_actor_t actor)
441 {
442   return actor->getHost();
443 }
444
445 /** \ingroup m_actor_management
446  * \brief Returns the value of a given actor property
447  *
448  * \param actor an actor
449  * \param name a property name
450  * \return value of a property (or nullptr if the property is not set)
451  */
452 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
453 {
454   return actor->getProperty(name);
455 }
456
457 /** \ingroup m_actor_management
458  * \brief Return the list of properties
459  *
460  * This function returns all the parameters associated with an actor
461  */
462 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
463 {
464   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
465   xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
466   std::map<std::string, std::string>* props = actor->getProperties();
467   if (props == nullptr)
468     return nullptr;
469   for (auto const& elm : *props) {
470     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
471   }
472   return as_dict;
473 }
474
475 /** \ingroup m_actor_management
476  * \brief Suspend the actor.
477  *
478  * This function suspends the actor by suspending the task on which it was waiting for the completion.
479  */
480 void sg_actor_suspend(sg_actor_t actor)
481 {
482   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
483   actor->suspend();
484 }
485
486 /** \ingroup m_actor_management
487  * \brief Resume a suspended actor.
488  *
489  * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
490  */
491 void sg_actor_resume(sg_actor_t actor)
492 {
493   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
494   actor->resume();
495 }
496
497 /** \ingroup m_actor_management
498  * \brief Returns true if the actor is suspended .
499  *
500  * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
501  */
502 int sg_actor_is_suspended(sg_actor_t actor)
503 {
504   return actor->isSuspended();
505 }
506
507 /**
508  * \ingroup m_actor_management
509  * \brief Restarts an actor from the beginning.
510  */
511 sg_actor_t sg_actor_restart(sg_actor_t actor)
512 {
513   return actor->restart();
514 }
515
516 /** @ingroup m_actor_management
517  * @brief This actor will be terminated automatically when the last non-daemon actor finishes
518  */
519 void sg_actor_daemonize(sg_actor_t actor)
520 {
521   actor->daemonize();
522 }
523
524 /** \ingroup m_actor_management
525  * \brief Migrates an actor to another location.
526  *
527  * This function changes the value of the #sg_host_t on  which \a actor is running.
528  */
529 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
530 {
531   process->migrate(host);
532 }
533
534 /** \ingroup m_actor_management
535 * \brief Wait for the completion of a #sg_actor_t.
536 *
537 * \param actor the actor to wait for
538 * \param timeout wait until the actor is over, or the timeout expires
539 */
540 void sg_actor_join(sg_actor_t actor, double timeout)
541 {
542   actor->join(timeout);
543 }
544
545 void sg_actor_kill(sg_actor_t actor)
546 {
547   actor->kill();
548 }
549
550 /** \ingroup m_actor_management
551  * \brief Set the kill time of an actor.
552  *
553  * \param actor an actor
554  * \param kill_time the time when the actor is killed.
555  */
556 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
557 {
558   actor->setKillTime(kill_time);
559 }