Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not clean twice in Activity::post(), so that even test() can call it
[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_type(s4u::Io::OpType type)
42 {
43   type_ = type;
44   return *this;
45 }
46
47 IoImpl& IoImpl::set_size(sg_size_t size)
48 {
49   size_ = size;
50   return *this;
51 }
52
53 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
54 {
55   disk_ = disk;
56   return *this;
57 }
58
59 IoImpl& IoImpl::set_host(s4u::Host* host)
60 {
61   host_ = host;
62   return *this;
63 }
64
65 IoImpl& IoImpl::set_dst_disk(resource::DiskImpl* disk)
66 {
67   dst_disk_ = disk;
68   return *this;
69 }
70
71 IoImpl& IoImpl::set_dst_host(s4u::Host* host)
72 {
73   dst_host_ = host;
74   return *this;
75 }
76
77 IoImpl* IoImpl::start()
78 {
79   set_state(State::RUNNING);
80   if (dst_host_ == nullptr) {
81     XBT_DEBUG("Starting regular I/O");
82     model_action_ = disk_->io_start(size_, type_);
83     model_action_->set_sharing_penalty(sharing_penalty_);
84   } else {
85     XBT_DEBUG("Starting streaming I/O");
86     auto host_model = dst_host_->get_netpoint()->get_englobing_zone()->get_host_model();
87     model_action_   = host_model->io_stream(host_, disk_, dst_host_, dst_disk_, size_);
88   }
89
90   model_action_->set_activity(this);
91   set_start_time(model_action_->get_start_time());
92
93   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
94
95   return this;
96 }
97
98 void IoImpl::post()
99 {
100   if (model_action_ != nullptr) {
101     performed_ioops_ = model_action_->get_cost();
102     if (model_action_->get_state() == resource::Action::State::FAILED) {
103       if (host_ && dst_host_) { // this is an I/O stream
104         if (not host_->is_on())
105           set_state(State::SRC_HOST_FAILURE);
106         else if (not dst_host_->is_on())
107           set_state(State::DST_HOST_FAILURE);
108       } else if ((disk_ && not disk_->is_on()) || (dst_disk_ && not dst_disk_->is_on()))
109         set_state(State::FAILED);
110       else
111         set_state(State::CANCELED);
112     } else {
113       set_state(State::DONE);
114     }
115
116     clean_action();
117   }
118
119   /* Answer all simcalls associated with the synchro */
120   finish();
121 }
122 void IoImpl::set_exception(actor::ActorImpl* issuer)
123 {
124   switch (get_state()) {
125     case State::FAILED:
126       issuer->set_wannadie();
127       static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
128       issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
129       break;
130     case State::CANCELED:
131       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
132       break;
133     case State::SRC_HOST_FAILURE:
134     case State::DST_HOST_FAILURE:
135        issuer->set_wannadie();
136        static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
137        issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Host failed"));
138       break;
139     case State::TIMEOUT:
140       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
141       break;
142     default:
143       xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
144                  get_state_str());
145   }
146 }
147
148 void IoImpl::finish()
149 {
150   XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
151   while (not simcalls_.empty()) {
152     actor::Simcall* simcall = simcalls_.front();
153     simcalls_.pop_front();
154
155     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
156      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
157      * simcall */
158
159     if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
160       continue;                                       // if process handling comm is killed
161
162     handle_activity_waitany(simcall);
163
164     if (not simcall->issuer_->get_host()->is_on()) {
165       simcall->issuer_->set_wannadie();
166     } else {
167       // Do not answer to dying actors
168       if (not simcall->issuer_->wannadie()) {
169         set_exception(simcall->issuer_);
170         simcall->issuer_->waiting_synchro_ = nullptr;
171         simcall->issuer_->simcall_answer();
172       }
173     }
174   }
175 }
176
177 } // namespace simgrid::kernel::activity