Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2021. 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 #include "src/kernel/activity/IoImpl.hpp"
6 #include "mc/mc.h"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/kernel/routing/NetPoint.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Io.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SimcallObserver.hpp"
15 #include "src/kernel/context/Context.hpp"
16 #include "src/kernel/resource/DiskImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
18 #include "src/surf/cpu_interface.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
21
22 namespace simgrid {
23 namespace kernel {
24 namespace activity {
25
26 IoImpl::IoImpl()
27 {
28   piface_ = new s4u::Io(this);
29 }
30
31 IoImpl& IoImpl::set_timeout(double timeout)
32 {
33   const s4u::Host* host = get_disk()->get_host();
34   timeout_detector_     = host->get_cpu()->sleep(timeout);
35   timeout_detector_->set_activity(this);
36   return *this;
37 }
38
39 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
40 {
41   type_ = type;
42   return *this;
43 }
44
45 IoImpl& IoImpl::set_size(sg_size_t size)
46 {
47   size_ = size;
48   return *this;
49 }
50
51 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
52 {
53   disk_ = disk;
54   return *this;
55 }
56
57 IoImpl* IoImpl::start()
58 {
59   state_ = State::RUNNING;
60   surf_action_ =
61       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
62   surf_action_->set_activity(this);
63
64   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
65
66   return this;
67 }
68
69 void IoImpl::post()
70 {
71   performed_ioops_ = surf_action_->get_cost();
72   if (surf_action_->get_state() == resource::Action::State::FAILED) {
73     if (disk_ && not disk_->is_on())
74       state_ = State::FAILED;
75     else
76       state_ = State::CANCELED;
77   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
78     if (surf_action_->get_remains() > 0.0) {
79       surf_action_->set_state(resource::Action::State::FAILED);
80       state_ = State::TIMEOUT;
81     } else {
82       state_ = State::DONE;
83     }
84   } else {
85     state_ = State::DONE;
86   }
87
88   clean_action();
89   if (timeout_detector_) {
90     timeout_detector_->unref();
91     timeout_detector_ = nullptr;
92   }
93
94   /* Answer all simcalls associated with the synchro */
95   finish();
96 }
97
98 void IoImpl::finish()
99 {
100   XBT_DEBUG("IoImpl::finish() in state %s", to_c_str(state_));
101   while (not simcalls_.empty()) {
102     smx_simcall_t simcall = simcalls_.front();
103     simcalls_.pop_front();
104
105     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
106      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
107      * simcall */
108
109     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
110       continue;                                 // if process handling comm is killed
111     if (auto* observer = dynamic_cast<kernel::actor::IoWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
112       const auto& ios = observer->get_ios();
113
114       for (auto* io : ios) {
115         io->unregister_simcall(simcall);
116
117         if (simcall->timeout_cb_) {
118           simcall->timeout_cb_->remove();
119           simcall->timeout_cb_ = nullptr;
120         }
121       }
122
123       if (not MC_is_active() && not MC_record_replay_is_active()) {
124         auto element = std::find(ios.begin(), ios.end(), this);
125         int rank     = element != ios.end() ? static_cast<int>(std::distance(ios.begin(), element)) : -1;
126         observer->set_result(rank);
127       }
128     }
129
130     switch (state_) {
131       case State::FAILED:
132         simcall->issuer_->context_->set_wannadie();
133         piface_->complete(s4u::Activity::State::FAILED);
134         simcall->issuer_->exception_ =
135             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
136         break;
137       case State::CANCELED:
138         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
139         break;
140       case State::TIMEOUT:
141         simcall->issuer_->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
142         break;
143       default:
144         xbt_assert(state_ == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
145                    to_c_str(state_));
146     }
147
148     simcall->issuer_->waiting_synchro_ = nullptr;
149     simcall->issuer_->simcall_answer();
150   }
151 }
152
153 void IoImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<IoImpl*>& ios, double timeout)
154 {
155   if (timeout < 0.0) {
156     issuer->simcall_.timeout_cb_ = nullptr;
157   } else {
158     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &ios]() {
159       issuer->simcall_.timeout_cb_ = nullptr;
160       for (auto* io : ios)
161         io->unregister_simcall(&issuer->simcall_);
162       // default result (-1) is set in actor::IoWaitanySimcall
163       issuer->simcall_answer();
164     });
165   }
166
167   for (auto* io : ios) {
168     /* associate this simcall to the the synchro */
169     io->simcalls_.push_back(&issuer->simcall_);
170
171     /* see if the synchro is already finished */
172     if (io->state_ != State::WAITING && io->state_ != State::RUNNING) {
173       io->finish();
174       break;
175     }
176   }
177 }
178
179 } // namespace activity
180 } // namespace kernel
181 } // namespace simgrid