Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'depencencies' of https://framagit.org/simgrid/simgrid into depencencies
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2020. 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/s4u/Host.hpp"
10 #include "src/kernel/resource/DiskImpl.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/simix/smx_private.hpp"
13 #include "src/surf/StorageImpl.hpp"
14 #include "src/surf/cpu_interface.hpp"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
17
18 void simcall_HANDLER_io_wait(smx_simcall_t simcall, simgrid::kernel::activity::IoImpl* synchro, double timeout)
19 {
20   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state_);
21
22   /* Associate this simcall to the synchro */
23   synchro->register_simcall(simcall);
24
25   if (MC_is_active() || MC_record_replay_is_active()) {
26     int idx = SIMCALL_GET_MC_VALUE(*simcall);
27     if (idx == 0) {
28       synchro->state_ = simgrid::kernel::activity::State::DONE;
29     } else {
30       /* If we reached this point, the wait simcall must have a timeout */
31       /* Otherwise it shouldn't be enabled and executed by the MC */
32       if (timeout < 0.0)
33         THROW_IMPOSSIBLE;
34       synchro->state_ = simgrid::kernel::activity::State::TIMEOUT;
35     }
36     synchro->finish();
37   }
38
39   /* If the synchro is already finished then perform the error handling */
40   if (synchro->state_ != simgrid::kernel::activity::State::RUNNING)
41     synchro->finish();
42   else {
43     /* we need a sleep action (even when there is no timeout) to be notified of host failures */
44     if (synchro->get_disk() != nullptr)
45       synchro->set_timeout_detector(synchro->get_disk()->get_host()->pimpl_cpu->sleep(timeout));
46     else
47       synchro->set_timeout_detector(
48           simgrid::s4u::Host::by_name(synchro->get_storage()->get_host())->pimpl_cpu->sleep(timeout));
49   }
50 }
51
52 void simcall_HANDLER_io_test(smx_simcall_t simcall, simgrid::kernel::activity::IoImpl* synchro)
53 {
54   bool res = (synchro->state_ != simgrid::kernel::activity::State::WAITING &&
55               synchro->state_ != simgrid::kernel::activity::State::RUNNING);
56   if (res) {
57     synchro->simcalls_.push_back(simcall);
58     synchro->finish();
59   } else {
60     simcall->issuer_->simcall_answer();
61   }
62   simcall_io_test__set__result(simcall, res);
63 }
64
65 namespace simgrid {
66 namespace kernel {
67 namespace activity {
68
69 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
70 {
71   type_ = type;
72   return *this;
73 }
74
75 IoImpl& IoImpl::set_size(sg_size_t size)
76 {
77   size_ = size;
78   return *this;
79 }
80
81 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
82 {
83   disk_ = disk;
84   return *this;
85 }
86
87 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
88 {
89   storage_ = storage;
90   return *this;
91 }
92
93 IoImpl* IoImpl::start()
94 {
95   state_ = State::RUNNING;
96   if (storage_)
97     surf_action_ = storage_->io_start(size_, type_);
98   else
99     surf_action_ = disk_->io_start(size_, type_);
100   surf_action_->set_activity(this);
101
102   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
103   IoImpl::on_start(*this);
104
105   return this;
106 }
107
108 void IoImpl::post()
109 {
110   performed_ioops_ = surf_action_->get_cost();
111   if (surf_action_->get_state() == resource::Action::State::FAILED) {
112     if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
113       state_ = State::FAILED;
114     else
115       state_ = State::CANCELED;
116   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
117     state_ = State::DONE;
118   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
119     state_ = State::TIMEOUT;
120   }
121
122   if (timeout_detector_) {
123     timeout_detector_->unref();
124     timeout_detector_ = nullptr;
125   }
126
127   on_completion(*this);
128
129   /* Answer all simcalls associated with the synchro */
130   finish();
131 }
132
133 void IoImpl::finish()
134 {
135   while (not simcalls_.empty()) {
136     const s_smx_simcall* simcall = simcalls_.front();
137     simcalls_.pop_front();
138     switch (state_) {
139       case State::DONE:
140         /* do nothing, synchro done */
141         break;
142       case State::FAILED:
143         simcall->issuer_->context_->set_wannadie();
144         simcall->issuer_->exception_ =
145             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
146         break;
147       case State::CANCELED:
148         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
149         break;
150       case State::TIMEOUT:
151         XBT_DEBUG("IoImpl::finish(): execution timeouted");
152         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
153         break;
154       default:
155         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
156     }
157
158     simcall->issuer_->waiting_synchro = nullptr;
159     simcall->issuer_->simcall_answer();
160   }
161 }
162
163 /*************
164  * Callbacks *
165  *************/
166 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
167 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
168
169 } // namespace activity
170 } // namespace kernel
171 } // namespace simgrid