Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d34a22d22922f2179cac1ba99cf447cc9d6694e6
[simgrid.git] / src / kernel / actor / Simcall.cpp
1 /* Copyright (c) 2010-2023. 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 "src/kernel/actor/Simcall.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/kernel/EngineImpl.hpp"
9 #include "src/kernel/actor/ActorImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/kernel/context/Context.hpp"
12 #include "xbt/log.h"
13
14 #if SIMGRID_HAVE_MC
15 #include "simgrid/modelchecker.h"
16 #include "src/mc/mc_replay.hpp"
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_simcall, kernel, "transmuting from user request into kernel handlers");
20
21 namespace simgrid::kernel::actor {
22
23 /** @brief returns a printable string representing a simcall */
24 const char* Simcall::get_cname() const
25 {
26   if (observer_ != nullptr) {
27     static std::string name;
28     name              = boost::core::demangle(typeid(*observer_).name());
29     const char* cname = name.c_str();
30     if (name.rfind("simgrid::kernel::", 0) == 0)
31       cname += 17; // strip prefix "simgrid::kernel::"
32     return cname;
33   } else {
34     return to_c_str(call_);
35   }
36 }
37 ObjectAccessSimcallItem::ObjectAccessSimcallItem()
38 {
39   take_ownership();
40 }
41 void ObjectAccessSimcallItem::take_ownership()
42 {
43   simcall_owner_ = ActorImpl::self();
44 }
45
46 } // namespace simgrid::kernel::actor
47
48 static void simcall(simgrid::kernel::actor::Simcall::Type call, std::function<void()> const& code,
49                     simgrid::kernel::actor::SimcallObserver* observer)
50 {
51   auto self                = simgrid::kernel::actor::ActorImpl::self();
52   self->simcall_.call_     = call;
53   self->simcall_.code_     = &code;
54   self->simcall_.observer_ = observer;
55   if (simgrid::kernel::EngineImpl::get_instance()->is_maestro(self)) {
56     self->simcall_handle(0);
57   } else {
58     XBT_DEBUG("Yield process '%s' on simcall %s", self->get_cname(), self->simcall_.get_cname());
59     self->yield();
60   }
61   self->simcall_.observer_ = nullptr;
62 }
63
64 void simcall_run_answered(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
65 {
66   // The function `code` is called in kernel mode (either because we are already in maestor or after a context switch)
67   // and simcall_answer() is called
68   simcall(simgrid::kernel::actor::Simcall::Type::RUN_ANSWERED, code, observer);
69 }
70
71 void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
72 {
73   // The function `code` is called in kernel mode (either because we are already in maestor or after a context switch)
74   // BUT simcall_answer IS NOT CALLED
75   simcall(simgrid::kernel::actor::Simcall::Type::RUN_BLOCKING, code, observer);
76 }
77
78 void simcall_run_object_access(std::function<void()> const& code, simgrid::kernel::actor::ObjectAccessSimcallItem* item)
79 {
80   auto self = simgrid::kernel::actor::ActorImpl::self();
81
82   // We only need a simcall if the order of the setters is important (parallel run or MC execution).
83   // Otherwise, just call the function with no simcall
84
85   if (simgrid::kernel::context::Context::is_parallel()
86 #if SIMGRID_HAVE_MC
87       || MC_is_active() || MC_record_replay_is_active()
88 #endif
89   ) {
90     simgrid::kernel::actor::ObjectAccessSimcallObserver observer{self, item};
91     simcall(simgrid::kernel::actor::Simcall::Type::RUN_ANSWERED, code, &observer);
92     item->take_ownership();
93   } else {
94     // don't return from the context-switch we don't do
95     self->simcall_.call_     = simgrid::kernel::actor::Simcall::Type::RUN_BLOCKING;
96     self->simcall_.code_     = &code;
97     self->simcall_.observer_ = nullptr;
98     self->simcall_handle(0);
99   }
100 }