Logo AND Algorithmique Numérique Distribuée

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