Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0a7cc6493260b6c7cec8443e742720e5148294c1
[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   synchro->wait_for(simcall->issuer_, timeout);
21 }
22
23 namespace simgrid {
24 namespace kernel {
25 namespace activity {
26
27 IoImpl& IoImpl::set_timeout(double timeout)
28 {
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);
32   return *this;
33 }
34
35 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
36 {
37   type_ = type;
38   return *this;
39 }
40
41 IoImpl& IoImpl::set_size(sg_size_t size)
42 {
43   size_ = size;
44   return *this;
45 }
46
47 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
48 {
49   disk_ = disk;
50   return *this;
51 }
52
53 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
54 {
55   storage_ = storage;
56   return *this;
57 }
58
59 IoImpl* IoImpl::start()
60 {
61   state_ = State::RUNNING;
62   if (storage_)
63     surf_action_ = storage_->io_start(size_, type_);
64   else
65     surf_action_ = disk_->io_start(size_, type_);
66   surf_action_->set_activity(this);
67
68   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
69   IoImpl::on_start(*this);
70
71   return this;
72 }
73
74 void IoImpl::post()
75 {
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;
80     else
81       state_ = State::CANCELED;
82   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
83     state_ = State::DONE;
84   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
85     state_ = State::TIMEOUT;
86   }
87
88   if (timeout_detector_) {
89     timeout_detector_->unref();
90     timeout_detector_ = nullptr;
91   }
92
93   on_completion(*this);
94
95   /* Answer all simcalls associated with the synchro */
96   finish();
97 }
98
99 void IoImpl::finish()
100 {
101   while (not simcalls_.empty()) {
102     const s_smx_simcall* simcall = simcalls_.front();
103     simcalls_.pop_front();
104     switch (state_) {
105       case State::DONE:
106         /* do nothing, synchro done */
107         break;
108       case State::FAILED:
109         simcall->issuer_->context_->set_wannadie();
110         simcall->issuer_->exception_ =
111             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
112         break;
113       case State::CANCELED:
114         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
115         break;
116       case State::TIMEOUT:
117         XBT_DEBUG("IoImpl::finish(): execution timeouted");
118         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
119         break;
120       default:
121         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
122     }
123
124     simcall->issuer_->waiting_synchro = nullptr;
125     simcall->issuer_->simcall_answer();
126   }
127 }
128
129 /*************
130  * Callbacks *
131  *************/
132 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
133 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
134
135 } // namespace activity
136 } // namespace kernel
137 } // namespace simgrid