Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Concatenate nested namespaces (sonar).
[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_ =
76       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
77   surf_action_->set_sharing_penalty(sharing_penalty_);
78   surf_action_->set_activity(this);
79   set_start_time(surf_action_->get_start_time());
80
81   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
82
83   return this;
84 }
85
86 void IoImpl::post()
87 {
88   performed_ioops_ = surf_action_->get_cost();
89   if (surf_action_->get_state() == resource::Action::State::FAILED) {
90     if (disk_ && not disk_->is_on())
91       set_state(State::FAILED);
92     else
93       set_state(State::CANCELED);
94   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
95     if (surf_action_->get_remains() > 0.0) {
96       surf_action_->set_state(resource::Action::State::FAILED);
97       set_state(State::TIMEOUT);
98     } else {
99       set_state(State::DONE);
100     }
101   } else {
102     set_state(State::DONE);
103   }
104
105   clean_action();
106   if (timeout_detector_) {
107     timeout_detector_->unref();
108     timeout_detector_ = nullptr;
109   }
110
111   /* Answer all simcalls associated with the synchro */
112   finish();
113 }
114 void IoImpl::set_exception(actor::ActorImpl* issuer)
115 {
116   switch (get_state()) {
117     case State::FAILED:
118       issuer->set_wannadie();
119       static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
120       issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
121       break;
122     case State::CANCELED:
123       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
124       break;
125     case State::TIMEOUT:
126       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
127       break;
128     default:
129       xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
130                  get_state_str());
131   }
132 }
133
134 void IoImpl::finish()
135 {
136   XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
137   while (not simcalls_.empty()) {
138     actor::Simcall* simcall = simcalls_.front();
139     simcalls_.pop_front();
140
141     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
142      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
143      * simcall */
144
145     if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
146       continue;                                       // if process handling comm is killed
147
148     handle_activity_waitany(simcall);
149
150     set_exception(simcall->issuer_);
151
152     simcall->issuer_->waiting_synchro_ = nullptr;
153     simcall->issuer_->simcall_answer();
154   }
155 }
156
157 } // namespace simgrid::kernel::activity