Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Split the Comm observers to their own files
[simgrid.git] / src / kernel / actor / CommObserver.cpp
1 /* Copyright (c) 2019-2022. 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/Host.hpp"
7 #include "src/kernel/activity/CommImpl.hpp"
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/SimcallObserver.hpp"
12 #include "src/mc/mc_config.hpp"
13
14 #include <sstream>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(obs_comm, mc_observer, "Logging specific to the Communication simcalls observation");
17
18 namespace simgrid {
19 namespace kernel {
20 namespace actor {
21
22 ActivityTestanySimcall::ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities)
23     : ResultingSimcall(actor, -1), activities_(activities)
24 {
25 }
26
27 int ActivityTestanySimcall::get_max_consider()
28 {
29   indexes_.clear();
30   // list all the activities that are ready
31   for (unsigned i = 0; i < activities_.size(); i++)
32     if (activities_[i]->test(get_issuer()))
33       indexes_.push_back(i);
34   return indexes_.size() + 1;
35 }
36
37 void ActivityTestanySimcall::prepare(int times_considered)
38 {
39   if (times_considered < static_cast<int>(indexes_.size()))
40     next_value_ = indexes_.at(times_considered);
41   else
42     next_value_ = -1;
43 }
44 static void serialize_activity_test(const activity::ActivityImpl* act, std::stringstream& stream)
45 {
46   if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
47     stream << "  " << (short)mc::Transition::Type::COMM_TEST;
48     stream << ' ' << (uintptr_t)comm;
49     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
50     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
51     stream << ' ' << comm->get_mailbox_id();
52     stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
53   } else {
54     stream << (short)mc::Transition::Type::UNKNOWN;
55   }
56 }
57 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
58 {
59   stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
60   for (auto const& act : activities_) {
61     serialize_activity_test(act, stream);
62     stream << ' ';
63   }
64 }
65 void ActivityTestSimcall::serialize(std::stringstream& stream) const
66 {
67   serialize_activity_test(activity_, stream);
68 }
69 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
70 {
71   if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
72     stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
73     stream << timeout << ' ' << (uintptr_t)comm;
74
75     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
76     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
77     stream << ' ' << comm->get_mailbox_id();
78     stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
79   } else {
80     stream << (short)mc::Transition::Type::UNKNOWN;
81   }
82 }
83
84 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
85 {
86   serialize_activity_wait(activity_, timeout_ > 0, stream);
87 }
88 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
89 {
90   stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
91   for (auto const& act : activities_) {
92     serialize_activity_wait(act, timeout_ > 0, stream);
93     stream << ' ';
94   }
95 }
96 ActivityWaitanySimcall::ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
97                                                double timeout)
98     : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
99 {
100 }
101
102 bool ActivityWaitSimcall::is_enabled()
103 {
104   // FIXME: if _sg_mc_timeout == 1 and if we have either a sender or receiver timeout, the transition is enabled
105   // because even if the communication is not ready, it can timeout and won't block.
106
107   return activity_->test(get_issuer());
108 }
109
110 bool ActivityWaitanySimcall::is_enabled()
111 {
112   // list all the activities that are ready
113   indexes_.clear();
114   for (unsigned i = 0; i < activities_.size(); i++)
115     if (activities_[i]->test(get_issuer()))
116       indexes_.push_back(i);
117
118   //  if (_sg_mc_timeout && timeout_)  FIXME: deal with the potential timeout of the WaitAny
119
120   // FIXME: even if the WaitAny has no timeout, some of the activities may still have one.
121   // we should iterate over the vector searching for them
122   return not indexes_.empty();
123 }
124
125 int ActivityWaitanySimcall::get_max_consider()
126 {
127   // list all the activities that are ready
128   indexes_.clear();
129   for (unsigned i = 0; i < activities_.size(); i++)
130     if (activities_[i]->test(get_issuer()))
131       indexes_.push_back(i);
132
133   int res = indexes_.size();
134   //  if (_sg_mc_timeout && timeout_)
135   //    res++;
136
137   return res;
138 }
139
140 void ActivityWaitanySimcall::prepare(int times_considered)
141 {
142   if (times_considered < static_cast<int>(indexes_.size()))
143     next_value_ = indexes_.at(times_considered);
144   else
145     next_value_ = -1;
146 }
147
148 void CommIsendSimcall::serialize(std::stringstream& stream) const
149 {
150   stream << (short)mc::Transition::Type::COMM_SEND << ' ';
151   stream << (uintptr_t)comm_ << ' ' << mbox_->get_id() << ' ' << (uintptr_t)src_buff_ << ' ' << src_buff_size_ << ' '
152          << tag_;
153   XBT_DEBUG("SendObserver comm:%p mbox:%u buff:%p size:%zu tag:%d", comm_, mbox_->get_id(), src_buff_, src_buff_size_,
154             tag_);
155 }
156
157 void CommIrecvSimcall::serialize(std::stringstream& stream) const
158 {
159   stream << (short)mc::Transition::Type::COMM_RECV << ' ';
160   stream << (uintptr_t)comm_ << ' ' << mbox_->get_id() << ' ' << (uintptr_t)dst_buff_ << ' ' << tag_;
161   XBT_DEBUG("RecvObserver comm:%p mbox:%u buff:%p tag:%d", comm_, mbox_->get_id(), dst_buff_, tag_);
162 }
163
164 } // namespace actor
165 } // namespace kernel
166 } // namespace simgrid