Logo AND Algorithmique Numérique Distribuée

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