Logo AND Algorithmique Numérique Distribuée

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