Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tell the MC transition about the semaphore capacity and use it
[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:" +
35          (get_mutex()->get_owner() == nullptr ? "none" : std::to_string(get_mutex()->get_owner()->get_pid())) + ")";
36 }
37
38 bool MutexObserver::is_enabled()
39 {
40   // Only wait can be disabled
41   return type_ != mc::Transition::Type::MUTEX_WAIT || mutex_->get_owner() == get_issuer();
42 }
43
44 SemaphoreObserver::SemaphoreObserver(ActorImpl* actor, mc::Transition::Type type, activity::SemaphoreImpl* sem)
45     : SimcallObserver(actor), type_(type), sem_(sem)
46 {
47   xbt_assert(sem_);
48 }
49
50 void SemaphoreObserver::serialize(std::stringstream& stream) const
51 {
52   stream << (short)type_ << ' ' << get_sem()->get_id() << ' ' << false /* Granted is ignored for LOCK/UNLOCK */ << ' '
53          << get_sem()->get_capacity();
54 }
55 std::string SemaphoreObserver::to_string() const
56 {
57   return std::string(mc::Transition::to_c_str(type_)) + "(sem_id:" + std::to_string(get_sem()->get_id()) + ")";
58 }
59
60 SemaphoreAcquisitionObserver::SemaphoreAcquisitionObserver(ActorImpl* actor, mc::Transition::Type type,
61                                                            activity::SemAcquisitionImpl* acqui, double timeout)
62     : ResultingSimcall(actor, false), type_(type), acquisition_(acqui), timeout_(timeout)
63 {
64 }
65 bool SemaphoreAcquisitionObserver::is_enabled()
66 {
67   return acquisition_->granted_;
68 }
69 void SemaphoreAcquisitionObserver::serialize(std::stringstream& stream) const
70 {
71   stream << (short)type_ << ' ' << acquisition_->semaphore_->get_id() << ' ' << acquisition_->granted_ << ' '
72          << acquisition_->semaphore_->get_capacity();
73 }
74 std::string SemaphoreAcquisitionObserver::to_string() const
75 {
76   return std::string(mc::Transition::to_c_str(type_)) +
77          "(sem_id:" + std::to_string(acquisition_->semaphore_->get_id()) + ' ' +
78          (acquisition_->granted_ ? "granted)" : "not granted)");
79 }
80
81 BarrierObserver::BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierImpl* bar)
82     : ResultingSimcall(actor, false), type_(type), barrier_(bar), timeout_(-1)
83 {
84   xbt_assert(type_ == mc::Transition::Type::BARRIER_ASYNC_LOCK);
85 }
86 BarrierObserver::BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierAcquisitionImpl* acqui,
87                                  double timeout)
88     : ResultingSimcall(actor, false), type_(type), acquisition_(acqui), timeout_(timeout)
89 {
90   xbt_assert(type_ == mc::Transition::Type::BARRIER_WAIT);
91 }
92 void BarrierObserver::serialize(std::stringstream& stream) const
93 {
94   xbt_assert(barrier_ != nullptr || (acquisition_ != nullptr && acquisition_->barrier_ != nullptr));
95   stream << (short)type_ << ' ' << (barrier_ != nullptr ? barrier_->get_id() : acquisition_->barrier_->get_id());
96 }
97 std::string BarrierObserver::to_string() const
98 {
99   return std::string(mc::Transition::to_c_str(type_)) +
100          "(barrier_id:" + std::to_string(barrier_ != nullptr ? barrier_->get_id() : acquisition_->barrier_->get_id()) +
101          ")";
102 }
103 bool BarrierObserver::is_enabled()
104 {
105   return type_ == mc::Transition::Type::BARRIER_ASYNC_LOCK ||
106          (type_ == mc::Transition::Type::BARRIER_WAIT && acquisition_ != nullptr && acquisition_->granted_);
107 }
108
109 } // namespace simgrid::kernel::actor