Logo AND Algorithmique Numérique Distribuée

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