1 /* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved. */
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"
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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
18 void simcall_HANDLER_io_wait(smx_simcall_t simcall, simgrid::kernel::activity::IoImpl* synchro, double timeout)
20 synchro->wait_for(simcall->issuer_, timeout);
27 IoImpl& IoImpl::set_timeout(double timeout)
29 s4u::Host* host = get_disk() ? get_disk()->get_host() : s4u::Host::by_name(get_storage()->get_host());
30 timeout_detector_ = host->pimpl_cpu->sleep(timeout);
31 timeout_detector_->set_activity(this);
35 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
41 IoImpl& IoImpl::set_size(sg_size_t size)
47 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
53 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
59 IoImpl* IoImpl::start()
61 state_ = State::RUNNING;
63 surf_action_ = storage_->io_start(size_, type_);
65 surf_action_ = disk_->io_start(size_, type_);
66 surf_action_->set_activity(this);
68 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
69 IoImpl::on_start(*this);
76 performed_ioops_ = surf_action_->get_cost();
77 if (surf_action_->get_state() == resource::Action::State::FAILED) {
78 if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
79 state_ = State::FAILED;
81 state_ = State::CANCELED;
82 } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
84 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
85 state_ = State::TIMEOUT;
88 if (timeout_detector_) {
89 timeout_detector_->unref();
90 timeout_detector_ = nullptr;
95 /* Answer all simcalls associated with the synchro */
101 while (not simcalls_.empty()) {
102 const s_smx_simcall* simcall = simcalls_.front();
103 simcalls_.pop_front();
106 /* do nothing, synchro done */
109 simcall->issuer_->context_->set_wannadie();
110 simcall->issuer_->exception_ =
111 std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
113 case State::CANCELED:
114 simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
117 XBT_DEBUG("IoImpl::finish(): execution timeouted");
118 simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
121 xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
124 simcall->issuer_->waiting_synchro = nullptr;
125 simcall->issuer_->simcall_answer();
132 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
133 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
135 } // namespace activity
136 } // namespace kernel
137 } // namespace simgrid