Logo AND Algorithmique Numérique Distribuée

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