Logo AND Algorithmique Numérique Distribuée

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