Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
efe5bffd31304f8f69d301a04c927e99710ca3e7
[simgrid.git] / src / kernel / actor / SynchroObserver.cpp
1 /* Copyright (c) 2019-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/SynchroObserver.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/kernel/activity/BarrierImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/activity/SemaphoreImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/mc/mc_config.hpp"
13
14 #include <sstream>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(obs_mutex, mc_observer, "Logging specific to mutex simcalls observation");
17
18 namespace simgrid::kernel::actor {
19
20 MutexObserver::MutexObserver(ActorImpl* actor, mc::Transition::Type type, activity::MutexImpl* mutex)
21     : SimcallObserver(actor), type_(type), mutex_(mutex)
22 {
23   xbt_assert(mutex_);
24 }
25
26 void MutexObserver::serialize(std::stringstream& stream) const
27 {
28   const auto* owner = get_mutex()->get_owner();
29   stream << (short)type_ << ' ' << get_mutex()->get_id() << ' ' << (owner != nullptr ? owner->get_pid() : -1);
30 }
31 std::string MutexObserver::to_string() const
32 {
33   return std::string(mc::Transition::to_c_str(type_)) + "(mutex_id:" + std::to_string(get_mutex()->get_id()) +
34          " owner:" + std::to_string(get_mutex()->get_owner()->get_pid()) + ")";
35 }
36
37 bool MutexObserver::is_enabled()
38 {
39   // Only wait can be disabled
40   return type_ != mc::Transition::Type::MUTEX_WAIT || mutex_->get_owner() == get_issuer();
41 }
42
43 SemaphoreObserver::SemaphoreObserver(ActorImpl* actor, mc::Transition::Type type, activity::SemaphoreImpl* sem)
44     : SimcallObserver(actor), type_(type), sem_(sem)
45 {
46   xbt_assert(sem_);
47 }
48
49 void SemaphoreObserver::serialize(std::stringstream& stream) const
50 {
51   stream << (short)type_ << ' ' << get_sem()->get_id() << ' ' << false /* Granted is ignored for LOCK/UNLOCK */;
52 }
53 std::string SemaphoreObserver::to_string() const
54 {
55   return std::string(mc::Transition::to_c_str(type_)) + "(sem_id:" + std::to_string(get_sem()->get_id()) + ")";
56 }
57
58 SemaphoreAcquisitionObserver::SemaphoreAcquisitionObserver(ActorImpl* actor, mc::Transition::Type type,
59                                                            activity::SemAcquisitionImpl* acqui, double timeout)
60     : ResultingSimcall(actor, false), type_(type), acquisition_(acqui), timeout_(timeout)
61 {
62 }
63 bool SemaphoreAcquisitionObserver::is_enabled()
64 {
65   return acquisition_->granted_;
66 }
67 void SemaphoreAcquisitionObserver::serialize(std::stringstream& stream) const
68 {
69   stream << (short)type_ << ' ' << acquisition_->semaphore_->get_id() << ' ' << acquisition_->granted_;
70 }
71 std::string SemaphoreAcquisitionObserver::to_string() const
72 {
73   return std::string(mc::Transition::to_c_str(type_)) +
74          "(sem_id:" + std::to_string(acquisition_->semaphore_->get_id()) + ' ' +
75          (acquisition_->granted_ ? "granted)" : "not granted)");
76 }
77
78 BarrierObserver::BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierImpl* bar)
79     : ResultingSimcall(actor, false), type_(type), barrier_(bar), timeout_(-1)
80 {
81   xbt_assert(type_ == mc::Transition::Type::BARRIER_ASYNC_LOCK);
82 }
83 BarrierObserver::BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierAcquisitionImpl* acqui,
84                                  double timeout)
85     : ResultingSimcall(actor, false), type_(type), acquisition_(acqui), timeout_(timeout)
86 {
87   xbt_assert(type_ == mc::Transition::Type::BARRIER_WAIT);
88 }
89 void BarrierObserver::serialize(std::stringstream& stream) const
90 {
91   xbt_assert(barrier_ != nullptr || (acquisition_ != nullptr && acquisition_->barrier_ != nullptr));
92   stream << (short)type_ << ' ' << (barrier_ != nullptr ? barrier_->get_id() : acquisition_->barrier_->get_id());
93 }
94 std::string BarrierObserver::to_string() const
95 {
96   return std::string(mc::Transition::to_c_str(type_)) +
97          "(barrier_id:" + std::to_string(barrier_ != nullptr ? barrier_->get_id() : acquisition_->barrier_->get_id()) +
98          ")";
99 }
100 bool BarrierObserver::is_enabled()
101 {
102   return type_ == mc::Transition::Type::BARRIER_ASYNC_LOCK ||
103          (type_ == mc::Transition::Type::BARRIER_WAIT && acquisition_ != nullptr && acquisition_->granted_);
104 }
105
106 } // namespace simgrid::kernel::actor