Logo AND Algorithmique Numérique Distribuée

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