Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[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 "simgrid/actor.h"
7 #include "simgrid/s4u/Actor.hpp"
8 #include "simgrid/s4u/Exec.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/simix/smx_host_private.hpp"
12 #include "src/simix/smx_private.hpp"
13 #include "src/surf/HostImpl.hpp"
14
15 #include <sstream>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
18
19 namespace simgrid {
20 namespace s4u {
21
22 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_creation;
23 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_suspend;
24 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_resume;
25 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_sleep;
26 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_wake_up;
27 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_migration_start;
28 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_migration_end;
29 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_destruction;
30
31 // ***** Actor creation *****
32 ActorPtr Actor::self()
33 {
34   smx_context_t self_context = SIMIX_context_self();
35   if (self_context == nullptr)
36     return simgrid::s4u::ActorPtr();
37
38   return self_context->process()->iface();
39 }
40
41 ActorPtr Actor::create(std::string name, s4u::Host* host, std::function<void()> code)
42 {
43   simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
44   return actor->iface();
45 }
46
47 ActorPtr Actor::create(std::string name, s4u::Host* host, std::string function, std::vector<std::string> args)
48 {
49   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
50   simgrid::simix::ActorCode code            = factory(std::move(args));
51   simgrid::kernel::actor::ActorImpl* actor  = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
52   return actor->iface();
53 }
54
55 void intrusive_ptr_add_ref(Actor* actor)
56 {
57   intrusive_ptr_add_ref(actor->pimpl_);
58 }
59 void intrusive_ptr_release(Actor* actor)
60 {
61   intrusive_ptr_release(actor->pimpl_);
62 }
63
64 // ***** Actor methods *****
65
66 void Actor::join()
67 {
68   simcall_process_join(this->pimpl_, -1);
69 }
70
71 void Actor::join(double timeout)
72 {
73   simcall_process_join(this->pimpl_, timeout);
74 }
75
76 void Actor::set_auto_restart(bool autorestart)
77 {
78   simgrid::simix::simcall([this, autorestart]() {
79     pimpl_->set_auto_restart(autorestart);
80
81     std::map<std::string, kernel::actor::ProcessArg*> actors_map = pimpl_->host_->pimpl_->actors_at_boot_;
82     if (actors_map.find(pimpl_->name_) == actors_map.end()) {
83       simgrid::kernel::actor::ProcessArg* arg = new simgrid::kernel::actor::ProcessArg(pimpl_->host_, pimpl_);
84       XBT_DEBUG("Adding Process %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
85       actors_map.insert({arg->name, arg});
86     }
87   });
88 }
89
90 void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data) /* deprecated */
91 {
92   simgrid::simix::simcall([this, fun, data] { SIMIX_process_on_exit(pimpl_, fun, data); });
93 }
94
95 void Actor::on_exit(std::function<void(int, void*)> fun, void* data)
96 {
97   simgrid::simix::simcall([this, fun, data] { SIMIX_process_on_exit(pimpl_, fun, data); });
98 }
99
100 void Actor::migrate(Host* new_host)
101 {
102   s4u::Actor::on_migration_start(this);
103
104   simgrid::simix::simcall([this, new_host]() {
105     if (pimpl_->waiting_synchro != nullptr) {
106       // The actor is blocked on an activity. If it's an exec, migrate it too.
107       // FIXME: implement the migration of other kind of activities
108       simgrid::kernel::activity::ExecImplPtr exec =
109           boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
110       xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
111       exec->migrate(new_host);
112     }
113     SIMIX_process_change_host(this->pimpl_, new_host);
114   });
115
116   s4u::Actor::on_migration_end(this);
117 }
118
119 s4u::Host* Actor::get_host()
120 {
121   return this->pimpl_->host_;
122 }
123
124 void Actor::daemonize()
125 {
126   simgrid::simix::simcall([this]() { pimpl_->daemonize(); });
127 }
128
129 bool Actor::is_daemon() const
130 {
131   return this->pimpl_->is_daemon();
132 }
133
134 const simgrid::xbt::string& Actor::get_name() const
135 {
136   return this->pimpl_->get_name();
137 }
138
139 const char* Actor::get_cname() const
140 {
141   return this->pimpl_->get_cname();
142 }
143
144 aid_t Actor::get_pid() const
145 {
146   return this->pimpl_->pid_;
147 }
148
149 aid_t Actor::get_ppid() const
150 {
151   return this->pimpl_->ppid_;
152 }
153
154 void Actor::suspend()
155 {
156   s4u::Actor::on_suspend(this);
157   simcall_process_suspend(pimpl_);
158 }
159
160 void Actor::resume()
161 {
162   simgrid::simix::simcall([this] { pimpl_->resume(); });
163   s4u::Actor::on_resume(this);
164 }
165
166 bool Actor::is_suspended()
167 {
168   return simgrid::simix::simcall([this] { return pimpl_->suspended_; });
169 }
170
171 void Actor::set_kill_time(double time)
172 {
173   simcall_process_set_kill_time(pimpl_, time);
174 }
175
176 /** @brief Get the kill time of an actor(or 0 if unset). */
177 double Actor::get_kill_time()
178 {
179   return SIMIX_timer_get_date(pimpl_->kill_timer);
180 }
181
182 void Actor::kill(aid_t pid)
183 {
184   smx_actor_t killer  = SIMIX_process_self();
185   smx_actor_t process = SIMIX_process_from_PID(pid);
186   if (process != nullptr) {
187     simgrid::simix::simcall([killer, process] { SIMIX_process_kill(process, killer); });
188   } else {
189     std::ostringstream oss;
190     oss << "kill: (" << pid << ") - No such actor" << std::endl;
191     throw std::runtime_error(oss.str());
192   }
193 }
194
195 void Actor::kill()
196 {
197   smx_actor_t process = SIMIX_process_self();
198   simgrid::simix::simcall(
199       [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
200 }
201
202 smx_actor_t Actor::get_impl()
203 {
204   return pimpl_;
205 }
206
207 // ***** Static functions *****
208
209 ActorPtr Actor::by_pid(aid_t pid)
210 {
211   smx_actor_t process = SIMIX_process_from_PID(pid);
212   if (process != nullptr)
213     return process->iface();
214   else
215     return ActorPtr();
216 }
217
218 void Actor::kill_all()
219 {
220   smx_actor_t self = SIMIX_process_self();
221   simgrid::simix::simcall([&self] { SIMIX_process_killall(self); });
222 }
223
224 std::unordered_map<std::string, std::string>* Actor::get_properties()
225 {
226   return simgrid::simix::simcall([this] { return this->pimpl_->get_properties(); });
227 }
228
229 /** Retrieve the property value (or nullptr if not set) */
230 const char* Actor::get_property(std::string key)
231 {
232   return simgrid::simix::simcall([this, key] { return pimpl_->get_property(key); });
233 }
234
235 void Actor::set_property(std::string key, std::string value)
236 {
237   simgrid::simix::simcall([this, key, value] { pimpl_->set_property(key, value); });
238 }
239
240 Actor* Actor::restart()
241 {
242   return simgrid::simix::simcall([this]() { return pimpl_->restart(); });
243 }
244
245 // ***** this_actor *****
246
247 namespace this_actor {
248
249 /** Returns true if run from the kernel mode, and false if run from a real actor
250  *
251  * Everything that is run out of any actor (simulation setup before the engine is run,
252  * computing the model evolutions as a result to the actors' action, etc) is run in
253  * kernel mode, just as in any operating systems.
254  *
255  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
256  * because it is the one scheduling when the others should move or wait.
257  */
258 bool is_maestro()
259 {
260   smx_actor_t process = SIMIX_process_self();
261   return process == nullptr || process == simix_global->maestro_process;
262 }
263
264 void sleep_for(double duration)
265 {
266   if (duration > 0) {
267     smx_actor_t actor = SIMIX_process_self();
268     simgrid::s4u::Actor::on_sleep(actor->iface());
269
270     simcall_process_sleep(duration);
271
272     simgrid::s4u::Actor::on_wake_up(actor->iface());
273   }
274 }
275
276 void yield()
277 {
278   simgrid::simix::simcall([] { /* do nothing*/ });
279 }
280
281 XBT_PUBLIC void sleep_until(double timeout)
282 {
283   double now = SIMIX_get_clock();
284   if (timeout > now)
285     sleep_for(timeout - now);
286 }
287
288 void execute(double flops)
289 {
290   execute(flops, 1.0 /* priority */);
291 }
292
293 void execute(double flops, double priority)
294 {
295   exec_init(flops)->set_priority(priority)->start()->wait();
296 }
297
298 void parallel_execute(int host_nb, s4u::Host** host_list, double* flops_amount, double* bytes_amount, double timeout)
299 {
300   smx_activity_t s =
301       simcall_execution_parallel_start("", host_nb, host_list, flops_amount, bytes_amount, /* rate */ -1, timeout);
302   simcall_execution_wait(s);
303 }
304
305 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
306 {
307   parallel_execute(host_nb, host_list, flops_amount, bytes_amount, /* timeout */ -1);
308 }
309
310 ExecPtr exec_init(double flops_amount)
311 {
312   ExecPtr res        = ExecPtr(new Exec());
313   res->host_         = get_host();
314   res->flops_amount_ = flops_amount;
315   res->set_remaining(flops_amount);
316   return res;
317 }
318
319 ExecPtr exec_async(double flops)
320 {
321   ExecPtr res = exec_init(flops);
322   res->start();
323   return res;
324 }
325
326 aid_t get_pid()
327 {
328   return SIMIX_process_self()->pid_;
329 }
330
331 aid_t get_ppid()
332 {
333   return SIMIX_process_self()->ppid_;
334 }
335
336 std::string get_name()
337 {
338   return SIMIX_process_self()->get_name();
339 }
340
341 const char* get_cname()
342 {
343   return SIMIX_process_self()->get_cname();
344 }
345
346 Host* get_host()
347 {
348   return SIMIX_process_self()->host_;
349 }
350
351 void suspend()
352 {
353   smx_actor_t actor = SIMIX_process_self();
354   simgrid::s4u::Actor::on_suspend(actor->iface());
355
356   simcall_process_suspend(actor);
357 }
358
359 void resume()
360 {
361   smx_actor_t process = SIMIX_process_self();
362   simgrid::simix::simcall([process] { process->resume(); });
363   simgrid::s4u::Actor::on_resume(process->iface());
364 }
365
366 bool is_suspended()
367 {
368   smx_actor_t process = SIMIX_process_self();
369   return simgrid::simix::simcall([process] { return process->suspended_; });
370 }
371
372 void exit()
373 {
374   smx_actor_t process = SIMIX_process_self();
375   simgrid::simix::simcall([process] { SIMIX_process_kill(process, process); });
376 }
377
378 void on_exit(std::function<void(int, void*)> fun, void* data)
379 {
380   SIMIX_process_self()->iface()->on_exit(fun, data);
381 }
382
383 /** @brief Moves the current actor to another host
384  *
385  * @see simgrid::s4u::Actor::migrate() for more information
386  */
387 void migrate(Host* new_host)
388 {
389   SIMIX_process_self()->iface()->migrate(new_host);
390 }
391
392 std::string getName() /* deprecated */
393 {
394   return get_name();
395 }
396 const char* getCname() /* deprecated */
397 {
398   return get_cname();
399 }
400 bool isMaestro() /* deprecated */
401 {
402   return is_maestro();
403 }
404 aid_t getPid() /* deprecated */
405 {
406   return get_pid();
407 }
408 aid_t getPpid() /* deprecated */
409 {
410   return get_ppid();
411 }
412 Host* getHost() /* deprecated */
413 {
414   return get_host();
415 }
416 bool isSuspended() /* deprecated */
417 {
418   return is_suspended();
419 }
420 void on_exit(int_f_pvoid_pvoid_t fun, void* data) /* deprecated */
421 {
422   SIMIX_process_self()->iface()->on_exit([fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
423 }
424 void onExit(int_f_pvoid_pvoid_t fun, void* data) /* deprecated */
425 {
426   on_exit([fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
427 }
428 void kill() /* deprecated */
429 {
430   exit();
431 }
432
433 } // namespace this_actor
434 } // namespace s4u
435 } // namespace simgrid
436
437 /* **************************** Public C interface *************************** */
438
439 /** @ingroup m_actor_management
440  * @brief Returns the process ID of @a actor.
441  *
442  * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
443  */
444 int sg_actor_get_PID(sg_actor_t actor)
445 {
446   /* Do not raise an exception here: this function is called by the logs
447    * and the exceptions, so it would be called back again and again */
448   if (actor == nullptr || actor->get_impl() == nullptr)
449     return 0;
450   return actor->get_pid();
451 }
452
453 /** @ingroup m_actor_management
454  * @brief Returns the process ID of the parent of @a actor.
455  *
456  * This function checks whether @a actor is a valid pointer and return its parent's PID.
457  * Returns -1 if the actor has not been created by any other actor.
458  */
459 int sg_actor_get_PPID(sg_actor_t actor)
460 {
461   return actor->get_ppid();
462 }
463
464 /** @ingroup m_actor_management
465  *
466  * @brief Return a #sg_actor_t given its PID.
467  *
468  * This function search in the list of all the created sg_actor_t for a sg_actor_t  whose PID is equal to @a PID.
469  * If none is found, @c nullptr is returned.
470    Note that the PID are unique in the whole simulation, not only on a given host.
471  */
472 sg_actor_t sg_actor_by_PID(aid_t pid)
473 {
474   return simgrid::s4u::Actor::by_pid(pid).get();
475 }
476
477 /** @ingroup m_actor_management
478  * @brief Return the name of an actor.
479  */
480 const char* sg_actor_get_name(sg_actor_t actor)
481 {
482   return actor->get_cname();
483 }
484
485 sg_host_t sg_actor_get_host(sg_actor_t actor)
486 {
487   return actor->get_host();
488 }
489
490 /** @ingroup m_actor_management
491  * @brief Returns the value of a given actor property
492  *
493  * @param actor an actor
494  * @param name a property name
495  * @return value of a property (or nullptr if the property is not set)
496  */
497 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
498 {
499   return actor->get_property(name);
500 }
501
502 /** @ingroup m_actor_management
503  * @brief Return the list of properties
504  *
505  * This function returns all the parameters associated with an actor
506  */
507 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
508 {
509   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
510   xbt_dict_t as_dict                        = xbt_dict_new_homogeneous(xbt_free_f);
511   std::unordered_map<std::string, std::string>* props = actor->get_properties();
512   if (props == nullptr)
513     return nullptr;
514   for (auto const& kv : *props) {
515     xbt_dict_set(as_dict, kv.first.c_str(), xbt_strdup(kv.second.c_str()), nullptr);
516   }
517   return as_dict;
518 }
519
520 /** @ingroup m_actor_management
521  * @brief Suspend the actor.
522  *
523  * This function suspends the actor by suspending the task on which it was waiting for the completion.
524  */
525 void sg_actor_suspend(sg_actor_t actor)
526 {
527   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
528   actor->suspend();
529 }
530
531 /** @ingroup m_actor_management
532  * @brief Resume a suspended actor.
533  *
534  * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
535  */
536 void sg_actor_resume(sg_actor_t actor)
537 {
538   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
539   actor->resume();
540 }
541
542 /** @ingroup m_actor_management
543  * @brief Returns true if the actor is suspended .
544  *
545  * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
546  */
547 int sg_actor_is_suspended(sg_actor_t actor)
548 {
549   return actor->is_suspended();
550 }
551
552 /**
553  * @ingroup m_actor_management
554  * @brief Restarts an actor from the beginning.
555  */
556 sg_actor_t sg_actor_restart(sg_actor_t actor)
557 {
558   return actor->restart();
559 }
560
561 /**
562  * @ingroup m_actor_management
563  * @brief Sets the "auto-restart" flag of the actor.
564  * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
565  */
566 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
567 {
568   actor->set_auto_restart(auto_restart);
569 }
570
571 /** @ingroup m_actor_management
572  * @brief This actor will be terminated automatically when the last non-daemon actor finishes
573  */
574 void sg_actor_daemonize(sg_actor_t actor)
575 {
576   actor->daemonize();
577 }
578
579 /** @ingroup m_actor_management
580  * @brief Migrates an actor to another location.
581  *
582  * This function changes the value of the #sg_host_t on  which @a actor is running.
583  */
584 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
585 {
586   process->migrate(host);
587 }
588
589 /** @ingroup m_actor_management
590  * @brief Wait for the completion of a #sg_actor_t.
591  *
592  * @param actor the actor to wait for
593  * @param timeout wait until the actor is over, or the timeout expires
594  */
595 void sg_actor_join(sg_actor_t actor, double timeout)
596 {
597   actor->join(timeout);
598 }
599
600 void sg_actor_kill(sg_actor_t actor)
601 {
602   actor->kill();
603 }
604
605 void sg_actor_kill_all()
606 {
607   simgrid::s4u::Actor::kill_all();
608 }
609
610 /** @ingroup m_actor_management
611  * @brief Set the kill time of an actor.
612  *
613  * @param actor an actor
614  * @param kill_time the time when the actor is killed.
615  */
616 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
617 {
618   actor->set_kill_time(kill_time);
619 }
620
621 /** Yield the current actor; let the other actors execute first */
622 void sg_actor_yield()
623 {
624   simgrid::s4u::this_actor::yield();
625 }