Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define ActivityImpl::set_timeout().
[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::wait_for(actor::ActorImpl* issuer, double timeout)
75 {
76   XBT_DEBUG("Wait for execution of synchro %p, state %d", this, (int)state_);
77
78   /* Associate this simcall to the synchro */
79   register_simcall(&issuer->simcall);
80
81   if (MC_is_active() || MC_record_replay_is_active()) {
82     int idx = SIMCALL_GET_MC_VALUE(issuer->simcall);
83     if (idx == 0) {
84       state_ = simgrid::kernel::activity::State::DONE;
85     } else {
86       /* If we reached this point, the wait simcall must have a timeout */
87       /* Otherwise it shouldn't be enabled and executed by the MC */
88       if (timeout < 0.0)
89         THROW_IMPOSSIBLE;
90       state_ = simgrid::kernel::activity::State::TIMEOUT;
91     }
92     finish();
93     return;
94   }
95
96   /* If the synchro is already finished then perform the error handling */
97   if (state_ != simgrid::kernel::activity::State::RUNNING)
98     finish();
99   else {
100     /* we need a sleep action (even when there is no timeout) to be notified of host failures */
101     set_timeout(timeout);
102   }
103 }
104
105 void IoImpl::post()
106 {
107   performed_ioops_ = surf_action_->get_cost();
108   if (surf_action_->get_state() == resource::Action::State::FAILED) {
109     if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
110       state_ = State::FAILED;
111     else
112       state_ = State::CANCELED;
113   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
114     state_ = State::DONE;
115   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
116     state_ = State::TIMEOUT;
117   }
118
119   if (timeout_detector_) {
120     timeout_detector_->unref();
121     timeout_detector_ = nullptr;
122   }
123
124   on_completion(*this);
125
126   /* Answer all simcalls associated with the synchro */
127   finish();
128 }
129
130 void IoImpl::finish()
131 {
132   while (not simcalls_.empty()) {
133     const s_smx_simcall* simcall = simcalls_.front();
134     simcalls_.pop_front();
135     switch (state_) {
136       case State::DONE:
137         /* do nothing, synchro done */
138         break;
139       case State::FAILED:
140         simcall->issuer_->context_->set_wannadie();
141         simcall->issuer_->exception_ =
142             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
143         break;
144       case State::CANCELED:
145         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
146         break;
147       case State::TIMEOUT:
148         XBT_DEBUG("IoImpl::finish(): execution timeouted");
149         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
150         break;
151       default:
152         xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
153     }
154
155     simcall->issuer_->waiting_synchro = nullptr;
156     simcall->issuer_->simcall_answer();
157   }
158 }
159
160 /*************
161  * Callbacks *
162  *************/
163 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
164 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
165
166 } // namespace activity
167 } // namespace kernel
168 } // namespace simgrid