Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replaced std::list by std::set to keep track of activities
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-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/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
10
11 #include "mc/mc.h"
12 #include "src/kernel/activity/IoImpl.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SimcallObserver.hpp"
15 #include "src/kernel/resource/CpuImpl.hpp"
16 #include "src/kernel/resource/DiskImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
18 #include "src/surf/HostImpl.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_io, kernel, "Kernel io-related synchronization");
21
22 namespace simgrid::kernel::activity {
23
24 IoImpl::IoImpl()
25 {
26   piface_ = new s4u::Io(this);
27   actor::ActorImpl* self = actor::ActorImpl::self();
28   if (self) {
29     set_actor(self);
30     self->activities_.insert(this);
31   }
32 }
33
34 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
35 {
36   sharing_penalty_ = sharing_penalty;
37   return *this;
38 }
39
40 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
41 {
42   sharing_penalty_ = sharing_penalty;
43   surf_action_->set_sharing_penalty(sharing_penalty);
44   return *this;
45 }
46
47 IoImpl& IoImpl::set_timeout(double timeout)
48 {
49   const s4u::Host* host = get_disk()->get_host();
50   timeout_detector_     = host->get_cpu()->sleep(timeout);
51   timeout_detector_->set_activity(this);
52   return *this;
53 }
54
55 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
56 {
57   type_ = type;
58   return *this;
59 }
60
61 IoImpl& IoImpl::set_size(sg_size_t size)
62 {
63   size_ = size;
64   return *this;
65 }
66
67 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
68 {
69   disk_ = disk;
70   return *this;
71 }
72
73 IoImpl& IoImpl::set_host(s4u::Host* host)
74 {
75   host_ = host;
76   return *this;
77 }
78
79 IoImpl& IoImpl::set_dst_disk(resource::DiskImpl* disk)
80 {
81   dst_disk_ = disk;
82   return *this;
83 }
84
85 IoImpl& IoImpl::set_dst_host(s4u::Host* host)
86 {
87   dst_host_ = host;
88   return *this;
89 }
90
91 IoImpl* IoImpl::start()
92 {
93   set_state(State::RUNNING);
94   if (dst_host_ == nullptr) {
95     XBT_DEBUG("Starting regular I/O");
96     surf_action_ = disk_->io_start(size_, type_);
97     surf_action_->set_sharing_penalty(sharing_penalty_);
98   } else {
99     XBT_DEBUG("Starting streaming I/O");
100     auto host_model = dst_host_->get_netpoint()->get_englobing_zone()->get_host_model();
101     surf_action_    = host_model->io_stream(host_, disk_, dst_host_, dst_disk_, size_);
102   }
103
104   surf_action_->set_activity(this);
105   set_start_time(surf_action_->get_start_time());
106
107   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
108
109   return this;
110 }
111
112 void IoImpl::post()
113 {
114   performed_ioops_ = surf_action_->get_cost();
115   if (surf_action_->get_state() == resource::Action::State::FAILED) {
116     if (host_ && dst_host_) { // this is an I/O stream
117       if (not host_->is_on())
118        set_state(State::SRC_HOST_FAILURE);
119       else if (not dst_host_->is_on())
120        set_state(State::DST_HOST_FAILURE);
121     } else if ((disk_ && not disk_->is_on()) || (dst_disk_ && not dst_disk_->is_on()))
122       set_state(State::FAILED);
123     else
124       set_state(State::CANCELED);
125   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
126     if (surf_action_->get_remains() > 0.0) {
127       surf_action_->set_state(resource::Action::State::FAILED);
128       set_state(State::TIMEOUT);
129     } else {
130       set_state(State::DONE);
131     }
132   } else {
133     set_state(State::DONE);
134   }
135
136   clean_action();
137   if (timeout_detector_) {
138     timeout_detector_->unref();
139     timeout_detector_ = nullptr;
140   }
141
142   /* Answer all simcalls associated with the synchro */
143   finish();
144 }
145 void IoImpl::set_exception(actor::ActorImpl* issuer)
146 {
147   switch (get_state()) {
148     case State::FAILED:
149       issuer->set_wannadie();
150       static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
151       issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
152       break;
153     case State::CANCELED:
154       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
155       break;
156     case State::SRC_HOST_FAILURE:
157     case State::DST_HOST_FAILURE:
158        issuer->set_wannadie();
159        static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
160        issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Host failed"));
161       break;
162     case State::TIMEOUT:
163       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
164       break;
165     default:
166       xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
167                  get_state_str());
168   }
169 }
170
171 void IoImpl::finish()
172 {
173   XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
174   while (not simcalls_.empty()) {
175     actor::Simcall* simcall = simcalls_.front();
176     simcalls_.pop_front();
177
178     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
179      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
180      * simcall */
181
182     if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
183       continue;                                       // if process handling comm is killed
184
185     handle_activity_waitany(simcall);
186
187     if (not simcall->issuer_->get_host()->is_on()) {
188       simcall->issuer_->set_wannadie();
189     } else {
190       // Do not answer to dying actors
191       if (not simcall->issuer_->wannadie()) {
192         set_exception(simcall->issuer_);
193         simcall->issuer_->waiting_synchro_ = nullptr;
194         simcall->issuer_->simcall_answer();
195       }
196     }
197   }
198 }
199
200 } // namespace simgrid::kernel::activity