Logo AND Algorithmique Numérique Distribuée

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