Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' 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     return;
38   }
39
40   /* If the synchro is already finished then perform the error handling */
41   if (synchro->state_ != simgrid::kernel::activity::State::RUNNING)
42     synchro->finish();
43   else {
44     /* we need a sleep action (even when there is no timeout) to be notified of host failures */
45     if (synchro->get_disk() != nullptr)
46       synchro->set_timeout_detector(synchro->get_disk()->get_host()->pimpl_cpu->sleep(timeout));
47     else
48       synchro->set_timeout_detector(
49           simgrid::s4u::Host::by_name(synchro->get_storage()->get_host())->pimpl_cpu->sleep(timeout));
50   }
51 }
52
53 void simcall_HANDLER_io_test(smx_simcall_t simcall, simgrid::kernel::activity::IoImpl* synchro)
54 {
55   bool res = (synchro->state_ != simgrid::kernel::activity::State::WAITING &&
56               synchro->state_ != simgrid::kernel::activity::State::RUNNING);
57   if (res) {
58     synchro->simcalls_.push_back(simcall);
59     synchro->finish();
60   } else {
61     simcall->issuer_->simcall_answer();
62   }
63   simcall_io_test__set__result(simcall, res);
64 }
65
66 namespace simgrid {
67 namespace kernel {
68 namespace activity {
69
70 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
71 {
72   type_ = type;
73   return *this;
74 }
75
76 IoImpl& IoImpl::set_size(sg_size_t size)
77 {
78   size_ = size;
79   return *this;
80 }
81
82 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
83 {
84   disk_ = disk;
85   return *this;
86 }
87
88 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
89 {
90   storage_ = storage;
91   return *this;
92 }
93
94 IoImpl* IoImpl::start()
95 {
96   state_ = State::RUNNING;
97   if (storage_)
98     surf_action_ = storage_->io_start(size_, type_);
99   else
100     surf_action_ = disk_->io_start(size_, type_);
101   surf_action_->set_activity(this);
102
103   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
104   IoImpl::on_start(*this);
105
106   return this;
107 }
108
109 void IoImpl::post()
110 {
111   performed_ioops_ = surf_action_->get_cost();
112   if (surf_action_->get_state() == resource::Action::State::FAILED) {
113     if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
114       state_ = State::FAILED;
115     else
116       state_ = State::CANCELED;
117   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
118     state_ = State::DONE;
119   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
120     state_ = State::TIMEOUT;
121   }
122
123   if (timeout_detector_) {
124     timeout_detector_->unref();
125     timeout_detector_ = nullptr;
126   }
127
128   on_completion(*this);
129
130   /* Answer all simcalls associated with the synchro */
131   finish();
132 }
133
134 void IoImpl::finish()
135 {
136   while (not simcalls_.empty()) {
137     const s_smx_simcall* simcall = simcalls_.front();
138     simcalls_.pop_front();
139     switch (state_) {
140       case State::DONE:
141         /* do nothing, synchro done */
142         break;
143       case State::FAILED:
144         simcall->issuer_->context_->set_wannadie();
145         simcall->issuer_->exception_ =
146             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
147         break;
148       case State::CANCELED:
149         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
150         break;
151       case State::TIMEOUT:
152         XBT_DEBUG("IoImpl::finish(): execution timeouted");
153         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
154         break;
155       default:
156         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
157     }
158
159     simcall->issuer_->waiting_synchro = nullptr;
160     simcall->issuer_->simcall_answer();
161   }
162 }
163
164 /*************
165  * Callbacks *
166  *************/
167 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
168 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
169
170 } // namespace activity
171 } // namespace kernel
172 } // namespace simgrid