Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I/O factors: noise for disks.
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2021. 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/kernel/routing/NetPoint.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Io.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/resource/DiskImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "src/simix/smx_private.hpp"
17 #include "src/surf/cpu_interface.hpp"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
20
21 namespace simgrid {
22 namespace kernel {
23 namespace activity {
24
25 IoImpl::IoImpl()
26 {
27   piface_ = new s4u::Io(this);
28 }
29
30 IoImpl& IoImpl::set_timeout(double timeout)
31 {
32   const s4u::Host* host = get_disk()->get_host();
33   timeout_detector_     = host->get_cpu()->sleep(timeout);
34   timeout_detector_->set_activity(this);
35   return *this;
36 }
37
38 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
39 {
40   type_ = type;
41   return *this;
42 }
43
44 IoImpl& IoImpl::set_size(sg_size_t size)
45 {
46   size_ = size;
47   return *this;
48 }
49
50 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
51 {
52   disk_ = disk;
53   return *this;
54 }
55
56 IoImpl* IoImpl::start()
57 {
58   state_ = State::RUNNING;
59   surf_action_ =
60       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
61   surf_action_->set_activity(this);
62   const auto& factor_cb = disk_->get_factor_cb();
63   if (factor_cb) { // handling disk variability
64     surf_action_->set_rate_factor(factor_cb(size_, type_));
65   }
66
67   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
68
69   return this;
70 }
71
72 void IoImpl::post()
73 {
74   performed_ioops_ = surf_action_->get_cost();
75   if (surf_action_->get_state() == resource::Action::State::FAILED) {
76     if (disk_ && not disk_->is_on())
77       state_ = State::FAILED;
78     else
79       state_ = State::CANCELED;
80   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
81     if (surf_action_->get_remains() > 0.0) {
82       surf_action_->set_state(resource::Action::State::FAILED);
83       state_ = State::TIMEOUT;
84     } else {
85       state_ = State::DONE;
86     }
87   } else {
88     state_ = State::DONE;
89   }
90
91   clean_action();
92   if (timeout_detector_) {
93     timeout_detector_->unref();
94     timeout_detector_ = nullptr;
95   }
96
97   /* Answer all simcalls associated with the synchro */
98   finish();
99 }
100
101 void IoImpl::finish()
102 {
103   XBT_DEBUG("IoImpl::finish() in state %s", to_c_str(state_));
104   while (not simcalls_.empty()) {
105     smx_simcall_t simcall = simcalls_.front();
106     simcalls_.pop_front();
107
108     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
109      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
110      * simcall */
111
112     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
113       continue;                                 // if process handling comm is killed
114     if (auto* observer = dynamic_cast<kernel::actor::IoWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
115       const auto& ios = observer->get_ios();
116
117       for (auto* io : ios) {
118         io->unregister_simcall(simcall);
119
120         if (simcall->timeout_cb_) {
121           simcall->timeout_cb_->remove();
122           simcall->timeout_cb_ = nullptr;
123         }
124       }
125
126       if (not MC_is_active() && not MC_record_replay_is_active()) {
127         auto element = std::find(ios.begin(), ios.end(), this);
128         int rank     = element != ios.end() ? static_cast<int>(std::distance(ios.begin(), element)) : -1;
129         observer->set_result(rank);
130       }
131     }
132
133     switch (state_) {
134       case State::FAILED:
135         simcall->issuer_->context_->set_wannadie();
136         simcall->issuer_->exception_ =
137             std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
138         break;
139       case State::CANCELED:
140         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
141         break;
142       case State::TIMEOUT:
143         simcall->issuer_->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
144         break;
145       default:
146         xbt_assert(state_ == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
147                    to_c_str(state_));
148     }
149
150     simcall->issuer_->waiting_synchro_ = nullptr;
151     simcall->issuer_->simcall_answer();
152   }
153 }
154
155 void IoImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<IoImpl*>& ios, double timeout)
156 {
157   if (timeout < 0.0) {
158     issuer->simcall_.timeout_cb_ = nullptr;
159   } else {
160     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &ios]() {
161       issuer->simcall_.timeout_cb_ = nullptr;
162       for (auto* io : ios)
163         io->unregister_simcall(&issuer->simcall_);
164       // default result (-1) is set in actor::IoWaitanySimcall
165       issuer->simcall_answer();
166     });
167   }
168
169   for (auto* io : ios) {
170     /* associate this simcall to the the synchro */
171     io->simcalls_.push_back(&issuer->simcall_);
172
173     /* see if the synchro is already finished */
174     if (io->state_ != State::WAITING && io->state_ != State::RUNNING) {
175       io->finish();
176       break;
177     }
178   }
179 }
180
181 } // namespace activity
182 } // namespace kernel
183 } // namespace simgrid