Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify the way I/O actions are created (CPU style)
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2022. 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
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
10
11 #include "mc/mc.h"
12 #include "src/kernel/activity/IoImpl.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SimcallObserver.hpp"
15 #include "src/kernel/resource/CpuImpl.hpp"
16 #include "src/kernel/resource/DiskImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_io, kernel, "Kernel io-related synchronization");
20
21 namespace simgrid::kernel::activity {
22
23 IoImpl::IoImpl()
24 {
25   piface_ = new s4u::Io(this);
26   actor::ActorImpl* self = actor::ActorImpl::self();
27   if (self) {
28     set_actor(self);
29     self->activities_.emplace_back(this);
30   }
31 }
32
33 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
34 {
35   sharing_penalty_ = sharing_penalty;
36   return *this;
37 }
38
39 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
40 {
41   sharing_penalty_ = sharing_penalty;
42   surf_action_->set_sharing_penalty(sharing_penalty);
43   return *this;
44 }
45
46 IoImpl& IoImpl::set_timeout(double timeout)
47 {
48   const s4u::Host* host = get_disk()->get_host();
49   timeout_detector_     = host->get_cpu()->sleep(timeout);
50   timeout_detector_->set_activity(this);
51   return *this;
52 }
53
54 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
55 {
56   type_ = type;
57   return *this;
58 }
59
60 IoImpl& IoImpl::set_size(sg_size_t size)
61 {
62   size_ = size;
63   return *this;
64 }
65
66 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
67 {
68   disk_ = disk;
69   return *this;
70 }
71
72 IoImpl* IoImpl::start()
73 {
74   set_state(State::RUNNING);
75   surf_action_ = disk_->io_start(size_, type_);
76   surf_action_->set_sharing_penalty(sharing_penalty_);
77   surf_action_->set_activity(this);
78   set_start_time(surf_action_->get_start_time());
79
80   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
81
82   return this;
83 }
84
85 void IoImpl::post()
86 {
87   performed_ioops_ = surf_action_->get_cost();
88   if (surf_action_->get_state() == resource::Action::State::FAILED) {
89     if (disk_ && not disk_->is_on())
90       set_state(State::FAILED);
91     else
92       set_state(State::CANCELED);
93   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
94     if (surf_action_->get_remains() > 0.0) {
95       surf_action_->set_state(resource::Action::State::FAILED);
96       set_state(State::TIMEOUT);
97     } else {
98       set_state(State::DONE);
99     }
100   } else {
101     set_state(State::DONE);
102   }
103
104   clean_action();
105   if (timeout_detector_) {
106     timeout_detector_->unref();
107     timeout_detector_ = nullptr;
108   }
109
110   /* Answer all simcalls associated with the synchro */
111   finish();
112 }
113 void IoImpl::set_exception(actor::ActorImpl* issuer)
114 {
115   switch (get_state()) {
116     case State::FAILED:
117       issuer->set_wannadie();
118       static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
119       issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
120       break;
121     case State::CANCELED:
122       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
123       break;
124     case State::TIMEOUT:
125       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
126       break;
127     default:
128       xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
129                  get_state_str());
130   }
131 }
132
133 void IoImpl::finish()
134 {
135   XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
136   while (not simcalls_.empty()) {
137     actor::Simcall* simcall = simcalls_.front();
138     simcalls_.pop_front();
139
140     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
141      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
142      * simcall */
143
144     if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
145       continue;                                       // if process handling comm is killed
146
147     handle_activity_waitany(simcall);
148
149     set_exception(simcall->issuer_);
150
151     simcall->issuer_->waiting_synchro_ = nullptr;
152     simcall->issuer_->simcall_answer();
153   }
154 }
155
156 } // namespace simgrid::kernel::activity